Skip to content

No longer possible to run tests for components which import plotly without a browser #1675

Closed
@wayofthepie

Description

@wayofthepie

Recently a change was made to svg_text_utils.js which instantiates a DOMParser at the top level of that module:

var parser = new DOMParser();
.

This has broken a number of tests I have which do not use a browser (or jsdom etc..) because the DOMParser will be instantiated on import, giving the following:

var parser = new DOMParser();
                 ^

ReferenceError: DOMParser is not defined
    at Object.<anonymous> (/home/user/repo/node_modules/plotly.js/src/lib/svg_text_utils.js:22:18)
...

For now I've applied a very quick fix:

var parser;
function cachingParser() {
  parser = parser === undefined ? new DOMParser(): parser;
  return parser
};
...
var dom = cachingParser().parseFromString(skeleton, 'application/xml'),
        childNode = dom.documentElement.firstChild;
...

Can this be updated to something like the above? If not, then does the DOMParser really need to be at the top level?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions