Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
hast-util-from-dom
Advanced tools
The `hast-util-from-dom` package is a utility for converting DOM nodes to HAST (Hypertext Abstract Syntax Tree) nodes. This is particularly useful for working with HTML content in a structured and programmatic way, enabling transformations, analysis, and rendering of HTML content.
Convert DOM to HAST
This feature allows you to convert a DOM node to a HAST node. In this example, we use `jsdom` to create a DOM from an HTML string and then convert a paragraph element to a HAST node.
const { fromDom } = require('hast-util-from-dom');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const dom = new JSDOM('<!DOCTYPE html><p>Hello world</p>');
const hast = fromDom(dom.window.document.querySelector('p'));
console.log(hast);
The `rehype-parse` package is used to parse HTML into a HAST tree directly from an HTML string. Unlike `hast-util-from-dom`, which converts existing DOM nodes to HAST, `rehype-parse` works directly with HTML strings and is part of the unified collective of tools for processing content.
hast utility to transform from a DOM tree.
This package is a utility that takes a DOM tree (from the actual DOM or from
things like jsdom
) as input and turns it into a hast (HTML)
syntax tree.
You can use this project when you want to use hast in browsers. This package is very small, but it does so by:
The hast utility hast-util-to-dom
does the inverse of this
utility.
It turns hast into a DOM tree.
The rehype plugin rehype-dom-parse
wraps this utility to
parse HTML with DOM APIs.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install hast-util-from-dom
In Deno with esm.sh
:
import {fromDom} from 'https://esm.sh/hast-util-from-dom@5'
In browsers with esm.sh
:
<script type="module">
import {fromDom} from 'https://esm.sh/hast-util-from-dom@5?bundle'
</script>
Say our page example.html
looks as follows:
<!doctype html>
<title>Example</title>
<body>
<main>
<h1>Hi</h1>
<p><em>Hello</em>, world!</p>
</main>
<script type="module">
import {fromDom} from 'https://esm.sh/hast-util-from-dom@4?bundle'
const hast = fromDom(document.querySelector('main'))
console.log(hast)
</script>
Now running open example.html
prints the following to the console:
{type: "element", tagName: "main", properties: {}, children: Array}
This package exports the identifier fromDom
.
There is no default export.
fromDom(tree, options?)
Transform a DOM tree to a hast tree.
Equivalent hast node (HastNode
).
AfterTransform
Callback called when each node is transformed (TypeScript type).
Nothing.
Options
Configuration (TypeScript type).
afterTransform
(AfterTransform
, optional)
— callback called when each node is transformedThis package is fully typed with TypeScript.
It exports the additional types AfterTransform
and
Options
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, hast-util-from-dom@^5
,
compatible with Node.js 16.
Use of hast-util-from-dom
itself is safe but see other utilities for more
information on potential security problems.
See contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.
hast-util-from-html
— parse hast from a string of HTMLhast-util-sanitize
— sanitize hast nodeshast-util-to-html
— serialize hast as HTMLhast-util-to-dom
— create DOM trees from hastFAQs
hast utility to create a tree from the DOM
We found that hast-util-from-dom demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.