UNPKG

3.91 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 parse5_1 = require("parse5");
26const element_reference_1 = require("../model/element-reference");
27const isCustomElement = dom5.predicates.hasMatchingTagName(/(.+-)+.+/);
28/**
29 * Scans for HTML element references/uses in a given document.
30 * All elements will be detected, including anything in <head>.
31 * This scanner will not be loaded by default, but the custom
32 * element extension of it will be.
33 */
34class HtmlElementReferenceScanner {
35 matches(node) {
36 return !!node;
37 }
38 scan(document, visit) {
39 return __awaiter(this, void 0, void 0, function* () {
40 const elements = [];
41 const visitor = (node) => {
42 if (node.tagName && this.matches(node)) {
43 const element = new element_reference_1.ScannedElementReference(node.tagName, document.sourceRangeForNode(node), { language: 'html', containingDocument: document, node });
44 if (node.attrs) {
45 for (const attr of node.attrs) {
46 element.attributes.set(attr.name, {
47 name: attr.name,
48 value: attr.value,
49 sourceRange: document.sourceRangeForAttribute(node, attr.name),
50 nameSourceRange: document.sourceRangeForAttributeName(node, attr.name),
51 valueSourceRange: document.sourceRangeForAttributeValue(node, attr.name)
52 });
53 }
54 }
55 elements.push(element);
56 }
57 // Descend into templates.
58 if (node.tagName === 'template') {
59 const content = parse5_1.treeAdapters.default.getTemplateContent(node);
60 if (content) {
61 for (const n of dom5.depthFirst(content)) {
62 visitor(n);
63 }
64 }
65 }
66 };
67 yield visit(visitor);
68 return { features: elements };
69 });
70 }
71}
72exports.HtmlElementReferenceScanner = HtmlElementReferenceScanner;
73/**
74 * Scans for custom element references/uses.
75 * All custom elements will be detected except <dom-module>.
76 * This is a default scanner.
77 */
78class HtmlCustomElementReferenceScanner extends HtmlElementReferenceScanner {
79 matches(node) {
80 return isCustomElement(node) && node.nodeName !== 'dom-module';
81 }
82}
83exports.HtmlCustomElementReferenceScanner = HtmlCustomElementReferenceScanner;
84//# sourceMappingURL=html-element-reference-scanner.js.map
\No newline at end of file