You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

417 lines
13 KiB

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