2018-04-16 23:43:45 +08:00
|
|
|
module.exports = class FakeDocument {
|
|
|
|
constructor() {
|
|
|
|
this.head = this.createElement("head");
|
|
|
|
}
|
|
|
|
|
|
|
|
createElement(type) {
|
|
|
|
return new FakeElement(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
getElementsByTagName(name) {
|
|
|
|
if (name === "head") return [this.head];
|
|
|
|
throw new Error(
|
|
|
|
`FakeDocument.getElementsByTagName(${name}): not implemented`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FakeElement {
|
2018-04-19 21:54:09 +08:00
|
|
|
constructor(type, autoload = true) {
|
2018-04-16 23:43:45 +08:00
|
|
|
this._type = type;
|
2018-04-19 21:54:09 +08:00
|
|
|
this._autoload = autoload;
|
2018-04-16 23:43:45 +08:00
|
|
|
this._children = [];
|
|
|
|
this._attributes = Object.create(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
appendChild(node) {
|
|
|
|
this._children.push(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
setAttribute(name, value) {
|
|
|
|
this._attributes[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAttribute(name) {
|
|
|
|
return this._attributes[name];
|
|
|
|
}
|
2018-04-19 03:10:21 +08:00
|
|
|
|
|
|
|
get onload() {
|
|
|
|
return this._onload;
|
|
|
|
}
|
|
|
|
|
|
|
|
set onload(script) {
|
2018-04-19 21:54:09 +08:00
|
|
|
if (this._autoload === true && typeof script === "function") {
|
2018-04-19 03:10:21 +08:00
|
|
|
script();
|
|
|
|
}
|
|
|
|
this._onload = script;
|
|
|
|
}
|
|
|
|
|
|
|
|
get src() {
|
|
|
|
return this._src;
|
|
|
|
}
|
|
|
|
|
|
|
|
set src(src) {
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const publicPath = __webpack_public_path__;
|
|
|
|
eval(`
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const content = fs.readFileSync(
|
|
|
|
path.join(__dirname, '${src}'.replace('${publicPath}', '')), "utf-8"
|
|
|
|
)
|
|
|
|
eval(content);
|
|
|
|
`);
|
|
|
|
this._src = src;
|
|
|
|
}
|
2018-04-16 23:43:45 +08:00
|
|
|
}
|