Skip to content

Commit e6e18e3

Browse files
mightyiamTooTallNate
authored andcommitted
Add Node.js support
Fixes #2. Squashed commit of the following: commit 5d261ee Author: Shahar Or <[email protected]> Date: Mon Feb 9 20:50:22 2015 +0200 Makefile: fixed commit 7c35b64 Author: Shahar Or <[email protected]> Date: Mon Feb 9 20:29:38 2015 +0200 rm extra blank line commit 3dbcd75 Author: Shahar Or <[email protected]> Date: Mon Feb 9 20:25:56 2015 +0200 Do not browserify jsdom by package.json, not .zuul.yml commit d1db1b3 Author: Shahar Or <[email protected]> Date: Mon Feb 9 20:22:16 2015 +0200 spec: don't use node-detect. check typeof window commit a3e0a9b Author: Shahar Or <[email protected]> Date: Mon Feb 9 20:16:55 2015 +0200 test: clean up spec-specific child elements commit e7cee9e Author: Shahar Or <[email protected]> Date: Mon Feb 9 20:14:05 2015 +0200 Use package.json browser field for not expecting window in non-browser commit f144af4 Author: Shahar Or <[email protected]> Date: Wed Jan 28 23:12:17 2015 +0200 Makefile: fix `test` command commit a9e5b66 Author: Shahar Or <[email protected]> Date: Wed Jan 28 22:49:31 2015 +0200 Fix for use in node.js
1 parent 7969916 commit e6e18e3

File tree

6 files changed

+74
-45
lines changed

6 files changed

+74
-45
lines changed

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ BIN := $(THIS_DIR)/node_modules/.bin
88

99
# applications
1010
NODE ?= node
11+
MOCHA ?= $(NODE) $(BIN)/mocha
1112
ZUUL ?= $(NODE) $(BIN)/zuul
1213

