URL.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const impl = utils.implSymbol;
  5. function URL(url) {
  6. if (!new.target) {
  7. throw new TypeError(
  8. "Failed to construct 'URL'. Please use the 'new' operator; this constructor " + "cannot be called as a function."
  9. );
  10. }
  11. if (arguments.length < 1) {
  12. throw new TypeError(
  13. "Failed to construct 'URL': 1 " + "argument required, but only " + arguments.length + " present."
  14. );
  15. }
  16. const args = [];
  17. for (let i = 0; i < arguments.length && i < 2; ++i) {
  18. args[i] = arguments[i];
  19. }
  20. args[0] = conversions["USVString"](args[0], { context: "Failed to construct 'URL': parameter 1" });
  21. if (args[1] !== undefined) {
  22. args[1] = conversions["USVString"](args[1], { context: "Failed to construct 'URL': parameter 2" });
  23. }
  24. iface.setup(this, args);
  25. }
  26. Object.defineProperty(URL, "prototype", {
  27. value: URL.prototype,
  28. writable: false,
  29. enumerable: false,
  30. configurable: false
  31. });
  32. URL.prototype.toJSON = function toJSON() {
  33. if (!this || !module.exports.is(this)) {
  34. throw new TypeError("Illegal invocation");
  35. }
  36. return this[impl].toJSON();
  37. };
  38. Object.defineProperty(URL.prototype, "href", {
  39. get() {
  40. if (!this || !module.exports.is(this)) {
  41. throw new TypeError("Illegal invocation");
  42. }
  43. return this[impl]["href"];
  44. },
  45. set(V) {
  46. if (!this || !module.exports.is(this)) {
  47. throw new TypeError("Illegal invocation");
  48. }
  49. V = conversions["USVString"](V, { context: "Failed to set the 'href' property on 'URL': The provided value" });
  50. this[impl]["href"] = V;
  51. },
  52. enumerable: true,
  53. configurable: true
  54. });
  55. URL.prototype.toString = function toString() {
  56. if (!this || !module.exports.is(this)) {
  57. throw new TypeError("Illegal invocation");
  58. }
  59. return this[impl]["href"];
  60. };
  61. Object.defineProperty(URL.prototype, "origin", {
  62. get() {
  63. if (!this || !module.exports.is(this)) {
  64. throw new TypeError("Illegal invocation");
  65. }
  66. return this[impl]["origin"];
  67. },
  68. enumerable: true,
  69. configurable: true
  70. });
  71. Object.defineProperty(URL.prototype, "protocol", {
  72. get() {
  73. if (!this || !module.exports.is(this)) {
  74. throw new TypeError("Illegal invocation");
  75. }
  76. return this[impl]["protocol"];
  77. },
  78. set(V) {
  79. if (!this || !module.exports.is(this)) {
  80. throw new TypeError("Illegal invocation");
  81. }
  82. V = conversions["USVString"](V, { context: "Failed to set the 'protocol' property on 'URL': The provided value" });
  83. this[impl]["protocol"] = V;
  84. },
  85. enumerable: true,
  86. configurable: true
  87. });
  88. Object.defineProperty(URL.prototype, "username", {
  89. get() {
  90. if (!this || !module.exports.is(this)) {
  91. throw new TypeError("Illegal invocation");
  92. }
  93. return this[impl]["username"];
  94. },
  95. set(V) {
  96. if (!this || !module.exports.is(this)) {
  97. throw new TypeError("Illegal invocation");
  98. }
  99. V = conversions["USVString"](V, { context: "Failed to set the 'username' property on 'URL': The provided value" });
  100. this[impl]["username"] = V;
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. Object.defineProperty(URL.prototype, "password", {
  106. get() {
  107. if (!this || !module.exports.is(this)) {
  108. throw new TypeError("Illegal invocation");
  109. }
  110. return this[impl]["password"];
  111. },
  112. set(V) {
  113. if (!this || !module.exports.is(this)) {
  114. throw new TypeError("Illegal invocation");
  115. }
  116. V = conversions["USVString"](V, { context: "Failed to set the 'password' property on 'URL': The provided value" });
  117. this[impl]["password"] = V;
  118. },
  119. enumerable: true,
  120. configurable: true
  121. });
  122. Object.defineProperty(URL.prototype, "host", {
  123. get() {
  124. if (!this || !module.exports.is(this)) {
  125. throw new TypeError("Illegal invocation");
  126. }
  127. return this[impl]["host"];
  128. },
  129. set(V) {
  130. if (!this || !module.exports.is(this)) {
  131. throw new TypeError("Illegal invocation");
  132. }
  133. V = conversions["USVString"](V, { context: "Failed to set the 'host' property on 'URL': The provided value" });
  134. this[impl]["host"] = V;
  135. },
  136. enumerable: true,
  137. configurable: true
  138. });
  139. Object.defineProperty(URL.prototype, "hostname", {
  140. get() {
  141. if (!this || !module.exports.is(this)) {
  142. throw new TypeError("Illegal invocation");
  143. }
  144. return this[impl]["hostname"];
  145. },
  146. set(V) {
  147. if (!this || !module.exports.is(this)) {
  148. throw new TypeError("Illegal invocation");
  149. }
  150. V = conversions["USVString"](V, { context: "Failed to set the 'hostname' property on 'URL': The provided value" });
  151. this[impl]["hostname"] = V;
  152. },
  153. enumerable: true,
  154. configurable: true
  155. });
  156. Object.defineProperty(URL.prototype, "port", {
  157. get() {
  158. if (!this || !module.exports.is(this)) {
  159. throw new TypeError("Illegal invocation");
  160. }
  161. return this[impl]["port"];
  162. },
  163. set(V) {
  164. if (!this || !module.exports.is(this)) {
  165. throw new TypeError("Illegal invocation");
  166. }
  167. V = conversions["USVString"](V, { context: "Failed to set the 'port' property on 'URL': The provided value" });
  168. this[impl]["port"] = V;
  169. },
  170. enumerable: true,
  171. configurable: true
  172. });
  173. Object.defineProperty(URL.prototype, "pathname", {
  174. get() {
  175. if (!this || !module.exports.is(this)) {
  176. throw new TypeError("Illegal invocation");
  177. }
  178. return this[impl]["pathname"];
  179. },
  180. set(V) {
  181. if (!this || !module.exports.is(this)) {
  182. throw new TypeError("Illegal invocation");
  183. }
  184. V = conversions["USVString"](V, { context: "Failed to set the 'pathname' property on 'URL': The provided value" });
  185. this[impl]["pathname"] = V;
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. Object.defineProperty(URL.prototype, "search", {
  191. get() {
  192. if (!this || !module.exports.is(this)) {
  193. throw new TypeError("Illegal invocation");
  194. }
  195. return this[impl]["search"];
  196. },
  197. set(V) {
  198. if (!this || !module.exports.is(this)) {
  199. throw new TypeError("Illegal invocation");
  200. }
  201. V = conversions["USVString"](V, { context: "Failed to set the 'search' property on 'URL': The provided value" });
  202. this[impl]["search"] = V;
  203. },
  204. enumerable: true,
  205. configurable: true
  206. });
  207. Object.defineProperty(URL.prototype, "searchParams", {
  208. get() {
  209. if (!this || !module.exports.is(this)) {
  210. throw new TypeError("Illegal invocation");
  211. }
  212. return utils.getSameObject(this, "searchParams", () => {
  213. return utils.tryWrapperForImpl(this[impl]["searchParams"]);
  214. });
  215. },
  216. enumerable: true,
  217. configurable: true
  218. });
  219. Object.defineProperty(URL.prototype, "hash", {
  220. get() {
  221. if (!this || !module.exports.is(this)) {
  222. throw new TypeError("Illegal invocation");
  223. }
  224. return this[impl]["hash"];
  225. },
  226. set(V) {
  227. if (!this || !module.exports.is(this)) {
  228. throw new TypeError("Illegal invocation");
  229. }
  230. V = conversions["USVString"](V, { context: "Failed to set the 'hash' property on 'URL': The provided value" });
  231. this[impl]["hash"] = V;
  232. },
  233. enumerable: true,
  234. configurable: true
  235. });
  236. Object.defineProperty(URL.prototype, Symbol.toStringTag, {
  237. value: "URL",
  238. writable: false,
  239. enumerable: false,
  240. configurable: true
  241. });
  242. const iface = {
  243. mixedInto: [],
  244. is(obj) {
  245. if (obj) {
  246. if (obj[impl] instanceof Impl.implementation) {
  247. return true;
  248. }
  249. for (let i = 0; i < module.exports.mixedInto.length; ++i) {
  250. if (obj instanceof module.exports.mixedInto[i]) {
  251. return true;
  252. }
  253. }
  254. }
  255. return false;
  256. },
  257. isImpl(obj) {
  258. if (obj) {
  259. if (obj instanceof Impl.implementation) {
  260. return true;
  261. }
  262. const wrapper = utils.wrapperForImpl(obj);
  263. for (let i = 0; i < module.exports.mixedInto.length; ++i) {
  264. if (wrapper instanceof module.exports.mixedInto[i]) {
  265. return true;
  266. }
  267. }
  268. }
  269. return false;
  270. },
  271. convert(obj, { context = "The provided value" } = {}) {
  272. if (module.exports.is(obj)) {
  273. return utils.implForWrapper(obj);
  274. }
  275. throw new TypeError(`${context} is not of type 'URL'.`);
  276. },
  277. create(constructorArgs, privateData) {
  278. let obj = Object.create(URL.prototype);
  279. obj = this.setup(obj, constructorArgs, privateData);
  280. return obj;
  281. },
  282. createImpl(constructorArgs, privateData) {
  283. let obj = Object.create(URL.prototype);
  284. obj = this.setup(obj, constructorArgs, privateData);
  285. return utils.implForWrapper(obj);
  286. },
  287. _internalSetup(obj) {},
  288. setup(obj, constructorArgs, privateData) {
  289. if (!privateData) privateData = {};
  290. privateData.wrapper = obj;
  291. this._internalSetup(obj);
  292. Object.defineProperty(obj, impl, {
  293. value: new Impl.implementation(constructorArgs, privateData),
  294. writable: false,
  295. enumerable: false,
  296. configurable: true
  297. });
  298. obj[impl][utils.wrapperSymbol] = obj;
  299. if (Impl.init) {
  300. Impl.init(obj[impl], privateData);
  301. }
  302. return obj;
  303. },
  304. interface: URL,
  305. expose: {
  306. Window: { URL },
  307. Worker: { URL }
  308. }
  309. }; // iface
  310. module.exports = iface;
  311. const Impl = require(".//URL-impl.js");