Last active
February 10, 2016 15:38
-
-
Save paolocaminiti/74fcd11b9da29a73c240 to your computer and use it in GitHub Desktop.
JSONML to immediate object rappresentation, parsable just as real DOM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Converts JSONML markup to an immediate obj rappresentation, parsable just as real DOM. | |
| * ex. { | |
| * attributes: [ | |
| * { name: 'id', value: 'unique' }, | |
| * { name: 'class', value: 'class class2' }, | |
| * { name: 'style', value: 'color: hotpink' }, | |
| * ... | |
| * ], | |
| * childNodes: [ | |
| * { | |
| * data: "text node, could have been a nested element", | |
| * nodeName: '#text', | |
| * nodeType: 3 | |
| * }, | |
| * ... | |
| * ], | |
| * nodeName: 'div', | |
| * nodeType: 1 | |
| * } | |
| * | |
| * @param {JSONML array} markup The JSONML markup. | |
| * @return {object} An object with exact same interface as the DOM described by markup. | |
| */ | |
| function jsonml2domobj(markup) { | |
| var el = { | |
| attributes: [], | |
| childNodes: [], | |
| nodeName: markup[0], | |
| nodeType: 1 | |
| } | |
| for (var i = 1, l = markup.length; i < l; i++) { | |
| var mNode = markup[i] | |
| var constructor = mNode.constructor | |
| if (constructor === Object) { | |
| for (var attr in mNode) { | |
| el.attributes.push({ name: attr, value: mNode[attr] }) | |
| } | |
| } else if (constructor === Array) { | |
| el.childNodes.push(jsonML2objML(mNode)) | |
| } else { | |
| el.childNodes.push({ data: mNode, nodeName: '#text', nodeType: 3 }) | |
| } | |
| } | |
| return el | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment