1 | ;
|
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 | */
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const model_1 = require("../model/model");
|
17 | /**
|
18 | * <script> tags are represented in two different ways: as inline documents,
|
19 | * or as imports, depending on whether the tag has a `src` attribute. This class
|
20 | * represents a script tag with a `src` attribute as an import, so that the
|
21 | * analyzer loads and parses the referenced document.
|
22 | */
|
23 | class ScriptTagImport extends model_1.Import {
|
24 | constructor(url, originalUrl, type, document, sourceRange, urlSourceRange, ast, warnings, lazy, isModule) {
|
25 | super(url, originalUrl, type, document, sourceRange, urlSourceRange, ast, warnings, lazy);
|
26 | this.type = 'html-script';
|
27 | this.isModule = isModule;
|
28 | }
|
29 | }
|
30 | exports.ScriptTagImport = ScriptTagImport;
|
31 | class ScannedScriptTagImport extends model_1.ScannedImport {
|
32 | constructor(url, sourceRange, urlSourceRange, ast, isModule) {
|
33 | super('html-script', url, sourceRange, urlSourceRange, ast, false);
|
34 | this.isModule = isModule;
|
35 | }
|
36 | resolve(document) {
|
37 | if (this.isModule) {
|
38 | // Module script imports are just normal imports, they shouldn't
|
39 | // depend on globals that any HTML context might involve.
|
40 | return super.resolve(document);
|
41 | }
|
42 | const resolvedUrl = this.getLoadableUrlOrWarn(document);
|
43 | if (this.url === undefined || resolvedUrl === undefined) {
|
44 | // Warning will already have been added to the document if necessary, so
|
45 | // we can just return here.
|
46 | return undefined;
|
47 | }
|
48 | // TODO(justinfagnani): warn if the same URL is loaded from more than one
|
49 | // non-module script tag
|
50 | // TODO(justinfagnani): Use the analyzer cache, since this is duplicating an
|
51 | // analysis of the external script, but the document the analyzer has
|
52 | // doesn't have its container as a feature.
|
53 | // A better design might be to have the import itself be in charge of
|
54 | // producing document objects. This will fit better with JS modules, where
|
55 | // the type attribute drives how the document is parsed.
|
56 | //
|
57 | // See https://github.com/Polymer/polymer-analyzer/issues/615
|
58 | const scannedDocument = document._analysisContext._getScannedDocument(resolvedUrl);
|
59 | let importedDocument;
|
60 | if (scannedDocument === undefined) {
|
61 | // not found or syntax error
|
62 | this.addCouldNotLoadWarning(document);
|
63 | importedDocument = undefined;
|
64 | }
|
65 | else {
|
66 | importedDocument =
|
67 | new model_1.Document(scannedDocument, document._analysisContext);
|
68 | // Scripts regularly make use of global variables or functions (e.g.
|
69 | // `Polymer()`, `$('#some-id')`, etc) that are defined in libraries
|
70 | // which are loaded via prior script tags or HTML imports. Since
|
71 | // JavaScript defined within `<script>` tags or loaded by a
|
72 | // `<script src=...>` share scope with other scripts previously
|
73 | // loaded by the page, this synthetic import is added to support
|
74 | // queries for features of the HTML document which should be "visible"
|
75 | // to the JavaScript document.
|
76 | const backReference = new model_1.DocumentBackreference(document);
|
77 | importedDocument._addFeature(backReference);
|
78 | importedDocument.resolve();
|
79 | }
|
80 | return this.constructImport(resolvedUrl, this.url, importedDocument, document);
|
81 | }
|
82 | constructImport(resolvedUrl, relativeUrl, importedDocument, _containingDocument) {
|
83 | return new ScriptTagImport(resolvedUrl, relativeUrl, this.type, importedDocument, this.sourceRange, this.urlSourceRange, this.astNode, this.warnings, this.lazy, this.isModule);
|
84 | }
|
85 | }
|
86 | exports.ScannedScriptTagImport = ScannedScriptTagImport;
|
87 | //# sourceMappingURL=html-script-tag.js.map |
\ | No newline at end of file |