GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.37k stars 4.05k forks source link

BUG: v0.19.5 Breaks headless editor.setComponents() (with Map/Video block) #4481

Closed skru closed 2 years ago

skru commented 2 years ago

GrapesJS version

What browser are you using?

Chrome 102.0.5005.115 (Official Build) (64-bit)

Reproducible demo link

-

Describe the bug

This is an extension of #4473

When adding a map or video block via editor.setComponents() in a headless environment you get a similar "document is not defined error" stemming from ./src/dom_components/model/ComponentImage.js where parseUri() is trying to use the document object.

I've had to quickly put a fix in for us as it's a product launch stopper. I've refactored the parseUri() method to look like this (tests pass and no error):

parseUri(uri) {

  let uriParams = {
    hash: "",
    hostname: "",
    pathname: "",
    port: "",
    protocol: "",
    query: {},
    search: ""
  }

  if (uri.substring(0, 4) !== "<svg") {

    const url = new URL(uri);
    let query = {};
    var qrs = url.search.substring(1).split('&');
    for (let i = 0; i < qrs.length; i++) {
      let pair = qrs[i].split('=');
      let name = decodeURIComponent(pair[0]);
      if (name) query[name] = decodeURIComponent(pair[1]);
    }

    uriParams.hostname = url.hostname;
    uriParams.pathname = url.pathname;
    uriParams.protocol = url.protocol;
    uriParams.search = url.search;
    uriParams.hash = url.hash;
    uriParams.port = url.port;
    uriParams.query = query;

    return uriParams;

  } else {

    uriParams.pathname = encodeURI(uri);

    return uriParams;
  }
},

That's working fine, but I'm not certain it's the best place to be checking for the svg?? Any tips @artf for a PR?

All the best

Code of Conduct

artf commented 2 years ago

Thanks @skru I've actually already taken care of the parseUri here so it should work in the next release