Skip to content

Commit 0a412f6

Browse files
committed
Fix d3#2006 - prevent leak of d3 global in node.
1 parent f8514d3 commit 0a412f6

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
var document = require("jsdom").jsdom("<html><head></head><body></body></html>"),
2-
window = document.parentWindow,
1+
var document = require("jsdom").jsdom(),
32
globals = {};
43

5-
// stash globals
4+
// Stash old globals.
5+
if ("d3" in global) globals.d3 = global.d3;
66
if ("window" in global) globals.window = global.window;
7-
global.window = window;
87
if ("document" in global) globals.document = global.document;
8+
9+
// Set temporary globals to pretend we’re in a browser.
10+
global.window = document.parentWindow;
911
global.document = document;
1012

1113
module.exports = require("./d3");
1214

13-
// restore globals
14-
if ("window" in globals) global.window = globals.window;
15-
else delete global.window;
16-
if ("document" in globals) global.document = globals.document;
17-
else delete global.document;
15+
// Restore old globals.
16+
if ("d3" in globals) global.d3 = globals.d3; else delete global.d3;
17+
if ("window" in globals) global.window = globals.window; else delete global.window;
18+
if ("document" in globals) global.document = globals.document; else delete global.document;

test/index-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var vows = require("vows"),
2+
assert = require("./assert");
3+
4+
var suite = vows.describe("d3");
5+
6+
require("../");
7+
8+
suite.addBatch({
9+
"d3": {
10+
"is not a global when require’d": function() {
11+
assert.equal("d3" in global, false);
12+
}
13+
}
14+
});
15+
16+
suite.export(module);

0 commit comments

Comments
 (0)