hast-util-parse-selector
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -1,20 +0,1 @@ | ||
/** | ||
* Create a hast element from a simple CSS selector. | ||
* | ||
* @param selector A simple CSS selector. | ||
* Can contain a tag-name (`foo`), classes (`.bar`), and an ID (`#baz`). | ||
* Multiple classes are allowed. | ||
* Uses the last ID if multiple IDs are found. | ||
* @param [defaultTagName='div'] Tag name to use if `selector` does not specify one. | ||
*/ | ||
export const parseSelector: < | ||
Selector extends string, | ||
DefaultTagName extends string = 'div' | ||
>( | ||
selector?: Selector, | ||
defaultTagName?: DefaultTagName | ||
) => import('hast').Element & { | ||
tagName: import('./extract.js').ExtractTagName<Selector, DefaultTagName> | ||
} | ||
export type Properties = import('hast').Properties | ||
export type Element = import('hast').Element | ||
export {parseSelector} from './lib/index.js' |
74
index.js
@@ -1,73 +0,1 @@ | ||
/** | ||
* @typedef {import('hast').Properties} Properties | ||
* @typedef {import('hast').Element} Element | ||
*/ | ||
var search = /[#.]/g | ||
/** | ||
* Create a hast element from a simple CSS selector. | ||
* | ||
* @param selector A simple CSS selector. | ||
* Can contain a tag-name (`foo`), classes (`.bar`), and an ID (`#baz`). | ||
* Multiple classes are allowed. | ||
* Uses the last ID if multiple IDs are found. | ||
* @param [defaultTagName='div'] Tag name to use if `selector` does not specify one. | ||
*/ | ||
export const parseSelector = | ||
/** | ||
* @type {( | ||
* <Selector extends string, DefaultTagName extends string = 'div'>(selector?: Selector, defaultTagName?: DefaultTagName) => Element & {tagName: import('./extract.js').ExtractTagName<Selector, DefaultTagName>} | ||
* )} | ||
*/ | ||
( | ||
/** | ||
* @param {string} [selector] | ||
* @param {string} [defaultTagName='div'] | ||
* @returns {Element} | ||
*/ | ||
function (selector, defaultTagName = 'div') { | ||
var value = selector || '' | ||
/** @type {Properties} */ | ||
var props = {} | ||
var start = 0 | ||
/** @type {string} */ | ||
var subvalue | ||
/** @type {string} */ | ||
var previous | ||
/** @type {RegExpMatchArray} */ | ||
var match | ||
while (start < value.length) { | ||
search.lastIndex = start | ||
match = search.exec(value) | ||
subvalue = value.slice(start, match ? match.index : value.length) | ||
if (subvalue) { | ||
if (!previous) { | ||
defaultTagName = subvalue | ||
} else if (previous === '#') { | ||
props.id = subvalue | ||
} else if (Array.isArray(props.className)) { | ||
props.className.push(subvalue) | ||
} else { | ||
props.className = [subvalue] | ||
} | ||
start += subvalue.length | ||
} | ||
if (match) { | ||
previous = match[0] | ||
start++ | ||
} | ||
} | ||
return { | ||
type: 'element', | ||
tagName: defaultTagName, | ||
properties: props, | ||
children: [] | ||
} | ||
} | ||
) | ||
export {parseSelector} from './lib/index.js' |
{ | ||
"name": "hast-util-parse-selector", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "hast utility to create an element from a simple CSS selector", | ||
@@ -33,4 +33,4 @@ "license": "MIT", | ||
"<=4.1": { | ||
"extract.d.ts": [ | ||
"extract-legacy.d.ts" | ||
"lib/extract.d.ts": [ | ||
"lib/extract-legacy.d.ts" | ||
] | ||
@@ -40,4 +40,3 @@ } | ||
"files": [ | ||
"extract-legacy.d.ts", | ||
"extract.d.ts", | ||
"lib/", | ||
"index.d.ts", | ||
@@ -50,20 +49,18 @@ "index.js" | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tsd": "^0.14.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"tsd": "^0.25.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.40.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && tsd && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -80,7 +77,3 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -87,0 +80,0 @@ "remarkConfig": { |
115
readme.md
@@ -11,12 +11,34 @@ # hast-util-parse-selector | ||
[**hast**][hast] utility to create an [*element*][element] from a simple CSS | ||
selector. | ||
[hast][] utility to create an element from a simple CSS selector. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`parseSelector(selector?[, defaultTagName])`](#parseselectorselector-defaulttagname) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a tiny utility that helps create elements. | ||
## When should I use this? | ||
This utility is super niche. | ||
You probably want the more powerful [`hastscript`][hastscript] or | ||
[`hast-util-from-selector`][hast-util-from-selector] | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -26,2 +48,16 @@ npm install hast-util-parse-selector | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {parseSelector} from 'https://esm.sh/hast-util-parse-selector@3' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {parseSelector} from 'https://esm.sh/hast-util-parse-selector@3?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -46,25 +82,37 @@ | ||
This package exports the following identifiers: `parseSelector`. | ||
This package exports the identifier [`parseSelector`][parseselector]. | ||
There is no default export. | ||
### `parseSelector([selector][, defaultTagName])` | ||
### `parseSelector(selector?[, defaultTagName])` | ||
Create an [*element*][element] [*node*][node] from a simple CSS selector. | ||
Create a hast element from a simple CSS selector. | ||
###### `selector` | ||
###### Parameters | ||
`string`, optional — Can contain a tag name (`foo`), classes (`.bar`), | ||
and an ID (`#baz`). | ||
Multiple classes are allowed. | ||
Uses the last ID if multiple IDs are found. | ||
* `selector` (`string`, optional) | ||
— simple CSS selector, can contain a tag name (`foo`), classes (`.bar`), | ||
and an ID (`#baz`), multiple classes are allowed, uses the last ID if | ||
multiple IDs are found | ||
* `defaultTagName` (`string`, default: `'div'`) | ||
— tag name to use if `selector` does not specify one | ||
###### `defaultTagName` | ||
###### Returns | ||
`string`, optional, defaults to `div` — Tag name to use if `selector` does not | ||
specify one. | ||
Built element ([`Element`][element]). | ||
###### Returns | ||
## Types | ||
[`Element`][element]. | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
In TypeScript 4.2+, the type system can infer the tag name of literal | ||
`selector`s and knows that the return element has that name. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Security | ||
@@ -76,3 +124,4 @@ | ||
Do not use user input in `selector` or use [`hast-util-santize`][sanitize]. | ||
Do not use user input in `selector` or use | ||
[`hast-util-santize`][hast-util-sanitize]. | ||
@@ -86,4 +135,4 @@ ## Related | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -129,2 +178,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -134,12 +189,18 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[health]: https://github.com/syntax-tree/.github | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[hast]: https://github.com/syntax-tree/hast | ||
[node]: https://github.com/syntax-tree/hast#nodes | ||
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize | ||
[hastscript]: https://github.com/syntax-tree/hastscript | ||
[hast-util-from-selector]: https://github.com/syntax-tree/hast-util-from-selector | ||
[element]: https://github.com/syntax-tree/hast#element | ||
@@ -149,2 +210,2 @@ | ||
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize | ||
[parseselector]: #parseselectorselector-defaulttagname |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
12797
9
10
125
204
1