What is hast-util-is-element?
The hast-util-is-element package is a utility for working with HAST (Hypertext Abstract Syntax Tree) nodes. It provides functions to check if a node is an element and optionally matches a given tag name. This is particularly useful when manipulating or inspecting HTML represented within JavaScript.
What are hast-util-is-element's main functionalities?
Check if a node is an element
This feature allows you to verify if a given node is an element node within a HAST structure. It's useful for filtering or processing nodes in a tree.
const isElement = require('hast-util-is-element');
const node = {type: 'element', tagName: 'div'};
console.log(isElement(node)); // Outputs: true
Check if a node is a specific element
This feature extends the basic check to allow for specification of the tag name. It's useful for finding or working with specific types of elements in a document.
const isElement = require('hast-util-is-element');
const node = {type: 'element', tagName: 'span'};
console.log(isElement(node, 'span')); // Outputs: true
Other packages similar to hast-util-is-element
unist-util-is
This package offers a more generalized approach for checking node types in unist trees, which is a part of the broader unified ecosystem. Unlike hast-util-is-element, unist-util-is can be used with different syntax trees beyond HTML, offering greater flexibility.
hast-util-has-property
Similar in utility scope within the HAST ecosystem, this package checks for the presence of properties in elements. While hast-util-is-element focuses on the type and tag name of nodes, hast-util-has-property deals with the properties of those nodes, providing a complementary functionality.
hast-util-is-element
hast utility to check if a node is a (certain)
element.
Install
npm:
npm install hast-util-is-element
Use
var is = require('hast-util-is-element')
is({type: 'text', value: 'foo'})
is({type: 'element', tagName: 'a'}, 'a')
is({type: 'element', tagName: 'a'}, ['a', 'area'])
API
isElement(node[, tagName|tagNames])
Check if the given value is a (certain) element.
- When given a
tagName
or tagNames
, checks that node
is an
element whose tagName
field matches tagName
or is included
in tagNames
- Otherwise checks that
node
is an element
Parameters
node
(*
) — Value to check, probably Node
tagName
(string
, optional) — Value that node
s tagName
field should
matchtagNames
(Array.<string>
, optional) — Values that should include node
s
tagName
field should match
Returns
boolean
— whether node
passes the test.
Throws
Error
— When the second parameter is given but invalid.
Security
hast-util-is-element
does not change the syntax tree so there are no openings
for cross-site scripting (XSS) attacks.
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, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer