URLSearchParams.js 12 KB

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