From 30c066c25663854b81dda60c5f75047f2644b904 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 223017b..fbcdc76 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -73,10 +73,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) { @@ -90,10 +93,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