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 certain property.
Contents
What is this?
This package is a small utility that checks if a node is an element that has
a given property.
When should I use this?
This utility is super niche, if you’re here you probably know what you’re
looking for!
Install
This package is ESM only.
In Node.js (version 16+), install with npm:
npm install hast-util-has-property
In Deno with esm.sh
:
import {hasProperty} from 'https://esm.sh/hast-util-has-property@3'
In browsers with esm.sh
:
<script type="module">
import {hasProperty} from 'https://esm.sh/hast-util-has-property@3?bundle'
</script>
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 identifier hasProperty
.
There is no default export.
hasProperty(node, field)
Check if node
is an element and has a name
property.
Parameters
node
(Node
) — node to check (typically Element
)name
(string
) - property name to check
Returns
Whether node
is an element that has a name
property (boolean
).
Types
This package is fully typed with TypeScript.
It exports no additional types.
Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
hast-util-has-property@^3
, compatible with Node.js 16.
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