Build: Don't assume the browser environment; smoke test on Node w/ jsdom

Fixes gh-1950
Closes gh-1949
This commit is contained in:
Michał Gołębiowski 2014-09-11 15:18:34 -05:00 committed by Michał Gołębiowski
parent ab20d9d24f
commit 76df9e4e38
23 changed files with 88 additions and 40 deletions

View File

@ -157,9 +157,24 @@ module.exports = function( grunt ) {
grunt.registerTask( "lint", [ "jshint", "jscs" ] ); grunt.registerTask( "lint", [ "jshint", "jscs" ] );
grunt.registerTask( "node_smoke_test", function() {
var done = this.async();
require( "jsdom" ).env( "", function( errors, window ) {
if ( errors ) {
console.error( errors );
done( false );
}
require( "./" )( window );
done();
});
});
// Short list as a high frequency watch task // Short list as a high frequency watch task
grunt.registerTask( "dev", [ "build:*:*", "lint" ] ); grunt.registerTask( "dev", [ "build:*:*", "lint" ] );
// Default grunt grunt.registerTask( "test_fast", [ "node_smoke_test" ] );
grunt.registerTask( "test", [ "default", "test_fast" ] );
grunt.registerTask( "default", [ "jsonlint", "dev", "uglify", "dist:*", "compare_size" ] ); grunt.registerTask( "default", [ "jsonlint", "dev", "uglify", "dist:*", "compare_size" ] );
}; };

View File

@ -43,6 +43,7 @@
"grunt-jsonlint": "1.0.4", "grunt-jsonlint": "1.0.4",
"grunt-npmcopy": "0.1.0", "grunt-npmcopy": "0.1.0",
"gzip-js": "0.3.2", "gzip-js": "0.3.2",
"jsdom": "1.5.0",
"load-grunt-tasks": "1.0.0", "load-grunt-tasks": "1.0.0",
"npm": "2.1.12", "npm": "2.1.12",
"qunitjs": "1.16.0", "qunitjs": "1.16.0",
@ -54,6 +55,6 @@
"scripts": { "scripts": {
"build": "npm install && grunt", "build": "npm install && grunt",
"start": "grunt watch", "start": "grunt watch",
"test": "grunt" "test": "grunt test"
} }
} }

View File

