Skip to content

Commit edb113c

Browse files
committed
Make html2canvas requireable in nodejs
1 parent 33cd2c5 commit edb113c

File tree

11 files changed

+45
-18
lines changed

11 files changed

+45
-18
lines changed

.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"boss": true,
1111
"eqnull": true,
1212
"browser": true,
13+
"node": true,
1314
"indent": 4,
1415
"globals": {
1516
"jQuery": true

Gruntfile.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = function(grunt) {
99
'<%= pkg.homepage ? " <" + pkg.homepage + ">" : "" %>' + '\n' +
1010
' Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>' +
1111
'\n\n Released under <%= _.pluck(pkg.licenses, "type").join(", ") %> License\n*/\n',
12-
pre: '\n(function(window, document, module, exports, global, define, undefined){\n\n',
13-
post: '\n}).call({}, window, document);'
12+
pre: '\n(function(window, document, exports, global, define, undefined){\n\n',
13+
post: '\n}).call({}, typeof(window) !== "undefined" ? window : undefined, typeof(document) !== "undefined" ? document : undefined);'
1414
};
1515

1616
var browsers = {
@@ -168,6 +168,12 @@ module.exports = function(grunt) {
168168
all: ['src/*.js', 'src/renderers/*.js', '!src/promise.js'],
169169
options: grunt.file.readJSON('./.jshintrc')
170170
},
171+
mochacli: {
172+
options: {
173+
reporter: 'spec'
174+
},
175+
all: ['tests/node/*.js']
176+
},
171177
mocha_phantomjs: {
172178
all: ['tests/mocha/**/*.html']
173179
},
@@ -205,10 +211,11 @@ module.exports = function(grunt) {
205211
grunt.loadNpmTasks('grunt-contrib-jshint');
206212
grunt.loadNpmTasks('grunt-contrib-connect');
207213
grunt.loadNpmTasks('grunt-execute');
214+
grunt.loadNpmTasks('grunt-mocha-cli');
208215

209216
grunt.registerTask('server', ['connect:cors', 'connect:proxy', 'connect:altServer', 'connect:server']);
210217
grunt.registerTask('build', ['execute', 'concat', 'uglify']);
211-
grunt.registerTask('default', ['jshint', 'build', 'connect:altServer', 'mocha_phantomjs']);
218+
grunt.registerTask('default', ['jshint', 'build', 'mochacli', 'connect:altServer', 'mocha_phantomjs']);
212219
grunt.registerTask('travis', ['jshint', 'build', 'connect:altServer', 'connect:ci', 'connect:proxy', 'connect:cors', 'mocha_phantomjs', 'webdriver']);
213220

214221
};

dist/html2canvas.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
html2canvas 0.5.0-alpha <http://html2canvas.hertzen.com>
2+
html2canvas 0.5.0-alpha1 <http://html2canvas.hertzen.com>
33
Copyright (c) 2015 Niklas von Hertzen
44
55
Released under MIT License
66
*/
77

8-
(function(window, document, module, exports, global, define, undefined){
8+
(function(window, document, exports, global, define, undefined){
99

1010
/*!
1111
* @overview es6-promise - a tiny implementation of Promises/A+.
@@ -24,10 +24,14 @@ Object.prototype.toString.call(a)},l=0,w="undefined"!==typeof window?window:{},B
2424
function(){this.b=Array(this.length)};k.prototype.k=function(){for(var a=this.length,b=this.c,c=this.m,d=0;void 0===b.a&&d<a;d++)this.j(c[d],d)};k.prototype.j=function(a,b){var c=this.n;"object"===typeof a&&null!==a?a.constructor===c&&void 0!==a.a?(a.f=null,this.g(a.a,b,a.b)):this.q(c.resolve(a),b):(this.d--,this.b[b]=this.h(a))};k.prototype.g=function(a,b,c){var d=this.c;void 0===d.a&&(this.d--,this.i&&2===a?g(d,c):this.b[b]=this.h(c));0===this.d&&m(d,this.b)};k.prototype.h=function(a){return a};
2525
k.prototype.q=function(a,b){var c=this;u(a,void 0,function(a){c.g(1,b,a)},function(a){c.g(2,b,a)})};var O=0;h.all=function(a,b){return(new k(this,a,!0,b)).c};h.race=function(a,b){function c(a){q(e,a)}function d(a){g(e,a)}var e=new this(p,b);if(!E(a))return (g(e,new TypeError("You must pass an array to race.")), e);for(var f=a.length,h=0;void 0===e.a&&h<f;h++)u(this.resolve(a[h]),void 0,c,d);return e};h.resolve=function(a,b){if(a&&"object"===typeof a&&a.constructor===this)return a;var c=new this(p,b);
2626
q(c,a);return c};h.reject=function(a,b){var c=new this(p,b);g(c,a);return c};h.prototype={constructor:h,then:function(a,b){var c=this.a;if(1===c&&!a||2===c&&!b)return this;var d=new this.constructor(p),e=this.b;if(c){var f=arguments[c-1];r(function(){C(c,d,f,e)})}else u(this,d,a,b);return d},"catch":function(a){return this.then(null,a)}};var z={Promise:h,polyfill:function(){var a;a="undefined"!==typeof global?global:"undefined"!==typeof window&&window.document?window:self;"Promise"in a&&"resolve"in
27-
a.Promise&&"reject"in a.Promise&&"all"in a.Promise&&"race"in a.Promise&&function(){var b;new a.Promise(function(a){b=a});return s(b)}()||(a.Promise=h)}};"function"===typeof define&&define.amd?define(function(){return z}):"undefined"!==typeof module&&module.exports?module.exports=z:"undefined"!==typeof this&&(this.ES6Promise=z);}).call(window);window.ES6Promise.polyfill();
27+
a.Promise&&"reject"in a.Promise&&"all"in a.Promise&&"race"in a.Promise&&function(){var b;new a.Promise(function(a){b=a});return s(b)}()||(a.Promise=h)}};"function"===typeof define&&define.amd?define(function(){return z}):"undefined"!==typeof module&&module.exports?module.exports=z:"undefined"!==typeof this&&(this.ES6Promise=z);}).call(window);
28+
if (window) {
29+
window.ES6Promise.polyfill();
30+
}
31+
2832

29-
if (typeof(Object.create) !== "function" || typeof(document.createElement("canvas").getContext) !== "function") {
30-
window.html2canvas = function() {
33+
if (typeof(document) === "undefined" || typeof(Object.create) !== "function" || typeof(document.createElement("canvas").getContext) !== "function") {
34+
(window || module.exports).html2canvas = function() {
3135
return Promise.reject("No canvas support");
3236
};
3337
return;
@@ -3368,4 +3372,4 @@ function hasEntries(array) {
33683372
return array.length > 0;
33693373
}
33703374

3371-
}).call({}, window, document);
3375+
}).call({}, typeof(window) !== "undefined" ? window : undefined, typeof(document) !== "undefined" ? document : undefined);

dist/html2canvas.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/html2canvas.svg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
html2canvas 0.5.0-alpha <http://html2canvas.hertzen.com>
2+
html2canvas 0.5.0-alpha1 <http://html2canvas.hertzen.com>
33
Copyright (c) 2015 Niklas von Hertzen
44

55
Released under MIT License

dist/html2canvas.svg.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/html2canvas').html2canvas;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"title": "html2canvas",
33
"name": "html2canvas",
44
"description": "Screenshots with JavaScript",
5-
"version": "0.5.0-alpha",
5+
"main": "main.js",
6+
"version": "0.5.0-alpha1",
67
"author": {
78
"name": "Niklas von Hertzen",
89
"email": "niklasvh@gmail.com",
@@ -29,6 +30,7 @@
2930
"grunt-contrib-uglify": "^0.6.0",
3031
"grunt-contrib-watch": "^0.6.1",
3132
"grunt-execute": "^0.2.2",
33+
"grunt-mocha-cli": "^1.12.0",
3234
"grunt-mocha-phantomjs": "^0.6.0",
3335
"html2canvas-proxy": "0.0.5",
3436
"humanize-duration": "^2.0.1",

src/fallback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
if (typeof(Object.create) !== "function" || typeof(document.createElement("canvas").getContext) !== "function") {
2-
window.html2canvas = function() {
1+
if (typeof(document) === "undefined" || typeof(Object.create) !== "function" || typeof(document.createElement("canvas").getContext) !== "function") {
2+
(window || module.exports).html2canvas = function() {
33
return Promise.reject("No canvas support");
44
};
55
return;

src/promise.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/node/package.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var assert = require('assert');
2+
var html2canvas = require('../../');
3+
4+
describe("Package", function() {
5+
it("should have html2canvas defined", function() {
6+
assert.equal(typeof(html2canvas), "function");
7+
});
8+
});

0 commit comments

Comments
 (0)