mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2024-12-29 09:22:22 -05:00
Improve handling of local paths and CORS on Node.js
This commit is contained in:
parent
093227be7e
commit
5a9f3dfb27
5 changed files with 13 additions and 13 deletions
|
@ -292,12 +292,6 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
|
|||
delete PaperScope._scopes[this._id];
|
||||
},
|
||||
|
||||
resolvePath: function(url) {
|
||||
// On Node.js, resolve relative URLs to local files:
|
||||
return url && this.agent.node && !/^[a-z]+:/i.test(url)
|
||||
? 'file://' + require('path').resolve(url) : url;
|
||||
},
|
||||
|
||||
statics: new function() {
|
||||
// Produces helpers to e.g. check for both 'canvas' and
|
||||
// 'data-paper-canvas' attributes:
|
||||
|
|
|
@ -371,7 +371,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
setSource: function(src) {
|
||||
var image = new window.Image(),
|
||||
crossOrigin = this._crossOrigin;
|
||||
image.src = paper.resolvePath(src);
|
||||
image.src = src;
|
||||
if (crossOrigin)
|
||||
image.crossOrigin = crossOrigin;
|
||||
this.setImage(image);
|
||||
|
|
|
@ -14,8 +14,7 @@ var Http = {
|
|||
request: function(options) {
|
||||
// Code borrowed from Coffee Script and extended:
|
||||
var xhr = new window.XMLHttpRequest();
|
||||
xhr.open((options.method || 'get').toUpperCase(),
|
||||
paper.resolvePath(options.url),
|
||||
xhr.open((options.method || 'get').toUpperCase(), options.url,
|
||||
Base.pick(options.async, true));
|
||||
if (options.mimeType)
|
||||
xhr.overrideMimeType(options.mimeType);
|
||||
|
|
|
@ -18,10 +18,9 @@ var jsdom = require('jsdom');
|
|||
// Create our document and window objects through jsdom.
|
||||
/* global document:true, window:true */
|
||||
var document = jsdom.jsdom('<html><body></body></html>', {
|
||||
// Give the resulting document the same origins as the XMLHttpRequest
|
||||
// objects for local files, so files can be loaded locally without CORS.
|
||||
// TODO: Find a proper solution instead of this hack.
|
||||
url: 'file://',
|
||||
// Use the current working directory as the document's origin, so
|
||||
// requests to local files work correctly with CORS.
|
||||
url: 'file://' + process.cwd() + '/',
|
||||
features: {
|
||||
FetchExternalResources: ['img', 'script']
|
||||
}
|
||||
|
|
|
@ -60,6 +60,10 @@ test('Create a raster from a dom image', function(assert) {
|
|||
equals(raster.size, new Size(146, 146), true);
|
||||
document.body.removeChild(img);
|
||||
done();
|
||||
},
|
||||
error: function() {
|
||||
pushFailure('Loading from a valid data URL should not give an error.');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -82,6 +86,10 @@ test('Create a raster from a dom id', function(assert) {
|
|||
equals(raster.size, new Size(146, 146), true);
|
||||
document.body.removeChild(img);
|
||||
done();
|
||||
},
|
||||
error: function() {
|
||||
pushFailure('Loading from a valid data URL should not give an error.');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue