CharacterData.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 = "CharacterData";
  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 'CharacterData'.`);
  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]["CharacterData"].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 CharacterData extends globalObject.Node {
  78. constructor() {
  79. throw new globalObject.TypeError("Illegal constructor");
  80. }
  81. substringData(offset, count) {
  82. const esValue = this !== null && this !== undefined ? this : globalObject;
  83. if (!exports.is(esValue)) {
  84. throw new globalObject.TypeError(
  85. "'substringData' called on an object that is not a valid instance of CharacterData."
  86. );
  87. }
  88. if (arguments.length < 2) {
  89. throw new globalObject.TypeError(
  90. `Failed to execute 'substringData' on 'CharacterData': 2 arguments required, but only ${arguments.length} present.`
  91. );
  92. }
  93. const args = [];
  94. {
  95. let curArg = arguments[0];
  96. curArg = conversions["unsigned long"](curArg, {
  97. context: "Failed to execute 'substringData' on 'CharacterData': parameter 1",
  98. globals: globalObject
  99. });
  100. args.push(curArg);
  101. }
  102. {
  103. let curArg = arguments[1];
  104. curArg = conversions["unsigned long"](curArg, {
  105. context: "Failed to execute 'substringData' on 'CharacterData': parameter 2",
  106. globals: globalObject
  107. });
  108. args.push(curArg);
  109. }
  110. return esValue[implSymbol].substringData(...args);
  111. }
  112. appendData(data) {
  113. const esValue = this !== null && this !== undefined ? this : globalObject;
  114. if (!exports.is(esValue)) {
  115. throw new globalObject.TypeError(
  116. "'appendData' called on an object that is not a valid instance of CharacterData."
  117. );
  118. }
  119. if (arguments.length < 1) {
  120. throw new globalObject.TypeError(
  121. `Failed to execute 'appendData' on 'CharacterData': 1 argument required, but only ${arguments.length} present.`
  122. );
  123. }
  124. const args = [];
  125. {
  126. let curArg = arguments[0];
  127. curArg = conversions["DOMString"](curArg, {
  128. context: "Failed to execute 'appendData' on 'CharacterData': parameter 1",
  129. globals: globalObject
  130. });
  131. args.push(curArg);
  132. }
  133. return esValue[implSymbol].appendData(...args);
  134. }
  135. insertData(offset, data) {
  136. const esValue = this !== null && this !== undefined ? this : globalObject;
  137. if (!exports.is(esValue)) {
  138. throw new globalObject.TypeError(
  139. "'insertData' called on an object that is not a valid instance of CharacterData."
  140. );
  141. }
  142. if (arguments.length < 2) {
  143. throw new globalObject.TypeError(
  144. `Failed to execute 'insertData' on 'CharacterData': 2 arguments required, but only ${arguments.length} present.`
  145. );
  146. }
  147. const args = [];
  148. {
  149. let curArg = arguments[0];
  150. curArg = conversions["unsigned long"](curArg, {
  151. context: "Failed to execute 'insertData' on 'CharacterData': parameter 1",
  152. globals: globalObject
  153. });
  154. args.push(curArg);
  155. }
  156. {
  157. let curArg = arguments[1];
  158. curArg = conversions["DOMString"](curArg, {
  159. context: "Failed to execute 'insertData' on 'CharacterData': parameter 2",
  160. globals: globalObject
  161. });
  162. args.push(curArg);
  163. }
  164. return esValue[implSymbol].insertData(...args);
  165. }
  166. deleteData(offset, count) {
  167. const esValue = this !== null && this !== undefined ? this : globalObject;
  168. if (!exports.is(esValue)) {
  169. throw new globalObject.TypeError(
  170. "'deleteData' called on an object that is not a valid instance of CharacterData."
  171. );
  172. }
  173. if (arguments.length < 2) {
  174. throw new globalObject.TypeError(
  175. `Failed to execute 'deleteData' on 'CharacterData': 2 arguments required, but only ${arguments.length} present.`
  176. );
  177. }
  178. const args = [];
  179. {
  180. let curArg = arguments[0];
  181. curArg = conversions["unsigned long"](curArg, {
  182. context: "Failed to execute 'deleteData' on 'CharacterData': parameter 1",
  183. globals: globalObject
  184. });
  185. args.push(curArg);
  186. }
  187. {
  188. let curArg = arguments[1];
  189. curArg = conversions["unsigned long"](curArg, {
  190. context: "Failed to execute 'deleteData' on 'CharacterData': parameter 2",
  191. globals: globalObject
  192. });
  193. args.push(curArg);
  194. }
  195. return esValue[implSymbol].deleteData(...args);
  196. }
  197. replaceData(offset, count, data) {
  198. const esValue = this !== null && this !== undefined ? this : globalObject;
  199. if (!exports.is(esValue)) {
  200. throw new globalObject.TypeError(
  201. "'replaceData' called on an object that is not a valid instance of CharacterData."
  202. );
  203. }
  204. if (arguments.length < 3) {
  205. throw new globalObject.TypeError(
  206. `Failed to execute 'replaceData' on 'CharacterData': 3 arguments required, but only ${arguments.length} present.`
  207. );
  208. }
  209. const args = [];
  210. {
  211. let curArg = arguments[0];
  212. curArg = conversions["unsigned long"](curArg, {
  213. context: "Failed to execute 'replaceData' on 'CharacterData': parameter 1",
  214. globals: globalObject
  215. });
  216. args.push(curArg);
  217. }
  218. {
  219. let curArg = arguments[1];
  220. curArg = conversions["unsigned long"](curArg, {
  221. context: "Failed to execute 'replaceData' on 'CharacterData': parameter 2",
  222. globals: globalObject
  223. });
  224. args.push(curArg);
  225. }
  226. {
  227. let curArg = arguments[2];
  228. curArg = conversions["DOMString"](curArg, {
  229. context: "Failed to execute 'replaceData' on 'CharacterData': parameter 3",
  230. globals: globalObject
  231. });
  232. args.push(curArg);
  233. }
  234. return esValue[implSymbol].replaceData(...args);
  235. }
  236. before() {
  237. const esValue = this !== null && this !== undefined ? this : globalObject;
  238. if (!exports.is(esValue)) {
  239. throw new globalObject.TypeError("'before' called on an object that is not a valid instance of CharacterData.");
  240. }
  241. const args = [];
  242. for (let i = 0; i < arguments.length; i++) {
  243. let curArg = arguments[i];
  244. if (Node.is(curArg)) {
  245. curArg = utils.implForWrapper(curArg);
  246. } else {
  247. curArg = conversions["DOMString"](curArg, {
  248. context: "Failed to execute 'before' on 'CharacterData': parameter " + (i + 1),
  249. globals: globalObject
  250. });
  251. }
  252. args.push(curArg);
  253. }
  254. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  255. try {
  256. return esValue[implSymbol].before(...args);
  257. } finally {
  258. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  259. }
  260. }
  261. after() {
  262. const esValue = this !== null && this !== undefined ? this : globalObject;
  263. if (!exports.is(esValue)) {
  264. throw new globalObject.TypeError("'after' called on an object that is not a valid instance of CharacterData.");
  265. }
  266. const args = [];
  267. for (let i = 0; i < arguments.length; i++) {
  268. let curArg = arguments[i];
  269. if (Node.is(curArg)) {
  270. curArg = utils.implForWrapper(curArg);
  271. } else {
  272. curArg = conversions["DOMString"](curArg, {
  273. context: "Failed to execute 'after' on 'CharacterData': parameter " + (i + 1),
  274. globals: globalObject
  275. });
  276. }
  277. args.push(curArg);
  278. }
  279. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  280. try {
  281. return esValue[implSymbol].after(...args);
  282. } finally {
  283. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  284. }
  285. }
  286. replaceWith() {
  287. const esValue = this !== null && this !== undefined ? this : globalObject;
  288. if (!exports.is(esValue)) {
  289. throw new globalObject.TypeError(
  290. "'replaceWith' called on an object that is not a valid instance of CharacterData."
  291. );
  292. }
  293. const args = [];
  294. for (let i = 0; i < arguments.length; i++) {
  295. let curArg = arguments[i];
  296. if (Node.is(curArg)) {
  297. curArg = utils.implForWrapper(curArg);
  298. } else {
  299. curArg = conversions["DOMString"](curArg, {
  300. context: "Failed to execute 'replaceWith' on 'CharacterData': parameter " + (i + 1),
  301. globals: globalObject
  302. });
  303. }
  304. args.push(curArg);
  305. }
  306. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  307. try {
  308. return esValue[implSymbol].replaceWith(...args);
  309. } finally {
  310. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  311. }
  312. }
  313. remove() {
  314. const esValue = this !== null && this !== undefined ? this : globalObject;
  315. if (!exports.is(esValue)) {
  316. throw new globalObject.TypeError("'remove' called on an object that is not a valid instance of CharacterData.");
  317. }
  318. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  319. try {
  320. return esValue[implSymbol].remove();
  321. } finally {
  322. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  323. }
  324. }
  325. get data() {
  326. const esValue = this !== null && this !== undefined ? this : globalObject;
  327. if (!exports.is(esValue)) {
  328. throw new globalObject.TypeError(
  329. "'get data' called on an object that is not a valid instance of CharacterData."
  330. );
  331. }
  332. return esValue[implSymbol]["data"];
  333. }
  334. set data(V) {
  335. const esValue = this !== null && this !== undefined ? this : globalObject;
  336. if (!exports.is(esValue)) {
  337. throw new globalObject.TypeError(
  338. "'set data' called on an object that is not a valid instance of CharacterData."
  339. );
  340. }
  341. V = conversions["DOMString"](V, {
  342. context: "Failed to set the 'data' property on 'CharacterData': The provided value",
  343. globals: globalObject,
  344. treatNullAsEmptyString: true
  345. });
  346. esValue[implSymbol]["data"] = V;
  347. }
  348. get length() {
  349. const esValue = this !== null && this !== undefined ? this : globalObject;
  350. if (!exports.is(esValue)) {
  351. throw new globalObject.TypeError(
  352. "'get length' called on an object that is not a valid instance of CharacterData."
  353. );
  354. }
  355. return esValue[implSymbol]["length"];
  356. }
  357. get previousElementSibling() {
  358. const esValue = this !== null && this !== undefined ? this : globalObject;
  359. if (!exports.is(esValue)) {
  360. throw new globalObject.TypeError(
  361. "'get previousElementSibling' called on an object that is not a valid instance of CharacterData."
  362. );
  363. }
  364. return utils.tryWrapperForImpl(esValue[implSymbol]["previousElementSibling"]);
  365. }
  366. get nextElementSibling() {
  367. const esValue = this !== null && this !== undefined ? this : globalObject;
  368. if (!exports.is(esValue)) {
  369. throw new globalObject.TypeError(
  370. "'get nextElementSibling' called on an object that is not a valid instance of CharacterData."
  371. );
  372. }
  373. return utils.tryWrapperForImpl(esValue[implSymbol]["nextElementSibling"]);
  374. }
  375. }
  376. Object.defineProperties(CharacterData.prototype, {
  377. substringData: { enumerable: true },
  378. appendData: { enumerable: true },
  379. insertData: { enumerable: true },
  380. deleteData: { enumerable: true },
  381. replaceData: { enumerable: true },
  382. before: { enumerable: true },
  383. after: { enumerable: true },
  384. replaceWith: { enumerable: true },
  385. remove: { enumerable: true },
  386. data: { enumerable: true },
  387. length: { enumerable: true },
  388. previousElementSibling: { enumerable: true },
  389. nextElementSibling: { enumerable: true },
  390. [Symbol.toStringTag]: { value: "CharacterData", configurable: true },
  391. [Symbol.unscopables]: {
  392. value: { before: true, after: true, replaceWith: true, remove: true, __proto__: null },
  393. configurable: true
  394. }
  395. });
  396. ctorRegistry[interfaceName] = CharacterData;
  397. Object.defineProperty(globalObject, interfaceName, {
  398. configurable: true,
  399. writable: true,
  400. value: CharacterData
  401. });
  402. };
  403. const Impl = require("../../jsdom/living/nodes/CharacterData-impl.js");