Skip to content

Commit 273a4c3

Browse files
committed
cache DOMParser instance on svg utils module object
- to not have to instantiate one on every appendSVG call
1 parent 0c62890 commit 273a4c3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lib/svg_text_utils.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ var Lib = require('../lib');
1717
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
1818
var stringMappings = require('../constants/string_mappings');
1919

20+
exports.getDOMParser = function() {
21+
if(exports.domParser) {
22+
return exports.domParser;
23+
} else if(window.DOMParser) {
24+
exports.domParser = new window.DOMParser();
25+
return exports.domParser;
26+
} else {
27+
throw new Error('Cannot initialize DOMParser');
28+
}
29+
};
30+
2031
// Append SVG
2132

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

30-
var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
31-
childNode = dom.documentElement.firstChild;
41+
var domParser = exports.getDOMParser();
42+
var dom = domParser.parseFromString(skeleton, 'application/xml');
43+
var childNode = dom.documentElement.firstChild;
3244

3345
while(childNode) {
3446
this.node().appendChild(this.node().ownerDocument.importNode(childNode, true));

0 commit comments

Comments
 (0)