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

Delta Between Two Patch Sets: chrome/content/cssProperties.js

Issue 29317059: Issue 2395 - Added content script for CSS property filters (Closed)
Left Patch Set: Created June 18, 2015, 6:44 a.m.
Right Patch Set: Make function to add CSS selectors configurable in the constructor Created Nov. 13, 2015, 1:16 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 CSSPropertyFilters = { 1 function CSSPropertyFilters(window, addSelectorsFunc) {
2 this.window = window;
3 this.addSelectorsFunc = addSelectorsFunc;
4 this.load(this.apply.bind(this));
5 }
6
7 CSSPropertyFilters.prototype = {
2 stringifyStyle: function(style) 8 stringifyStyle: function(style)
3 { 9 {
4 var styles = []; 10 var styles = [];
5 for (var i = 0; i < style.length; i++) 11 for (var i = 0; i < style.length; i++)
6 { 12 {
7 var property = style.item(i); 13 var property = style.item(i);
8 var value = style.getPropertyValue(property); 14 var value = style.getPropertyValue(property);
9 var priority = style.getPropertyPriority(property); 15 var priority = style.getPropertyPriority(property);
10 styles.push(property + ": " + value + (priority ? " !" + priority : "") + ";"); 16 styles.push(property + ": " + value + (priority ? " !" + priority : "") + ";");
11 } 17 }
12 styles.sort(); 18 styles.sort();
13 return styles.join(" "); 19 return styles.join(" ");
14 }, 20 },
15 21
16 findSelectors: function(stylesheet, selectors) 22 findSelectors: function(stylesheet, selectors)
17 { 23 {
18 var rules = stylesheet.cssRules; 24 var rules = stylesheet.cssRules;
19 if (!rules) 25 if (!rules)
20 return; 26 return;
21 27
22 for (var i = 0; i < rules.length; i++) 28 for (var i = 0; i < rules.length; i++)
23 { 29 {
24 var rule = rules[i]; 30 var rule = rules[i];
25 if (rule.type != CSSRule.STYLE_RULE) 31 if (rule.type != this.window.CSSRule.STYLE_RULE)
26 continue; 32 continue;
27 33
28 var style = this.stringifyStyle(rule.style); 34 var style = this.stringifyStyle(rule.style);
29 for (var j = 0; j < this.patterns.length; j++) 35 for (var j = 0; j < this.patterns.length; j++)
30 { 36 {
31 var pattern = this.patterns[j]; 37 var pattern = this.patterns[j];
32 var regexp = pattern.regexp; 38 var regexp = pattern.regexp;
33 39
34 if (typeof regexp == "string") 40 if (typeof regexp == "string")
35 regexp = pattern.regexp = new RegExp(regexp); 41 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
36 42
37 if (regexp.test(style)) 43 if (regexp.test(style))
38 selectors.push(pattern.prefix + rule.selectorText + pattern.suffix); 44 selectors.push(pattern.prefix + rule.selectorText + pattern.suffix);
39 } 45 }
40 } 46 }
41 }, 47 },
42 48
43 addSelectors: function(stylesheets) 49 addSelectors: function(stylesheets)
44 { 50 {
45 var selectors = []; 51 var selectors = [];
46 for (var i = 0; i < stylesheets.length; i++) 52 for (var i = 0; i < stylesheets.length; i++)
47 this.findSelectors(stylesheets[i], selectors); 53 this.findSelectors(stylesheets[i], selectors);
48 addElemHideSelectors(selectors); 54 this.addSelectorsFunc(selectors);
49 }, 55 },
50 56
51 onLoad: function(event) 57 onLoad: function(event)
52 { 58 {
53 var stylesheet = event.target.sheet; 59 var stylesheet = event.target.sheet;
54 if (stylesheet) 60 if (stylesheet)
55 CSSPropertyFilters.addSelectors([stylesheet]); 61 this.addSelectors([stylesheet]);
56 }, 62 },
57 63
58 load: function(callback) 64 load: function(callback)
59 { 65 {
60 ext.backgroundPage.sendMessage( 66 ext.backgroundPage.sendMessage(
61 { 67 {
62 type: "filters.get", 68 type: "filters.get",
63 what: "cssproperties" 69 what: "cssproperties"
64 }, 70 },
65 function(patterns) 71 function(patterns)
66 { 72 {
67 this.patterns = patterns; 73 this.patterns = patterns;
68 callback(); 74 callback();
69 }.bind(this) 75 }.bind(this)
70 ); 76 );
71 }, 77 },
72 78
73 apply: function() 79 apply: function()
74 { 80 {
75 if (patterns.length > 0) 81 if (this.patterns.length > 0)
76 { 82 {
83 var document = this.window.document;
77 this.addSelectors(document.styleSheets); 84 this.addSelectors(document.styleSheets);
78 document.addEventListener("load", this.onLoad, true); 85 document.addEventListener("load", this.onLoad.bind(this), 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
79 } 86 }
80 },
81
82 init: function()
83 {
84 this.load(this.apply.bind(this));
85 } 87 }
86 }; 88 };
87
88 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.
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld