UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2015 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 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const dom5_1 = require("dom5");
17const dom5 = require("dom5/lib/index-next");
18const parse5_1 = require("parse5");
19const html_document_1 = require("./html-document");
20class HtmlParser {
21 /**
22 * Parse html into ASTs.
23 *
24 * @param {string} htmlString an HTML document.
25 * @param {string} href is the path of the document.
26 */
27 parse(contents, url, urlResolver, inlineInfo) {
28 const ast = parse5_1.parse(contents, { locationInfo: true });
29 // There should be at most one <base> tag and it must be inside <head> tag.
30 const baseTag = dom5.query(ast, dom5_1.predicates.AND(dom5_1.predicates.parentMatches(dom5_1.predicates.hasTagName('head')), dom5_1.predicates.hasTagName('base'), dom5_1.predicates.hasAttr('href')));
31 const isInline = !!inlineInfo;
32 inlineInfo = inlineInfo || {};
33 let baseUrl = inlineInfo.baseUrl !== undefined ? inlineInfo.baseUrl : url;
34 if (baseTag) {
35 const baseTagHref = dom5_1.getAttribute(baseTag, 'href');
36 const resolvedBaseTagHref = urlResolver.resolve(url, baseTagHref, undefined);
37 if (resolvedBaseTagHref !== undefined) {
38 baseUrl = resolvedBaseTagHref;
39 }
40 }
41 return new html_document_1.ParsedHtmlDocument({
42 url,
43 baseUrl,
44 contents,
45 ast,
46 locationOffset: inlineInfo.locationOffset,
47 astNode: inlineInfo.astNode,
48 isInline,
49 });
50 }
51}
52exports.HtmlParser = HtmlParser;
53//# sourceMappingURL=html-parser.js.map
\No newline at end of file