Skip to content

Commit 4652200

Browse files
authored
Merge pull request #33 from sjrd/fix-jsdom-v12-and-later
Fix #32: Use the new API of jsdom v10.
2 parents 39395f3 + 2a4efbc commit 4652200

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed

.travis.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ jdk:
99
env:
1010
- JSDOM_VERSION=9.12.0
1111
- JSDOM_VERSION=10.0.0
12+
- JSDOM_VERSION=16.0.0
1213
install:
1314
# We need a recent version of Node.js for jsdom
14-
- nvm install 6
15-
- nvm use 6
15+
- nvm install 12
16+
- nvm use 12
1617
- node --version
1718
# Of course we need jsdom
1819
- npm install jsdom@$JSDOM_VERSION
1920
script:
2021
- sbt ++$TRAVIS_SCALA_VERSION scalajs-env-jsdom-nodejs/test scalajs-env-jsdom-nodejs/doc
21-
- |
22-
if [[ "${TRAVIS_SCALA_VERSION}" != "2.10.7" ]]; then
23-
sbt ++$TRAVIS_SCALA_VERSION test-project/run test-project/test
24-
fi
22+
- sbt ++$TRAVIS_SCALA_VERSION test-project/run test-project/test
2523
cache:
2624
directories:
2725
- $HOME/.ivy2/cache

jsdom-nodejs-env/src/main/scala/org/scalajs/jsenv/jsdomnodejs/JSDOMNodeJSEnv.scala

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)