hast-util-to-dom
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
export function toDom(node: HastNode, options?: Options): Node | ||
export function toDom(node: HastNode, options?: Options | undefined): Node | ||
export type HastParent = import('hast').Parent | ||
@@ -18,2 +18,6 @@ export type HastRoot = import('hast').Root | ||
export type HastNode = HastChild | HastRoot | ||
/** | ||
* Function called when a hast node is transformed into a DOM node | ||
*/ | ||
export type AfterTransform = (hastNode: HastNode, domNode: Node) => void | ||
export type Options = { | ||
@@ -23,17 +27,22 @@ /** | ||
*/ | ||
fragment?: boolean | ||
fragment?: boolean | undefined | ||
/** | ||
* Document interface to use (default: `globalThis.document`) | ||
*/ | ||
document?: Document | ||
document?: Document | undefined | ||
/** | ||
* `namespace` to use to create elements | ||
*/ | ||
namespace?: string | ||
namespace?: string | undefined | ||
/** | ||
* Callback invoked after each node transformation | ||
*/ | ||
afterTransform?: AfterTransform | undefined | ||
} | ||
export type Context = { | ||
doc: Document | ||
fragment?: boolean | ||
namespace?: string | ||
impliedNamespace?: string | ||
fragment?: boolean | undefined | ||
namespace?: string | undefined | ||
impliedNamespace?: string | undefined | ||
afterTransform?: AfterTransform | undefined | ||
} |
@@ -11,6 +11,19 @@ /** | ||
* | ||
* @callback AfterTransform | ||
* Function called when a hast node is transformed into a DOM node | ||
* @param {HastNode} hastNode | ||
* The hast node that was handled | ||
* @param {Node} domNode | ||
* The corresponding DOM node | ||
* @returns {void} | ||
* | ||
* @typedef Options | ||
* @property {boolean} [fragment=false] Whether a DOM fragment should be returned | ||
* @property {Document} [document] Document interface to use (default: `globalThis.document`) | ||
* @property {string} [namespace] `namespace` to use to create elements | ||
* @property {boolean} [fragment=false] | ||
* Whether a DOM fragment should be returned | ||
* @property {Document} [document] | ||
* Document interface to use (default: `globalThis.document`) | ||
* @property {string} [namespace] | ||
* `namespace` to use to create elements | ||
* @property {AfterTransform} [afterTransform] | ||
* Callback invoked after each node transformation | ||
* | ||
@@ -22,2 +35,3 @@ * @typedef Context | ||
* @property {string} [impliedNamespace] | ||
* @property {AfterTransform} [afterTransform] | ||
*/ | ||
@@ -32,5 +46,15 @@ | ||
* @param {HastNode} node | ||
* @param {Context} [ctx] | ||
* @param {Context} ctx | ||
*/ | ||
function transform(node, ctx) { | ||
const transformed = one(node, ctx) | ||
if (ctx.afterTransform) ctx.afterTransform(node, transformed) | ||
return transformed | ||
} | ||
/** | ||
* @param {HastNode} node | ||
* @param {Context} ctx | ||
*/ | ||
function one(node, ctx) { | ||
switch (node.type) { | ||
@@ -62,9 +86,9 @@ case 'root': | ||
const {children = []} = node | ||
const {length: childrenLength} = children | ||
let namespace = ctxNamespace | ||
let rootIsDocument = childrenLength === 0 | ||
let rootIsDocument = children.length === 0 | ||
let index = -1 | ||
for (let i = 0; i < childrenLength; i += 1) { | ||
const child = children[i] | ||
while (++index < children.length) { | ||
const child = children[index] | ||
@@ -78,3 +102,3 @@ if (child.type === 'element' && child.tagName === 'html') { | ||
// Take namespace of the first child. | ||
if (typeof ctxNamespace === 'undefined') { | ||
if (ctxNamespace === undefined) { | ||
namespace = String(properties.xmlns || '') || webNamespaces.html | ||
@@ -90,3 +114,3 @@ } | ||
if (rootIsDocument) { | ||
result = doc.implementation.createDocument(namespace, '', null) | ||
result = doc.implementation.createDocument(namespace || null, '', null) | ||
} else if (fragment) { | ||
@@ -201,2 +225,3 @@ result = doc.createDocumentFragment() | ||
if (mustUseProperty) { | ||
// @ts-expect-error: fine. | ||
result[property] = value | ||
@@ -203,0 +228,0 @@ } |
{ | ||
"name": "hast-util-to-dom", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "hast utility to transform to the DOM", | ||
@@ -41,2 +41,3 @@ "license": "ISC", | ||
"devDependencies": { | ||
"@types/glob": "^7.0.0", | ||
"@types/jsdom": "^16.0.0", | ||
@@ -48,6 +49,6 @@ "@types/tape": "^4.0.0", | ||
"hastscript": "^7.0.0", | ||
"jsdom": "^16.5.3", | ||
"jsdom": "^19.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
@@ -57,4 +58,4 @@ "tape": "^5.0.0", | ||
"typescript": "^4.0.0", | ||
"w3c-xmlserializer": "^2.0.0", | ||
"xo": "^0.39.0" | ||
"w3c-xmlserializer": "^3.0.0", | ||
"xo": "^0.47.0" | ||
}, | ||
@@ -61,0 +62,0 @@ "scripts": { |
@@ -29,3 +29,3 @@ # hast-util-to-dom | ||
```js | ||
import toDom from 'hast-util-to-dom'; | ||
import {toDom} from 'hast-util-to-dom'; | ||
@@ -92,2 +92,8 @@ const el = toDom({ | ||
###### `options.afterTransform` | ||
Function called when a hast node is transformed into a DOM node (`Function?`). | ||
Given the hast node that was handled as the first parameter and the | ||
corresponding DOM node as the second parameter. | ||
## Security | ||
@@ -94,0 +100,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15788
290
175
0
17