Skip to content

Commit

Permalink
build(deps): bump css-what, add domhandler as dep
Browse files Browse the repository at this point in the history
And clean up uses of domutils
  • Loading branch information
fb55 committed Oct 1, 2020
1 parent b625e13 commit cbb7187
Show file tree
Hide file tree
Showing 7 changed files with 621 additions and 1,016 deletions.
1,573 changes: 594 additions & 979 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
],
"dependencies": {
"boolbase": "^1.0.0",
"css-what": "^3.3.0",
"domutils": "^2.4.0",
"css-what": "^3.4.1",
"domhandler": "^3.2.0",
"domutils": "^2.4.1",
"nth-check": "^1.0.2"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions test/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as CSSselect from "../src";
import { parseDOM as makeDom } from "htmlparser2";
import { parseDOM } from "htmlparser2";
import { trueFunc, falseFunc } from "boolbase";
import type { Element } from "domhandler";

const [dom] = makeDom("<div id=foo><p>foo</p></div>") as Element[];
const [xmlDom] = makeDom("<DiV id=foo><P>foo</P></DiV>", {
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[];

Expand Down Expand Up @@ -44,7 +44,7 @@ describe("API", () => {
expect(CSSselect.selectAll("p", ps)).toStrictEqual(ps);
});
it("should support pseudos led by a traversal (#111)", () => {
const [dom] = makeDom(
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);
Expand Down
4 changes: 2 additions & 2 deletions test/nwmatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ describe("NWMatcher", () => {
describe("Basic Selectors", () => {
it("*", () => {
// Universal selector
const results = document.getElementsByTagName("*");
const results = DomUtils.getElementsByTagName("*", document);
// Comment nodes should be ignored.
expect(select("*")).toStrictEqual(results);
});

it("E", () => {
// Type selector
const nodes = document.getElementsByTagName("li");
const nodes = DomUtils.getElementsByTagName("li", document);
expect(select("li")).toStrictEqual(nodes);
expect(select("strong", getById("fixtures"))[0]).toBe(
getById("strong")
Expand Down
28 changes: 13 additions & 15 deletions test/qwery.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { DomUtils } from "htmlparser2";
import * as helper from "./tools/helper";
const document = helper.getDocument("qwery.html");
import * as CSSselect from "../src";
import type { Node, Element } from "domhandler";
import * as DomUtils from "domutils";
import type { Element } from "domhandler";
import { parseDOM } from "htmlparser2";

const location = { hash: "" };
CSSselect.pseudos.target = (elem, { adapter }) =>
adapter.getAttributeValue(elem, "id") === location.hash.substr(1);

const getElementsByTagName = (id: string, document: Node | Node[]) =>
DomUtils.getElementsByTagName(id, document, true);
// ---

/*
Expand Down Expand Up @@ -592,7 +590,7 @@ describe("qwery", () => {
});

it(":last-child", () => {
const all = getElementsByTagName("div", pseudos);
const all = DomUtils.getElementsByTagName("div", pseudos);
expect(
CSSselect.selectAll("#pseudos div:last-child", document)[0]
).toBe(all[all.length - 1]); // Found last child
Expand Down Expand Up @@ -621,7 +619,7 @@ describe("qwery", () => {
});

it(":nth-child(odd|even|x)", () => {
const second = getElementsByTagName("div", pseudos)[1];
const second = DomUtils.getElementsByTagName("div", pseudos)[1];
expect(
CSSselect.selectAll("#pseudos :nth-child(odd)", document)
).toHaveLength(4); // Found 4 odd elements
Expand All @@ -637,8 +635,8 @@ describe("qwery", () => {
});

it(":nth-child(expr)", () => {
const fifth = getElementsByTagName("a", pseudos)[0];
const sixth = getElementsByTagName("div", pseudos)[4];
const fifth = DomUtils.getElementsByTagName("a", pseudos)[0];
const sixth = DomUtils.getElementsByTagName("div", pseudos)[4];

expect(
CSSselect.selectAll("#pseudos :nth-child(3n+1)", document)
Expand All @@ -661,7 +659,7 @@ describe("qwery", () => {
});

it(":nth-last-child(odd|even|x)", () => {
const second = getElementsByTagName("div", pseudos)[1];
const second = DomUtils.getElementsByTagName("div", pseudos)[1];
expect(
CSSselect.selectAll("#pseudos :nth-last-child(odd)", document)
).toHaveLength(4); // Found 4 odd elements
Expand All @@ -686,7 +684,7 @@ describe("qwery", () => {
});

it(":nth-last-child(expr)", () => {
const third = getElementsByTagName("div", pseudos)[2];
const third = DomUtils.getElementsByTagName("div", pseudos)[2];

expect(
CSSselect.selectAll("#pseudos :nth-last-child(3n+1)", document)
Expand All @@ -709,7 +707,7 @@ describe("qwery", () => {
});

it(":nth-of-type(expr)", () => {
const a = getElementsByTagName("a", pseudos)[0];
const a = DomUtils.getElementsByTagName("a", pseudos)[0];

expect(
CSSselect.selectAll("#pseudos div:nth-of-type(3n+1)", document)
Expand All @@ -732,7 +730,7 @@ describe("qwery", () => {
});

it(":nth-last-of-type(expr)", () => {
const second = getElementsByTagName("div", pseudos)[1];
const second = DomUtils.getElementsByTagName("div", pseudos)[1];

expect(
CSSselect.selectAll(
Expand All @@ -757,14 +755,14 @@ describe("qwery", () => {
it(":first-of-type", () => {
expect(
CSSselect.selectAll("#pseudos a:first-of-type", document)[0]
).toBe(getElementsByTagName("a", pseudos)[0]); // Found first a element
).toBe(DomUtils.getElementsByTagName("a", pseudos)[0]); // Found first a element
expect(
CSSselect.selectAll("#pseudos a:first-of-type", document)
).toHaveLength(1); // Found only 1
});

it(":last-of-type", () => {
const all = getElementsByTagName("div", pseudos);
const all = DomUtils.getElementsByTagName("div", pseudos);
expect(
CSSselect.selectAll("#pseudos div:last-of-type", document)[0]
).toBe(all[all.length - 1]); // Found last div element
Expand All @@ -776,7 +774,7 @@ describe("qwery", () => {
it(":only-of-type", () => {
expect(
CSSselect.selectAll("#pseudos a:only-of-type", document)[0]
).toBe(getElementsByTagName("a", pseudos)[0]); // Found the only a element
).toBe(DomUtils.getElementsByTagName("a", pseudos)[0]); // Found the only a element
expect(
CSSselect.selectAll("#pseudos a:first-of-type", document)
).toHaveLength(1); // Found only 1
Expand Down
5 changes: 1 addition & 4 deletions test/sizzle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as DomUtils from "domutils";
import * as helper from "./tools/helper";
import * as CSSselect from "../src";
import type { Element } from "domhandler";
import { q, t, createWithFriesXML, loadDoc } from "./tools/sizzle-testinit";
Expand Down Expand Up @@ -934,9 +933,7 @@ describe("Sizzle", () => {

// #3279
const div = document.createElement("div");
div.children = parseDOM(
"<div id='foo' xml:test='something'></div>"
);
div.children = parseDOM("<div id='foo' xml:test='something'></div>");

// Finding by attribute with escaped characters.
expect(CSSselect.selectAll("[xml\\:test]", div)).toStrictEqual([
Expand Down
14 changes: 4 additions & 10 deletions test/tools/helper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import fs from "fs";
import path from "path";
import * as htmlparser2 from "htmlparser2";
import { parseDOM, ParserOptions, ElementType } from "htmlparser2";
import * as DomUtils from "domutils";
import { DataNode, Element, Node } from "domhandler";

export function getDOMFromPath(
file: string,
options?: htmlparser2.ParserOptions
): Node[] {
export function getDOMFromPath(file: string, options?: ParserOptions): Node[] {
const filePath = path.join(__dirname, "..", "fixtures", file);
return htmlparser2.parseDOM(fs.readFileSync(filePath, "utf8"), options);
return parseDOM(fs.readFileSync(filePath, "utf8"), options);
}

export interface SimpleDocument extends Array<Node> {
getElementsByTagName(name: string): Element[];
getElementById(id: string): Element;
createTextNode(content: string): DataNode;
createElement(name: string): Element;
Expand All @@ -24,12 +20,10 @@ export interface SimpleDocument extends Array<Node> {
export function getDocument(file: string): SimpleDocument {
const document = getDOMFromPath(file) as SimpleDocument;

document.getElementsByTagName = (name = "*") =>
DomUtils.getElementsByTagName(name, document, true);
document.getElementById = (id: string) =>
DomUtils.getElementById(id, document) as Element;
document.createTextNode = (content: string) =>
new DataNode(htmlparser2.ElementType.Text, content);
new DataNode(ElementType.Text, content);
document.createElement = (name: string) =>
new Element(name.toLocaleLowerCase(), {});
[document.body] = DomUtils.getElementsByTagName("body", document, true, 1);
Expand Down

0 comments on commit cbb7187

Please sign in to comment.