Created
January 9, 2014 03:12
-
-
Save nornagon/8328845 to your computer and use it in GitHub Desktop.
tag()
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
tag = (name, text, attrs) -> | |
parts = (name ? 'div').split /(?=[.#])/ | |
tagName = "div" | |
classes = [] | |
id = undefined | |
for p in parts when p.length | |
switch p[0] | |
when '#' then id = p.substr 1 if p.length > 1 | |
when '.' then classes.push p.substr 1 if p.length > 1 | |
else tagName = p | |
if /:/.test tagName | |
[ns, tagName] = tagName.split /:/ | |
if ns is 'svg' | |
element = document.createElementNS "http://www.w3.org/2000/svg", tagName | |
else | |
element = document.createElement tagName | |
element.id = id if id? | |
element.classList.add c for c in classes | |
element = $ element | |
element.attr attrs if attrs? | |
if typeof text is 'string' or typeof text is 'number' | |
element.text text | |
else if text?.length? | |
element.append e for e in text | |
else if text | |
element.append text | |
element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment