Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
1,227 changes: 649 additions & 578 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"htmlparser2"
],
"dependencies": {
"dom-serializer": "^1.0.1",
"dom-serializer": "^2.0.0",
"domelementtype": "^2.3.0",
"domhandler": "^4.3.1"
"domhandler": "^5.0.1"
},
"devDependencies": {
"@types/jest": "^27.4.1",
Expand Down
19 changes: 17 additions & 2 deletions src/__fixtures__/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { parseDOM } from "htmlparser2";
import { Parser, ParserOptions } from "htmlparser2";
import DomHandler, { Document } from "domhandler";

/**
* Re-implementation of htmlparser2's `parseDocument` function.
*
* @param str String to parse.
* @param options Optional parser options to use.
* @returns The parsed document.
*/
export function parseDocument(str: string, options?: ParserOptions): Document {
const handler = new DomHandler();
new Parser(handler, options).end(str);
return handler.root;
}

const markup = Array(21).join(
"<?xml><tag1 id='asdf'> <script>text</script> <!-- comment --> <tag2> text </tag1>"
);

export default parseDOM(markup);
export default parseDocument(markup).children;
2 changes: 1 addition & 1 deletion src/feeds.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { getFeed } from "./feeds";
import fs from "fs";
import path from "path";
import { parseDocument } from "htmlparser2";
import { parseDocument } from "./__fixtures__/fixture";

const documents = path.join(__dirname, "__fixtures__", "Documents");

Expand Down
16 changes: 10 additions & 6 deletions src/feeds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node, Element } from "domhandler";
import type { AnyNode, Element } from "domhandler";
import { textContent } from "./stringify";
import { getElementsByTagName } from "./legacy";

