css-select
Advanced tools
Comparing version 5.0.1 to 5.1.0
@@ -68,3 +68,3 @@ "use strict"; | ||
/* | ||
* CSS 4 Spec (Draft): 3.3.1. Absolutizing a Scope-relative Selector | ||
* CSS 4 Spec (Draft): 3.4.1. Absolutizing a Relative Selector | ||
* http://www.w3.org/TR/selectors4/#absolutizing | ||
@@ -101,3 +101,9 @@ */ | ||
var finalContext = context && (Array.isArray(context) ? context : [context]); | ||
absolutize(token, options, finalContext); | ||
// Check if the selector is relative | ||
if (options.relativeSelector !== false) { | ||
absolutize(token, options, finalContext); | ||
} | ||
else if (token.some(function (t) { return t.length > 0 && (0, sort_js_1.isTraversal)(t[0]); })) { | ||
throw new Error("Relative selectors are not allowed when the `relativeSelector` option is disabled"); | ||
} | ||
var shouldTestNextSiblings = false; | ||
@@ -104,0 +110,0 @@ var query = token |
@@ -37,3 +37,3 @@ import { parse, SelectorType } from "css-what"; | ||
/* | ||
* CSS 4 Spec (Draft): 3.3.1. Absolutizing a Scope-relative Selector | ||
* CSS 4 Spec (Draft): 3.4.1. Absolutizing a Relative Selector | ||
* http://www.w3.org/TR/selectors4/#absolutizing | ||
@@ -68,3 +68,9 @@ */ | ||
const finalContext = context && (Array.isArray(context) ? context : [context]); | ||
absolutize(token, options, finalContext); | ||
// Check if the selector is relative | ||
if (options.relativeSelector !== false) { | ||
absolutize(token, options, finalContext); | ||
} | ||
else if (token.some((t) => t.length > 0 && isTraversal(t[0]))) { | ||
throw new Error("Relative selectors are not allowed when the `relativeSelector` option is disabled"); | ||
} | ||
let shouldTestNextSiblings = false; | ||
@@ -71,0 +77,0 @@ const query = token |
@@ -62,14 +62,14 @@ import boolbase from "boolbase"; | ||
const { adapter } = options; | ||
// @ts-expect-error Uses an array as a pointer to the current element (side effects) | ||
const opts = copyOptions(options); | ||
opts.relativeSelector = true; | ||
const context = subselect.some((s) => s.some(isTraversal)) | ||
? [PLACEHOLDER_ELEMENT] | ||
? // Used as a placeholder. Will be replaced with the actual element. | ||
[PLACEHOLDER_ELEMENT] | ||
: undefined; | ||
const compiled = compileToken(subselect, copyOptions(options), context); | ||
const compiled = compileToken(subselect, opts, context); | ||
if (compiled === boolbase.falseFunc) | ||
return boolbase.falseFunc; | ||
if (compiled === boolbase.trueFunc) { | ||
return (elem) => adapter.getChildren(elem).some(adapter.isTag) && next(elem); | ||
} | ||
const hasElement = ensureIsTag(compiled, adapter); | ||
if (context) { | ||
// If `compiled` is `trueFunc`, we can skip this. | ||
if (context && compiled !== boolbase.trueFunc) { | ||
/* | ||
@@ -76,0 +76,0 @@ * `shouldTestNextSiblings` will only be true if the query starts with |
@@ -131,6 +131,20 @@ import type { Selector } from "css-what"; | ||
* The context of the current query. Used to limit the scope of searches. | ||
* Can be matched directly using the `:scope` pseudo-selector. | ||
* Can be matched directly using the `:scope` pseudo-class. | ||
*/ | ||
context?: Node | Node[]; | ||
/** | ||
* Indicates whether to consider the selector as a relative selector. | ||
* | ||
* Relative selectors that don't include a `:scope` pseudo-class behave | ||
* as if they have a `:scope ` prefix (a `:scope` pseudo-class, followed by | ||
* a descendant selector). | ||
* | ||
* If relative selectors are disabled, selectors starting with a traversal | ||
* will lead to an error. | ||
* | ||
* @default true | ||
* @see {@link https://www.w3.org/TR/selectors-4/#relative} | ||
*/ | ||
relativeSelector?: boolean; | ||
/** | ||
* Allow css-select to cache results for some selectors, sometimes greatly | ||
@@ -137,0 +151,0 @@ * improving querying performance. Disable this if your document can |
@@ -79,18 +79,14 @@ "use strict"; | ||
var adapter = options.adapter; | ||
// @ts-expect-error Uses an array as a pointer to the current element (side effects) | ||
var context = subselect.some(function (s) { | ||
return s.some(sort_js_1.isTraversal); | ||
}) | ||
? [exports.PLACEHOLDER_ELEMENT] | ||
var opts = copyOptions(options); | ||
opts.relativeSelector = true; | ||
var context = subselect.some(function (s) { return s.some(sort_js_1.isTraversal); }) | ||
? // Used as a placeholder. Will be replaced with the actual element. | ||
[exports.PLACEHOLDER_ELEMENT] | ||
: undefined; | ||
var compiled = compileToken(subselect, copyOptions(options), context); | ||
var compiled = compileToken(subselect, opts, context); | ||
if (compiled === boolbase_1.default.falseFunc) | ||
return boolbase_1.default.falseFunc; | ||
if (compiled === boolbase_1.default.trueFunc) { | ||
return function (elem) { | ||
return adapter.getChildren(elem).some(adapter.isTag) && next(elem); | ||
}; | ||
} | ||
var hasElement = ensureIsTag(compiled, adapter); | ||
if (context) { | ||
// If `compiled` is `trueFunc`, we can skip this. | ||
if (context && compiled !== boolbase_1.default.trueFunc) { | ||
/* | ||
@@ -97,0 +93,0 @@ * `shouldTestNextSiblings` will only be true if the query starts with |
@@ -131,6 +131,20 @@ import type { Selector } from "css-what"; | ||
* The context of the current query. Used to limit the scope of searches. | ||
* Can be matched directly using the `:scope` pseudo-selector. | ||
* Can be matched directly using the `:scope` pseudo-class. | ||
*/ | ||
context?: Node | Node[]; | ||
/** | ||
* Indicates whether to consider the selector as a relative selector. | ||
* | ||
* Relative selectors that don't include a `:scope` pseudo-class behave | ||
* as if they have a `:scope ` prefix (a `:scope` pseudo-class, followed by | ||
* a descendant selector). | ||
* | ||
* If relative selectors are disabled, selectors starting with a traversal | ||
* will lead to an error. | ||
* | ||
* @default true | ||
* @see {@link https://www.w3.org/TR/selectors-4/#relative} | ||
*/ | ||
relativeSelector?: boolean; | ||
/** | ||
* Allow css-select to cache results for some selectors, sometimes greatly | ||
@@ -137,0 +151,0 @@ * improving querying performance. Disable this if your document can |
{ | ||
"name": "css-select", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"description": "a CSS selector compiler/engine", | ||
@@ -38,7 +38,7 @@ "author": "Felix Boehm <me@feedic.com>", | ||
"@types/jest": "^27.4.1", | ||
"@types/node": "^17.0.25", | ||
"@typescript-eslint/eslint-plugin": "^5.20.0", | ||
"@typescript-eslint/parser": "^5.20.0", | ||
"@types/node": "^17.0.29", | ||
"@typescript-eslint/eslint-plugin": "^5.21.0", | ||
"@typescript-eslint/parser": "^5.21.0", | ||
"cheerio-soupselect": "^0.1.1", | ||
"eslint": "^8.13.0", | ||
"eslint": "^8.14.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
@@ -45,0 +45,0 @@ "htmlparser2": "^8.0.0", |
@@ -25,4 +25,6 @@ # css-select [![NPM version](http://img.shields.io/npm/v/css-select.svg)](https://npmjs.org/package/css-select) [![Build Status](https://travis-ci.com/fb55/css-select.svg?branch=master)](http://travis-ci.com/fb55/css-select) [![Downloads](https://img.shields.io/npm/dm/css-select.svg)](https://npmjs.org/package/css-select) [![Coverage](https://coveralls.io/repos/fb55/css-select/badge.svg?branch=master)](https://coveralls.io/r/fb55/css-select) | ||
remaining selectors) | ||
- ๐งโ๐ฌ High test coverage, including the full test suites from Sizzle, Qwery and | ||
NWMatcher. | ||
- ๐งโ๐ฌ High test coverage, including the full test suites from | ||
[`Sizzle`](https://github.com/jquery/sizzle), | ||
[`Qwery`](https://github.com/ded/qwery) and | ||
[`NWMatcher`](https://github.com/dperini/nwmatcher/) and . | ||
- ๐ฅผ Reliably great performance | ||
@@ -33,8 +35,8 @@ | ||
Most CSS engines written in JavaScript execute selectors left-to-right. That | ||
means thet execute every component of the selector in order, from left to right | ||
_(duh)_. As an example: For the selector `a b`, these engines will first query | ||
for `a` elements, then search these for `b` elements. (That's the approach of | ||
eg. [`Sizzle`](https://github.com/jquery/sizzle), | ||
[`nwmatcher`](https://github.com/dperini/nwmatcher/) and | ||
[`qwery`](https://github.com/ded/qwery).) | ||
means thet execute every component of the selector in order, from left to right. | ||
As an example: For the selector `a b`, these engines will first query for `a` | ||
elements, then search these for `b` elements. (That's the approach of eg. | ||
[`Sizzle`](https://github.com/jquery/sizzle), | ||
[`Qwery`](https://github.com/ded/qwery) and | ||
[`NWMatcher`](https://github.com/dperini/nwmatcher/).) | ||
@@ -105,3 +107,3 @@ While this works, it has some downsides: Children of `a`s will be checked | ||
**Note:** css-select throws errors when invalid selectors are passed to it.This | ||
**Note:** css-select throws errors when invalid selectors are passed to it. This | ||
is done to aid with writing css selectors, but can be unexpected when processing | ||
@@ -145,3 +147,9 @@ arbitrary strings. | ||
- `context`: The context of the current query. Used to limit the scope of | ||
searches. Can be matched directly using the `:scope` pseudo-selector. | ||
searches. Can be matched directly using the `:scope` pseudo-class. | ||
- `relativeSelector`: By default, selectors are relative to the `context`, | ||
which means that no parent elements of the context will be matched. (Eg. | ||
`a b c` with context `b` will never give any results.) If `relativeSelector` | ||
is set to `false`, selectors won't be | ||
[absolutized](http://www.w3.org/TR/selectors4/#absolutizing) and selectors | ||
can test for parent elements outside of the `context`. | ||
- `cacheResults`: Allow css-select to cache results for some selectors, | ||
@@ -151,3 +159,3 @@ sometimes greatly improving querying performance. Disable this if your | ||
Default: `true`. | ||
- `pseudos`: A map of pseudo-selectors to functions or strings. | ||
- `pseudos`: A map of pseudo-class names to functions or strings. | ||
@@ -154,0 +162,0 @@ #### Custom Adapters |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
224084
2814
265