Skip to content

Commit

Permalink
cache DOMParser instance on svg utils module object
Browse files Browse the repository at this point in the history
- to not have to instantiate one on every appendSVG call
  • Loading branch information
etpinard committed May 15, 2017
1 parent 0c62890 commit 273a4c3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ var Lib = require('../lib');
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
var stringMappings = require('../constants/string_mappings');

exports.getDOMParser = function() {
if(exports.domParser) {
return exports.domParser;
} else if(window.DOMParser) {
exports.domParser = new window.DOMParser();
return exports.domParser;
} else {
throw new Error('Cannot initialize DOMParser');
}
};

// Append SVG

d3.selection.prototype.appendSVG = function(_svgString) {
Expand All @@ -27,8 +38,9 @@ d3.selection.prototype.appendSVG = function(_svgString) {
'</svg>'
].join('');

var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
childNode = dom.documentElement.firstChild;
var domParser = exports.getDOMParser();
var dom = domParser.parseFromString(skeleton, 'application/xml');
var childNode = dom.documentElement.firstChild;

while(childNode) {
this.node().appendChild(this.node().ownerDocument.importNode(childNode, true));
Expand Down

0 comments on commit 273a4c3

Please sign in to comment.