@@ -75,43 +75,83 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
7575 val scriptsURIs = scripts.map(JSDOMNodeJSEnv .materialize(_))
7676 val scriptsURIsAsJSStrings =
7777 scriptsURIs.map(uri => " \" " + escapeJS(uri.toASCIIString) + " \" " )
78+ val scriptsURIsJSArray = scriptsURIsAsJSStrings.mkString(" [" , " , " , " ]" )
7879 val jsDOMCode = {
7980 s """
81+ |
8082 |(function () {
81- | var jsdom;
82- | try {
83- | jsdom = require("jsdom/lib/old-api.js"); // jsdom >= 10.x
84- | } catch (e) {
85- | jsdom = require("jsdom"); // jsdom <= 9.x
86- | }
83+ | var jsdom = require("jsdom");
8784 |
88- | var virtualConsole = jsdom.createVirtualConsole()
89- | .sendTo(console, { omitJsdomErrors: true });
90- | virtualConsole.on("jsdomError", function (error) {
91- | /* This inelegant if + console.error is the only way I found
92- | * to make sure the stack trace of the original error is
93- | * printed out.
94- | */
95- | if (error.detail && error.detail.stack)
96- | console.error(error.detail.stack);
85+ | if (typeof jsdom.JSDOM === "function") {
86+ | // jsdom >= 10.0.0
87+ | var virtualConsole = new jsdom.VirtualConsole()
88+ | .sendTo(console, { omitJSDOMErrors: true });
89+ | virtualConsole.on("jsdomError", function (error) {
90+ | try {
91+ | // Display as much info about the error as possible
92+ | if (error.detail && error.detail.stack) {
93+ | console.error("" + error.detail);
94+ | console.error(error.detail.stack);
95+ | } else {
96+ | console.error(error);
97+ | }
98+ | } finally {
99+ | // Whatever happens, kill the process so that the run fails
100+ | process.exit(1);
101+ | }
102+ | });
97103 |
98- | // Throw the error anew to make sure the whole execution fails
99- | throw error;
100- | });
104+ | var dom = new jsdom.JSDOM("", {
105+ | virtualConsole: virtualConsole,
106+ | url: "http://localhost/",
101107 |
102- | jsdom.env({
103- | html: "",
104- | url: "http://localhost/",
105- | virtualConsole: virtualConsole,
106- | created: function (error, window) {
107- | if (error == null) {
108- | window["scalajsCom"] = global.scalajsCom;
109- | } else {
110- | throw error;
111- | }
112- | },
113- | scripts: [ ${scriptsURIsAsJSStrings.mkString(" , " )}]
114- | });
108+ | /* Allow unrestricted <script> tags. This is exactly as
109+ | * "dangerous" as the arbitrary execution of script files we
110+ | * do in the non-jsdom Node.js env.
111+ | */
112+ | resources: "usable",
113+ | runScripts: "dangerously"
114+ | });
115+ |
116+ | var window = dom.window;
117+ | window["scalajsCom"] = global.scalajsCom;
118+ |
119+ | var scriptsSrcs = $scriptsURIsJSArray;
120+ | for (var i = 0; i < scriptsSrcs.length; i++) {
121+ | var script = window.document.createElement("script");
122+ | script.src = scriptsSrcs[i];
123+ | window.document.body.appendChild(script);
124+ | }
125+ | } else {
126+ | // jsdom v9.x
127+ | var virtualConsole = jsdom.createVirtualConsole()
128+ | .sendTo(console, { omitJsdomErrors: true });
129+ | virtualConsole.on("jsdomError", function (error) {
130+ | /* This inelegant if + console.error is the only way I found
131+ | * to make sure the stack trace of the original error is
132+ | * printed out.
133+ | */
134+ | if (error.detail && error.detail.stack)
135+ | console.error(error.detail.stack);
136+ |
137+ | // Throw the error anew to make sure the whole execution fails
138+ | throw error;
139+ | });
140+ |
141+ | jsdom.env({
142+ | html: "",
143+ | virtualConsole: virtualConsole,
144+ | url: "http://localhost/",
145+ | created: function (error, window) {
146+ | if (error == null) {
147+ | window["scalajsCom"] = global.scalajsCom;
148+ | } else {
149+ | throw error;
150+ | }
151+ | },
152+ | scripts: $scriptsURIsJSArray
153+ | });
154+ | }
115155 |})();
116156 | """ .stripMargin
117157 }
0 commit comments