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