import { after, Assert, before, describe, it } from '@ephox/bedrock-client'; import { Arr, Fun } from '@ephox/katamari'; import { Attribute, Html, Insert, InsertAll, Remove, SugarElement } from '@ephox/sugar'; import * as DomSearch from 'ephox/phoenix/api/dom/DomSearch'; import * as DomWrapping from 'ephox/phoenix/api/dom/DomWrapping'; interface EntitiesMap { readonly [name: string]: string; } describe('browser.phoenix.api.DomSearchTest', () => { let container: SugarElement; before(() => { const body = SugarElement.fromDom(document.body); container = SugarElement.fromTag('div'); Insert.append(body, container); }); // Taken from: modules/tinymce/src/core/main/ts/api/html/Entities.ts, makes tests easier to read const decode = (html: string) => { const entityRegExp = /&#([a-z0-9]+);?|&([a-z0-9]+);/gi; const reverseEntities: EntitiesMap = { '<': '<', '>': '>', '&': '&', '"': '"', ''': `'` }; return html.replace(entityRegExp, (all) => reverseEntities[all]); }; after(() => Remove.remove(container)); const check = (expected: string, rawTexts: string[], words: string[]) => { const elements = Arr.map(rawTexts, (x) => SugarElement.fromText(x)); Remove.empty(container); InsertAll.append(container, elements); const snapshots = DomSearch.safeWords(elements, words, Fun.never); Arr.each(snapshots, (x) => { DomWrapping.wrapper(x.elements, () => { const span = SugarElement.fromTag('span'); Attribute.set(span, 'data-word', x.word); return DomWrapping.nu(span); }); }); Assert.eq(`Asserting result for word: ${rawTexts.join('')},`, expected, decode(Html.get(container))); }; it('TINY-10062: Search for text in DOM', () => { check('Sed', [ 'Sed' ], [ 'Sed' ]); check('Sed ut perspiciatis unde' + ' omnis iste natus error sit voluptatem', [ 'Sed', ' ut per', 'spiciatis u', 'nde om', 'ni', 's iste', ' natus', ' error', ' sit voluptatem' ], [ 'Sed', 'iste', 'unde', 'sit' ]); check('Sed ut per', [ 'Sed', ' ut per' ], [ 'Sed', 'ut' ]); check( [ '

', 'Hello', 'World</p>' ].join(' '), [ '

Hello World

' ], [ '

', 'Hello', 'World<', 'p>' ] ); // No word boundaries before the space between the text within the element, so we just pass

Hello check( [ '

Hello', 'World</p>' ].join(' '), [ '

Hello World

' ], [ '

Hello', 'World<', 'p>' ] ); check( [ '

', 'Hello', 'World', '</p>' ].join(' '), [ '

Hello World

' ], [ '

', 'Hello', 'World', '<', 'p>' ] ); check( [ '

Hello', 'World', '</p>' ].join(' '), [ '

Hello World

' ], [ '

Hello', 'World', '<', 'p>' ] ); check( [ 'Test.', '[IF:INTEXT]Test2', '[/IF]' ].join(' '), [ 'Test. [IF:INTEXT]Test2 [/IF]' ], [ 'IF:INTEXT', 'IF' ] ); check( [ 'Test.', '[/IF]Test2', '[IF:INTEXT]' ].join(' '), [ 'Test. [/IF]Test2 [IF:INTEXT]' ], [ 'IF', 'IF:INTEXT' ] ); }); });