|
1 | 1 | import * as CSSselect from "../src"; |
2 | 2 | import { parseDOM, parseDocument } from "htmlparser2"; |
3 | 3 | import { trueFunc, falseFunc } from "boolbase"; |
4 | | -import type { Element } from "domhandler"; |
| 4 | +import * as DomUtils from "domutils"; |
| 5 | +import type { Node, Element } from "domhandler"; |
5 | 6 | import { SelectorType, AttributeAction } from "css-what"; |
| 7 | +import { Adapter } from "../src/types"; |
6 | 8 |
|
7 | 9 | const [dom] = parseDOM("<div id=foo><p>foo</p></div>") as Element[]; |
8 | 10 | const [xmlDom] = parseDOM("<DiV id=foo><P>foo</P></DiV>", { |
@@ -294,4 +296,34 @@ describe("API", () => { |
294 | 296 | expect(CSSselect.selectAll(query, [dom])).toHaveLength(1); |
295 | 297 | }); |
296 | 298 | }); |
| 299 | + |
| 300 | + describe("optional adapter methods", () => { |
| 301 | + it("should support prevElementSibling", () => { |
| 302 | + const adapter = { |
| 303 | + ...DomUtils, |
| 304 | + prevElementSibling: undefined, |
| 305 | + } as Adapter<Node, Element>; |
| 306 | + |
| 307 | + const dom = parseDOM( |
| 308 | + `${"<p>foo".repeat(10)}<div>bar</div>` |
| 309 | + ) as Element[]; |
| 310 | + |
| 311 | + expect( |
| 312 | + CSSselect.selectAll("p + div", dom, { adapter }) |
| 313 | + ).toStrictEqual(CSSselect.selectAll("p + div", dom)); |
| 314 | + }); |
| 315 | + |
| 316 | + it("should support isHovered", () => { |
| 317 | + const dom = parseDOM(`${"<p>foo".repeat(10)}`) as Element[]; |
| 318 | + |
| 319 | + const adapter = { |
| 320 | + ...DomUtils, |
| 321 | + isHovered: (el) => el === dom[dom.length - 1], |
| 322 | + } as Adapter<Node, Element>; |
| 323 | + |
| 324 | + const selection = CSSselect.selectAll("p:hover", dom, { adapter }); |
| 325 | + expect(selection).toHaveLength(1); |
| 326 | + expect(selection[0]).toBe(dom[dom.length - 1]); |
| 327 | + }); |
| 328 | + }); |
297 | 329 | }); |
0 commit comments