diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/url.js b/src/legacy/core_plugins/kibana/common/field_formats/types/url.js index 4058ef61bbeb6..c2e5d9205be0b 100644 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/url.js +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/url.js @@ -18,7 +18,7 @@ */ import _ from 'lodash'; -import { getHighlightHtml } from '../../highlight/highlight_html'; +import { getHighlightHtml } from '../../../../../../plugins/data/common/highlight/highlight_html'; const templateMatchRE = /{{([\s\S]+?)}}/g; const whitelistUrlSchemes = ['http://', 'https://']; diff --git a/src/legacy/core_plugins/kibana/common/highlight/__tests__/highlight_html.js b/src/legacy/core_plugins/kibana/common/highlight/__tests__/highlight_html.js deleted file mode 100644 index 0fdec31ec0d00..0000000000000 --- a/src/legacy/core_plugins/kibana/common/highlight/__tests__/highlight_html.js +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import expect from '@kbn/expect'; -import { highlightTags } from '../highlight_tags'; -import { htmlTags } from '../html_tags'; -import { getHighlightHtml } from '../highlight_html'; - -describe('getHighlightHtml', function () { - const text = '' + - 'Bacon ipsum dolor amet pork loin pork cow pig beef chuck ground round shankle sirloin landjaeger kevin ' + - 'venison sausage ribeye tongue. Chicken bacon ball tip pork. Brisket pork capicola spare ribs pastrami rump ' + - 'sirloin, t-bone ham shoulder jerky turducken bresaola. Chicken cow beef picanha. Picanha hamburger alcatra ' + - 'cupim. Salami capicola boudin pork belly shank picanha.'; - - it('should not modify text if highlight is empty', function () { - expect(getHighlightHtml(text, undefined)).to.be(text); - expect(getHighlightHtml(text, null)).to.be(text); - expect(getHighlightHtml(text, [])).to.be(text); - }); - - it('should preserve escaped text', function () { - const highlights = ['']; - const result = getHighlightHtml('<foo>', highlights); - expect(result.indexOf('')).to.be(-1); - expect(result.indexOf('<foo>')).to.be.greaterThan(-1); - }); - - it('should highlight a single result', function () { - const highlights = [ - highlightTags.pre + 'hamburger' + highlightTags.post + ' alcatra cupim. Salami capicola boudin pork belly shank picanha.' - ]; - const result = getHighlightHtml(text, highlights); - expect(result.indexOf(htmlTags.pre + 'hamburger' + htmlTags.post)).to.be.greaterThan(-1); - expect(result.split(htmlTags.pre + 'hamburger' + htmlTags.post).length).to.be(text.split('hamburger').length); - }); - - it('should highlight multiple results', function () { - const highlights = [ - 'kevin venison sausage ribeye tongue. ' + highlightTags.pre + 'Chicken' + highlightTags.post + ' bacon ball tip pork. Brisket ' + - 'pork capicola spare ribs pastrami rump sirloin, t-bone ham shoulder jerky turducken bresaola. ' + highlightTags.pre + - 'Chicken' + highlightTags.post + ' cow beef picanha. Picanha' - ]; - const result = getHighlightHtml(text, highlights); - expect(result.indexOf(htmlTags.pre + 'Chicken' + htmlTags.post)).to.be.greaterThan(-1); - expect(result.split(htmlTags.pre + 'Chicken' + htmlTags.post).length).to.be(text.split('Chicken').length); - }); - - it('should highlight multiple hits in a result', function () { - const highlights = [ - 'Bacon ipsum dolor amet ' + highlightTags.pre + 'pork' + highlightTags.post + ' loin ' + - '' + highlightTags.pre + 'pork' + highlightTags.post + ' cow pig beef chuck ground round shankle ' + - 'sirloin landjaeger', - 'kevin venison sausage ribeye tongue. Chicken bacon ball tip ' + - '' + highlightTags.pre + 'pork' + highlightTags.post + '. Brisket ' + - '' + highlightTags.pre + 'pork' + highlightTags.post + ' capicola spare ribs', - 'hamburger alcatra cupim. Salami capicola boudin ' + highlightTags.pre + 'pork' + highlightTags.post + ' ' + - 'belly shank picanha.' - ]; - const result = getHighlightHtml(text, highlights); - expect(result.indexOf(htmlTags.pre + 'pork' + htmlTags.post)).to.be.greaterThan(-1); - expect(result.split(htmlTags.pre + 'pork' + htmlTags.post).length).to.be(text.split('pork').length); - }); - - it('should accept an object and return a string containing its properties', function () { - const obj = { foo: 1, bar: 2 }; - const result = getHighlightHtml(obj, null); - expect(result.indexOf('' + obj)).to.be(-1); - expect(result.indexOf('foo')).to.be.greaterThan(-1); - expect(result.indexOf('bar')).to.be.greaterThan(-1); - }); -}); diff --git a/src/legacy/ui/field_formats/content_types/html_content_type.ts b/src/legacy/ui/field_formats/content_types/html_content_type.ts index 21cdb21d16895..3253ea5f60126 100644 --- a/src/legacy/ui/field_formats/content_types/html_content_type.ts +++ b/src/legacy/ui/field_formats/content_types/html_content_type.ts @@ -21,8 +21,7 @@ import { FieldFormatConvert, IFieldFormat, HtmlConventTypeConvert } from '../typ // @ts-ignore import { asPrettyString } from '../../../core_plugins/kibana/common/utils/as_pretty_string'; -// @ts-ignore -import { getHighlightHtml } from '../../../core_plugins/kibana/common/highlight/highlight_html'; +import { getHighlightHtml } from '../../../../plugins/data/common/highlight/highlight_html'; const CONTEXT_TYPE = 'html'; diff --git a/src/legacy/ui/public/courier/search_source/search_source.js b/src/legacy/ui/public/courier/search_source/search_source.js index 4a2c441012b50..17db0c941a21d 100644 --- a/src/legacy/ui/public/courier/search_source/search_source.js +++ b/src/legacy/ui/public/courier/search_source/search_source.js @@ -80,7 +80,7 @@ import { SearchRequestProvider } from '../fetch/request'; import { searchRequestQueue } from '../search_request_queue'; import { FetchSoonProvider } from '../fetch'; import { FieldWildcardProvider } from '../../field_wildcard'; -import { getHighlightRequest } from '../../../../core_plugins/kibana/common/highlight'; +import { getHighlightRequest } from '../../../../../plugins/data/common/highlight'; const FIELDS = [ 'type', diff --git a/src/plugins/data/common/highlight/highlight_html.test.ts b/src/plugins/data/common/highlight/highlight_html.test.ts new file mode 100644 index 0000000000000..6ada630851c1d --- /dev/null +++ b/src/plugins/data/common/highlight/highlight_html.test.ts @@ -0,0 +1,124 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { highlightTags } from './highlight_tags'; +import { htmlTags } from './html_tags'; +import { getHighlightHtml } from './highlight_html'; + +describe('getHighlightHtml', function() { + const text = + '' + + 'Bacon ipsum dolor amet pork loin pork cow pig beef chuck ground round shankle sirloin landjaeger kevin ' + + 'venison sausage ribeye tongue. Chicken bacon ball tip pork. Brisket pork capicola spare ribs pastrami rump ' + + 'sirloin, t-bone ham shoulder jerky turducken bresaola. Chicken cow beef picanha. Picanha hamburger alcatra ' + + 'cupim. Salami capicola boudin pork belly shank picanha.'; + + test('should not modify text if highlight is empty', function() { + expect(getHighlightHtml(text, undefined)).toBe(text); + expect(getHighlightHtml(text, null)).toBe(text); + expect(getHighlightHtml(text, [])).toBe(text); + }); + + test('should preserve escaped text', function() { + const highlights = ['']; + const result = getHighlightHtml('<foo>', highlights); + expect(result.indexOf('')).toBe(-1); + expect(result.indexOf('<foo>')).toBeGreaterThan(-1); + }); + + test('should highlight a single result', function() { + const highlights = [ + highlightTags.pre + + 'hamburger' + + highlightTags.post + + ' alcatra cupim. Salami capicola boudin pork belly shank picanha.', + ]; + const result = getHighlightHtml(text, highlights); + expect(result.indexOf(htmlTags.pre + 'hamburger' + htmlTags.post)).toBeGreaterThan(-1); + expect(result.split(htmlTags.pre + 'hamburger' + htmlTags.post).length).toBe( + text.split('hamburger').length + ); + }); + + test('should highlight multiple results', function() { + const highlights = [ + 'kevin venison sausage ribeye tongue. ' + + highlightTags.pre + + 'Chicken' + + highlightTags.post + + ' bacon ball tip pork. Brisket ' + + 'pork capicola spare ribs pastrami rump sirloin, t-bone ham shoulder jerky turducken bresaola. ' + + highlightTags.pre + + 'Chicken' + + highlightTags.post + + ' cow beef picanha. Picanha', + ]; + const result = getHighlightHtml(text, highlights); + expect(result.indexOf(htmlTags.pre + 'Chicken' + htmlTags.post)).toBeGreaterThan(-1); + expect(result.split(htmlTags.pre + 'Chicken' + htmlTags.post).length).toBe( + text.split('Chicken').length + ); + }); + + test('should highlight multiple hits in a result', function() { + const highlights = [ + 'Bacon ipsum dolor amet ' + + highlightTags.pre + + 'pork' + + highlightTags.post + + ' loin ' + + '' + + highlightTags.pre + + 'pork' + + highlightTags.post + + ' cow pig beef chuck ground round shankle ' + + 'sirloin landjaeger', + 'kevin venison sausage ribeye tongue. Chicken bacon ball tip ' + + '' + + highlightTags.pre + + 'pork' + + highlightTags.post + + '. Brisket ' + + '' + + highlightTags.pre + + 'pork' + + highlightTags.post + + ' capicola spare ribs', + 'hamburger alcatra cupim. Salami capicola boudin ' + + highlightTags.pre + + 'pork' + + highlightTags.post + + ' ' + + 'belly shank picanha.', + ]; + const result = getHighlightHtml(text, highlights); + expect(result.indexOf(htmlTags.pre + 'pork' + htmlTags.post)).toBeGreaterThan(-1); + expect(result.split(htmlTags.pre + 'pork' + htmlTags.post).length).toBe( + text.split('pork').length + ); + }); + + test('should accept an object and return a string containing its properties', function() { + const obj = { foo: 1, bar: 2 }; + const result = getHighlightHtml(obj, null); + expect(result.indexOf('' + obj)).toBe(-1); + expect(result.indexOf('foo')).toBeGreaterThan(-1); + expect(result.indexOf('bar')).toBeGreaterThan(-1); + }); +}); diff --git a/src/legacy/core_plugins/kibana/common/highlight/highlight_html.js b/src/plugins/data/common/highlight/highlight_html.ts similarity index 76% rename from src/legacy/core_plugins/kibana/common/highlight/highlight_html.js rename to src/plugins/data/common/highlight/highlight_html.ts index fe0d28eb1f923..c7c092941728e 100644 --- a/src/legacy/core_plugins/kibana/common/highlight/highlight_html.js +++ b/src/plugins/data/common/highlight/highlight_html.ts @@ -21,23 +21,25 @@ import _ from 'lodash'; import { highlightTags } from './highlight_tags'; import { htmlTags } from './html_tags'; -export function getHighlightHtml(fieldValue, highlights) { - let highlightHtml = (typeof fieldValue === 'object') - ? JSON.stringify(fieldValue) - : fieldValue; +export function getHighlightHtml(fieldValue: any, highlights: any) { + let highlightHtml = typeof fieldValue === 'object' ? JSON.stringify(fieldValue) : fieldValue; - _.each(highlights, function (highlight) { + _.each(highlights, function(highlight) { const escapedHighlight = _.escape(highlight); // Strip out the highlight tags to compare against the field text const untaggedHighlight = escapedHighlight - .split(highlightTags.pre).join('') - .split(highlightTags.post).join(''); + .split(highlightTags.pre) + .join('') + .split(highlightTags.post) + .join(''); // Replace all highlight tags with proper html tags const taggedHighlight = escapedHighlight - .split(highlightTags.pre).join(htmlTags.pre) - .split(highlightTags.post).join(htmlTags.post); + .split(highlightTags.pre) + .join(htmlTags.pre) + .split(highlightTags.post) + .join(htmlTags.post); // Replace all instances of the untagged string with the properly tagged string highlightHtml = highlightHtml.split(untaggedHighlight).join(taggedHighlight); diff --git a/src/legacy/core_plugins/kibana/common/highlight/__tests__/highlight_request.js b/src/plugins/data/common/highlight/highlight_request.test.ts similarity index 67% rename from src/legacy/core_plugins/kibana/common/highlight/__tests__/highlight_request.js rename to src/plugins/data/common/highlight/highlight_request.test.ts index f7015ee333f6c..5312f1be6c26c 100644 --- a/src/legacy/core_plugins/kibana/common/highlight/__tests__/highlight_request.js +++ b/src/plugins/data/common/highlight/highlight_request.test.ts @@ -17,40 +17,39 @@ * under the License. */ -import expect from '@kbn/expect'; -import { getHighlightRequest } from '../highlight_request'; +import { getHighlightRequest } from './highlight_request'; describe('getHighlightRequest', () => { - let configMock; - const getConfig = (key) => configMock[key]; + let configMock: Record; + const getConfig = (key: string) => configMock[key]; const queryStringQuery = { query_string: { query: 'foo' } }; - beforeEach(function () { + beforeEach(function() { configMock = {}; configMock['doc_table:highlight'] = true; }); - it('should be a function', () => { - expect(getHighlightRequest).to.be.a(Function); + test('should be a function', () => { + expect(getHighlightRequest).toBeInstanceOf(Function); }); - it('should not modify the original query', () => { + test('should not modify the original query', () => { getHighlightRequest(queryStringQuery, getConfig); - expect(queryStringQuery.query_string).to.not.have.property('highlight'); + expect(queryStringQuery.query_string).not.toHaveProperty('highlight'); }); - it('should return undefined if highlighting is turned off', () => { + test('should return undefined if highlighting is turned off', () => { configMock['doc_table:highlight'] = false; const request = getHighlightRequest(queryStringQuery, getConfig); - expect(request).to.be(undefined); + expect(request).toBe(undefined); }); - it('should enable/disable highlighting if config is changed', () => { + test('should enable/disable highlighting if config is changed', () => { let request = getHighlightRequest(queryStringQuery, getConfig); - expect(request).to.not.be(undefined); + expect(request).not.toBe(undefined); configMock['doc_table:highlight'] = false; request = getHighlightRequest(queryStringQuery, getConfig); - expect(request).to.be(undefined); + expect(request).toBe(undefined); }); }); diff --git a/src/legacy/core_plugins/kibana/common/highlight/highlight_request.js b/src/plugins/data/common/highlight/highlight_request.ts similarity index 90% rename from src/legacy/core_plugins/kibana/common/highlight/highlight_request.js rename to src/plugins/data/common/highlight/highlight_request.ts index 29d73b699d300..199a73e692e39 100644 --- a/src/legacy/core_plugins/kibana/common/highlight/highlight_request.js +++ b/src/plugins/data/common/highlight/highlight_request.ts @@ -21,15 +21,15 @@ import { highlightTags } from './highlight_tags'; const FRAGMENT_SIZE = Math.pow(2, 31) - 1; // Max allowed value for fragment_size (limit of a java int) -export function getHighlightRequest(query, getConfig) { +export function getHighlightRequest(query: any, getConfig: Function) { if (!getConfig('doc_table:highlight')) return; return { pre_tags: [highlightTags.pre], post_tags: [highlightTags.post], fields: { - '*': {} + '*': {}, }, - fragment_size: FRAGMENT_SIZE + fragment_size: FRAGMENT_SIZE, }; } diff --git a/src/legacy/core_plugins/kibana/common/highlight/highlight_tags.js b/src/plugins/data/common/highlight/highlight_tags.ts similarity index 96% rename from src/legacy/core_plugins/kibana/common/highlight/highlight_tags.js rename to src/plugins/data/common/highlight/highlight_tags.ts index 0105c7314a76b..851c8e4b17808 100644 --- a/src/legacy/core_plugins/kibana/common/highlight/highlight_tags.js +++ b/src/plugins/data/common/highlight/highlight_tags.ts @@ -22,5 +22,5 @@ // inside a field value. export const highlightTags = { pre: '@kibana-highlighted-field@', - post: '@/kibana-highlighted-field@' + post: '@/kibana-highlighted-field@', }; diff --git a/src/legacy/core_plugins/kibana/common/highlight/html_tags.js b/src/plugins/data/common/highlight/html_tags.ts similarity index 97% rename from src/legacy/core_plugins/kibana/common/highlight/html_tags.js rename to src/plugins/data/common/highlight/html_tags.ts index d09b83bc6186a..ef40ccf589b26 100644 --- a/src/legacy/core_plugins/kibana/common/highlight/html_tags.js +++ b/src/plugins/data/common/highlight/html_tags.ts @@ -20,5 +20,5 @@ // These are the html tags that will replace the highlight tags. export const htmlTags = { pre: '', - post: '' + post: '', }; diff --git a/src/legacy/core_plugins/kibana/common/highlight/index.js b/src/plugins/data/common/highlight/index.ts similarity index 100% rename from src/legacy/core_plugins/kibana/common/highlight/index.js rename to src/plugins/data/common/highlight/index.ts