jsdom-url
Version:
a proxy JSDOM URL object
113 lines (107 loc) • 3.11 kB
JavaScript
;
/**
* Created by user on 2018/2/11/011.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const class_proxy_1 = require("class-proxy");
const URL_impl_1 = require("whatwg-url/lib/URL-impl");
const URLSearchParams_1 = require("./URLSearchParams");
exports.URLSearchParamsImpl = URLSearchParams_1.URLSearchParamsImpl;
exports.URLSearchParamsImplCore = URLSearchParams_1.URLSearchParamsImplCore;
const URL_1 = require("./valid/URL");
class URLImplCore extends URL_impl_1.implementation {
constructor(href, base) {
if (Array.isArray(href)) {
[href, base] = href;
}
//console.log(222, [href, base]);
super([href && href.toString(), base ? base.toString() : undefined]);
this._query = new URLSearchParams_1.URLSearchParamsImpl(this._query, {
doNotStripQMark: true,
});
//console.log(this._query);
}
static create(href, base) {
return new this(href, base);
}
get searchParams() {
return this._query;
}
get [Symbol.toStringTag]() {
return 'URLImpl';
}
inspect() {
let name = this[Symbol.toStringTag];
return `${name}("${this.toString()}")`;
}
toJSON() {
return this.href;
}
toString() {
return this.href;
}
toObject() {
const self = this;
return Object.keys(self).reduce(function (ret, k) {
ret[k] = self[k];
return ret;
}, {});
}
static isValidURLObject(url) {
return URL_1.isValidURLObject(url);
}
}
exports.URLImplCore = URLImplCore;
function packProxyURL(classURL) {
return class_proxy_1.default(classURL, {
get(target, name) {
return target[name];
},
set(target, prop, value, receiver) {
if (prop == '_query') {
value._url = target;
//console.log(value);
}
target[prop] = value;
return true;
},
ownKeys(target) {
return [
'href',
'origin',
'protocol',
'username',
'password',
'host',
'hostname',
'port',
'pathname',
'search',
'hash',
'searchParams',
];
},
getOwnPropertyDescriptor(target, prop) {
return {
value: target[prop],
enumerable: this.ownKeys(target).includes(prop),
configurable: true,
};
},
});
}
exports.packProxyURL = packProxyURL;
exports.URLImpl = packProxyURL(URLImplCore);
/*
let url = new URLImpl(['https://www.npmjs.com/package/dgeni?l=1&l=2&k=kkk']);
let url3 = new URLImplProxy([url]);
console.log(url);
console.log(url3, url3 instanceof WURLImpl);
//console.log(Object.keys(url));
//console.log(Object.keys(url3));
console.dir(url);
console.dir(url3);
console.dir(url3._url);
//console.dir(url);
*/
exports.default = exports.URLImpl;