@ -15,9 +15,13 @@
"sub": true, "sub": true,
"browser": true,
"globals": { "globals": {
"window": true,
"setTimeout": true,
"clearTimeout": true,
"setInterval": true,
"clearInterval": true,
"jQuery": true, "jQuery": true,
"define": true, "define": true,
"module": true, "module": true,

View File

@ -1,13 +1,15 @@
define([ define([
"./core", "./core",
"./var/document",
"./var/rnotwhite", "./var/rnotwhite",
"./ajax/var/location",
"./ajax/var/nonce", "./ajax/var/nonce",
"./ajax/var/rquery", "./ajax/var/rquery",
"./core/init", "./core/init",
"./ajax/parseJSON", "./ajax/parseJSON",
"./ajax/parseXML", "./ajax/parseXML",
"./deferred" "./deferred"
], function( jQuery, rnotwhite, nonce, rquery ) { ], function( jQuery, document, rnotwhite, location, nonce, rquery ) {
var var
rhash = /#.*$/, rhash = /#.*$/,

View File

@ -11,7 +11,7 @@ jQuery.parseXML = function( data ) {
// Support: IE9 // Support: IE9
try { try {
xml = ( new DOMParser() ).parseFromString( data, "text/xml" ); xml = ( new window.DOMParser() ).parseFromString( data, "text/xml" );
} catch ( e ) { } catch ( e ) {
xml = undefined; xml = undefined;
} }

View File

@ -1,7 +1,8 @@
define([ define([
"../core", "../core",
"../var/document",
"../ajax" "../ajax"
], function( jQuery ) { ], function( jQuery, document ) {
// Install script dataType // Install script dataType
jQuery.ajaxSetup({ jQuery.ajaxSetup({

3
src/ajax/var/location.js Normal file
View File

@ -0,0 +1,3 @@
define(function() {
return window.location;
});

View File

@ -6,7 +6,7 @@ define([
jQuery.ajaxSettings.xhr = function() { jQuery.ajaxSettings.xhr = function() {
try { try {
return new XMLHttpRequest(); return new window.XMLHttpRequest();
} catch ( e ) {} } catch ( e ) {}
}; };

View File

@ -1,6 +1,7 @@
define([ define([
"../var/document",
"../var/support" "../var/support"
], function( support ) { ], function( document, support ) {
(function() { (function() {
var input = document.createElement( "input" ), var input = document.createElement( "input" ),

View File

@ -1,5 +1,6 @@
define([ define([
"./var/arr", "./var/arr",
"./var/document",
"./var/slice", "./var/slice",
"./var/concat", "./var/concat",
"./var/push", "./var/push",
@ -8,12 +9,9 @@ define([
"./var/toString", "./var/toString",
"./var/hasOwn", "./var/hasOwn",
"./var/support" "./var/support"
], function( arr, slice, concat, push, indexOf, class2type, toString, hasOwn, support ) { ], function( arr, document, slice, concat, push, indexOf, class2type, toString, hasOwn, support ) {
var var
// Use the correct document accordingly with window argument (sandbox)
document = window.document,
version = "@VERSION", version = "@VERSION",
// Define a local copy of jQuery // Define a local copy of jQuery

View File

@ -1,9 +1,10 @@
// Initialize a jQuery object // Initialize a jQuery object
define([ define([
"../core", "../core",
"../var/document",
"./var/rsingleTag", "./var/rsingleTag",
"../traversing/findFilter" "../traversing/findFilter"
], function( jQuery, rsingleTag ) { ], function( jQuery, document, rsingleTag ) {
// A central reference to the root jQuery(document) // A central reference to the root jQuery(document)
var rootjQuery, var rootjQuery,

View File

@ -1,5 +1,6 @@
define([ define([
"../core", "../core",
"../var/document",
"./var/rsingleTag", "./var/rsingleTag",
// This is the only module that needs core/support // This is the only module that needs core/support
@ -7,7 +8,7 @@ define([
// buildFragment // buildFragment
"../manipulation" "../manipulation"
], function( jQuery, rsingleTag, support ) { ], function( jQuery, document, rsingleTag, support ) {
// data: string of html // data: string of html
// context (optional): If specified, the fragment will be created in this context, // context (optional): If specified, the fragment will be created in this context,

View File

@ -1,8 +1,9 @@
define([ define([
"../core", "../core",
"../var/document",
"../core/init", "../core/init",
"../deferred" "../deferred"
], function( jQuery ) { ], function( jQuery, document ) {
// The deferred used on DOM ready // The deferred used on DOM ready
var readyList; var readyList;

View File

@ -1,9 +1,15 @@
define([ define([
"../var/document",
"../var/support" "../var/support"
], function( support ) { ], function( document, support ) {
support.createHTMLDocument = (function() { support.createHTMLDocument = (function() {
var doc = document.implementation.createHTMLDocument( "" ); var doc = document.implementation.createHTMLDocument( "" );
// Support: Node with jsdom<=1.5.0+
// jsdom's document created via the above method doesn't contain the body
if ( !doc.body ) {
return false;
}
doc.body.innerHTML = "<form></form><form></form>"; doc.body.innerHTML = "<form></form><form></form>";
return doc.body.childNodes.length === 2; return doc.body.childNodes.length === 2;
})(); })();

View File

@ -1,7 +1,8 @@
define([ define([
"../core", "../core",
"../var/document",
"../manipulation" // appendTo "../manipulation" // appendTo
], function( jQuery ) { ], function( jQuery, document ) {
var iframe, var iframe,
elemdisplay = { elemdisplay = {

View File

@ -1,11 +1,12 @@
define([ define([
"../core", "../core",
"../var/document",
"../var/documentElement",
"../var/support" "../var/support"
], function( jQuery, support ) { ], function( jQuery, document, documentElement, support ) {
(function() { (function() {
var pixelPositionVal, boxSizingReliableVal, var pixelPositionVal, boxSizingReliableVal,
docElem = document.documentElement,
container = document.createElement( "div" ), container = document.createElement( "div" ),
div = document.createElement( "div" ); div = document.createElement( "div" );
@ -33,13 +34,13 @@ define([
"display:block;margin-top:1%;top:1%;" + "display:block;margin-top:1%;top:1%;" +
"border:1px;padding:1px;width:4px;position:absolute"; "border:1px;padding:1px;width:4px;position:absolute";
div.innerHTML = ""; div.innerHTML = "";
docElem.appendChild( container ); documentElement.appendChild( container );
var divStyle = window.getComputedStyle( div, null ); var divStyle = window.getComputedStyle( div, null );
pixelPositionVal = divStyle.top !== "1%"; pixelPositionVal = divStyle.top !== "1%";
boxSizingReliableVal = divStyle.width === "4px"; boxSizingReliableVal = divStyle.width === "4px";
docElem.removeChild( container ); documentElement.removeChild( container );
} }
// Support: node.js jsdom // Support: node.js jsdom
@ -78,11 +79,11 @@ define([
"display:block;margin:0;border:0;padding:0"; "display:block;margin:0;border:0;padding:0";
marginDiv.style.marginRight = marginDiv.style.width = "0"; marginDiv.style.marginRight = marginDiv.style.width = "0";
div.style.width = "1px"; div.style.width = "1px";
docElem.appendChild( container ); documentElement.appendChild( container );
ret = !parseFloat( window.getComputedStyle( marginDiv, null ).marginRight ); ret = !parseFloat( window.getComputedStyle( marginDiv, null ).marginRight );
docElem.removeChild( container ); documentElement.removeChild( container );
div.removeChild( marginDiv ); div.removeChild( marginDiv );
return ret; return ret;

3
src/effects.js vendored
View File

@ -1,5 +1,6 @@
define([ define([
"./core", "./core",
"./var/document",
"./var/pnum", "./var/pnum",
"./css/var/cssExpand", "./css/var/cssExpand",
"./css/var/isHidden", "./css/var/isHidden",
@ -12,7 +13,7 @@ define([
"./css", "./css",
"./deferred", "./deferred",
"./traversing" "./traversing"
], function( jQuery, pnum, cssExpand, isHidden, defaultDisplay, dataPriv ) { ], function( jQuery, document, pnum, cssExpand, isHidden, defaultDisplay, dataPriv ) {
var var
fxNow, timerId, fxNow, timerId,

View File

@ -1,5 +1,6 @@
define([ define([
"./core", "./core",
"./var/document",
"./var/rnotwhite", "./var/rnotwhite",
"./var/hasOwn", "./var/hasOwn",
"./var/slice", "./var/slice",
@ -9,7 +10,7 @@ define([
"./core/init", "./core/init",
"./data/accepts", "./data/accepts",
"./selector" "./selector"
], function( jQuery, rnotwhite, hasOwn, slice, support, dataPriv ) { ], function( jQuery, document, rnotwhite, hasOwn, slice, support, dataPriv ) {
var var
rkeyEvent = /^key/, rkeyEvent = /^key/,

View File

@ -1,6 +1,7 @@
define([ define([
"../var/document",
"../var/support" "../var/support"
], function( support ) { ], function( document, support ) {
(function() { (function() {
var fragment = document.createDocumentFragment(), var fragment = document.createDocumentFragment(),

View File

@ -1,6 +1,8 @@
define([ define([
"./core", "./core",
"./core/access", "./core/access",
"./var/document",
"./var/documentElement",
"./css/var/rnumnonpx", "./css/var/rnumnonpx",
"./css/curCSS", "./css/curCSS",
"./css/addGetHookIf", "./css/addGetHookIf",
@ -9,9 +11,7 @@ define([
"./core/init", "./core/init",
"./css", "./css",
"./selector" // contains "./selector" // contains
], function( jQuery, access, rnumnonpx, curCSS, addGetHookIf, support ) { ], function( jQuery, access, document, documentElement, rnumnonpx, curCSS, addGetHookIf, support ) {
var docElem = window.document.documentElement;
/** /**
* Gets a window from an element * Gets a window from an element
@ -145,14 +145,14 @@ jQuery.fn.extend({
offsetParent: function() { offsetParent: function() {
return this.map(function() { return this.map(function() {
var offsetParent = this.offsetParent || docElem; var offsetParent = this.offsetParent || documentElement;
while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) &&
jQuery.css( offsetParent, "position" ) === "static" ) ) { jQuery.css( offsetParent, "position" ) === "static" ) ) {
offsetParent = offsetParent.offsetParent; offsetParent = offsetParent.offsetParent;
} }
return offsetParent || docElem; return offsetParent || documentElement;
}); });
} }
}); });

View File

@ -1,6 +1,8 @@
define([ define([
"./core" "./core",
], function( jQuery ) { "./var/document",
"./var/documentElement"
], function( jQuery, document, documentElement ) {
/* /*
* Optional (non-Sizzle) selector module for custom builds. * Optional (non-Sizzle) selector module for custom builds.
@ -28,12 +30,11 @@ define([
*/ */
var hasDuplicate, var hasDuplicate,
docElem = window.document.documentElement, matches = documentElement.matches ||
matches = docElem.matches || documentElement.webkitMatchesSelector ||
docElem.webkitMatchesSelector || documentElement.mozMatchesSelector ||
docElem.mozMatchesSelector || documentElement.oMatchesSelector ||
docElem.oMatchesSelector || documentElement.msMatchesSelector,
docElem.msMatchesSelector,
sortOrder = function( a, b ) { sortOrder = function( a, b ) {
// Flag for duplicate removal // Flag for duplicate removal
if ( a === b ) { if ( a === b ) {

3
src/var/document.js Normal file
View File

@ -0,0 +1,3 @@
define(function() {
return window.document;
});

View File

@ -0,0 +1,5 @@
define([
"./document"
], function( document ) {
return document.documentElement;
});