UNPKG

3.45 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24const dom5 = require("dom5/lib/index-next");
25const model_1 = require("../model/model");
26const p = dom5.predicates;
27const linkTag = p.hasTagName('link');
28const notCssLink = p.NOT(p.hasAttrValue('type', 'css'));
29const isHtmlImportNode = p.AND(linkTag, p.hasAttr('href'), p.hasSpaceSeparatedAttrValue('rel', 'import'), p.NOT(p.hasSpaceSeparatedAttrValue('rel', 'lazy-import')), notCssLink, p.NOT(p.parentMatches(p.hasTagName('template'))));
30const isLazyImportNode = p.AND(p.hasTagName('link'), p.hasSpaceSeparatedAttrValue('rel', 'lazy-import'), p.hasAttr('href'), p.NOT(p.hasSpaceSeparatedAttrValue('rel', 'import')), notCssLink, p.NOT(p.parentMatches(p.hasTagName('template'))));
31/**
32 * Scans for <link rel="import"> and <link rel="lazy-import">
33 */
34class HtmlImportScanner {
35 constructor(_lazyEdges) {
36 this._lazyEdges = _lazyEdges;
37 }
38 scan(document, visit) {
39 return __awaiter(this, void 0, void 0, function* () {
40 const imports = [];
41 const type = 'html-import';
42 yield visit((node) => {
43 let lazy;
44 if (isHtmlImportNode(node)) {
45 lazy = false;
46 }
47 else if (isLazyImportNode(node)) {
48 lazy = true;
49 }
50 else {
51 return;
52 }
53 const href = dom5.getAttribute(node, 'href');
54 imports.push(new model_1.ScannedImport(type, href, document.sourceRangeForNode(node), document.sourceRangeForAttributeValue(node, 'href'), { language: 'html', node, containingDocument: document }, lazy));
55 });
56 if (this._lazyEdges) {
57 const edges = this._lazyEdges.get(document.url);
58 if (edges) {
59 for (const edge of edges) {
60 imports.push(new model_1.ScannedImport(type,
61 // This cast is very suspicious.
62 edge, undefined, undefined, undefined, true));
63 }
64 }
65 }
66 return { features: imports };
67 });
68 }
69}
70exports.HtmlImportScanner = HtmlImportScanner;
71//# sourceMappingURL=html-import-scanner.js.map
\No newline at end of file