RhinoTestBed.class, "/js/envjs/env.js"));
int injectHtmlIndex = inputs.size();
List<Pair<String, InputSource>> scriptContent
= new ArrayList<Pair<String, InputSource>>();
MessageContext mc = new MessageContext();
List<Element> scripts = new ArrayList<Element>();
for (Node root : Nodes.childrenOf(html)) {
if (root.getNodeType() == 1) {
for (Element script : Nodes.nodeListIterable(
((Element) root).getElementsByTagNameNS(HTML_NS, "script"),
Element.class)) {
scripts.add(script);
}
}
}
for (Element script : scripts) {
Attr src = script.getAttributeNodeNS(HTML_NS, "src");
CharProducer scriptBody;
if (src != null) {
String resourcePath = src.getNodeValue();
InputSource resource;
if (resourcePath.startsWith("/")) {
try {
resource = new InputSource(
RhinoTestBed.class.getResource(resourcePath).toURI());
} catch (URISyntaxException ex) {
throw new SomethingWidgyHappenedError(
"java.net.URL is not a valid java.net.URI", ex);
}
} else {
InputSource baseUri = Nodes.getFilePositionFor(html).source();
resource = new InputSource(baseUri.getUri().resolve(resourcePath));
}
scriptBody = loadResource(resource);
} else {
scriptBody = textContentOf(script);
}
String scriptText;
InputSource isrc = scriptBody.getSourceBreaks(0).source();
// Add blank lines at the front so that Rhino stack traces have correct
// line numbers.
scriptText = prefixWithBlankLines(
scriptBody.toString(0, scriptBody.getLimit()),
Nodes.getFilePositionFor(script).startLineNo() - 1);
scriptContent.add(Pair.pair(scriptText, isrc));
mc.addInputSource(isrc);
script.getParentNode().removeChild(script);
}
for (Pair<String, InputSource> script : scriptContent) {
inputs.add(new Executor.Input(script.a, mc.abbreviate(script.b)));
}
// Set up the DOM. env.js requires that location be set to a URI before it
// creates a DOM. Since it fetches HTML via java.net.URL and passes it off
// to the org.w3c parser, we use a content: URL which is handled by handlers