Skip to content

Commit

Permalink
refactor: Remove strict option
Browse files Browse the repository at this point in the history
The option only tested for CSS3 and there wasn't a good way to upgrade to newer CSS standards. I also doubt that this had much usage.
  • Loading branch information
fb55 committed Jan 15, 2021
1 parent c1e5d24 commit 9329fb8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 68 deletions.
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ the first match, or `null` if there was no match.
All options are optional.

- `xmlMode`: When enabled, tag names will be case-sensitive. Default: `false`.
- `strict`: Limits the module to only use CSS3 selectors. Default: `false`.
- `rootFunc`: The last function in the stack, will be called with the last
element that's looked at.
- `adapter`: The adapter to use when interacting with the backing DOM
Expand Down Expand Up @@ -156,9 +155,9 @@ _As defined by CSS 4 and / or jQuery._

- Universal (`*`)
- Tag (`<tagname>`)
- Descendant (``)
- Descendant (` `)
- Child (`>`)
- Parent (`<`) \*
- Parent (`<`)
- Sibling (`+`)
- Adjacent (`~`)
- Attribute (`[attr=foo]`), with supported comparisons:
Expand All @@ -169,33 +168,31 @@ _As defined by CSS 4 and / or jQuery._
- `*=`
- `^=`
- `$=`
- `!=` \*
- `!=`
- Also, `i` can be added after the comparison to make the comparison
case-insensitive (eg. `[attr=foo i]`) \*
case-insensitive (eg. `[attr=foo i]`)
- Pseudos:
- `:not`
- `:contains` \*
- `:icontains` \* (case-insensitive version of `:contains`)
- `:has` \*
- `:contains`
- `:icontains` (case-insensitive version of `:contains`)
- `:has`
- `:root`
- `:empty`
- `:parent` \*
- `:parent`
- `:[first|last]-child[-of-type]`
- `:only-of-type`, `:only-child`
- `:nth-[last-]child[-of-type]`
- `:link`, `:any-link`
- `:visited`, `:hover`, `:active` \* (these depend on optional Adapter
- `:visited`, `:hover`, `:active` (these depend on optional Adapter
methods, so these will work only if implemented in Adapter)
- `:selected` \*, `:checked`
- `:selected`, `:checked`
- `:enabled`, `:disabled`
- `:required`, `:optional`
- `:header`, `:button`, `:input`, `:text`, `:checkbox`, `:file`,
`:password`, `:reset`, `:radio` etc. \*
- `:matches`, `:is` \*
`:password`, `:reset`, `:radio` etc.
- `:matches`, `:is`
- `:scope` (uses the context from the passed options)

**\***: Not part of CSS3

---

License: BSD-2-Clause
Expand Down
11 changes: 0 additions & 11 deletions src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
throw new Error("Pseudo-elements are not supported by css-select");

case "attribute":
if (
options.strict &&
(selector.ignoreCase || selector.action === "not")
) {
throw new Error("Unsupported attribute selector");
}

return attributeRules[selector.action](next, selector, options);

case "pseudo":
Expand Down Expand Up @@ -98,10 +91,6 @@ export function compileGeneralSelector<Node, ElementNode extends Node>(
};

case "parent":
if (options.strict) {
throw new Error("Parent selector isn't part of CSS3");
}

return function parent(elem: ElementNode): boolean {
return adapter
.getChildren(elem)
Expand Down
7 changes: 0 additions & 7 deletions src/pseudo-selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import { subselects } from "./subselects";

export { filters, pseudos };

// FIXME This is pretty hacky
const reCSS3 = /^(?:(?:nth|last|first|only)-(?:child|of-type)|root|empty|(?:en|dis)abled|checked|not)$/;

export function compilePseudoSelector<Node, ElementNode extends Node>(
next: CompiledQuery<ElementNode>,
selector: PseudoSelector,
Expand All @@ -33,10 +30,6 @@ export function compilePseudoSelector<Node, ElementNode extends Node>(
): CompiledQuery<ElementNode> {
const { name, data } = selector;

if (options.strict && !reCSS3.test(name)) {
throw new Error(`:${name} isn't part of CSS3`);
}

if (Array.isArray(data)) {
return subselects[name](next, data, options, context, compileToken);
}
Expand Down
11 changes: 0 additions & 11 deletions src/pseudo-selectors/subselects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const subselects: Record<string, Subselect> = {
matches(next, token, options, context, compileToken) {
const opts = {
xmlMode: !!options.xmlMode,
strict: !!options.strict,
adapter: options.adapter,
equals: options.equals,
rootFunc: next,
Expand All @@ -64,19 +63,10 @@ export const subselects: Record<string, Subselect> = {
not(next, token, options, context, compileToken) {
const opts = {
xmlMode: !!options.xmlMode,
strict: !!options.strict,
adapter: options.adapter,
equals: options.equals,
};

if (opts.strict) {
if (token.length > 1 || token.some(containsTraversal)) {
throw new Error(
"complex selectors in :not aren't allowed in strict mode"
);
}
}

const func = compileToken(token, opts, context);

if (func === falseFunc) return next;
Expand All @@ -96,7 +86,6 @@ export const subselects: Record<string, Subselect> = {
const { adapter } = options;
const opts = {
xmlMode: !!options.xmlMode,
strict: !!options.strict,
adapter,
equals: options.equals,
};
Expand Down
6 changes: 0 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ export interface Options<Node, ElementNode extends Node> {
* @default false
*/
xmlMode?: boolean;
/**
* Limits the module to only use CSS3 selectors.
*
* @default false
*/
strict?: boolean;
/**
* The last function in the stack, will be called with the last element
* that's looked at.
Expand Down
18 changes: 0 additions & 18 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,6 @@ describe("API", () => {
).toBe(true);
});

it("should be strict", () => {
const opts = { strict: true };
expect(() => CSSselect.compile(":checkbox", opts)).toThrow(Error);
expect(() => CSSselect.compile("[attr=val i]", opts)).toThrow(
Error
);
expect(() => CSSselect.compile("[attr!=val]", opts)).toThrow(Error);
expect(() => CSSselect.compile("[attr!=val i]", opts)).toThrow(
Error
);
expect(() => CSSselect.compile("foo < bar", opts)).toThrow(Error);
expect(() => CSSselect.compile(":not(:parent)", opts)).toThrow(
Error
);
expect(() => CSSselect.compile(":not(a > b)", opts)).toThrow(Error);
expect(() => CSSselect.compile(":not(a, b)", opts)).toThrow(Error);
});

it("should recognize contexts", () => {
const div = CSSselect.selectAll("div", [dom]);
const p = CSSselect.selectAll("p", [dom]);
Expand Down

0 comments on commit 9329fb8

Please sign in to comment.