Blame view

lib/jsdom/living/nodes/HTMLTemplateElement-impl.js 1.99 KB
858f2bdf5   Boyan Georgiev   fixes
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  "use strict";
  
  const HTMLElementImpl = require("./HTMLElement-impl").implementation;
  
  const Document = require("../generated/Document");
  const DocumentFragment = require("../generated/DocumentFragment");
  
  const { cloningSteps, domSymbolTree } = require("../helpers/internal-constants");
  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() {
      const doc = this._appropriateTemplateContentsOwnerDocument(this._ownerDocument);
      doc._adoptNode(this._templateContents);
    }
  
    get content() {
      return this._templateContents;
    }
  
    [cloningSteps](copy, node, document, cloneChildren) {
      if (!cloneChildren) {
        return;
      }
  
      for (const child of domSymbolTree.childrenIterator(node._templateContents)) {
        const childCopy = clone(child, copy._templateContents._ownerDocument, true);
        copy._templateContents.appendChild(childCopy);
      }
    }
  }
  
  module.exports = {
    implementation: HTMLTemplateElementImpl
  };