JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
start making the stack of open elements
authorJason Woofenden <jason@jasonwoof.com>
Mon, 14 Dec 2015 19:56:16 +0000 (14:56 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Mon, 14 Dec 2015 19:56:16 +0000 (14:56 -0500)
parse-html.coffee

index a393dc0..a9e1ce0 100644 (file)
@@ -155,7 +155,7 @@ parse_html = (txt) ->
        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
@@ -487,10 +487,10 @@ parse_html = (txt) ->
        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
@@ -499,9 +499,9 @@ parse_html = (txt) ->
                                        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
@@ -511,8 +511,9 @@ parse_html = (txt) ->
                                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
@@ -524,7 +525,7 @@ parse_html = (txt) ->
                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