hast-util-is-element
Advanced tools
Comparing version 1.0.4 to 1.1.0
48
index.js
'use strict' | ||
var convert = require('./convert') | ||
module.exports = isElement | ||
// Check if if `node` is an `element` and, if `tagNames` is given, `node` | ||
// matches them `tagNames`. | ||
function isElement(node, tagNames) { | ||
var name | ||
isElement.convert = convert | ||
// Check if if `node` is an `element` and whether it passes the given test. | ||
function isElement(node, test, index, parent, context) { | ||
var hasParent = parent !== null && parent !== undefined | ||
var hasIndex = index !== null && index !== undefined | ||
var check = convert(test) | ||
if ( | ||
!( | ||
tagNames === null || | ||
tagNames === undefined || | ||
typeof tagNames === 'string' || | ||
(typeof tagNames === 'object' && tagNames.length !== 0) | ||
) | ||
hasIndex && | ||
(typeof index !== 'number' || index < 0 || index === Infinity) | ||
) { | ||
throw new Error( | ||
'Expected `string` or `Array.<string>` for `tagNames`, not `' + | ||
tagNames + | ||
'`' | ||
) | ||
throw new Error('Expected positive finite index for child node') | ||
} | ||
if ( | ||
!node || | ||
typeof node !== 'object' || | ||
node.type !== 'element' || | ||
typeof node.tagName !== 'string' | ||
) { | ||
return false | ||
if (hasParent && (!parent.type || !parent.children)) { | ||
throw new Error('Expected parent node') | ||
} | ||
if (tagNames === null || tagNames === undefined) { | ||
return true | ||
if (!node || !node.type || typeof node.type !== 'string') { | ||
return false | ||
} | ||
name = node.tagName | ||
if (typeof tagNames === 'string') { | ||
return name === tagNames | ||
if (hasParent !== hasIndex) { | ||
throw new Error('Expected both parent and index') | ||
} | ||
return tagNames.indexOf(name) !== -1 | ||
return check.call(context, node, index, parent) | ||
} |
{ | ||
"name": "hast-util-is-element", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "hast utility to check if a node is a (certain) element", | ||
@@ -27,2 +27,3 @@ "license": "MIT", | ||
"files": [ | ||
"convert.js", | ||
"index.js" | ||
@@ -34,11 +35,11 @@ ], | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"tinyify": "^2.0.0", | ||
"xo": "^0.27.0" | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^8.0.0", | ||
"remark-preset-wooorm": "^7.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"xo": "^0.33.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"format": "remark . -qfo && prettier . --write && xo --fix", | ||
"build-bundle": "browserify . -s hastUtilIsElement > hast-util-is-element.js", | ||
@@ -63,3 +64,5 @@ "build-mangle": "browserify . -s hastUtilIsElement -p tinyify > hast-util-is-element.min.js", | ||
"rules": { | ||
"unicorn/prefer-includes": "off" | ||
"max-params": "off", | ||
"unicorn/prefer-includes": "off", | ||
"unicorn/prefer-reflect-apply": "off" | ||
}, | ||
@@ -66,0 +69,0 @@ "ignores": [ |
100
readme.md
@@ -36,27 +36,52 @@ # hast-util-is-element | ||
### `isElement(node[, tagName|tagNames])` | ||
### `isElement(node[, test[, index, parent[, context]]])` | ||
Check if the given value is a (certain) [*element*][element]. | ||
* When given a `tagName` or `tagNames`, checks that `node` is an | ||
[*element*][element] whose `tagName` field matches `tagName` or is included | ||
in `tagNames` | ||
* Otherwise checks that `node` is an [*element*][element] | ||
* `node` ([`Node`][node]) — Node to check. | ||
* `test` ([`Function`][test], `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`][test] | ||
* `index` (`number`, optional) — [Index][] of `node` in `parent` | ||
* `parent` ([`Node`][node], optional) — [Parent][] of `node` | ||
* `context` (`*`, optional) — Context object to invoke `test` with | ||
###### Returns | ||
`boolean` — Whether `test` passed *and* `node` is an [`Element`][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 | ||
* `node` (`*`) — Value to check, probably [`Node`][node] | ||
* `tagName` (`string`, optional) — Value that `node`s `tagName` field should | ||
match | ||
* `tagNames` (`Array.<string>`, optional) — Values that should include `node`s | ||
`tagName` field should match | ||
* `element` ([`Element`][element]) — Element to check | ||
* `index` (`number?`) — [Index][] of `node` in `parent` | ||
* `parent` ([`Node?`][node]) — [Parent][] of `node` | ||
###### Context | ||
`*` — The to `is` given `context`. | ||
###### Returns | ||
`boolean` — whether `node` passes the test. | ||
`boolean?` — Whether `element` matches. | ||
###### Throws | ||
### `isElement.convert(test)` | ||
`Error` — When the second parameter is given but invalid. | ||
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. | ||
Can also be accessed with `require('hast-util-is-element/convert')`. | ||
## Security | ||
@@ -67,2 +92,35 @@ | ||
## Related | ||
* [`hast-util-has-property`](https://github.com/syntax-tree/hast-util-has-property) | ||
— check if a node has a property | ||
* [`hast-util-is-body-ok-link`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-body-ok-link) | ||
— check if a node is “Body OK” link element | ||
* [`hast-util-is-conditional-comment`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-conditional-comment) | ||
— check if a node is a conditional comment | ||
* [`hast-util-is-css-link`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-css-link) | ||
— check if a node is a CSS link element | ||
* [`hast-util-is-css-style`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-css-style) | ||
— check if a node is a CSS style element | ||
* [`hast-util-embedded`](https://github.com/syntax-tree/hast-util-embedded) | ||
— check if a node is an embedded element | ||
* [`hast-util-heading`](https://github.com/syntax-tree/hast-util-heading) | ||
— check if a node is a heading element | ||
* [`hast-util-interactive`](https://github.com/syntax-tree/hast-util-interactive) | ||
— check if a node is interactive | ||
* [`hast-util-is-javascript`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-javascript) | ||
— check if a node is a JavaScript script element | ||
* [`hast-util-labelable`](https://github.com/syntax-tree/hast-util-labelable) | ||
— check whether a node is labelable | ||
* [`hast-util-phrasing`](https://github.com/syntax-tree/hast-util-phrasing) | ||
— check if a node is phrasing content | ||
* [`hast-util-script-supporting`](https://github.com/syntax-tree/hast-util-script-supporting) | ||
— check if a node is a script-supporting element | ||
* [`hast-util-sectioning`](https://github.com/syntax-tree/hast-util-sectioning) | ||
— check if a node is a sectioning element | ||
* [`hast-util-transparent`](https://github.com/syntax-tree/hast-util-transparent) | ||
— check if a node is a transparent element | ||
* [`hast-util-whitespace`](https://github.com/syntax-tree/hast-util-whitespace) | ||
— check if a node is inter-element whitespace | ||
## Contribute | ||
@@ -106,5 +164,5 @@ | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/syntax-tree | ||
[chat]: https://github.com/syntax-tree/unist/discussions | ||
@@ -117,7 +175,7 @@ [npm]: https://docs.npmjs.com/cli/install | ||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[support]: https://github.com/syntax-tree/.github/blob/master/support.md | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
@@ -130,2 +188,8 @@ [hast]: https://github.com/syntax-tree/hast | ||
[parent]: https://github.com/syntax-tree/unist#parent-1 | ||
[index]: https://github.com/syntax-tree/unist#index | ||
[test]: #function-testelement-index-parent | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12095
5
89
191