From cc29cab671e368dba863adfd03f56463e2f1f5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 26 Aug 2013 16:35:15 -0700 Subject: [PATCH] Fix issue with importing SVG document nodes. Closes #276. --- src/dom/DomElement.js | 4 +++- src/svg/SVGImport.js | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/dom/DomElement.js b/src/dom/DomElement.js index 38a5ff43..00822716 100644 --- a/src/dom/DomElement.js +++ b/src/dom/DomElement.js @@ -95,7 +95,9 @@ var DomElement = new function() { }, getStyles: function(el) { - var view = el && el.ownerDocument.defaultView; + // If el is a document (nodeType == 9), use it directly + var doc = el && el.nodeType !== 9 ? el.ownerDocument : el, + view = doc && doc.defaultView; return view && view.getComputedStyle(el, ''); }, diff --git a/src/svg/SVGImport.js b/src/svg/SVGImport.js index ba3f014c..43f5089a 100644 --- a/src/svg/SVGImport.js +++ b/src/svg/SVGImport.js @@ -94,7 +94,7 @@ new function() { for (var i = 0, l = nodes.length; i < l; i++) { var childNode = nodes[i], child; - if (childNode.nodeType == 1 && (child = importSVG(childNode))) { + if (childNode.nodeType === 1 && (child = importSVG(childNode))) { // When adding CompoundPaths to other CompoundPaths, // we need to "unbox" them first: if (clip && child instanceof CompoundPath) { @@ -148,7 +148,7 @@ new function() { stops = []; for (var i = 0, l = nodes.length; i < l; i++) { var child = nodes[i]; - if (child.nodeType == 1) + if (child.nodeType === 1) stops.push(applyAttributes(new GradientStop(), child)); } var isRadial = type === 'radialgradient', @@ -172,6 +172,10 @@ new function() { // NOTE: All importers are lowercase, since jsdom is using uppercase // nodeNames still. var importers = { + '#document': function(node) { + return importSVG(node.childNodes[0]); + }, + // http://www.w3.org/TR/SVG/struct.html#Groups g: importGroup, // http://www.w3.org/TR/SVG/struct.html#NewDocument @@ -486,7 +490,7 @@ new function() { var type = node.nodeName.toLowerCase(), importer = importers[type], item = importer && importer(node, type), - data = node.getAttribute('data-paper-data'); + data = type !== '#document' && node.getAttribute('data-paper-data'); // See importGroup() for an explanation of this filtering: if (item && !(item instanceof Group)) item = applyAttributes(item, node);