@asamuzakjp/dom-selector
Advanced tools
Comparing version 6.3.7 to 6.4.0
@@ -21,8 +21,18 @@ { | ||
"exports": { | ||
"import": "./src/index.js", | ||
"require": "./dist/cjs/index.js" | ||
"import": { | ||
"types": "./types/index.d.ts", | ||
"default": "./src/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/index.d.cts", | ||
"default": "./dist/cjs/index.cjs" | ||
}, | ||
"default": { | ||
"types": "./dist/cjs/types/index.d.cts", | ||
"default": "./dist/cjs/index.cjs" | ||
} | ||
}, | ||
"types": "types/index.d.ts", | ||
"dependencies": { | ||
"@asamuzakjp/nwsapi": "^2.2.27", | ||
"@asamuzakjp/nwsapi": "^2.3.0", | ||
"bidi-js": "^1.0.3", | ||
@@ -33,25 +43,26 @@ "css-tree": "^3.1.0", | ||
"devDependencies": { | ||
"@types/css-tree": "^2.3.9", | ||
"@types/css-tree": "^2.3.10", | ||
"benchmark": "^2.1.4", | ||
"c8": "^10.1.3", | ||
"chai": "^5.1.2", | ||
"commander": "^12.1.0", | ||
"esbuild": "^0.24.0", | ||
"eslint": "^9.17.0", | ||
"eslint-plugin-import-x": "^4.5.0", | ||
"commander": "^13.0.0", | ||
"esbuild": "^0.24.2", | ||
"eslint": "^9.18.0", | ||
"eslint-plugin-import-x": "^4.6.1", | ||
"eslint-plugin-jsdoc": "^50.6.1", | ||
"eslint-plugin-regexp": "^2.7.0", | ||
"eslint-plugin-unicorn": "^56.0.1", | ||
"globals": "^15.13.0", | ||
"happy-dom": "^15.11.7", | ||
"jsdom": "^25.0.1", | ||
"linkedom": "^0.18.5", | ||
"globals": "^15.14.0", | ||
"happy-dom": "^16.5.3", | ||
"jsdom": "^26.0.0", | ||
"linkedom": "^0.18.6", | ||
"mocha": "^11.0.1", | ||
"neostandard": "^0.12.0", | ||
"sinon": "^19.0.2", | ||
"typescript": "^5.7.2", | ||
"tsup": "^8.3.5", | ||
"typescript": "^5.7.3", | ||
"wpt-runner": "^6.0.0" | ||
}, | ||
"overrides": { | ||
"jsdom": "^25.0.1" | ||
"jsdom": "^26.0.0" | ||
}, | ||
@@ -61,6 +72,7 @@ "scripts": { | ||
"bench-sizzle": "node benchmark/bench-sizzle.js", | ||
"build": "npm run tsc && npm run lint && npm test && npm run bundle", | ||
"bundle": "esbuild --format=cjs --platform=node --outdir=dist/cjs/ --minify --sourcemap src/**/*.js", | ||
"build": "npm run tsc && npm run lint && npm test && npm run bundle && npm run test-cjs", | ||
"bundle": "tsup src/index.js --format=cjs --platform=node --outDir=dist/cjs/ --minify --sourcemap --dts", | ||
"lint": "eslint --fix .", | ||
"test": "c8 --reporter=text mocha --exit test/**/*.test.js", | ||
"test-cjs": "mocha --exit test/index.test.cjs", | ||
"test-wpt": "node test/wpt/wpt-runner.js", | ||
@@ -70,3 +82,3 @@ "tsc": "node scripts/index clean --dir=types -i && npx tsc", | ||
}, | ||
"version": "6.3.7" | ||
"version": "6.4.0" | ||
} |
@@ -6,2 +6,3 @@ /** | ||
/* string */ | ||
export const ATRULE = 'Atrule'; | ||
export const ATTR_SELECTOR = 'AttributeSelector'; | ||
@@ -17,3 +18,6 @@ export const CLASS_SELECTOR = 'ClassSelector'; | ||
export const PS_ELEMENT_SELECTOR = 'PseudoElementSelector'; | ||
export const RULE = 'Rule'; | ||
export const SCOPE = 'Scope'; | ||
export const SELECTOR = 'Selector'; | ||
export const SELECTOR_LIST = 'SelectorList'; | ||
export const STRING = 'String'; | ||
@@ -20,0 +24,0 @@ export const SYNTAX_ERR = 'SyntaxError'; |
@@ -120,3 +120,3 @@ /** | ||
} | ||
return selector; | ||
return selector.replace(/\x26/g, ':scope'); | ||
}; | ||
@@ -123,0 +123,0 @@ |
@@ -8,2 +8,3 @@ /** | ||
import bidiFactory from 'bidi-js'; | ||
import { generate, parse, walk } from 'css-tree'; | ||
import isCustomElementName from 'is-potential-custom-element-name'; | ||
@@ -13,6 +14,7 @@ | ||
import { | ||
DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINS, | ||
ATRULE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINS, | ||
DOCUMENT_POSITION_PRECEDING, ELEMENT_NODE, HAS_COMPOUND, KEY_INPUT_BUTTON, | ||
KEY_INPUT_EDIT, KEY_INPUT_TEXT, LOGIC_COMPLEX, LOGIC_COMPOUND, N_TH, | ||
PSEUDO_CLASS, TARGET_LINEAL, TARGET_SELF, TEXT_NODE, TYPE_FROM, TYPE_TO | ||
PSEUDO_CLASS, RULE, SCOPE, SELECTOR_LIST, TARGET_LINEAL, TARGET_SELF, | ||
TEXT_NODE, TYPE_FROM, TYPE_TO | ||
} from './constant.js'; | ||
@@ -36,2 +38,24 @@ const REG_LOGIC_COMPLEX = | ||
/** | ||
* verify array contents | ||
* @param {Array} arr - array | ||
* @param {string} type - expected type, e.g. 'String' | ||
* @throws | ||
* @returns {Array} - verified array | ||
*/ | ||
export const verifyArray = (arr, type) => { | ||
if (!Array.isArray(arr)) { | ||
throw new TypeError(`Unexpected type ${getType(arr)}`); | ||
} | ||
if (typeof type !== 'string') { | ||
throw new TypeError(`Unexpected type ${getType(type)}`); | ||
} | ||
for (const item of arr) { | ||
if (getType(item) !== type) { | ||
throw new TypeError(`Unexpected type ${getType(item)}`); | ||
} | ||
} | ||
return arr; | ||
}; | ||
/** | ||
* resolve content document, root node and tree walker, is in shadow | ||
@@ -647,2 +671,147 @@ * @param {object} node - Document, DocumentFragment, Element node | ||
/** | ||
* concat array of nested selectors into equivalent selector | ||
* @param {Array.<Array.<string>>} selectors - [parents, children, ...] | ||
* @returns {string} - selector | ||
*/ | ||
export const concatNestedSelectors = selectors => { | ||
if (!Array.isArray(selectors)) { | ||
throw new TypeError(`Unexpected type ${getType(selectors)}`); | ||
} | ||
let selector = ''; | ||
if (selectors.length) { | ||
selectors = selectors.reverse(); | ||
let child = verifyArray(selectors.shift(), 'String'); | ||
if (child.length === 1) { | ||
[child] = child; | ||
} | ||
while (selectors.length) { | ||
const parentArr = verifyArray(selectors.shift(), 'String'); | ||
if (!parentArr.length) { | ||
continue; | ||
} | ||
let parent; | ||
if (parentArr.length === 1) { | ||
[parent] = parentArr; | ||
if (!/^[>~+]/.test(parent) && /[\s>~+]/.test(parent)) { | ||
parent = `:is(${parent})`; | ||
} | ||
} else { | ||
parent = `:is(${parentArr.join(', ')})`; | ||
} | ||
if (selector.includes('\x26')) { | ||
selector = selector.replace(/\x26/g, parent); | ||
} | ||
if (Array.isArray(child)) { | ||
const items = []; | ||
for (let item of child) { | ||
if (item.includes('\x26')) { | ||
if (/^[>~+]/.test(item)) { | ||
item = `${parent} ${item.replace(/\x26/g, parent)} ${selector}`; | ||
} else { | ||
item = `${item.replace(/\x26/g, parent)} ${selector}`; | ||
} | ||
} else { | ||
item = `${parent} ${item} ${selector}`; | ||
} | ||
items.push(item.trim()); | ||
} | ||
selector = items.join(', '); | ||
} else if (selectors.length) { | ||
selector = `${child} ${selector}`; | ||
} else { | ||
if (child.includes('\x26')) { | ||
if (/^[>~+]/.test(child)) { | ||
selector = | ||
`${parent} ${child.replace(/\x26/g, parent)} ${selector}`; | ||
} else { | ||
selector = `${child.replace(/\x26/g, parent)} ${selector}`; | ||
} | ||
} else { | ||
selector = `${parent} ${child} ${selector}`; | ||
} | ||
} | ||
selector = selector.trim(); | ||
if (selectors.length) { | ||
child = parentArr.length > 1 ? parentArr : parent; | ||
} else { | ||
break; | ||
} | ||
} | ||
selector = selector.replace(/\x26/g, ':scope').trim(); | ||
} | ||
return selector; | ||
}; | ||
/** | ||
* extract nested selectors from CSSRule.cssText | ||
* @param {string} css - CSSRule.cssText | ||
* @returns {Array.<Array.<string>>} - array of nested selectors | ||
*/ | ||
export const extractNestedSelectors = css => { | ||
const ast = parse(css, { | ||
context: 'rule' | ||
}); | ||
const selectors = []; | ||
let isScoped = false; | ||
walk(ast, { | ||
enter: node => { | ||
switch (node.type) { | ||
case ATRULE: { | ||
if (node.name === 'scope') { | ||
isScoped = true; | ||
} | ||
break; | ||
} | ||
case SCOPE: { | ||
const { children, type } = node.root; | ||
const arr = []; | ||
if (type === SELECTOR_LIST) { | ||
for (const child of children) { | ||
const selector = generate(child); | ||
arr.push(selector); | ||
} | ||
selectors.push(arr); | ||
} | ||
break; | ||
} | ||
case RULE: { | ||
const { children, type } = node.prelude; | ||
const arr = []; | ||
if (type === SELECTOR_LIST) { | ||
let hasAmp = false; | ||
for (const child of children) { | ||
const selector = generate(child); | ||
if (isScoped && !hasAmp) { | ||
hasAmp = /\x26/.test(selector); | ||
} | ||
arr.push(selector); | ||
} | ||
if (isScoped) { | ||
if (hasAmp) { | ||
selectors.push(arr); | ||
/* FIXME: | ||
} else { | ||
selectors = arr; | ||
isScoped = false; | ||
*/ | ||
} | ||
} else { | ||
selectors.push(arr); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
leave: node => { | ||
if (node.type === ATRULE) { | ||
if (node.name === 'scope') { | ||
isScoped = false; | ||
} | ||
} | ||
} | ||
}); | ||
return selectors; | ||
}; | ||
/** | ||
* init nwsapi | ||
@@ -649,0 +818,0 @@ * @param {object} window - Window |
@@ -0,1 +1,2 @@ | ||
export const ATRULE: "Atrule"; | ||
export const ATTR_SELECTOR: "AttributeSelector"; | ||
@@ -11,3 +12,6 @@ export const CLASS_SELECTOR: "ClassSelector"; | ||
export const PS_ELEMENT_SELECTOR: "PseudoElementSelector"; | ||
export const RULE: "Rule"; | ||
export const SCOPE: "Scope"; | ||
export const SELECTOR: "Selector"; | ||
export const SELECTOR_LIST: "SelectorList"; | ||
export const STRING: "String"; | ||
@@ -14,0 +18,0 @@ export const SYNTAX_ERR: "SyntaxError"; |
export function getType(o: any): string; | ||
export function verifyArray(arr: any[], type: string): any[]; | ||
export function resolveContent(node: object): Array<object | boolean>; | ||
@@ -16,3 +17,5 @@ export function traverseNode(node: object, walker: object, force?: boolean): object | null; | ||
export function sortNodes(nodes?: Array<object> | Set<object>): Array<object | undefined>; | ||
export function concatNestedSelectors(selectors: Array<Array<string>>): string; | ||
export function extractNestedSelectors(css: string): Array<Array<string>>; | ||
export function initNwsapi(window: object, document: object): object; | ||
export function filterSelector(selector: string, opt?: object): boolean; |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
5059
1
435832
21
18
Updated@asamuzakjp/nwsapi@^2.3.0