@web/parse5-utils
Advanced tools
Comparing version 0.0.0-canary-20230420104136 to 0.0.0-canary-20231122093600
@@ -1,9 +0,1 @@ | ||
export type TreeAdapter = import('parse5').TreeAdapter; | ||
export type Element = import('parse5').Element; | ||
export type Attribute = import('parse5').Attribute; | ||
export type Node = import('parse5').Node; | ||
export type ParentNode = import('parse5').ParentNode; | ||
export type ChildNode = import('parse5').ChildNode; | ||
export type CommentNode = import('parse5').CommentNode; | ||
export type TextNode = import('parse5').TextNode; | ||
/** | ||
@@ -31,5 +23,4 @@ * Creates an element node. | ||
* @param {Element} element | ||
* @param {string} name | ||
*/ | ||
export function hasAttribute(element: Element, name: string): boolean; | ||
export function getAttributes(element: Element): Record<string, string>; | ||
/** | ||
@@ -42,4 +33,5 @@ * @param {Element} element | ||
* @param {Element} element | ||
* @param {string} name | ||
*/ | ||
export function getAttributes(element: Element): Record<string, string>; | ||
export function hasAttribute(element: Element, name: string): boolean; | ||
/** | ||
@@ -63,10 +55,10 @@ * @param {Element} element | ||
* @param {Node} node | ||
* @param {string} value | ||
* @returns {string} | ||
*/ | ||
export function setTextContent(node: Node, value: string): void; | ||
export function getTextContent(node: Node): string; | ||
/** | ||
* @param {Node} node | ||
* @returns {string} | ||
* @param {string} value | ||
*/ | ||
export function getTextContent(node: Node): string; | ||
export function setTextContent(node: Node, value: string): void; | ||
/** | ||
@@ -125,3 +117,42 @@ * Removes element from the AST. | ||
export function appendToDocument(document: string, appendedHtml: string): string; | ||
export { createDocument, createDocumentFragment, createCommentNode, appendChild, insertBefore, setTemplateContent, getTemplateContent, setDocumentType, setDocumentMode, getDocumentMode, detachNode, insertText, insertTextBefore, adoptAttributes, getFirstChild, getChildNodes, getParentNode, getAttrList, getTagName, getNamespaceURI, getTextNodeContent, getCommentNodeContent, getDocumentTypeNodeName, getDocumentTypeNodePublicId, getDocumentTypeNodeSystemId, isTextNode, isCommentNode, isDocumentTypeNode, isElementNode, setNodeSourceCodeLocation, getNodeSourceCodeLocation }; | ||
export const createDocument: () => parse5.Document; | ||
export const createDocumentFragment: () => parse5.DocumentFragment; | ||
export const createCommentNode: (data: string) => parse5.CommentNode; | ||
export const appendChild: (parentNode: parse5.ParentNode, newNode: parse5.Node) => void; | ||
export const insertBefore: (parentNode: parse5.ParentNode, newNode: parse5.Node, referenceNode: parse5.Node) => void; | ||
export const setTemplateContent: (templateElement: parse5.Element, contentElement: parse5.DocumentFragment) => void; | ||
export const getTemplateContent: (templateElement: parse5.Element) => parse5.DocumentFragment; | ||
export const setDocumentType: (document: parse5.Document, name: string, publicId: string, systemId: string) => void; | ||
export const setDocumentMode: (document: parse5.Document, mode: parse5.DocumentMode) => void; | ||
export const getDocumentMode: (document: parse5.Document) => parse5.DocumentMode; | ||
export const detachNode: (node: parse5.Node) => void; | ||
export const insertText: (parentNode: parse5.ParentNode, text: string) => void; | ||
export const insertTextBefore: (parentNode: parse5.ParentNode, text: string, referenceNode: parse5.Node) => void; | ||
export const adoptAttributes: (recipient: parse5.Element, attrs: parse5.Attribute[]) => void; | ||
export const getFirstChild: (node: parse5.ParentNode) => parse5.ChildNode | undefined; | ||
export const getChildNodes: (node: parse5.ParentNode) => parse5.ChildNode[]; | ||
export const getParentNode: (node: parse5.ChildNode) => parse5.ParentNode; | ||
export const getAttrList: (element: parse5.Element) => parse5.Attribute[]; | ||
export const getTagName: (element: parse5.Element) => string; | ||
export const getNamespaceURI: (element: parse5.Element) => string; | ||
export const getTextNodeContent: (textNode: parse5.TextNode) => string; | ||
export const getCommentNodeContent: (commentNode: parse5.CommentNode) => string; | ||
export const getDocumentTypeNodeName: (doctypeNode: parse5.DocumentType) => string; | ||
export const getDocumentTypeNodePublicId: (doctypeNode: parse5.DocumentType) => string; | ||
export const getDocumentTypeNodeSystemId: (doctypeNode: parse5.DocumentType) => string; | ||
export const isTextNode: (node: parse5.Node) => node is parse5.TextNode; | ||
export const isCommentNode: (node: parse5.Node) => node is parse5.CommentNode; | ||
export const isDocumentTypeNode: (node: parse5.Node) => node is parse5.DocumentType; | ||
export const isElementNode: (node: parse5.Node) => node is parse5.Element; | ||
export const setNodeSourceCodeLocation: (node: parse5.Node, location: parse5.Location | parse5.ElementLocation | null) => void; | ||
export const getNodeSourceCodeLocation: (node: parse5.Node) => parse5.Location | parse5.ElementLocation | null; | ||
export type TreeAdapter = import('parse5').TreeAdapter; | ||
export type Element = import('parse5').Element; | ||
export type Attribute = import('parse5').Attribute; | ||
export type Node = import('parse5').Node; | ||
export type ParentNode = import('parse5').ParentNode; | ||
export type ChildNode = import('parse5').ChildNode; | ||
export type CommentNode = import('parse5').CommentNode; | ||
export type TextNode = import('parse5').TextNode; | ||
import parse5 from 'parse5'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,1 @@ | ||
export * from './dist/index'; | ||
export * from './dist/index.js'; |
{ | ||
"name": "@web/parse5-utils", | ||
"version": "0.0.0-canary-20230420104136", | ||
"version": "0.0.0-canary-20231122093600", | ||
"publishConfig": { | ||
@@ -17,4 +17,6 @@ "access": "public" | ||
"main": "src/index.js", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./index.d.ts", | ||
"import": "./index.mjs", | ||
@@ -25,3 +27,3 @@ "require": "./src/index.js" | ||
"engines": { | ||
"node": ">=16.0.0" | ||
"node": ">=18.0.0" | ||
}, | ||
@@ -28,0 +30,0 @@ "scripts": { |
137
src/index.js
@@ -10,4 +10,4 @@ /** @typedef {import('parse5').TreeAdapter} TreeAdapter */ | ||
const parse5 = require('parse5'); | ||
const adapter = require('parse5/lib/tree-adapters/default'); | ||
import parse5 from 'parse5'; | ||
import adapter from 'parse5/lib/tree-adapters/default.js'; | ||
@@ -25,3 +25,3 @@ const DEFAULT_NAMESPACE = 'http://www.w3.org/1999/xhtml'; | ||
*/ | ||
function createElement(tagName, attrs = {}, namespaceURI = DEFAULT_NAMESPACE) { | ||
export function createElement(tagName, attrs = {}, namespaceURI = DEFAULT_NAMESPACE) { | ||
const attrsArray = Object.entries(attrs).map(([name, value]) => ({ name, value })); | ||
@@ -37,3 +37,3 @@ return adapter.createElement(tagName, namespaceURI, attrsArray); | ||
*/ | ||
function createScript(attrs = {}, code = undefined) { | ||
export function createScript(attrs = {}, code = undefined) { | ||
const element = createElement('script', attrs); | ||
@@ -49,3 +49,3 @@ if (code) { | ||
*/ | ||
function isHtmlFragment(html) { | ||
export function isHtmlFragment(html) { | ||
let htmlWithoutComments = html.replace(/<!--.*?-->/gs, ''); | ||
@@ -58,3 +58,3 @@ return !REGEXP_IS_HTML_DOCUMENT.test(htmlWithoutComments); | ||
*/ | ||
function getAttributes(element) { | ||
export function getAttributes(element) { | ||
const attrsArray = adapter.getAttrList(element); | ||
@@ -73,3 +73,3 @@ /** @type {Record<string,string>} */ | ||
*/ | ||
function getAttribute(element, name) { | ||
export function getAttribute(element, name) { | ||
const attrList = adapter.getAttrList(element); | ||
@@ -90,3 +90,3 @@ if (!attrList) { | ||
*/ | ||
function hasAttribute(element, name) { | ||
export function hasAttribute(element, name) { | ||
return getAttribute(element, name) != null; | ||
@@ -100,3 +100,3 @@ } | ||
*/ | ||
function setAttribute(element, name, value) { | ||
export function setAttribute(element, name, value) { | ||
const attrs = adapter.getAttrList(element); | ||
@@ -116,3 +116,3 @@ const existing = attrs.find(a => a.name === name); | ||
*/ | ||
function setAttributes(element, attributes) { | ||
export function setAttributes(element, attributes) { | ||
for (const [name, value] of Object.entries(attributes)) { | ||
@@ -129,3 +129,3 @@ if (value !== undefined) { | ||
*/ | ||
function removeAttribute(element, name) { | ||
export function removeAttribute(element, name) { | ||
const attrs = adapter.getAttrList(element); | ||
@@ -139,3 +139,3 @@ element.attrs = attrs.filter(attr => attr.name !== name); | ||
*/ | ||
function getTextContent(node) { | ||
export function getTextContent(node) { | ||
if (adapter.isCommentNode(node)) { | ||
@@ -155,3 +155,3 @@ return node.data || ''; | ||
*/ | ||
function setTextContent(node, value) { | ||
export function setTextContent(node, value) { | ||
if (adapter.isCommentNode(node)) { | ||
@@ -177,3 +177,3 @@ node.data = value; | ||
*/ | ||
function remove(node) { | ||
export function remove(node) { | ||
const parent = node.parentNode; | ||
@@ -193,3 +193,3 @@ if (parent && parent.childNodes) { | ||
*/ | ||
function findNode(nodes, test) { | ||
export function findNode(nodes, test) { | ||
const n = Array.isArray(nodes) ? nodes.slice() : [nodes]; | ||
@@ -219,3 +219,3 @@ | ||
*/ | ||
function findNodes(nodes, test) { | ||
export function findNodes(nodes, test) { | ||
const n = Array.isArray(nodes) ? nodes.slice() : [nodes]; | ||
@@ -233,3 +233,15 @@ /** @type {Node[]} */ | ||
} | ||
const children = adapter.getChildNodes(/** @type {ParentNode} */ (node)); | ||
/** @type {Node[]} */ | ||
let children = []; | ||
if (adapter.isElementNode(node) && adapter.getTagName(node) === 'template') { | ||
const content = adapter.getTemplateContent(node); | ||
if (content) { | ||
children = adapter.getChildNodes(content); | ||
} | ||
} else { | ||
children = adapter.getChildNodes(/** @type {ParentNode} */ (node)); | ||
} | ||
if (Array.isArray(children)) { | ||
@@ -248,3 +260,3 @@ n.unshift(...children); | ||
*/ | ||
function findElement(nodes, test) { | ||
export function findElement(nodes, test) { | ||
return /** @type {Element | null} */ (findNode(nodes, n => adapter.isElementNode(n) && test(n))); | ||
@@ -259,3 +271,3 @@ } | ||
*/ | ||
function findElements(nodes, test) { | ||
export function findElements(nodes, test) { | ||
return /** @type {Element[]} */ (findNodes(nodes, n => adapter.isElementNode(n) && test(n))); | ||
@@ -268,3 +280,3 @@ } | ||
*/ | ||
function prepend(parent, node) { | ||
export function prepend(parent, node) { | ||
parent.childNodes.unshift(node); | ||
@@ -281,3 +293,3 @@ node.parentNode = parent; | ||
*/ | ||
function prependToDocument(document, appendedHtml) { | ||
export function prependToDocument(document, appendedHtml) { | ||
const documentAst = parse5.parse(document, { sourceCodeLocationInfo: true }); | ||
@@ -314,3 +326,3 @@ let appendNode = findElement(documentAst, node => adapter.getTagName(node) === 'head'); | ||
*/ | ||
function appendToDocument(document, appendedHtml) { | ||
export function appendToDocument(document, appendedHtml) { | ||
const documentAst = parse5.parse(document, { sourceCodeLocationInfo: true }); | ||
@@ -341,51 +353,32 @@ let appendNode = findElement(documentAst, node => adapter.getTagName(node) === 'body'); | ||
module.exports.createDocument = adapter.createDocument; | ||
module.exports.createDocumentFragment = adapter.createDocumentFragment; | ||
module.exports.createElement = createElement; | ||
module.exports.createScript = createScript; | ||
module.exports.createCommentNode = adapter.createCommentNode; | ||
module.exports.appendChild = adapter.appendChild; | ||
module.exports.insertBefore = adapter.insertBefore; | ||
module.exports.setTemplateContent = adapter.setTemplateContent; | ||
module.exports.getTemplateContent = adapter.getTemplateContent; | ||
module.exports.setDocumentType = adapter.setDocumentType; | ||
module.exports.setDocumentMode = adapter.setDocumentMode; | ||
module.exports.getDocumentMode = adapter.getDocumentMode; | ||
module.exports.detachNode = adapter.detachNode; | ||
module.exports.insertText = adapter.insertText; | ||
module.exports.insertTextBefore = adapter.insertTextBefore; | ||
module.exports.adoptAttributes = adapter.adoptAttributes; | ||
module.exports.getFirstChild = adapter.getFirstChild; | ||
module.exports.getChildNodes = adapter.getChildNodes; | ||
module.exports.getParentNode = adapter.getParentNode; | ||
module.exports.getAttrList = adapter.getAttrList; | ||
module.exports.getTagName = adapter.getTagName; | ||
module.exports.getNamespaceURI = adapter.getNamespaceURI; | ||
module.exports.getTextNodeContent = adapter.getTextNodeContent; | ||
module.exports.getCommentNodeContent = adapter.getCommentNodeContent; | ||
module.exports.getDocumentTypeNodeName = adapter.getDocumentTypeNodeName; | ||
module.exports.getDocumentTypeNodePublicId = adapter.getDocumentTypeNodePublicId; | ||
module.exports.getDocumentTypeNodeSystemId = adapter.getDocumentTypeNodeSystemId; | ||
module.exports.isTextNode = adapter.isTextNode; | ||
module.exports.isCommentNode = adapter.isCommentNode; | ||
module.exports.isDocumentTypeNode = adapter.isDocumentTypeNode; | ||
module.exports.isElementNode = adapter.isElementNode; | ||
module.exports.setNodeSourceCodeLocation = adapter.setNodeSourceCodeLocation; | ||
module.exports.getNodeSourceCodeLocation = adapter.getNodeSourceCodeLocation; | ||
module.exports.isHtmlFragment = isHtmlFragment; | ||
module.exports.hasAttribute = hasAttribute; | ||
module.exports.getAttribute = getAttribute; | ||
module.exports.getAttributes = getAttributes; | ||
module.exports.setAttribute = setAttribute; | ||
module.exports.setAttributes = setAttributes; | ||
module.exports.removeAttribute = removeAttribute; | ||
module.exports.setTextContent = setTextContent; | ||
module.exports.getTextContent = getTextContent; | ||
module.exports.remove = remove; | ||
module.exports.findNode = findNode; | ||
module.exports.findNodes = findNodes; | ||
module.exports.findElement = findElement; | ||
module.exports.findElements = findElements; | ||
module.exports.prepend = prepend; | ||
module.exports.prependToDocument = prependToDocument; | ||
module.exports.appendToDocument = appendToDocument; | ||
export const createDocument = adapter.createDocument; | ||
export const createDocumentFragment = adapter.createDocumentFragment; | ||
export const createCommentNode = adapter.createCommentNode; | ||
export const appendChild = adapter.appendChild; | ||
export const insertBefore = adapter.insertBefore; | ||
export const setTemplateContent = adapter.setTemplateContent; | ||
export const getTemplateContent = adapter.getTemplateContent; | ||
export const setDocumentType = adapter.setDocumentType; | ||
export const setDocumentMode = adapter.setDocumentMode; | ||
export const getDocumentMode = adapter.getDocumentMode; | ||
export const detachNode = adapter.detachNode; | ||
export const insertText = adapter.insertText; | ||
export const insertTextBefore = adapter.insertTextBefore; | ||
export const adoptAttributes = adapter.adoptAttributes; | ||
export const getFirstChild = adapter.getFirstChild; | ||
export const getChildNodes = adapter.getChildNodes; | ||
export const getParentNode = adapter.getParentNode; | ||
export const getAttrList = adapter.getAttrList; | ||
export const getTagName = adapter.getTagName; | ||
export const getNamespaceURI = adapter.getNamespaceURI; | ||
export const getTextNodeContent = adapter.getTextNodeContent; | ||
export const getCommentNodeContent = adapter.getCommentNodeContent; | ||
export const getDocumentTypeNodeName = adapter.getDocumentTypeNodeName; | ||
export const getDocumentTypeNodePublicId = adapter.getDocumentTypeNodePublicId; | ||
export const getDocumentTypeNodeSystemId = adapter.getDocumentTypeNodeSystemId; | ||
export const isTextNode = adapter.isTextNode; | ||
export const isCommentNode = adapter.isCommentNode; | ||
export const isDocumentTypeNode = adapter.isDocumentTypeNode; | ||
export const isElementNode = adapter.isElementNode; | ||
export const setNodeSourceCodeLocation = adapter.setNodeSourceCodeLocation; | ||
export const getNodeSourceCodeLocation = adapter.getNodeSourceCodeLocation; |
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
47279
9
874
Yes