Skip to content

Instantly share code, notes, and snippets.

@jcutrell
Forked from padolsey/gist:3901222
Created October 16, 2012 19:04
Show Gist options
  • Save jcutrell/3901273 to your computer and use it in GitHub Desktop.
Save jcutrell/3901273 to your computer and use it in GitHub Desktop.
// From the 140bytes wishlist here: https://github.com/jed/140bytes/wiki/Wishlist
// TASK:
// Create a function which can create a DOM structure from this:
//
domBuilder(
[/HTML/],
[/HEAD/],
[/TITLE/],
"Wouldn't this be cool?",
[],
[],
[/BODY/],
[/DIV/, {id: "container"}],
"Hello, world",
[],
[],
[]
);
// =>
// <html>
// <head><title>Wouldn't this be cool?</title></head>
// <body><div id="container">Hello, world</div></body>
// </html>
/////////////////////
// My implementation:
/////////////////////
function domBuilder(a, b, c, d, i, m) {
a = [].slice.call(arguments);
for (d = i = 0,m=document; b = a[i++];)
!b.sort && d.appendChild(m.createTextNode(b)); || for (
c in
// NOTE: this is not a notable part of the for..in -- it is placed here for space-saving
// Here we create a new child or traverse upwards if b[0] isn't defined:
d = b[0] ? (c = m.createElement(b[0].source),d ? d.appendChild(c) : c) : d.parentNode || d,
b[1]
) // apply attribute:
d[c]=b[1][c];
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment