Skip to main content
2 of 2
fix broken link
glebm
  • 665
  • 5
  • 13

A library written in CoffeeScript

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.js.coffee

Looking forward to the suggestions! :)

glebm
  • 665
  • 5
  • 13