mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Node.js: Add code to handle separate shim modules.
Planend are the modules paper-jsdom and paper-jsdom-canvas, as shim modules that require and handle the dependencies as peer dependencies. Relates to #1252
This commit is contained in:
parent
48e9ef62a6
commit
29de03dc30
4 changed files with 72 additions and 41 deletions
|
@ -35,8 +35,9 @@ paper = new (PaperScope.inject(Base.exports, {
|
||||||
// If we're on node, require some additional functionality now before finishing:
|
// If we're on node, require some additional functionality now before finishing:
|
||||||
// - PaperScript support in require() with sourceMaps
|
// - PaperScript support in require() with sourceMaps
|
||||||
// - exportFrames / exportImage on CanvasView
|
// - exportFrames / exportImage on CanvasView
|
||||||
if (paper.agent.node)
|
if (paper.agent.node) {
|
||||||
require('./node/extend.js')(paper);
|
require('./node/extend.js')(paper);
|
||||||
|
}
|
||||||
|
|
||||||
// https://github.com/umdjs/umd
|
// https://github.com/umdjs/umd
|
||||||
if (typeof define === 'function' && define.amd) {
|
if (typeof define === 'function' && define.amd) {
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
// - Various Node Canvas methods, routed through from HTMLCanvasElement:
|
// - Various Node Canvas methods, routed through from HTMLCanvasElement:
|
||||||
// toBuffer, pngStream, createPNGStream, jpgStream, createJPGStream
|
// toBuffer, pngStream, createPNGStream, jpgStream, createJPGStream
|
||||||
|
|
||||||
module.exports = function(self) {
|
module.exports = function(self, requireName) {
|
||||||
var Canvas;
|
var Canvas;
|
||||||
try {
|
try {
|
||||||
Canvas = require('canvas');
|
Canvas = require('canvas');
|
||||||
|
@ -26,13 +26,16 @@ module.exports = function(self) {
|
||||||
// - On Node.js, it basically means the canvas is missing or not working
|
// - On Node.js, it basically means the canvas is missing or not working
|
||||||
// which can be treated the same way.
|
// which can be treated the same way.
|
||||||
delete self.window;
|
delete self.window;
|
||||||
console.info(
|
// Check the required module's name to see if it contains canvas, and
|
||||||
'Canvas module not found, running in a headless context.');
|
// only complain about its lack if the module requires it.
|
||||||
|
if (/\bcanvas\b/.test(requireName)) {
|
||||||
|
throw new Error('Unable to load canvas module.');
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var idlUtils = require('jsdom/lib/jsdom/living/generated/utils'),
|
var HTMLCanvasElement = self.HTMLCanvasElement,
|
||||||
HTMLCanvasElement = self.HTMLCanvasElement;
|
idlUtils = require('jsdom/lib/jsdom/living/generated/utils');
|
||||||
|
|
||||||
// Add fake HTMLCanvasElement#type property:
|
// Add fake HTMLCanvasElement#type property:
|
||||||
Object.defineProperty(HTMLCanvasElement.prototype, 'type', {
|
Object.defineProperty(HTMLCanvasElement.prototype, 'type', {
|
||||||
|
|
|
@ -13,11 +13,28 @@
|
||||||
// Node.js emulation layer of browser environment, based on jsdom with node-
|
// Node.js emulation layer of browser environment, based on jsdom with node-
|
||||||
// canvas integration.
|
// canvas integration.
|
||||||
|
|
||||||
var self;
|
var path = require('path');
|
||||||
|
// Determine the name by which name the module was required (either 'paper',
|
||||||
|
// 'paper-jsdom' or 'paper-jsdom-canvas'), and use this to determine if error
|
||||||
|
// exceptions should be thrown or if loading should fail silently.
|
||||||
|
var parent = module.parent.parent,
|
||||||
|
requireName = parent && path.basename(path.dirname(parent.filename));
|
||||||
|
requireName = /^paper/.test(requireName) ? requireName : 'paper';
|
||||||
|
|
||||||
|
var jsdom,
|
||||||
|
self;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var jsdom = require('jsdom');
|
jsdom = require('jsdom');
|
||||||
|
} catch(e) {
|
||||||
|
// Check the required module's name to see if it contains jsdom, and only
|
||||||
|
// complain about its lack if the module requires it.
|
||||||
|
if (/\bjsdom\b/.test(requireName)) {
|
||||||
|
throw new Error('Unable to load jsdom module.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsdom) {
|
||||||
// Create our document and window objects through jsdom.
|
// Create our document and window objects through jsdom.
|
||||||
/* global document:true, window:true */
|
/* global document:true, window:true */
|
||||||
var document = jsdom.jsdom('<html><body></body></html>', {
|
var document = jsdom.jsdom('<html><body></body></html>', {
|
||||||
|
@ -29,39 +46,9 @@ try {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
self = document.defaultView;
|
self = document.defaultView;
|
||||||
|
require('./canvas.js')(self, requireName);
|
||||||
require('./canvas.js')(self);
|
require('./xml.js')(self);
|
||||||
|
} else {
|
||||||
// Define XMLSerializer shim, to emulate browser behavior.
|
|
||||||
// Effort to bring XMLSerializer to jsdom:
|
|
||||||
// https://github.com/tmpvar/jsdom/issues/1368
|
|
||||||
/*jshint -W082 */
|
|
||||||
function XMLSerializer() {
|
|
||||||
}
|
|
||||||
|
|
||||||
XMLSerializer.prototype.serializeToString = function(node) {
|
|
||||||
if (!node)
|
|
||||||
return '';
|
|
||||||
// Fix a jsdom issue where all SVG tagNames are lowercased:
|
|
||||||
// https://github.com/tmpvar/jsdom/issues/620
|
|
||||||
var text = node.outerHTML,
|
|
||||||
tagNames = ['linearGradient', 'radialGradient', 'clipPath',
|
|
||||||
'textPath'];
|
|
||||||
for (var i = 0, l = tagNames.length; i < l; i++) {
|
|
||||||
var tagName = tagNames[i];
|
|
||||||
text = text.replace(
|
|
||||||
new RegExp('(<|</)' + tagName.toLowerCase() + '\\b', 'g'),
|
|
||||||
function(match, start) {
|
|
||||||
return start + tagName;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return text;
|
|
||||||
};
|
|
||||||
|
|
||||||
self.XMLSerializer = XMLSerializer;
|
|
||||||
} catch(e) {
|
|
||||||
console.info(
|
|
||||||
'JSDom module not found, running in a headless context without DOM.');
|
|
||||||
self = {
|
self = {
|
||||||
navigator: {
|
navigator: {
|
||||||
userAgent: 'Node.js (' + process.platform + '; U; rv:' +
|
userAgent: 'Node.js (' + process.platform + '; U; rv:' +
|
||||||
|
|
40
src/node/xml.js
Normal file
40
src/node/xml.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
||||||
|
* http://paperjs.org/
|
||||||
|
*
|
||||||
|
* Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
|
||||||
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
||||||
|
*
|
||||||
|
* Distributed under the MIT license. See LICENSE file for details.
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = function(self) {
|
||||||
|
// Define XMLSerializer shim, to emulate browser behavior.
|
||||||
|
// Effort to bring XMLSerializer to jsdom:
|
||||||
|
// https://github.com/tmpvar/jsdom/issues/1368
|
||||||
|
self.XMLSerializer = function XMLSerializer() {
|
||||||
|
};
|
||||||
|
|
||||||
|
self.XMLSerializer.prototype = {
|
||||||
|
serializeToString: function(node) {
|
||||||
|
if (!node)
|
||||||
|
return '';
|
||||||
|
// Fix a jsdom issue where all SVG tagNames are lowercased:
|
||||||
|
// https://github.com/tmpvar/jsdom/issues/620
|
||||||
|
var text = node.outerHTML,
|
||||||
|
tagNames = ['linearGradient', 'radialGradient', 'clipPath',
|
||||||
|
'textPath'];
|
||||||
|
for (var i = 0, l = tagNames.length; i < l; i++) {
|
||||||
|
var tagName = tagNames[i];
|
||||||
|
text = text.replace(
|
||||||
|
new RegExp('(<|</)' + tagName.toLowerCase() + '\\b', 'g'),
|
||||||
|
function(match, start) {
|
||||||
|
return start + tagName;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in a new issue