Skip to content

Commit

Permalink
Update constants
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Sep 24, 2023
1 parent 8365218 commit 0cd5e18
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/js/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export const CLASS_SELECTOR = 'ClassSelector';
export const COMBINATOR = 'Combinator';
export const ID_SELECTOR = 'IdSelector';
export const IDENTIFIER = 'Identifier';
export const NOT_SUPPORTED_ERR = 'NotSupportedError';
export const NTH = 'Nth';
export const PSEUDO_CLASS_SELECTOR = 'PseudoClassSelector';
export const PSEUDO_ELEMENT_SELECTOR = 'PseudoElementSelector';
export const RAW = 'Raw';
export const SELECTOR = 'Selector';
export const SELECTOR_LIST = 'SelectorList';
export const STRING = 'String';
export const SYNTAX_ERR = 'SyntaxError';
export const TYPE_SELECTOR = 'TypeSelector';
33 changes: 17 additions & 16 deletions src/js/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { generateCSS, parseSelector, walkAST } from './parser.js';
/* constants */
import {
ATTRIBUTE_SELECTOR, CLASS_SELECTOR, COMBINATOR, ID_SELECTOR,
PSEUDO_CLASS_SELECTOR, PSEUDO_ELEMENT_SELECTOR, TYPE_SELECTOR
NOT_SUPPORTED_ERR, PSEUDO_CLASS_SELECTOR, PSEUDO_ELEMENT_SELECTOR,
SYNTAX_ERR, TYPE_SELECTOR
} from './constant.js';
const BIT_ATTRIBUTE_SELECTOR = 16;
const BIT_CLASS_SELECTOR = 2;
Expand Down Expand Up @@ -182,14 +183,14 @@ export const parseASTName = (name, node) => {
[astPrefix, astNodeName] = name.split('|');
if (astPrefix && astPrefix !== '*' &&
node && !isNamespaceDeclared(astPrefix, node)) {
throw new DOMException(`invalid selector ${name}`, 'SyntaxError');
throw new DOMException(`invalid selector ${name}`, SYNTAX_ERR);
}
} else {
astPrefix = '*';
astNodeName = name;
}
} else {
throw new DOMException(`invalid selector ${name}`, 'SyntaxError');
throw new DOMException(`invalid selector ${name}`, SYNTAX_ERR);
}
return {
astNodeName,
Expand Down Expand Up @@ -265,7 +266,7 @@ export class Matcher {
* @returns {void}
*/
_onError(e) {
if (e instanceof DOMException && e.name === 'NotSupportedError') {
if (e instanceof DOMException && e.name === NOT_SUPPORTED_ERR) {
if (this.#warn) {
console.warn(e.message);
}
Expand Down Expand Up @@ -374,7 +375,7 @@ export class Matcher {
const [nextItem] = items;
if (nextItem.type === COMBINATOR) {
const msg = `invalid combinator, ${item.name}${nextItem.name}`;
throw new DOMException(msg, 'SyntaxError');
throw new DOMException(msg, SYNTAX_ERR);
}
branch.push({
combo: item,
Expand Down Expand Up @@ -661,11 +662,11 @@ export class Matcher {
(!inputType ||
/^(?:(?:emai|te|ur)l|search|text)$/.test(inputType))))) {
throw new DOMException('Unsupported pseudo-class :dir()',
'NotSupportedError');
NOT_SUPPORTED_ERR);
// FIXME:
} else if (nodeDir === 'auto' || (localName === 'bdi' && !nodeDir)) {
throw new DOMException('Unsupported pseudo-class :dir()',
'NotSupportedError');
NOT_SUPPORTED_ERR);
} else if (!nodeDir) {
let parent = node.parentNode;
while (parent) {
Expand Down Expand Up @@ -869,10 +870,10 @@ export class Matcher {
case 'nth-col':
case 'nth-last-col':
throw new DOMException(`Unsupported pseudo-class :${astName}()`,
'NotSupportedError');
NOT_SUPPORTED_ERR);
default:
throw new DOMException(`Unknown pseudo-class :${astName}()`,
'SyntaxError');
SYNTAX_ERR);
}
}
} else {
Expand Down Expand Up @@ -1178,7 +1179,7 @@ export class Matcher {
// FIXME:
if (isMultiple) {
throw new DOMException(`Unsupported pseudo-class :${astName}`,
'NotSupportedError');
NOT_SUPPORTED_ERR);
} else {
const firstOpt = parentNode.firstElementChild;
const defaultOpt = new Set();
Expand Down Expand Up @@ -1388,7 +1389,7 @@ export class Matcher {
case 'first-letter':
case 'first-line': {
throw new DOMException(`Unsupported pseudo-element ::${astName}`,
'NotSupportedError');
NOT_SUPPORTED_ERR);
}
case 'active':
case 'autofill':
Expand All @@ -1412,11 +1413,11 @@ export class Matcher {
case 'volume-locked':
case '-webkit-autofill': {
throw new DOMException(`Unsupported pseudo-class :${astName}`,
'NotSupportedError');
NOT_SUPPORTED_ERR);
}
default: {
throw new DOMException(`Unknown pseudo-class :${astName}`,
'SyntaxError');
SYNTAX_ERR);
}
}
}
Expand All @@ -1434,7 +1435,7 @@ export class Matcher {
flags: astFlags, matcher: astMatcher, name: astName, value: astValue
} = ast;
if (typeof astFlags === 'string' && !/^[is]$/i.test(astFlags)) {
throw new DOMException('invalid attribute selector', 'SyntaxError');
throw new DOMException('invalid attribute selector', SYNTAX_ERR);
}
const { attributes } = node;
let res;
Expand Down Expand Up @@ -1681,12 +1682,12 @@ export class Matcher {
case 'slotted':
case 'target-text': {
msg = `Unsupported pseudo-element ::${astName}`;
type = 'NotSupportedError';
type = NOT_SUPPORTED_ERR;
break;
}
default: {
msg = `Unknown pseudo-element ::${astName}`;
type = 'SyntaxError';
type = SYNTAX_ERR;
}
}
throw new DOMException(msg, type);
Expand Down
8 changes: 4 additions & 4 deletions src/js/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { findAll, parse, toPlainObject, walk } from 'css-tree';

/* constants */
import { PSEUDO_CLASS_SELECTOR, SELECTOR } from './constant.js';
import { PSEUDO_CLASS_SELECTOR, SELECTOR, SYNTAX_ERR } from './constant.js';
const CODE_POINT_UNIT = parseInt('10000', 16);
const HEX = 16;
const PAIR = 2;
Expand Down Expand Up @@ -54,7 +54,7 @@ export const preprocess = (...args) => {
selector = Object.prototype.toString.call(selector)
.slice(TYPE_FROM, TYPE_TO).toLowerCase();
} else {
throw new DOMException(`invalid selector ${selector}`, 'SyntaxError');
throw new DOMException(`invalid selector ${selector}`, SYNTAX_ERR);
}
return selector;
};
Expand All @@ -68,7 +68,7 @@ export const parseSelector = selector => {
selector = preprocess(selector);
// invalid selectors
if (selector === '' || /^\s*>/.test(selector) || /,\s*$/.test(selector)) {
throw new DOMException(`invalid selector ${selector}`, 'SyntaxError');
throw new DOMException(`invalid selector ${selector}`, SYNTAX_ERR);
}
let res;
try {
Expand All @@ -83,7 +83,7 @@ export const parseSelector = selector => {
} else if (e.message === '")" is expected' && !selector.endsWith(')')) {
res = parseSelector(`${selector})`);
} else {
throw new DOMException(e.message, 'SyntaxError');
throw new DOMException(e.message, SYNTAX_ERR);
}
}
return res;
Expand Down
8 changes: 4 additions & 4 deletions test/matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import * as matcherJs from '../src/js/matcher.js';
/* constants */
import {
AN_PLUS_B, ATTRIBUTE_SELECTOR, CLASS_SELECTOR, COMBINATOR, IDENTIFIER,
ID_SELECTOR, NTH, PSEUDO_CLASS_SELECTOR, PSEUDO_ELEMENT_SELECTOR, RAW,
SELECTOR, SELECTOR_LIST, STRING, TYPE_SELECTOR
ID_SELECTOR, NOT_SUPPORTED_ERR, NTH, PSEUDO_CLASS_SELECTOR,
PSEUDO_ELEMENT_SELECTOR, RAW, SELECTOR, SELECTOR_LIST, STRING, TYPE_SELECTOR
} from '../src/js/constant.js';

const globalKeys = ['DOMParser', 'NodeIterator'];
Expand Down Expand Up @@ -481,15 +481,15 @@ describe('match AST leaf and DOM node', () => {
});

it('should not throw', () => {
const e = new DOMException('error', 'NotSupportedError');
const e = new DOMException('error', NOT_SUPPORTED_ERR);
const matcher = new Matcher('*', document);
const res = matcher._onError(e);
assert.isUndefined(res, 'result');
});

it('should warn', () => {
const stubWarn = sinon.stub(console, 'warn');
const e = new DOMException('error', 'NotSupportedError');
const e = new DOMException('error', NOT_SUPPORTED_ERR);
const matcher = new Matcher('*', document, {
warn: true
});
Expand Down

0 comments on commit 0cd5e18

Please sign in to comment.