@parse5/tools
Advanced tools
Comparing version 0.0.2-0 to 0.0.2-1
@@ -0,1 +1,2 @@ | ||
import { html, Token } from 'parse5'; | ||
import type { Element, ParentNode, CommentNode, ChildNode, TextNode, Template, DocumentFragment, Document, DocumentType, Node } from 'parse5/dist/tree-adapters/default.js'; | ||
@@ -103,2 +104,10 @@ /** | ||
/** | ||
* Creates an element node | ||
* @param {string} tagName Name of the tag to create | ||
* @param {NS} namespaceURI Namespace of the element | ||
* @param {Record<string, string>|Attribute[]} attrs Attributes for the element | ||
* @return {Element} | ||
*/ | ||
export declare function createElement(tagName: string, namespaceURI?: html.NS, attrs?: Record<string, string> | Token.Attribute[]): Element; | ||
/** | ||
* Sets the text content of the given node | ||
@@ -105,0 +114,0 @@ * @param {Node} node Node to set contents of |
@@ -1,2 +0,2 @@ | ||
import { defaultTreeAdapter } from 'parse5'; | ||
import { defaultTreeAdapter, html } from 'parse5'; | ||
/** | ||
@@ -172,2 +172,28 @@ * Determines if a given node is a document or not | ||
/** | ||
* Creates an element node | ||
* @param {string} tagName Name of the tag to create | ||
* @param {NS} namespaceURI Namespace of the element | ||
* @param {Record<string, string>|Attribute[]} attrs Attributes for the element | ||
* @return {Element} | ||
*/ | ||
export function createElement(tagName, namespaceURI = html.NS.HTML, attrs = []) { | ||
const normalisedAttrs = []; | ||
if (Array.isArray(attrs)) { | ||
for (const attr of attrs) { | ||
normalisedAttrs.push(attr); | ||
} | ||
} | ||
else { | ||
for (const name in attrs) { | ||
if (Object.prototype.hasOwnProperty.call(attrs, name)) { | ||
normalisedAttrs.push({ | ||
name, | ||
value: attrs[name] | ||
}); | ||
} | ||
} | ||
} | ||
return defaultTreeAdapter.createElement(tagName, namespaceURI, normalisedAttrs); | ||
} | ||
/** | ||
* Sets the text content of the given node | ||
@@ -174,0 +200,0 @@ * @param {Node} node Node to set contents of |
{ | ||
"name": "@parse5/tools", | ||
"version": "0.0.2-0", | ||
"version": "0.0.2-1", | ||
"description": "A small set of utilities for dealing with parse5 syntax trees", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -18,2 +18,13 @@ # @parse5/tools | ||
### Node creation | ||
The default parse5 adapter is usually enough to create the nodes you need. | ||
To make some use cases a little easier, the following do exist, though: | ||
* `createElement(tagName[, namespaceURI[, attrs]])` | ||
* The attributes can be an array (e.g. `[{name: 'foo', value: 'bar'}]`) or | ||
an object (e.g. `{foo: 'bar'}`) | ||
* `createTextNode(value)` | ||
### Node type guards | ||
@@ -20,0 +31,0 @@ |
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
22079
504
185