hast-util-is-element
hast utility to check if a node is a (certain) element
Last updated 5 years ago by wooorm .
MIT · Repository · Bugs · Original npm
$ cnpm install hast-util-is-element 
SYNC missed versions from official npm registry.

hast-util-is-element

Build Coverage Downloads Size Sponsors Backers Chat

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

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install hast-util-is-element

Use

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) element.

  • node (Node) — Node to check.
  • test (Function, string, or Array.<Test>, optional) — When array, checks if any one of the subtests pass. When string, checks that the element has that tag name. When function, see test
  • index (number, optional) — Index of node in parent
  • parent (Node, optional) — Parent of node
  • context (*, optional) — Context object to invoke test with
Returns

boolean — Whether test passed and node is an Element.

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
  • element (Element) — Element to check
  • index (number?) — Index of node in parent
  • parent (Node?) — Parent of node
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) attacks.

Related

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

Current Tags

  • 2.1.3                                ...           latest (3 years ago)

5 Versions

  • 2.1.3                                ...           3 years ago
  • 2.1.2                                ...           4 years ago
  • 2.1.1                                ...           4 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.4                                ...           6 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (8)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org