URL.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const Impl = require(".//URL-impl.js");
  5. const impl = utils.implSymbol;
  6. function URL(url) {
  7. if (!this || this[impl] || !(this instanceof URL)) {
  8. throw new TypeError("Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function.");
  9. }
  10. if (arguments.length < 1) {
  11. throw new TypeError("Failed to construct 'URL': 1 argument required, but only " + arguments.length + " present.");
  12. }
  13. const args = [];
  14. for (let i = 0; i < arguments.length && i < 2; ++i) {
  15. args[i] = arguments[i];
  16. }
  17. args[0] = conversions["USVString"](args[0]);
  18. if (args[1] !== undefined) {
  19. args[1] = conversions["USVString"](args[1]);
  20. }
  21. module.exports.setup(this, args);
  22. }
  23. URL.prototype.toJSON = function toJSON() {
  24. if (!this || !module.exports.is(this)) {
  25. throw new TypeError("Illegal invocation");
  26. }
  27. const args = [];
  28. for (let i = 0; i < arguments.length && i < 0; ++i) {
  29. args[i] = arguments[i];
  30. }
  31. return this[impl].toJSON.apply(this[impl], args);
  32. };
  33. Object.defineProperty(URL.prototype, "href", {
  34. get() {
  35. return this[impl].href;
  36. },
  37. set(V) {
  38. V = conversions["USVString"](V);
  39. this[impl].href = V;
  40. },
  41. enumerable: true,
  42. configurable: true
  43. });
  44. URL.prototype.toString = function () {
  45. if (!this || !module.exports.is(this)) {
  46. throw new TypeError("Illegal invocation");
  47. }
  48. return this.href;
  49. };
  50. Object.defineProperty(URL.prototype, "origin", {
  51. get() {
  52. return this[impl].origin;
  53. },
  54. enumerable: true,
  55. configurable: true
  56. });
  57. Object.defineProperty(URL.prototype, "protocol", {
  58. get() {
  59. return this[impl].protocol;
  60. },
  61. set(V) {
  62. V = conversions["USVString"](V);
  63. this[impl].protocol = V;
  64. },
  65. enumerable: true,
  66. configurable: true
  67. });
  68. Object.defineProperty(URL.prototype, "username", {
  69. get() {
  70. return this[impl].username;
  71. },
  72. set(V) {
  73. V = conversions["USVString"](V);
  74. this[impl].username = V;
  75. },
  76. enumerable: true,
  77. configurable: true
  78. });
  79. Object.defineProperty(URL.prototype, "password", {
  80. get() {
  81. return this[impl].password;
  82. },
  83. set(V) {
  84. V = conversions["USVString"](V);
  85. this[impl].password = V;
  86. },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. Object.defineProperty(URL.prototype, "host", {
  91. get() {
  92. return this[impl].host;
  93. },
  94. set(V) {
  95. V = conversions["USVString"](V);
  96. this[impl].host = V;
  97. },
  98. enumerable: true,
  99. configurable: true
  100. });
  101. Object.defineProperty(URL.prototype, "hostname", {
  102. get() {
  103. return this[impl].hostname;
  104. },
  105. set(V) {
  106. V = conversions["USVString"](V);
  107. this[impl].hostname = V;
  108. },
  109. enumerable: true,
  110. configurable: true
  111. });
  112. Object.defineProperty(URL.prototype, "port", {
  113. get() {
  114. return this[impl].port;
  115. },
  116. set(V) {
  117. V = conversions["USVString"](V);
  118. this[impl].port = V;
  119. },
  120. enumerable: true,
  121. configurable: true
  122. });
  123. Object.defineProperty(URL.prototype, "pathname", {
  124. get() {
  125. return this[impl].pathname;
  126. },
  127. set(V) {
  128. V = conversions["USVString"](V);
  129. this[impl].pathname = V;
  130. },
  131. enumerable: true,
  132. configurable: true
  133. });
  134. Object.defineProperty(URL.prototype, "search", {
  135. get() {
  136. return this[impl].search;
  137. },
  138. set(V) {
  139. V = conversions["USVString"](V);
  140. this[impl].search = V;
  141. },
  142. enumerable: true,
  143. configurable: true
  144. });
  145. Object.defineProperty(URL.prototype, "hash", {
  146. get() {
  147. return this[impl].hash;
  148. },
  149. set(V) {
  150. V = conversions["USVString"](V);
  151. this[impl].hash = V;
  152. },
  153. enumerable: true,
  154. configurable: true
  155. });
  156. module.exports = {
  157. is(obj) {
  158. return !!obj && obj[impl] instanceof Impl.implementation;
  159. },
  160. create(constructorArgs, privateData) {
  161. let obj = Object.create(URL.prototype);
  162. this.setup(obj, constructorArgs, privateData);
  163. return obj;
  164. },
  165. setup(obj, constructorArgs, privateData) {
  166. if (!privateData) privateData = {};
  167. privateData.wrapper = obj;
  168. obj[impl] = new Impl.implementation(constructorArgs, privateData);
  169. obj[impl][utils.wrapperSymbol] = obj;
  170. },
  171. interface: URL,
  172. expose: {
  173. Window: { URL: URL },
  174. Worker: { URL: URL }
  175. }
  176. };