@acemir/cssom 0.9.26 → 0.9.27

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 CHANGED
@@ -1,17 +1,56 @@
1
- var __globalObject = null;
2
-
3
1
  var CSSOM = {
4
- setup: function(opts) {
5
- if (opts.globalObject) {
6
- __globalObject = opts.globalObject;
7
- }
8
- },
9
- getGlobalObject: function() {
10
- return __globalObject;
2
+ /**
3
+ * Creates and configures a new CSSOM instance with the specified options.
4
+ *
5
+ * @param {Object} opts - Configuration options for the CSSOM instance
6
+ * @param {Object} [opts.globalObject] - Optional global object to be assigned to CSSOM objects prototype
7
+ * @returns {Object} A new CSSOM instance with the applied configuration
8
+ * @description
9
+ * This method creates a new instance of CSSOM and optionally
10
+ * configures CSSStyleSheet with a global object reference. When a globalObject is provided
11
+ * and CSSStyleSheet exists on the instance, it creates a new CSSStyleSheet constructor
12
+ * using a factory function and assigns the globalObject to its prototype's __globalObject property.
13
+ */
14
+ setup: function (opts) {
15
+ var instance = Object.create(this);
16
+ if (opts.globalObject) {
17
+ if (instance.CSSStyleSheet) {
18
+ const factoryCSSStyleSheet = createFunctionFactory(instance.CSSStyleSheet);
19
+ const CSSStyleSheet = factoryCSSStyleSheet();
20
+ CSSStyleSheet.prototype.__globalObject = opts.globalObject;
21
+
22
+ instance.CSSStyleSheet = CSSStyleSheet;
23
+ }
11
24
  }
25
+ return instance;
26
+ }
12
27
  };
13
28
 
29
+ function createFunctionFactory(fn) {
30
+ return function() {
31
+ // Create a new function that delegates to the original
32
+ var newFn = function() {
33
+ return fn.apply(this, arguments);
34
+ };
14
35
 
36
+ // Copy prototype chain
37
+ Object.setPrototypeOf(newFn, Object.getPrototypeOf(fn));
38
+
39
+ // Copy own properties
40
+ for (var key in fn) {
41
+ if (Object.prototype.hasOwnProperty.call(fn, key)) {
42
+ newFn[key] = fn[key];
43
+ }
44
+ }
45
+
46
+ // Clone the .prototype object for constructor-like behavior
47
+ if (fn.prototype) {
48
+ newFn.prototype = Object.create(fn.prototype);
49
+ }
50
+
51
+ return newFn;
52
+ };
53
+ }
15
54
 
16
55
 
17
56
 
@@ -27,8 +66,19 @@ var CSSOM = {
27
66
  * @return {Function} The error constructor
28
67
  */
29
68
  function getErrorConstructor(context, errorType) {
30
- if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()[errorType]) {
31
- return CSSOM.getGlobalObject()[errorType];
69
+ // Try parentStyleSheet.__globalObject first
70
+ if (context.parentStyleSheet && context.parentStyleSheet.__globalObject && context.parentStyleSheet.__globalObject[errorType]) {
71
+ return context.parentStyleSheet.__globalObject[errorType];
72
+ }
73
+
74
+ // Try __parentStyleSheet (alternative naming)
75
+ if (context.__parentStyleSheet && context.__parentStyleSheet.__globalObject && context.__parentStyleSheet.__globalObject[errorType]) {
76
+ return context.__parentStyleSheet.__globalObject[errorType];
77
+ }
78
+
79
+ // Try __globalObject on the context itself
80
+ if (context.__globalObject && context.__globalObject[errorType]) {
81
+ return context.__globalObject[errorType];
32
82
  }
33
83
 
34
84
  // Fall back to native constructor
@@ -1826,14 +1876,14 @@ CSSOM.CSSStyleSheet.prototype.removeRule = function(index) {
1826
1876
  */
1827
1877
  CSSOM.CSSStyleSheet.prototype.replace = function(text) {
1828
1878
  var _Promise;
1829
- if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()['Promise']) {
1830
- _Promise = CSSOM.getGlobalObject()['Promise'];
1879
+ if (this.__globalObject && this.__globalObject['Promise']) {
1880
+ _Promise = this.__globalObject['Promise'];
1831
1881
  } else {
1832
1882
  _Promise = Promise;
1833
1883
  }
1834
1884
  var _setTimeout;
1835
- if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()['setTimeout']) {
1836
- _setTimeout = CSSOM.getGlobalObject()['setTimeout'];
1885
+ if (this.__globalObject && this.__globalObject['setTimeout']) {
1886
+ _setTimeout = this.__globalObject['setTimeout'];
1837
1887
  } else {
1838
1888
  _setTimeout = setTimeout;
1839
1889
  }
@@ -2924,17 +2974,17 @@ Object.defineProperty(CSSOM.CSSPageRule.prototype, "cssText", {
2924
2974
 
2925
2975
 
2926
2976
  /**
2927
- * Parses a CSS string and returns a CSSOM.CSSStyleSheet object representing the parsed stylesheet.
2977
+ * Parses a CSS string and returns a `CSSStyleSheet` object representing the parsed stylesheet.
2928
2978
  *
2929
2979
  * @param {string} token - The CSS string to parse.
2930
2980
  * @param {object} [opts] - Optional parsing options.
2931
- * @param {object} [opts.globalObject] - @deprecated This property will be removed in the next release. Use CSSOM.setup({ globalObject }) instead. - An optional global object to override globals and window. Useful on jsdom webplatform tests.
2981
+ * @param {object} [opts.globalObject] - An optional global object to prioritize over the window object. Useful on jsdom webplatform tests.
2932
2982
  * @param {Element | ProcessingInstruction} [opts.ownerNode] - The owner node of the stylesheet.
2933
2983
  * @param {CSSRule} [opts.ownerRule] - The owner rule of the stylesheet.
2934
2984
  * @param {CSSOM.CSSStyleSheet} [opts.styleSheet] - Reuse a style sheet instead of creating a new one (e.g. as `parentStyleSheet`)
2935
2985
  * @param {CSSOM.CSSRuleList} [opts.cssRules] - Prepare all rules in this list instead of mutating the style sheet continually
2936
2986
  * @param {function|boolean} [errorHandler] - Optional error handler function or `true` to use `console.error`.
2937
- * @returns {CSSOM.CSSStyleSheet} The parsed CSSStyleSheet object.
2987
+ * @returns {CSSOM.CSSStyleSheet} The parsed `CSSStyleSheet` object.
2938
2988
  */
2939
2989
  CSSOM.parse = function parse(token, opts, errorHandler) {
2940
2990
  errorHandler = errorHandler === true ? (console && console.error) : errorHandler;
@@ -2983,7 +3033,11 @@ CSSOM.parse = function parse(token, opts, errorHandler) {
2983
3033
  if (opts && opts.styleSheet) {
2984
3034
  styleSheet = opts.styleSheet;
2985
3035
  } else {
2986
- styleSheet = new CSSOM.CSSStyleSheet()
3036
+ if (opts && opts.globalObject && opts.globalObject.CSSStyleSheet) {
3037
+ styleSheet = new opts.globalObject.CSSStyleSheet();
3038
+ } else {
3039
+ styleSheet = new CSSOM.CSSStyleSheet();
3040
+ }
2987
3041
  styleSheet.__constructed = false;
2988
3042
  }
2989
3043
 
@@ -2994,10 +3048,6 @@ CSSOM.parse = function parse(token, opts, errorHandler) {
2994
3048
  topScope = styleSheet;
2995
3049
  }
2996
3050
 
2997
- if (opts && opts.globalObject) {
2998
- CSSOM.setup({ globalObject: opts.globalObject });
2999
- }
3000
-
3001
3051
  if (opts && opts.ownerNode) {
3002
3052
  styleSheet.__ownerNode = opts.ownerNode;
3003
3053
  var ownerNodeMedia = opts.ownerNode.media || (opts.ownerNode.getAttribute && opts.ownerNode.getAttribute("media"));
@@ -5029,6 +5079,9 @@ CSSOM.parse = function parse(token, opts, errorHandler) {
5029
5079
  });
5030
5080
  if (isValid) {
5031
5081
  importRule = new CSSOM.CSSImportRule();
5082
+ if (opts && opts.globalObject && opts.globalObject.CSSStyleSheet) {
5083
+ importRule.styleSheet = new opts.globalObject.CSSStyleSheet();
5084
+ }
5032
5085
  importRule.__parentStyleSheet = importRule.styleSheet.__parentStyleSheet = styleSheet;
5033
5086
  importRule.parse(buffer + character);
5034
5087
  topScope.cssRules.push(importRule);
package/lib/CSSOM.js CHANGED
@@ -1,18 +1,58 @@
1
- var __globalObject = null;
2
-
3
1
  var CSSOM = {
4
- setup: function(opts) {
5
- if (opts.globalObject) {
6
- __globalObject = opts.globalObject;
7
- }
8
- },
9
- getGlobalObject: function() {
10
- return __globalObject;
2
+ /**
3
+ * Creates and configures a new CSSOM instance with the specified options.
4
+ *
5
+ * @param {Object} opts - Configuration options for the CSSOM instance
6
+ * @param {Object} [opts.globalObject] - Optional global object to be assigned to CSSOM objects prototype
7
+ * @returns {Object} A new CSSOM instance with the applied configuration
8
+ * @description
9
+ * This method creates a new instance of CSSOM and optionally
10
+ * configures CSSStyleSheet with a global object reference. When a globalObject is provided
11
+ * and CSSStyleSheet exists on the instance, it creates a new CSSStyleSheet constructor
12
+ * using a factory function and assigns the globalObject to its prototype's __globalObject property.
13
+ */
14
+ setup: function (opts) {
15
+ var instance = Object.create(this);
16
+ if (opts.globalObject) {
17
+ if (instance.CSSStyleSheet) {
18
+ const factoryCSSStyleSheet = createFunctionFactory(instance.CSSStyleSheet);
19
+ const CSSStyleSheet = factoryCSSStyleSheet();
20
+ CSSStyleSheet.prototype.__globalObject = opts.globalObject;
21
+
22
+ instance.CSSStyleSheet = CSSStyleSheet;
23
+ }
11
24
  }
25
+ return instance;
26
+ }
12
27
  };
13
28
 
29
+ function createFunctionFactory(fn) {
30
+ return function() {
31
+ // Create a new function that delegates to the original
32
+ var newFn = function() {
33
+ return fn.apply(this, arguments);
34
+ };
35
+
36
+ // Copy prototype chain
37
+ Object.setPrototypeOf(newFn, Object.getPrototypeOf(fn));
38
+
39
+ // Copy own properties
40
+ for (var key in fn) {
41
+ if (Object.prototype.hasOwnProperty.call(fn, key)) {
42
+ newFn[key] = fn[key];
43
+ }
44
+ }
45
+
46
+ // Clone the .prototype object for constructor-like behavior
47
+ if (fn.prototype) {
48
+ newFn.prototype = Object.create(fn.prototype);
49
+ }
50
+
51
+ return newFn;
52
+ };
53
+ }
54
+
14
55
  //.CommonJS
15
- exports.setup = CSSOM.setup;
16
- exports.getGlobalObject = CSSOM.getGlobalObject;
56
+ module.exports = CSSOM;
17
57
  ///CommonJS
18
58
 
@@ -1,6 +1,5 @@
1
1
  //.CommonJS
2
2
  var CSSOM = {
3
- getGlobalObject: require('./CSSOM').getGlobalObject,
4
3
  MediaList: require("./MediaList").MediaList,
5
4
  StyleSheet: require("./StyleSheet").StyleSheet,
6
5
  CSSRuleList: require("./CSSRuleList").CSSRuleList,
@@ -272,14 +271,14 @@ CSSOM.CSSStyleSheet.prototype.removeRule = function(index) {
272
271
  */
273
272
  CSSOM.CSSStyleSheet.prototype.replace = function(text) {
274
273
  var _Promise;
275
- if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()['Promise']) {
276
- _Promise = CSSOM.getGlobalObject()['Promise'];
274
+ if (this.__globalObject && this.__globalObject['Promise']) {
275
+ _Promise = this.__globalObject['Promise'];
277
276
  } else {
278
277
  _Promise = Promise;
279
278
  }
280
279
  var _setTimeout;
281
- if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()['setTimeout']) {
282
- _setTimeout = CSSOM.getGlobalObject()['setTimeout'];
280
+ if (this.__globalObject && this.__globalObject['setTimeout']) {
281
+ _setTimeout = this.__globalObject['setTimeout'];
283
282
  } else {
284
283
  _setTimeout = setTimeout;
285
284
  }
package/lib/errorUtils.js CHANGED
@@ -1,9 +1,3 @@
1
- //.CommonJS
2
- var CSSOM = {
3
- getGlobalObject: require('./CSSOM').getGlobalObject
4
- }
5
- ///CommonJS
6
-
7
1
  // Utility functions for CSSOM error handling
8
2
 
9
3
  /**
@@ -16,8 +10,19 @@ var CSSOM = {
16
10
  * @return {Function} The error constructor
17
11
  */
18
12
  function getErrorConstructor(context, errorType) {
19
- if (CSSOM.getGlobalObject() && CSSOM.getGlobalObject()[errorType]) {
20
- return CSSOM.getGlobalObject()[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];
21
26
  }
22
27
 
23
28
  // Fall back to native constructor
package/lib/parse.js CHANGED
@@ -1,21 +1,19 @@
1
1
  //.CommonJS
2
- var CSSOM = {
3
- setup: require('./CSSOM').setup
4
- };
2
+ var CSSOM = {};
5
3
  ///CommonJS
6
4
 
7
5
  /**
8
- * Parses a CSS string and returns a CSSOM.CSSStyleSheet object representing the parsed stylesheet.
6
+ * Parses a CSS string and returns a `CSSStyleSheet` object representing the parsed stylesheet.
9
7
  *
10
8
  * @param {string} token - The CSS string to parse.
11
9
  * @param {object} [opts] - Optional parsing options.
12
- * @param {object} [opts.globalObject] - @deprecated This property will be removed in the next release. Use CSSOM.setup({ globalObject }) instead. - An optional global object to override globals and window. Useful on jsdom webplatform tests.
10
+ * @param {object} [opts.globalObject] - An optional global object to prioritize over the window object. Useful on jsdom webplatform tests.
13
11
  * @param {Element | ProcessingInstruction} [opts.ownerNode] - The owner node of the stylesheet.
14
12
  * @param {CSSRule} [opts.ownerRule] - The owner rule of the stylesheet.
15
13
  * @param {CSSOM.CSSStyleSheet} [opts.styleSheet] - Reuse a style sheet instead of creating a new one (e.g. as `parentStyleSheet`)
16
14
  * @param {CSSOM.CSSRuleList} [opts.cssRules] - Prepare all rules in this list instead of mutating the style sheet continually
17
15
  * @param {function|boolean} [errorHandler] - Optional error handler function or `true` to use `console.error`.
18
- * @returns {CSSOM.CSSStyleSheet} The parsed CSSStyleSheet object.
16
+ * @returns {CSSOM.CSSStyleSheet} The parsed `CSSStyleSheet` object.
19
17
  */
20
18
  CSSOM.parse = function parse(token, opts, errorHandler) {
21
19
  errorHandler = errorHandler === true ? (console && console.error) : errorHandler;
@@ -64,7 +62,11 @@ CSSOM.parse = function parse(token, opts, errorHandler) {
64
62
  if (opts && opts.styleSheet) {
65
63
  styleSheet = opts.styleSheet;
66
64
  } else {
67
- styleSheet = new CSSOM.CSSStyleSheet()
65
+ if (opts && opts.globalObject && opts.globalObject.CSSStyleSheet) {
66
+ styleSheet = new opts.globalObject.CSSStyleSheet();
67
+ } else {
68
+ styleSheet = new CSSOM.CSSStyleSheet();
69
+ }
68
70
  styleSheet.__constructed = false;
69
71
  }
70
72
 
@@ -75,10 +77,6 @@ CSSOM.parse = function parse(token, opts, errorHandler) {
75
77
  topScope = styleSheet;
76
78
  }
77
79
 
78
- if (opts && opts.globalObject) {
79
- CSSOM.setup({ globalObject: opts.globalObject });
80
- }
81
-
82
80
  if (opts && opts.ownerNode) {
83
81
  styleSheet.__ownerNode = opts.ownerNode;
84
82
  var ownerNodeMedia = opts.ownerNode.media || (opts.ownerNode.getAttribute && opts.ownerNode.getAttribute("media"));
@@ -2110,6 +2108,9 @@ CSSOM.parse = function parse(token, opts, errorHandler) {
2110
2108
  });
2111
2109
  if (isValid) {
2112
2110
  importRule = new CSSOM.CSSImportRule();
2111
+ if (opts && opts.globalObject && opts.globalObject.CSSStyleSheet) {
2112
+ importRule.styleSheet = new opts.globalObject.CSSStyleSheet();
2113
+ }
2113
2114
  importRule.__parentStyleSheet = importRule.styleSheet.__parentStyleSheet = styleSheet;
2114
2115
  importRule.parse(buffer + character);
2115
2116
  topScope.cssRules.push(importRule);
@@ -2484,9 +2485,5 @@ CSSOM.CSSLayerBlockRule = require("./CSSLayerBlockRule").CSSLayerBlockRule;
2484
2485
  CSSOM.CSSLayerStatementRule = require("./CSSLayerStatementRule").CSSLayerStatementRule;
2485
2486
  CSSOM.CSSPageRule = require("./CSSPageRule").CSSPageRule;
2486
2487
  // Use cssstyle if available
2487
- try {
2488
- CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
2489
- } catch (e) {
2490
- // ignore
2491
- }
2488
+ require("./cssstyleTryCatchBlock");
2492
2489
  ///CommonJS
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "parser",
8
8
  "styleSheet"
9
9
  ],
10
- "version": "0.9.26",
10
+ "version": "0.9.27",
11
11
  "author": "Nikita Vasilyev <me@elv1s.ru>",
12
12
  "contributors": [
13
13
  "Acemir Sousa Mendes <acemirsm@gmail.com>"