Skip to content

Commit 528b2eb

Browse files
authored
Merge 7a71632 into b9df809
2 parents b9df809 + 7a71632 commit 528b2eb

File tree

12 files changed

+191
-200
lines changed

12 files changed

+191
-200
lines changed

package-lock.json

Lines changed: 64 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"dependencies": {
2424
"boolbase": "^1.0.0",
2525
"css-what": "^6.1.0",
26-
"domhandler": "^4.3.1",
27-
"domutils": "^2.8.0",
26+
"domhandler": "^5.0.2",
27+
"domutils": "^3.0.1",
2828
"nth-check": "^2.0.1"
2929
},
3030
"devDependencies": {
@@ -36,7 +36,7 @@
3636
"cheerio-soupselect": "^0.1.1",
3737
"eslint": "^8.13.0",
3838
"eslint-config-prettier": "^8.5.0",
39-
"htmlparser2": "^7.2.0",
39+
"htmlparser2": "^8.0.0",
4040
"jest": "^27.5.1",
4141
"prettier": "^2.6.2",
4242
"ts-jest": "^27.1.4",

src/general.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import { attributeRules } from "./attributes";
22
import { compilePseudoSelector } from "./pseudo-selectors";
33
import type {
4+
Adapter,
45
CompiledQuery,
56
InternalOptions,
67
InternalSelector,
78
CompileToken,
89
} from "./types";
910
import { SelectorType } from "css-what";
1011

12+
function getElementParent<Node, ElementNode extends Node>(
13+
node: ElementNode,
14+
adapter: Adapter<Node, ElementNode>
15+
): ElementNode | null {
16+
const parent = adapter.getParent(node);
17+
if (parent && adapter.isTag(parent)) {
18+
return parent;
19+
}
20+
return null;
21+
}
22+
1123
/*
1224
* All available rules
1325
*/
@@ -79,8 +91,8 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
7991
return function descendant(elem: ElementNode): boolean {
8092
let current: ElementNode | null = elem;
8193

82-
while ((current = adapter.getParent(current))) {
83-
if (adapter.isTag(current) && next(current)) {
94+
while ((current = getElementParent(current, adapter))) {
95+
if (next(current)) {
8496
return true;
8597
}
8698
}
@@ -94,7 +106,7 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
94106
return function cachedDescendant(elem: ElementNode): boolean {
95107
let current: ElementNode | null = elem;
96108

97-
while ((current = adapter.getParent(current))) {
109+
while ((current = getElementParent(current, adapter))) {
98110
if (!isFalseCache.has(current)) {
99111
if (adapter.isTag(current) && next(current)) {
100112
return true;
@@ -112,8 +124,8 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
112124
let current: ElementNode | null = elem;
113125

114126
do {
115-
if (adapter.isTag(current) && next(current)) return true;
116-
} while ((current = adapter.getParent(current)));
127+
if (next(current)) return true;
128+
} while ((current = getElementParent(current, adapter)));
117129

118130
return false;
119131
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as DomUtils from "domutils";
22
import { falseFunc } from "boolbase";
33
import type {
4-
Node as DomHandlerNode,
4+
AnyNode as DomHandlerNode,
55
Element as DomHandlerElement,
66
} from "domhandler";
77
import { compile as compileRaw, compileUnsafe, compileToken } from "./compile";

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface Adapter<Node, ElementNode extends Node> {
3232
/**
3333
* Get the parent of the node
3434
*/
35-
getParent: (node: ElementNode) => ElementNode | null;
35+
getParent: (node: ElementNode) => Node | null;
3636

3737
/**
3838
* Get the siblings of the node. Note that unlike jQuery's `siblings` method,

test/api.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import * as CSSselect from "../src";
22
import { parseDOM, parseDocument } from "htmlparser2";
33
import { trueFunc, falseFunc } from "boolbase";
44
import * as DomUtils from "domutils";
5-
import type { Node, Element } from "domhandler";
5+
import type { Element } from "domhandler";
66
import { SelectorType, AttributeAction } from "css-what";
7-
import { Adapter } from "../src/types";
87

98
const [dom] = parseDOM("<div id=foo><p>foo</p></div>") as Element[];
109
const [xmlDom] = parseDOM("<DiV id=foo><P>foo</P></DiV>", {
@@ -71,7 +70,7 @@ describe("API", () => {
7170
) as Element[];
7271
const a = CSSselect.selectAll(".foo:has(+.bar)", dom);
7372
expect(a).toHaveLength(1);
74-
expect(a[0]).toStrictEqual(dom.children[0] as Element);
73+
expect(a[0]).toStrictEqual(dom.children[0]);
7574
});
7675

7776
it("should accept document root nodes", () => {
@@ -317,7 +316,7 @@ describe("API", () => {
317316

318317
describe("optional adapter methods", () => {
319318
it("should support prevElementSibling", () => {
320-
const adapter: Adapter<Node, Element> = {
319+
const adapter = {
321320
...DomUtils,
322321
prevElementSibling: undefined,
323322
};
@@ -334,9 +333,9 @@ describe("API", () => {
334333
it("should support isHovered", () => {
335334
const dom = parseDOM(`${"<p>foo".repeat(10)}`) as Element[];
336335

337-
const adapter: Adapter<Node, Element> = {
336+
const adapter = {
338337
...DomUtils,
339-
isHovered: (el) => el === dom[dom.length - 1],
338+
isHovered: (el: Element) => el === dom[dom.length - 1],
340339
};
341340

342341
const selection = CSSselect.selectAll("p:hover", dom, { adapter });

0 commit comments

Comments
 (0)