UNPKG

4.97 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 model_1 = require("../model/model");
27const expression_scanner_1 = require("./expression-scanner");
28const polymer_element_1 = require("./polymer-element");
29const p = dom5.predicates;
30const isDomModule = p.hasTagName('dom-module');
31class ScannedDomModule {
32 constructor(id, node, sourceRange, ast, warnings, template, slots, localIds, databindings) {
33 this.warnings = [];
34 this.id = id;
35 this.node = node;
36 this.comment = model_1.getAttachedCommentText(node);
37 this.sourceRange = sourceRange;
38 this.astNode = ast;
39 this.slots = slots;
40 this.localIds = localIds;
41 this.warnings = warnings;
42 this.template = template;
43 this.databindings = databindings;
44 }
45 resolve() {
46 return new DomModule(this.node, this.id, this.comment, this.sourceRange, this.astNode, this.warnings, this.slots, this.localIds, this.template, this.databindings);
47 }
48}
49exports.ScannedDomModule = ScannedDomModule;
50class DomModule {
51 constructor(node, id, comment, sourceRange, ast, warnings, slots, localIds, template, databindings) {
52 this.kinds = new Set(['dom-module']);
53 this.identifiers = new Set();
54 this.node = node;
55 this.id = id;
56 this.comment = comment;
57 if (id) {
58 this.identifiers.add(id);
59 }
60 this.sourceRange = sourceRange;
61 this.astNode = ast;
62 this.warnings = warnings;
63 this.slots = slots;
64 this.localIds = localIds;
65 this.template = template;
66 this.databindings = databindings;
67 }
68}
69exports.DomModule = DomModule;
70class DomModuleScanner {
71 scan(document, visit) {
72 return __awaiter(this, void 0, void 0, function* () {
73 const domModules = [];
74 yield visit((node) => {
75 if (isDomModule(node)) {
76 const children = dom5.defaultChildNodes(node) || [];
77 const template = children.find(dom5.predicates.hasTagName('template'));
78 let slots = [];
79 let localIds = [];
80 let databindings = [];
81 let warnings = [];
82 if (template) {
83 const templateContent = parse5_1.treeAdapters.default.getTemplateContent(template);
84 slots = [...dom5.queryAll(templateContent, dom5.predicates.hasTagName('slot'))]
85 .map((s) => new model_1.Slot(dom5.getAttribute(s, 'name') || '', document.sourceRangeForNode(s), {
86 language: 'html',
87 node: s,
88 containingDocument: document
89 }));
90 localIds =
91 [...dom5.queryAll(templateContent, dom5.predicates.hasAttr('id'))]
92 .map((e) => new polymer_element_1.LocalId(dom5.getAttribute(e, 'id'), document.sourceRangeForNode(e), e.nodeName));
93 const results = expression_scanner_1.scanDatabindingTemplateForExpressions(document, template);
94 warnings = results.warnings;
95 databindings = results.expressions;
96 }
97 domModules.push(new ScannedDomModule(dom5.getAttribute(node, 'id'), node, document.sourceRangeForNode(node), { language: 'html', node, containingDocument: document }, warnings, template, slots, localIds, databindings));
98 }
99 });
100 return { features: domModules };
101 });
102 }
103}
104exports.DomModuleScanner = DomModuleScanner;
105//# sourceMappingURL=dom-module-scanner.js.map
\No newline at end of file