@web/parse5-utils
Advanced tools
Comparing version 1.0.0 to 1.1.0
# @web/parse5-utils | ||
## 1.1.0 | ||
### Minor Changes | ||
- 3121966: add textcontent helpers | ||
## 1.0.0 | ||
### Major Changes | ||
- cd5244e: First setup |
declare const _exports: { | ||
isHtmlFragment: typeof isHtmlFragment; | ||
createElement: typeof createElement; | ||
createScript: typeof createScript; | ||
hasAttribute: typeof hasAttribute; | ||
@@ -10,2 +11,7 @@ getAttribute: typeof getAttribute; | ||
removeAttribute: typeof removeAttribute; | ||
getTextContent: typeof getTextContent; | ||
setTextContent: typeof setTextContent; | ||
remove: typeof remove; | ||
findNode: typeof findNode; | ||
findNodes: typeof findNodes; | ||
findElement: typeof findElement; | ||
@@ -50,3 +56,2 @@ findElements: typeof findElements; | ||
export type Element = object | import("parse5").DefaultTreeElement; | ||
export type DefaultTreeElement = import("parse5").DefaultTreeElement; | ||
export type Attribute = import("parse5").Attribute; | ||
@@ -56,2 +61,7 @@ export type Node = object | import("parse5").DefaultTreeNode; | ||
export type ChildNode = object | import("parse5").DefaultTreeChildNode; | ||
export type DefaultTreeElement = import("parse5").DefaultTreeElement; | ||
export type DefaultTreeNode = import("parse5").DefaultTreeNode; | ||
export type DefaultTreeChildNode = import("parse5").DefaultTreeChildNode; | ||
export type DefaultTreeCommentNode = import("parse5").DefaultTreeCommentNode; | ||
export type DefaultTreeTextNode = import("parse5").DefaultTreeTextNode; | ||
/** | ||
@@ -70,2 +80,8 @@ * @param {string} html | ||
/** | ||
* Creates a script element. | ||
* @param {Record<string,string>} [attrs] | ||
* @param {string} [code] | ||
*/ | ||
declare function createScript(attrs?: Record<string, string> | undefined, code?: string | undefined): import("parse5").Element; | ||
/** | ||
* @param {Node} element | ||
@@ -102,4 +118,33 @@ * @param {string} name | ||
/** | ||
* @param {Node} node | ||
* @returns {string} | ||
*/ | ||
declare function getTextContent(node: Node): string; | ||
/** | ||
* @param {Element} node | ||
* @param {string} value | ||
*/ | ||
declare function setTextContent(node: Element, value: string): void; | ||
/** | ||
* Removes element from the AST. | ||
* @param {ChildNode} node | ||
*/ | ||
declare function remove(node: ChildNode): void; | ||
/** | ||
* Looks for a child node which passes the given test | ||
* @param {Node[] | Node} nodes | ||
* @param {(node: DefaultTreeNode) => boolean} test | ||
* @returns {DefaultTreeNode | null} | ||
*/ | ||
declare function findNode(nodes: Node[] | Node, test: (node: DefaultTreeNode) => boolean): DefaultTreeNode | null; | ||
/** | ||
* Looks for all child nodes which passes the given test | ||
* @param {Node | Node[]} nodes | ||
* @param {(node: DefaultTreeNode) => boolean} test | ||
* @returns {DefaultTreeNode[]} | ||
*/ | ||
declare function findNodes(nodes: Node | Node[], test: (node: DefaultTreeNode) => boolean): DefaultTreeNode[]; | ||
/** | ||
* Looks for a child element which passes the given test | ||
* @param {Node[] | Node} nodes | ||
* @param {(node: DefaultTreeElement) => boolean} test | ||
@@ -110,3 +155,3 @@ * @returns {DefaultTreeElement | null} | ||
/** | ||
* Looks for all child element which passes the given test | ||
* Looks for all child elements which passes the given test | ||
* @param {Node | Node[]} nodes | ||
@@ -113,0 +158,0 @@ * @param {(node: Node) => boolean} test |
{ | ||
"name": "@web/parse5-utils", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"publishConfig": { | ||
@@ -41,2 +41,5 @@ "access": "public" | ||
}, | ||
"devDependencies": { | ||
"@types/html-minifier-terser": "^5.1.1" | ||
}, | ||
"exports": { | ||
@@ -43,0 +46,0 @@ ".": { |
@@ -64,2 +64,3 @@ # parse5-utils | ||
- createElement | ||
- createScript | ||
- createCommentNode | ||
@@ -102,2 +103,5 @@ - appendChild | ||
- removeAttribute | ||
- remove | ||
- findNode | ||
- findNodes | ||
- findElement | ||
@@ -104,0 +108,0 @@ - findElements |
127
src/index.js
/** @typedef {import('parse5').TreeAdapter} TreeAdapter */ | ||
/** @typedef {import('parse5').Element} Element */ | ||
/** @typedef {import('parse5').DefaultTreeElement} DefaultTreeElement */ | ||
/** @typedef {import('parse5').Attribute} Attribute */ | ||
@@ -8,6 +7,11 @@ /** @typedef {import('parse5').Node} Node */ | ||
/** @typedef {import('parse5').ChildNode} ChildNode */ | ||
/** @typedef {import('parse5').DefaultTreeElement} DefaultTreeElement */ | ||
/** @typedef {import('parse5').DefaultTreeNode} DefaultTreeNode */ | ||
/** @typedef {import('parse5').DefaultTreeChildNode} DefaultTreeChildNode */ | ||
/** @typedef {import('parse5').DefaultTreeCommentNode} DefaultTreeCommentNode */ | ||
/** @typedef {import('parse5').DefaultTreeTextNode} DefaultTreeTextNode */ | ||
const parse5 = require('parse5'); | ||
// the tree adapter is not in the parse5 types | ||
//@ts-expect-error | ||
//@ts-ignore | ||
const adapter = /** @type {TreeAdapter} */ (require('parse5/lib/tree-adapters/default')); | ||
@@ -31,2 +35,15 @@ | ||
/** | ||
* Creates a script element. | ||
* @param {Record<string,string>} [attrs] | ||
* @param {string} [code] | ||
*/ | ||
function createScript(attrs = {}, code = undefined) { | ||
const element = createElement('script', attrs); | ||
if (code) { | ||
setTextContent(element, code); | ||
} | ||
return element; | ||
} | ||
/** | ||
* @param {string} html | ||
@@ -115,16 +132,66 @@ */ | ||
/** | ||
* @param {Node} node | ||
* @returns {string} | ||
*/ | ||
function getTextContent(node) { | ||
if (adapter.isCommentNode(node)) { | ||
return /** @type {DefaultTreeCommentNode} */ (node).data || ''; | ||
} | ||
if (adapter.isTextNode(node)) { | ||
return /** @type {DefaultTreeTextNode} */ (node).value || ''; | ||
} | ||
const subtree = findNodes(node, n => adapter.isTextNode(n)); | ||
return subtree.map(getTextContent).join(''); | ||
} | ||
/** | ||
* @param {Element} node | ||
* @param {string} value | ||
*/ | ||
function setTextContent(node, value) { | ||
if (adapter.isCommentNode(node)) { | ||
/** @type {DefaultTreeCommentNode} */ (node).data = value; | ||
} else if (adapter.isTextNode(node)) { | ||
/** @type {DefaultTreeTextNode} */ (node).value = value; | ||
} else { | ||
const textNode = { | ||
nodeName: '#text', | ||
value: value, | ||
parentNode: node, | ||
attrs: [], | ||
__location: undefined, | ||
}; | ||
/** @type {DefaultTreeElement} */ (node).childNodes = [textNode]; | ||
} | ||
} | ||
/** | ||
* Removes element from the AST. | ||
* @param {ChildNode} node | ||
*/ | ||
function remove(node) { | ||
const n = /** @type {DefaultTreeChildNode} */ (node); | ||
const parent = n.parentNode; | ||
if (parent && parent.childNodes) { | ||
const idx = parent.childNodes.indexOf(n); | ||
parent.childNodes.splice(idx, 1); | ||
} | ||
/** @type {any} */ (n).parentNode = undefined; | ||
} | ||
/** | ||
* Looks for a child node which passes the given test | ||
* @param {Node[] | Node} nodes | ||
* @param {(node: DefaultTreeElement) => boolean} test | ||
* @returns {DefaultTreeElement | null} | ||
* @param {(node: DefaultTreeNode) => boolean} test | ||
* @returns {DefaultTreeNode | null} | ||
*/ | ||
function findElement(nodes, test) { | ||
function findNode(nodes, test) { | ||
const n = Array.isArray(nodes) ? nodes.slice() : [nodes]; | ||
while (n.length > 0) { | ||
const node = /** @type {DefaultTreeElement} */ (n.shift()); | ||
const node = /** @type {DefaultTreeNode} */ (n.shift()); | ||
if (!node) { | ||
continue; | ||
} | ||
if (adapter.isElementNode(node) && test(node)) { | ||
if (test(node)) { | ||
return node; | ||
@@ -141,18 +208,18 @@ } | ||
/** | ||
* Looks for all child element which passes the given test | ||
* Looks for all child nodes which passes the given test | ||
* @param {Node | Node[]} nodes | ||
* @param {(node: Node) => boolean} test | ||
* @returns {DefaultTreeElement[]} | ||
* @param {(node: DefaultTreeNode) => boolean} test | ||
* @returns {DefaultTreeNode[]} | ||
*/ | ||
function findElements(nodes, test) { | ||
function findNodes(nodes, test) { | ||
const n = Array.isArray(nodes) ? nodes.slice() : [nodes]; | ||
/** @type {DefaultTreeElement[]} */ | ||
/** @type {DefaultTreeNode[]} */ | ||
const found = []; | ||
while (n.length) { | ||
const node = /** @type {DefaultTreeElement} */ (n.shift()); | ||
const node = /** @type {DefaultTreeNode} */ (n.shift()); | ||
if (!node) { | ||
continue; | ||
} | ||
if (adapter.isElementNode(node) && test(node)) { | ||
if (test(node)) { | ||
found.push(node); | ||
@@ -169,2 +236,28 @@ } | ||
/** | ||
* Looks for a child element which passes the given test | ||
* @param {Node[] | Node} nodes | ||
* @param {(node: DefaultTreeElement) => boolean} test | ||
* @returns {DefaultTreeElement | null} | ||
*/ | ||
function findElement(nodes, test) { | ||
return /** @type {DefaultTreeElement | null} */ (findNode( | ||
nodes, | ||
n => adapter.isElementNode(n) && test(/** @type {DefaultTreeElement} */ (n)), | ||
)); | ||
} | ||
/** | ||
* Looks for all child elements which passes the given test | ||
* @param {Node | Node[]} nodes | ||
* @param {(node: Node) => boolean} test | ||
* @returns {DefaultTreeElement[]} | ||
*/ | ||
function findElements(nodes, test) { | ||
return /** @type {DefaultTreeElement[] } */ (findNodes( | ||
nodes, | ||
n => adapter.isElementNode(n) && test(/** @type {DefaultTreeElement} */ (n)), | ||
)); | ||
} | ||
/** | ||
* @param {ParentNode} parent | ||
@@ -247,2 +340,3 @@ * @param {ChildNode} node | ||
createElement, | ||
createScript, | ||
hasAttribute, | ||
@@ -254,2 +348,7 @@ getAttribute, | ||
removeAttribute, | ||
getTextContent, | ||
setTextContent, | ||
remove, | ||
findNode, | ||
findNodes, | ||
findElement, | ||
@@ -256,0 +355,0 @@ findElements, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25377
596
109
1