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 nit I missed before Created Feb. 17, 2016, 3:25 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..cbc9fecedde3ca4058fe8e0b7485b82bae088c24 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 split within quoted text
+ sep = ""; // e.g. [attr=","]
+ 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 position = 0;
+ for (let ID of positions)
+ {
+ newSelector += selector.substring(position, ID.start);
+ newSelector += '[id=' + selector.substring(ID.start + 1, ID.end) + ']';
+ position = ID.end;
+ }
+ newSelector += selector.substring(position);
+
+ return newSelector;
+}
+
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