Skip to content

Commit

Permalink
Support names starting with arbitrary characters
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
fb55 committed May 31, 2020
1 parent 3817077 commit 03d0fe8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/__fixtures__/tests.ts
Expand Up @@ -353,6 +353,23 @@ export const tests: [string, Selector[][], string][] = [
"attribute with previously normalized characters",
],

// ID starting with a dot
[
"#.identifier",
[
[
{
type: "attribute",
action: "equals",
name: "id",
value: ".identifier",
ignoreCase: false,
},
],
],
"ID starting with a dot",
],

//pseudo selectors
[
":foo",
Expand Down
20 changes: 10 additions & 10 deletions src/parse.ts
Expand Up @@ -67,10 +67,10 @@ export type TraversalType =
| "parent"
| "sibling";

const reName = /^(?:\\([\da-f]{1,6}\s?|(\s)|.)|[\w\-\u00b0-\uFFFF])+/,
reEscape = /\\([\da-f]{1,6}\s?|(\s)|.)/gi,
//modified version of https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L87
reAttr = /^\s*((?:\\.|[\w\u00b0-\uFFFF-])+)\s*(?:(\S?)=\s*(?:(['"])([^]*?)\3|(#?(?:\\.|[\w\u00b0-\uFFFF-])*)|)|)\s*(i)?\]/;
const reName = /^[^\\]?(?:\\(?:[\da-f]{1,6}\s?|.)|[\w\-\u00b0-\uFFFF])+/;
const reEscape = /\\([\da-f]{1,6}\s?|.)/gi;
//modified version of https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L87
const reAttr = /^\s*((?:\\.|[\w\u00b0-\uFFFF-])+)\s*(?:(\S?)=\s*(?:(['"])([^]*?)\3|(#?(?:\\.|[\w\u00b0-\uFFFF-])*)|)|)\s*(i)?\]/;

const actionTypes: { [key: string]: AttributeAction } = {
undefined: "exists",
Expand Down Expand Up @@ -127,7 +127,7 @@ function isWhitespace(c: string) {
function parse(selector: string, options?: Options): Selector[][] {
const subselects: Selector[][] = [];

selector = parseSelector(subselects, selector + "", options);
selector = parseSelector(subselects, `${selector}`, options);

if (selector !== "") {
throw new Error(`Unmatched selector: ${selector}`);
Expand Down Expand Up @@ -232,7 +232,7 @@ function parseSelector(

tokens.push({
type: "attribute",
name: name,
name,
action: actionTypes[data[2]],
value: unescapeCSS(data[4] || data[5] || ""),
ignoreCase: !!data[6],
Expand Down Expand Up @@ -278,8 +278,8 @@ function parseSelector(

selector = selector.substr(1);
} else {
let pos = 1,
counter = 1;
let pos = 1;
let counter = 1;

for (; counter > 0 && pos < selector.length; pos++) {
if (selector.charAt(pos) === "(" && !isEscaped(pos))
Expand Down Expand Up @@ -310,7 +310,7 @@ function parseSelector(
}
}

tokens.push({ type: "pseudo", name: name, data: data });
tokens.push({ type: "pseudo", name, data });
} else if (reName.test(selector)) {
let name = getName();

Expand All @@ -323,7 +323,7 @@ function parseSelector(
name = name.toLowerCase();
}

tokens.push({ type: "tag", name: name });
tokens.push({ type: "tag", name });
} else {
if (
tokens.length &&
Expand Down

0 comments on commit 03d0fe8

Please sign in to comment.