Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadSVGFromURL no way to tell if failed #2528

Closed
jrett opened this issue Oct 6, 2015 · 1 comment · Fixed by #2994
Closed

loadSVGFromURL no way to tell if failed #2528

jrett opened this issue Oct 6, 2015 · 1 comment · Fixed by #2994
Assignees
Labels
Milestone

Comments

@jrett
Copy link

jrett commented Oct 6, 2015

Unless I'm missing something, the callback is called only if the image is successfully loaded. If an error occurs, it is logged to the console, but there is no explicit error either thrown or returned.
I think it should call the callback with a null object.
I believe this is also happening on the API Image.fromURL.

@kangax kangax added the feature label Oct 11, 2015
@asturur asturur self-assigned this May 6, 2016
@asturur
Copy link
Member

asturur commented May 6, 2016

    /**
     * Takes url corresponding to an SVG document, and parses it into a set of fabric objects. Note that SVG is fetched via XMLHttpRequest, so it needs to conform to SOP (Same Origin Policy)
     * @memberOf fabric
     * @param {String} url
     * @param {Function} callback
     * @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
     */
    loadSVGFromURL: function(url, callback, reviver) {

      url = url.replace(/^\n\s*/, '').trim();
      svgCache.has(url, function (hasUrl) {
        if (hasUrl) {
          svgCache.get(url, function (value) {
            var enlivedRecord = _enlivenCachedObject(value);
            callback(enlivedRecord.objects, enlivedRecord.options);
          });
        }
        else {
          new fabric.util.request(url, {
            method: 'get',
            onComplete: onComplete
          });
        }
      });

      function onComplete(r) {

        var xml = r.responseXML;
        if (xml && !xml.documentElement && fabric.window.ActiveXObject && r.responseText) {
          xml = new ActiveXObject('Microsoft.XMLDOM');
          xml.async = 'false';
          //IE chokes on DOCTYPE
          xml.loadXML(r.responseText.replace(/<!DOCTYPE[\s\S]*?(\[[\s\S]*\])*?>/i, ''));
        }
        if (!xml || !xml.documentElement) {
-           return;
+          callback && callback(null);
        }

        fabric.parseSVGDocument(xml.documentElement, function (results, options) {
          svgCache.set(url, {
            objects: fabric.util.array.invoke(results, 'toObject'),
            options: options
          });
+          callback && callback(results, options);
-           callback(results, options);
        }, reviver);
      }
    },

If you mean something like this i can make a pr.
The callback is called with null on failure like on the image.fromURL api.
Just think if we should check r.status != 200 and redirects....?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants