UNPKG

3.5 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 p = dom5.predicates;
28const isStyleElement = p.AND(p.hasTagName('style'), p.OR(p.NOT(p.hasAttr('type')), p.hasAttrValue('type', 'text/css')));
29const isStyleLink = p.AND(p.hasTagName('link'), p.hasSpaceSeparatedAttrValue('rel', 'stylesheet'), p.hasAttr('href'));
30const isStyleNode = p.OR(isStyleElement, isStyleLink);
31class HtmlStyleScanner {
32 scan(document, visit) {
33 return __awaiter(this, void 0, void 0, function* () {
34 const features = [];
35 const visitor = (node) => __awaiter(this, void 0, void 0, function* () {
36 if (isStyleNode(node)) {
37 const tagName = node.nodeName;
38 if (tagName === 'link') {
39 const href = dom5.getAttribute(node, 'href');
40 features.push(new model_1.ScannedImport('html-style', href, document.sourceRangeForNode(node), document.sourceRangeForAttributeValue(node, 'href'), { language: 'html', node, containingDocument: document }, true));
41 }
42 else {
43 const contents = dom5.getTextContent(node);
44 const locationOffset = model_1.getLocationOffsetOfStartOfTextContent(node, document);
45 const commentText = model_1.getAttachedCommentText(node) || '';
46 features.push(new model_1.ScannedInlineDocument('css', contents, locationOffset, commentText, document.sourceRangeForNode(node), { language: 'html', node, containingDocument: document }));
47 }
48 }
49 // Descend into templates.
50 if (node.tagName === 'template') {
51 const content = parse5_1.treeAdapters.default.getTemplateContent(node);
52 if (content) {
53 for (const n of dom5.depthFirst(content)) {
54 visitor(n);
55 }
56 }
57 }
58 });
59 yield visit(visitor);
60 return { features };
61 });
62 }
63}
64exports.HtmlStyleScanner = HtmlStyleScanner;
65//# sourceMappingURL=html-style-scanner.js.map
\No newline at end of file