UNPKG

3 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 html_script_tag_1 = require("./html-script-tag");
27const p = dom5.predicates;
28const isJsScriptNode = p.AND(p.hasTagName('script'), p.OR(p.NOT(p.hasAttr('type')), p.hasAttrValue('type', 'text/javascript'), p.hasAttrValue('type', 'application/javascript'), p.hasAttrValue('type', 'module')));
29class HtmlScriptScanner {
30 scan(document, visit) {
31 return __awaiter(this, void 0, void 0, function* () {
32 const features = [];
33 const myVisitor = (node) => {
34 if (isJsScriptNode(node)) {
35 const src = dom5.getAttribute(node, 'src');
36 if (src) {
37 features.push(new html_script_tag_1.ScannedScriptTagImport(src, document.sourceRangeForNode(node), document.sourceRangeForAttributeValue(node, 'src'), { language: 'html', node, containingDocument: document }, dom5.getAttribute(node, 'type') === 'module'));
38 }
39 else {
40 const locationOffset = model_1.getLocationOffsetOfStartOfTextContent(node, document);
41 const attachedCommentText = model_1.getAttachedCommentText(node) || '';
42 const contents = dom5.getTextContent(node);
43 features.push(new model_1.ScannedInlineDocument('js', contents, locationOffset, attachedCommentText, document.sourceRangeForNode(node), { language: 'html', node, containingDocument: document }));
44 }
45 }
46 };
47 yield visit(myVisitor);
48 return { features };
49 });
50 }
51}
52exports.HtmlScriptScanner = HtmlScriptScanner;
53//# sourceMappingURL=html-script-scanner.js.map
\No newline at end of file