Socket
Socket
Sign inDemoInstall

domutils

Package Overview
Dependencies
4
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.1 to 3.1.0

6

lib/esm/feeds.d.ts
import type { AnyNode } from "domhandler";
/**
* The type of a media item.
* The medium of a media item.
*
* @category Feeds
*/
export declare type FeedItemMediaMedium = "image" | "audio" | "video" | "document" | "executable";
export type FeedItemMediaMedium = "image" | "audio" | "video" | "document" | "executable";
/**

@@ -13,3 +13,3 @@ * The type of a media item.

*/
export declare type FeedItemMediaExpression = "sample" | "full" | "nonstop";
export type FeedItemMediaExpression = "sample" | "full" | "nonstop";
/**

@@ -16,0 +16,0 @@ * A media item of a feed entry.

@@ -83,3 +83,3 @@ import { textContent } from "./stringify.js";

addConditionally(entry, "description", "description", children);
const pubDate = fetch("pubDate", children);
const pubDate = fetch("pubDate", children) || fetch("dc:date", children);
if (pubDate)

@@ -86,0 +86,0 @@ entry.pubDate = new Date(pubDate);

import { AnyNode } from "domhandler";
/**
* Given an array of nodes, remove any member that is contained by another.
* Given an array of nodes, remove any member that is contained by another
* member.
*
* @category Helpers
* @param nodes Nodes to filter.
* @returns Remaining nodes that aren't subtrees of each other.
* @returns Remaining nodes that aren't contained by other nodes.
*/

@@ -22,4 +23,4 @@ export declare function removeSubsets(nodes: AnyNode[]): AnyNode[];

/**
* Compare the position of one node against another node in any other document.
* The return value is a bitmask with the values from {@link DocumentPosition}.
* Compare the position of one node against another node in any other document,
* returning a bitmask with the values from {@link DocumentPosition}.
*

@@ -50,5 +51,5 @@ * Document order:

/**
* Sort an array of nodes based on their relative position in the document and
* remove any duplicate nodes. If the array contains nodes that do not belong to
* the same document, sort order is unspecified.
* Sort an array of nodes based on their relative position in the document,
* removing any duplicate nodes. If the array contains nodes that do not belong
* to the same document, sort order is unspecified.
*

@@ -55,0 +56,0 @@ * @category Helpers

import { hasChildren } from "domhandler";
/**
* Given an array of nodes, remove any member that is contained by another.
* Given an array of nodes, remove any member that is contained by another
* member.
*
* @category Helpers
* @param nodes Nodes to filter.
* @returns Remaining nodes that aren't subtrees of each other.
* @returns Remaining nodes that aren't contained by other nodes.
*/

