Last active
August 29, 2015 14:10
-
-
Save vnys/d81086b77fd96c6af7ad to your computer and use it in GitHub Desktop.
simple jsonml parser
This file contains 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
(function() { | |
var wrapper = document.createElement('div'); | |
var jsonml = [ | |
['p', 'dette er kun tekst'], | |
['p', 'dette er', [ 'strong', 'bold' ], 'tekst'], | |
['p', 'dette er enda en linje med en', ['a', { 'href' : 'http://www.ba.no' }, 'link']] | |
]; | |
function generateMarkup(parent, input) { | |
if (input instanceof Array) { | |
if (typeof input[0] === 'string') { | |
var el = document.createElement(input.shift()); | |
parent.appendChild(el); | |
parent = el; | |
} | |
for (var i = 0, len = input.length; i < len; i++) { | |
generateMarkup(parent, input[i]); | |
} | |
} else if (typeof input === 'string') { | |
var txt = document.createTextNode(' ' + input + ' '); | |
parent.appendChild(txt); | |
} else { | |
for (var attr in input) { | |
if (input.hasOwnProperty(attr)) { | |
parent.setAttribute(attr, input[attr]); | |
} | |
} | |
}; | |
}; | |
generateMarkup(wrapper, jsonml); | |
document.onreadystatechange = function() { | |
if (document.readyState === 'interactive') { | |
document.body.insertBefore(wrapper, document.body.firstChild); | |
} | |
}; | |
}()); |
This file contains 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
<div> | |
<p> dette er kun tekst </p> | |
<p> dette er <strong> bold </strong> tekst </p> | |
<p> dette er enda en linje med en <a href="http://www.ba.no"> link </a></p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment