@acemir/cssom 0.9.24 → 0.9.26

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.
@@ -24,9 +24,11 @@ CSSOM.CSSPageRule = function CSSPageRule() {
24
24
  this.__style.parentRule = this;
25
25
  };
26
26
 
27
- CSSOM.CSSPageRule.prototype = new CSSOM.CSSGroupingRule();
27
+ CSSOM.CSSPageRule.prototype = Object.create(CSSOM.CSSGroupingRule.prototype);
28
28
  CSSOM.CSSPageRule.prototype.constructor = CSSOM.CSSPageRule;
29
29
 
30
+ Object.setPrototypeOf(CSSOM.CSSPageRule, CSSOM.CSSGroupingRule);
31
+
30
32
  Object.defineProperty(CSSOM.CSSPageRule.prototype, "type", {
31
33
  value: 6,
32
34
  writable: false
@@ -121,160 +123,9 @@ Object.defineProperty(CSSOM.CSSPageRule.prototype, "cssText", {
121
123
  values = " {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
122
124
  }
123
125
  return "@page" + (this.selectorText ? " " + this.selectorText : "") + values;
124
- },
125
- set: function(cssText) {
126
- if (typeof value === "string") {
127
- var rule = CSSOM.CSSPageRule.parse(cssText);
128
- this.__style = rule.style;
129
- this.selectorText = rule.selectorText;
130
- }
131
126
  }
132
127
  });
133
128
 
134
- /**
135
- * NON-STANDARD
136
- * lightweight version of parse.js.
137
- * @param {string} ruleText
138
- * @return CSSPageRule
139
- */
140
- CSSOM.CSSPageRule.parse = function(ruleText) {
141
- var i = 0;
142
- var state = "selector";
143
- var index;
144
- var j = i;
145
- var buffer = "";
146
-
147
- var SIGNIFICANT_WHITESPACE = {
148
- "selector": true,
149
- "value": true
150
- };
151
-
152
- var pageRule = new CSSOM.CSSPageRule();
153
- var name, priority="";
154
-
155
- for (var character; (character = ruleText.charAt(i)); i++) {
156
-
157
- switch (character) {
158
-
159
- case " ":
160
- case "\t":
161
- case "\r":
162
- case "\n":
163
- case "\f":
164
- if (SIGNIFICANT_WHITESPACE[state]) {
165
- // Squash 2 or more white-spaces in the row into 1
166
- switch (ruleText.charAt(i - 1)) {
167
- case " ":
168
- case "\t":
169
- case "\r":
170
- case "\n":
171
- case "\f":
172
- break;
173
- default:
174
- buffer += " ";
175
- break;
176
- }
177
- }
178
- break;
179
-
180
- // String
181
- case '"':
182
- j = i + 1;
183
- index = ruleText.indexOf('"', j) + 1;
184
- if (!index) {
185
- throw '" is missing';
186
- }
187
- buffer += ruleText.slice(i, index);
188
- i = index - 1;
189
- break;
190
-
191
- case "'":
192
- j = i + 1;
193
- index = ruleText.indexOf("'", j) + 1;
194
- if (!index) {
195
- throw "' is missing";
196
- }
197
- buffer += ruleText.slice(i, index);
198
- i = index - 1;
199
- break;
200
-
201
- // Comment
202
- case "/":
203
- if (ruleText.charAt(i + 1) === "*") {
204
- i += 2;
205
- index = ruleText.indexOf("*/", i);
206
- if (index === -1) {
207
- throw new SyntaxError("Missing */");
208
- } else {
209
- i = index + 1;
210
- }
211
- } else {
212
- buffer += character;
213
- }
214
- break;
215
-
216
- case "{":
217
- if (state === "selector") {
218
- pageRule.selectorText = buffer.trim();
219
- buffer = "";
220
- state = "name";
221
- }
222
- break;
223
-
224
- case ":":
225
- if (state === "name") {
226
- name = buffer.trim();
227
- buffer = "";
228
- state = "value";
229
- } else {
230
- buffer += character;
231
- }
232
- break;
233
-
234
- case "!":
235
- if (state === "value" && ruleText.indexOf("!important", i) === i) {
236
- priority = "important";
237
- i += "important".length;
238
- } else {
239
- buffer += character;
240
- }
241
- break;
242
-
243
- case ";":
244
- if (state === "value") {
245
- pageRule.style.setProperty(name, buffer.trim(), priority);
246
- priority = "";
247
- buffer = "";
248
- state = "name";
249
- } else {
250
- buffer += character;
251
- }
252
- break;
253
-
254
- case "}":
255
- if (state === "value") {
256
- pageRule.style.setProperty(name, buffer.trim(), priority);
257
- priority = "";
258
- buffer = "";
259
- } else if (state === "name") {
260
- break;
261
- } else {
262
- buffer += character;
263
- }
264
- state = "selector";
265
- break;
266
-
267
- default:
268
- buffer += character;
269
- break;
270
-
271
- }
272
- }
273
-
274
- return pageRule;
275
-
276
- };
277
-
278
129
  //.CommonJS
279
130
  exports.CSSPageRule = CSSOM.CSSPageRule;
280
131
  ///CommonJS
package/lib/CSSRule.js CHANGED
@@ -43,6 +43,16 @@ Object.defineProperties(CSSOM.CSSRule.prototype, {
43
43
  enumerable: true
44
44
  },
45
45
 
46
+ cssText: {
47
+ get: function() {
48
+ // Default getter: subclasses should override this
49
+ return "";
50
+ },
51
+ set: function(cssText) {
52
+ return cssText;
53
+ }
54
+ },
55
+
46
56
  parentRule: {
47
57
  get: function() {
48
58
  return this.__parentRule
@@ -16,9 +16,10 @@ CSSOM.CSSScopeRule = function CSSScopeRule() {
16
16
  this.__end = null;
17
17
  };
18
18
 
19
- CSSOM.CSSScopeRule.prototype = new CSSOM.CSSGroupingRule();
19
+ CSSOM.CSSScopeRule.prototype = Object.create(CSSOM.CSSGroupingRule.prototype);
20
20
  CSSOM.CSSScopeRule.prototype.constructor = CSSOM.CSSScopeRule;
21
21
 
22
+ Object.setPrototypeOf(CSSOM.CSSScopeRule, CSSOM.CSSGroupingRule);
22
23
 
23
24
  Object.defineProperties(CSSOM.CSSScopeRule.prototype, {
24
25
  type: {
@@ -15,9 +15,16 @@ CSSOM.CSSStartingStyleRule = function CSSStartingStyleRule() {
15
15
  CSSOM.CSSGroupingRule.call(this);
16
16
  };
17
17
 
18
- CSSOM.CSSStartingStyleRule.prototype = new CSSOM.CSSGroupingRule();
18
+ CSSOM.CSSStartingStyleRule.prototype = Object.create(CSSOM.CSSGroupingRule.prototype);
19
19
  CSSOM.CSSStartingStyleRule.prototype.constructor = CSSOM.CSSStartingStyleRule;
20
- CSSOM.CSSStartingStyleRule.prototype.type = 1002;
20
+
21
+ Object.setPrototypeOf(CSSOM.CSSStartingStyleRule, CSSOM.CSSGroupingRule);
22
+
23
+ Object.defineProperty(CSSOM.CSSStartingStyleRule.prototype, "type", {
24
+ value: 1002,
25
+ writable: false
26
+ });
27
+
21
28
  //FIXME
22
29
  //CSSOM.CSSStartingStyleRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
23
30
  //CSSOM.CSSStartingStyleRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;
@@ -26,9 +26,11 @@ CSSOM.CSSStyleRule = function CSSStyleRule() {
26
26
  this.__style.parentRule = this;
27
27
  };
28
28
 
29
- CSSOM.CSSStyleRule.prototype = new CSSOM.CSSGroupingRule();
29
+ CSSOM.CSSStyleRule.prototype = Object.create(CSSOM.CSSGroupingRule.prototype);
30
30
  CSSOM.CSSStyleRule.prototype.constructor = CSSOM.CSSStyleRule;
31
31
 
32
+ Object.setPrototypeOf(CSSOM.CSSStyleRule, CSSOM.CSSGroupingRule);
33
+
32
34
  Object.defineProperty(CSSOM.CSSStyleRule.prototype, "type", {
33
35
  value: 1,
34
36
  writable: false
@@ -92,162 +94,9 @@ Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
92
94
  text = "";
93
95
  }
94
96
  return text;
95
- },
96
- set: function(cssText) {
97
- if (typeof cssText === "string") {
98
- var rule = CSSOM.CSSStyleRule.parse(cssText);
99
- this.__style = rule.style;
100
- this.selectorText = rule.selectorText;
101
- }
102
97
  }
103
98
  });
104
99
 
105
-
106
- /**
107
- * NON-STANDARD
108
- * lightweight version of parse.js.
109
- * @param {string} ruleText
110
- * @return CSSStyleRule
111
- */
112
- CSSOM.CSSStyleRule.parse = function(ruleText) {
113
- var i = 0;
114
- var state = "selector";
115
- var index;
116
- var j = i;
117
- var buffer = "";
118
-
119
- var SIGNIFICANT_WHITESPACE = {
120
- "selector": true,
121
- "value": true
122
- };
123
-
124
- var styleRule = new CSSOM.CSSStyleRule();
125
- var name, priority="";
126
-
127
- for (var character; (character = ruleText.charAt(i)); i++) {
128
-
129
- switch (character) {
130
-
131
- case " ":
132
- case "\t":
133
- case "\r":
134
- case "\n":
135
- case "\f":
136
- if (SIGNIFICANT_WHITESPACE[state]) {
137
- // Squash 2 or more white-spaces in the row into 1
138
- switch (ruleText.charAt(i - 1)) {
139
- case " ":
140
- case "\t":
141
- case "\r":
142
- case "\n":
143
- case "\f":
144
- break;
145
- default:
146
- buffer += " ";
147
- break;
148
- }
149
- }
150
- break;
151
-
152
- // String
153
- case '"':
154
- j = i + 1;
155
- index = ruleText.indexOf('"', j) + 1;
156
- if (!index) {
157
- throw '" is missing';
158
- }
159
- buffer += ruleText.slice(i, index);
160
- i = index - 1;
161
- break;
162
-
163
- case "'":
164
- j = i + 1;
165
- index = ruleText.indexOf("'", j) + 1;
166
- if (!index) {
167
- throw "' is missing";
168
- }
169
- buffer += ruleText.slice(i, index);
170
- i = index - 1;
171
- break;
172
-
173
- // Comment
174
- case "/":
175
- if (ruleText.charAt(i + 1) === "*") {
176
- i += 2;
177
- index = ruleText.indexOf("*/", i);
178
- if (index === -1) {
179
- throw new SyntaxError("Missing */");
180
- } else {
181
- i = index + 1;
182
- }
183
- } else {
184
- buffer += character;
185
- }
186
- break;
187
-
188
- case "{":
189
- if (state === "selector") {
190
- styleRule.selectorText = buffer.trim();
191
- buffer = "";
192
- state = "name";
193
- }
194
- break;
195
-
196
- case ":":
197
- if (state === "name") {
198
- name = buffer.trim();
199
- buffer = "";
200
- state = "value";
201
- } else {
202
- buffer += character;
203
- }
204
- break;
205
-
206
- case "!":
207
- if (state === "value" && ruleText.indexOf("!important", i) === i) {
208
- priority = "important";
209
- i += "important".length;
210
- } else {
211
- buffer += character;
212
- }
213
- break;
214
-
215
- case ";":
216
- if (state === "value") {
217
- styleRule.style.setProperty(name, buffer.trim(), priority);
218
- priority = "";
219
- buffer = "";
220
- state = "name";
221
- } else {
222
- buffer += character;
223
- }
224
- break;
225
-
226
- case "}":
227
- if (state === "value") {
228
- styleRule.style.setProperty(name, buffer.trim(), priority);
229
- priority = "";
230
- buffer = "";
231
- } else if (state === "name") {
232
- break;
233
- } else {
234
- buffer += character;
235
- }
236
- state = "selector";
237
- break;
238
-
239
- default:
240
- buffer += character;
241
- break;
242
-
243
- }
244
- }
245
-
246
- return styleRule;
247
-
248
- };
249
-
250
-
251
100
  //.CommonJS
252
101
  exports.CSSStyleRule = CSSOM.CSSStyleRule;
253
102
  ///CommonJS
@@ -1,5 +1,6 @@
1
1
  //.CommonJS
2
2
  var CSSOM = {
3
+ getGlobalObject: require('./CSSOM').getGlobalObject,
3
4
  MediaList: require("./MediaList").MediaList,
4
5
  StyleSheet: require("./StyleSheet").StyleSheet,
5
6
  CSSRuleList: require("./CSSRuleList").CSSRuleList,
@@ -11,21 +12,52 @@ var errorUtils = require("./errorUtils").errorUtils;
11
12
 
12
13
  /**
13
14
  * @constructor
15
+ * @param {CSSStyleSheetInit} [opts] - CSSStyleSheetInit options.
16
+ * @param {string} [opts.baseURL] - The base URL of the stylesheet.
17
+ * @param {boolean} [opts.disabled] - The disabled attribute of the stylesheet.
18
+ * @param {MediaList | string} [opts.media] - The media attribute of the stylesheet.
14
19
  * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet
15
20
  */
16
- CSSOM.CSSStyleSheet = function CSSStyleSheet() {
21
+ CSSOM.CSSStyleSheet = function CSSStyleSheet(opts) {
17
22
  CSSOM.StyleSheet.call(this);
18
23
  this.__constructed = true;
19
- this.cssRules = new CSSOM.CSSRuleList();
24
+ this.__cssRules = new CSSOM.CSSRuleList();
25
+ this.__ownerRule = null;
26
+
27
+ if (opts && typeof opts === "object") {
28
+ if (opts.baseURL && typeof opts.baseURL === "string") {
29
+ this.__baseURL = opts.baseURL;
30
+ }
31
+ if (opts.media && typeof opts.media === "string") {
32
+ this.media.mediaText = opts.media;
33
+ }
34
+ if (typeof opts.disabled === "boolean") {
35
+ this.disabled = opts.disabled;
36
+ }
37
+ }
20
38
  };
21
39
 
22
40
 
23
- CSSOM.CSSStyleSheet.prototype = new CSSOM.StyleSheet();
41
+ CSSOM.CSSStyleSheet.prototype = Object.create(CSSOM.StyleSheet.prototype);
24
42
  CSSOM.CSSStyleSheet.prototype.constructor = CSSOM.CSSStyleSheet;
25
43
 
44
+ Object.setPrototypeOf(CSSOM.CSSStyleSheet, CSSOM.StyleSheet);
45
+
46
+ Object.defineProperty(CSSOM.CSSStyleSheet.prototype, "cssRules", {
47
+ get: function() {
48
+ return this.__cssRules;
49
+ }
50
+ });
51
+
26
52
  Object.defineProperty(CSSOM.CSSStyleSheet.prototype, "rules", {
27
53
  get: function() {
28
- return this.cssRules;
54
+ return this.__cssRules;
55
+ }
56
+ });
57
+
58
+ Object.defineProperty(CSSOM.CSSStyleSheet.prototype, "ownerRule", {
59
+ get: function() {
60
+ return this.__ownerRule;
29
61
  }
30
62
  });
31
63
 
@@ -225,6 +257,9 @@ CSSOM.CSSStyleSheet.prototype.deleteRule = function(index) {
225
257
  };
226
258
 
227
259
  CSSOM.CSSStyleSheet.prototype.removeRule = function(index) {
260
+ if (index === void 0) {
261
+ index = 0;
262
+ }
228
263
  this.deleteRule(index);
229
264
  };
230
265
 
@@ -237,11 +272,17 @@ CSSOM.CSSStyleSheet.prototype.removeRule = function(index) {
237
272
  */
238
273
  CSSOM.CSSStyleSheet.prototype.replace = function(text) {
239
274
  var _Promise;
240
- if (this.__globalObject) {
241
- _Promise = this.__globalObject['Promise'];
275
+ if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()['Promise']) {
276
+ _Promise = CSSOM.getGlobalObject()['Promise'];
242
277
  } else {
243
278
  _Promise = Promise;
244
279
  }
280
+ var _setTimeout;
281
+ if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()['setTimeout']) {
282
+ _setTimeout = CSSOM.getGlobalObject()['setTimeout'];
283
+ } else {
284
+ _setTimeout = setTimeout;
285
+ }
245
286
  var sheet = this;
246
287
  return new _Promise(function (resolve, reject) {
247
288
  // If the constructed flag is not set, or the disallow modification flag is set, throw a NotAllowedError DOMException.
@@ -254,7 +295,7 @@ CSSOM.CSSStyleSheet.prototype.replace = function(text) {
254
295
  sheet.__disallowModification = true;
255
296
 
256
297
  // In parallel, do these steps:
257
- setTimeout(function() {
298
+ _setTimeout(function() {
258
299
  // Let rules be the result of running parse a stylesheet's contents from text.
259
300
  var rules = new CSSOM.CSSRuleList();
260
301
  CSSOM.parse(text, { styleSheet: sheet, cssRules: rules });
@@ -268,7 +309,7 @@ CSSOM.CSSStyleSheet.prototype.replace = function(text) {
268
309
  }
269
310
  }
270
311
  // Set sheet's CSS rules to rules.
271
- sheet.cssRules = rules;
312
+ sheet.__cssRules.splice.apply(sheet.__cssRules, [0, sheet.__cssRules.length].concat(rules));
272
313
  // Unset sheet’s disallow modification flag.
273
314
  delete sheet.__disallowModification;
274
315
  // Resolve promise with sheet.
@@ -303,7 +344,7 @@ CSSOM.CSSStyleSheet.prototype.replaceSync = function(text) {
303
344
  }
304
345
  }
305
346
  // Set sheet's CSS rules to rules.
306
- sheet.cssRules = rules;
347
+ sheet.__cssRules.splice.apply(sheet.__cssRules, [0, sheet.__cssRules.length].concat(rules));
307
348
  }
308
349
 
309
350
  /**
@@ -16,9 +16,15 @@ CSSOM.CSSSupportsRule = function CSSSupportsRule() {
16
16
  CSSOM.CSSConditionRule.call(this);
17
17
  };
18
18
 
19
- CSSOM.CSSSupportsRule.prototype = new CSSOM.CSSConditionRule();
19
+ CSSOM.CSSSupportsRule.prototype = Object.create(CSSOM.CSSConditionRule.prototype);
20
20
  CSSOM.CSSSupportsRule.prototype.constructor = CSSOM.CSSSupportsRule;
21
- CSSOM.CSSSupportsRule.prototype.type = 12;
21
+
22
+ Object.setPrototypeOf(CSSOM.CSSSupportsRule, CSSOM.CSSConditionRule);
23
+
24
+ Object.defineProperty(CSSOM.CSSSupportsRule.prototype, "type", {
25
+ value: 12,
26
+ writable: false
27
+ });
22
28
 
23
29
  Object.defineProperty(CSSOM.CSSSupportsRule.prototype, "cssText", {
24
30
  get: function() {
@@ -15,9 +15,11 @@ CSSOM.CSSValueExpression = function CSSValueExpression(token, idx) {
15
15
  this._idx = idx;
16
16
  };
17
17
 
18
- CSSOM.CSSValueExpression.prototype = new CSSOM.CSSValue();
18
+ CSSOM.CSSValueExpression.prototype = Object.create(CSSOM.CSSValue.prototype);
19
19
  CSSOM.CSSValueExpression.prototype.constructor = CSSOM.CSSValueExpression;
20
20
 
21
+ Object.setPrototypeOf(CSSOM.CSSValueExpression, CSSOM.CSSValue);
22
+
21
23
  /**
22
24
  * parse css expression() value
23
25
  *
package/lib/MediaList.js CHANGED
@@ -26,12 +26,20 @@ CSSOM.MediaList.prototype = {
26
26
  * @param {string} value
27
27
  */
28
28
  set mediaText(value) {
29
- var values = value.split(",").filter(function(text){
30
- return !!text;
31
- });
32
- var length = this.length = values.length;
33
- for (var i=0; i<length; i++) {
34
- this[i] = values[i].trim();
29
+ if (typeof value === "string") {
30
+ var values = value.split(",").filter(function(text){
31
+ return !!text;
32
+ });
33
+ var length = this.length = values.length;
34
+ for (var i=0; i<length; i++) {
35
+ this[i] = values[i].trim();
36
+ }
37
+ } else if (value === null) {
38
+ var length = this.length;
39
+ for (var i = 0; i < length; i++) {
40
+ delete this[i];
41
+ }
42
+ this.length = 0;
35
43
  }
36
44
  },
37
45
 
@@ -53,8 +61,15 @@ CSSOM.MediaList.prototype = {
53
61
  if (index !== -1) {
54
62
  Array.prototype.splice.call(this, index, 1);
55
63
  }
56
- }
64
+ },
57
65
 
66
+ item: function(index) {
67
+ return this[index] || null;
68
+ },
69
+
70
+ toString: function() {
71
+ return this.mediaText;
72
+ }
58
73
  };
59
74
 
60
75
 
package/lib/StyleSheet.js CHANGED
@@ -6,15 +6,38 @@ var CSSOM = {
6
6
 
7
7
 
8
8
  /**
9
- * @constructor
10
9
  * @see http://dev.w3.org/csswg/cssom/#the-stylesheet-interface
11
10
  */
12
11
  CSSOM.StyleSheet = function StyleSheet() {
12
+ this.__href = null;
13
+ this.__ownerNode = null;
14
+ this.__title = null;
13
15
  this.__media = new CSSOM.MediaList();
14
16
  this.__parentStyleSheet = null;
17
+ this.disabled = false;
15
18
  };
16
19
 
17
20
  Object.defineProperties(CSSOM.StyleSheet.prototype, {
21
+ type: {
22
+ get: function() {
23
+ return "text/css";
24
+ }
25
+ },
26
+ href: {
27
+ get: function() {
28
+ return this.__href;
29
+ }
30
+ },
31
+ ownerNode: {
32
+ get: function() {
33
+ return this.__ownerNode;
34
+ }
35
+ },
36
+ title: {
37
+ get: function() {
38
+ return this.__title;
39
+ }
40
+ },
18
41
  media: {
19
42
  get: function() {
20
43
  return this.__media;
package/lib/errorUtils.js CHANGED
@@ -1,3 +1,9 @@
1
+ //.CommonJS
2
+ var CSSOM = {
3
+ getGlobalObject: require('./CSSOM').getGlobalObject
4
+ }
5
+ ///CommonJS
6
+
1
7
  // Utility functions for CSSOM error handling
2
8
 
3
9
  /**
@@ -10,19 +16,8 @@
10
16
  * @return {Function} The error constructor
11
17
  */
12
18
  function getErrorConstructor(context, errorType) {
13
- // Try parentStyleSheet.__globalObject first
14
- if (context.parentStyleSheet && context.parentStyleSheet.__globalObject && context.parentStyleSheet.__globalObject[errorType]) {
15
- return context.parentStyleSheet.__globalObject[errorType];
16
- }
17
-
18
- // Try __parentStyleSheet (alternative naming)
19
- if (context.__parentStyleSheet && context.__parentStyleSheet.__globalObject && context.__parentStyleSheet.__globalObject[errorType]) {
20
- return context.__parentStyleSheet.__globalObject[errorType];
21
- }
22
-
23
- // Try __globalObject on the context itself
24
- if (context.__globalObject && context.__globalObject[errorType]) {
25
- return context.__globalObject[errorType];
19
+ if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()[errorType]) {
20
+ return CSSOM.getGlobalObject()[errorType];
26
21
  }
27
22
 
28
23
  // Fall back to native constructor
package/lib/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ exports.setup = require('./CSSOM').setup;
4
+
3
5
  require('./errorUtils');
4
6
 
5
7
  exports.CSSStyleDeclaration = require('./CSSStyleDeclaration').CSSStyleDeclaration;