@@ -48,4 +49,4 @@ export function removeSubsets(nodes) {

/**
* Compare the position of one node against another node in any other document.
* The return value is a bitmask with the values from {@link DocumentPosition}.
* Compare the position of one node against another node in any other document,
* returning a bitmask with the values from {@link DocumentPosition}.
*

@@ -114,5 +115,5 @@ * Document order:

/**
* Sort an array of nodes based on their relative position in the document and
* remove any duplicate nodes. If the array contains nodes that do not belong to
* the same document, sort order is unspecified.
* Sort an array of nodes based on their relative position in the document,
* removing any duplicate nodes. If the array contains nodes that do not belong
* to the same document, sort order is unspecified.
*

@@ -119,0 +120,0 @@ * @category Helpers

@@ -17,2 +17,4 @@ import { AnyNode, Element } from "domhandler";

/**
* Checks whether a node matches the description in `options`.
*
* @category Legacy Query Functions

@@ -25,2 +27,4 @@ * @param options An object describing nodes to look for.

/**
* Returns all nodes that match `options`.
*
* @category Legacy Query Functions

@@ -35,2 +39,4 @@ * @param options An object describing nodes to look for.

/**
* Returns the node with the supplied ID.
*
* @category Legacy Query Functions

@@ -44,2 +50,4 @@ * @param id The unique ID attribute value to look for.

/**
* Returns all nodes with the supplied `tagName`.
*
* @category Legacy Query Functions

@@ -54,2 +62,4 @@ * @param tagName Tag name to search for.

/**
* Returns all nodes with the supplied `type`.
*
* @category Legacy Query Functions

@@ -56,0 +66,0 @@ * @param type Element type to look for.

import { isTag, isText } from "domhandler";
import { filter, findOne } from "./querying.js";
/**
* A map of functions to check nodes against.
*/
const Checks = {

@@ -27,2 +30,5 @@ tag_name(name) {

/**
* Returns a function to check whether a node has an attribute with a particular
* value.
*
* @param attrib Attribute to check.

@@ -40,2 +46,5 @@ * @param value Attribute value to look for.

/**
* Returns a function that returns `true` if either of the input functions
* returns `true` for a node.
*
* @param a First function to combine.

@@ -50,5 +59,8 @@ * @param b Second function to combine.

/**
* Returns a function that executes all checks in `options` and returns `true`
* if any of them match a node.
*
* @param options An object describing nodes to look for.
* @returns A function executing all checks in `options` and returning `true` if
* any of them match a node.
* @returns A function that executes all checks in `options` and returns `true`
* if any of them match a node.
*/

@@ -65,2 +77,4 @@ function compileTest(options) {

/**
* Checks whether a node matches the description in `options`.
*
* @category Legacy Query Functions

@@ -76,2 +90,4 @@ * @param options An object describing nodes to look for.

/**
* Returns all nodes that match `options`.
*
* @category Legacy Query Functions

@@ -89,2 +105,4 @@ * @param options An object describing nodes to look for.

/**
* Returns the node with the supplied ID.
*
* @category Legacy Query Functions

@@ -102,2 +120,4 @@ * @param id The unique ID attribute value to look for.

/**
* Returns all nodes with the supplied `tagName`.
*
* @category Legacy Query Functions

@@ -114,2 +134,4 @@ * @param tagName Tag name to search for.

/**
* Returns all nodes with the supplied `type`.
*
* @category Legacy Query Functions

@@ -116,0 +138,0 @@ * @param type Element type to look for.

@@ -1,2 +0,2 @@

import type { ChildNode, Element } from "domhandler";
import type { ChildNode, ParentNode } from "domhandler";
/**

@@ -21,6 +21,6 @@ * Remove an element from the dom

* @category Manipulation
* @param elem The element to append to.
* @param parent The element to append to.
* @param child The element to be added as a child.
*/
export declare function appendChild(elem: Element, child: ChildNode): void;
export declare function appendChild(parent: ParentNode, child: ChildNode): void;
/**

@@ -38,6 +38,6 @@ * Append an element after another.

* @category Manipulation
* @param elem The element to prepend before.
* @param parent The element to prepend before.
* @param child The element to be added as a child.
*/
export declare function prependChild(elem: Element, child: ChildNode): void;
export declare function prependChild(parent: ParentNode, child: ChildNode): void;
/**

@@ -44,0 +44,0 @@ * Prepend an element before another.

@@ -14,4 +14,10 @@ /**

const childs = elem.parent.children;
childs.splice(childs.lastIndexOf(elem), 1);
const childsIndex = childs.lastIndexOf(elem);
if (childsIndex >= 0) {
childs.splice(childsIndex, 1);
}
}
elem.next = null;
elem.prev = null;
elem.parent = null;
}

@@ -45,11 +51,11 @@ /**

* @category Manipulation
* @param elem The element to append to.
* @param parent The element to append to.
* @param child The element to be added as a child.
*/
export function appendChild(elem, child) {
export function appendChild(parent, child) {
removeElement(child);
child.next = null;
child.parent = elem;
if (elem.children.push(child) > 1) {
const sibling = elem.children[elem.children.length - 2];
child.parent = parent;
if (parent.children.push(child) > 1) {
const sibling = parent.children[parent.children.length - 2];
sibling.next = child;

@@ -92,11 +98,11 @@ child.prev = sibling;

* @category Manipulation
* @param elem The element to prepend before.
* @param parent The element to prepend before.
* @param child The element to be added as a child.
*/
export function prependChild(elem, child) {
export function prependChild(parent, child) {
removeElement(child);
child.parent = elem;
child.parent = parent;
child.prev = null;
if (elem.children.unshift(child) !== 1) {
const sibling = elem.children[1];
if (parent.children.unshift(child) !== 1) {
const sibling = parent.children[1];
sibling.prev = child;

@@ -103,0 +109,0 @@ child.next = sibling;

import { Element, AnyNode } from "domhandler";
/**
* Search a node and its children for nodes passing a test function.
* Search a node and its children for nodes passing a test function. If `node` is not an array, it will be wrapped in one.
*

@@ -14,3 +14,3 @@ * @category Querying

/**
* Search an array of node and its children for nodes passing a test function.
* Search an array of nodes and their children for nodes passing a test function.
*

@@ -26,3 +26,3 @@ * @category Querying

/**
* Finds the first element inside of an array that matches a test function.
* Finds the first element inside of an array that matches a test function. This is an alias for `Array.prototype.find`.
*

@@ -41,8 +41,10 @@ * @category Querying

* @param test Function to test nodes on.
* @param nodes Array of nodes to search.
* @param nodes Node or array of nodes to search.
* @param recurse Also consider child nodes.
* @returns The first child node that passes `test`.
* @returns The first node that passes `test`.
*/
export declare function findOne(test: (elem: Element) => boolean, nodes: AnyNode[], recurse?: boolean): Element | null;
/**
* Checks if a tree of nodes contains at least one node passing a test.
*
* @category Querying

@@ -55,3 +57,3 @@ * @param test Function to test nodes on.

/**
* Search and array of nodes and its children for elements passing a test function.
* Search an array of nodes and their children for elements passing a test function.
*

@@ -58,0 +60,0 @@ * Same as `find`, but limited to elements and with less options, leading to reduced complexity.

import { isTag, hasChildren } from "domhandler";
/**
* Search a node and its children for nodes passing a test function.
* Search a node and its children for nodes passing a test function. If `node` is not an array, it will be wrapped in one.
*

@@ -13,8 +13,6 @@ * @category Querying

export function filter(test, node, recurse = true, limit = Infinity) {
if (!Array.isArray(node))
node = [node];
return find(test, node, recurse, limit);
return find(test, Array.isArray(node) ? node : [node], recurse, limit);
}
/**
* Search an array of node and its children for nodes passing a test function.
* Search an array of nodes and their children for nodes passing a test function.
*

@@ -30,20 +28,37 @@ * @category Querying

const result = [];
for (const elem of nodes) {
/** Stack of the arrays we are looking at. */
const nodeStack = [nodes];
/** Stack of the indices within the arrays. */
const indexStack = [0];
for (;;) {
// First, check if the current array has any more elements to look at.
if (indexStack[0] >= nodeStack[0].length) {
// If we have no more arrays to look at, we are done.
if (indexStack.length === 1) {
return result;
}
// Otherwise, remove the current array from the stack.
nodeStack.shift();
indexStack.shift();
// Loop back to the start to continue with the next array.
continue;
}
const elem = nodeStack[0][indexStack[0]++];
if (test(elem)) {
result.push(elem);
if (--limit <= 0)
break;
return result;
}
if (recurse && hasChildren(elem) && elem.children.length > 0) {
const children = find(test, elem.children, recurse, limit);
result.push(...children);
limit -= children.length;
if (limit <= 0)
break;
/*
* Add the children to the stack. We are depth-first, so this is
* the next array we look at.
*/
indexStack.unshift(0);
nodeStack.unshift(elem.children);
}
}
return result;
}
/**
* Finds the first element inside of an array that matches a test function.
* Finds the first element inside of an array that matches a test function. This is an alias for `Array.prototype.find`.
*

@@ -64,5 +79,5 @@ * @category Querying

* @param test Function to test nodes on.
* @param nodes Array of nodes to search.
* @param nodes Node or array of nodes to search.
* @param recurse Also consider child nodes.
* @returns The first child node that passes `test`.
* @returns The first node that passes `test`.
*/

@@ -72,11 +87,11 @@ export function findOne(test, nodes, recurse = true) {

for (let i = 0; i < nodes.length && !elem; i++) {
const checked = nodes[i];
if (!isTag(checked)) {
const node = nodes[i];
if (!isTag(node)) {
continue;
}
else if (test(checked)) {
elem = checked;
else if (test(node)) {
elem = node;
}
else if (recurse && checked.children.length > 0) {
elem = findOne(test, checked.children, true);
else if (recurse && node.children.length > 0) {
elem = findOne(test, node.children, true);
}

@@ -87,2 +102,4 @@ }

/**
* Checks if a tree of nodes contains at least one node passing a test.
*
* @category Querying

@@ -95,8 +112,6 @@ * @param test Function to test nodes on.

return nodes.some((checked) => isTag(checked) &&
(test(checked) ||
(checked.children.length > 0 &&
existsOne(test, checked.children))));
(test(checked) || existsOne(test, checked.children)));
}
/**
* Search and array of nodes and its children for elements passing a test function.
* Search an array of nodes and their children for elements passing a test function.
*

@@ -111,16 +126,27 @@ * Same as `find`, but limited to elements and with less options, leading to reduced complexity.

export function findAll(test, nodes) {
var _a;
const result = [];
const stack = nodes.filter(isTag);
let elem;
while ((elem = stack.shift())) {
const children = (_a = elem.children) === null || _a === void 0 ? void 0 : _a.filter(isTag);
if (children && children.length > 0) {
stack.unshift(...children);
const nodeStack = [nodes];
const indexStack = [0];
for (;;) {
if (indexStack[0] >= nodeStack[0].length) {
if (nodeStack.length === 1) {
return result;
}
// Otherwise, remove the current array from the stack.
nodeStack.shift();
indexStack.shift();
// Loop back to the start to continue with the next array.
continue;
}
const elem = nodeStack[0][indexStack[0]++];
if (!isTag(elem))
continue;
if (test(elem))
result.push(elem);
if (elem.children.length > 0) {
indexStack.unshift(0);
nodeStack.unshift(elem.children);
}
}
return result;
}
//# sourceMappingURL=querying.js.map

@@ -20,3 +20,3 @@ import { AnyNode } from "domhandler";

/**
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags. Ignores comments.
*

@@ -30,3 +30,3 @@ * @category Stringify

/**
* Get a node's text content.
* Get a node's text content. Ignores comments.
*

@@ -40,3 +40,3 @@ * @category Stringify

/**
* Get a node's inner text.
* Get a node's inner text, ignoring `<script>` and `<style>` tags. Ignores comments.
*

@@ -43,0 +43,0 @@ * @category Stringify

@@ -27,3 +27,3 @@ import { isTag, isCDATA, isText, hasChildren, isComment, } from "domhandler";

/**
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags. Ignores comments.
*

@@ -47,3 +47,3 @@ * @category Stringify

/**
* Get a node's text content.
* Get a node's text content. Ignores comments.
*

@@ -66,3 +66,3 @@ * @category Stringify

/**
* Get a node's inner text.
* Get a node's inner text, ignoring `<script>` and `<style>` tags. Ignores comments.
*

@@ -69,0 +69,0 @@ * @category Stringify

@@ -20,3 +20,3 @@ import { AnyNode, ChildNode, Element, ParentNode } from "domhandler";

* @param elem Element to get the siblings of.
* @returns `elem`'s siblings.
* @returns `elem`'s siblings, including `elem`.
*/

@@ -55,3 +55,4 @@ export declare function getSiblings(elem: AnyNode): AnyNode[];

* @param elem The element to get the next sibling of.
* @returns `elem`'s next sibling that is a tag.
* @returns `elem`'s next sibling that is a tag, or `null` if there is no next
* sibling.
*/

@@ -64,5 +65,6 @@ export declare function nextElementSibling(elem: AnyNode): Element | null;

* @param elem The element to get the previous sibling of.
* @returns `elem`'s previous sibling that is a tag.
* @returns `elem`'s previous sibling that is a tag, or `null` if there is no
* previous sibling.
*/
export declare function prevElementSibling(elem: AnyNode): Element | null;
//# sourceMappingURL=traversal.d.ts.map

@@ -17,3 +17,3 @@ import { isTag, hasChildren, } from "domhandler";

* @param elem Node to get the parent of.
* @returns `elem`'s parent node.
* @returns `elem`'s parent node, or `null` if `elem` is a root node.
*/

@@ -32,3 +32,3 @@ export function getParent(elem) {

* @param elem Element to get the siblings of.
* @returns `elem`'s siblings.
* @returns `elem`'s siblings, including `elem`.
*/

@@ -91,3 +91,4 @@ export function getSiblings(elem) {

* @param elem The element to get the next sibling of.
* @returns `elem`'s next sibling that is a tag.
* @returns `elem`'s next sibling that is a tag, or `null` if there is no next
* sibling.
*/

@@ -105,3 +106,4 @@ export function nextElementSibling(elem) {

* @param elem The element to get the previous sibling of.
* @returns `elem`'s previous sibling that is a tag.
* @returns `elem`'s previous sibling that is a tag, or `null` if there is no
* previous sibling.
*/

@@ -108,0 +110,0 @@ export function prevElementSibling(elem) {

import type { AnyNode } from "domhandler";
/**
* The type of a media item.
* The medium of a media item.
*
* @category Feeds
*/
export declare type FeedItemMediaMedium = "image" | "audio" | "video" | "document" | "executable";
export type FeedItemMediaMedium = "image" | "audio" | "video" | "document" | "executable";
/**

@@ -13,3 +13,3 @@ * The type of a media item.

*/
export declare type FeedItemMediaExpression = "sample" | "full" | "nonstop";
export type FeedItemMediaExpression = "sample" | "full" | "nonstop";
/**

@@ -16,0 +16,0 @@ * A media item of a feed entry.

@@ -87,3 +87,3 @@ "use strict";

addConditionally(entry, "description", "description", children);
var pubDate = fetch("pubDate", children);
var pubDate = fetch("pubDate", children) || fetch("dc:date", children);
if (pubDate)

@@ -90,0 +90,0 @@ entry.pubDate = new Date(pubDate);

import { AnyNode } from "domhandler";
/**
* Given an array of nodes, remove any member that is contained by another.
* Given an array of nodes, remove any member that is contained by another
* member.
*
* @category Helpers
* @param nodes Nodes to filter.
* @returns Remaining nodes that aren't subtrees of each other.
* @returns Remaining nodes that aren't contained by other nodes.
*/

@@ -22,4 +23,4 @@ export declare function removeSubsets(nodes: AnyNode[]): AnyNode[];

/**
* Compare the position of one node against another node in any other document.
* The return value is a bitmask with the values from {@link DocumentPosition}.
* Compare the position of one node against another node in any other document,
* returning a bitmask with the values from {@link DocumentPosition}.
*

@@ -50,5 +51,5 @@ * Document order:

/**
* Sort an array of nodes based on their relative position in the document and
* remove any duplicate nodes. If the array contains nodes that do not belong to
* the same document, sort order is unspecified.
* Sort an array of nodes based on their relative position in the document,
* removing any duplicate nodes. If the array contains nodes that do not belong
* to the same document, sort order is unspecified.
*

@@ -55,0 +56,0 @@ * @category Helpers

@@ -6,7 +6,8 @@ "use strict";

/**
* Given an array of nodes, remove any member that is contained by another.
* Given an array of nodes, remove any member that is contained by another
* member.
*
* @category Helpers
* @param nodes Nodes to filter.
* @returns Remaining nodes that aren't subtrees of each other.
* @returns Remaining nodes that aren't contained by other nodes.
*/

@@ -53,4 +54,4 @@ function removeSubsets(nodes) {

/**
* Compare the position of one node against another node in any other document.
* The return value is a bitmask with the values from {@link DocumentPosition}.
* Compare the position of one node against another node in any other document,
* returning a bitmask with the values from {@link DocumentPosition}.
*

@@ -120,5 +121,5 @@ * Document order:

/**
* Sort an array of nodes based on their relative position in the document and
* remove any duplicate nodes. If the array contains nodes that do not belong to
* the same document, sort order is unspecified.
* Sort an array of nodes based on their relative position in the document,
* removing any duplicate nodes. If the array contains nodes that do not belong
* to the same document, sort order is unspecified.
*

@@ -125,0 +126,0 @@ * @category Helpers

@@ -17,2 +17,4 @@ import { AnyNode, Element } from "domhandler";

/**
* Checks whether a node matches the description in `options`.
*
* @category Legacy Query Functions

@@ -25,2 +27,4 @@ * @param options An object describing nodes to look for.

/**
* Returns all nodes that match `options`.
*
* @category Legacy Query Functions

@@ -35,2 +39,4 @@ * @param options An object describing nodes to look for.

/**
* Returns the node with the supplied ID.
*
* @category Legacy Query Functions

@@ -44,2 +50,4 @@ * @param id The unique ID attribute value to look for.

/**
* Returns all nodes with the supplied `tagName`.
*
* @category Legacy Query Functions

@@ -54,2 +62,4 @@ * @param tagName Tag name to search for.

/**
* Returns all nodes with the supplied `type`.
*
* @category Legacy Query Functions

@@ -56,0 +66,0 @@ * @param type Element type to look for.

@@ -6,2 +6,5 @@ "use strict";

var querying_js_1 = require("./querying.js");
/**
* A map of functions to check nodes against.
*/
var Checks = {

@@ -31,2 +34,5 @@ tag_name: function (name) {

/**
* Returns a function to check whether a node has an attribute with a particular
* value.
*
* @param attrib Attribute to check.

@@ -44,2 +50,5 @@ * @param value Attribute value to look for.

/**
* Returns a function that returns `true` if either of the input functions
* returns `true` for a node.
*
* @param a First function to combine.

@@ -54,5 +63,8 @@ * @param b Second function to combine.

/**
* Returns a function that executes all checks in `options` and returns `true`
* if any of them match a node.
*
* @param options An object describing nodes to look for.
* @returns A function executing all checks in `options` and returning `true` if
* any of them match a node.
* @returns A function that executes all checks in `options` and returns `true`
* if any of them match a node.
*/

@@ -69,2 +81,4 @@ function compileTest(options) {

/**
* Checks whether a node matches the description in `options`.
*
* @category Legacy Query Functions

@@ -81,2 +95,4 @@ * @param options An object describing nodes to look for.

/**
* Returns all nodes that match `options`.
*
* @category Legacy Query Functions

@@ -96,2 +112,4 @@ * @param options An object describing nodes to look for.

/**
* Returns the node with the supplied ID.
*
* @category Legacy Query Functions

@@ -111,2 +129,4 @@ * @param id The unique ID attribute value to look for.

/**
* Returns all nodes with the supplied `tagName`.
*
* @category Legacy Query Functions

@@ -126,2 +146,4 @@ * @param tagName Tag name to search for.

/**
* Returns all nodes with the supplied `type`.
*
* @category Legacy Query Functions

@@ -128,0 +150,0 @@ * @param type Element type to look for.

@@ -1,2 +0,2 @@

import type { ChildNode, Element } from "domhandler";
import type { ChildNode, ParentNode } from "domhandler";
/**

@@ -21,6 +21,6 @@ * Remove an element from the dom

* @category Manipulation
* @param elem The element to append to.
* @param parent The element to append to.
* @param child The element to be added as a child.
*/
export declare function appendChild(elem: Element, child: ChildNode): void;
export declare function appendChild(parent: ParentNode, child: ChildNode): void;
/**

@@ -38,6 +38,6 @@ * Append an element after another.

* @category Manipulation
* @param elem The element to prepend before.
* @param parent The element to prepend before.
* @param child The element to be added as a child.
*/
export declare function prependChild(elem: Element, child: ChildNode): void;
export declare function prependChild(parent: ParentNode, child: ChildNode): void;
/**

@@ -44,0 +44,0 @@ * Prepend an element before another.

@@ -17,4 +17,10 @@ "use strict";

var childs = elem.parent.children;
childs.splice(childs.lastIndexOf(elem), 1);
var childsIndex = childs.lastIndexOf(elem);
if (childsIndex >= 0) {
childs.splice(childsIndex, 1);
}
}
elem.next = null;
elem.prev = null;
elem.parent = null;
}

@@ -50,11 +56,11 @@ exports.removeElement = removeElement;

* @category Manipulation
* @param elem The element to append to.
* @param parent The element to append to.
* @param child The element to be added as a child.
*/
function appendChild(elem, child) {
function appendChild(parent, child) {
removeElement(child);
child.next = null;
child.parent = elem;
if (elem.children.push(child) > 1) {
var sibling = elem.children[elem.children.length - 2];
child.parent = parent;
if (parent.children.push(child) > 1) {
var sibling = parent.children[parent.children.length - 2];
sibling.next = child;

@@ -99,11 +105,11 @@ child.prev = sibling;

* @category Manipulation
* @param elem The element to prepend before.
* @param parent The element to prepend before.
* @param child The element to be added as a child.
*/
function prependChild(elem, child) {
function prependChild(parent, child) {
removeElement(child);
child.parent = elem;
child.parent = parent;
child.prev = null;
if (elem.children.unshift(child) !== 1) {
var sibling = elem.children[1];
if (parent.children.unshift(child) !== 1) {
var sibling = parent.children[1];
sibling.prev = child;

@@ -110,0 +116,0 @@ child.next = sibling;

import { Element, AnyNode } from "domhandler";
/**
* Search a node and its children for nodes passing a test function.
* Search a node and its children for nodes passing a test function. If `node` is not an array, it will be wrapped in one.
*

@@ -14,3 +14,3 @@ * @category Querying

/**
* Search an array of node and its children for nodes passing a test function.
* Search an array of nodes and their children for nodes passing a test function.
*

@@ -26,3 +26,3 @@ * @category Querying

/**
* Finds the first element inside of an array that matches a test function.
* Finds the first element inside of an array that matches a test function. This is an alias for `Array.prototype.find`.
*

@@ -41,8 +41,10 @@ * @category Querying

* @param test Function to test nodes on.
* @param nodes Array of nodes to search.
* @param nodes Node or array of nodes to search.
* @param recurse Also consider child nodes.
* @returns The first child node that passes `test`.
* @returns The first node that passes `test`.
*/
export declare function findOne(test: (elem: Element) => boolean, nodes: AnyNode[], recurse?: boolean): Element | null;
/**
* Checks if a tree of nodes contains at least one node passing a test.
*
* @category Querying

@@ -55,3 +57,3 @@ * @param test Function to test nodes on.

/**
* Search and array of nodes and its children for elements passing a test function.
* Search an array of nodes and their children for elements passing a test function.
*

@@ -58,0 +60,0 @@ * Same as `find`, but limited to elements and with less options, leading to reduced complexity.

@@ -6,3 +6,3 @@ "use strict";

/**
* Search a node and its children for nodes passing a test function.
* Search a node and its children for nodes passing a test function. If `node` is not an array, it will be wrapped in one.
*

@@ -19,9 +19,7 @@ * @category Querying

if (limit === void 0) { limit = Infinity; }
if (!Array.isArray(node))
node = [node];
return find(test, node, recurse, limit);
return find(test, Array.isArray(node) ? node : [node], recurse, limit);
}
exports.filter = filter;
/**
* Search an array of node and its children for nodes passing a test function.
* Search an array of nodes and their children for nodes passing a test function.
*

@@ -37,22 +35,38 @@ * @category Querying

var result = [];
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
var elem = nodes_1[_i];
/** Stack of the arrays we are looking at. */
var nodeStack = [nodes];
/** Stack of the indices within the arrays. */
var indexStack = [0];
for (;;) {
// First, check if the current array has any more elements to look at.
if (indexStack[0] >= nodeStack[0].length) {
// If we have no more arrays to look at, we are done.
if (indexStack.length === 1) {
return result;
}
// Otherwise, remove the current array from the stack.
nodeStack.shift();
indexStack.shift();
// Loop back to the start to continue with the next array.
continue;
}
var elem = nodeStack[0][indexStack[0]++];
if (test(elem)) {
result.push(elem);
if (--limit <= 0)
break;
return result;
}
if (recurse && (0, domhandler_1.hasChildren)(elem) && elem.children.length > 0) {
var children = find(test, elem.children, recurse, limit);
result.push.apply(result, children);
limit -= children.length;
if (limit <= 0)
break;
/*
* Add the children to the stack. We are depth-first, so this is
* the next array we look at.
*/
indexStack.unshift(0);
nodeStack.unshift(elem.children);
}
}
return result;
}
exports.find = find;
/**
* Finds the first element inside of an array that matches a test function.
* Finds the first element inside of an array that matches a test function. This is an alias for `Array.prototype.find`.
*

@@ -74,5 +88,5 @@ * @category Querying

* @param test Function to test nodes on.
* @param nodes Array of nodes to search.
* @param nodes Node or array of nodes to search.
* @param recurse Also consider child nodes.
* @returns The first child node that passes `test`.
* @returns The first node that passes `test`.
*/

@@ -83,11 +97,11 @@ function findOne(test, nodes, recurse) {

for (var i = 0; i < nodes.length && !elem; i++) {
var checked = nodes[i];
if (!(0, domhandler_1.isTag)(checked)) {
var node = nodes[i];
if (!(0, domhandler_1.isTag)(node)) {
continue;
}
else if (test(checked)) {
elem = checked;
else if (test(node)) {
elem = node;
}
else if (recurse && checked.children.length > 0) {
elem = findOne(test, checked.children, true);
else if (recurse && node.children.length > 0) {
elem = findOne(test, node.children, true);
}

@@ -99,2 +113,4 @@ }

/**
* Checks if a tree of nodes contains at least one node passing a test.
*
* @category Querying

@@ -108,5 +124,3 @@ * @param test Function to test nodes on.

return (0, domhandler_1.isTag)(checked) &&
(test(checked) ||
(checked.children.length > 0 &&
existsOne(test, checked.children)));
(test(checked) || existsOne(test, checked.children));
});

@@ -116,3 +130,3 @@ }

/**
* Search and array of nodes and its children for elements passing a test function.
* Search an array of nodes and their children for elements passing a test function.
*

@@ -127,17 +141,28 @@ * Same as `find`, but limited to elements and with less options, leading to reduced complexity.

function findAll(test, nodes) {
var _a;
var result = [];
var stack = nodes.filter(domhandler_1.isTag);
var elem;
while ((elem = stack.shift())) {
var children = (_a = elem.children) === null || _a === void 0 ? void 0 : _a.filter(domhandler_1.isTag);
if (children && children.length > 0) {
stack.unshift.apply(stack, children);
var nodeStack = [nodes];
var indexStack = [0];
for (;;) {
if (indexStack[0] >= nodeStack[0].length) {
if (nodeStack.length === 1) {
return result;
}
// Otherwise, remove the current array from the stack.
nodeStack.shift();
indexStack.shift();
// Loop back to the start to continue with the next array.
continue;
}
var elem = nodeStack[0][indexStack[0]++];
if (!(0, domhandler_1.isTag)(elem))
continue;
if (test(elem))
result.push(elem);
if (elem.children.length > 0) {
indexStack.unshift(0);
nodeStack.unshift(elem.children);
}
}
return result;
}
exports.findAll = findAll;
//# sourceMappingURL=querying.js.map

@@ -20,3 +20,3 @@ import { AnyNode } from "domhandler";

/**
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags. Ignores comments.
*

@@ -30,3 +30,3 @@ * @category Stringify

/**
* Get a node's text content.
* Get a node's text content. Ignores comments.
*

@@ -40,3 +40,3 @@ * @category Stringify

/**
* Get a node's inner text.
* Get a node's inner text, ignoring `<script>` and `<style>` tags. Ignores comments.
*

@@ -43,0 +43,0 @@ * @category Stringify

@@ -35,3 +35,3 @@ "use strict";

/**
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags. Ignores comments.
*

@@ -56,3 +56,3 @@ * @category Stringify

/**
* Get a node's text content.
* Get a node's text content. Ignores comments.
*

@@ -76,3 +76,3 @@ * @category Stringify

/**
* Get a node's inner text.
* Get a node's inner text, ignoring `<script>` and `<style>` tags. Ignores comments.
*

@@ -79,0 +79,0 @@ * @category Stringify

@@ -20,3 +20,3 @@ import { AnyNode, ChildNode, Element, ParentNode } from "domhandler";

* @param elem Element to get the siblings of.
* @returns `elem`'s siblings.
* @returns `elem`'s siblings, including `elem`.
*/

@@ -55,3 +55,4 @@ export declare function getSiblings(elem: AnyNode): AnyNode[];

* @param elem The element to get the next sibling of.
* @returns `elem`'s next sibling that is a tag.
* @returns `elem`'s next sibling that is a tag, or `null` if there is no next
* sibling.
*/

@@ -64,5 +65,6 @@ export declare function nextElementSibling(elem: AnyNode): Element | null;

* @param elem The element to get the previous sibling of.
* @returns `elem`'s previous sibling that is a tag.
* @returns `elem`'s previous sibling that is a tag, or `null` if there is no
* previous sibling.
*/
export declare function prevElementSibling(elem: AnyNode): Element | null;
//# sourceMappingURL=traversal.d.ts.map

@@ -21,3 +21,3 @@ "use strict";

* @param elem Node to get the parent of.
* @returns `elem`'s parent node.
* @returns `elem`'s parent node, or `null` if `elem` is a root node.
*/

@@ -37,3 +37,3 @@ function getParent(elem) {

* @param elem Element to get the siblings of.
* @returns `elem`'s siblings.
* @returns `elem`'s siblings, including `elem`.
*/

@@ -101,3 +101,4 @@ function getSiblings(elem) {

* @param elem The element to get the next sibling of.
* @returns `elem`'s next sibling that is a tag.
* @returns `elem`'s next sibling that is a tag, or `null` if there is no next
* sibling.
*/

@@ -117,3 +118,4 @@ function nextElementSibling(elem) {

* @param elem The element to get the previous sibling of.
* @returns `elem`'s previous sibling that is a tag.
* @returns `elem`'s previous sibling that is a tag, or `null` if there is no
* previous sibling.
*/

@@ -120,0 +122,0 @@ function prevElementSibling(elem) {

{
"name": "domutils",
"version": "3.0.1",
"version": "3.1.0",
"description": "Utilities for working with htmlparser2's dom",

@@ -48,18 +48,18 @@ "author": "Felix Boehm <me@feedic.com>",

"domelementtype": "^2.3.0",
"domhandler": "^5.0.1"
"domhandler": "^5.0.3"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.25",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jsdoc": "^39.2.7",
"htmlparser2": "~7.2.0",
"jest": "^27.5.1",
"prettier": "^2.6.2",
"ts-jest": "^27.1.4",
"typedoc": "^0.22.15",
"typescript": "^4.6.3"
"@types/jest": "^29.5.1",
"@types/node": "^18.16.2",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"eslint": "^8.39.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-jsdoc": "^43.1.1",
"htmlparser2": "~8.0.2",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"typedoc": "^0.24.6",
"typescript": "^5.0.4"
},

@@ -71,3 +71,6 @@ "jest": {

"moduleNameMapper": {
"^(.*)\\.js$": "$1"
"^(.*)\\.js$": [
"$1.js",
"$1"
]
}

@@ -74,0 +77,0 @@ },

@@ -1,2 +0,2 @@

# domutils [![Build Status](https://travis-ci.com/fb55/domutils.svg?branch=master)](https://travis-ci.com/fb55/domutils)
# domutils [![Node.js CI](https://github.com/fb55/domutils/actions/workflows/nodejs-test.yml/badge.svg)](https://github.com/fb55/domutils/actions/workflows/nodejs-test.yml)

@@ -3,0 +3,0 @@ Utilities for working with [htmlparser2](https://github.com/fb55/htmlparser2)'s DOM.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc