Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: include.postload.js

Issue 4935175632846848: Issue 1527 - Properly escape generated CSS selectors (Closed)
Patch Set: Renamed function and added test case Created Nov. 6, 2014, 2:59 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | qunit/index.html » ('j') | qunit/tests/cssEscaping.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.postload.js
===================================================================
--- a/include.postload.js
+++ b/include.postload.js
@@ -24,9 +24,24 @@
var clickHideFiltersDialog = null;
var lastRightClickEvent = null;
+var ctrlChar = /[\x00-\x1F\x7F]/;
+
+function escapeChar(chr)
+{
+ if (ctrlChar.test(chr) || /\d/.test(chr))
+ return "\\" + chr.charCodeAt(0).toString(16) + " ";
+
+ return "\\" + chr;
+}
+
function quote(value)
{
- return '"' + value.replace(/(["\\])/g, "\\$1") + '"';
+ return '"' + value.replace(new RegExp('["\\\\]|' + ctrlChar.source, "g"), escapeChar) + '"';
Wladimir Palant 2014/11/06 21:26:06 Generating regular expressions dynamically for no
Sebastian Noack 2014/11/10 12:13:18 The idea was not to hard-code the char codes for c
+}
+
+function escapeCSS(s)
+{
+ return s.replace(/^\d|^-(?![^\d\-])|[^\w\-\u0080-\uFFFF]/g, escapeChar);
Wladimir Palant 2014/11/06 21:26:06 Thinking more about this, I'm definitely opposed t
Sebastian Noack 2014/11/10 12:13:18 It's probably not worth to argue here. But I still
}
function supportsShadowRoot(element)
@@ -376,10 +391,6 @@
else if (elt.src)
url = elt.src;
- // Construct filters. The popup will retrieve these.
- // Only one ID
- var elementId = elt.id ? elt.id.split(' ').join('') : null;
-
clickHideFilters = new Array();
selectorList = new Array();
@@ -389,15 +400,15 @@
selectorList.push(selector);
};
- if (elementId)
- addSelector("#" + elementId);
+ if (elt.id)
+ addSelector("#" + escapeCSS(elt.id));
if (elt.classList.length > 0)
{
var selector = "";
for (var i = 0; i < elt.classList.length; i++)
- selector += "." + elt.classList[i].replace(/([^\w-])/g, "\\$1");
+ selector += "." + escapeCSS(elt.classList[i]);
addSelector(selector);
}
@@ -405,7 +416,7 @@
if (url)
{
var src = elt.getAttribute("src");
- var selector = src && elt.localName + '[src=' + quote(src) + ']';
+ var selector = src && escapeCSS(elt.localName) + '[src=' + quote(src) + ']';
if (/^https?:/i.test(url))
{
@@ -427,7 +438,7 @@
{
var style = elt.getAttribute("style");
if (style)
- addSelector(elt.localName + '[style=' + quote(style) + ']');
+ addSelector(escapeCSS(elt.localName) + '[style=' + quote(style) + ']');
}
// Show popup
« no previous file with comments | « no previous file | qunit/index.html » ('j') | qunit/tests/cssEscaping.js » ('J')

Powered by Google App Engine
This is Rietveld