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

Side by Side Diff: chrome/content/cssProperties.js

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

Powered by Google App Engine
This is Rietveld