What is hast-util-has-property?
The `hast-util-has-property` package is a utility for checking if a given HAST (Hypertext Abstract Syntax Tree) node has a specific property. This is particularly useful when working with HAST nodes in the context of HTML processing and transformations.
What are hast-util-has-property's main functionalities?
Check if a node has a specific property
This feature allows you to check if a given HAST node has a specific property. In the example, the node is an anchor element with an `href` property, and the function checks for the presence of this property.
const hasProperty = require('hast-util-has-property');
const node = { type: 'element', tagName: 'a', properties: { href: 'https://example.com' } };
const result = hasProperty(node, 'href');
console.log(result); // true
Other packages similar to hast-util-has-property
unist-util-visit
The `unist-util-visit` package is used to recursively walk through a Unist syntax tree, which includes HAST. It allows you to visit nodes and perform actions on them. While it doesn't specifically check for properties, it can be used to traverse nodes and then check for properties manually.
hast-util-select
The `hast-util-select` package provides CSS-like selectors to find nodes in a HAST tree. It can be used to select nodes based on their properties, among other criteria. This package offers more advanced querying capabilities compared to `hast-util-has-property`.
hast-util-is-element
The `hast-util-is-element` package checks if a node is a specific element. While it doesn't directly check for properties, it can be used in conjunction with other utilities to achieve similar functionality.
hast-util-has-property
hast utility to check if an element has a
property.
Install
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install hast-util-has-property
Use
import {hasProperty} from 'hast-util-has-property'
hasProperty({type: 'text', value: 'alpha'}, 'bravo')
hasProperty(
{
type: 'element',
tagName: 'div',
properties: {id: 'bravo'},
children: []
},
'className'
)
hasProperty(
{
type: 'element',
tagName: 'div',
properties: {id: 'charlie'},
children: []
},
'id'
)
API
This package exports the following identifiers: hasProperty
.
There is no default export.
hasProperty(node, name)
Check if node
is an element that has a name
property name.
Parameters
Returns
boolean
— Whether node
is an element that has a name
property name.
Security
hast-util-has-property
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