Skip to content

Commit d4dda8c

Browse files
committed
Escape class name values by default.
1 parent ff45e25 commit d4dda8c

10 files changed

Lines changed: 130 additions & 18 deletions

File tree

‎postcss-selector-parser.d.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ declare namespace parser {
162162
next(): Node;
163163
prev(): Node;
164164
clone(opts: {[override: string]:any}): Node;
165+
/**
166+
* Some non-standard syntax doesn't follow normal escaping rules for css,
167+
* this allows the escaped value to be specified directly, allowing illegal characters to be
168+
* directly inserted into css output.
169+
* @param name the property to set
170+
* @param value the unescaped value of the property
171+
* @param valueEscaped optional. the escaped value of the property.
172+
*/
173+
setPropertyAndEscape(name: string, value: any, valueEscaped: string);
174+
/**
175+
* When you want a value to passed through to CSS directly. This method
176+
* deletes the corresponding raw value causing the stringifier to fallback
177+
* to the unescaped value.
178+
* @param name the property to set.
179+
* @param value The value that is both escaped and unescaped.
180+
*/
181+
setPropertyWithoutEscape(name: string, value: any);
182+
/**
183+
* Some non-standard syntax doesn't follow normal escaping rules for css.
184+
* This allows non standard syntax to be appended to an existing property
185+
* by specifying the escaped value. By specifying the escaped value,
186+
* illegal characters are allowed to be directly inserted into css output.
187+
* @param {string} name the property to set
188+
* @param {any} value the unescaped value of the property
189+
* @param {string} valueEscaped optional. the escaped value of the property.
190+
*/
191+
appendToPropertyAndEscape(name, value, valueEscaped);
165192
toString(): string;
166193
}
167194
interface ContainerOptions extends NodeOptions {

‎src/__tests__/classes.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ test('qualified class', 'button.btn-primary', (t, tree) => {
1818

1919
test('escaped numbers in class name', '.\\31\\ 0', (t, tree) => {
2020
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
21-
t.deepEqual(tree.nodes[0].nodes[0].value, '\\31\\ 0');
21+
t.deepEqual(tree.nodes[0].nodes[0].value, '1 0');
22+
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\31\\ 0');
2223
});
2324

2425
test('extraneous non-combinating whitespace', ' .h1 , .h2 ', (t, tree) => {

‎src/__tests__/escapes.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {test} from './util/helpers';
22

33
test('escaped semicolon', '.\\;', (t, tree) => {
4-
t.deepEqual(tree.nodes[0].nodes[0].value, '\\;');
4+
t.deepEqual(tree.nodes[0].nodes[0].value, ';');
5+
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\;');
56
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
67
});
78

‎src/__tests__/nonstandard.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {test} from './util/helpers';
22

33
test('non-standard selector', '.icon.is-$(network)', (t, tree) => {
4-
t.deepEqual(tree.nodes[0].nodes[0].value, 'icon');
5-
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
6-
t.deepEqual(tree.nodes[0].nodes[1].value, 'is-$(network)');
7-
t.deepEqual(tree.nodes[0].nodes[1].type, 'class');
4+
let class1 = tree.nodes[0].nodes[0];
5+
t.deepEqual(class1.value, 'icon');
6+
t.deepEqual(class1.type, 'class');
7+
let class2 = tree.nodes[0].nodes[1];
8+
t.deepEqual(class2.value, 'is-$(network)');
9+
t.deepEqual(class2.type, 'class');
810
});
911

1012
test('at word in selector', '[email protected]', (t, tree) => {

‎src/parser.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,16 @@ export default class Parser {
458458
}
459459
this.current = cache;
460460
} else {
461-
last.value += '(';
461+
last.appendToPropertyAndEscape("value", '(', '(');
462462
while (this.position < this.tokens.length && balanced) {
463463
if (this.currToken[0] === tokens.openParenthesis) {
464464
balanced ++;
465465
}
466466
if (this.currToken[0] === tokens.closeParenthesis) {
467467
balanced --;
468468
}
469-
last.value += this.parseParenthesisToken(this.currToken);
469+
let additionalValue = this.parseParenthesisToken(this.currToken);
470+
last.appendToPropertyAndEscape("value", additionalValue, additionalValue);
470471
this.position ++;
471472
}
472473
}
@@ -613,11 +614,17 @@ export default class Parser {
613614
current[2] + (index - 1)
614615
);
615616
if (~hasClass.indexOf(ind)) {
616-
node = new ClassName({
617-
value: value.slice(1),
617+
let v = value.slice(1);
618+
let u = unesc(v);
619+
let classNameOpts = {
620+
value: u,
618621
source,
619622
sourceIndex,
620-
});
623+
};
624+
if (v !== u) {
625+
classNameOpts.raws = {value: v};
626+
}
627+
node = new ClassName(classNameOpts);
621628
} else if (~hasId.indexOf(ind)) {
622629
node = new ID({
623630
value: value.slice(1),

‎src/selectors/attribute.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,6 @@ export default class Attribute extends Namespace {
300300
let rawSpaces = (this.raws.spaces && this.raws.spaces[name]) || {};
301301
return Object.assign(attrSpaces, spaces, rawSpaces);
302302
}
303-
_valueFor (name) {
304-
return this.raws[name] || this[name];
305-
}
306303

307304
_stringFor (name, spaceName = name, concat = defaultAttrConcat) {
308305
let attrSpaces = this._spacesFor(spaceName);

‎src/selectors/className.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1+
import cssesc from "cssesc";
2+
import {ensureObject} from '../util';
13
import Node from './node';
24
import {CLASS} from './types';
35

46
export default class ClassName extends Node {
57
constructor (opts) {
68
super(opts);
79
this.type = CLASS;
10+
this._constructed = true;
11+
}
12+
13+
set value (v) {
14+
if (this._constructed) {
15+
let escaped = cssesc(v, {isIdentifier: true});
16+
if (escaped !== v) {
17+
ensureObject(this, "raws");
18+
this.raws.value = escaped;
19+
} else if (this.raws) {
20+
delete this.raws.value;
21+
}
22+
}
23+
this._value = v;
24+
}
25+
26+
get value () {
27+
return this._value;
828
}
929

1030
toString () {
1131
return [
1232
this.spaces.before,
13-
String('.' + this.value),
33+
String('.' + this._valueFor("value")),
1434
this.spaces.after,
1535
].join('');
1636
}

‎src/selectors/namespace.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class Namespace extends Node {
2222

2323
get namespaceString () {
2424
if (this.namespace) {
25-
let ns = this.raws && this.raws.namespace || this.namespace;
25+
let ns = this._valueFor("namespace");
2626
if (ns === true) {
2727
return '';
2828
} else {

‎src/selectors/node.js‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,67 @@ export default class Node {
6868
return cloned;
6969
}
7070

71+
/**
72+
* Some non-standard syntax doesn't follow normal escaping rules for css.
73+
* This allows non standard syntax to be appended to an existing property
74+
* by specifying the escaped value. By specifying the escaped value,
75+
* illegal characters are allowed to be directly inserted into css output.
76+
* @param {string} name the property to set
77+
* @param {any} value the unescaped value of the property
78+
* @param {string} valueEscaped optional. the escaped value of the property.
79+
*/
80+
appendToPropertyAndEscape (name, value, valueEscaped) {
81+
if (!this.raws) {
82+
this.raws = {};
83+
}
84+
let originalValue = this[name];
85+
let originalEscaped = this.raws[name];
86+
this[name] = originalValue + value; // this may trigger a setter that updates raws, so it has to be set first.
87+
if (originalEscaped || valueEscaped !== value) {
88+
this.raws[name] = (originalEscaped || originalValue) + valueEscaped;
89+
} else {
90+
delete this.raws[name]; // delete any escaped value that was created by the setter.
91+
}
92+
}
93+
94+
/**
95+
* Some non-standard syntax doesn't follow normal escaping rules for css.
96+
* This allows the escaped value to be specified directly, allowing illegal
97+
* characters to be directly inserted into css output.
98+
* @param {string} name the property to set
99+
* @param {any} value the unescaped value of the property
100+
* @param {string} valueEscaped optional. the escaped value of the property.
101+
*/
102+
setPropertyAndEscape (name, value, valueEscaped) {
103+
if (!this.raws) {
104+
this.raws = {};
105+
}
106+
this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
107+
this.raws[name] = valueEscaped;
108+
}
109+
110+
/**
111+
* When you want a value to passed through to CSS directly. This method
112+
* deletes the corresponding raw value causing the stringifier to fallback
113+
* to the unescaped value.
114+
* @param {string} name the property to set.
115+
* @param {any} value The value that is both escaped and unescaped.
116+
*/
117+
setPropertyWithoutEscape (name, value) {
118+
this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
119+
if (this.raws) {
120+
delete this.raws[name];
121+
}
122+
}
123+
124+
_valueFor (name) {
125+
return (this.raws && this.raws[name]) || this[name];
126+
}
127+
71128
toString () {
72129
return [
73130
this.spaces.before,
74-
String(this.value),
131+
String(this._valueFor("value")),
75132
this.spaces.after,
76133
].join('');
77134
}

‎src/util/unesc.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const HEX_ESC = /\\(?:([0-9a-fA-F]{6})|([0-9a-fA-F]{1,5}) )/g;
1+
const HEX_ESC = /\\(?:([0-9a-fA-F]{6})|([0-9a-fA-F]{1,5})(?: |(?![0-9a-fA-F])))/g;
22
const OTHER_ESC = /\\(.)/g;
33
export default function unesc (str) {
44
str = str.replace(HEX_ESC, (_, hex1, hex2) => {

0 commit comments

Comments
 (0)