Hypertext Abstract Syntax Tree format.
hast is a specification for representing HTML (and embedded SVG or MathML) as an abstract syntax tree. It implements the unist spec.
This document may not be released.
See releases for released documents.
The latest released version is 2.4.0
.
Contents
- Introduction
- Types
- Nodes (abstract)
- Nodes
- Other types
- Glossary
- List of utilities
- Related HTML utilities
- References
- Security
- Related
- Contribute
- Acknowledgments
- License
Introduction
This document defines a format for representing hypertext as an abstract syntax tree. Development of hast started in April 2016 for rehype. This specification is written in a Web IDL-like grammar.
Where this specification fits
hast extends unist, a format for syntax trees, to benefit from its ecosystem of utilities.
hast relates to JavaScript in that it has an ecosystem of utilities for working with compliant syntax trees in JavaScript. However, hast is not limited to JavaScript and can be used in other programming languages.
hast relates to the unified and rehype projects in that hast syntax trees are used throughout their ecosystems.
Virtual DOM
The reason for introducing a new βvirtualβ DOM is primarily:
- The DOM is very heavy to implement outside of the browser, a lean and stripped down virtual DOM can be used everywhere
- Most virtual DOMs do not focus on ease of use in transformations
- Other virtual DOMs cannot represent the syntax of HTML in its entirety (think comments and document types)
- Neither the DOM nor virtual DOMs focus on positional information
Types
If you are using TypeScript, you can use the hast types by installing them with npm:
npm install @types/hast
Nodes (abstract)
Literal
interface Literal <: UnistLiteral {
value: string
}
Literal (UnistLiteral) represents a node in hast containing a value.
Parent
interface Parent <: UnistParent {
children: [Comment | Doctype | Element | Text]
}
Parent (UnistParent) represents a node in hast containing other nodes (said to be children).
Its content is limited to only other hast content.
Nodes
Comment
interface Comment <: Literal {
type: 'comment'
}
Comment (Literal) represents a Comment ([DOM]).
For example, the following HTML:
<!--Charlie-->
Yields:
{type: 'comment', value: 'Charlie'}
Doctype
interface Doctype <: Node {
type: 'doctype'
}
Doctype (Node) represents a DocumentType ([DOM]).
For example, the following HTML:
<!doctype html>
Yields:
{type: 'doctype'}
Element
interface Element <: Parent {
type: 'element'
tagName: string
properties: Properties?
content: Root?
children: [Comment | Element | Text]
}
Element (Parent) represents an Element ([DOM]).
A tagName
field must be present.
It represents the elementβs local name ([DOM]).
The properties
field represents information associated with the element.
The value of the properties
field implements the
Properties interface.
If the tagName
field is 'template'
, a content
field can be present.
The value of the content
field implements the Root interface.
If the tagName
field is 'template'
, the element must be a
leaf.
If the tagName
field is 'noscript'
, its children should
be represented as if scripting is disabled
([HTML]).
For example, the following HTML:
<a href="https://alpha.com" class="bravo" download></a>
Yields:
{
type: 'element',
tagName: 'a',
properties: {
href: 'https://alpha.com',
className: ['bravo'],
download: true
},
children: []
}
Root
interface Root <: Parent {
type: 'root'
}
Root (Parent) represents a document.
Root can be used as the root of a tree, or as
a value of the content
field on a 'template'
Element,
never as a child.
Text
interface Text <: Literal {
type: 'text'
}
Text (Literal) represents a Text ([DOM]).
For example, the following HTML:
<span>Foxtrot</span>
Yields:
{
type: 'element',
tagName: 'span',
properties: {},
children: [{type: 'text', value: 'Foxtrot'}]
}
Other types
Properties
interface Properties {}
Properties represents information associated with an element.
Every field must be a PropertyName and every value a PropertyValue.
PropertyName
typedef string PropertyName
Property names are keys on Properties objects and reflect
HTML, SVG, ARIA, XML, XMLNS, or XLink attribute names.
Often, they have the same value as the corresponding attribute (for example,
id
is a property name reflecting the id
attribute name), but there are some
notable differences.
These rules arenβt simple. Use
hastscript
(orproperty-information
directly) to help.
The following rules are used to transform HTML attribute names to property names. These rules are based on how ARIA is reflected in the DOM ([ARIA]), and differs from how some (older) HTML attributes are reflected in the DOM.
- Any name referencing a combinations of multiple words (such as βstroke
miter limitβ) becomes a camelcased property name capitalizing each word
boundary.
This includes combinations that are sometimes written as several words.
For example,
stroke-miterlimit
becomesstrokeMiterLimit
,autocorrect
becomesautoCorrect
, andallowfullscreen
becomesallowFullScreen
. - Any name that can be hyphenated, becomes a camelcased property name
capitalizing each boundary.
For example, βread-onlyβ becomes
readOnly
. - Compound words that are not used with spaces or hyphens are treated as a normal word and the previous rules apply. For example, βplaceholderβ, βstrikethroughβ, and βplaybackβ stay the same.
- Acronyms in names are treated as a normal word and the previous rules apply.
For example,
itemid
becomeitemId
andbgcolor
becomesbgColor
.
Exceptions
Some jargon is seen as one word even though it may not be seen as such by
dictionaries.
For example, nohref
becomes noHref
, playsinline
becomes playsInline
,
and accept-charset
becomes acceptCharset
.
The HTML attributes class
and for
respectively become className
and
htmlFor
in alignment with the DOM.
No other attributes gain different names as properties, other than a change in
casing.
Notes
property-information
lists all property names.
The property name rules differ from how HTML is reflected in the DOM for the following attributes:
View list of differences
charoff
becomescharOff
(notchOff
)char
stayschar
(does not becomech
)rel
staysrel
(does not becomerelList
)checked
stayschecked
(does not becomedefaultChecked
)muted
staysmuted
(does not becomedefaultMuted
)value
staysvalue
(does not becomedefaultValue
)selected
staysselected
(does not becomedefaultSelected
)allowfullscreen
becomesallowFullScreen
(notallowFullscreen
)hreflang
becomeshrefLang
, nothreflang
autoplay
becomesautoPlay
, notautoplay
autocomplete
becomesautoComplete
(notautocomplete
)autofocus
becomesautoFocus
, notautofocus
enctype
becomesencType
, notenctype
formenctype
becomesformEncType
(notformEnctype
)vspace
becomesvSpace
, notvspace
hspace
becomeshSpace
, nothspace
lowsrc
becomeslowSrc
, notlowsrc
PropertyValue
typedef any PropertyValue
Property values should reflect the data type determined by their property name.
For example, the HTML <div hidden></div>
has a hidden
attribute, which is
reflected as a hidden
property name set to the property value true
, and
<input minlength="5">
, which has a minlength
attribute, is reflected as a
minLength
property name set to the property value 5
.
In JSON, the value
null
must be treated as if the property was not included. In JavaScript, bothnull
andundefined
must be similarly ignored.
The DOM has strict rules on how it coerces HTML to expected values, whereas hast
is more lenient in how it reflects the source.
Where the DOM treats <div hidden="no"></div>
as having a value of true
and
<img width="yes">
as having a value of 0
, these should be reflected as
'no'
and 'yes'
, respectively, in hast.
The reason for this is to allow plugins and utilities to inspect these non-standard values.
The DOM also specifies comma separated and space separated lists attribute
values.
In hast, these should be treated as ordered lists.
For example, <div class="alpha bravo"></div>
is represented as ['alpha', 'bravo']
.
Thereβs no special format for the property value of the
style
property name.
Glossary
See the unist glossary.
List of utilities
See the unist list of utilities for more utilities.
hastscript
β create treeshast-util-assert
β assert nodeshast-util-class-list
β simulate the browserβsclassList
API for hast nodeshast-util-classnames
β merge class names togetherhast-util-embedded
β check if a node is an embedded elementhast-util-excerpt
β truncate the tree to a commenthast-util-find-and-replace
β find and replace text in a treehast-util-from-dom
β transform from DOM treehast-util-from-html
β parse from HTMLhast-util-from-parse5
β transform from Parse5βs ASThast-util-from-selector
β parse CSS selectors to nodeshast-util-from-string
β set the plain-text value of a node (textContent
)hast-util-from-text
β set the plain-text value of a node (innerText
)hast-util-from-webparser
β transform Webparserβs AST to hasthast-util-has-property
β check if an element has a certain propertyhast-util-heading
β check if a node is heading contenthast-util-heading-rank
β get the rank (also known as depth or level) of headingshast-util-interactive
β check if a node is interactivehast-util-is-body-ok-link
β check if alink
element is βBody OKβhast-util-is-conditional-comment
β check ifnode
is a conditional commenthast-util-is-css-link
β check ifnode
is a CSSlink
hast-util-is-css-style
β check ifnode
is a CSSstyle
hast-util-is-element
β check ifnode
is a (certain) elementhast-util-is-event-handler
β check ifproperty
is an event handlerhast-util-is-javascript
β check ifnode
is a JavaScriptscript
hast-util-labelable
β check ifnode
is labelablehast-util-parse-selector
β create an element from a simple CSS selectorhast-util-phrasing
β check if a node is phrasing contenthast-util-raw
β parse a tree againhast-util-reading-time
β estimate the reading timehast-util-sanitize
β sanitize nodeshast-util-script-supporting
β check ifnode
is script-supporting contenthast-util-select
βquerySelector
,querySelectorAll
, andmatches
hast-util-sectioning
β check ifnode
is sectioning contenthast-util-shift-heading
β change heading rank (depth, level)hast-util-table-cell-style
β transform deprecated styling attributes on table cells to inline styleshast-util-to-dom
β transform to a DOM treehast-util-to-estree
β transform to estree (JavaScript AST) JSXhast-util-to-html
β serialize as HTMLhast-util-to-jsx
β transform hast to JSXhast-util-to-jsx-runtime
β transform to preact, react, solid, svelte, vue, etchast-util-to-mdast
β transform to mdast (markdown)hast-util-to-nlcst
β transform to nlcst (natural language)hast-util-to-parse5
β transform to Parse5βs ASThast-util-to-portable-text
β transform to portable texthast-util-to-string
β get the plain-text value of a node (textContent
)hast-util-to-text
β get the plain-text value of a node (innerText
)hast-util-to-xast
β transform to xast (xml)hast-util-transparent
β check ifnode
is transparent contenthast-util-truncate
β truncate the tree to a certain number of charactershast-util-whitespace
β check ifnode
is inter-element whitespace
Related HTML utilities
a-rel
β List of link types forrel
ona
/area
aria-attributes
β List of ARIA attributescollapse-white-space
β Replace multiple white-space characters with a single spacecomma-separated-tokens
β Parse/stringify comma separated tokenshtml-tag-names
β List of HTML tag nameshtml-dangerous-encodings
β List of dangerous HTML character encoding labelshtml-encodings
β List of HTML character encoding labelshtml-element-attributes
β Map of HTML attributeshtml-event-attributes
β List of HTML event handler content attributeshtml-void-elements
β List of void HTML tag nameslink-rel
β List of link types forrel
onlink
mathml-tag-names
β List of MathML tag namesmeta-name
β List of values forname
onmeta
property-information
β Information on HTML propertiesspace-separated-tokens
β Parse/stringify space separated tokenssvg-tag-names
β List of SVG tag namessvg-element-attributes
β Map of SVG attributessvg-event-attributes
β List of SVG event handler content attributesweb-namespaces
β Map of web namespaces
References
- unist: Universal Syntax Tree. T. Wormer; et al.
- JavaScript: ECMAScript Language Specification. Ecma International.
- HTML: HTML Standard, A. van Kesteren; et al. WHATWG.
- DOM: DOM Standard, A. van Kesteren, A. Gregor, Ms2ger. WHATWG.
- SVG: Scalable Vector Graphics (SVG), N. Andronikos, R. Atanassov, T. Bah, B. Birtles, B. Brinza, C. Concolato, E. DahlstrΓΆm, C. Lilley, C. McCormack, D. Schepers, R. Schwerdtfeger, D. Storey, S. Takagi, J. Watt. W3C.
- MathML: Mathematical Markup Language Standard, D. Carlisle, P. Ion, R. Miner. W3C.
- ARIA: Accessible Rich Internet Applications (WAI-ARIA), J. Diggs, J. Craig, S. McCarron, M. Cooper. W3C.
- JSON The JavaScript Object Notation (JSON) Data Interchange Format, T. Bray. IETF.
- Web IDL: Web IDL, C. McCormack. W3C.
Security
As hast represents HTML, and improper use of HTML can open you up to a
cross-site scripting (XSS) attack, improper use of hast is also unsafe.
Always be careful with user input and use hast-util-santize
to
make the hast tree safe.
Related
- mdast β Markdown Abstract Syntax Tree format
- nlcst β Natural Language Concrete Syntax Tree format
- xast β Extensible Abstract Syntax Tree
Contribute
See contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
Ideas for new utilities and tools can be posted in syntax-tree/ideas
.
A curated list of awesome syntax-tree, unist, mdast, hast, xast, and nlcst resources can be found in awesome syntax-tree.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
Acknowledgments
The initial release of this project was authored by @wooorm.
Special thanks to @eush77 for their work, ideas, and incredibly valuable feedback!
Thanks to @andrewburgess, @arobase-che, @arystan-sw, @BarryThePenguin, @brechtcs, @ChristianMurphy, @ChristopherBiscardi, @craftzdog, @cupojoe, @davidtheclark, @derhuerst, @detj, @DxCx, @erquhart, @flurmbo, @Hamms, @Hypercubed, @inklesspen, @jeffal, @jlevy, @Justineo, @lfittl, @kgryte, @kmck, @kthjm, @KyleAMathews, @macklinu, @medfreeman, @Murderlon, @nevik, @nokome, @phiresky, @revolunet, @rhysd, @Rokt33r, @rubys, @s1n, @Sarah-Seo, @sethvincent, @simov, @s1n, @StarpTech, @stefanprobst, @stuff, @subhero24, @tripodsan, @tunnckoCore, @vhf, @voischev, and @zjaml, for contributing to hast and related projects!