Skip to content

Instantly share code, notes, and snippets.

@tobitailor
Forked from padolsey/gist:3901222
Created October 16, 2012 20:20
Show Gist options
  • Save tobitailor/3901732 to your computer and use it in GitHub Desktop.
Save tobitailor/3901732 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() {
for (var a, b = 0, c, d = document, e, f; a = arguments[b++];) {
for (
c in
a = a.sort ? // an array?
// 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 a[0] isn't defined:
a[0] ? (e = d.createElement(a[0].source), f = f ? f.appendChild(e) : e, a[1]) : +(f = f.parentNode || f)
: { innerHTML: a }
) // apply attribute:
e[c] = a[c];
}
return f;
}
///////////////////////
// Minified (209 bytes)
///////////////////////
function domBuilder(){for(var a,e=0,d,f=document,c,b;a=arguments[e++];)for(d in a=a.sort?a[0]?(c=f.createElement(a[0].source),b=b?b.appendChild(c):c,a[1]):+(b=b.parentNode||b):{innerHTML:a})c[d]=a[d];return b}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment