List<ScriptPlaceholder> js = Lists.newArrayList();
URI baseUriForJsModules = null;
for (Iterator<JobEnvelope> it = jobs.getJobs().iterator(); it.hasNext();) {
JobEnvelope env = it.next();
Job job = env.job;
switch (env.sourceType) {
case CSS:
if (!env.fromCache) {
css.add(new ValidatedStylesheet(
env, (CssTree.StyleSheet) job.getRoot(), job.getBaseUri()));
it.remove();
}
break;
case HTML:
html.add(new IhtmlRoot(
env, ((Dom) job.getRoot()).getValue(), job.getBaseUri()));
// Module loading in embedded <script>s should use the URI of the
// HTML file as the base URI. We use a heuristic that there's only
// one HTML file per compilation task, and use the URI of that.
if (baseUriForJsModules == null) {
baseUriForJsModules = job.getBaseUri();
}
it.remove();
break;
case JS:
if (env.placeholderId != null) {
js.add(new ScriptPlaceholder(env, env.job.getRoot()));
it.remove();
}
break;
default: break;
}
}
// TODO(ihab.awad): We do *not* want to support multiple HTML files
// being cajoled at once since this can be mis-used for modularity
// and we set up expectations on the part of our users to
// maintain this behavior, regardless of whatever complexity that
// might entail.
MessageQueue mq = jobs.getMessageQueue();
TemplateSanitizer ts = new TemplateSanitizer(htmlSchema, mq);
for (IhtmlRoot ihtmlRoot : html) {
ts.sanitize(ihtmlRoot.root);
}
TemplateCompiler tc = new TemplateCompiler(
html, css, js, cssSchema, htmlSchema,
jobs.getPluginMeta(), jobs.getMessageContext(), mq);
Pair<List<SafeHtmlChunk>, List<SafeJsChunk>> htmlAndJs = tc.getSafeHtml(
DomParser.makeDocument(null, null));
for (SafeHtmlChunk outputHtml : htmlAndJs.a) {
Job outJob = makeJobFromHtml(outputHtml.root, outputHtml.baseUri);
if (outJob != null) {
jobs.getJobs().add(outputHtml.source.withJob(outJob));
}
}