Skip to content

Commit

Permalink
Added support for short unicode escape sequences, fixes #2650
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Nov 2, 2015
1 parent 30da1b3 commit 2c87145
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/css-parse.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
if (node.parent) {
var ss = node.previous ? node.previous.end : node.parent.start;
t = text.substring(ss, node.start-1);
t = this._expandUnicodeEscapes(t);
// TODO(sorvell): ad hoc; make selector include only after last ;
// helps with mixin syntax
t = t.substring(t.lastIndexOf(';')+1);
Expand Down Expand Up @@ -89,6 +90,18 @@
return node;
},

// conversion of sort unicode escapes with spaces like `\33 ` (and longer) into
// expanded form that doesn't require trailing space `\000033`
_expandUnicodeEscapes : function(s) {
return s.replace(/\\([0-9a-f]{1,6})\s/gi, function() {
var code = arguments[1], repeat = 6 - code.length;
while (repeat--) {
code = '0' + code;
}
return '\\' + code;
});
},

// stringify parsed css.
stringify: function(node, preserveProperties, text) {
text = text || '';
Expand Down
32 changes: 32 additions & 0 deletions test/unit/css-parse.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@
}
/* comment */
</style>

<style id="short-escape-sequence">
.\33 d-model {
border-top: 3px solid red;
}
.\a33 d-model {
border-top: 3px solid red;
}
.\b333 d-model {
border-top: 3px solid red;
}
.\c3333 d-model {
border-top: 3px solid red;
}
.\d33333 d-model {
border-top: 3px solid red;
}
.\e33333d-model {
border-top: 3px solid red;
}
</style>
<script>

function sanitizeCss(text) {
Expand Down Expand Up @@ -119,6 +140,17 @@
assert.equal(result, '.stuff { background: red; }', 'unexpected stringified output');
});

test('short escape sequences', function() {
var s3 = document.querySelector('#short-escape-sequence');
var t = css.parse(s3.textContent);
assert.equal(t.rules[0].selector, '.\\000033d-model');
assert.equal(t.rules[1].selector, '.\\000a33d-model');
assert.equal(t.rules[2].selector, '.\\00b333d-model');
assert.equal(t.rules[3].selector, '.\\0c3333d-model');
assert.equal(t.rules[4].selector, '.\\d33333d-model');
assert.equal(t.rules[5].selector, '.\\e33333d-model');
});

});
</script>

Expand Down

0 comments on commit 2c87145

Please sign in to comment.