StyleSheet.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const implSymbol = utils.implSymbol;
  5. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  6. const interfaceName = "StyleSheet";
  7. exports.is = value => {
  8. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  9. };
  10. exports.isImpl = value => {
  11. return utils.isObject(value) && value instanceof Impl.implementation;
  12. };
  13. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  14. if (exports.is(value)) {
  15. return utils.implForWrapper(value);
  16. }
  17. throw new globalObject.TypeError(`${context} is not of type 'StyleSheet'.`);
  18. };
  19. function makeWrapper(globalObject, newTarget) {
  20. let proto;
  21. if (newTarget !== undefined) {
  22. proto = newTarget.prototype;
  23. }
  24. if (!utils.isObject(proto)) {
  25. proto = globalObject[ctorRegistrySymbol]["StyleSheet"].prototype;
  26. }
  27. return Object.create(proto);
  28. }
  29. exports.create = (globalObject, constructorArgs, privateData) => {
  30. const wrapper = makeWrapper(globalObject);
  31. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  32. };
  33. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  34. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  35. return utils.implForWrapper(wrapper);
  36. };
  37. exports._internalSetup = (wrapper, globalObject) => {};
  38. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  39. privateData.wrapper = wrapper;
  40. exports._internalSetup(wrapper, globalObject);
  41. Object.defineProperty(wrapper, implSymbol, {
  42. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  43. configurable: true
  44. });
  45. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  46. if (Impl.init) {
  47. Impl.init(wrapper[implSymbol]);
  48. }
  49. return wrapper;
  50. };
  51. exports.new = (globalObject, newTarget) => {
  52. const wrapper = makeWrapper(globalObject, newTarget);
  53. exports._internalSetup(wrapper, globalObject);
  54. Object.defineProperty(wrapper, implSymbol, {
  55. value: Object.create(Impl.implementation.prototype),
  56. configurable: true
  57. });
  58. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  59. if (Impl.init) {
  60. Impl.init(wrapper[implSymbol]);
  61. }
  62. return wrapper[implSymbol];
  63. };
  64. const exposed = new Set(["Window"]);
  65. exports.install = (globalObject, globalNames) => {
  66. if (!globalNames.some(globalName => exposed.has(globalName))) {
  67. return;
  68. }
  69. const ctorRegistry = utils.initCtorRegistry(globalObject);
  70. class StyleSheet {
  71. constructor() {
  72. throw new globalObject.TypeError("Illegal constructor");
  73. }
  74. get type() {
  75. const esValue = this !== null && this !== undefined ? this : globalObject;
  76. if (!exports.is(esValue)) {
  77. throw new globalObject.TypeError("'get type' called on an object that is not a valid instance of StyleSheet.");
  78. }
  79. return utils.tryWrapperForImpl(esValue[implSymbol]["type"]);
  80. }
  81. get href() {
  82. const esValue = this !== null && this !== undefined ? this : globalObject;
  83. if (!exports.is(esValue)) {
  84. throw new globalObject.TypeError("'get href' called on an object that is not a valid instance of StyleSheet.");
  85. }
  86. return esValue[implSymbol]["href"];
  87. }
  88. get ownerNode() {
  89. const esValue = this !== null && this !== undefined ? this : globalObject;
  90. if (!exports.is(esValue)) {
  91. throw new globalObject.TypeError(
  92. "'get ownerNode' called on an object that is not a valid instance of StyleSheet."
  93. );
  94. }
  95. return utils.tryWrapperForImpl(esValue[implSymbol]["ownerNode"]);
  96. }
  97. get parentStyleSheet() {
  98. const esValue = this !== null && this !== undefined ? this : globalObject;
  99. if (!exports.is(esValue)) {
  100. throw new globalObject.TypeError(
  101. "'get parentStyleSheet' called on an object that is not a valid instance of StyleSheet."
  102. );
  103. }
  104. return utils.tryWrapperForImpl(esValue[implSymbol]["parentStyleSheet"]);
  105. }
  106. get title() {
  107. const esValue = this !== null && this !== undefined ? this : globalObject;
  108. if (!exports.is(esValue)) {
  109. throw new globalObject.TypeError("'get title' called on an object that is not a valid instance of StyleSheet.");
  110. }
  111. return esValue[implSymbol]["title"];
  112. }
  113. get media() {
  114. const esValue = this !== null && this !== undefined ? this : globalObject;
  115. if (!exports.is(esValue)) {
  116. throw new globalObject.TypeError("'get media' called on an object that is not a valid instance of StyleSheet.");
  117. }
  118. return utils.getSameObject(this, "media", () => {
  119. return utils.tryWrapperForImpl(esValue[implSymbol]["media"]);
  120. });
  121. }
  122. set media(V) {
  123. const esValue = this !== null && this !== undefined ? this : globalObject;
  124. if (!exports.is(esValue)) {
  125. throw new globalObject.TypeError("'set media' called on an object that is not a valid instance of StyleSheet.");
  126. }
  127. const Q = esValue["media"];
  128. if (!utils.isObject(Q)) {
  129. throw new globalObject.TypeError("Property 'media' is not an object");
  130. }
  131. Reflect.set(Q, "mediaText", V);
  132. }
  133. get disabled() {
  134. const esValue = this !== null && this !== undefined ? this : globalObject;
  135. if (!exports.is(esValue)) {
  136. throw new globalObject.TypeError(
  137. "'get disabled' called on an object that is not a valid instance of StyleSheet."
  138. );
  139. }
  140. return esValue[implSymbol]["disabled"];
  141. }
  142. set disabled(V) {
  143. const esValue = this !== null && this !== undefined ? this : globalObject;
  144. if (!exports.is(esValue)) {
  145. throw new globalObject.TypeError(
  146. "'set disabled' called on an object that is not a valid instance of StyleSheet."
  147. );
  148. }
  149. V = conversions["boolean"](V, {
  150. context: "Failed to set the 'disabled' property on 'StyleSheet': The provided value",
  151. globals: globalObject
  152. });
  153. esValue[implSymbol]["disabled"] = V;
  154. }
  155. }
  156. Object.defineProperties(StyleSheet.prototype, {
  157. type: { enumerable: true },
  158. href: { enumerable: true },
  159. ownerNode: { enumerable: true },
  160. parentStyleSheet: { enumerable: true },
  161. title: { enumerable: true },
  162. media: { enumerable: true },
  163. disabled: { enumerable: true },
  164. [Symbol.toStringTag]: { value: "StyleSheet", configurable: true }
  165. });
  166. ctorRegistry[interfaceName] = StyleSheet;
  167. Object.defineProperty(globalObject, interfaceName, {
  168. configurable: true,
  169. writable: true,
  170. value: StyleSheet
  171. });
  172. };
  173. const Impl = require("../../jsdom/living/css/StyleSheet-impl.js");