From d582d1ee82b5d736637849a38a8f5288b75b3f76 Mon Sep 17 00:00:00 2001 From: Nikita Vasilyev Date: Wed, 18 Jul 2012 13:16:56 +0400 Subject: [PATCH] Allow `\'` and `\"` in strings. #39 --- lib/parse.js | 22 ++++++++++++++-------- spec/parse.spec.js | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index dd6945e..39a46b9 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -83,10 +83,13 @@ CSSOM.parse = function parse(token) { // String case '"': - index = token.indexOf('"', i + 1) + 1; - if (!index) { - parseError('Unmatched "'); - } + index = i + 1; + do { + index = token.indexOf('"', index) + 1; + if (!index) { + parseError('Unmatched "'); + } + } while (token[index - 2] === '\\') buffer += token.slice(i, index); i = index - 1; switch (state) { @@ -100,10 +103,13 @@ CSSOM.parse = function parse(token) { break; case "'": - index = token.indexOf("'", i + 1) + 1; - if (!index) { - parseError("Unmatched '"); - } + index = i + 1; + do { + index = token.indexOf("'", index) + 1; + if (!index) { + parseError("Unmatched '"); + } + } while (token[index - 2] === '\\') buffer += token.slice(i, index); i = index - 1; switch (state) { diff --git a/spec/parse.spec.js b/spec/parse.spec.js index 01d1b4f..7d415e6 100644 --- a/spec/parse.spec.js +++ b/spec/parse.spec.js @@ -956,9 +956,30 @@ describe('parse', function() { }); }); + given('a{content:"\\""}', function(input) { + var parsed = CSSOM.parse(input); + expect(parsed.cssRules[0].style.content).toBe('"\\""'); + }); + + given("a{content:'\\''}", function(input) { + var parsed = CSSOM.parse(input); + expect(parsed.cssRules[0].style.content).toBe("'\\''"); + }); + + given('a{content:"abc\\"\\"d\\"ef"}', function(input) { + var parsed = CSSOM.parse(input); + expect(parsed.cssRules[0].style.content).toBe('"abc\\"\\"d\\"ef"'); + }); + + given("a{content:'abc\\'\\'d\\'ef'}", function(input) { + var parsed = CSSOM.parse(input); + expect(parsed.cssRules[0].style.content).toBe("'abc\\'\\'d\\'ef'"); + }); + }); }); + /** * Recursively remove all keys which start with '_', except "_vendorPrefix", which needs to be tested against. * @param {Object} object