Fix issue with importing SVG document nodes.

Closes #276.
This commit is contained in:
Jürg Lehni 2013-08-26 16:35:15 -07:00
parent 19c7788617
commit cc29cab671
2 changed files with 10 additions and 4 deletions

View file

@ -95,7 +95,9 @@ var DomElement = new function() {
}, },
getStyles: function(el) { 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, ''); return view && view.getComputedStyle(el, '');
}, },

View file

@ -94,7 +94,7 @@ new function() {
for (var i = 0, l = nodes.length; i < l; i++) { for (var i = 0, l = nodes.length; i < l; i++) {
var childNode = nodes[i], var childNode = nodes[i],
child; child;
if (childNode.nodeType == 1 && (child = importSVG(childNode))) { if (childNode.nodeType === 1 && (child = importSVG(childNode))) {
// When adding CompoundPaths to other CompoundPaths, // When adding CompoundPaths to other CompoundPaths,
// we need to "unbox" them first: // we need to "unbox" them first:
if (clip && child instanceof CompoundPath) { if (clip && child instanceof CompoundPath) {
@ -148,7 +148,7 @@ new function() {
stops = []; stops = [];
for (var i = 0, l = nodes.length; i < l; i++) { for (var i = 0, l = nodes.length; i < l; i++) {
var child = nodes[i]; var child = nodes[i];
if (child.nodeType == 1) if (child.nodeType === 1)
stops.push(applyAttributes(new GradientStop(), child)); stops.push(applyAttributes(new GradientStop(), child));
} }
var isRadial = type === 'radialgradient', var isRadial = type === 'radialgradient',
@ -172,6 +172,10 @@ new function() {
// NOTE: All importers are lowercase, since jsdom is using uppercase // NOTE: All importers are lowercase, since jsdom is using uppercase
// nodeNames still. // nodeNames still.
var importers = { var importers = {
'#document': function(node) {
return importSVG(node.childNodes[0]);
},
// http://www.w3.org/TR/SVG/struct.html#Groups // http://www.w3.org/TR/SVG/struct.html#Groups
g: importGroup, g: importGroup,
// http://www.w3.org/TR/SVG/struct.html#NewDocument // http://www.w3.org/TR/SVG/struct.html#NewDocument
@ -486,7 +490,7 @@ new function() {
var type = node.nodeName.toLowerCase(), var type = node.nodeName.toLowerCase(),
importer = importers[type], importer = importers[type],
item = importer && importer(node, 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: // See importGroup() for an explanation of this filtering:
if (item && !(item instanceof Group)) if (item && !(item instanceof Group))
item = applyAttributes(item, node); item = applyAttributes(item, node);