Skip to content

Commit

Permalink
support svg crossorigin on images
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed Mar 25, 2017
1 parent 9778898 commit e4a42e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/elements_parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
fabric.ElementsParser = function(elements, callback, options, reviver) {
fabric.ElementsParser = function(elements, callback, options, reviver, parsingOptions) {
this.elements = elements;
this.callback = callback;
this.options = options;
this.reviver = reviver;
this.svgUid = (options && options.svgUid) || 0;
this.parsingOptions = parsingOptions;
};

fabric.ElementsParser.prototype.parse = function() {
Expand Down
32 changes: 19 additions & 13 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,10 @@
* @param {Function} callback Callback to call when parsing is finished;
* It's being passed an array of elements (parsed from a document).
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
* @param {Object} [parsingOptions] options for parsing document
* @param {String} [parsingOptions.crossOrigin] crossOrigin settings
*/
fabric.parseSVGDocument = function(doc, callback, reviver) {
fabric.parseSVGDocument = function(doc, callback, reviver, parsingOptions) {
if (!doc) {
return;
}
Expand All @@ -613,7 +615,7 @@
var svgUid = fabric.Object.__uid++,
options = applyViewboxTransform(doc),
descendants = fabric.util.toArray(doc.getElementsByTagName('*'));

options.crossOrigin = parsingOptions.crossOrigin;
options.svgUid = svgUid;

if (descendants.length === 0 && fabric.isLikelyNode) {
Expand Down Expand Up @@ -645,7 +647,7 @@
if (callback) {
callback(instances, options);
}
}, clone(options), reviver);
}, clone(options), reviver, parsingOptions);
};

var reFontDeclaration = new RegExp(
Expand Down Expand Up @@ -797,8 +799,8 @@
* @param {Object} [options] Options object
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
*/
parseElements: function(elements, callback, options, reviver) {
new fabric.ElementsParser(elements, callback, options, reviver).parse();
parseElements: function(elements, callback, options, reviver, parsingOptions) {
new fabric.ElementsParser(elements, callback, options, reviver, parsingOptions).parse();
},

/**
Expand Down Expand Up @@ -924,8 +926,10 @@
* @param {String} url
* @param {Function} callback
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
* @param {Object} [options] Object containing options for parsing
* @param {String} [options.crossOrigin] crossOrigin crossOrigin setting to use for external resources
*/
loadSVGFromURL: function(url, callback, reviver) {
loadSVGFromURL: function(url, callback, reviver, options) {

url = url.replace(/^\n\s*/, '').trim();
new fabric.util.request(url, {
Expand All @@ -946,9 +950,9 @@
callback && callback(null);
}

fabric.parseSVGDocument(xml.documentElement, function (results, options) {
callback && callback(results, options);
}, reviver);
fabric.parseSVGDocument(xml.documentElement, function (results, _options) {
callback && callback(results, _options);
}, reviver, options);
}
},

Expand All @@ -958,8 +962,10 @@
* @param {String} string
* @param {Function} callback
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
* @param {Object} [options] Object containing options for parsing
* @param {String} [options.crossOrigin] crossOrigin crossOrigin setting to use for external resources
*/
loadSVGFromString: function(string, callback, reviver) {
loadSVGFromString: function(string, callback, reviver, options) {
string = string.trim();
var doc;
if (typeof DOMParser !== 'undefined') {
Expand All @@ -975,9 +981,9 @@
doc.loadXML(string.replace(/<!DOCTYPE[\s\S]*?(\[[\s\S]*\])*?>/i, ''));
}

fabric.parseSVGDocument(doc.documentElement, function (results, options) {
callback(results, options);
}, reviver);
fabric.parseSVGDocument(doc.documentElement, function (results, _options) {
callback(results, _options);
}, reviver, options);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@
* @see {@link http://www.w3.org/TR/SVG/struct.html#ImageElement}
*/
fabric.Image.ATTRIBUTE_NAMES =
fabric.SHARED_ATTRIBUTES.concat('x y width height preserveAspectRatio xlink:href'.split(' '));
fabric.SHARED_ATTRIBUTES.concat('x y width height preserveAspectRatio xlink:href crossOrigin'.split(' '));

/**
* Returns {@link fabric.Image} instance from an SVG element
Expand Down

0 comments on commit e4a42e0

Please sign in to comment.