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: Addressed comments Created Nov. 10, 2014, 12:10 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;
+function escapeChar(chr)
+{
+ var code = chr.charCodeAt(0);
+
+ if (code <= 0x1F || code == 0x7F || /\d/.test(chr))
+ return "\\" + code.toString(16) + " ";
+
+ return "\\" + chr;
+}
+
function quote(value)
{
- return '"' + value.replace(/(["\\])/g, "\\$1") + '"';
+ return '"' + value.replace(/["\\\x00-\x1F\x7F]/g, escapeChar) + '"';
+}
+
+function escapeCSS(s)
+{
+ return s.replace(/^[\d\-]|[^\w\-\u0080-\uFFFF]/g, escapeChar);
}
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