@asamuzakjp/dom-selector
Advanced tools
Comparing version 0.10.0 to 0.11.1
@@ -45,3 +45,3 @@ { | ||
}, | ||
"version": "0.10.0" | ||
"version": "0.11.1" | ||
} |
@@ -15,2 +15,3 @@ /** | ||
PSEUDO_CLASS_SELECTOR: 'PseudoClassSelector', | ||
PSEUDO_ELEMENT_SELECTOR: 'PseudoElementSelector', | ||
RAW: 'Raw', | ||
@@ -17,0 +18,0 @@ SELECTOR: 'Selector', |
@@ -14,3 +14,3 @@ /** | ||
ATTRIBUTE_SELECTOR, CLASS_SELECTOR, COMBINATOR, IDENTIFIER, ID_SELECTOR, | ||
NTH, PSEUDO_CLASS_SELECTOR, TYPE_SELECTOR | ||
NTH, PSEUDO_CLASS_SELECTOR, PSEUDO_ELEMENT_SELECTOR, TYPE_SELECTOR | ||
} = require('./constant.js'); | ||
@@ -894,2 +894,8 @@ const ELEMENT_NODE = 1; | ||
} | ||
// legacy pseudo-elements | ||
case 'after': | ||
case 'before': | ||
case 'first-letter': | ||
case 'first-line': | ||
break; | ||
case 'active': | ||
@@ -935,2 +941,38 @@ case 'autofill': | ||
/** | ||
* match pseudo-element selector | ||
* NOTE: throws DOMException | ||
* @param {object} ast - AST | ||
* @param {object} node - Element node | ||
* @returns {void} | ||
*/ | ||
const matchPseudoElementSelector = (ast = {}, node = {}) => { | ||
const { type: astType } = ast; | ||
const { nodeType } = node; | ||
if (astType === PSEUDO_ELEMENT_SELECTOR && nodeType === ELEMENT_NODE) { | ||
const astName = unescapeSelector(ast.name); | ||
switch (astName) { | ||
case 'after': | ||
case 'backdrop': | ||
case 'before': | ||
case 'cue': | ||
case 'cue-region': | ||
case 'first-letter': | ||
case 'first-line': | ||
case 'file-selector-button': | ||
case 'marker': | ||
case 'part': | ||
case 'placeholder': | ||
case 'selection': | ||
case 'slotted': | ||
case 'target-text': | ||
throw new DOMException(`Unsupported pseudo-element ${astName}`, | ||
'NotSupportedError'); | ||
default: | ||
throw new DOMException(`Unknown pseudo-element ${astName}`, | ||
'SyntaxError'); | ||
} | ||
} | ||
}; | ||
/** | ||
* Matcher | ||
@@ -963,2 +1005,22 @@ */ | ||
/** | ||
* is attached | ||
* @returns {boolean} - result | ||
*/ | ||
_isAttached() { | ||
const root = this.#document?.documentElement; | ||
let bool; | ||
if (root) { | ||
let node = this.#node; | ||
while (node) { | ||
if (node === root) { | ||
bool = true; | ||
break; | ||
} | ||
node = node.parentNode; | ||
} | ||
} | ||
return !!bool; | ||
}; | ||
/** | ||
* create iterator | ||
@@ -1300,11 +1362,19 @@ * @param {object} ast - AST | ||
} else if (itemType === COMBINATOR) { | ||
let [nextItem] = items; | ||
let nextItemName = unescapeSelector(nextItem.name); | ||
if (nextItem.type === COMBINATOR) { | ||
const comboName = `${itemName}${nextItemName}`; | ||
throw new DOMException(`Unknown combinator ${comboName}`, | ||
'SyntaxError'); | ||
} | ||
const leaves = []; | ||
leaves.push(item); | ||
while (items.length) { | ||
const [nextItem] = items; | ||
[nextItem] = items; | ||
nextItemName = unescapeSelector(nextItem.name); | ||
if (nextItem.type === COMBINATOR || | ||
(nextItem.type === PSEUDO_CLASS_SELECTOR && | ||
PSEUDO_NTH.test(unescapeSelector(nextItem.name))) || | ||
PSEUDO_NTH.test(nextItemName)) || | ||
(nextItem.type === PSEUDO_CLASS_SELECTOR && | ||
PSEUDO_FUNC.test(unescapeSelector(nextItem.name)))) { | ||
PSEUDO_FUNC.test(nextItemName))) { | ||
break; | ||
@@ -1396,2 +1466,5 @@ } else { | ||
break; | ||
case PSEUDO_ELEMENT_SELECTOR: | ||
matchPseudoElementSelector(ast, node); | ||
break; | ||
default: { | ||
@@ -1414,3 +1487,9 @@ const arr = this._parseAST(ast, node); | ||
try { | ||
const arr = this._match(this.#ast, this.#document); | ||
let node; | ||
if (this._isAttached()) { | ||
node = this.#document; | ||
} else { | ||
node = this.#node; | ||
} | ||
const arr = this._match(this.#ast, node); | ||
res = arr.length && arr.includes(this.#node); | ||
@@ -1524,4 +1603,5 @@ } catch (e) { | ||
matchPseudoClassSelector, | ||
matchPseudoElementSelector, | ||
matchTypeSelector, | ||
unescapeSelector | ||
}; |
@@ -9,2 +9,3 @@ export const AN_PLUS_B: string; | ||
export const PSEUDO_CLASS_SELECTOR: string; | ||
export const PSEUDO_ELEMENT_SELECTOR: string; | ||
export const RAW: string; | ||
@@ -11,0 +12,0 @@ export const SELECTOR: string; |
@@ -5,2 +5,3 @@ export class Matcher { | ||
}); | ||
_isAttached(): boolean; | ||
_createIterator(ast?: object, root?: object): object; | ||
@@ -37,3 +38,4 @@ _parseAST(ast: object, node: object): Array<object | undefined>; | ||
export function matchPseudoClassSelector(ast?: object, node?: object, refPoint?: object): Array<object | undefined>; | ||
export function matchPseudoElementSelector(ast?: object, node?: object): void; | ||
export function matchTypeSelector(ast?: object, node?: object): object | null; | ||
export function unescapeSelector(selector?: string): string | null; |
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
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
62994
1859