-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssProperties.js
129 lines (114 loc) · 3.07 KB
/
cssProperties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
function splitSelector(selector)
{
if (selector.indexOf(",") == -1)
return [selector];
var selectors = [];
var start = 0;
var level = 0;
var sep = "";
for (var i = 0; i < selector.length; i++)
{
var 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 (chr == "(") // don't split between parentheses
level++; // e.g. :matches(div,span)
else if (chr == ")")
level = Math.max(0, level - 1);
else if (chr == "," && level == 0)
{
selectors.push(selector.substring(start, i));
start = i + 1;
}
}
}
selectors.push(selector.substring(start));
return selectors;
}
function CSSPropertyFilters(window, addSelectorsFunc) {
this.window = window;
this.addSelectorsFunc = addSelectorsFunc;
}
CSSPropertyFilters.prototype = {
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 != this.window.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);
if (regexp.test(style))
{
var subSelectors = splitSelector(rule.selectorText);
for (var k = 0; k < subSelectors.length; k++)
selectors.push(pattern.prefix + subSelectors[k] + pattern.suffix);
}
}
}
},
addSelectors: function(stylesheets)
{
var selectors = [];
for (var i = 0; i < stylesheets.length; i++)
this.findSelectors(stylesheets[i], selectors);
this.addSelectorsFunc(selectors);
},
onLoad: function(event)
{
var stylesheet = event.target.sheet;
if (stylesheet)
this.addSelectors([stylesheet]);
},
load: function(callback)
{
ext.backgroundPage.sendMessage(
{
type: "filters.get",
what: "cssproperties"
},
function(patterns)
{
this.patterns = patterns;
callback();
}.bind(this)
);
},
apply: function()
{
if (this.patterns.length > 0)
{
var document = this.window.document;
this.addSelectors(document.styleSheets);
document.addEventListener("load", this.onLoad.bind(this), true);
}
}
};