CustomElementRegistry.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const CustomElementConstructor = require("./CustomElementConstructor.js");
  5. const ElementDefinitionOptions = require("./ElementDefinitionOptions.js");
  6. const ceReactionsPreSteps_jsdom_living_helpers_custom_elements =
  7. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPreSteps;
  8. const ceReactionsPostSteps_jsdom_living_helpers_custom_elements =
  9. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPostSteps;
  10. const Node = require("./Node.js");
  11. const implSymbol = utils.implSymbol;
  12. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  13. const interfaceName = "CustomElementRegistry";
  14. exports.is = value => {
  15. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  16. };
  17. exports.isImpl = value => {
  18. return utils.isObject(value) && value instanceof Impl.implementation;
  19. };
  20. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  21. if (exports.is(value)) {
  22. return utils.implForWrapper(value);
  23. }
  24. throw new globalObject.TypeError(`${context} is not of type 'CustomElementRegistry'.`);
  25. };
  26. function makeWrapper(globalObject, newTarget) {
  27. let proto;
  28. if (newTarget !== undefined) {
  29. proto = newTarget.prototype;
  30. }
  31. if (!utils.isObject(proto)) {
  32. proto = globalObject[ctorRegistrySymbol]["CustomElementRegistry"].prototype;
  33. }
  34. return Object.create(proto);
  35. }
  36. exports.create = (globalObject, constructorArgs, privateData) => {
  37. const wrapper = makeWrapper(globalObject);
  38. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  39. };
  40. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  41. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  42. return utils.implForWrapper(wrapper);
  43. };
  44. exports._internalSetup = (wrapper, globalObject) => {};
  45. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  46. privateData.wrapper = wrapper;
  47. exports._internalSetup(wrapper, globalObject);
  48. Object.defineProperty(wrapper, implSymbol, {
  49. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  50. configurable: true
  51. });
  52. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  53. if (Impl.init) {
  54. Impl.init(wrapper[implSymbol]);
  55. }
  56. return wrapper;
  57. };
  58. exports.new = (globalObject, newTarget) => {
  59. const wrapper = makeWrapper(globalObject, newTarget);
  60. exports._internalSetup(wrapper, globalObject);
  61. Object.defineProperty(wrapper, implSymbol, {
  62. value: Object.create(Impl.implementation.prototype),
  63. configurable: true
  64. });
  65. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  66. if (Impl.init) {
  67. Impl.init(wrapper[implSymbol]);
  68. }
  69. return wrapper[implSymbol];
  70. };
  71. const exposed = new Set(["Window"]);
  72. exports.install = (globalObject, globalNames) => {
  73. if (!globalNames.some(globalName => exposed.has(globalName))) {
  74. return;
  75. }
  76. const ctorRegistry = utils.initCtorRegistry(globalObject);
  77. class CustomElementRegistry {
  78. constructor() {
  79. throw new globalObject.TypeError("Illegal constructor");
  80. }
  81. define(name, constructor) {
  82. const esValue = this !== null && this !== undefined ? this : globalObject;
  83. if (!exports.is(esValue)) {
  84. throw new globalObject.TypeError(
  85. "'define' called on an object that is not a valid instance of CustomElementRegistry."
  86. );
  87. }
  88. if (arguments.length < 2) {
  89. throw new globalObject.TypeError(
  90. `Failed to execute 'define' on 'CustomElementRegistry': 2 arguments required, but only ${arguments.length} present.`
  91. );
  92. }
  93. const args = [];
  94. {
  95. let curArg = arguments[0];
  96. curArg = conversions["DOMString"](curArg, {
  97. context: "Failed to execute 'define' on 'CustomElementRegistry': parameter 1",
  98. globals: globalObject
  99. });
  100. args.push(curArg);
  101. }
  102. {
  103. let curArg = arguments[1];
  104. curArg = CustomElementConstructor.convert(globalObject, curArg, {
  105. context: "Failed to execute 'define' on 'CustomElementRegistry': parameter 2"
  106. });
  107. args.push(curArg);
  108. }
  109. {
  110. let curArg = arguments[2];
  111. curArg = ElementDefinitionOptions.convert(globalObject, curArg, {
  112. context: "Failed to execute 'define' on 'CustomElementRegistry': parameter 3"
  113. });
  114. args.push(curArg);
  115. }
  116. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  117. try {
  118. return esValue[implSymbol].define(...args);
  119. } finally {
  120. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  121. }
  122. }
  123. get(name) {
  124. const esValue = this !== null && this !== undefined ? this : globalObject;
  125. if (!exports.is(esValue)) {
  126. throw new globalObject.TypeError(
  127. "'get' called on an object that is not a valid instance of CustomElementRegistry."
  128. );
  129. }
  130. if (arguments.length < 1) {
  131. throw new globalObject.TypeError(
  132. `Failed to execute 'get' on 'CustomElementRegistry': 1 argument required, but only ${arguments.length} present.`
  133. );
  134. }
  135. const args = [];
  136. {
  137. let curArg = arguments[0];
  138. curArg = conversions["DOMString"](curArg, {
  139. context: "Failed to execute 'get' on 'CustomElementRegistry': parameter 1",
  140. globals: globalObject
  141. });
  142. args.push(curArg);
  143. }
  144. return esValue[implSymbol].get(...args);
  145. }
  146. getName(constructor) {
  147. const esValue = this !== null && this !== undefined ? this : globalObject;
  148. if (!exports.is(esValue)) {
  149. throw new globalObject.TypeError(
  150. "'getName' called on an object that is not a valid instance of CustomElementRegistry."
  151. );
  152. }
  153. if (arguments.length < 1) {
  154. throw new globalObject.TypeError(
  155. `Failed to execute 'getName' on 'CustomElementRegistry': 1 argument required, but only ${arguments.length} present.`
  156. );
  157. }
  158. const args = [];
  159. {
  160. let curArg = arguments[0];
  161. curArg = CustomElementConstructor.convert(globalObject, curArg, {
  162. context: "Failed to execute 'getName' on 'CustomElementRegistry': parameter 1"
  163. });
  164. args.push(curArg);
  165. }
  166. return esValue[implSymbol].getName(...args);
  167. }
  168. whenDefined(name) {
  169. try {
  170. const esValue = this !== null && this !== undefined ? this : globalObject;
  171. if (!exports.is(esValue)) {
  172. throw new globalObject.TypeError(
  173. "'whenDefined' called on an object that is not a valid instance of CustomElementRegistry."
  174. );
  175. }
  176. if (arguments.length < 1) {
  177. throw new globalObject.TypeError(
  178. `Failed to execute 'whenDefined' on 'CustomElementRegistry': 1 argument required, but only ${arguments.length} present.`
  179. );
  180. }
  181. const args = [];
  182. {
  183. let curArg = arguments[0];
  184. curArg = conversions["DOMString"](curArg, {
  185. context: "Failed to execute 'whenDefined' on 'CustomElementRegistry': parameter 1",
  186. globals: globalObject
  187. });
  188. args.push(curArg);
  189. }
  190. return utils.tryWrapperForImpl(esValue[implSymbol].whenDefined(...args));
  191. } catch (e) {
  192. return globalObject.Promise.reject(e);
  193. }
  194. }
  195. upgrade(root) {
  196. const esValue = this !== null && this !== undefined ? this : globalObject;
  197. if (!exports.is(esValue)) {
  198. throw new globalObject.TypeError(
  199. "'upgrade' called on an object that is not a valid instance of CustomElementRegistry."
  200. );
  201. }
  202. if (arguments.length < 1) {
  203. throw new globalObject.TypeError(
  204. `Failed to execute 'upgrade' on 'CustomElementRegistry': 1 argument required, but only ${arguments.length} present.`
  205. );
  206. }
  207. const args = [];
  208. {
  209. let curArg = arguments[0];
  210. curArg = Node.convert(globalObject, curArg, {
  211. context: "Failed to execute 'upgrade' on 'CustomElementRegistry': parameter 1"
  212. });
  213. args.push(curArg);
  214. }
  215. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  216. try {
  217. return esValue[implSymbol].upgrade(...args);
  218. } finally {
  219. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  220. }
  221. }
  222. }
  223. Object.defineProperties(CustomElementRegistry.prototype, {
  224. define: { enumerable: true },
  225. get: { enumerable: true },
  226. getName: { enumerable: true },
  227. whenDefined: { enumerable: true },
  228. upgrade: { enumerable: true },
  229. [Symbol.toStringTag]: { value: "CustomElementRegistry", configurable: true }
  230. });
  231. ctorRegistry[interfaceName] = CustomElementRegistry;
  232. Object.defineProperty(globalObject, interfaceName, {
  233. configurable: true,
  234. writable: true,
  235. value: CustomElementRegistry
  236. });
  237. };
  238. const Impl = require("../../jsdom/living/custom-elements/CustomElementRegistry-impl.js");