import { Assert, UnitTest } from '@ephox/bedrock-client'; import { Arr, Fun, Optional } from '@ephox/katamari'; import { KAssert } from '@ephox/katamari-assertions'; import { Attribute, Compare, Hierarchy, Html, SelectorFind, SugarElement } from '@ephox/sugar'; import * as DomParent from 'ephox/robin/api/dom/DomParent'; UnitTest.test( 'DomParentTest', () => { const check = (expected: string, p: string, c: string) => { const container = SugarElement.fromTag('div'); container.dom.innerHTML = '
    ' + '
  1. One
  2. ' + '
  3. Two
  4. ' + '
      ' + '
    1. three
    2. ' + '
    3. four
    4. ' + '
    5. five
    6. ' + '
    ' + '
  5. six
  6. ' + '
      ' + '
        ' + '
      1. seven
      2. ' + '
      3. eight
      4. ' + '
      ' + '
    1. nine
    2. ' + '
    ' + '
'; const parent = SelectorFind.descendant(container, '.' + p).getOrDie(); const child = SelectorFind.descendant(container, '.' + c).getOrDie(); DomParent.breakToRight(parent, child); Assert.eq('eq', expected, Html.get(container)); }; check('
    ' + '
  1. One
  2. ' + '
  3. Two
  4. ' + '
      ' + '
    1. three
    2. ' + '
    3. four
    4. ' + '
    5. five
    6. ' + '
    ' + '
      ' + '
    ' + '
  5. six
  6. ' + '
      ' + '
        ' + '
      1. seven
      2. ' + '
      3. eight
      4. ' + '
      ' + '
    1. nine
    2. ' + '
    ' + '
', 'three-five', 'five'); check( '
    ' + '
  1. One
  2. ' + '
  3. Two
  4. ' + '
      ' + '
    1. three
    2. ' + '
    3. four
    4. ' + '
    5. five
    6. ' + '
    ' + '
  5. six
  6. ' + '
' + '
    ' + '
      ' + '
        ' + '
      1. seven
      2. ' + '
      3. eight
      4. ' + '
      ' + '
    1. nine
    2. ' + '
    ' + '
', 'one-nine', 'six'); const checkPath = (expected: string, input: string, p: number[], c: number[]) => { const container = SugarElement.fromTag('div'); container.dom.innerHTML = input; const parent = Hierarchy.follow(container, p).getOrDie(); const child = Hierarchy.follow(container, c).getOrDie(); const isTop = Fun.curry(Compare.eq, parent); DomParent.breakPath(child, isTop, DomParent.breakToLeft); Assert.eq('eq', expected, Html.get(container)); }; checkPath( '
' + '' + 'Cat' + 'Dog' + '' + '' + '' + '' + '
', '
' + '' + 'Cat' + 'Dog' + '' + '
', [ 0, 0 ], [ 0, 0, 1, 0 ]); checkPath( '
' + '' + 'Cat' + '' + 'Hello' + '
' + // --- SPLIT to '
' + '' + '' + '' + 'World' + '' + 'Dog' + '' + '
', '
' + '' + 'Cat' + '' + 'Hello' + '
' + 'World' + '
' + 'Dog' + '
' + '
', [ 0, 0 ], [ 0, 0, 1, 1 ]); checkPath( '
' + '' + 'Cat' + '' + 'Hello' + '
' + 'World' + // --- SPLIT to '
' + '' + '' + '' + 'Dog' + '' + '
', '
' + '' + 'Cat' + '' + 'Hello' + '
' + 'World' + '
' + 'Dog' + '
' + '
', [ 0, 0 ], [ 0, 0, 1, 2 ]); checkPath( '
' + '' + 'Cat' + '' + 'Hello' + // --- SPLIT to '' + '' + '' + '' + '
' + 'World' + '
' + 'Dog' + '
' + '
', '
' + '' + 'Cat' + '' + 'Hello' + '
' + 'World' + '
' + 'Dog' + '
' + '
', [ 0, 0 ], [ 0, 0, 1, 0 ]); (() => { const check = (expected: Optional, s: string, f: string) => { const container = SugarElement.fromTag('div'); container.dom.innerHTML = '
    ' + '
  1. One
  2. ' + '
  3. Two
  4. ' + '
      ' + '
    1. three
    2. ' + '
    3. four
    4. ' + '
    5. five
    6. ' + '
    ' + '
  5. six
  6. ' + '
      ' + '
        ' + '
      1. seven
      2. ' + '
      3. eight
      4. ' + '
      ' + '
    1. nine
    2. ' + '
    ' + '
'; const parent = SelectorFind.descendant(container, '.' + s).getOrDie(); const child = SelectorFind.descendant(container, '.' + f).getOrDie(); const subset = DomParent.subset(parent, child); const actual = subset.map((ss) => Arr.map(ss, (x) => Attribute.get(x, 'class'))); const expected_ = expected.map((ss) => Arr.map((ss), Fun.identity)); KAssert.eqOptional('eq', expected_, actual); }; check(Optional.some([ 'three-five' ]), 'three-five', 'five'); check(Optional.some([ 'three-five' ]), 'five', 'three-five'); check(Optional.some([ 'two', 'three-five' ]), 'two', 'five'); check(Optional.some([ 'two', 'three-five', 'six', 'seven-nine' ]), 'two', 'eight'); })(); } );