CSSOM.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var CSSOM = {
  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. var factoryCSSStyleSheet = createFunctionFactory(instance.CSSStyleSheet);
  19. var CSSStyleSheet = factoryCSSStyleSheet();
  20. CSSStyleSheet.prototype.__globalObject = opts.globalObject;
  21. instance.CSSStyleSheet = CSSStyleSheet;
  22. }
  23. }
  24. return instance;
  25. }
  26. };
  27. function createFunctionFactory(fn) {
  28. return function() {
  29. // Create a new function that delegates to the original
  30. var newFn = function() {
  31. return fn.apply(this, arguments);
  32. };
  33. // Copy prototype chain
  34. Object.setPrototypeOf(newFn, Object.getPrototypeOf(fn));
  35. // Copy own properties
  36. for (var key in fn) {
  37. if (Object.prototype.hasOwnProperty.call(fn, key)) {
  38. newFn[key] = fn[key];
  39. }
  40. }
  41. // Clone the .prototype object for constructor-like behavior
  42. if (fn.prototype) {
  43. newFn.prototype = Object.create(fn.prototype);
  44. }
  45. return newFn;
  46. };
  47. }
  48. //.CommonJS
  49. module.exports = CSSOM;
  50. ///CommonJS