[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 8 | const Template = require("../Template");
|
---|
| 9 | const HelperRuntimeModule = require("./HelperRuntimeModule");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 12 |
|
---|
| 13 | class RelativeUrlRuntimeModule extends HelperRuntimeModule {
|
---|
| 14 | constructor() {
|
---|
| 15 | super("relative url");
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * @returns {string | null} runtime code
|
---|
| 20 | */
|
---|
| 21 | generate() {
|
---|
| 22 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
| 23 | const { runtimeTemplate } = compilation;
|
---|
| 24 | return Template.asString([
|
---|
| 25 | `${RuntimeGlobals.relativeUrl} = function RelativeURL(url) {`,
|
---|
| 26 | Template.indent([
|
---|
| 27 | 'var realUrl = new URL(url, "x:/");',
|
---|
| 28 | "var values = {};",
|
---|
| 29 | "for (var key in realUrl) values[key] = realUrl[key];",
|
---|
| 30 | "values.href = url;",
|
---|
| 31 | 'values.pathname = url.replace(/[?#].*/, "");',
|
---|
| 32 | 'values.origin = values.protocol = "";',
|
---|
| 33 | `values.toString = values.toJSON = ${runtimeTemplate.returningFunction(
|
---|
| 34 | "url"
|
---|
| 35 | )};`,
|
---|
| 36 | "for (var key in values) Object.defineProperty(this, key, { enumerable: true, configurable: true, value: values[key] });"
|
---|
| 37 | ]),
|
---|
| 38 | "};",
|
---|
| 39 | `${RuntimeGlobals.relativeUrl}.prototype = URL.prototype;`
|
---|
| 40 | ]);
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | module.exports = RelativeUrlRuntimeModule;
|
---|