Expand Down Expand Up @@ -80,7 +80,7 @@ export interface Feed {
* @param doc - The DOM to to extract the feed from.
* @returns The feed.
*/
export function getFeed(doc: Node[]): Feed | null {
export function getFeed(doc: AnyNode[]): Feed | null {
const feedRoot = getOneElement(isValidFeed, doc);

return !feedRoot
Expand Down Expand Up @@ -206,7 +206,7 @@ const MEDIA_KEYS_INT = [
* @param where Nodes to search in.
* @returns Media elements.
*/
function getMediaElements(where: Node | Node[]): FeedItemMedia[] {
function getMediaElements(where: AnyNode[]): FeedItemMedia[] {
return getElementsByTagName("media:content", where).map((elem) => {
const { attribs } = elem;

Expand Down Expand Up @@ -247,7 +247,7 @@ function getMediaElements(where: Node | Node[]): FeedItemMedia[] {
*/
function getOneElement(
tagName: string | ((name: string) => boolean),
node: Node | Node[]
node: AnyNode[]
): Element | null {
return getElementsByTagName(tagName, node, true, 1)[0];
}
Expand All @@ -260,7 +260,11 @@ function getOneElement(
* @param recurse Whether to recurse into child nodes.
* @returns The text content of the element.
*/
function fetch(tagName: string, where: Node | Node[], recurse = false): string {
function fetch(
tagName: string,
where: AnyNode | AnyNode[],
recurse = false
): string {
return textContent(getElementsByTagName(tagName, where, recurse, 1)).trim();
}

Expand All @@ -277,7 +281,7 @@ function addConditionally<T>(
obj: T,
prop: keyof T,
tagName: string,
where: Node | Node[],
where: AnyNode[],
recurse = false
) {
const val = fetch(tagName, where, recurse);
Expand Down
16 changes: 7 additions & 9 deletions src/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { parseDOM, parseDocument } from "htmlparser2";
import { parseDocument } from "./__fixtures__/fixture";
import { removeSubsets, compareDocumentPosition, uniqueSort } from "./helpers";
import type { Element, Document } from "domhandler";

describe("helpers", () => {
describe("removeSubsets", () => {
const dom = parseDOM(
"<div><p><span></span></p><p></p></div>"
)[0] as Element;
const dom = parseDocument("<div><p><span></span></p><p></p></div>")
.children[0] as Element;

it("removes identical trees", () =>
expect(removeSubsets([dom, dom])).toHaveLength(1));
Expand All @@ -28,7 +27,7 @@ describe("helpers", () => {

describe("compareDocumentPosition", () => {
const markup = "<div><p><span></span></p><a></a></div>";
const dom = parseDOM(markup)[0] as Element;
const dom = parseDocument(markup).children[0] as Element;
const p = dom.children[0] as Element;
const span = p.children[0];
const a = dom.children[1];
Expand All @@ -46,7 +45,7 @@ describe("helpers", () => {
expect(compareDocumentPosition(span, p)).toBe(20));

it("reports when the nodes belong to separate documents", () => {
const otherDom = parseDOM(markup)[0] as Element;
const otherDom = parseDocument(markup).children[0] as Element;
const other = (otherDom.children[0] as Element).children[0];

expect(compareDocumentPosition(span, other)).toBe(1);
Expand All @@ -56,9 +55,8 @@ describe("helpers", () => {
expect(compareDocumentPosition(span, span)).toBe(0));

it("does not end up in infinite loop (#109)", () => {
const dom = parseDOM(
"<div><span>1</span><span>2</span></div>"
)[0] as Element;
const dom = parseDocument("<div><span>1</span><span>2</span></div>")
.children[0] as Element;

expect(
compareDocumentPosition(
Expand Down
17 changes: 10 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasChildren, Node } from "domhandler";
import { hasChildren, AnyNode, ParentNode } from "domhandler";

/**
* Given an array of nodes, remove any member that is contained by another.
Expand All @@ -7,7 +7,7 @@ import { hasChildren, Node } from "domhandler";
* @param nodes Nodes to filter.
* @returns Remaining nodes that aren't subtrees of each other.
*/
export function removeSubsets(nodes: Node[]): Node[] {
export function removeSubsets(nodes: AnyNode[]): AnyNode[] {
let idx = nodes.length;

/*
Expand Down Expand Up @@ -75,9 +75,12 @@ export const enum DocumentPosition {
* See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for
* a description of these values.
*/
export function compareDocumentPosition(nodeA: Node, nodeB: Node): number {
const aParents = [];
const bParents = [];
export function compareDocumentPosition(
nodeA: AnyNode,
nodeB: AnyNode
): number {
const aParents: ParentNode[] = [];
const bParents: ParentNode[] = [];

if (nodeA === nodeB) {
return 0;
Expand Down Expand Up @@ -105,7 +108,7 @@ export function compareDocumentPosition(nodeA: Node, nodeB: Node): number {
}

const sharedParent = aParents[idx - 1];
const siblings = sharedParent.children;
const siblings: AnyNode[] = sharedParent.children;
const aSibling = aParents[idx];
const bSibling = bParents[idx];

Expand All @@ -130,7 +133,7 @@ export function compareDocumentPosition(nodeA: Node, nodeB: Node): number {
* @param nodes Array of DOM nodes.
* @returns Collection of unique nodes, sorted in document order.
*/
export function uniqueSort<T extends Node>(nodes: T[]): T[] {
export function uniqueSort<T extends AnyNode>(nodes: T[]): T[] {
nodes = nodes.filter((node, i, arr) => !arr.includes(node, i + 1));

nodes.sort((a, b) => {
Expand Down
6 changes: 3 additions & 3 deletions src/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
getElementsByTagName,
getElementsByTagType,
} from "./legacy";
import type { Node, Element } from "domhandler";
import type { AnyNode, Element } from "domhandler";

describe("legacy", () => {
// Set up expected structures
const expected = {
idAsdf: fixture[1] as Element,
tag2: [] as Node[],
typeScript: [] as Node[],
tag2: [] as AnyNode[],
typeScript: [] as AnyNode[],
};

beforeAll(() => {
Expand Down
36 changes: 18 additions & 18 deletions src/legacy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isTag, isText, Node, Element } from "domhandler";
import { isTag, isText, AnyNode, Element } from "domhandler";
import { ElementType } from "domelementtype";
import { filter, findOne } from "./querying";

type TestType = (elem: Node) => boolean;
type TestType = (elem: AnyNode) => boolean;

/**
* An object with keys to check elements against. If a key is `tag_name`,
Expand All @@ -27,23 +27,23 @@ const Checks: Record<
> = {
tag_name(name) {
if (typeof name === "function") {
return (elem: Node) => isTag(elem) && name(elem.name);
return (elem: AnyNode) => isTag(elem) && name(elem.name);
} else if (name === "*") {
return isTag;
}
return (elem: Node) => isTag(elem) && elem.name === name;
return (elem: AnyNode) => isTag(elem) && elem.name === name;
},
tag_type(type) {
if (typeof type === "function") {
return (elem: Node) => type(elem.type);
return (elem: AnyNode) => type(elem.type);
}
return (elem: Node) => elem.type === type;
return (elem: AnyNode) => elem.type === type;
},
tag_contains(data) {
if (typeof data === "function") {
return (elem: Node) => isText(elem) && data(elem.data);
return (elem: AnyNode) => isText(elem) && data(elem.data);
}
return (elem: Node) => isText(elem) && elem.data === data;
return (elem: AnyNode) => isText(elem) && elem.data === data;
},
};

Expand All @@ -58,9 +58,9 @@ function getAttribCheck(
value: undefined | string | ((value: string) => boolean)
): TestType {
if (typeof value === "function") {
return (elem: Node) => isTag(elem) && value(elem.attribs[attrib]);
return (elem: AnyNode) => isTag(elem) && value(elem.attribs[attrib]);
}
return (elem: Node) => isTag(elem) && elem.attribs[attrib] === value;
return (elem: AnyNode) => isTag(elem) && elem.attribs[attrib] === value;
}

/**
Expand All @@ -70,7 +70,7 @@ function getAttribCheck(
* functions returns `true` for the node.
*/
function combineFuncs(a: TestType, b: TestType): TestType {
return (elem: Node) => a(elem) || b(elem);
return (elem: AnyNode) => a(elem) || b(elem);
}

/**
Expand All @@ -95,7 +95,7 @@ function compileTest(options: TestElementOpts): TestType | null {
* @param node The element to test.
* @returns Whether the element matches the description in `options`.
*/
export function testElement(options: TestElementOpts, node: Node): boolean {
export function testElement(options: TestElementOpts, node: AnyNode): boolean {
const test = compileTest(options);
return test ? test(node) : true;
}
Expand All @@ -110,10 +110,10 @@ export function testElement(options: TestElementOpts, node: Node): boolean {
*/
export function getElements(
options: TestElementOpts,
nodes: Node | Node[],
nodes: AnyNode | AnyNode[],
recurse: boolean,
limit = Infinity
): Node[] {
): AnyNode[] {
const test = compileTest(options);
return test ? filter(test, nodes, recurse, limit) : [];
}
Expand All @@ -127,7 +127,7 @@ export function getElements(
*/
export function getElementById(
id: string | ((id: string) => boolean),
nodes: Node | Node[],
nodes: AnyNode | AnyNode[],
recurse = true
): Element | null {
if (!Array.isArray(nodes)) nodes = [nodes];
Expand All @@ -144,7 +144,7 @@ export function getElementById(
*/
export function getElementsByTagName(
tagName: string | ((name: string) => boolean),
nodes: Node | Node[],
nodes: AnyNode | AnyNode[],
recurse = true,
limit = Infinity
): Element[] {
Expand All @@ -161,9 +161,9 @@ export function getElementsByTagName(
*/
export function getElementsByTagType(
type: ElementType | ((type: ElementType) => boolean),
nodes: Node | Node[],
nodes: AnyNode | AnyNode[],
recurse = true,
limit = Infinity
): Node[] {
): AnyNode[] {
return filter(Checks.tag_type(type as string), nodes, recurse, limit);
}
14 changes: 7 additions & 7 deletions src/manipulation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Node, Element } from "domhandler";
import type { ChildNode, Element } from "domhandler";

/**
* Remove an element from the dom
*
* @category Manipulation
* @param elem The element to be removed
*/
export function removeElement(elem: Node): void {
export function removeElement(elem: ChildNode): void {
if (elem.prev) elem.prev.next = elem.next;
if (elem.next) elem.next.prev = elem.prev;

Expand All @@ -23,7 +23,7 @@ export function removeElement(elem: Node): void {
* @param elem The element to be replaced
* @param replacement The element to be added
*/
export function replaceElement(elem: Node, replacement: Node): void {
export function replaceElement(elem: ChildNode, replacement: ChildNode): void {
const prev = (replacement.prev = elem.prev);
if (prev) {
prev.next = replacement;
Expand All @@ -48,7 +48,7 @@ export function replaceElement(elem: Node, replacement: Node): void {
* @param elem The element to append to.
* @param child The element to be added as a child.
*/
export function appendChild(elem: Element, child: Node): void {
export function appendChild(elem: Element, child: ChildNode): void {
removeElement(child);

child.next = null;
Expand All @@ -70,7 +70,7 @@ export function appendChild(elem: Element, child: Node): void {
* @param elem The element to append after.
* @param next The element be added.
*/
export function append(elem: Node, next: Node): void {
export function append(elem: ChildNode, next: ChildNode): void {
removeElement(next);

const { parent } = elem;
Expand Down Expand Up @@ -99,7 +99,7 @@ export function append(elem: Node, next: Node): void {
* @param elem The element to prepend before.
* @param child The element to be added as a child.
*/
export function prependChild(elem: Element, child: Node): void {
export function prependChild(elem: Element, child: ChildNode): void {
removeElement(child);

child.parent = elem;
Expand All @@ -121,7 +121,7 @@ export function prependChild(elem: Element, child: Node): void {
* @param elem The element to prepend before.
* @param prev The element be added.
*/
export function prepend(elem: Node, prev: Node): void {
export function prepend(elem: ChildNode, prev: ChildNode): void {
removeElement(prev);

const { parent } = elem;
Expand Down
Loading