hast-util-is-element

hast utility to check if a node is a (certain) element

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
hast-util-is-element
802.1.2a year ago7 years agoMinified + gzip package size for hast-util-is-element in KB

Readme

hast-util-is-element
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
hasthast utility to check if a nodenode is a (certain) elementelement.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.
npm install hast-util-is-element
## Use

```js
import {isElement} from 'hast-util-is-element'

isElement({type: 'text', value: 'foo'}) // => false

isElement({type: 'element', tagName: 'a'}, 'a') // => true

isElement({type: 'element', tagName: 'a'}, ['a', 'area']) // => true

API

This package exports the following identifiers: isElement, convertElement. There is no default export.

isElement(node[, test[, index, parent[, context]]])

Check if the given value is a (certain) elementelement.
— When `array`, checks if any one of the subtests pass.
When `string`, checks that the element has that tag name.
When `function`, see [`test`][test]
  • index (number, optional) — Index of node in parent
  • parent (Nodenode, optional) — Parent of node
  • context (*, optional) — Context object to invoke test with
Returns
boolean — Whether test passed and node is an Elementelement.
Throws
Error — When an incorrect test, index, or parent is given. A node that is not a node, or not an element, does not throw.

function test(element[, index, parent])

Parameters
Context
* — The to is given context.
Returns
boolean? — Whether element matches.

convertElement(test)

Create a test function from test, that can later be called with a node, index, and parent. Useful if you’re going to test many nodes, for example when creating a utility where something else passes a compatible test.
The created function is slightly faster because it expects valid input only. Therefore, passing invalid input, yields unexpected results.

Security

hast-util-is-element does not change the syntax tree so there are no openings for cross-site scripting (XSS)xss attacks.

Related

— check if a node has a property
— check if a node is “Body OK” link element
— check if a node is a conditional comment
— check if a node is a CSS link element
— check if a node is a CSS style element
— check if a node is an embedded element
— check if a node is a heading element
— check if a node is interactive
— check if a node is a JavaScript script element
— check whether a node is labelable
— check if a node is phrasing content
— check if a node is a script-supporting element
— check if a node is a sectioning element
— check if a node is a transparent element
— check if a node is inter-element whitespace

Contribute

See contributing.md in syntax-tree/.githubcontributing for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor