No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

208 líneas
4.0 KiB

  1. "use strict";
  2. const usm = require("./url-state-machine");
  3. exports.implementation = class URLImpl {
  4. constructor(constructorArgs) {
  5. const url = constructorArgs[0];
  6. const base = constructorArgs[1];
  7. let parsedBase = null;
  8. if (base !== undefined) {
  9. parsedBase = usm.basicURLParse(base);
  10. if (parsedBase.failure) {
  11. throw new TypeError("Invalid base URL");
  12. }
  13. }
  14. const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
  15. if (parsedURL.failure) {
  16. throw new TypeError("Invalid URL");
  17. }
  18. this._url = parsedURL;
  19. // TODO: query stuff
  20. }
  21. static domainToASCII(domain) {
  22. const asciiDomain = usm.parseHost(domain);
  23. if (typeof asciiDomain !== "string") {
  24. return "";
  25. }
  26. return asciiDomain;
  27. }
  28. static domainToUnicode(domain) {
  29. const unicodeDomain = usm.parseHost(domain, true);
  30. if (typeof unicodeDomain !== "string") {
  31. return "";
  32. }
  33. return unicodeDomain;
  34. }
  35. get href() {
  36. return usm.serializeURL(this._url);
  37. }
  38. set href(v) {
  39. this._url = usm.basicURLParse(v);
  40. }
  41. get origin() {
  42. return usm.serializeURLToUnicodeOrigin(this._url);
  43. }
  44. get protocol() {
  45. return this._url.scheme + ":";
  46. }
  47. set protocol(v) {
  48. usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" });
  49. }
  50. get username() {
  51. return this._url.username;
  52. }
  53. set username(v) {
  54. if (this._url.host === null || this._url.cannotBeABaseURL) {
  55. return;
  56. }
  57. usm.setTheUsername(this._url, v);
  58. }
  59. get password() {
  60. if (this._url.password === null) {
  61. return "";
  62. }
  63. return this._url.password;
  64. }
  65. set password(v) {
  66. if (this._url.host === null || this._url.cannotBeABaseURL) {
  67. return;
  68. }
  69. usm.setThePassword(this._url, v);
  70. }
  71. get host() {
  72. const url = this._url;
  73. if (url.host === null) {
  74. return "";
  75. }
  76. if (url.port === null) {
  77. return usm.serializeHost(url.host);
  78. }
  79. return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port);
  80. }
  81. set host(v) {
  82. if (this._url.cannotBeABaseURL) {
  83. return;
  84. }
  85. usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
  86. }
  87. get hostname() {
  88. if (this._url.host === null) {
  89. return "";
  90. }
  91. return usm.serializeHost(this._url.host);
  92. }
  93. set hostname(v) {
  94. if (this._url.cannotBeABaseURL) {
  95. return;
  96. }
  97. usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
  98. }
  99. get port() {
  100. if (this._url.port === null) {
  101. return "";
  102. }
  103. return usm.serializeInteger(this._url.port);
  104. }
  105. set port(v) {
  106. if (this._url.host === null || this._url.cannotBeABaseURL || this._url.scheme === "file") {
  107. return;
  108. }
  109. usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
  110. }
  111. get pathname() {
  112. if (this._url.cannotBeABaseURL) {
  113. return this._url.path[0];
  114. }
  115. return "/" + this._url.path.join("/");
  116. }
  117. set pathname(v) {
  118. if (this._url.cannotBeABaseURL) {
  119. return;
  120. }
  121. this._url.path = [];
  122. usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
  123. }
  124. get search() {
  125. if (this._url.query === null || this._url.query === "") {
  126. return "";
  127. }
  128. return "?" + this._url.query;
  129. }
  130. set search(v) {
  131. // TODO: query stuff
  132. const url = this._url;
  133. if (v === "") {
  134. url.query = null;
  135. return;
  136. }
  137. const input = v[0] === "?" ? v.substring(1) : v;
  138. url.query = "";
  139. usm.basicURLParse(input, { url, stateOverride: "query" });
  140. }
  141. get hash() {
  142. if (this._url.fragment === null || this._url.fragment === "") {
  143. return "";
  144. }
  145. return "#" + this._url.fragment;
  146. }
  147. set hash(v) {
  148. if (this._url.scheme === "javascript") {
  149. return;
  150. }
  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. };