So, I wrote this small library for fast DOM building with API tailored for CoffeeScript
I feel like the code overall can be made better/smaller/faster.
I am also not particularly happy about a few functions, namely:
# Parses out css classes and id from string like:
# p#warning.big.yellow # => p # attr {"id": "warning", "class": ['big', 'yellow']}
# #container # => div # attr {"id": "container"}
# @returns node name (e.g. "span")
dotHashRe = /[.#]/
parseElem = (elem, attr) ->
return elem unless dotHashRe.test elem
attr['class'] ||= []
attr['class'] = [attr['class']] if typeof attr['class'] == 'string'
elem = "div#{elem}" if dotHashRe.test(elem.charAt(0))
pieces = elem.split(dotHashRe)
elemType = pieces.shift()
pos = elemType.length
classes = attr['class']
for piece in pieces
if elem.charAt(pos) == '#'
attr['id'] = piece
else
classes.push(piece)
pos += piece.length + 1
delete attr['class'] unless attr['class'].length
elemType
It is fast, but it looks like the code can be made a lot nicer
The library (~100 LOC with comments): https://github.com/glebm/DOMBrew/blob/master/dombrew.coffeehttps://github.com/glebm/DOMBrew/blob/master/dombrew.js.coffee
Looking forward to the suggestions! :)