From f177b0a7ed719812a9fee4ab7b2a6378a3686b1c Mon Sep 17 00:00:00 2001 From: Simon Pelchat Date: Tue, 16 Oct 2018 10:52:08 -0700 Subject: [PATCH] Improve parsing URIs with escape sequences. --- src/css/PropertyValuePart.js | 12 ++++++------ tests/css/Parser.js | 11 ++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/css/PropertyValuePart.js b/src/css/PropertyValuePart.js index 4db048db..70aef3d5 100644 --- a/src/css/PropertyValuePart.js +++ b/src/css/PropertyValuePart.js @@ -146,7 +146,7 @@ function PropertyValuePart(text, line, col, optionalHint) { this.saturation = Number(RegExp.$2) / 100; this.lightness = Number(RegExp.$3) / 100; this.alpha = Number(RegExp.$4); - } else if (/^url\(("([^\\"]|\.)*")\)/i.test(text)) { // URI + } else if (/^url\(("([^\\"]|\\.)*")\)/i.test(text)) { // URI // generated by TokenStream.readURI, so always double-quoted. this.type = "uri"; this.uri = PropertyValuePart.parseString(RegExp.$1); @@ -214,14 +214,14 @@ PropertyValuePart.parseString = function(str) { * Helper method to serialize a CSS string. */ PropertyValuePart.serializeString = function(value) { - var replacer = function(match, c) { - if (c === "\"") { - return "\\" + c; + var replacer = function(match) { + if (match === "\"") { + return "\\" + match; } - var cp = String.codePointAt ? String.codePointAt(0) : + var cp = match.codePointAt ? match.codePointAt(0) : // We only escape non-surrogate chars, so using charCodeAt // is harmless here. - String.charCodeAt(0); + match.charCodeAt(0); return "\\" + cp.toString(16) + " "; }; return "\"" + value.replace(/["\r\n\f]/g, replacer) + "\""; diff --git a/tests/css/Parser.js b/tests/css/Parser.js index f050f412..b0b01108 100644 --- a/tests/css/Parser.js +++ b/tests/css/Parser.js @@ -1271,6 +1271,16 @@ var YUITest = require("yuitest"), Assert.areEqual("https://yahoo.com", result.parts[0].uri); }, + testURIValue5: function() { + var parser = new Parser({ strict:true }); + var result = parser.parsePropertyValue("url(url_with_\\\".png)"); + + Assert.isInstanceOf(parserlib.css.PropertyValue, result); + Assert.areEqual(1, result.parts.length); + Assert.areEqual("uri", result.parts[0].type); + Assert.areEqual("url_with_\".png", result.parts[0].uri); + }, + testStringValue: function() { var parser = new Parser(); var result = parser.parsePropertyValue("'Hello world!'"); @@ -1373,7 +1383,6 @@ var YUITest = require("yuitest"), Assert.areEqual("serif", result.parts[7].value); } - })); suite.add(new YUITest.TestCase({