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.

' + '

This is the heading line.

' + '
  1. apples
  2. oranges
' + 'and more' + '
' + '

the

' + 'something one' + '
'; // Highlighting from This is the s|econd line -> someth|ing checkFractures( '

This is the second line.

' + '

This is the heading line.

' + '
  1. apples
  2. oranges
' + 'and more
' + '

the

' + 'something one' + '
', [ 0, 0 ], 'This is the s'.length, [ 6 ], 'someth'.length ); container.dom.innerHTML = '
  1. apples
  2. oranges

And breaking it:

  1. here
and more
and more
and more
'; // Highlighting from And bre|aking -> and mo|re checkFractures( '
  1. apples
  2. oranges

And breaking it:

  1. here
and more
and mo
re
and more
', [ 1, 0 ], 7, [ 5 ], 6 ); container.dom.innerHTML = 'Text node
Another text node
' + '

This is the heading line.

'; // Highlighting from Anoth|er text node -> Thi|is is the heading line checkFractures( 'Text node
Another text node
' + '

This is the heading line.

', [ 1, 2 ], 5, [ 2, 0, 0 ], 3 ); container.dom.innerHTML = 'Text node
Another text node

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 node
Another text node

This 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 = '\n\n\n \n\n
OneTwo
'; checkFractures( '' + '' + '' + '\n\n\n \n' + '' + '' + '\n' + '
OneTwo
', [ 0, 0, 0, 0, 0 ], 0, [ 0, 0, 0 ], 2); container.dom.innerHTML = '\n\n\n\n\n \n\n
OneTwo
'; checkFractures( '\n' + '\n' + '' + '\n\n\n \n' + '' + '' + '\n' + '
OneTwo
', [ 0 ], 0, [ 0 ], 1); container.dom.innerHTML = '\n\n\n\n\n \n\n
Heading
OneTwo
'; checkFractures( '\n' + '\n' + '' + '\n\n\n \n' + '' + '' + '\n' + '
Heading
OneTwo
', [ 0 ], 0, [ 0 ], 2); container.dom.innerHTML = '
  1. One
  2. Two
  3. Three
'; checkFractures( '
  1. One
  2. Two
  3. Three
', [ 0 ], 0, [ 0 ], 2); container.dom.innerHTML = '
  1. One
  2. Two
  3. Three
'; checkFractures( '
  1. One
  2. Two
  3. Three
', [ 0, 0 ], 0, [ 0, 2 ], 0); Remove.remove(container); });