Created
January 27, 2016 20:06
-
-
Save mikeyhew/84881b969321c5ab5011 to your computer and use it in GitHub Desktop.
Simple DOM element creator with nice syntax for coffeescript. Not recommended for production applications.
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
range = (start, stop, step=1) -> | |
if arguments.length == 1 | |
stop = start | |
start = 0 | |
x = start | |
while start < stop | |
old = start | |
start += step | |
old | |
methods = ( -> | |
parent = null | |
error = (message) -> | |
parent = null | |
throw new Error(message) | |
return { | |
tag: (tagName, attributes, body) -> | |
switch arguments.length | |
when 1 | |
attributes = {} | |
body = null | |
when 2 | |
if typeof attributes == 'function' | |
body = attributes | |
attributes = {} | |
else | |
body = null | |
when 3 | |
else | |
throw new Error("unexpected number of arguments: #{arguments.length}") | |
el = document.createElement tagName | |
for attr, value of attributes | |
el.setAttribute attr, value | |
if parent != null | |
parent.appendChild(el) | |
if body | |
oldParent = parent | |
parent = el | |
body() | |
parent = oldParent | |
return el | |
html: (str) -> | |
parent.innerHTML = str | |
parent.children | |
text: (str) -> | |
parent.textContent = str | |
parent.textContent | |
} | |
)() | |
self.tag = methods.tag | |
self.text = methods.text | |
self.html = methods.html |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<script type="text/javascript" src="./tagCreator.js"></script> | |
<script type="text/coffeescript"> | |
t = tag 'div', -> | |
tag 'h1', -> text 'yolo' | |
tag 'p', -> text """ | |
My name is Michael, and I am awesome. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
""" | |
tag 'footer', -> | |
tag 'div', -> text 'this is the footer' | |
tag 'div', -> html '<b>this is some html in the footer</p>' | |
document.body.appendChild t | |
</script> | |
<script type="text/javascript" src="http://github.com/jashkenas/coffee-script/raw/master/extras/coffee-script.js"></script> | |
</body> |
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
// Generated by CoffeeScript 1.10.0 | |
(function() { | |
var methods, range; | |
range = function(start, stop, step) { | |
var old, results, x; | |
if (step == null) { | |
step = 1; | |
} | |
if (arguments.length === 1) { | |
stop = start; | |
start = 0; | |
} | |
x = start; | |
results = []; | |
while (start < stop) { | |
old = start; | |
start += step; | |
results.push(old); | |
} | |
return results; | |
}; | |
methods = (function() { | |
var error, parent; | |
parent = null; | |
error = function(message) { | |
parent = null; | |
throw new Error(message); | |
}; | |
return { | |
tag: function(tagName, attributes, body) { | |
var attr, el, oldParent, value; | |
switch (arguments.length) { | |
case 1: | |
attributes = {}; | |
body = null; | |
break; | |
case 2: | |
if (typeof attributes === 'function') { | |
body = attributes; | |
attributes = {}; | |
} else { | |
body = null; | |
} | |
break; | |
case 3: | |
break; | |
default: | |
throw new Error("unexpected number of arguments: " + arguments.length); | |
} | |
el = document.createElement(tagName); | |
for (attr in attributes) { | |
value = attributes[attr]; | |
el.setAttribute(attr, value); | |
} | |
if (parent !== null) { | |
parent.appendChild(el); | |
} | |
if (body) { | |
oldParent = parent; | |
parent = el; | |
body(); | |
parent = oldParent; | |
} | |
return el; | |
}, | |
html: function(str) { | |
parent.innerHTML = str; | |
return parent.children; | |
}, | |
text: function(str) { | |
parent.textContent = str; | |
return parent.textContent; | |
} | |
}; | |
})(); | |
self.tag = methods.tag; | |
self.text = methods.text; | |
self.html = methods.html; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment