Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h1>Pure JavaScript HTML5 Parser</h1>
if (e) e.preventDefault();
if (typeof event != "undefined") event.returnValue = false;

output.value = HTMLtoXML(input.value);
output.value = HTMLtoXML(input.value) + "\r\n\r\n" + JSON.stringify(HTMLtoJSONML(input.value));
return false;
};
};
Expand Down
30 changes: 30 additions & 0 deletions htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,36 @@

return doc;
};
this.HTMLtoJSONML = function (html, nodes) {
nodes = nodes || ['div', []]
var elems = [], curParentNode = nodes;

HTMLParser(html, {
start: function (tag, attrs, unary) {
var elem = [tag, attrs]
curParentNode.push(elem)

if (!unary) {
elems.push(elem);
curParentNode = elem;
}
},
end: function (tag) {
elems.length -= 1;

// Init the new parentNode
curParentNode = elems[elems.length - 1];
},
chars: function (text) {
curParentNode.push(text)
},
comment: function (text) {
// create comment node
}
});

return nodes;
};

function makeMap(str) {
var obj = {}, items = str.split(",");
Expand Down