diff --git a/clipart/star.svg b/clipart/star.svg
index c5aa58a1..62bb89a4 100644
--- a/clipart/star.svg
+++ b/clipart/star.svg
@@ -1,4 +1,4 @@
diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js
index 41568947..5e42531d 100644
--- a/editor/svgcanvas.js
+++ b/editor/svgcanvas.js
@@ -5548,6 +5548,7 @@ function BatchCommand(text) {
call("saved", str);
};
+ // Walks the tree and executes the callback on each element in a top-down fashion
var walkTree = function(elem, cbFn){
if (elem && elem.nodeType == 1) {
cbFn(elem);
@@ -5557,6 +5558,16 @@ function BatchCommand(text) {
}
}
};
+ // Walks the tree and executes the callback on each element in a depth-first fashion
+ var walkTreePost = function(elem, cbFn) {
+ if (elem && elem.nodeType == 1) {
+ var i = elem.childNodes.length;
+ while (i--) {
+ walkTree(elem.childNodes.item(i), cbFn);
+ }
+ cbFn(elem);
+ }
+ };
// Function: getSvgString
// Returns the current drawing as raw SVG XML text.
@@ -5617,17 +5628,7 @@ function BatchCommand(text) {
// recalculate dimensions on the top-level children so that unnecessary transforms
// are removed
- var deepdive = function(node) {
- if (node.nodeType == 1) {
- var children = node.childNodes;
- var i = children.length;
- while (i--) { deepdive(children.item(i)); }
- try {
- recalculateDimensions(node);
- } catch(e) { console.log(e); }
- }
- }
- deepdive(svgcontent);
+ walkTreePost(svgcontent, function(n){try{recalculateDimensions(n)}catch(e){console.log(e)}});
var content = $(svgcontent);
@@ -5695,7 +5696,6 @@ function BatchCommand(text) {
// TODO: import should happen in top-left of current zoomed viewport
// TODO: create a new layer for the imported SVG
- // TODO: properly re-identify all elements and references in the imported SVG
this.importSvgString = function(xmlString) {
try {
// convert string into XML document
@@ -5708,34 +5708,95 @@ function BatchCommand(text) {
// import new svg document into our document
var importedNode = svgdoc.importNode(newDoc.documentElement, true);
- if (current_layer) {
- // TODO: properly handle if width/height are not specified or if in percentages
- // TODO: properly handle if width/height are in units (px, etc)
- var innerw = importedNode.getAttribute("width"),
- innerh = importedNode.getAttribute("height"),
- innervb = importedNode.getAttribute("viewBox"),
- // if no explicit viewbox, create one out of the width and height
- vb = innervb ? innervb.split(" ") : [0,0,innerw,innerh];
- for (var j = 0; j < 4; ++j)
- vb[j] = Number(vb[j]);
+ if (current_layer) {
+ // TODO: properly handle if width/height are not specified or if in percentages
+ // TODO: properly handle if width/height are in units (px, etc)
+ var innerw = importedNode.getAttribute("width"),
+ innerh = importedNode.getAttribute("height"),
+ innervb = importedNode.getAttribute("viewBox"),
+ // if no explicit viewbox, create one out of the width and height
+ vb = innervb ? innervb.split(" ") : [0,0,innerw,innerh];
+ for (var j = 0; j < 4; ++j)
+ vb[j] = Number(vb[j]);
- // TODO: properly handle preserveAspectRatio
- var canvasw = Number(svgcontent.getAttribute("width")),
- canvash = Number(svgcontent.getAttribute("height"));
- // imported content should be 1/3 of the canvas on its largest dimension
- if (innerh > innerw) {
- var ts = "scale(" + (canvash/3)/vb[3] + ")";
- }
- else {
- var ts = "scale(" + (canvash/3)/vb[2] + ")";
- }
+ // TODO: properly handle preserveAspectRatio
+ var canvasw = Number(svgcontent.getAttribute("width")),
+ canvash = Number(svgcontent.getAttribute("height"));
+ // imported content should be 1/3 of the canvas on its largest dimension
+ if (innerh > innerw) {
+ var ts = "scale(" + (canvash/3)/vb[3] + ")";
+ }
+ else {
+ var ts = "scale(" + (canvash/3)/vb[2] + ")";
+ }
- // add all children of the imported