Package Exports
- hast-util-from-dom
- hast-util-from-dom/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hast-util-from-dom) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
hast-util-from-dom
hast utility to transform from a DOM tree.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Contribute
- Related
- License
What is this?
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.
When should I use this?
You can use this project when you want to use hast in browsers. This package is very small, but it does so by:
- …not providing positional information
- …potentially yielding varying results in different (especially older) browsers
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.
Install
This package is ESM only. In Node.js (version 14.14+ and 16.0+), install with npm:
npm install hast-util-from-dom
In Deno with esm.sh
:
import {fromDom} from 'https://esm.sh/hast-util-from-dom@4'
In browsers with esm.sh
:
<script type="module">
import {fromDom} from 'https://esm.sh/hast-util-from-dom@4?bundle'
</script>
Use
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}
API
This package exports the identifier fromDom
.
There is no default export.
fromDom(tree, options?)
Transform a DOM tree to a hast tree.
Parameters
Returns
Equivalent hast node (HastNode
).
AfterTransform
Callback called when each node is transformed (TypeScript type).
Parameters
Returns
Nothing.
Options
Configuration (TypeScript type).
Fields
afterTransform
(AfterTransform
, optional) — callback called when each node is transformed
Returns
Types
This package is fully typed with TypeScript.
It exports the additional type Options
.
Compatibility
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.
Security
Use of hast-util-from-dom
itself is safe but see other utilities for more
information on potential security problems.
Contribute
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.
Related
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 hast