Skip to content

Commit

Permalink
Fix #77: Regression in selector parsing: Attribute selectors not pars…
Browse files Browse the repository at this point in the history
…ed correctly
  • Loading branch information
Jean-Philippe Zolesio committed Jan 11, 2023
1 parent 9568669 commit 79b873a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/css-tools",
"version": "4.0.1",
"version": "4.0.2",
"description": "CSS parser / stringifier",
"main": "dist/umd/cssTools.js",
"module": "dist/cjs/cssTools.js",
Expand Down
15 changes: 5 additions & 10 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,16 @@ export const parse = (
* div:matches(.toto, .titi:matches(.toto, .titi))
*
* Regex logic:
* ("|')(?:\\\1|.)*?,(?:\\\1|.)*?\1 => Handle the " and '
* \(.*?,.*?\) => Handle the ()
*
* Optimization 0:
* No greedy capture (see docs about the difference between .* and .*?)
* ("|')(?:\\\1|.)*?\1 => Handle the " and '
* \(.*?\) => Handle the ()
*
* Optimization 1:
* \(.*?,.*?\) instead of \(.*?\) to limit the number of replace (don't need to replace if , is not in the string)
* No greedy capture (see docs about the difference between .* and .*?)
*
* Optimization 2:
* ("|')(?:\\\1|.)*?,(?:\\\1|.)*?\1 this use reference to capture group, it work faster.
* ("|')(?:\\\1|.)*?\1 this use reference to capture group, it work faster.
*/
.replace(/("|')(?:\\\1|.)*?,(?:\\\1|.)*?\1|\(.*?,.*?\)/g, m =>
m.replace(/,/g, '\u200C')
)
.replace(/("|')(?:\\\1|.)*?\1|\(.*?\)/g, m => m.replace(/,/g, '\u200C'))
// Split the selector by ','
.split(',')
// Replace back \u200C by ','
Expand Down
43 changes: 43 additions & 0 deletions test/cases/selectors-attributes/ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "rule",
"selectors": [
"div[data-value='foo']",
"div[data-value='bar']"
],
"declarations": [
{
"type": "declaration",
"property": "color",
"value": "'black'",
"position": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 17
},
"source": "input.css"
}
}
],
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 3,
"column": 2
},
"source": "input.css"
}
}
]
}
}
1 change: 1 addition & 0 deletions test/cases/selectors-attributes/compressed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div[data-value='foo'],div[data-value='bar']{color:'black';}
3 changes: 3 additions & 0 deletions test/cases/selectors-attributes/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div[data-value='foo'],div[data-value='bar'] {
color: 'black';
}
4 changes: 4 additions & 0 deletions test/cases/selectors-attributes/output.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div[data-value='foo'],
div[data-value='bar'] {
color: 'black';
}

0 comments on commit 79b873a

Please sign in to comment.