@acemir/cssom 0.9.19 → 0.9.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/CSSOM.js +1121 -217
- package/lib/CSSConditionRule.js +8 -3
- package/lib/CSSContainerRule.js +21 -12
- package/lib/CSSDocumentRule.js +2 -1
- package/lib/CSSGroupingRule.js +2 -1
- package/lib/CSSHostRule.js +3 -2
- package/lib/CSSImportRule.js +92 -19
- package/lib/CSSKeyframesRule.js +2 -1
- package/lib/CSSLayerBlockRule.js +1 -1
- package/lib/CSSMediaRule.js +14 -5
- package/lib/CSSNamespaceRule.js +27 -11
- package/lib/CSSPageRule.js +275 -0
- package/lib/CSSRule.js +6 -0
- package/lib/CSSRuleList.js +26 -0
- package/lib/CSSScopeRule.js +53 -0
- package/lib/CSSStartingStyleRule.js +5 -4
- package/lib/CSSStyleRule.js +21 -5
- package/lib/CSSStyleSheet.js +49 -9
- package/lib/CSSSupportsRule.js +1 -0
- package/lib/StyleSheet.js +16 -1
- package/lib/clone.js +1 -0
- package/lib/index.js +3 -0
- package/lib/parse.js +504 -104
- package/package.json +1 -1
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
//.CommonJS
|
|
2
|
+
var CSSOM = {
|
|
3
|
+
CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
|
|
4
|
+
CSSRule: require("./CSSRule").CSSRule,
|
|
5
|
+
CSSRuleList: require("./CSSRuleList").CSSRuleList,
|
|
6
|
+
CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
|
|
7
|
+
};
|
|
8
|
+
// Use cssstyle if available
|
|
9
|
+
try {
|
|
10
|
+
CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
// ignore
|
|
13
|
+
}
|
|
14
|
+
///CommonJS
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @constructor
|
|
19
|
+
* @see https://drafts.csswg.org/cssom/#the-csspagerule-interface
|
|
20
|
+
*/
|
|
21
|
+
CSSOM.CSSPageRule = function CSSPageRule() {
|
|
22
|
+
CSSOM.CSSGroupingRule.call(this);
|
|
23
|
+
this.__style = new CSSOM.CSSStyleDeclaration();
|
|
24
|
+
this.__style.parentRule = this;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
CSSOM.CSSPageRule.prototype = new CSSOM.CSSGroupingRule();
|
|
28
|
+
CSSOM.CSSPageRule.prototype.constructor = CSSOM.CSSPageRule;
|
|
29
|
+
|
|
30
|
+
Object.defineProperty(CSSOM.CSSPageRule.prototype, "type", {
|
|
31
|
+
value: 6,
|
|
32
|
+
writable: false
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
Object.defineProperty(CSSOM.CSSPageRule.prototype, "selectorText", {
|
|
36
|
+
get: function() {
|
|
37
|
+
return this.__selectorText;
|
|
38
|
+
},
|
|
39
|
+
set: function(value) {
|
|
40
|
+
if (typeof value === "string") {
|
|
41
|
+
var trimmedValue = value.trim();
|
|
42
|
+
|
|
43
|
+
// Empty selector is valid for @page
|
|
44
|
+
if (trimmedValue === '') {
|
|
45
|
+
this.__selectorText = '';
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Parse @page selectorText for page name and pseudo-pages
|
|
50
|
+
// Valid formats:
|
|
51
|
+
// - (empty - no name, no pseudo-page)
|
|
52
|
+
// - :left, :right, :first, :blank (pseudo-page only)
|
|
53
|
+
// - named (named page only)
|
|
54
|
+
// - named:first (named page with single pseudo-page)
|
|
55
|
+
// - named:first:left (named page with multiple pseudo-pages)
|
|
56
|
+
var atPageRuleSelectorRegExp = /^([^\s:]+)?((?::\w+)*)$/;
|
|
57
|
+
var match = trimmedValue.match(atPageRuleSelectorRegExp);
|
|
58
|
+
if (match) {
|
|
59
|
+
var pageName = match[1] || '';
|
|
60
|
+
var pseudoPages = match[2] || '';
|
|
61
|
+
|
|
62
|
+
// Validate page name if present
|
|
63
|
+
if (pageName) {
|
|
64
|
+
var cssCustomIdentifierRegExp = /^(-?[_a-zA-Z]+(\.[_a-zA-Z]+)*[_a-zA-Z0-9-]*)$/; // Validates a css custom identifier
|
|
65
|
+
// Page name can be an identifier or a string
|
|
66
|
+
if (!cssCustomIdentifierRegExp.test(pageName)) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Validate pseudo-pages if present
|
|
72
|
+
if (pseudoPages) {
|
|
73
|
+
var pseudos = pseudoPages.split(':').filter(function(p) { return p; });
|
|
74
|
+
var validPseudos = ['left', 'right', 'first', 'blank'];
|
|
75
|
+
var allValid = true;
|
|
76
|
+
for (var j = 0; j < pseudos.length; j++) {
|
|
77
|
+
if (validPseudos.indexOf(pseudos[j].toLowerCase()) === -1) {
|
|
78
|
+
allValid = false;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!allValid) {
|
|
84
|
+
return; // Invalid pseudo-page, do nothing
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.__selectorText = pageName + pseudoPages.toLowerCase();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
Object.defineProperty(CSSOM.CSSPageRule.prototype, "style", {
|
|
95
|
+
get: function() {
|
|
96
|
+
return this.__style;
|
|
97
|
+
},
|
|
98
|
+
set: function(value) {
|
|
99
|
+
if (typeof value === "string") {
|
|
100
|
+
this.__style.cssText = value;
|
|
101
|
+
} else {
|
|
102
|
+
this.__style = value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
Object.defineProperty(CSSOM.CSSPageRule.prototype, "cssText", {
|
|
108
|
+
get: function() {
|
|
109
|
+
var values = ""
|
|
110
|
+
if (this.cssRules.length) {
|
|
111
|
+
var valuesArr = [" {"];
|
|
112
|
+
this.style.cssText && valuesArr.push(this.style.cssText);
|
|
113
|
+
valuesArr.push(this.cssRules.map(function(rule){ return rule.cssText }).join("\n "));
|
|
114
|
+
values = valuesArr.join("\n ") + "\n}"
|
|
115
|
+
} else {
|
|
116
|
+
values = " {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
|
|
117
|
+
}
|
|
118
|
+
return "@page" + (this.selectorText ? " " + this.selectorText : "") + values;
|
|
119
|
+
},
|
|
120
|
+
set: function(cssText) {
|
|
121
|
+
if (typeof value === "string") {
|
|
122
|
+
var rule = CSSOM.CSSPageRule.parse(cssText);
|
|
123
|
+
this.__style = rule.style;
|
|
124
|
+
this.selectorText = rule.selectorText;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* NON-STANDARD
|
|
131
|
+
* lightweight version of parse.js.
|
|
132
|
+
* @param {string} ruleText
|
|
133
|
+
* @return CSSPageRule
|
|
134
|
+
*/
|
|
135
|
+
CSSOM.CSSPageRule.parse = function(ruleText) {
|
|
136
|
+
var i = 0;
|
|
137
|
+
var state = "selector";
|
|
138
|
+
var index;
|
|
139
|
+
var j = i;
|
|
140
|
+
var buffer = "";
|
|
141
|
+
|
|
142
|
+
var SIGNIFICANT_WHITESPACE = {
|
|
143
|
+
"selector": true,
|
|
144
|
+
"value": true
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
var pageRule = new CSSOM.CSSPageRule();
|
|
148
|
+
var name, priority="";
|
|
149
|
+
|
|
150
|
+
for (var character; (character = ruleText.charAt(i)); i++) {
|
|
151
|
+
|
|
152
|
+
switch (character) {
|
|
153
|
+
|
|
154
|
+
case " ":
|
|
155
|
+
case "\t":
|
|
156
|
+
case "\r":
|
|
157
|
+
case "\n":
|
|
158
|
+
case "\f":
|
|
159
|
+
if (SIGNIFICANT_WHITESPACE[state]) {
|
|
160
|
+
// Squash 2 or more white-spaces in the row into 1
|
|
161
|
+
switch (ruleText.charAt(i - 1)) {
|
|
162
|
+
case " ":
|
|
163
|
+
case "\t":
|
|
164
|
+
case "\r":
|
|
165
|
+
case "\n":
|
|
166
|
+
case "\f":
|
|
167
|
+
break;
|
|
168
|
+
default:
|
|
169
|
+
buffer += " ";
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
|
|
175
|
+
// String
|
|
176
|
+
case '"':
|
|
177
|
+
j = i + 1;
|
|
178
|
+
index = ruleText.indexOf('"', j) + 1;
|
|
179
|
+
if (!index) {
|
|
180
|
+
throw '" is missing';
|
|
181
|
+
}
|
|
182
|
+
buffer += ruleText.slice(i, index);
|
|
183
|
+
i = index - 1;
|
|
184
|
+
break;
|
|
185
|
+
|
|
186
|
+
case "'":
|
|
187
|
+
j = i + 1;
|
|
188
|
+
index = ruleText.indexOf("'", j) + 1;
|
|
189
|
+
if (!index) {
|
|
190
|
+
throw "' is missing";
|
|
191
|
+
}
|
|
192
|
+
buffer += ruleText.slice(i, index);
|
|
193
|
+
i = index - 1;
|
|
194
|
+
break;
|
|
195
|
+
|
|
196
|
+
// Comment
|
|
197
|
+
case "/":
|
|
198
|
+
if (ruleText.charAt(i + 1) === "*") {
|
|
199
|
+
i += 2;
|
|
200
|
+
index = ruleText.indexOf("*/", i);
|
|
201
|
+
if (index === -1) {
|
|
202
|
+
throw new SyntaxError("Missing */");
|
|
203
|
+
} else {
|
|
204
|
+
i = index + 1;
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
buffer += character;
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
|
|
211
|
+
case "{":
|
|
212
|
+
if (state === "selector") {
|
|
213
|
+
pageRule.selectorText = buffer.trim();
|
|
214
|
+
buffer = "";
|
|
215
|
+
state = "name";
|
|
216
|
+
}
|
|
217
|
+
break;
|
|
218
|
+
|
|
219
|
+
case ":":
|
|
220
|
+
if (state === "name") {
|
|
221
|
+
name = buffer.trim();
|
|
222
|
+
buffer = "";
|
|
223
|
+
state = "value";
|
|
224
|
+
} else {
|
|
225
|
+
buffer += character;
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
|
|
229
|
+
case "!":
|
|
230
|
+
if (state === "value" && ruleText.indexOf("!important", i) === i) {
|
|
231
|
+
priority = "important";
|
|
232
|
+
i += "important".length;
|
|
233
|
+
} else {
|
|
234
|
+
buffer += character;
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
237
|
+
|
|
238
|
+
case ";":
|
|
239
|
+
if (state === "value") {
|
|
240
|
+
pageRule.style.setProperty(name, buffer.trim(), priority);
|
|
241
|
+
priority = "";
|
|
242
|
+
buffer = "";
|
|
243
|
+
state = "name";
|
|
244
|
+
} else {
|
|
245
|
+
buffer += character;
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
|
|
249
|
+
case "}":
|
|
250
|
+
if (state === "value") {
|
|
251
|
+
pageRule.style.setProperty(name, buffer.trim(), priority);
|
|
252
|
+
priority = "";
|
|
253
|
+
buffer = "";
|
|
254
|
+
} else if (state === "name") {
|
|
255
|
+
break;
|
|
256
|
+
} else {
|
|
257
|
+
buffer += character;
|
|
258
|
+
}
|
|
259
|
+
state = "selector";
|
|
260
|
+
break;
|
|
261
|
+
|
|
262
|
+
default:
|
|
263
|
+
buffer += character;
|
|
264
|
+
break;
|
|
265
|
+
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return pageRule;
|
|
270
|
+
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
//.CommonJS
|
|
274
|
+
exports.CSSPageRule = CSSOM.CSSPageRule;
|
|
275
|
+
///CommonJS
|
package/lib/CSSRule.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//.CommonJS
|
|
2
|
+
var CSSOM = {};
|
|
3
|
+
///CommonJS
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @constructor
|
|
8
|
+
* @see https://drafts.csswg.org/cssom/#the-cssrulelist-interface
|
|
9
|
+
*/
|
|
10
|
+
CSSOM.CSSRuleList = function CSSRuleList(){
|
|
11
|
+
const arr = new Array();
|
|
12
|
+
Object.setPrototypeOf(arr, CSSOM.CSSRuleList.prototype);
|
|
13
|
+
return arr;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
CSSOM.CSSRuleList.prototype = Object.create(Array.prototype);
|
|
17
|
+
CSSOM.CSSRuleList.prototype.constructor = CSSOM.CSSRuleList;
|
|
18
|
+
|
|
19
|
+
CSSOM.CSSRuleList.prototype.item = function(index) {
|
|
20
|
+
return this[index] || null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
//.CommonJS
|
|
25
|
+
exports.CSSRuleList = CSSOM.CSSRuleList;
|
|
26
|
+
///CommonJS
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//.CommonJS
|
|
2
|
+
var CSSOM = {
|
|
3
|
+
CSSRule: require("./CSSRule").CSSRule,
|
|
4
|
+
CSSRuleList: require("./CSSRuleList").CSSRuleList,
|
|
5
|
+
CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
|
|
6
|
+
};
|
|
7
|
+
///CommonJS
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @constructor
|
|
11
|
+
* @see https://drafts.csswg.org/css-cascade-6/#cssscoperule
|
|
12
|
+
*/
|
|
13
|
+
CSSOM.CSSScopeRule = function CSSScopeRule() {
|
|
14
|
+
CSSOM.CSSGroupingRule.call(this);
|
|
15
|
+
this.__start = null;
|
|
16
|
+
this.__end = null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
CSSOM.CSSScopeRule.prototype = new CSSOM.CSSGroupingRule();
|
|
20
|
+
CSSOM.CSSScopeRule.prototype.constructor = CSSOM.CSSScopeRule;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
Object.defineProperties(CSSOM.CSSScopeRule.prototype, {
|
|
24
|
+
type: {
|
|
25
|
+
value: 0,
|
|
26
|
+
writable: false,
|
|
27
|
+
},
|
|
28
|
+
cssText: {
|
|
29
|
+
get: function () {
|
|
30
|
+
var cssTexts = [];
|
|
31
|
+
for (var i = 0, length = this.cssRules.length; i < length; i++) {
|
|
32
|
+
cssTexts.push(this.cssRules[i].cssText);
|
|
33
|
+
}
|
|
34
|
+
return "@scope " + (this.start ? "(" + this.start + ") " : "") + (this.end ? "to (" + this.end + ") " : "") + "{" + (cssTexts.length ? "\n " + cssTexts.join("\n ") : "") + "\n}";
|
|
35
|
+
},
|
|
36
|
+
configurable: true,
|
|
37
|
+
enumerable: true,
|
|
38
|
+
},
|
|
39
|
+
start: {
|
|
40
|
+
get: function () {
|
|
41
|
+
return this.__start;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
end: {
|
|
45
|
+
get: function () {
|
|
46
|
+
return this.__end;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
//.CommonJS
|
|
52
|
+
exports.CSSScopeRule = CSSOM.CSSScopeRule;
|
|
53
|
+
///CommonJS
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
//.CommonJS
|
|
2
2
|
var CSSOM = {
|
|
3
|
-
CSSRule: require("./CSSRule").CSSRule
|
|
3
|
+
CSSRule: require("./CSSRule").CSSRule,
|
|
4
|
+
CSSRuleList: require("./CSSRuleList").CSSRuleList,
|
|
5
|
+
CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule
|
|
4
6
|
};
|
|
5
7
|
///CommonJS
|
|
6
8
|
|
|
@@ -10,11 +12,10 @@ var CSSOM = {
|
|
|
10
12
|
* @see http://www.w3.org/TR/shadow-dom/#host-at-rule
|
|
11
13
|
*/
|
|
12
14
|
CSSOM.CSSStartingStyleRule = function CSSStartingStyleRule() {
|
|
13
|
-
CSSOM.
|
|
14
|
-
this.cssRules = [];
|
|
15
|
+
CSSOM.CSSGroupingRule.call(this);
|
|
15
16
|
};
|
|
16
17
|
|
|
17
|
-
CSSOM.CSSStartingStyleRule.prototype = new CSSOM.
|
|
18
|
+
CSSOM.CSSStartingStyleRule.prototype = new CSSOM.CSSGroupingRule();
|
|
18
19
|
CSSOM.CSSStartingStyleRule.prototype.constructor = CSSOM.CSSStartingStyleRule;
|
|
19
20
|
CSSOM.CSSStartingStyleRule.prototype.type = 1002;
|
|
20
21
|
//FIXME
|
package/lib/CSSStyleRule.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
//.CommonJS
|
|
2
2
|
var CSSOM = {
|
|
3
3
|
CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
|
|
4
|
+
CSSRule: require("./CSSRule").CSSRule,
|
|
5
|
+
CSSRuleList: require("./CSSRuleList").CSSRuleList,
|
|
4
6
|
CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
|
|
5
|
-
CSSRule: require("./CSSRule").CSSRule
|
|
6
7
|
};
|
|
7
8
|
// Use cssstyle if available
|
|
8
9
|
try {
|
|
@@ -38,7 +39,20 @@ Object.defineProperty(CSSOM.CSSStyleRule.prototype, "selectorText", {
|
|
|
38
39
|
return this.__selectorText;
|
|
39
40
|
},
|
|
40
41
|
set: function(value) {
|
|
41
|
-
|
|
42
|
+
if (typeof value === "string") {
|
|
43
|
+
var trimmedValue = value.trim();
|
|
44
|
+
|
|
45
|
+
if (trimmedValue === '') {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// TODO: Setting invalid selectorText should be ignored
|
|
50
|
+
// There are some validations already on lib/parse.js
|
|
51
|
+
// but the same validations should be applied here.
|
|
52
|
+
// Check if we can move these validation logic to a shared function.
|
|
53
|
+
|
|
54
|
+
this.__selectorText = trimmedValue;
|
|
55
|
+
}
|
|
42
56
|
}
|
|
43
57
|
});
|
|
44
58
|
|
|
@@ -75,9 +89,11 @@ Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
|
|
|
75
89
|
return text;
|
|
76
90
|
},
|
|
77
91
|
set: function(cssText) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
92
|
+
if (typeof cssText === "string") {
|
|
93
|
+
var rule = CSSOM.CSSStyleRule.parse(cssText);
|
|
94
|
+
this.__style = rule.style;
|
|
95
|
+
this.selectorText = rule.selectorText;
|
|
96
|
+
}
|
|
81
97
|
}
|
|
82
98
|
});
|
|
83
99
|
|
package/lib/CSSStyleSheet.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
//.CommonJS
|
|
2
2
|
var CSSOM = {
|
|
3
|
+
MediaList: require("./MediaList").MediaList,
|
|
3
4
|
StyleSheet: require("./StyleSheet").StyleSheet,
|
|
4
|
-
|
|
5
|
+
CSSRuleList: require("./CSSRuleList").CSSRuleList,
|
|
6
|
+
CSSStyleRule: require("./CSSStyleRule").CSSStyleRule,
|
|
5
7
|
};
|
|
6
8
|
var errorUtils = require("./errorUtils").errorUtils;
|
|
7
9
|
///CommonJS
|
|
@@ -13,13 +15,18 @@ var errorUtils = require("./errorUtils").errorUtils;
|
|
|
13
15
|
*/
|
|
14
16
|
CSSOM.CSSStyleSheet = function CSSStyleSheet() {
|
|
15
17
|
CSSOM.StyleSheet.call(this);
|
|
16
|
-
this.cssRules =
|
|
18
|
+
this.cssRules = new CSSOM.CSSRuleList();
|
|
17
19
|
};
|
|
18
20
|
|
|
19
21
|
|
|
20
22
|
CSSOM.CSSStyleSheet.prototype = new CSSOM.StyleSheet();
|
|
21
23
|
CSSOM.CSSStyleSheet.prototype.constructor = CSSOM.CSSStyleSheet;
|
|
22
24
|
|
|
25
|
+
Object.defineProperty(CSSOM.CSSStyleSheet.prototype, "rules", {
|
|
26
|
+
get: function() {
|
|
27
|
+
return this.cssRules;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
23
30
|
|
|
24
31
|
/**
|
|
25
32
|
* Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
|
|
@@ -53,7 +60,10 @@ CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) {
|
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
var ruleToParse = String(rule);
|
|
56
|
-
var
|
|
63
|
+
var parseErrors = [];
|
|
64
|
+
var parsedSheet = CSSOM.parse(ruleToParse, undefined, function(err) {
|
|
65
|
+
parseErrors.push(err);
|
|
66
|
+
} );
|
|
57
67
|
if (parsedSheet.cssRules.length !== 1) {
|
|
58
68
|
errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');
|
|
59
69
|
}
|
|
@@ -121,12 +131,21 @@ CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) {
|
|
|
121
131
|
'HierarchyRequestError');
|
|
122
132
|
}
|
|
123
133
|
|
|
134
|
+
// Cannot insert if there are already non-special rules
|
|
135
|
+
if (firstNonImportNamespaceIndex < this.cssRules.length) {
|
|
136
|
+
errorUtils.throwError(this, 'DOMException',
|
|
137
|
+
"Failed to execute 'insertRule' on '" + this.constructor.name + "': Failed to insert the rule.",
|
|
138
|
+
'InvalidStateError');
|
|
139
|
+
}
|
|
140
|
+
|
|
124
141
|
// Cannot insert after other types of rules
|
|
125
142
|
if (index > firstNonImportNamespaceIndex) {
|
|
126
143
|
errorUtils.throwError(this, 'DOMException',
|
|
127
144
|
"Failed to execute 'insertRule' on '" + this.constructor.name + "': Failed to insert the rule.",
|
|
128
145
|
'HierarchyRequestError');
|
|
129
146
|
}
|
|
147
|
+
|
|
148
|
+
|
|
130
149
|
} else if (cssRule.constructor.name === 'CSSLayerStatementRule') {
|
|
131
150
|
// @layer statement rules can be inserted anywhere before @import and @namespace
|
|
132
151
|
// No additional restrictions beyond what's already handled
|
|
@@ -143,6 +162,10 @@ CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) {
|
|
|
143
162
|
"Failed to execute 'insertRule' on '" + this.constructor.name + "': Failed to insert the rule.",
|
|
144
163
|
'HierarchyRequestError');
|
|
145
164
|
}
|
|
165
|
+
|
|
166
|
+
if (parseErrors.length !== 0) {
|
|
167
|
+
errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');
|
|
168
|
+
}
|
|
146
169
|
}
|
|
147
170
|
|
|
148
171
|
cssRule.__parentStyleSheet = this;
|
|
@@ -150,6 +173,13 @@ CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) {
|
|
|
150
173
|
return index;
|
|
151
174
|
};
|
|
152
175
|
|
|
176
|
+
CSSOM.CSSStyleSheet.prototype.addRule = function(selector, styleBlock, index) {
|
|
177
|
+
if (index === void 0) {
|
|
178
|
+
index = this.cssRules.length;
|
|
179
|
+
}
|
|
180
|
+
this.insertRule(selector + "{" + styleBlock + "}", index);
|
|
181
|
+
return -1;
|
|
182
|
+
};
|
|
153
183
|
|
|
154
184
|
/**
|
|
155
185
|
* Used to delete a rule from the style sheet.
|
|
@@ -175,17 +205,27 @@ CSSOM.CSSStyleSheet.prototype.deleteRule = function(index) {
|
|
|
175
205
|
if (index >= this.cssRules.length) {
|
|
176
206
|
errorUtils.throwIndexError(this, 'deleteRule', this.constructor.name, index, this.cssRules.length);
|
|
177
207
|
}
|
|
178
|
-
if (this.cssRules[index]
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
208
|
+
if (this.cssRules[index]) {
|
|
209
|
+
if (this.cssRules[index].constructor.name == "CSSNamespaceRule") {
|
|
210
|
+
var shouldContinue = this.cssRules.every(function (rule) {
|
|
211
|
+
return ['CSSImportRule','CSSLayerStatementRule','CSSNamespaceRule'].indexOf(rule.constructor.name) !== -1
|
|
212
|
+
});
|
|
213
|
+
if (!shouldContinue) {
|
|
214
|
+
errorUtils.throwError(this, 'DOMException', "Failed to execute 'deleteRule' on '" + this.constructor.name + "': Failed to delete rule.", "InvalidStateError");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (this.cssRules[index].constructor.name == "CSSImportRule") {
|
|
218
|
+
this.cssRules[index].styleSheet.__parentStyleSheet = null;
|
|
184
219
|
}
|
|
220
|
+
|
|
221
|
+
this.cssRules[index].__parentStyleSheet = null;
|
|
185
222
|
}
|
|
186
223
|
this.cssRules.splice(index, 1);
|
|
187
224
|
};
|
|
188
225
|
|
|
226
|
+
CSSOM.CSSStyleSheet.prototype.removeRule = function(index) {
|
|
227
|
+
this.deleteRule(index);
|
|
228
|
+
};
|
|
189
229
|
|
|
190
230
|
/**
|
|
191
231
|
* NON-STANDARD
|
package/lib/CSSSupportsRule.js
CHANGED
package/lib/StyleSheet.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
//.CommonJS
|
|
2
|
-
var CSSOM = {
|
|
2
|
+
var CSSOM = {
|
|
3
|
+
MediaList: require("./MediaList").MediaList
|
|
4
|
+
};
|
|
3
5
|
///CommonJS
|
|
4
6
|
|
|
5
7
|
|
|
@@ -8,10 +10,23 @@ var CSSOM = {};
|
|
|
8
10
|
* @see http://dev.w3.org/csswg/cssom/#the-stylesheet-interface
|
|
9
11
|
*/
|
|
10
12
|
CSSOM.StyleSheet = function StyleSheet() {
|
|
13
|
+
this.__media = new CSSOM.MediaList();
|
|
11
14
|
this.__parentStyleSheet = null;
|
|
12
15
|
};
|
|
13
16
|
|
|
14
17
|
Object.defineProperties(CSSOM.StyleSheet.prototype, {
|
|
18
|
+
media: {
|
|
19
|
+
get: function() {
|
|
20
|
+
return this.__media;
|
|
21
|
+
},
|
|
22
|
+
set: function(value) {
|
|
23
|
+
if (typeof value === "string") {
|
|
24
|
+
this.__media.mediaText = value;
|
|
25
|
+
} else {
|
|
26
|
+
this.__media = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
15
30
|
parentStyleSheet: {
|
|
16
31
|
get: function() {
|
|
17
32
|
return this.__parentStyleSheet;
|
package/lib/clone.js
CHANGED
|
@@ -12,6 +12,7 @@ var CSSOM = {
|
|
|
12
12
|
CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
|
|
13
13
|
CSSKeyframeRule: require('./CSSKeyframeRule').CSSKeyframeRule,
|
|
14
14
|
CSSKeyframesRule: require('./CSSKeyframesRule').CSSKeyframesRule,
|
|
15
|
+
CSSScopeRule: require('./CSSScopeRule').CSSScopeRule,
|
|
15
16
|
CSSLayerBlockRule: require('./CSSLayerBlockRule').CSSLayerBlockRule,
|
|
16
17
|
CSSLayerStatementRule: require('./CSSLayerStatementRule').CSSLayerStatementRule
|
|
17
18
|
};
|
package/lib/index.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.CSSStyleDeclaration = require('./CSSStyleDeclaration').CSSStyleDeclarati
|
|
|
7
7
|
require('./cssstyleTryCatchBlock');
|
|
8
8
|
|
|
9
9
|
exports.CSSRule = require('./CSSRule').CSSRule;
|
|
10
|
+
exports.CSSRuleList = require('./CSSRuleList').CSSRuleList;
|
|
10
11
|
exports.CSSNestedDeclarations = require('./CSSNestedDeclarations').CSSNestedDeclarations;
|
|
11
12
|
exports.CSSGroupingRule = require('./CSSGroupingRule').CSSGroupingRule;
|
|
12
13
|
exports.CSSCounterStyleRule = require('./CSSCounterStyleRule').CSSCounterStyleRule;
|
|
@@ -29,7 +30,9 @@ exports.MatcherList = require('./MatcherList').MatcherList;
|
|
|
29
30
|
exports.CSSDocumentRule = require('./CSSDocumentRule').CSSDocumentRule;
|
|
30
31
|
exports.CSSValue = require('./CSSValue').CSSValue;
|
|
31
32
|
exports.CSSValueExpression = require('./CSSValueExpression').CSSValueExpression;
|
|
33
|
+
exports.CSSScopeRule = require('./CSSScopeRule').CSSScopeRule;
|
|
32
34
|
exports.CSSLayerBlockRule = require('./CSSLayerBlockRule').CSSLayerBlockRule;
|
|
33
35
|
exports.CSSLayerStatementRule = require('./CSSLayerStatementRule').CSSLayerStatementRule;
|
|
36
|
+
exports.CSSPageRule = require('./CSSPageRule').CSSPageRule;
|
|
34
37
|
exports.parse = require('./parse').parse;
|
|
35
38
|
exports.clone = require('./clone').clone;
|