JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
code cleanup, esp cloning nodes
authorJason Woofenden <jason@jasonwoof.com>
Tue, 22 Dec 2015 23:01:25 +0000 (18:01 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 22 Dec 2015 23:01:25 +0000 (18:01 -0500)
parse-html.coffee

index f64c734..cf84b32 100644 (file)
@@ -96,11 +96,6 @@ class Node
                        @id = "#{args.id}+"
                else
                        @id = "#{++prev_node_id}"
-       shallow_clone: -> # return a new node that's the same except without the children or parent
-               # WARNING this doesn't work right on open tags that are still being parsed
-               attrs = {}
-               attrs[k] = v for k, v of @attrs
-               return new Node @type, name: @name, text: @text, attrs: attrs, namespace: @namespace, id: @id, token: @token
        acknowledge_self_closing: ->
                if @token?
                        @token.flag 'did_self_close'
@@ -143,8 +138,7 @@ class Node
                                ret += 'comment:'
                                ret += JSON.stringify @text
                        when TYPE_DOCTYPE
-                               ret += 'doctype'
-                               # FIXME
+                               ret += "doctype:#{@name},#{JSON.stringify(@public_identifier ? '')},#{JSON.stringify(@system_identifier ? '')}"
                        when TYPE_AFE_MARKER
                                ret += 'marker'
                        when TYPE_AAA_BOOKMARK
@@ -441,7 +435,7 @@ parse_html = (txt, parse_error_cb = null) ->
                        if scope2[t.name] is t.namespace
                                return false
                return false
-       standard_scopers = { # FIXME these are supposed to be namespace specific
+       standard_scopers = {
                applet: NS_HTML, caption: NS_HTML, html: NS_HTML, table: NS_HTML,
                td: NS_HTML, th: NS_HTML, marquee: NS_HTML, object: NS_HTML,
                template: NS_HTML, mi: NS_MATHML,
@@ -625,11 +619,13 @@ parse_html = (txt, parse_error_cb = null) ->
                        if node.name is 'html'
                                # 1. If the head element pointer is null, switch the insertion
                                # mode to "before head" and abort these steps. (fragment case)
-                               # fixfull (fragment case)
-
-                               # 2. Otherwise, the head element pointer is not null, switch
-                               # the insertion mode to "after head" and abort these steps.
-                               insertion_mode = ins_mode_in_body # FIXME fixfull
+                               if head_element_pointer is null
+                                       ins_mode = ins_mode_before_head
+                               else
+                                       # 2. Otherwise, the head element pointer is not null,
+                                       # switch the insertion mode to "after head" and abort these
+                                       # steps.
+                                       insertion_mode = ins_mode_after_head
                                return
                        # 17. If last is true, then switch the insertion mode to "in body"
                        # and abort these steps. (fragment case)
@@ -668,8 +664,7 @@ parse_html = (txt, parse_error_cb = null) ->
                                break
                # Create
                loop
-                       el = afe[i].shallow_clone()
-                       tree_insert_element el
+                       el = insert_html_element afe[i].token
                        afe[i] = el
                        break if i is 0
                        i -= 1 # Advance
@@ -840,7 +835,7 @@ parse_html = (txt, parse_error_cb = null) ->
                                # element, replace the entry for node in the stack of open
                                # elements with an entry for the new element, and let node be
                                # the new element.
-                               new_node = node.shallow_clone()
+                               new_node = token_to_element node.token, NS_HTML, ca
                                for t, i in afe
                                        if t is node
                                                afe[i] = new_node
@@ -926,7 +921,7 @@ parse_html = (txt, parse_error_cb = null) ->
                        # 15. Create an element for the token for which formatting element
                        # was created, in the HTML namespace, with furthest block as the
                        # intended parent.
-                       new_element = fe.shallow_clone() # FIXME intended parent thing
+                       new_element = token_to_element fe.token, NS_HTML, fb
                        # 16. Take all of the child nodes of furthest block and append them
                        # to the element created in the last step.
                        while fb.children.length
@@ -1086,11 +1081,9 @@ parse_html = (txt, parse_error_cb = null) ->
        # http://www.w3.org/TR/html5/syntax.html#create-an-element-for-the-token
        # aka create_an_element_for_token
        token_to_element = (t, namespace, intended_parent) ->
-               t.type = TYPE_TAG # not TYPE_START_TAG
                # convert attributes into a hash
                attrs = {}
-               while t.attrs_a.length
-                       a = t.attrs_a.pop()
+               for a in t.attrs_a
                        attrs[a[0]] = a[1] # TODO check what to do with dupilcate attrs
                el = new Node TYPE_TAG, name: t.name, namespace: namespace, attrs: attrs, token: t
 
@@ -1118,27 +1111,6 @@ parse_html = (txt, parse_error_cb = null) ->
        # http://www.w3.org/TR/html5/syntax.html#insert-an-html-element
        insert_html_element = insert_foreign_element # (token, namespace) ->
 
-       # FIXME read implement "foster parenting" part
-       # FIXME read spec, do this right
-       # FIXME implement the override target thing
-       # note: this assumes it's an open tag
-       # FIXME what part of the spec is this?
-       # TODO look through all callers of this, and see what they should really be doing.
-       #   eg probably insert_html_element for tokens
-       tree_insert_element = (el, override_target = null, namespace = null) ->
-               if namespace?
-                       el.namespace = namespace
-               dest = adjusted_insertion_location override_target
-               if el.type is TYPE_START_TAG # means it's a "token"
-                       el = token_to_element el, namespace, dest[0]
-               unless el.namespace?
-                       namespace = dest.namespace
-               # fixfull: Document nodes sometimes can't accept more chidren
-               dest[0].children.splice dest[1], 0, el
-               el.parent = dest[0]
-               open_els.unshift el
-               return el
-
        # http://www.w3.org/TR/html5/syntax.html#insert-a-comment
        # position should be [node, index_within_children]
        insert_comment = (t, position = null) ->