Skip to content

Commit a2a300b

Browse files
committed
Correctly append children; don't wrap text nodes
1 parent 580b7f6 commit a2a300b

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

index.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,24 @@ JSONMLParser.prototype._createSourceOptions = function() {
2727
var transform = this;
2828
return {
2929
onopentag: function(tagName, attributes) {
30-
var elementList = [];
31-
var element = [tagName, attributes, elementList];
30+
var element = [tagName];
31+
if (!isEmpty(attributes)) {
32+
element.push(attributes);
33+
}
3234
transform._parent.push(element);
33-
elementList.parent = transform._parent;
34-
transform._parent = elementList;
35+
element.parent = transform._parent;
36+
transform._parent = element;
3537
},
3638
ontext: function(text) {
37-
transform._parent.push(['#text', text]);
39+
transform._parent.push(text);
3840
},
3941
oncomment: function(text) {
4042
transform._parent.push(['#comment', text]);
4143
},
4244
onclosetag: function() {
43-
var p = transform._parent.parent;
44-
// Delete elementList and/or attributes if empty
45-
var lastChild = p[p.length - 1];
46-
for (var i = 2; i > 0; i--) {
47-
if (isEmpty(lastChild[i])) {
48-
lastChild.splice(i, 1);
49-
}
50-
}
45+
var parent = transform._parent.parent;
5146
delete transform._parent.parent;
52-
transform._parent = p;
47+
transform._parent = parent;
5348
},
5449
onerror: function(err) {
5550
transform.emit('error', err);

test/04-nested-tags/expected.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
[["ul", [
2-
["#text", "\n "],
3-
["li", [["#text", "One"]]],
4-
["#text", "\n "],
5-
["li", { "class": "selected" }, [["#text", "Two"]]],
6-
["#text", "\n "],
7-
["li", [["#text", "Three"]]],
8-
["#text", "\n"]
9-
]]]
1+
[["ul",
2+
"\n ",
3+
["li", "One"],
4+
"\n ",
5+
["li", { "class": "selected" }, "Two"],
6+
"\n ",
7+
["li", "Three"],
8+
"\n"
9+
]]

test/05-comments/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
22
["#comment", " this is a comment "],
3-
["#text", "\nNot a comment\n"],
3+
"\nNot a comment\n",
44
["#comment", " another comment "]
55
]

test/cli/expected.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)