hast utility to parse from HTML
npm install hast-util-from-html[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[hast][] utility that turns HTML into a syntax tree.
* What is this?
* When should I use this?
* Install
* Use
* API
* [fromHtml(value[, options])](#fromhtmlvalue-options)
* ErrorCode
* ErrorSeverity
* OnError
* Options
* Examples
* Example: fragment versus document
* Example: whitespace around and inside
* Example: parse errors
* Syntax
* Types
* Compatibility
* Security
* Related
* Contribute
* License
This package is a utility that takes HTML input and turns it into a hast syntax
tree.
If you want to handle syntax trees manually, use this.
Use [parse5][parse5] instead when you just want to parse HTML and don’t care
about [hast][].
You can also use [hast-util-from-parse5][hast-util-from-parse5] and
[parse5][parse5] yourself, or use the rehype plugin
[rehype-parse][rehype-parse], which wraps this utility to also parse HTML at
a higher-level (easier) abstraction.
[xast-util-from-xml][xast-util-from-xml] can be used if you are dealing with
XML instead of HTML.
If you might run in a browser and prefer a ligher alternative, while not caring
about positional info, parse errors, and consistency across browsers, use
[hast-util-from-html-isomorphic][hast-util-from-html-isomorphic], which
wraps this in Node and uses browser APIs otherwise.
Finally you can use the utility [hast-util-to-html][hast-util-to-html] for
the inverse of this utility.
It turns hast into HTML.
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install hast-util-from-html
In Deno with [esm.sh][esmsh]:
`js`
import {fromHtml} from 'https://esm.sh/hast-util-from-html@2'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {fromHtml} from 'hast-util-from-html'
const tree = fromHtml('
console.log(tree)
`
Yields:
`js`
{
type: 'root',
children: [
{
type: 'element',
tagName: 'h1',
properties: {},
children: [Array],
position: [Object]
}
],
data: { quirksMode: false },
position: {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 23, offset: 22 }
}
}
This package exports the identifier [fromHtml][api-from-html].
There is no default export.
Turn serialized HTML into a hast tree.
###### Parameters
* value ([Compatible][compatible])options
— serialized HTML to parse
* ([Options][api-options], optional)
— configuration
###### Returns
Tree ([Root][root]).
Known names of parse errors (TypeScript type).
###### Types
`tsoptions[key in ErrorCode]
type ErrorCode =
| 'abandonedHeadElementChild'
| 'abruptClosingOfEmptyComment'
| 'abruptDoctypePublicIdentifier'
// … see readme on above.`
Error severity (TypeScript type).
###### Types
`ts`
export type ErrorSeverity =
// Turn the parse error off:
| 0
| false
// Turn the parse error into a warning:
| 1
| true
// Turn the parse error into an actual error: processing stops.
| 2
Function called when encountering [HTML parse errors][parse-errors].
###### Parameters
* error ([VFileMessage][vfile-message])
— message
###### Returns
Nothing (void).
Configuration (TypeScript type).
##### Fields
###### options.space
Which space the document is in ('html' or 'svg', default: 'html').
When an
> 👉 Note: this is not an XML parser.
> It supports SVG as embedded in HTML.
> It does not support the features available in XML.
> Passing SVG files might break but fragments of modern SVG should be fine.
> Use [xast-util-from-xml][xast-util-from-xml] to parse XML.
> 👉 Note: make sure to set fragment: true if space: 'svg'.
###### options.verbose
Add extra positional info about attributes, start tags, and end tags
(boolean, default: false).
###### options.fragment
Whether to parse as a fragment (boolean, default: false).html
The default is to expect a whole document.
In document mode, unopened , head, and body elements are opened.
###### options.onerror
Function called when encountering [HTML parse errors][parse-errors]
([OnError][api-on-error], optional).
###### options[key in ErrorCode]
Specific parse errors can be configured by setting their identifiers (see
[ErrorCode][api-error-code]) as keys directly in options to anErrorSeverity
[][api-error-severity] as value.
The list of parse errors:
* abandonedHeadElementChild — unexpected metadata element after head (example)abruptClosingOfEmptyComment
* — unexpected abruptly closed empty comment (example)abruptDoctypePublicIdentifier
* — unexpected abruptly closed public identifier (example)abruptDoctypeSystemIdentifier
* — unexpected abruptly closed system identifier (example)absenceOfDigitsInNumericCharacterReference
* — unexpected non-digit at start of numeric character reference (example)cdataInHtmlContent
* — unexpected CDATA section in HTML (example)characterReferenceOutsideUnicodeRange
* — unexpected too big numeric character reference (example)closingOfElementWithOpenChildElements
* — unexpected closing tag with open child elements (example)controlCharacterInInputStream
* — unexpected control character (example)controlCharacterReference
* — unexpected control character reference (example)disallowedContentInNoscriptInHead
* — disallowed content inside
The following example shows the difference between parsing as a document and
parsing as a fragment:
`js
import {fromHtml} from 'hast-util-from-html'
const doc = '
console.log(fromHtml(doc))
console.log(fromHtml(doc, {fragment: true}))
`
…yields (positional info and data omitted for brevity):
`js`
{
type: 'root',
children: [
{type: 'element', tagName: 'html', properties: {}, children: [Array]}
]
}
`js`
{
type: 'root',
children: [
{type: 'element', tagName: 'title', properties: {}, children: [Array]},
{type: 'element', tagName: 'h1', properties: {}, children: [Array]}
]
}
> 👉 Note: observe that when a whole document is expected (first example),
> missing elements are opened and closed.
The following example shows how whitespace is handled when around and directly
inside the element:
`js
import {fromHtml} from 'hast-util-from-html'
import {inspect} from 'unist-util-inspect'
const doc =
console.log(inspect(fromHtml(doc)))
`…yields:
`txt
root[2] (1:1-9:8, 0-119)
│ data: {"quirksMode":false}
├─0 doctype (1:1-1:16, 0-15)
└─1 element[3] (2:1-9:8, 16-119)
│ properties: {"lang":"en"}
├─0 element[3] (3:3-5:10, 33-72)
│ │ properties: {}
│ ├─0 text "\n " (3:9-4:5, 39-44)
│ ├─1 element[1] (4:5-4:23, 44-62)
│ │ │ properties: {}
│ │ └─0 text "Hi!" (4:12-4:15, 51-54)
│ └─2 text "\n " (4:23-5:3, 62-65)
├─1 text "\n " (5:10-6:3, 72-75)
└─2 element [3] (6:3-8:10, 75-111)
│ properties: {}
├─0 text "\n " (6:9-7:5, 81-86)
├─1 element[1] (7:5-7:20, 86-101)
│ │ properties: {}
│ └─0 text "Hello!" (7:9-7:15, 90-96)
└─2 text "\n \n" (7:20-9:1, 101-112)
`> 👉 Note: observe that the line ending before
is ignored, the line
> ending and two spaces before is moved inside it, and the line ending
> after is moved before it.This behavior is described by the HTML standard (see the section 13.2.6.4.1
“The ‘initial’ insertion mode” and adjacent states) which we follow.
The changes to this meaningless whitespace should not matter, except when
formatting markup, in which case [
rehype-format][rehype-format] can be used to
improve the source code.$3
The following example shows how HTML parse errors can be enabled and configured:
`js
import {fromHtml} from 'hast-util-from-html'const doc =
fromHtml(doc, {
onerror: console.log,
missingWhitespaceBeforeDoctypeName: 2, // Mark one as a fatal error.
nonVoidHtmlElementStartTagWithTrailingSolidus: false // Ignore one.
})
`…yields:
`txt
[1:10-1:10: Missing whitespace before doctype name] {
ancestors: undefined,
cause: undefined,
column: 10,
fatal: true,
line: 1,
place: {
start: { line: 1, column: 10, offset: 9 },
end: { line: 1, column: 10, offset: 9 }
},
reason: 'Missing whitespace before doctype name',
ruleId: 'missing-whitespace-before-doctype-name',
source: 'hast-util-from-html',
note: 'Unexpected h. Expected ASCII whitespace instead',
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-before-doctype-name'
}
[2:23-2:23: Unexpected duplicate attribute] {
ancestors: undefined,
cause: undefined,
column: 23,
fatal: false,
line: 2,
place: {
start: { line: 2, column: 23, offset: 37 },
end: { line: 2, column: 23, offset: 37 }
},
reason: 'Unexpected duplicate attribute',
ruleId: 'duplicate-attribute',
source: 'hast-util-from-html',
note: 'Unexpectedly double attribute. Expected attributes to occur only once',
url: 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute'
}
`> 🧑🏫 Info: messages in unified are warnings instead of errors.
> Other linters (such as ESLint) almost always use errors.
> Why?
> Those tools only check code style.
> They don’t generate, transform, and format code, which is what we focus on,
> too.
> Errors in unified mean the same as an exception in your JavaScript code: a
> crash.
> That’s why we use warnings instead, because we can continue doing work.
Syntax
HTML is parsed according to WHATWG HTML (the living standard), which is also
followed by browsers such as Chrome and Firefox.
Types
This package is fully typed with [TypeScript][].
It exports the additional types
[
ErrorCode][api-error-code], [ErrorSeverity][api-error-severity],
[OnError][api-on-error], and [Options][api-options].Compatibility
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-html@^2,
compatible with Node.js 16.Security
Parsing HTML is safe but using user-provided content can open you up to a
[cross-site scripting (XSS)][xss] attack.
Use [
hast-util-santize][hast-util-sanitize] to make the hast tree safe.Related
hast-util-to-html
— serialize hast
* hast-util-sanitize
— sanitize hast
* [xast-util-from-xml][xast-util-from-xml]
— parse XMLContribute
See [
contributing.md][contributing] in [syntax-tree/.github][health] for
ways to get started.
See [support.md`][support] for ways to get help.This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/syntax-tree/hast-util-from-html/workflows/main/badge.svg
[build]: https://github.com/syntax-tree/hast-util-from-html/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-from-html.svg
[coverage]: https://codecov.io/github/syntax-tree/hast-util-from-html
[downloads-badge]: https://img.shields.io/npm/dm/hast-util-from-html.svg
[downloads]: https://www.npmjs.com/package/hast-util-from-html
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=hast-util-from-html
[size]: https://bundlejs.com/?q=hast-util-from-html
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/syntax-tree/unist/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[license]: license
[author]: https://wooorm.com
[health]: https://github.com/syntax-tree/.github
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
[hast]: https://github.com/syntax-tree/hast
[root]: https://github.com/syntax-tree/hast#root
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
[hast-util-from-parse5]: https://github.com/syntax-tree/hast-util-from-parse5
[hast-util-to-html]: https://github.com/syntax-tree/hast-util-to-html
[xast-util-from-xml]: https://github.com/syntax-tree/xast-util-from-xml
[rehype-parse]: https://github.com/rehypejs/rehype/tree/main/packages/rehype-parse#readme
[rehype-format]: https://github.com/rehypejs/rehype-format
[parse5]: https://github.com/inikulin/parse5
[parse-errors]: https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
[vfile-message]: https://github.com/vfile/vfile-message#vfilemessagereason-place-origin
[hast-util-from-html-isomorphic]: https://github.com/syntax-tree/hast-util-from-html-isomorphic
[compatible]: https://github.com/vfile/vfile#compatible
[api-from-html]: #fromhtmlvalue-options
[api-error-code]: #errorcode
[api-error-severity]: #errorseverity
[api-on-error]: #onerror
[api-options]: #options