Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@markuplint/selector

Package Overview
Dependencies
Maintainers
1
Versions
99
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@markuplint/selector - npm Package Compare versions

Comparing version 3.0.0-dev.96 to 3.0.0-dev.176

test/create-selector.spec.js

11

lib/extended-selector/aria-pseudo-class.js

@@ -9,3 +9,5 @@ "use strict";

function ariaPseudoClass() {
return (content) => (el) => {
return (content) => (
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) => {
const aria = ariaPseudoClassParser(content);

@@ -48,4 +50,7 @@ const name = (0, ml_spec_1.getAccname)(el);

const [_query, _version] = syntax.split('|');
const query = _query.replace(/\s+/g, '').toLowerCase();
const version = _version === '1.1' ? '1.1' : '1.2';
const query = _query === null || _query === void 0 ? void 0 : _query.replace(/\s+/g, '').toLowerCase();
const version = _version !== null && _version !== void 0 ? _version : ml_spec_1.ARIA_RECOMMENDED_VERSION;
if (!(0, ml_spec_1.validateAriaVersion)(version)) {
throw new SyntaxError(`Unsupported ARIA version: ${version}`);
}
switch (query) {

@@ -52,0 +57,0 @@ case 'hasname': {

@@ -6,6 +6,8 @@ "use strict";

function ariaRolePseudoClass(specs) {
return (content) => (el) => {
return (content) => (
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) => {
var _a, _b;
const aria = ariaPseudoClassParser(content);
const computed = (0, ml_spec_1.getComputedRole)(specs, el, (_a = aria.version) !== null && _a !== void 0 ? _a : '1.2');
const computed = (0, ml_spec_1.getComputedRole)(specs, el, (_a = aria.version) !== null && _a !== void 0 ? _a : ml_spec_1.ARIA_RECOMMENDED_VERSION);
if (((_b = computed.role) === null || _b === void 0 ? void 0 : _b.name) === aria.role) {

@@ -27,8 +29,12 @@ return {

function ariaPseudoClassParser(syntax) {
var _a;
const [roleName, _version] = syntax.split('|');
const version = _version === '1.1' ? '1.1' : '1.2';
const version = _version !== null && _version !== void 0 ? _version : ml_spec_1.ARIA_RECOMMENDED_VERSION;
if (!(0, ml_spec_1.validateAriaVersion)(version)) {
throw new SyntaxError(`Unsupported ARIA version: ${version}`);
}
return {
role: roleName.trim().toLowerCase(),
role: (_a = roleName === null || roleName === void 0 ? void 0 : roleName.trim().toLowerCase()) !== null && _a !== void 0 ? _a : syntax.trim().toLowerCase(),
version,
};
}

@@ -7,3 +7,5 @@ "use strict";

function contentModelPseudoClass(specs) {
return (category) => (el) => {
return (category) => (
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) => {
category = category.trim().toLowerCase();

@@ -44,3 +46,3 @@ const selectors = (0, ml_spec_1.contentModelCategoryToTagNames)(`#${category}`, specs.def);

.filter((m) => m.matched);
if (matched.length) {
if (matched.length > 0) {
return {

@@ -47,0 +49,0 @@ specificity: [0, 1, 0],

export declare class InvalidSelectorError extends Error {
name: string;
name: string;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPureHTMLElement = exports.isNonDocumentTypeChildNode = exports.isElement = void 0;
function isElement(node) {
function isElement(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
node) {
return node.nodeType === node.ELEMENT_NODE;
}
exports.isElement = isElement;
function isNonDocumentTypeChildNode(node) {
function isNonDocumentTypeChildNode(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
node) {
return 'previousElementSibling' in node && 'nextElementSibling' in node;
}
exports.isNonDocumentTypeChildNode = isNonDocumentTypeChildNode;
function isPureHTMLElement(el) {
function isPureHTMLElement(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) {
return el.localName !== el.nodeName;
}
exports.isPureHTMLElement = isPureHTMLElement;
import type { Specificity, RegexSelector } from './types';
export type SelectorMatches = SelectorMatched | SelectorUnmatched;
type SelectorMatched = {
matched: true;
selector: string;
specificity: Specificity;
data?: Record<string, string>;
readonly matched: true;
readonly selector: string;
readonly specificity: Specificity;
readonly data?: Readonly<Record<string, string>>;
};
type SelectorUnmatched = {
matched: false;
readonly matched: false;
};
export declare function matchSelector(el: Node, selector: string | RegexSelector | undefined): SelectorMatches;
export {};

@@ -9,4 +9,6 @@ "use strict";

const selector_1 = require("./selector");
function matchSelector(el, selector) {
if (!selector) {
function matchSelector(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el, selector) {
if (selector == null || selector === '') {
return {

@@ -19,3 +21,3 @@ matched: false,

const specificity = sel.match(el);
if (specificity) {
if (specificity !== false) {
return {

@@ -34,3 +36,5 @@ matched: true,

exports.matchSelector = matchSelector;
function regexSelect(el, selector) {
function regexSelect(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el, selector) {
let edge = new SelectorTarget(selector);

@@ -55,3 +59,5 @@ let edgeSelector = selector.combination;

}
match(el) {
match(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) {
const unitCheck = this._matchWithoutCombineChecking(el);

@@ -146,3 +152,5 @@ if (!unitCheck.matched) {

}
_matchWithoutCombineChecking(el) {
_matchWithoutCombineChecking(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) {
return uncombinedRegexSelect(el, tslib_1.__classPrivateFieldGet(this, _SelectorTarget_selector, "f"));

@@ -152,3 +160,5 @@ }

_SelectorTarget_combinedFrom = new WeakMap(), _SelectorTarget_selector = new WeakMap();
function uncombinedRegexSelect(el, selector) {
function uncombinedRegexSelect(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el, selector) {
if (!(0, is_1.isElement)(el)) {

@@ -155,0 +165,0 @@ return {

@@ -1,3 +0,7 @@

export declare function regexSelectorMatches(reg: string, raw: string, ignoreCase: boolean): {
[x: string]: string;
export declare function regexSelectorMatches(
reg: string,
raw: string,
ignoreCase: boolean,
): {
[x: string]: string;
} | null;

@@ -21,3 +21,3 @@ "use strict";

const matched = pattern.match(/^\/(.+)\/([ig]*)$/i);
if (matched) {
if (matched && matched[1]) {
return new RegExp(matched[1], matched[2]);

@@ -24,0 +24,0 @@ }

import type { SelectorResult, Specificity } from './types';
type ExtendedPseudoClass = Record<string, (content: string) => (el: Element) => SelectorResult>;
type ExtendedPseudoClass = Readonly<Record<string, (content: string) => (el: Element) => SelectorResult>>;
export declare class Selector {
#private;
constructor(selector: string, extended?: ExtendedPseudoClass);
match(el: Node, scope?: ParentNode | null): Specificity | false;
search(el: Node, scope?: ParentNode | null): SelectorResult[];
#private;
constructor(selector: string, extended?: ExtendedPseudoClass);
match(el: Node, scope?: ParentNode | null): Specificity | false;
search(el: Node, scope?: ParentNode | null): SelectorResult[];
}
export {};

@@ -19,3 +19,8 @@ "use strict";

}
match(el, scope = (0, is_1.isElement)(el) ? el : null) {
match(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope) {
scope = (0, is_1.isElement)(el) ? el : null;
const results = this.search(el, scope);

@@ -29,3 +34,8 @@ for (const result of results) {

}
search(el, scope = (0, is_1.isElement)(el) ? el : null) {
search(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope) {
scope = (0, is_1.isElement)(el) ? el : null;
return tslib_1.__classPrivateFieldGet(this, _Selector_ruleset, "f").match(el, scope);

@@ -53,13 +63,18 @@ }

constructor(selectors, extended, depth) {
var _a, _b;
_Ruleset_selectorGroup.set(this, []);
tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f").push(...selectors.map(selector => new StructuredSelector(selector, depth, extended)));
const head = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0];
this.headCombinator = (head === null || head === void 0 ? void 0 : head.headCombinator) || null;
this.headCombinator = (_a = head === null || head === void 0 ? void 0 : head.headCombinator) !== null && _a !== void 0 ? _a : null;
if (this.headCombinator) {
if (depth <= 0) {
throw new invalid_selector_error_1.InvalidSelectorError(`'${tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0].selector}' is not a valid selector`);
throw new invalid_selector_error_1.InvalidSelectorError(`'${(_b = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0]) === null || _b === void 0 ? void 0 : _b.selector}' is not a valid selector`);
}
}
}
match(el, scope) {
match(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope) {
(0, debug_1.log)('<%s> (%s)', (0, is_1.isElement)(el) ? el.localName : el.nodeName, scope ? ((0, is_1.isElement)(scope) ? scope.localName : scope.nodeName) : null);

@@ -77,2 +92,3 @@ return tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f").map(selector => {

constructor(selector, depth, extended) {
var _a, _b;
_StructuredSelector_edge.set(this, void 0);

@@ -83,7 +99,8 @@ _StructuredSelector_selector.set(this, void 0);

this.headCombinator =
tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0].type === 'combinator' ? tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0].value || null : null;
((_a = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0]) === null || _a === void 0 ? void 0 : _a.type) === 'combinator' ? (_b = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0].value) !== null && _b !== void 0 ? _b : null : null;
const nodes = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.slice();
if (0 < depth && this.headCombinator) {
tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.unshift((0, postcss_selector_parser_1.pseudo)({ value: ':scope' }));
nodes.unshift((0, postcss_selector_parser_1.pseudo)({ value: ':scope' }));
}
tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.forEach(node => {
nodes.forEach(node => {
switch (node.type) {

@@ -115,3 +132,7 @@ case 'combinator': {

}
match(el, scope) {
match(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope) {
return tslib_1.__classPrivateFieldGet(this, _StructuredSelector_edge, "f").match(el, scope, 0);

@@ -163,12 +184,16 @@ }

}
match(el, scope, count) {
var _a;
match(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope, count) {
var _a, _b, _c;
const result = this._match(el, scope, count);
if (selLog.enabled) {
const nodeName = el.nodeName;
const selector = ((_a = tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) === null || _a === void 0 ? void 0 : _a.target.toString()) || this.toString();
const selector = (_b = (_a = tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) === null || _a === void 0 ? void 0 : _a.target.toString()) !== null && _b !== void 0 ? _b : this.toString();
const combinator = result.combinator ? ` ${result.combinator}` : '';
selLog('The %s element by "%s" => %s (%d)', nodeName, `${selector}${combinator}`, result.matched, count);
if (selector === ':scope') {
selLog(`† Scope is the ${(scope === null || scope === void 0 ? void 0 : scope.nodeName) || null}`);
selLog(`† Scope is the ${(_c = scope === null || scope === void 0 ? void 0 : scope.nodeName) !== null && _c !== void 0 ? _c : null}`);
}

@@ -189,3 +214,7 @@ }

}
_match(el, scope, count) {
_match(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope, count) {
const unitCheck = this._matchWithoutCombineChecking(el, scope);

@@ -238,3 +267,3 @@ if (!unitCheck.matched) {

}
if (matchedNodes.length) {
if (matchedNodes.length > 0) {
return {

@@ -260,3 +289,3 @@ combinator: '␣',

const not = [];
const specificity = unitCheck.specificity;
const specificity = [...unitCheck.specificity];
const parentNode = el.parentElement;

@@ -284,3 +313,3 @@ if (parentNode) {

}
if (matchedNodes.length) {
if (matchedNodes.length > 0) {
return {

@@ -306,3 +335,3 @@ combinator: '>',

const not = [];
const specificity = unitCheck.specificity;
const specificity = [...unitCheck.specificity];
if (el.previousElementSibling) {

@@ -329,3 +358,3 @@ const res = target.match(el.previousElementSibling, scope, count + 1);

}
if (matchedNodes.length) {
if (matchedNodes.length > 0) {
return {

@@ -381,3 +410,3 @@ combinator: '+',

}
if (matchedNodes.length) {
if (matchedNodes.length > 0) {
return {

@@ -407,3 +436,7 @@ combinator: '~',

}
_matchWithoutCombineChecking(el, scope) {
_matchWithoutCombineChecking(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope) {
var _a;

@@ -499,3 +532,5 @@ const specificity = [0, 0, 0];

_SelectorTarget_combinedFrom = new WeakMap(), _SelectorTarget_extended = new WeakMap(), _SelectorTarget_isAdded = new WeakMap();
function attrMatch(attr, el) {
function attrMatch(attr,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) {
return Array.from(el.attributes).some(attrOfEl => {

@@ -538,3 +573,3 @@ if (attr.attribute !== attrOfEl.localName) {

case '*=': {
if (valueOfEl.indexOf(value) === -1) {
if (!valueOfEl.includes(value)) {
return false;

@@ -561,3 +596,7 @@ }

}
function pseudoMatch(pseudo, el, scope, extended, depth) {
function pseudoMatch(pseudo,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope, extended, depth) {
switch (pseudo.value) {

@@ -574,3 +613,3 @@ //

const matched = ruleset.match(parent, scope).filter((r) => r.matched);
if (matched.length) {
if (matched.length > 0) {
return {

@@ -619,3 +658,3 @@ specificity,

specificity,
matched: !!matched.length,
matched: matched.length > 0,
nodes: matched.map(m => m.nodes).flat(),

@@ -634,3 +673,3 @@ has: matched.map(m => m.has).flat(),

.flat();
if (has.length) {
if (has.length > 0) {
return {

@@ -652,3 +691,3 @@ specificity,

.flat();
if (has.length) {
if (has.length > 0) {
return {

@@ -674,3 +713,3 @@ specificity,

specificity: [0, 0, 0],
matched: !!matched.length,
matched: matched.length > 0,
nodes: matched.map(m => m.nodes).flat(),

@@ -764,2 +803,5 @@ has: matched.map(m => m.has).flat(),

const hook = extended[ext];
if (!hook) {
continue;
}
const matcher = hook(content);

@@ -772,6 +814,13 @@ return matcher(el);

}
function isScope(el, scope) {
return el === scope || el.parentNode === null;
function isScope(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
scope) {
var _a;
return (_a = el === scope) !== null && _a !== void 0 ? _a : el.parentNode === null;
}
function getDescendants(el, includeSelf = false) {
function getDescendants(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el, includeSelf = false) {
return [

@@ -784,7 +833,11 @@ ...Array.from(el.children)

}
function getSiblings(el) {
var _a;
return Array.from(((_a = el.parentElement) === null || _a === void 0 ? void 0 : _a.children) || []);
function getSiblings(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
el) {
var _a, _b;
return Array.from((_b = (_a = el.parentElement) === null || _a === void 0 ? void 0 : _a.children) !== null && _b !== void 0 ? _b : []);
}
function getSpecificity(results) {
function getSpecificity(
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
results) {
let specificity;

@@ -791,0 +844,0 @@ for (const result of results) {

@@ -1,24 +0,24 @@

export type Specificity = [number, number, number];
export type Specificity = readonly [number, number, number];
export type SelectorResult = SelectorMatchedResult | SelectorUnmatchedResult;
export type SelectorMatchedResult = {
specificity: Specificity;
matched: true;
nodes: (Element | Text)[];
has: SelectorMatchedResult[];
readonly specificity: Specificity;
readonly matched: true;
readonly nodes: readonly (Element | Text)[];
readonly has: readonly SelectorMatchedResult[];
};
export type SelectorUnmatchedResult = {
specificity: Specificity;
matched: false;
not?: SelectorMatchedResult[];
readonly specificity: Specificity;
readonly matched: false;
readonly not?: readonly SelectorMatchedResult[];
};
export type RegexSelector = RegexSelectorWithoutCombination & {
combination?: {
combinator: RegexSelectorCombinator;
} & RegexSelector;
readonly combination?: {
readonly combinator: RegexSelectorCombinator;
} & RegexSelector;
};
export type RegexSelectorCombinator = ' ' | '>' | '+' | '~' | ':has(+)' | ':has(~)';
export type RegexSelectorWithoutCombination = {
nodeName?: string;
attrName?: string;
attrValue?: string;
readonly nodeName?: string;
readonly attrName?: string;
readonly attrValue?: string;
};
{
"name": "@markuplint/selector",
"version": "3.0.0-dev.96+3b9f1720",
"version": "3.0.0-dev.176+f6ad62e9",
"description": "Extended W3C Selectors matcher",

@@ -23,13 +23,14 @@ "repository": "git@github.com:markuplint/markuplint.git",

"dependencies": {
"@markuplint/ml-spec": "3.0.0-dev.176+f6ad62e9",
"@types/debug": "^4.1.7",
"debug": "^4.3.4",
"postcss-selector-parser": "^6.0.11",
"tslib": "^2.4.1"
"tslib": "^2.4.1",
"type-fest": "^3.8.0"
},
"devDependencies": {
"@markuplint/ml-spec": "3.0.0-dev.96+3b9f1720",
"@types/debug": "^4.1.7",
"@types/jsdom": "16",
"jsdom": "19"
"@types/jsdom": "21.1.1",
"jsdom": "21.1.1"
},
"gitHead": "3b9f17205d7754b29edf790bdbbf5e4931ba27a2"
"gitHead": "f6ad62e992e1569be4067f1e90d2d6017a658f57"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc