-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathHTMLImportTranscoder.js
95 lines (82 loc) · 3.32 KB
/
HTMLImportTranscoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2015 Traceur Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Applies Traceur to HTML Import scripts related to a Web page.
import {StringMap} from './util/StringMap.js';
import {WebPageTranscoder, scriptSelector} from './WebPageTranscoder.js';
const importSelector = 'link[rel=import][href]';
export class HTMLImportTranscoder {
constructor() {
this.importsToProcess_ = [];
}
findAllChildrenHTMLImports_(parentImportNodes) {
let foundImportNodes = [];
// process any import children
for (let parentIndex = 0; parentIndex < parentImportNodes.length; parentIndex++) {
let parentLink = parentImportNodes[parentIndex];
let childImportNodes = parentLink.import.querySelectorAll(importSelector);
if (childImportNodes.length > 0)
this.findAllChildrenHTMLImports_(childImportNodes);
this.importsToProcess_.push(parentLink);
}
}
filterHTMLImports_(importNodes) {
// process any import children
this.findAllChildrenHTMLImports_(importNodes);
// find all scripts to import
let importsToParse = [];
let dupFilterMap = new StringMap();
for (let index = 0; index < this.importsToProcess_.length; index++) {
let processLink = this.importsToProcess_[index];
if (!dupFilterMap.has(processLink.href)) {
dupFilterMap.set(processLink.href, 0);
let scripts = processLink.import.querySelectorAll(scriptSelector);
if (scripts.length > 0)
importsToParse.push({href: processLink.href, scripts: scripts});
}
}
this.importsToProcess_ = [];
return importsToParse;
}
selectAndProcessHTMLImports(importNodes, done) {
// extract all imports and their child imports
let importInfoList = this.filterHTMLImports_(importNodes);
if (importInfoList.length === 0)
done();
// creates a transcoder for each HTMLImport and parse their script elements
let processCount = importInfoList.length;
importInfoList.forEach((importInfo) => {
let transcoder = new WebPageTranscoder(importInfo.href);
transcoder.addFilesFromScriptElements(importInfo.scripts, () => {
processCount--;
if (processCount === 0 && done)
done();
});
});
}
run(done = () => {}) {
let ready = document.readyState;
if (ready === 'complete' || ready === 'loaded') {
let importNodes = document.querySelectorAll(importSelector);
if (importNodes.length > 0)
this.selectAndProcessHTMLImports(importNodes, done);
} else {
document.addEventListener('HTMLImportsLoaded',
(event) => {
let importNodes = event.detail && event.detail.allImports ? event.detail.allImports : document.querySelectorAll(importSelector);
if (importNodes.length > 0)
this.selectAndProcessHTMLImports(importNodes, done);
});
}
}
}