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 =
'
' +
'- One
' +
'- Two
' +
'' +
'- three
' +
'- four
' +
'- five
' +
'
' +
'- six
' +
'' +
'' +
'- seven
' +
'- eight
' +
'
' +
'- nine
' +
'
' +
'
';
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('' +
'- One
' +
'- Two
' +
'' +
'- three
' +
'- four
' +
'- five
' +
'
' +
'' +
'
' +
'- six
' +
'' +
'' +
'- seven
' +
'- eight
' +
'
' +
'- nine
' +
'
' +
'
', 'three-five', 'five');
check(
'' +
'- One
' +
'- Two
' +
'' +
'- three
' +
'- four
' +
'- five
' +
'
' +
'- six
' +
'
' +
'' +
'' +
'' +
'- seven
' +
'- eight
' +
'
' +
'- nine
' +
'
' +
'
', '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 =
'' +
'- One
' +
'- Two
' +
'' +
'- three
' +
'- four
' +
'- five
' +
'
' +
'- six
' +
'' +
'' +
'- seven
' +
'- eight
' +
'
' +
'- nine
' +
'
' +
'
';
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');
})();
}
);