DocumentFragment.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const Node = require("./Node.js");
  5. const ceReactionsPreSteps_jsdom_living_helpers_custom_elements =
  6. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPreSteps;
  7. const ceReactionsPostSteps_jsdom_living_helpers_custom_elements =
  8. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPostSteps;
  9. const implSymbol = utils.implSymbol;
  10. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  11. const interfaceName = "DocumentFragment";
  12. exports.is = value => {
  13. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  14. };
  15. exports.isImpl = value => {
  16. return utils.isObject(value) && value instanceof Impl.implementation;
  17. };
  18. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  19. if (exports.is(value)) {
  20. return utils.implForWrapper(value);
  21. }
  22. throw new globalObject.TypeError(`${context} is not of type 'DocumentFragment'.`);
  23. };
  24. function makeWrapper(globalObject, newTarget) {
  25. let proto;
  26. if (newTarget !== undefined) {
  27. proto = newTarget.prototype;
  28. }
  29. if (!utils.isObject(proto)) {
  30. proto = globalObject[ctorRegistrySymbol]["DocumentFragment"].prototype;
  31. }
  32. return Object.create(proto);
  33. }
  34. exports.create = (globalObject, constructorArgs, privateData) => {
  35. const wrapper = makeWrapper(globalObject);
  36. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  37. };
  38. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  39. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  40. return utils.implForWrapper(wrapper);
  41. };
  42. exports._internalSetup = (wrapper, globalObject) => {
  43. Node._internalSetup(wrapper, globalObject);
  44. };
  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 DocumentFragment extends globalObject.Node {
  78. constructor() {
  79. return exports.setup(Object.create(new.target.prototype), globalObject, undefined);
  80. }
  81. getElementById(elementId) {
  82. const esValue = this !== null && this !== undefined ? this : globalObject;
  83. if (!exports.is(esValue)) {
  84. throw new globalObject.TypeError(
  85. "'getElementById' called on an object that is not a valid instance of DocumentFragment."
  86. );
  87. }
  88. if (arguments.length < 1) {
  89. throw new globalObject.TypeError(
  90. `Failed to execute 'getElementById' on 'DocumentFragment': 1 argument 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 'getElementById' on 'DocumentFragment': parameter 1",
  98. globals: globalObject
  99. });
  100. args.push(curArg);
  101. }
  102. return utils.tryWrapperForImpl(esValue[implSymbol].getElementById(...args));
  103. }
  104. prepend() {
  105. const esValue = this !== null && this !== undefined ? this : globalObject;
  106. if (!exports.is(esValue)) {
  107. throw new globalObject.TypeError(
  108. "'prepend' called on an object that is not a valid instance of DocumentFragment."
  109. );
  110. }
  111. const args = [];
  112. for (let i = 0; i < arguments.length; i++) {
  113. let curArg = arguments[i];
  114. if (Node.is(curArg)) {
  115. curArg = utils.implForWrapper(curArg);
  116. } else {
  117. curArg = conversions["DOMString"](curArg, {
  118. context: "Failed to execute 'prepend' on 'DocumentFragment': parameter " + (i + 1),
  119. globals: globalObject
  120. });
  121. }
  122. args.push(curArg);
  123. }
  124. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  125. try {
  126. return esValue[implSymbol].prepend(...args);
  127. } finally {
  128. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  129. }
  130. }
  131. append() {
  132. const esValue = this !== null && this !== undefined ? this : globalObject;
  133. if (!exports.is(esValue)) {
  134. throw new globalObject.TypeError(
  135. "'append' called on an object that is not a valid instance of DocumentFragment."
  136. );
  137. }
  138. const args = [];
  139. for (let i = 0; i < arguments.length; i++) {
  140. let curArg = arguments[i];
  141. if (Node.is(curArg)) {
  142. curArg = utils.implForWrapper(curArg);
  143. } else {
  144. curArg = conversions["DOMString"](curArg, {
  145. context: "Failed to execute 'append' on 'DocumentFragment': parameter " + (i + 1),
  146. globals: globalObject
  147. });
  148. }
  149. args.push(curArg);
  150. }
  151. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  152. try {
  153. return esValue[implSymbol].append(...args);
  154. } finally {
  155. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  156. }
  157. }
  158. replaceChildren() {
  159. const esValue = this !== null && this !== undefined ? this : globalObject;
  160. if (!exports.is(esValue)) {
  161. throw new globalObject.TypeError(
  162. "'replaceChildren' called on an object that is not a valid instance of DocumentFragment."
  163. );
  164. }
  165. const args = [];
  166. for (let i = 0; i < arguments.length; i++) {
  167. let curArg = arguments[i];
  168. if (Node.is(curArg)) {
  169. curArg = utils.implForWrapper(curArg);
  170. } else {
  171. curArg = conversions["DOMString"](curArg, {
  172. context: "Failed to execute 'replaceChildren' on 'DocumentFragment': parameter " + (i + 1),
  173. globals: globalObject
  174. });
  175. }
  176. args.push(curArg);
  177. }
  178. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  179. try {
  180. return esValue[implSymbol].replaceChildren(...args);
  181. } finally {
  182. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  183. }
  184. }
  185. querySelector(selectors) {
  186. const esValue = this !== null && this !== undefined ? this : globalObject;
  187. if (!exports.is(esValue)) {
  188. throw new globalObject.TypeError(
  189. "'querySelector' called on an object that is not a valid instance of DocumentFragment."
  190. );
  191. }
  192. if (arguments.length < 1) {
  193. throw new globalObject.TypeError(
  194. `Failed to execute 'querySelector' on 'DocumentFragment': 1 argument required, but only ${arguments.length} present.`
  195. );
  196. }
  197. const args = [];
  198. {
  199. let curArg = arguments[0];
  200. curArg = conversions["DOMString"](curArg, {
  201. context: "Failed to execute 'querySelector' on 'DocumentFragment': parameter 1",
  202. globals: globalObject
  203. });
  204. args.push(curArg);
  205. }
  206. return utils.tryWrapperForImpl(esValue[implSymbol].querySelector(...args));
  207. }
  208. querySelectorAll(selectors) {
  209. const esValue = this !== null && this !== undefined ? this : globalObject;
  210. if (!exports.is(esValue)) {
  211. throw new globalObject.TypeError(
  212. "'querySelectorAll' called on an object that is not a valid instance of DocumentFragment."
  213. );
  214. }
  215. if (arguments.length < 1) {
  216. throw new globalObject.TypeError(
  217. `Failed to execute 'querySelectorAll' on 'DocumentFragment': 1 argument required, but only ${arguments.length} present.`
  218. );
  219. }
  220. const args = [];
  221. {
  222. let curArg = arguments[0];
  223. curArg = conversions["DOMString"](curArg, {
  224. context: "Failed to execute 'querySelectorAll' on 'DocumentFragment': parameter 1",
  225. globals: globalObject
  226. });
  227. args.push(curArg);
  228. }
  229. return utils.tryWrapperForImpl(esValue[implSymbol].querySelectorAll(...args));
  230. }
  231. get children() {
  232. const esValue = this !== null && this !== undefined ? this : globalObject;
  233. if (!exports.is(esValue)) {
  234. throw new globalObject.TypeError(
  235. "'get children' called on an object that is not a valid instance of DocumentFragment."
  236. );
  237. }
  238. return utils.getSameObject(this, "children", () => {
  239. return utils.tryWrapperForImpl(esValue[implSymbol]["children"]);
  240. });
  241. }
  242. get firstElementChild() {
  243. const esValue = this !== null && this !== undefined ? this : globalObject;
  244. if (!exports.is(esValue)) {
  245. throw new globalObject.TypeError(
  246. "'get firstElementChild' called on an object that is not a valid instance of DocumentFragment."
  247. );
  248. }
  249. return utils.tryWrapperForImpl(esValue[implSymbol]["firstElementChild"]);
  250. }
  251. get lastElementChild() {
  252. const esValue = this !== null && this !== undefined ? this : globalObject;
  253. if (!exports.is(esValue)) {
  254. throw new globalObject.TypeError(
  255. "'get lastElementChild' called on an object that is not a valid instance of DocumentFragment."
  256. );
  257. }
  258. return utils.tryWrapperForImpl(esValue[implSymbol]["lastElementChild"]);
  259. }
  260. get childElementCount() {
  261. const esValue = this !== null && this !== undefined ? this : globalObject;
  262. if (!exports.is(esValue)) {
  263. throw new globalObject.TypeError(
  264. "'get childElementCount' called on an object that is not a valid instance of DocumentFragment."
  265. );
  266. }
  267. return esValue[implSymbol]["childElementCount"];
  268. }
  269. }
  270. Object.defineProperties(DocumentFragment.prototype, {
  271. getElementById: { enumerable: true },
  272. prepend: { enumerable: true },
  273. append: { enumerable: true },
  274. replaceChildren: { enumerable: true },
  275. querySelector: { enumerable: true },
  276. querySelectorAll: { enumerable: true },
  277. children: { enumerable: true },
  278. firstElementChild: { enumerable: true },
  279. lastElementChild: { enumerable: true },
  280. childElementCount: { enumerable: true },
  281. [Symbol.toStringTag]: { value: "DocumentFragment", configurable: true },
  282. [Symbol.unscopables]: {
  283. value: { prepend: true, append: true, replaceChildren: true, __proto__: null },
  284. configurable: true
  285. }
  286. });
  287. ctorRegistry[interfaceName] = DocumentFragment;
  288. Object.defineProperty(globalObject, interfaceName, {
  289. configurable: true,
  290. writable: true,
  291. value: DocumentFragment
  292. });
  293. };
  294. const Impl = require("../../jsdom/living/nodes/DocumentFragment-impl.js");