URL-impl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. const usm = require("./url-state-machine");
  3. const urlencoded = require("./urlencoded");
  4. const URLSearchParams = require("./URLSearchParams");
  5. exports.implementation = class URLImpl {
  6. constructor(globalObject, constructorArgs) {
  7. const url = constructorArgs[0];
  8. const base = constructorArgs[1];
  9. let parsedBase = null;
  10. if (base !== undefined) {
  11. parsedBase = usm.basicURLParse(base);
  12. if (parsedBase === null) {
  13. throw new TypeError(`Invalid base URL: ${base}`);
  14. }
  15. }
  16. const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
  17. if (parsedURL === null) {
  18. throw new TypeError(`Invalid URL: ${url}`);
  19. }
  20. const query = parsedURL.query !== null ? parsedURL.query : "";
  21. this._url = parsedURL;
  22. // We cannot invoke the "new URLSearchParams object" algorithm without going through the constructor, which strips
  23. // question mark by default. Therefore the doNotStripQMark hack is used.
  24. this._query = URLSearchParams.createImpl(globalObject, [query], { doNotStripQMark: true });
  25. this._query._url = this;
  26. }
  27. get href() {
  28. return usm.serializeURL(this._url);
  29. }
  30. set href(v) {
  31. const parsedURL = usm.basicURLParse(v);
  32. if (parsedURL === null) {
  33. throw new TypeError(`Invalid URL: ${v}`);
  34. }
  35. this._url = parsedURL;
  36. this._query._list.splice(0);
  37. const { query } = parsedURL;
  38. if (query !== null) {
  39. this._query._list = urlencoded.parseUrlencodedString(query);
  40. }
  41. }
  42. get origin() {
  43. return usm.serializeURLOrigin(this._url);
  44. }
  45. get protocol() {
  46. return `${this._url.scheme}:`;
  47. }
  48. set protocol(v) {
  49. usm.basicURLParse(`${v}:`, { url: this._url, stateOverride: "scheme start" });
  50. }
  51. get username() {
  52. return this._url.username;
  53. }
  54. set username(v) {
  55. if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
  56. return;
  57. }
  58. usm.setTheUsername(this._url, v);
  59. }
  60. get password() {
  61. return this._url.password;
  62. }
  63. set password(v) {
  64. if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
  65. return;
  66. }
  67. usm.setThePassword(this._url, v);
  68. }
  69. get host() {
  70. const url = this._url;
  71. if (url.host === null) {
  72. return "";
  73. }
  74. if (url.port === null) {
  75. return usm.serializeHost(url.host);
  76. }
  77. return `${usm.serializeHost(url.host)}:${usm.serializeInteger(url.port)}`;
  78. }
  79. set host(v) {
  80. if (usm.hasAnOpaquePath(this._url)) {
  81. return;
  82. }
  83. usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
  84. }
  85. get hostname() {
  86. if (this._url.host === null) {
  87. return "";
  88. }
  89. return usm.serializeHost(this._url.host);
  90. }
  91. set hostname(v) {
  92. if (usm.hasAnOpaquePath(this._url)) {
  93. return;
  94. }
  95. usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
  96. }
  97. get port() {
  98. if (this._url.port === null) {
  99. return "";
  100. }
  101. return usm.serializeInteger(this._url.port);
  102. }
  103. set port(v) {
  104. if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
  105. return;
  106. }
  107. if (v === "") {
  108. this._url.port = null;
  109. } else {
  110. usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
  111. }
  112. }
  113. get pathname() {
  114. return usm.serializePath(this._url);
  115. }
  116. set pathname(v) {
  117. if (usm.hasAnOpaquePath(this._url)) {
  118. return;
  119. }
  120. this._url.path = [];
  121. usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
  122. }
  123. get search() {
  124. if (this._url.query === null || this._url.query === "") {
  125. return "";
  126. }
  127. return `?${this._url.query}`;
  128. }
  129. set search(v) {
  130. const url = this._url;
  131. if (v === "") {
  132. url.query = null;
  133. this._query._list = [];
  134. return;
  135. }
  136. const input = v[0] === "?" ? v.substring(1) : v;
  137. url.query = "";
  138. usm.basicURLParse(input, { url, stateOverride: "query" });
  139. this._query._list = urlencoded.parseUrlencodedString(input);
  140. }
  141. get searchParams() {
  142. return this._query;
  143. }
  144. get hash() {
  145. if (this._url.fragment === null || this._url.fragment === "") {
  146. return "";
  147. }
  148. return `#${this._url.fragment}`;
  149. }
  150. set hash(v) {
  151. if (v === "") {
  152. this._url.fragment = null;
  153. return;
  154. }
  155. const input = v[0] === "#" ? v.substring(1) : v;
  156. this._url.fragment = "";
  157. usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
  158. }
  159. toJSON() {
  160. return this.href;
  161. }
  162. };