UNPKG

2.31 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const Builder_1 = require("./Builder");
13const utils = require("./utils");
14const path = require("path");
15/*@internal*/
16class Import extends Builder_1.default {
17 constructor(spec, node, importLocation, relativeRoot, source) {
18 super(spec, node);
19 this.importLocation = importLocation;
20 this.relativeRoot = relativeRoot;
21 this.source = source;
22 }
23 static build(spec, node, root) {
24 return __awaiter(this, void 0, void 0, function* () {
25 const href = node.getAttribute('href');
26 if (!href)
27 throw new Error('Import missing href attribute.');
28 const importPath = path.join(root, href);
29 const relativeRoot = path.dirname(importPath);
30 const html = yield spec.fetch(importPath);
31 const imp = new Import(spec, node, importPath, relativeRoot, html);
32 spec.imports.push(imp);
33 const importDom = utils.htmlToDom(html);
34 node.dom = importDom;
35 node.source = html;
36 node.importPath = importPath;
37 const importDoc = importDom.window.document;
38 const nodes = importDoc.body.childNodes;
39 const frag = spec.doc.createDocumentFragment();
40 for (let i = 0; i < nodes.length; i++) {
41 const node = nodes[i];
42 const importedNode = spec.doc.adoptNode(node);
43 frag.appendChild(importedNode);
44 }
45 node.appendChild(frag);
46 return imp;
47 });
48 }
49}
50exports.default = Import;