1314
test:
14-
@if [ "x$(BROWSER_PLATFORM)" = "x" ]; then \
15-
$(ZUUL) \
16-
--ui mocha-bdd \
17-
--browser-name $(BROWSER_NAME) \
18-
--browser-version $(BROWSER_VERSION) \
19-
test/*.js; \
15+
@if [ "x$(BROWSER_NAME)" = "x" ]; then \
16+
$(MAKE) test-node; \
2017
else \
21-
$(ZUUL) \
18+
$(MAKE) test-zuul; \
19+
fi
20+
21+
test-node:
22+
@$(MOCHA) test/*.js
23+
24+
test-zuul:
25+
@$(ZUUL) \
2226
--ui mocha-bdd \
2327
--browser-name $(BROWSER_NAME) \
2428
--browser-version $(BROWSER_VERSION) \

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ var getDocument = require('get-document');
1111

1212
module.exports = getWindow;
1313

14-
// old-IE fallback logic: http://stackoverflow.com/a/10260692
15-
var needsIEFallback = !!document.attachEvent && window !== document.parentWindow;
14+
var needsIEFallback = require('./needs-ie-fallback');
1615

1716
/**
1817
* Returns `true` if `w` is a Window object, or `false` otherwise.

needs-ie-fallback.br.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// this is a browser-only module. There is a non-browser equivalent in the same
2+
// directory. This is done using a `package.json` browser field.
3+
// old-IE fallback logic: http://stackoverflow.com/a/10260692
4+
module.exports = !!document.attachEvent && window !== document.parentWindow;

needs-ie-fallback.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// there is a browser-specific, equivalent, module in the same directory
2+
module.exports = false;

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"browser"
1515
],
1616
"main": "index.js",
17+
"browser": {
18+
"./needs-ie-fallback.js": "needs-ie-fallback.br.js",
19+
"jsdom": false
20+
},
1721
"dependencies": {
1822
"get-document": "1"
1923
},
@@ -27,6 +31,8 @@
2731
"url": "https://github.com/webmodules/get-window.git"
2832
},
2933
"devDependencies": {
34+
"jsdom": "^3.1.0",
35+
"mocha": "^2.1.0",
3036
"zuul": "~1.16.5"
3137
}
3238
}

test/test.js

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,109 @@ var assert = require('assert');
33
var getWindow = require('../');
44
var getDocument = require('get-document');
55

6+
var inBrowser = typeof window !== 'undefined';
7+
8+
// Zuul is configured to pass on to Browserify a configuration to exclude the
9+
// following module, 'jsdom' because it is only for testing in node. Browsers
10+
// have real dom
11+
var jsdom = !inBrowser ? require('jsdom') : null;
12+
13+
var doc = inBrowser ? document : jsdom.jsdom();
14+
var win = inBrowser ? window : doc.parentWindow;
15+
616
describe('get-window', function () {
717

818
it('should work with the global Window object', function () {
9-
var win = getWindow(window);
10-
assert(win === window);
19+
var result = getWindow(win);
20+
assert(result === win);
1121
});
1222

1323
it('should work with the global Document object', function () {
14-
var win = getWindow(document);
15-
assert(win === window);
24+
var result = getWindow(doc);
25+
assert(result === win);
1626
});
1727

1828
it('should work with the `document.body` object', function () {
19-
var win = getWindow(document.body);
20-
assert(win === window);
29+
var result = getWindow(doc.body);
30+
assert(result === win);
2131
});
2232

2333
it('should work with the a DOM element inside `document.body`', function () {
24-
var win = getWindow(document.body.firstChild);
25-
assert(win === window);
34+
var p = doc.createElement('p')
35+
doc.body.appendChild(p);
36+
var result = getWindow(doc.body.firstChild);
37+
assert(result === win);
38+
doc.body.removeChild(p);
2639
});
2740

2841
it('should work with a new DOM element', function () {
29-
var win = getWindow(document.createElement('div'));
30-
assert(win === window);
42+
var result = getWindow(doc.createElement('div'));
43+
assert(result === win);
3144
});
3245

3346
it('should work with a new TextNode', function () {
34-
var win = getWindow(document.createTextNode(''));
35-
assert(win === window);
47+
var result = getWindow(doc.createTextNode(''));
48+
assert(result === win);
3649
});
3750

3851
// skip on IE <= 8
39-
if ('function' === typeof document.createRange) {
52+
if ('function' === typeof doc.createRange) {
4053
it('should work with a DOM Range instance', function () {
41-
var win = getWindow(document.createRange());
42-
assert(win === window);
54+
var result = getWindow(doc.createRange());
55+
assert(result === win);
4356
});
4457
}
4558

4659
// skip on IE <= 8
47-
if ('function' === typeof window.getSelection) {
60+
if ('function' === typeof win.getSelection) {
4861
it('should work with a DOM Selection instance', function () {
4962
// NOTE: a Selection needs to have some kind of selection on it
5063
// (i.e. not `type: "None"`) in order for a Document to be found
51-
var range = document.createRange();
52-
var t = document.createTextNode('t');
53-
document.body.appendChild(t);
64+
var range = doc.createRange();
65+
var t = doc.createTextNode('t');
66+
doc.body.appendChild(t);
5467
range.setStart(t, 0);
5568
range.setEnd(t, t.nodeValue.length);
5669

57-
var sel = window.getSelection();
70+
var sel = win.getSelection();
5871
sel.removeAllRanges();
5972
sel.addRange(range);
6073
assert.equal(1, sel.rangeCount);
6174

62-
var win = getWindow(sel);
63-
assert(win === window);
75+
var result = getWindow(sel);
76+
assert(result === win);
6477

6578
// clean up
6679
sel.removeAllRanges();
80+
doc.body.removeChild(t)
6781
});
6882
}
6983

7084
it('should work with the child node of an IFRAME element', function () {
71-
var iframe = document.createElement('iframe');
72-
document.body.appendChild(iframe);
85+
var iframe = doc.createElement('iframe');
86+
doc.body.appendChild(iframe);
7387

7488
// `contentWindow` should be used for best browser compatibility
75-
var doc = getDocument(iframe.contentWindow);
76-
assert.equal(9, doc.nodeType);
89+
var iframeDoc = getDocument(iframe.contentWindow);
90+
assert.equal(9, iframeDoc.nodeType);
7791

78-
doc.open();
79-
doc.write('<body><b>hello world</b></body>');
80-
doc.close();
92+
iframeDoc.open();
93+
iframeDoc.write('<body><b>hello world</b></body>');
94+
iframeDoc.close();
8195

82-
var win = getWindow(doc);
83-
assert(win);
84-
assert(win !== window);
96+
var result = getWindow(iframeDoc);
97+
assert(result );
98+
assert(result !== win);
8599

86100
// test the <body>
87-
var body = doc.body;
88-
assert(win === getWindow(body));
101+
var body = iframeDoc.body;
102+
assert(result === getWindow(body));
89103

90104
// test the <b> node
91-
assert(win === getWindow(body.firstChild));
105+
assert(result === getWindow(body.firstChild));
92106

93107
// clean up
94-
document.body.removeChild(iframe);
108+
doc.body.removeChild(iframe);
95109
});
96110

97111
});

0 commit comments

Comments
 (0)