import { Assert, UnitTest } from '@ephox/bedrock-client'; import { Arr } from '@ephox/katamari'; import { type SugarElement, SugarText, Traverse } from '@ephox/sugar'; import * as DomSplit from 'ephox/phoenix/api/dom/DomSplit'; import { Page } from '../module/ephox/phoenix/test/Page'; UnitTest.test('DomSplitTest', () => { /* {t1:First paragraph}{t2:Second }{t3:here}{t4: is }{t5:something} {t6:More data} {t7:Next }{t8:Section }{t9:no}{t10:w} */ const check = (expected: string[], element: SugarElement) => { const parent = Traverse.parent(element); parent.fold(() => { throw new Error('Element must have parent for test to work'); }, (v) => { const children = Traverse.children(v) as SugarElement[]; const text = Arr.map(children, SugarText.get); Assert.eq('', expected, text); }); }; const checkSplitByPair = (expected: string[], element: SugarElement, start: number, end: number) => { DomSplit.splitByPair(element, start, end); check(expected, element); }; const checkSplit = (expected: string[], element: SugarElement, offset: number) => { DomSplit.split(element, offset); check(expected, element); }; checkSplit([ 'no', 'w' ], Page().t9, 2); checkSplit([ 'no', 'w' ], Page().t9, 0); checkSplit([ 'n', 'o', 'w' ], Page().t9, 1); checkSplit([ 'no', 'w' ], Page().t10, 0); checkSplit([ 'no', 'w' ], Page().t10, 1); checkSplitByPair([ 'something' ], Page().t5, 0, 9); checkSplitByPair([ 'something' ], Page().t5, 0, 0); checkSplitByPair([ 'something' ], Page().t5, 9, 9); checkSplitByPair([ 's', 'omething' ], Page().t5, 0, 1); checkSplitByPair([ 'some', 'thing' ], Page().t5, 0, 4); checkSplitByPair([ 'some', 'thing' ], Page().t5, 4, 9); checkSplitByPair([ 's', 'omet', 'hing' ], Page().t5, 1, 5); });