cur = 0 # index of next char in txt to be parsed
# declare tree and tokenizer variables so they're in scope below
tree = null
- tree_append_point = null
+ open_tags = [] # stack of open elements
tree_state = null
tok_state = null
tok_cur_tag = null # partially parsed tag
tree_append = (t) ->
switch t[0]
when TYPE_TEXT
- if tree_append_point.length > 0 and tree_append_point[tree_append_point.length - 1][0] is TYPE_TEXT
- tree_append_point[tree_append_point.length - 1][1] += t[1]
+ if open_tags[0][3].length > 0 and open_tags[0][3][open_tags[0][3].length - 1][0] is TYPE_TEXT
+ open_tags[0][3][open_tags[0][3].length - 1][1] += t[1]
else
- tree_append_point.push t
+ open_tags[0][3].push t
when TYPE_OPEN_TAG
t[0] = TYPE_TAG
# convert attributes into a hash
a = t[2].pop()
attrs[a[0]] = a[1]
t[2] = attrs
- tree_append_point.push t
- tree_append_point = t[3]
- # TODO implement stack of open elements
+ # FIXME probs have to auto-close things first
+ open_tags[0][3].push t
+ open_tags.unshift t
# TODO implement formatting elements thing
when TYPE_EOF
return
console.log "UNIMPLEMENTED tag type: #{t[0]}"
# tree constructor initialization
- tree = [] # see comments on TYPE_TAG/etc for the structure of this data
- tree_append_point = tree
+ # see comments on TYPE_TAG/etc for the structure of this data
+ tree = [TYPE_TAG, 'html', {}, []]
+ open_tags = [tree]
tree_state = tree_append
# tokenizer initialization
if t?
tree_state t
if t[0] is TYPE_EOF
- return tree
+ return tree[3]
return # never reached
# everything below is tests on the above