URL.js 8.3 KB

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