Skip to content

Commit

Permalink
fix(parser): Support escaped characters in attribute value
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Oct 1, 2020
1 parent ff21060 commit a05da96
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 1 deletion.
215 changes: 215 additions & 0 deletions src/__fixtures__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,219 @@ export const tests: [string, Selector[][], string][] = [
],
"pseudo selector with data",
],

/*
* Bad attributes (taken from Sizzle)
* https://github.com/jquery/sizzle/blob/af163873d7cdfc57f18b16c04b1915209533f0b1/test/unit/selector.js#L602-L651
*/
[
"[id=types_all]",
[
[
{
type: "attribute",
action: "equals",
name: "id",
value: "types_all",
ignoreCase: false,
},
],
],
"Underscores don't need escaping",
],
[
"[name=foo\\ bar]",
[
[
{
type: "attribute",
action: "equals",
name: "name",
value: "foo bar",
ignoreCase: false,
},
],
],
"Escaped space",
],
[
"[name=foo\\.baz]",
[
[
{
type: "attribute",
action: "equals",
name: "name",
value: "foo.baz",
ignoreCase: false,
},
],
],
"Escaped dot",
],
[
"[name=foo\\[baz\\]]",
[
[
{
type: "attribute",
action: "equals",
name: "name",
value: "foo[baz]",
ignoreCase: false,
},
],
],
"Escaped brackets",
],
[
"[data-attr='foo_baz\\']']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "foo_baz']",
ignoreCase: false,
},
],
],
"Escaped quote + right bracket",
],
[
"[data-attr='\\'']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "'",
ignoreCase: false,
},
],
],
"Quoted quote",
],
[
"[data-attr='\\\\']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\\",
ignoreCase: false,
},
],
],
"Quoted backslash",
],
[
"[data-attr='\\\\\\'']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\\'",
ignoreCase: false,
},
],
],
"Quoted backslash quote",
],
[
"[data-attr='\\\\\\\\']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\\\\",
ignoreCase: false,
},
],
],
"Quoted backslash backslash",
],
[
"[data-attr='\\5C\\\\']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\\\\",
ignoreCase: false,
},
],
],
"Quoted backslash backslash (numeric escape)",
],
[
"[data-attr='\\5C \\\\']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\\\\",
ignoreCase: false,
},
],
],
"Quoted backslash backslash (numeric escape with trailing space)",
],
[
"[data-attr='\\5C\t\\\\']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\\\\",
ignoreCase: false,
},
],
],
"Quoted backslash backslash (numeric escape with trailing tab)",
],
[
"[data-attr='\\04e00']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\u4e00",
ignoreCase: false,
},
],
],
"Long numeric escape (BMP)",
],
[
"[data-attr='\\01D306A']",
[
[
{
type: "attribute",
action: "equals",
name: "data-attr",
value: "\uD834\uDF06A",
ignoreCase: false,
},
],
],
"Long numeric escape (non-BMP)",
],
];
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type TraversalType =
const reName = /^[^\\]?(?:\\(?:[\da-f]{1,6}\s?|.)|[\w\-\u00b0-\uFFFF])+/;
const reEscape = /\\([\da-f]{1,6}\s?|(\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 reAttr = /^\s*((?:\\.|[\w\u00b0-\uFFFF-])+)\s*(?:(\S?)=\s*(?:(['"])((?:[^\\]|\\[^])*?)\3|(#?(?:\\.|[\w\u00b0-\uFFFF-])*)|)|)\s*(i)?\]/;

const actionTypes: { [key: string]: AttributeAction } = {
undefined: "exists",
Expand Down
1 change: 1 addition & 0 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const charsToEscape = new Set([
"[",
"]",
" ",
"\\",
]);

export default function stringify(token: Selector[][]): string {
Expand Down

0 comments on commit a05da96

Please sign in to comment.