import { Assert, UnitTest } from '@ephox/bedrock-client';
import * as Hierarchy from 'ephox/sugar/api/dom/Hierarchy';
import { SugarElement } from 'ephox/sugar/api/node/SugarElement';
import * as Html from 'ephox/sugar/api/properties/Html';
import type { Situ } from 'ephox/sugar/api/selection/Situ';
import * as Prefilter from 'ephox/sugar/selection/quirks/Prefilter';
UnitTest.test('Browser Test: PrefilterTest', () => {
const root = SugarElement.fromHtml(
'
' +
'
dog' +
'
' +
'
![]()
' +
'
cat' +
'
'
);
const toString = (situ: Situ) => situ.fold(
(b) => 'before(' + Html.getOuter(b) + ')',
(e, o) => 'on(' + Html.getOuter(e) + ', ' + o + ')',
(a) => 'after(' + Html.getOuter(a) + ')'
);
const check = (label: string, expected: string, elementPath: number[], offset: number) => {
const element = Hierarchy.follow(root, elementPath).getOrDie('Test: ' + label + '. Could not find the element path within root: ' + elementPath);
const actual = Prefilter.beforeSpecial(element, offset);
const actualS = toString(actual);
Assert.eq('Test: ' + label + '. Was: ' + actualS + ', but expected: ' + expected, expected, actualS);
};
check('div 0', 'on(dog
cat , 0)', [ ], 0);
check('div > span, 0', 'on(dog, 0)', [ 0 ], 0);
check('div > span, 1', 'on(dog, 1)', [ 0 ], 1);
check('div > br, 0', 'before(
)', [ 1 ], 0);
check('div > br, 1', 'after(
)', [ 1 ], 1);
check('div > img, 0', 'before(
)', [ 2 ], 0);
check('div > img, 1', 'after(
)', [ 2 ], 1);
check('div > span3, 0', 'on(cat, 0)', [ 3 ], 0);
check('div > span3, 1', 'on(cat, 1)', [ 3 ], 1);
});