From 893f8fb2cc9d0ab15dce72e1949ac73d283f725d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 3 Nov 2013 13:20:51 +0100 Subject: [PATCH] Fix issues with importing full SVG documents where default styles would not be detected. --- src/svg/SVGImport.js | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/svg/SVGImport.js b/src/svg/SVGImport.js index c9eb47df..0827a8a3 100644 --- a/src/svg/SVGImport.js +++ b/src/svg/SVGImport.js @@ -74,12 +74,11 @@ new function() { function importGroup(node, type, isRoot, options) { var nodes = node.childNodes, isClip = type === 'clippath', - isDocument = type === '#document', item = new Group(), project = item._project, currentStyle = project._currentStyle, children = []; - if (!isClip && !isDocument) { + if (!isClip) { // Have the group not pass on all transformations to its children, // as this is how SVG works too. item._transformContent = false; @@ -103,16 +102,10 @@ new function() { item.addChildren(children); // Clip paths are reduced (unboxed) and their attributes applied at the // end. - if (isClip || isDocument) { - // If a document or a clip item only contain one child, remove the - // group. - item = item.reduce(); - if (isClip) - item = applyAttributes(item, node, isRoot); - } else { - // Restore currentStyle - project._currentStyle = currentStyle; - } + if (isClip) + item = applyAttributes(item.reduce(), node, isRoot); + // Restore currentStyle + project._currentStyle = currentStyle; if (isClip || type === 'defs') { // We don't want the defs in the DOM. But we might want to use // Symbols for them to save memory? @@ -173,7 +166,26 @@ new function() { // NOTE: All importers are lowercase, since jsdom is using uppercase // nodeNames still. var importers = { - '#document': importGroup, + '#document': function (node, type, isRoot, options) { + var nodes = node.childNodes; + for (var i = 0, l = nodes.length; i < l; i++) { + var child = nodes[i]; + if (child.nodeType === 1) { + // NOTE: We need to move the svg node into our current + // document, so default styles apply! + var next = child.nextSibling; + document.body.appendChild(child); + var item = importSVG(child, isRoot, options); + // After import, we move it back to where it was: + if (next) { + node.insertBefore(child, next); + } else { + node.appendChild(child); + } + return item; + } + } + }, // http://www.w3.org/TR/SVG/struct.html#Groups g: importGroup, // http://www.w3.org/TR/SVG/struct.html#NewDocument