1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
import * as CSSselect from "../src";
import { parseDOM } from "htmlparser2";
import { trueFunc, falseFunc } from "boolbase";
import type { Element } from "domhandler";
const [dom] = parseDOM("<div id=foo><p>foo</p></div>") as Element[];
const [xmlDom] = parseDOM("<DiV id=foo><P>foo</P></DiV>", {
xmlMode: true,
}) as Element[];
describe("API", () => {
describe("removes duplicates", () => {
it("between identical trees", () => {
expect(CSSselect.selectAll("div", [dom, dom])).toHaveLength(1);
});
it("between a superset and subset", () => {
expect(
CSSselect.selectAll("p", [dom, dom.children[0]])
).toHaveLength(1);
});
it("betweeen a subset and superset", () => {
expect(
CSSselect.selectAll("p", [dom.children[0], dom])
).toHaveLength(1);
});
});
describe("can be queried by function", () => {
it("in `is`", () => {
expect(CSSselect.is(dom, (elem) => elem.attribs.id === "foo")).toBe(
true
);
});
// Probably more cases should be added here
});
describe("selectAll", () => {
it("should query array elements directly when they have no parents", () => {
const divs = [dom];
expect(CSSselect.selectAll("div", divs)).toStrictEqual(divs);
});
it("should query array elements directly when they have parents", () => {
const ps = CSSselect.selectAll("p", [dom]);
expect(CSSselect.selectAll("p", ps)).toStrictEqual(ps);
});
it("should support pseudos led by a traversal (#111)", () => {
const [dom] = parseDOM(
'<div><div class="foo">a</div><div class="bar">b</div></div>'
) as Element[];
const a = CSSselect.selectAll(".foo:has(+.bar)", dom);
expect(a).toHaveLength(1);
expect(a[0]).toStrictEqual(dom.children[0] as Element);
});
});
describe("unsatisfiable and universally valid selectors", () => {
it("in :not", () => {
let func = CSSselect._compileUnsafe(":not(*)");
expect(func).toBe(falseFunc);
func = CSSselect._compileUnsafe(":not(:nth-child(-1n-1))");
expect(func).toBe(trueFunc);
func = CSSselect._compileUnsafe(":not(:not(:not(*)))");
expect(func).toBe(falseFunc);
});
it("in :has", () => {
const matches = CSSselect.selectAll(":has(*)", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0]).toBe(dom);
const func = CSSselect._compileUnsafe(":has(:nth-child(-1n-1))");
expect(func).toBe(falseFunc);
});
it("should skip unsatisfiable", () => {
const func = CSSselect._compileUnsafe("* :not(*) foo");
expect(func).toBe(falseFunc);
});
it("should promote universally valid", () => {
const func = CSSselect._compileUnsafe("*, foo");
expect(func).toBe(trueFunc);
});
});
describe(":matches", () => {
it("should select multiple elements", () => {
let matches = CSSselect.selectAll(":matches(p, div)", [dom]);
expect(matches).toHaveLength(2);
matches = CSSselect.selectAll(":matches(div, :not(div))", [dom]);
expect(matches).toHaveLength(2);
matches = CSSselect.selectAll(
":matches(boo, baa, tag, div, foo, bar, baz)",
[dom]
);
expect(matches).toHaveLength(1);
expect(matches[0]).toBe(dom);
});
});
describe("parent selector (<)", () => {
it("should select the right element", () => {
const matches = CSSselect.selectAll("p < div", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0]).toBe(dom);
});
it("should not select nodes without children", () => {
const matches = CSSselect.selectAll("p < div", [dom]);
expect(matches).toStrictEqual(CSSselect.selectAll("* < *", [dom]));
});
});
describe("selectOne", () => {
it("should select elements in traversal order", () => {
expect(CSSselect.selectOne("p", [dom])).toBe(dom.children[0]);
expect(CSSselect.selectOne(":contains(foo)", [dom])).toBe(dom);
});
it("should take shortcuts when applicable", () => {
let match = CSSselect.selectOne(falseFunc, {
get length() {
throw new Error("Did not take shortcut");
},
});
expect(match).toBeNull();
match = CSSselect.selectOne("*", []);
expect(match).toBeNull();
});
it("should properly handle root elements", () => {
expect(CSSselect.selectOne("div:root", [dom])).toBe(dom);
expect(CSSselect.selectOne("* > div", [dom])).toBeNull();
});
});
describe("options", () => {
const opts = { xmlMode: true };
it("should recognize xmlMode in :has and :not", () => {
expect(CSSselect.is(xmlDom, "DiV:has(P)", opts)).toBe(true);
expect(CSSselect.is(xmlDom, "DiV:not(div)", opts)).toBe(true);
expect(
CSSselect.is(xmlDom.children[0], "DiV:has(P) :not(p)", opts)
).toBe(true);
});
it("should be strict", () => {
const opts = { strict: true };
expect(() => CSSselect.compile(":checkbox", opts)).toThrow(Error);
expect(() => CSSselect.compile("[attr=val i]", opts)).toThrow(
Error
);
expect(() => CSSselect.compile("[attr!=val]", opts)).toThrow(Error);
expect(() => CSSselect.compile("[attr!=val i]", opts)).toThrow(
Error
);
expect(() => CSSselect.compile("foo < bar", opts)).toThrow(Error);
expect(() => CSSselect.compile(":not(:parent)", opts)).toThrow(
Error
);
expect(() => CSSselect.compile(":not(a > b)", opts)).toThrow(Error);
expect(() => CSSselect.compile(":not(a, b)", opts)).toThrow(Error);
});
it("should recognize contexts", () => {
const div = CSSselect.selectAll("div", [dom]);
const p = CSSselect.selectAll("p", [dom]);
expect(CSSselect.selectOne("div", div, { context: div })).toBe(
div[0]
);
expect(CSSselect.selectOne("div", div, { context: p })).toBe(null);
expect(
CSSselect.selectAll("p", div, { context: div })
).toStrictEqual(p);
});
it("should cache results by default", () => {
const [dom] = parseDOM(
'<div id="foo"><p>bar</p></div>'
) as Element[];
const query = CSSselect.compile("#bar p");
expect(CSSselect.selectAll(query, [dom])).toHaveLength(0);
dom.attribs.id = "bar";
// The query should be cached and the changed attribute should be ignored.
expect(CSSselect.selectAll(query, [dom])).toHaveLength(0);
});
it("should skip cacheing results if asked to", () => {
const [dom] = parseDOM(
'<div id="foo"><p>bar</p></div>'
) as Element[];
const query = CSSselect.compile("#bar p", { cacheResults: false });
expect(CSSselect.selectAll(query, [dom])).toHaveLength(0);
dom.attribs.id = "bar";
// The query should not be cached, the changed attribute should be picked up.
expect(CSSselect.selectAll(query, [dom])).toHaveLength(1);
});
});
});
|