diff --git a/demo.html b/demo.html index b4a63e2..158e42a 100644 --- a/demo.html +++ b/demo.html @@ -70,7 +70,7 @@

Pure JavaScript HTML5 Parser

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; }; }; diff --git a/htmlparser.js b/htmlparser.js index 625eee2..22cb90b 100644 --- a/htmlparser.js +++ b/htmlparser.js @@ -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(",");