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

Unified Diff: chrome/content/cssProperties.js

Issue 29317059: Issue 2395 - Added content script for CSS property filters (Closed)
Patch Set: Created June 18, 2015, 6:44 a.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: chrome/content/cssProperties.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/chrome/content/cssProperties.js
@@ -0,0 +1,88 @@
+CSSPropertyFilters = {
+ stringifyStyle: function(style)
+ {
+ var styles = [];
+ for (var i = 0; i < style.length; i++)
+ {
+ var property = style.item(i);
+ var value = style.getPropertyValue(property);
+ var priority = style.getPropertyPriority(property);
+ styles.push(property + ": " + value + (priority ? " !" + priority : "") + ";");
+ }
+ styles.sort();
+ return styles.join(" ");
+ },
+
+ findSelectors: function(stylesheet, selectors)
+ {
+ var rules = stylesheet.cssRules;
+ if (!rules)
+ return;
+
+ for (var i = 0; i < rules.length; i++)
+ {
+ var rule = rules[i];
+ if (rule.type != CSSRule.STYLE_RULE)
+ continue;
+
+ var style = this.stringifyStyle(rule.style);
+ for (var j = 0; j < this.patterns.length; j++)
+ {
+ var pattern = this.patterns[j];
+ var regexp = pattern.regexp;
+
+ if (typeof regexp == "string")
+ regexp = pattern.regexp = new RegExp(regexp);
Wladimir Palant 2015/10/23 18:36:37 What about invalid regexps? new RegExp() can throw
Sebastian Noack 2015/10/23 20:49:31 The regular expression is generated by our code. S
+
+ if (regexp.test(style))
+ selectors.push(pattern.prefix + rule.selectorText + pattern.suffix);
+ }
+ }
+ },
+
+ addSelectors: function(stylesheets)
+ {
+ var selectors = [];
+ for (var i = 0; i < stylesheets.length; i++)
+ this.findSelectors(stylesheets[i], selectors);
+ addElemHideSelectors(selectors);
+ },
+
+ onLoad: function(event)
+ {
+ var stylesheet = event.target.sheet;
+ if (stylesheet)
+ CSSPropertyFilters.addSelectors([stylesheet]);
+ },
+
+ load: function(callback)
+ {
+ ext.backgroundPage.sendMessage(
+ {
+ type: "filters.get",
+ what: "cssproperties"
+ },
+ function(patterns)
+ {
+ this.patterns = patterns;
+ callback();
+ }.bind(this)
+ );
+ },
+
+ apply: function()
+ {
+ if (patterns.length > 0)
+ {
+ this.addSelectors(document.styleSheets);
+ document.addEventListener("load", this.onLoad, true);
Wladimir Palant 2015/10/23 18:36:36 Are you sure that the "load" event is being trigge
Sebastian Noack 2015/10/23 20:49:31 Seems so. Tested using https://pastebin.mozilla.or
+ }
+ },
+
+ init: function()
+ {
+ this.load(this.apply.bind(this));
+ }
+};
+
+CSSPropertyFilters.init();
Wladimir Palant 2015/10/23 18:36:37 As I said earlier, CSSPropertyFilters should ideal
Sebastian Noack 2015/10/23 20:49:31 I've added the check. Do we have to use a construc
Wladimir Palant 2015/10/23 21:04:40 In Firefox there will be more than one window/docu
Sebastian Noack 2015/10/23 21:23:22 Gotya.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld