Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

handle arrays of arrays. and children within arrays still render norm…

…all.
  • Loading branch information...
commit 0bfa0461ff0e8b2c9e61ff3a14eb94bc1d82beb9 1 parent 1c5084d
@sconover sconover authored committed
Showing with 47 additions and 11 deletions.
  1. +25 −0 specs/node_spec.js
  2. +22 −11 src/Node.js
View
25 specs/node_spec.js
@@ -156,6 +156,31 @@ describe("Jaml.Node", function() {
" <x/>\n" +
"yz</fooBar>\n");
});
+
+ it("renders arrays of arrays too", function(){
+ expect(fooBar.addChild([[new Jaml.Node("x").addChild(new Jaml.Node("J")),
+ [new Jaml.Node("y").addChild(new Jaml.Node("K"))]]]).
+ addChild(new Jaml.Node("z")).render()).
+ toEqual("<fooBar>\n" +
+ " <x>\n" +
+ " <J/>\n" +
+ " </x>\n" +
+ " <y>\n" +
+ " <K/>\n" +
+ " </y>\n" +
+ " <z/>\n" +
+ "</fooBar>\n");
+ });
+
+ it("renders children of children in arrays properly", function(){
+ expect(fooBar.addChild([[new Jaml.Node("x"), [new Jaml.Node("y")]]]).
+ addChild(new Jaml.Node("z")).render()).
+ toEqual("<fooBar>\n" +
+ " <x/>\n" +
+ " <y/>\n" +
+ " <z/>\n" +
+ "</fooBar>\n");
+ });
})
describe("textnode children", function() {
View
33 src/Node.js
@@ -84,17 +84,8 @@ Jaml.Node.prototype = {
node.push(">");
if (multiline) node.push("\n");
-
- for (var i=0; i < this.children.length; i++) {
- var child = this.children[i];
- if (child instanceof Array) {
- for (var j=0; j < child.length; j++) {
- node.push(child[j].render(lpad + 2));
- }
- } else {
- node.push(child.render(lpad + 2));
- }
- }
+
+ this.renderChildren(node, this.children, lpad);
if (multiline) node.push(this.getPadding(lpad));
node.push("</", this.tagName, ">\n");
@@ -102,6 +93,26 @@ Jaml.Node.prototype = {
return node.join("");
},
+
+ /**
+ * Renders an array of children
+ * @node {Array} the current array of rendered lines
+ * @children {Array} the child nodes to be rendered
+ * @param {Number} lpad Amount of whitespace to add to the left of the string
+ */
+ renderChildren: function(node, children, lpad) {
+ var childLpad = lpad + 2;
+
+ for (var i=0; i < children.length; i++) {
+ var child = children[i];
+ if (child instanceof Array) {
+ var nestedChildren = child;
+ this.renderChildren(node, nestedChildren, lpad)
+ } else {
+ node.push(child.render(childLpad));
+ }
+ }
+ },
/**
* Returns true if this tag should be rendered with multiple newlines (e.g. if it contains child nodes)
Please sign in to comment.
Something went wrong with that request. Please try again.