URLSearchParams.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const impl = utils.implSymbol;
  5. const IteratorPrototype = Object.create(utils.IteratorPrototype, {
  6. next: {
  7. value: function next() {
  8. const internal = this[utils.iterInternalSymbol];
  9. const { target, kind, index } = internal;
  10. const values = Array.from(target[impl]);
  11. const len = values.length;
  12. if (index >= len) {
  13. return { value: undefined, done: true };
  14. }
  15. const pair = values[index];
  16. internal.index = index + 1;
  17. const [key, value] = pair.map(utils.tryWrapperForImpl);
  18. let result;
  19. switch (kind) {
  20. case "key":
  21. result = key;
  22. break;
  23. case "value":
  24. result = value;
  25. break;
  26. case "key+value":
  27. result = [key, value];
  28. break;
  29. }
  30. return { value: result, done: false };
  31. },
  32. writable: true,
  33. enumerable: true,
  34. configurable: true
  35. },
  36. [Symbol.toStringTag]: {
  37. value: "URLSearchParams Iterator",
  38. configurable: true
  39. }
  40. });
  41. class URLSearchParams {
  42. constructor() {
  43. const args = [];
  44. {
  45. let curArg = arguments[0];
  46. if (curArg !== undefined) {
  47. if (utils.isObject(curArg)) {
  48. if (curArg[Symbol.iterator] !== undefined) {
  49. if (!utils.isObject(curArg)) {
  50. throw new TypeError(
  51. "Failed to construct 'URLSearchParams': parameter 1" + " sequence" + " is not an iterable object."
  52. );
  53. } else {
  54. const V = [];
  55. const tmp = curArg;
  56. for (let nextItem of tmp) {
  57. if (!utils.isObject(nextItem)) {
  58. throw new TypeError(
  59. "Failed to construct 'URLSearchParams': parameter 1" +
  60. " sequence" +
  61. "'s element" +
  62. " is not an iterable object."
  63. );
  64. } else {
  65. const V = [];
  66. const tmp = nextItem;
  67. for (let nextItem of tmp) {
  68. nextItem = conversions["USVString"](nextItem, {
  69. context:
  70. "Failed to construct 'URLSearchParams': parameter 1" + " sequence" + "'s element" + "'s element"
  71. });
  72. V.push(nextItem);
  73. }
  74. nextItem = V;
  75. }
  76. V.push(nextItem);
  77. }
  78. curArg = V;
  79. }
  80. } else {
  81. if (!utils.isObject(curArg)) {
  82. throw new TypeError(
  83. "Failed to construct 'URLSearchParams': parameter 1" + " record" + " is not an object."
  84. );
  85. } else {
  86. const result = Object.create(null);
  87. for (const key of Reflect.ownKeys(curArg)) {
  88. const desc = Object.getOwnPropertyDescriptor(curArg, key);
  89. if (desc && desc.enumerable) {
  90. let typedKey = key;
  91. let typedValue = curArg[key];
  92. typedKey = conversions["USVString"](typedKey, {
  93. context: "Failed to construct 'URLSearchParams': parameter 1" + " record" + "'s key"
  94. });
  95. typedValue = conversions["USVString"](typedValue, {
  96. context: "Failed to construct 'URLSearchParams': parameter 1" + " record" + "'s value"
  97. });
  98. result[typedKey] = typedValue;
  99. }
  100. }
  101. curArg = result;
  102. }
  103. }
  104. } else {
  105. curArg = conversions["USVString"](curArg, { context: "Failed to construct 'URLSearchParams': parameter 1" });
  106. }
  107. } else {
  108. curArg = "";
  109. }
  110. args.push(curArg);
  111. }
  112. return iface.setup(Object.create(new.target.prototype), args);
  113. }
  114. append(name, value) {
  115. if (!this || !module.exports.is(this)) {
  116. throw new TypeError("Illegal invocation");
  117. }
  118. if (arguments.length < 2) {
  119. throw new TypeError(
  120. "Failed to execute 'append' on 'URLSearchParams': 2 arguments required, but only " +
  121. arguments.length +
  122. " present."
  123. );
  124. }
  125. const args = [];
  126. {
  127. let curArg = arguments[0];
  128. curArg = conversions["USVString"](curArg, {
  129. context: "Failed to execute 'append' on 'URLSearchParams': parameter 1"
  130. });
  131. args.push(curArg);
  132. }
  133. {
  134. let curArg = arguments[1];
  135. curArg = conversions["USVString"](curArg, {
  136. context: "Failed to execute 'append' on 'URLSearchParams': parameter 2"
  137. });
  138. args.push(curArg);
  139. }
  140. return this[impl].append(...args);
  141. }
  142. delete(name) {
  143. if (!this || !module.exports.is(this)) {
  144. throw new TypeError("Illegal invocation");
  145. }
  146. if (arguments.length < 1) {
  147. throw new TypeError(
  148. "Failed to execute 'delete' on 'URLSearchParams': 1 argument required, but only " +
  149. arguments.length +
  150. " present."
  151. );
  152. }
  153. const args = [];
  154. {
  155. let curArg = arguments[0];
  156. curArg = conversions["USVString"](curArg, {
  157. context: "Failed to execute 'delete' on 'URLSearchParams': parameter 1"
  158. });
  159. args.push(curArg);
  160. }
  161. return this[impl].delete(...args);
  162. }
  163. get(name) {
  164. if (!this || !module.exports.is(this)) {
  165. throw new TypeError("Illegal invocation");
  166. }
  167. if (arguments.length < 1) {
  168. throw new TypeError(
  169. "Failed to execute 'get' on 'URLSearchParams': 1 argument required, but only " + arguments.length + " present."
  170. );
  171. }
  172. const args = [];
  173. {
  174. let curArg = arguments[0];
  175. curArg = conversions["USVString"](curArg, {
  176. context: "Failed to execute 'get' on 'URLSearchParams': parameter 1"
  177. });
  178. args.push(curArg);
  179. }
  180. return this[impl].get(...args);
  181. }
  182. getAll(name) {
  183. if (!this || !module.exports.is(this)) {
  184. throw new TypeError("Illegal invocation");
  185. }
  186. if (arguments.length < 1) {
  187. throw new TypeError(
  188. "Failed to execute 'getAll' on 'URLSearchParams': 1 argument required, but only " +
  189. arguments.length +
  190. " present."
  191. );
  192. }
  193. const args = [];
  194. {
  195. let curArg = arguments[0];
  196. curArg = conversions["USVString"](curArg, {
  197. context: "Failed to execute 'getAll' on 'URLSearchParams': parameter 1"
  198. });
  199. args.push(curArg);
  200. }
  201. return utils.tryWrapperForImpl(this[impl].getAll(...args));
  202. }
  203. has(name) {
  204. if (!this || !module.exports.is(this)) {
  205. throw new TypeError("Illegal invocation");
  206. }
  207. if (arguments.length < 1) {
  208. throw new TypeError(
  209. "Failed to execute 'has' on 'URLSearchParams': 1 argument required, but only " + arguments.length + " present."
  210. );
  211. }
  212. const args = [];
  213. {
  214. let curArg = arguments[0];
  215. curArg = conversions["USVString"](curArg, {
  216. context: "Failed to execute 'has' on 'URLSearchParams': parameter 1"
  217. });
  218. args.push(curArg);
  219. }
  220. return this[impl].has(...args);
  221. }
  222. set(name, value) {
  223. if (!this || !module.exports.is(this)) {
  224. throw new TypeError("Illegal invocation");
  225. }
  226. if (arguments.length < 2) {
  227. throw new TypeError(
  228. "Failed to execute 'set' on 'URLSearchParams': 2 arguments required, but only " + arguments.length + " present."
  229. );
  230. }
  231. const args = [];
  232. {
  233. let curArg = arguments[0];
  234. curArg = conversions["USVString"](curArg, {
  235. context: "Failed to execute 'set' on 'URLSearchParams': parameter 1"
  236. });
  237. args.push(curArg);
  238. }
  239. {
  240. let curArg = arguments[1];
  241. curArg = conversions["USVString"](curArg, {
  242. context: "Failed to execute 'set' on 'URLSearchParams': parameter 2"
  243. });
  244. args.push(curArg);
  245. }
  246. return this[impl].set(...args);
  247. }
  248. sort() {
  249. if (!this || !module.exports.is(this)) {
  250. throw new TypeError("Illegal invocation");
  251. }
  252. return this[impl].sort();
  253. }
  254. toString() {
  255. if (!this || !module.exports.is(this)) {
  256. throw new TypeError("Illegal invocation");
  257. }
  258. return this[impl].toString();
  259. }
  260. keys() {
  261. if (!this || !module.exports.is(this)) {
  262. throw new TypeError("Illegal invocation");
  263. }
  264. return module.exports.createDefaultIterator(this, "key");
  265. }
  266. values() {
  267. if (!this || !module.exports.is(this)) {
  268. throw new TypeError("Illegal invocation");
  269. }
  270. return module.exports.createDefaultIterator(this, "value");
  271. }
  272. entries() {
  273. if (!this || !module.exports.is(this)) {
  274. throw new TypeError("Illegal invocation");
  275. }
  276. return module.exports.createDefaultIterator(this, "key+value");
  277. }
  278. forEach(callback) {
  279. if (!this || !module.exports.is(this)) {
  280. throw new TypeError("Illegal invocation");
  281. }
  282. if (arguments.length < 1) {
  283. throw new TypeError("Failed to execute 'forEach' on 'iterable': 1 argument required, " + "but only 0 present.");
  284. }
  285. if (typeof callback !== "function") {
  286. throw new TypeError(
  287. "Failed to execute 'forEach' on 'iterable': The callback provided " + "as parameter 1 is not a function."
  288. );
  289. }
  290. const thisArg = arguments[1];
  291. let pairs = Array.from(this[impl]);
  292. let i = 0;
  293. while (i < pairs.length) {
  294. const [key, value] = pairs[i].map(utils.tryWrapperForImpl);
  295. callback.call(thisArg, value, key, this);
  296. pairs = Array.from(this[impl]);
  297. i++;
  298. }
  299. }
  300. }
  301. Object.defineProperties(URLSearchParams.prototype, {
  302. append: { enumerable: true },
  303. delete: { enumerable: true },
  304. get: { enumerable: true },
  305. getAll: { enumerable: true },
  306. has: { enumerable: true },
  307. set: { enumerable: true },
  308. sort: { enumerable: true },
  309. toString: { enumerable: true },
  310. keys: { enumerable: true },
  311. values: { enumerable: true },
  312. entries: { enumerable: true },
  313. forEach: { enumerable: true },
  314. [Symbol.toStringTag]: { value: "URLSearchParams", configurable: true },
  315. [Symbol.iterator]: { value: URLSearchParams.prototype.entries, configurable: true, writable: true }
  316. });
  317. const iface = {
  318. // When an interface-module that implements this interface as a mixin is loaded, it will append its own `.is()`
  319. // method into this array. It allows objects that directly implements *those* interfaces to be recognized as
  320. // implementing this mixin interface.
  321. _mixedIntoPredicates: [],
  322. is(obj) {
  323. if (obj) {
  324. if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation) {
  325. return true;
  326. }
  327. for (const isMixedInto of module.exports._mixedIntoPredicates) {
  328. if (isMixedInto(obj)) {
  329. return true;
  330. }
  331. }
  332. }
  333. return false;
  334. },
  335. isImpl(obj) {
  336. if (obj) {
  337. if (obj instanceof Impl.implementation) {
  338. return true;
  339. }
  340. const wrapper = utils.wrapperForImpl(obj);
  341. for (const isMixedInto of module.exports._mixedIntoPredicates) {
  342. if (isMixedInto(wrapper)) {
  343. return true;
  344. }
  345. }
  346. }
  347. return false;
  348. },
  349. convert(obj, { context = "The provided value" } = {}) {
  350. if (module.exports.is(obj)) {
  351. return utils.implForWrapper(obj);
  352. }
  353. throw new TypeError(`${context} is not of type 'URLSearchParams'.`);
  354. },
  355. createDefaultIterator(target, kind) {
  356. const iterator = Object.create(IteratorPrototype);
  357. Object.defineProperty(iterator, utils.iterInternalSymbol, {
  358. value: { target, kind, index: 0 },
  359. configurable: true
  360. });
  361. return iterator;
  362. },
  363. create(constructorArgs, privateData) {
  364. let obj = Object.create(URLSearchParams.prototype);
  365. obj = this.setup(obj, constructorArgs, privateData);
  366. return obj;
  367. },
  368. createImpl(constructorArgs, privateData) {
  369. let obj = Object.create(URLSearchParams.prototype);
  370. obj = this.setup(obj, constructorArgs, privateData);
  371. return utils.implForWrapper(obj);
  372. },
  373. _internalSetup(obj) {},
  374. setup(obj, constructorArgs, privateData) {
  375. if (!privateData) privateData = {};
  376. privateData.wrapper = obj;
  377. this._internalSetup(obj);
  378. Object.defineProperty(obj, impl, {
  379. value: new Impl.implementation(constructorArgs, privateData),
  380. configurable: true
  381. });
  382. obj[impl][utils.wrapperSymbol] = obj;
  383. if (Impl.init) {
  384. Impl.init(obj[impl], privateData);
  385. }
  386. return obj;
  387. },
  388. interface: URLSearchParams,
  389. expose: {
  390. Window: { URLSearchParams },
  391. Worker: { URLSearchParams }
  392. }
  393. }; // iface
  394. module.exports = iface;
  395. const Impl = require("./URLSearchParams-impl.js");