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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
import * as CSSselect from "../src";
import { parseDOM, parseDocument } from "htmlparser2";
import boolbase from "boolbase";
import * as DomUtils from "domutils";
import type { AnyNode, Element } from "domhandler";
import { SelectorType, AttributeAction } from "css-what";
import type { Adapter } from "../src/types.js";
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[];
const notYet = "not yet supported by css-select";
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 with more than a selector", () => {
it("function in `is`", () => {
expect(
CSSselect.is(dom, (elem) => elem.attribs["id"] === "foo")
).toBe(true);
});
it("parsed selector in `is`", () => {
expect(
CSSselect.is(dom, [
[
{
type: SelectorType.Attribute,
action: AttributeAction.Equals,
ignoreCase: false,
name: "id",
namespace: null,
value: "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]);
});
it("should accept document root nodes", () => {
const doc = parseDocument("<div id=foo><p>foo</p></div>");
expect(CSSselect.selectAll(":contains(foo)", doc)).toHaveLength(2);
});
it("should support scoped selections relative to the root (#709)", () => {
const doc = parseDocument(`
<div class="parent">
<div class="one"><p class="p1"></p></div>
<div class="two"><p class="p2"></p></div>
<div class="three"><p class="p3"></p></div>
</div>`);
const two = CSSselect.selectOne(".two", doc);
expect(
CSSselect.selectOne(".parent .two .p2", two, {
relativeSelector: false,
})
).toMatchObject({
attribs: { class: "p2" },
});
expect(
CSSselect.selectOne(".parent .two .p3", two, {
relativeSelector: false,
})
).toBeNull();
});
});
describe("errors", () => {
it("should throw with a pseudo-element", () => {
expect(() => CSSselect.compile("::after")).toThrow("not supported");
});
it("should throw an error if encountering a traversal-first selector with relative selectors disabled", () =>
expect(() =>
CSSselect.compile("> p", { relativeSelector: false })
).toThrow(
"Relative selectors are not allowed when the `relativeSelector` option is disabled"
));
it("should throw with a column combinator", () => {
expect(() => CSSselect.compile("foo || bar")).toThrow(notYet);
});
it("should throw with attribute namespace", () => {
expect(() => CSSselect.compile("[foo|bar]")).toThrow(notYet);
expect(() => CSSselect.compile("[|bar]")).not.toThrow();
expect(() => CSSselect.compile("[*|bar]")).toThrow(notYet);
});
it("should throw with tag namespace", () => {
expect(() => CSSselect.compile("foo|bar")).toThrow(notYet);
expect(() => CSSselect.compile("|bar")).toThrow(notYet);
expect(() => CSSselect.compile("*|bar")).toThrow(notYet);
});
it("should throw with universal selector", () => {
expect(() => CSSselect.compile("foo|*")).toThrow(notYet);
expect(() => CSSselect.compile("|*")).toThrow(notYet);
expect(() => CSSselect.compile("*|*")).not.toThrow();
});
it("should throw if parameter is supplied for pseudo", () => {
expect(() => CSSselect.compile(":any-link(test)")).toThrow(
"doesn't have any arguments"
);
expect(() => CSSselect.compile(":only-child(test)")).toThrow(
"doesn't have any arguments"
);
});
it("should throw if no parameter is supplied for pseudo", () => {
CSSselect.pseudos["foovalue"] = (elem, { adapter }, subselect) =>
adapter.getAttributeValue(elem, "foo") === subselect;
expect(() => CSSselect.compile(":foovalue")).toThrow(
"requires an argument"
);
delete CSSselect.pseudos["foovalue"];
});
it("should throw if parameter is supplied for user-provided pseudo", () =>
expect(() =>
CSSselect.compile(":foovalue(boo)", {
pseudos: { foovalue: "tag" },
})
).toThrow("doesn't have any arguments"));
it("should throw if no parameter is supplied for user-provided pseudo", () =>
expect(() =>
CSSselect.compile(":foovalue", {
pseudos: {
foovalue(_el, data) {
return data != null;
},
},
})
).toThrow("requires an argument"));
});
describe("unsatisfiable and universally valid selectors", () => {
it("in :not", () => {
expect(CSSselect._compileUnsafe(":not(*)")).toBe(
boolbase.falseFunc
);
expect(CSSselect._compileUnsafe(":not(:nth-child(-1n-1))")).toBe(
boolbase.trueFunc
);
expect(CSSselect._compileUnsafe(":not(:not(:not(*)))")).toBe(
boolbase.falseFunc
);
});
it("in :has", () => {
const matches = CSSselect.selectAll(":has(*)", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0]).toBe(dom);
expect(CSSselect._compileUnsafe(":has(:nth-child(-1n-1))")).toBe(
boolbase.falseFunc
);
const matches2 = CSSselect.selectAll(
"p:has(+ *)",
parseDOM("<p><p>")
);
expect(matches2).toHaveLength(1);
expect(matches2[0]).toHaveProperty("tagName", "p");
});
it("in :is", () => {
expect(CSSselect._compileUnsafe(":is(*)")).toBe(boolbase.trueFunc);
expect(CSSselect._compileUnsafe(":is(:nth-child(-1n-1))")).toBe(
boolbase.falseFunc
);
expect(CSSselect._compileUnsafe(":is(:not(:not(*)))")).toBe(
boolbase.trueFunc
);
expect(CSSselect._compileUnsafe(":is(*, :scope)")).toBe(
boolbase.trueFunc
);
});
it("should skip unsatisfiable", () =>
expect(CSSselect._compileUnsafe("* :not(*) foo")).toBe(
boolbase.falseFunc
));
it("should promote universally valid", () =>
expect(CSSselect._compileUnsafe("*, foo")).toBe(boolbase.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);
});
it("should support traversals", () => {
let matches = CSSselect.selectAll(":matches(div p)", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0].name).toBe("p");
matches = CSSselect.selectAll(":matches(div > p)", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0].name).toBe("p");
matches = CSSselect.selectAll(":matches(p < div)", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0].name).toBe("div");
matches = CSSselect.selectAll(":matches(> p)", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0].name).toBe("p");
matches = CSSselect.selectAll("div:has(:is(:scope p))", [dom]);
expect(matches).toHaveLength(1);
expect(matches[0].name).toBe("div");
const [multiLevelDom] = parseDOM("<a><b><c><d>") as Element[];
matches = CSSselect.selectAll(":is(* c)", multiLevelDom);
expect(matches).toHaveLength(1);
expect(matches[0].name).toBe("c");
});
it("should support alias :is", () => {
let matches = CSSselect.selectAll(":is(p, div)", [dom]);
expect(matches).toHaveLength(2);
matches = CSSselect.selectAll(":is(div, :not(div))", [dom]);
expect(matches).toHaveLength(2);
matches = CSSselect.selectAll(
":is(boo, baa, tag, div, foo, bar, baz)",
[dom]
);
expect(matches).toHaveLength(1);
expect(matches[0]).toBe(dom);
});
it("should support alias :where", () => {
let matches = CSSselect.selectAll(":where(p, div)", [dom]);
expect(matches).toHaveLength(2);
matches = CSSselect.selectAll(":where(div, :not(div))", [dom]);
expect(matches).toHaveLength(2);
matches = CSSselect.selectAll(
":where(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(boolbase.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 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 not crash when siblings repeat", () => {
const dom = parseDOM(`<div></div>`.repeat(51)) as Element[];
expect(
CSSselect.selectAll("+div", dom, { context: dom })
).toHaveLength(50);
});
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);
});
it("should pass options to sub-selectors", () => {
expect(
CSSselect.selectAll("div:is(DiV#FOO):not([ID]):foo", [dom], {
xmlMode: true,
lowerCaseTags: true,
quirksMode: true,
pseudos: { foo: "#fOO" },
})
).toHaveLength(1);
});
});
describe("optional adapter methods", () => {
it("should support prevElementSibling", () => {
const adapter: Adapter<AnyNode, Element> = { ...DomUtils };
delete adapter.prevElementSibling;
const dom = parseDOM(
`${"<p>foo".repeat(10)}<div>bar</div>`
) as Element[];
expect(
CSSselect.selectAll("p + div", dom, { adapter })
).toStrictEqual(CSSselect.selectAll("p + div", dom));
});
it("should support isHovered", () => {
const dom = parseDOM(`${"<p>foo".repeat(10)}`) as Element[];
const adapter = {
...DomUtils,
isHovered: (el: Element) => el === dom[dom.length - 1],
};
const selection = CSSselect.selectAll("p:hover", dom, { adapter });
expect(selection).toHaveLength(1);
expect(selection[0]).toBe(dom[dom.length - 1]);
});
it("should not match any elements if `isHovered` is not defined", () => {
const dom = parseDOM(`${"<p>foo".repeat(10)}`);
expect(CSSselect.selectAll("p:hover", dom)).toHaveLength(0);
});
});
});
|