JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
update aaa to WHATWG version
[peach-html5-editor.git] / parse-html.coffee
index 0fb97b9..f5437c9 100644 (file)
@@ -24,7 +24,7 @@
 #
 # Deviations from that spec:
 #
-#   Purposeful: search this file for "WTAG"
+#   Purposeful: search this file for "WHATWG"
 #
 #   Not finished yet: search this file for "fixfull", "TODO" and "FIXME"
 
@@ -342,7 +342,7 @@ special_elements = {
        img:NS_HTML, input:NS_HTML, isindex:NS_HTML, li:NS_HTML, link:NS_HTML,
        listing:NS_HTML, main:NS_HTML, marquee:NS_HTML,
 
-       menu:NS_HTML,menuitem:NS_HTML, # WATWG adds these
+       menu:NS_HTML,menuitem:NS_HTML, # WHATWG adds these
 
        meta:NS_HTML, nav:NS_HTML, noembed:NS_HTML, noframes:NS_HTML,
        noscript:NS_HTML, object:NS_HTML, ol:NS_HTML, p:NS_HTML, param:NS_HTML,
@@ -468,7 +468,7 @@ svg_attribute_fixes = {
        diffuseconstant: 'diffuseConstant'
        edgemode: 'edgeMode'
        externalresourcesrequired: 'externalResourcesRequired'
-       # WTAG removes this: filterres: 'filterRes'
+       # WHATWG removes this: filterres: 'filterRes'
        filterunits: 'filterUnits'
        glyphref: 'glyphRef'
        gradienttransform: 'gradientTransform'
@@ -896,16 +896,46 @@ parse_html = (args) ->
                debug_log "tree: #{serialize_els doc.children, false, true}"
                debug_log "open_els: #{serialize_els open_els, true, true}"
                debug_log "afe: #{serialize_els afe, true, true}"
+# this block implements tha W3C spec
+#              # 1. If the current node is an HTML element whose tag name is subject,
+#              # then run these substeps:
+#              #
+#              # 1. Let element be the current node.
+#              #
+#              # 2. Pop element off the stack of open elements.
+#              #
+#              # 3. If element is also in the list of active formatting elements,
+#              # remove the element from the list.
+#              #
+#              # 4. Abort the adoption agency algorithm.
+#              if open_els[0].name is subject and open_els[0].namespace is NS_HTML
+#                      el = open_els.shift()
+#                      # remove it from the list of active formatting elements (if found)
+#                      for t, i in afe
+#                              if t is el
+#                                      afe.splice i, 1
+#                                      break
+#                      debug_log "aaa: starting off with subject on top of stack, exiting"
+#                      return
+# WHATWG: https://html.spec.whatwg.org/multipage/syntax.html#adoption-agency-algorithm
+               # If the current node is an HTML element whose tag name is subject, and
+               # the current node is not in the list of active formatting elements,
+               # then pop the current node off the stack of open elements, and abort
+               # these steps.
                if open_els[0].name is subject and open_els[0].namespace is NS_HTML
-                       el = open_els[0]
-                       open_els.shift()
+                       debug_log "aaa: starting off with subject on top of stack, exiting"
                        # remove it from the list of active formatting elements (if found)
-                       for t, i in afe
-                               if t is el
-                                       afe.splice i, 1
+                       in_afe = false
+                       for el, i in afe
+                               if el is open_els[0]
+                                       in_afe = true
                                        break
-                       debug_log "aaa: starting off with subject on top of stack, exiting"
-                       return
+                       unless in_afe
+                               debug_log "aaa: ...and not in afe, aaa done"
+                               open_els.shift()
+                               return
+                       # fall through
+# END WHATWG
                outer = 0
                loop
                        if outer >= 8
@@ -2138,7 +2168,7 @@ parse_html = (args) ->
 #                                      parse_error()
 #                      insert_html_element t
 #                      return
-# below implements the WATWG spec https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inbody
+# below implements the WHATWG spec https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inbody
                if t.type is TYPE_START_TAG and (t.name is 'rb' or t.name is 'rtc')
                        if is_in_scope 'ruby', NS_HTML
                                generate_implied_end_tags()
@@ -2153,7 +2183,7 @@ parse_html = (args) ->
                                        parse_error()
                        insert_html_element t
                        return
-# end WATWG chunk
+# end WHATWG chunk
                if t.type is TYPE_START_TAG and t.name is 'math'
                        reconstruct_afe()
                        adjust_mathml_attributes t
@@ -2726,7 +2756,8 @@ parse_html = (args) ->
                        ins_mode_in_body t
                        return
                if t.type is TYPE_COMMENT
-                       insert_comment t, [open_els[0], open_els[0].children.length]
+                       first = open_els[open_els.length - 1]
+                       insert_comment t, [first, first.children.length]
                        return
                if t.type is TYPE_DOCTYPE
                        parse_error()
@@ -2805,7 +2836,7 @@ parse_html = (args) ->
                        ins_mode_in_body t
                        return
                if t.type is TYPE_END_TAG and t.name is 'html'
-                       insert_mode = ins_mode_after_after_frameset
+                       ins_mode = ins_mode_after_after_frameset
                        return
                if t.type is TYPE_START_TAG and t.name is 'noframes'
                        ins_mode_in_head t
@@ -3047,25 +3078,27 @@ parse_html = (args) ->
 
        # 8.2.4.9 http://www.w3.org/TR/html5/syntax.html#end-tag-open-state
        tok_state_end_tag_open = ->
-               switch c = txt.charAt(cur++)
-                       when '>'
-                               parse_error()
-                               tok_state = tok_state_data
-                       when '' # EOF
-                               parse_error()
-                               tok_state = tok_state_data
-                               return new_text_node '</'
-                       else
-                               if is_uc_alpha(c)
-                                       tok_cur_tag = new_end_tag c.toLowerCase()
-                                       tok_state = tok_state_tag_name
-                               else if is_lc_alpha(c)
-                                       tok_cur_tag = new_end_tag c
-                                       tok_state = tok_state_tag_name
-                               else
-                                       parse_error()
-                                       tok_cur_tag = new_comment_token '/'
-                                       tok_state = tok_state_bogus_comment
+               c = txt.charAt(cur++)
+               if is_uc_alpha(c)
+                       tok_cur_tag = new_end_tag c.toLowerCase()
+                       tok_state = tok_state_tag_name
+                       return
+               if is_lc_alpha(c)
+                       tok_cur_tag = new_end_tag c
+                       tok_state = tok_state_tag_name
+                       return
+               if c is '>'
+                       parse_error()
+                       tok_state = tok_state_data
+                       return
+               if c is '' # EOF
+                       parse_error()
+                       tok_state = tok_state_data
+                       return new_text_node '</'
+               # Anything else
+               parse_error()
+               tok_cur_tag = new_comment_token c
+               tok_state = tok_state_bogus_comment
                return null
 
        # 8.2.4.10 http://www.w3.org/TR/html5/syntax.html#tag-name-state
@@ -4504,6 +4537,7 @@ parse_html = (args) ->
        head_element_pointer = null
        flag_fragment_parsing = false # parser originally created as part of the html fragment parsing algorithm (fragment case)
        context_element = null # FIXME initialize from args.fragment http://www.w3.org/TR/html5/syntax.html#parsing-html-fragments
+       prev_node_id = 0 # just for debugging
 
        # tokenizer initialization
        tok_state = tok_state_data
@@ -4514,7 +4548,7 @@ parse_html = (args) ->
        txt = txt.replace(new RegExp("\r\n", 'g'), "\n") # fixfull spec doesn't say this
        txt = txt.replace(new RegExp("\r", 'g'), "\n") # fixfull spec doesn't say this
 
-       if args.name is "tests16.dat #25"
+       if args.name is "tests18.dat #17"
                console.log "hi"
        # proccess input
        # http://www.w3.org/TR/html5/syntax.html#tree-construction