import { Assert, UnitTest } from '@ephox/bedrock-client'; import { Arr } from '@ephox/katamari'; import { Compare, Hierarchy, Html, Insert, InsertAll, Remove, SugarBody, SugarElement } from '@ephox/sugar'; import * as DomClumps from 'ephox/robin/api/dom/DomClumps'; UnitTest.test('DomClumpsTest', () => { const body = SugarBody.body(); const container = SugarElement.fromTag('div'); Insert.append(body, container); const find = (path: number[]) => { return Hierarchy.follow(container, path).getOrDie('Could not find the path: ' + path.join(',')); }; const isRoot = (elem: SugarElement) => { return Compare.eq(elem, container); }; const mark = (res: SugarElement[]) => { if (res.length > 0) { const strong = SugarElement.fromTag('strong'); Insert.before(res[0], strong); InsertAll.append(strong, res); } }; const checkFracture = (expected: string, start: number[], soffset: number, finish: number[], foffset: number) => { DomClumps.fracture(isRoot, find(start), soffset, find(finish), foffset).each(mark); Assert.eq('', expected, Html.get(container)); }; const checkFractures = (expected: string, start: number[], soffset: number, finish: number[], foffset: number) => { const sections = DomClumps.fractures(isRoot, find(start), soffset, find(finish), foffset); Arr.each(sections, mark); Assert.eq('', expected, Html.get(container)); }; container.dom.innerHTML = '
One
' + '| Heading |
|---|
| Data |
Two
'; // Highlighting over a table from O|ne -> T|wo checkFractures( 'One
' + '| Heading |
|---|
| Data |
Two
', [ 0, 0 ], 'O'.length, [ 2, 0 ], 'T'.length ); container.dom.innerHTML = 'This is the second line.
' + 'the
' + 'something one' + 'This is the second line.
' + 'the
' + 'something one' + 'And breaking it:
And breaking it:
This is the first line of what I am typing
This is the second line.
'; // Highlighting from Text n|ode -> This is th|e second line checkFractures( 'Text nodeThis is the first line of what I am typing
' + 'This is the second line.
', [ 0 ], 'Text n'.length, [ 5, 0 ], 'This is th'.length); container.dom.innerHTML = 'This is bold text and italic text here.
'; // Highlighting from This| -> an|d checkFracture( 'This is bold text and italic text here.
', [ 0, 0 ], 'This'.length, [ 0, 2 ], ' an'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; // Highlighting from -> checkFracture( 'This is bold text and italic text here.
', [ 0 ], 1, [ 0 ], 2 ); container.dom.innerHTML = 'This is bold text and italic text here.
'; // Highlighting from to text| checkFracture( 'This is bold text and italic text here.
', [ 0 ], 1, [ 0, 1 ], 1 ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0 ], 1, [ 0, 1, 0 ], 'bold'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0, 0 ], 'T'.length, [ 0, 1, 0 ], 'bold'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0 ], 2, [ 0 ], 3 ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0 ], 3, [ 0 ], 4 ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0 ], 4, [ 0 ], 5 ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0 ], 4, [ 0, 4 ], ' he'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0, 1, 0 ], 'b'.length, [ 0, 4 ], ' he'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0, 1, 0 ], 'b'.length, [ 0, 1, 0 ], 'bo'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0, 1 ], 1, [ 0, 3 ], 0 ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( // Remove the empty formatting element. 'This is bold text and italic text here.
', [ 0, 1, 0 ], ''.length, [ 0, 2 ], ' a'.length ); container.dom.innerHTML = 'This is bold text and italic text here.
'; checkFracture( 'This is bold text and italic text here.
', [ 0, 1, 0 ], ''.length, [ 0, 1, 0 ], 'bold text'.length ); container.dom.innerHTML = 'This new words is bold text and italic text here.
'; checkFracture( 'This new words is bold text and italic text here.
', [ 0, 1 ], 2, [ 0, 3, 0 ], 'bo'.length ); container.dom.innerHTML = 'This is the word that I can understand, even if it is not the same as before.
' + 'And another paragraph
' + 'Plus one more.
' + 'Last one, I promise
'; checkFractures( 'This is the word that I can understand, even if it is not the same as before.
' + 'And another paragraph
' + 'Plus one more.
' + 'Last one, I promise
', [ 0, 1, 0 ], 'the'.length, [ 3, 0 ], 'Last'.length ); container.dom.innerHTML = 'This is completely different from what you would_expect_
' + 'And more of this is here again.
'; checkFractures( 'This is completely different from what you would_expect_
' + 'And more of this is here again.
', [ 0, 1, 1, 1, 0 ], 't'.length, [ 1, 1, 1, 0 ], 'h'.length ); container.dom.innerHTML = '| One | \n\n\n \nTwo |
| One | ' + '\n\n\n \n' + 'Two | ' + '
| One | \n\n\n \nTwo |
| One | ' + '\n\n\n \n' + 'Two | ' + '
| One | \n\n\n \nTwo |
| One | ' + '\n\n\n \n' + 'Two | ' + '