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

Unified Diff: abp2blocklist.js

Issue 29336525: Issue 3584 - Work around WebKit uppercase ID matching bug (Closed)
Patch Set: Addressed further feedback Created Feb. 17, 2016, 3:33 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: abp2blocklist.js
diff --git a/abp2blocklist.js b/abp2blocklist.js
index 71d227f3e66f9a9f9c044409840967523982d923..2a032dd8f666ef4245889721676254e02b2a8501 100644
--- a/abp2blocklist.js
+++ b/abp2blocklist.js
@@ -254,6 +254,57 @@ function hasNonASCI(obj)
return false;
}
+function convertIDSelectorsToAttributeSelectors(selector)
+{
+ // First we figure out where all the IDs are
+ let sep = "";
+ let start = null;
+ let positions = [];
+ for (let i = 0; i < selector.length; i++)
+ {
+ let chr = selector[i];
+
+ if (chr == "\\") // ignore escaped characters
+ i++;
+ else if (chr == sep) // don't match IDs within quoted text
+ sep = ""; // e.g. [attr="#Hello"]
+ else if (sep == "")
+ {
+ if (chr == '"' || chr == "'")
+ sep = chr;
+ else if (start == null) // look for the start of an ID
+ {
+ if (chr == "#")
+ start = i;
+ }
+ else if (chr != "-" && chr != "_" &&
+ (chr < "0" ||
+ chr > "9" && chr < "A" ||
+ chr > "Z" && chr < "a" ||
+ chr > "z" && chr < "\x80")) // look for the end of the ID
+ {
+ positions.push({start: start, end: i});
+ start = null;
+ }
+ }
+ }
+ if (start != null)
+ positions.push({start: start, end: selector.length});
+
+ // Now replace them all with the [id="someID"] form
+ let newSelector = [];
+ let i = 0;
+ for (let pos of positions)
+ {
+ newSelector.push(selector.substring(i, pos.start));
+ newSelector.push('[id=' + selector.substring(pos.start + 1, pos.end) + ']');
+ i = pos.end;
+ }
+ newSelector.push(selector.substring(i));
+
+ return newSelector.join("");
+}
+
function logRules()
{
let rules = [];
@@ -286,10 +337,16 @@ function logRules()
{
while (selectors.length)
{
+ let selector = selectors.splice(0, selectorLimit).join(", ");
+
+ // As of Safari 9.0 element IDs are matched as lowercase. We work around
+ // this by converting to the attribute format [id="elementID"]
+ selector = convertIDSelectorsToAttributeSelectors(selector);
+
addRule({
trigger: {"url-filter": matchDomain},
action: {type: "css-display-none",
- selector: selectors.splice(0, selectorLimit).join(", ")}
+ selector: selector}
});
}
});
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld