Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
* @typedef {import('hast').Comment} HastComment
* @typedef {HastParent['children'][number]} HastChild
* @typedef {HastChild|HastRoot} HastNode
*
* @callback AfterTransform
* Function called when a DOM node is transformed into a hast node
* @param {Node} domNode
* The DOM node that was handled
* @param {HastNode|undefined} hastNode
* The corresponding hast node
* @returns {void}
*
* @typedef Options
* @property {AfterTransform} [afterTransform]
*
* @typedef Context
* @property {AfterTransform} [afterTransform]
*/

import {webNamespaces} from 'web-namespaces'
Expand All @@ -21,17 +35,29 @@ const DOCUMENT_FRAGMENT_NODE = 11

/**
* @param {Node} node
* @param {Context} ctx
* @returns {HastNode|undefined}
*/
function transform(node, ctx) {
const transformed = one(node, ctx)
if (ctx.afterTransform) ctx.afterTransform(node, transformed)
return transformed
}

/**
* @param {Node} node
* @param {Context} ctx
* @returns {HastNode|undefined}
*/
function transform(node) {
function one(node, ctx) {
switch (node.nodeType) {
case ELEMENT_NODE:
// @ts-expect-error TypeScript is wrong.
return element(node)
return element(node, ctx)
case DOCUMENT_NODE:
case DOCUMENT_FRAGMENT_NODE:
// @ts-expect-error TypeScript is wrong.
return root(node)
return root(node, ctx)
case TEXT_NODE:
// @ts-expect-error TypeScript is wrong.
return text(node)
Expand All @@ -49,10 +75,11 @@ function transform(node) {
* Transform a document.
*
* @param {Document|DocumentFragment} node
* @param {Context} ctx
* @returns {HastRoot}
*/
function root(node) {
return {type: 'root', children: all(node)}
function root(node, ctx) {
return {type: 'root', children: all(node, ctx)}
}

/**
Expand Down Expand Up @@ -89,9 +116,10 @@ function comment(node) {
* Transform an element.
*
* @param {Element} node
* @param {Context} ctx
* @returns {HastElement}
*/
function element(node) {
function element(node, ctx) {
const space = node.namespaceURI
const fn = space === webNamespaces.svg ? s : h
const tagName =
Expand All @@ -109,23 +137,24 @@ function element(node) {
props[attributes[index]] = node.getAttribute(attributes[index]) || ''
}

return fn(tagName, props, all(content))
return fn(tagName, props, all(content, ctx))
}

/**
* Transform an element.
*
* @param {Document|DocumentFragment|Element} node
* @param {Context} ctx
* @returns {Array.<HastChild>}
*/
function all(node) {
function all(node, ctx) {
const nodes = node.childNodes
/** @type {Array.<HastChild>} */
const children = []
let index = -1

while (++index < nodes.length) {
const child = transform(nodes[index])
const child = transform(nodes[index], ctx)

if (child !== undefined) {
// @ts-expect-error Assume no document inside document.
Expand All @@ -138,8 +167,9 @@ function all(node) {

/**
* @param {Node} node
* @param {Options} [options]
* @returns {HastNode}
*/
export function fromDom(node) {
return transform(node || {}) || {type: 'root', children: []}
export function fromDom(node, options = {}) {
return transform(node || {}, options) || {type: 'root', children: []}
}
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ This works in a similar way to the [`parse5`][hast-util-from-parse5] version
except that it works directly from the DOM rather than a string of HTML.
Consequently, it does not maintain [positional info][positional-information].

##### `options`

###### `options.afterTransform`

Function called when a DOM node is transformed into a hast node (`Function?`).
Given the DOM node that was handled as the first parameter and the
corresponding hast node as the second parameter.

## Security

Use of `hast-util-from-dom` can open you up to a
Expand Down
38 changes: 38 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('../lib/index.js').HastNode} HastNode
*/

/* eslint-env browser */

import fs from 'node:fs'
Expand Down Expand Up @@ -144,6 +148,40 @@ test('hast-util-from-dom', (t) => {
'should support an attribute w/o value'
)

const heading = document.createElement('h2')
const text = document.createTextNode('Hello')
heading.append(text)

t.deepEqual(
(() => {
/** @type {Array<[Node, HastNode|undefined]>} */
const calls = []
fromDom(heading, {
/**
* @param {Node} node
* @param {HastNode|undefined} transformed
*/
afterTransform: (node, transformed) => {
calls.push([node, transformed])
}
})
return calls
})(),
[
[text, {type: 'text', value: 'Hello'}],
[
heading,
{
type: 'element',
tagName: 'h2',
properties: {},
children: [{type: 'text', value: 'Hello'}]
}
]
],
'should invoke afterTransform'
)

t.end()
})

Expand Down