import { Assert, UnitTest } from '@ephox/bedrock-client'; import { DomUniverse } from '@ephox/boss'; import { Arr } from '@ephox/katamari'; import { Compare, Hierarchy, Insert, Remove, SugarBody, SugarElement } from '@ephox/sugar'; import * as Clumps from 'ephox/robin/clumps/Clumps'; UnitTest.test('ClumpsTest', () => { /* The purpose of this test is to take a large section of html and test dividing it into * inline clumps */ const find = (path: number[]) => { return Hierarchy.follow(container, path).getOrDie('Could not find the path: ' + path.join(',')); }; const body = SugarBody.body(); const container = SugarElement.fromTag('div'); 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
'; Insert.append(body, container); const isRoot = (elem: SugarElement) => { return Compare.eq(elem, container); }; interface Expected { start: number[]; end: number[]; } const check = (expected: Expected[], start: number[], soffset: number, finish: number[], foffset: number) => { const actual = Clumps.collect(DomUniverse(), isRoot, find(start), soffset, find(finish), foffset); Assert.eq('The length of Clumps was different. Expected: ' + expected.length + ', actual: ' + actual.length, expected.length, actual.length); Arr.each(expected, (exp, i) => { const act = actual[i]; Assert.eq('', true, Compare.eq(find(exp.start), act.start)); Assert.eq('', true, Compare.eq(find(exp.end), act.finish)); }); }; 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
'; check([ // the word -> before { start: [ 0, 1, 0 ], end: [ 0, 4 ] }, // And -> paragraph { start: [ 1, 0 ], end: [ 1, 1 ] }, // Plus -> Plus { start: [ 2, 0 ], end: [ 2, 0 ] }, // Last one -> Last one { start: [ 3, 0 ], end: [ 3, 0 ] } ], [ 0, 1, 0 ], 'the'.length, [ 3, 0 ], 'Last'.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
'; check([ // the word -> before { start: [ 0, 1, 0 ], end: [ 0, 4 ] }, // And -> paragraph { start: [ 1, 0 ], end: [ 1, 1, 0 ] } ], [ 0, 1, 0 ], 'the'.length, [ 1, 1, 0 ], 'par'.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
'; check([ // the word -> the word { start: [ 0, 1, 0 ], end: [ 0, 1, 0 ] } ], [ 0, 1, 0 ], 'the'.length, [ 0, 1, 0 ], 'the wor'.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
'; check([ // the word -> it { start: [ 0, 1, 0 ], end: [ 0, 3, 0 ] } ], [ 0, 1, 0 ], 'the'.length, [ 0, 3, 0 ], 'i'.length); container.dom.innerHTML = 'This is completely different from what you would_expect_
' + 'And more of this is here again.
'; check([ // from -> { start: [ 0, 1, 1, 1, 0 ], end: [ 0, 1 ] }, // And -> here { start: [ 1, 0 ], end: [ 1, 1, 1, 0 ] } ], [ 0, 1, 1, 1, 0 ], 'f'.length, [ 1, 1, 1, 0 ], 'h'.length); container.dom.innerHTML = '| One | Two |
Paragraph
'; check([ // One -> One { start: [ 0, 0, 0, 0, 0 ], end: [ 0, 0, 0, 0, 0 ] }, // Two -> Two { start: [ 0, 0, 0, 1, 0 ], end: [ 0, 0, 0, 1, 0 ] } ], [ 0, 0, 0, 0 ], 0, [ 0, 0, 0, 1 ], 1); container.dom.innerHTML = 'Text
| One | Two |
Paragraph
'; check([ { start: [ 1, 0, 0, 0, 0 ], end: [ 1, 0, 0, 0, 0 ] }, { start: [ 1, 0, 0, 1, 0 ], end: [ 1, 0, 0, 1, 0 ] }, { start: [ 2, 0 ], end: [ 2, 0 ] } ], [], 1, [ 2 ], 0); Remove.remove(container); });