Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Bump dependencies, update types #754

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 64 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"dependencies": {
"boolbase": "^1.0.0",
"css-what": "^6.1.0",
"domhandler": "^4.3.1",
"domutils": "^2.8.0",
"domhandler": "^5.0.2",
"domutils": "^3.0.1",
"nth-check": "^2.0.1"
},
"devDependencies": {
Expand All @@ -36,7 +36,7 @@
"cheerio-soupselect": "^0.1.1",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"htmlparser2": "^7.2.0",
"htmlparser2": "^8.0.0",
"jest": "^27.5.1",
"prettier": "^2.6.2",
"ts-jest": "^27.1.4",
Expand Down
22 changes: 17 additions & 5 deletions src/general.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { attributeRules } from "./attributes";
import { compilePseudoSelector } from "./pseudo-selectors";
import type {
Adapter,
CompiledQuery,
InternalOptions,
InternalSelector,
CompileToken,
} from "./types";
import { SelectorType } from "css-what";

function getElementParent<Node, ElementNode extends Node>(
node: ElementNode,
adapter: Adapter<Node, ElementNode>
): ElementNode | null {
const parent = adapter.getParent(node);
if (parent && adapter.isTag(parent)) {
return parent;
}
return null;
}

/*
* All available rules
*/
Expand Down Expand Up @@ -79,8 +91,8 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
return function descendant(elem: ElementNode): boolean {
let current: ElementNode | null = elem;

while ((current = adapter.getParent(current))) {
if (adapter.isTag(current) && next(current)) {
while ((current = getElementParent(current, adapter))) {
if (next(current)) {
return true;
}
}
Expand All @@ -94,7 +106,7 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
return function cachedDescendant(elem: ElementNode): boolean {
let current: ElementNode | null = elem;

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

do {
if (adapter.isTag(current) && next(current)) return true;
} while ((current = adapter.getParent(current)));
if (next(current)) return true;
} while ((current = getElementParent(current, adapter)));

return false;
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as DomUtils from "domutils";
import { falseFunc } from "boolbase";
import type {
Node as DomHandlerNode,
AnyNode as DomHandlerNode,
Element as DomHandlerElement,
} from "domhandler";
import { compile as compileRaw, compileUnsafe, compileToken } from "./compile";
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface Adapter<Node, ElementNode extends Node> {
/**
* Get the parent of the node
*/
getParent: (node: ElementNode) => ElementNode | null;
getParent: (node: ElementNode) => Node | null;

/**
* Get the siblings of the node. Note that unlike jQuery's `siblings` method,
Expand Down
11 changes: 5 additions & 6 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import * as CSSselect from "../src";
import { parseDOM, parseDocument } from "htmlparser2";
import { trueFunc, falseFunc } from "boolbase";
import * as DomUtils from "domutils";
import type { Node, Element } from "domhandler";
import type { Element } from "domhandler";
import { SelectorType, AttributeAction } from "css-what";
import { Adapter } from "../src/types";

const [dom] = parseDOM("<div id=foo><p>foo</p></div>") as Element[];
const [xmlDom] = parseDOM("<DiV id=foo><P>foo</P></DiV>", {
Expand Down Expand Up @@ -71,7 +70,7 @@ describe("API", () => {
) as Element[];
const a = CSSselect.selectAll(".foo:has(+.bar)", dom);
expect(a).toHaveLength(1);
expect(a[0]).toStrictEqual(dom.children[0] as Element);
expect(a[0]).toStrictEqual(dom.children[0]);
});

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

describe("optional adapter methods", () => {
it("should support prevElementSibling", () => {
const adapter: Adapter<Node, Element> = {
const adapter = {
...DomUtils,
prevElementSibling: undefined,
};
Expand All @@ -334,9 +333,9 @@ describe("API", () => {
it("should support isHovered", () => {
const dom = parseDOM(`${"<p>foo".repeat(10)}`) as Element[];

const adapter: Adapter<Node, Element> = {
const adapter = {
...DomUtils,
isHovered: (el) => el === dom[dom.length - 1],
isHovered: (el: Element) => el === dom[dom.length - 1],
};

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