DOMImplementation.js 7.6 KB

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