jsdom
Version:
A JavaScript implementation of many web standards
68 lines (53 loc) • 2.03 kB
JavaScript
;
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
const Document = require("../../../generated/idl/Document");
const DocumentFragment = require("../../../generated/idl/DocumentFragment");
const { clone } = require("../node");
class HTMLTemplateElementImpl extends HTMLElementImpl {
constructor(globalObject, args, privateData) {
super(globalObject, args, privateData);
const doc = this._appropriateTemplateContentsOwnerDocument(this._ownerDocument);
this._templateContents = DocumentFragment.createImpl(this._globalObject, [], {
ownerDocument: doc,
host: this
});
}
// https://html.spec.whatwg.org/multipage/scripting.html#appropriate-template-contents-owner-document
_appropriateTemplateContentsOwnerDocument(doc) {
if (!doc._isInertTemplateDocument) {
if (doc._associatedInertTemplateDocument === undefined) {
const newDoc = Document.createImpl(this._globalObject, [], {
options: {
parsingMode: doc._parsingMode,
encoding: doc._encoding
}
});
newDoc._isInertTemplateDocument = true;
doc._associatedInertTemplateDocument = newDoc;
}
doc = doc._associatedInertTemplateDocument;
}
return doc;
}
// https://html.spec.whatwg.org/multipage/scripting.html#template-adopting-steps
_adoptingSteps(oldDocument) {
super._adoptingSteps(oldDocument);
const doc = this._appropriateTemplateContentsOwnerDocument(this._ownerDocument);
doc._adoptNode(this._templateContents);
}
get content() {
return this._templateContents;
}
// https://html.spec.whatwg.org/multipage/scripting.html#the-template-element:concept-node-clone-ext
_cloningSteps(copy, subtree) {
if (!subtree) {
return;
}
for (const child of this._templateContents._children()) {
clone(child, copy._templateContents._ownerDocument, true, copy._templateContents);
}
}
}
module.exports = {
implementation: HTMLTemplateElementImpl
};