X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=parse-html.coffee;h=1ae7a24b526857e145ba8c8b6e780a18ca5a23d6;hb=a8059f4062151f2c79d6bdbaf7617e481d1879ab;hp=f0f5e9171da7f25fc089e7e24ca629d34e623727;hpb=dc04008f5d598663a2bf763f169cf30594a5c4b5;p=peach-html5-editor.git diff --git a/parse-html.coffee b/parse-html.coffee index f0f5e91..1ae7a24 100644 --- a/parse-html.coffee +++ b/parse-html.coffee @@ -38,9 +38,9 @@ # # "grows downwards" means it's visualized like this: (index: el, names) # -# 6: g "start of the list", "topmost" +# 6: g "start of the list", "topmost", "first" # 5: f -# 4: e "previous" (to d), "above" +# 4: e "previous" (to d), "above", "before" # 3: d (previous/next are relative to this element) # 2: c "next", "after", "lower", "below" # 1: b @@ -54,10 +54,10 @@ TYPE_TEXT = 1 # "text" TYPE_COMMENT = 2 TYPE_DOCTYPE = 3 # the following types are emited by the tokenizer, but shouldn't end up in the tree: -TYPE_OPEN_TAG = 4 # name, [attributes ([key,value]...) in reverse order], [children] +TYPE_START_TAG = 4 # name, [attributes ([key,value]...) in reverse order], [children] TYPE_END_TAG = 5 # name TYPE_EOF = 6 -TYPE_MARKER = 7 # http://www.w3.org/TR/html5/syntax.html#reconstruct-the-active-formatting-elements +TYPE_AFE_MARKER = 7 # http://www.w3.org/TR/html5/syntax.html#reconstruct-the-active-formatting-elements TYPE_AAA_BOOKMARK = 8 # http://www.w3.org/TR/html5/syntax.html#adoption-agency-algorithm # namespace constants @@ -81,7 +81,7 @@ class Node @name = args.name ? '' # tag name @text = args.text ? '' # contents for text/comment nodes @attrs = args.attrs ? {} - @attrs_a = args.attr_k ? [] # attrs in progress, TYPE_OPEN_TAG only + @attrs_a = args.attr_k ? [] # attrs in progress, TYPE_START_TAG only @children = args.children ? [] @namespace = args.namespace ? NS_HTML @parent = args.parent ? null @@ -94,6 +94,10 @@ class Node attrs = {} attrs[k] = v for k, v of @attrs return new Node @type, name: @name, text: @text, attrs: attrs, namespace: @namespace, id: @id + acknowledge_self_closing: -> + @flag 'did_self_close', true + flag: -> + # fixfull serialize: (shallow = false, show_ids = false) -> # for unit tests ret = '' switch @type @@ -105,8 +109,17 @@ class Node ret += "##{@id}," if shallow break - ret += JSON.stringify @attrs - ret += ',[' + attr_keys = [] + for k of @attrs + attr_keys.push k + attr_keys.sort() + ret += '{' + sep = '' + for k in attr_keys + ret += sep + sep = ',' + ret += "#{JSON.stringify k}:#{JSON.stringify @attrs[k]}" + ret += '},[' sep = '' for c in @children ret += sep @@ -122,7 +135,7 @@ class Node when TYPE_DOCTYPE ret += 'doctype' # FIXME - when TYPE_MARKER + when TYPE_AFE_MARKER ret += 'marker' when TYPE_AAA_BOOKMARK ret += 'aaa_bookmark' @@ -133,22 +146,25 @@ class Node # helpers: (only take args that are normally known when parser creates nodes) new_open_tag = (name) -> - return new Node TYPE_OPEN_TAG, name: name + return new Node TYPE_START_TAG, name: name new_end_tag = (name) -> return new Node TYPE_END_TAG, name: name new_element = (name) -> return new Node TYPE_TAG, name: name new_text_node = (txt) -> return new Node TYPE_TEXT, text: txt +new_character_token = new_text_node new_comment_node = (txt) -> return new Node TYPE_COMMENT, text: txt new_eof_token = -> return new Node TYPE_EOF +new_afe_marker = -> + return new Node TYPE_AFE_MARKER new_aaa_bookmark = -> return new Node TYPE_AAA_BOOKMARK -lc_alpha = "abcdefghijklmnopqrstuvwxqz" -uc_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXQZ" +lc_alpha = "abcdefghijklmnopqrstuvwxyz" +uc_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" digits = "0123456789" alnum = lc_alpha + uc_alpha + digits hex_chars = digits + "abcdefABCDEF" @@ -276,6 +292,14 @@ formatting_elements = { u: true } +foster_parenting_targets = { + table: true + tbody: true + tfoot: true + thead: true + tr: true +} + # all html I presume end_tag_implied = { dd: true @@ -291,9 +315,7 @@ end_tag_implied = { } el_is_special = (e) -> - return special_elements[e.name]? - # FIXME it should really be: - #return special_elements[e.name] is e.namespace + return special_elements[e.name] is e.namespace # decode_named_char_ref() # @@ -321,14 +343,20 @@ parse_html = (txt, parse_error_cb = null) -> cur = 0 # index of next char in txt to be parsed # declare tree and tokenizer variables so they're in scope below tree = null - open_els = [] # stack of open elements - tree_state = null + open_els = null # stack of open elements + afe = null # active formatting elements + template_insertion_modes = null + insertion_mode = null + original_insertion_mode = null tok_state = null tok_cur_tag = null # partially parsed tag + flag_scripting = null flag_frameset_ok = null flag_parsing = null flag_foster_parenting = null - afe = [] # active formatting elements + form_element_pointer = null + temporary_buffer = null + pending_table_character_tokens = null parse_error = -> if parse_error_cb? @@ -336,6 +364,21 @@ parse_html = (txt, parse_error_cb = null) -> else console.log "Parse error at character #{cur} of #{txt.length}" + afe_push = (new_el) -> + matches = 0 + for el, i in afe + if el.name is new_el.name and el.namespace is new_el.namespace + for k, v of el.attrs + continue unless new_el.attrs[k] is v + for k, v of new_el.attrs + continue unless el.attrs[k] is v + matches += 1 + if matches is 3 + afe.splice i, 1 + break + afe.unshift new_el + afe_push_marker = -> + afe.unshift new_afe_marker() # the functions below impliment the Tree Contstruction algorithm # http://www.w3.org/TR/html5/syntax.html#tree-construction @@ -397,12 +440,135 @@ parse_html = (txt, parse_error_cb = null) -> return false return false + # 8.2.3.1 ... + # http://www.w3.org/TR/html5/syntax.html#reset-the-insertion-mode-appropriately + reset_insertion_mode = -> + # 1. Let last be false. + last = false + # 2. Let node be the last node in the stack of open elements. + node_i = 0 + node = open_els[node_i] + # 3. Loop: If node is the first node in the stack of open elements, + # then set last to true, and, if the parser was originally created as + # part of the HTML fragment parsing algorithm (fragment case) set node + # to the context element. + loop + if node_i is open_els.length - 1 + last = true + # fixfull (fragment case) + + # 4. If node is a select element, run these substeps: + if node.name is 'select' + # 1. If last is true, jump to the step below labeled done. + unless last + # 2. Let ancestor be node. + ancestor_i = node_i + ancestor = node + # 3. Loop: If ancestor is the first node in the stack of + # open elements, jump to the step below labeled done. + loop + if ancestor_i is open_els.length - 1 + break + # 4. Let ancestor be the node before ancestor in the stack + # of open elements. + ancestor_i += 1 + ancestor = open_els[ancestor_i] + # 5. If ancestor is a template node, jump to the step below + # labeled done. + if ancestor.name is 'template' + break + # 6. If ancestor is a table node, switch the insertion mode + # to "in select in table" and abort these steps. + if ancestor.name is 'table' + insertion_mode = ins_mode_in_select_in_table + return + # 7. Jump back to the step labeled loop. + # 8. Done: Switch the insertion mode to "in select" and abort + # these steps. + insertion_mode = ins_mode_in_select + return + # 5. If node is a td or th element and last is false, then switch + # the insertion mode to "in cell" and abort these steps. + if (node.name is 'td' or node.name is 'th') and last is false + insertion_mode = ins_mode_in_cell + return + # 6. If node is a tr element, then switch the insertion mode to "in + # row" and abort these steps. + if node.name is 'tr' + insertion_mode = ins_mode_in_row + return + # 7. If node is a tbody, thead, or tfoot element, then switch the + # insertion mode to "in table body" and abort these steps. + if node.name is 'tbody' or node.name is 'thead' or node.name is 'tfoot' + insertion_mode = ins_mode_in_table_body + return + # 8. If node is a caption element, then switch the insertion mode + # to "in caption" and abort these steps. + if node.name is 'caption' + insertion_mode = ins_mode_in_caption + return + # 9. If node is a colgroup element, then switch the insertion mode + # to "in column group" and abort these steps. + if node.name is 'colgroup' + insertion_mode = ins_mode_in_column_group + return + # 10. If node is a table element, then switch the insertion mode to + # "in table" and abort these steps. + if node.name is 'table' + insertion_mode = ins_mode_in_table + return + # 11. If node is a template element, then switch the insertion mode + # to the current template insertion mode and abort these steps. + # fixfull (template insertion mode stack) + + # 12. If node is a head element and last is true, then switch the + # insertion mode to "in body" ("in body"! not "in head"!) and abort + # these steps. (fragment case) + if node.name is 'head' and last + insertion_mode = ins_mode_in_body + return + # 13. If node is a head element and last is false, then switch the + # insertion mode to "in head" and abort these steps. + if node.name is 'head' and last is false + insertion_mode = ins_mode_in_head + return + # 14. If node is a body element, then switch the insertion mode to + # "in body" and abort these steps. + if node.name is 'body' + insertion_mode = ins_mode_in_body + return + # 15. If node is a frameset element, then switch the insertion mode + # to "in frameset" and abort these steps. (fragment case) + if node.name is 'frameset' + insertion_mode = ins_mode_in_frameset + return + # 16. If node is an html element, run these substeps: + 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 + return + # 17. If last is true, then switch the insertion mode to "in body" + # and abort these steps. (fragment case) + if last + insertion_mode = ins_mode_in_body + return + # 18. Let node now be the node before node in the stack of open + # elements. + node_i += 1 + node = open_els[node_i] + # 19. Return to the step labeled loop. + # http://www.w3.org/TR/html5/syntax.html#reconstruct-the-active-formatting-elements # this implementation is structured (mostly) as described at the link above. # capitalized comments are the "labels" described at the link above. reconstruct_active_formatting_elements = -> return if afe.length is 0 - if afe[0].type is TYPE_MARKER or afe[0] in open_els + if afe[0].type is TYPE_AFE_MARKER or afe[0] in open_els return # Rewind i = 0 @@ -410,7 +576,7 @@ parse_html = (txt, parse_error_cb = null) -> if i is afe.length - 1 break i += 1 - if afe[i].type is TYPE_MARKER or afe[i] in open_els + if afe[i].type is TYPE_AFE_MARKER or afe[i] in open_els i -= 1 # Advance break # Create @@ -419,7 +585,7 @@ parse_html = (txt, parse_error_cb = null) -> tree_insert_element el afe[i] = el break if i is 0 - i -= 1 + i -= 1 # Advance # http://www.w3.org/TR/html5/syntax.html#adoption-agency-algorithm # adoption agency algorithm @@ -428,6 +594,10 @@ parse_html = (txt, parse_error_cb = null) -> # http://www.w3.org/TR/html5/syntax.html#misnested-tags:-b-p-/b-/p # http://www.w3.org/TR/html5/syntax.html#unclosed-formatting-elements adoption_agency = (subject) -> + debug_log "adoption_agency()" + debug_log "tree: #{serialize_els tree.children, false, true}" + debug_log "open_els: #{serialize_els open_els, true, true}" + debug_log "afe: #{serialize_els afe, true, true}" if open_els[0].name is subject el = open_els[0] open_els.shift() @@ -436,6 +606,7 @@ parse_html = (txt, parse_error_cb = null) -> if t is el afe.splice i, 1 break + debug_log "aaa: starting off with subject on top of stack, exiting" return outer = 0 loop @@ -448,7 +619,7 @@ parse_html = (txt, parse_error_cb = null) -> # the list otherwise, and has the tag name subject. fe = null for t, fe_of_afe in afe - if t.type is TYPE_MARKER + if t.type is TYPE_AFE_MARKER break if t.name is subject fe = t @@ -456,6 +627,7 @@ parse_html = (txt, parse_error_cb = null) -> # If there is no such element, then abort these steps and instead # act as described in the "any other end tag" entry above. if fe is null + debug_log "aaa: fe not found in afe" in_body_any_other_end_tag subject return # 6. If formatting element is not in the stack of open elements, @@ -467,6 +639,7 @@ parse_html = (txt, parse_error_cb = null) -> in_open_els = true break unless in_open_els + debug_log "aaa: fe not found in open_els" parse_error() # "remove it from the list" must mean afe, since it's not in open_els afe.splice fe_of_afe, 1 @@ -475,6 +648,7 @@ parse_html = (txt, parse_error_cb = null) -> # the element is not in scope, then this is a parse error; abort # these steps. unless el_is_in_scope fe + debug_log "aaa: fe not in scope" parse_error() return # 8. If formatting element is not the current node, this is a parse @@ -500,6 +674,7 @@ parse_html = (txt, parse_error_cb = null) -> # formatting element from the list of active formatting elements, # and finally abort these steps. if fb is null + debug_log "aaa: no fb" loop t = open_els.shift() if t is fe @@ -532,8 +707,8 @@ parse_html = (txt, parse_error_cb = null) -> break node = node_next ? node_above debug_log "inner loop #{inner}" - debug_log "open_els: #{serialize_els open_els, true, true}" debug_log "tree: #{serialize_els tree.children, false, true}" + debug_log "open_els: #{serialize_els open_els, true, true}" debug_log "afe: #{serialize_els afe, true, true}" debug_log "ca: #{ca.name}##{ca.id} children: #{serialize_els ca.children, true, true}" debug_log "fe: #{fe.name}##{fe.id} children: #{serialize_els fe.children, true, true}" @@ -705,22 +880,23 @@ parse_html = (txt, parse_error_cb = null) -> debug_log "AAA DONE" # http://www.w3.org/TR/html5/syntax.html#close-a-p-element - # FIXME test this (particularly emplied end tags) close_p_element = -> generate_implied_end_tags 'p' # arg is exception if open_els[0].name isnt 'p' parse_error() while open_els.length > 1 # just in case - t = open_els.shift() - if t.name is 'p' + el = open_els.shift() + if el.name is 'p' return close_p_if_in_button_scope = -> if is_in_button_scope 'p' - close_a_p_element() + close_p_element() # http://www.w3.org/TR/html5/syntax.html#insert-a-character - tree_insert_text = (t) -> + # aka insert_a_character = (t) -> + insert_character = (t) -> dest = adjusted_insertion_location() + # fixfull check for Document node if dest[1] > 0 prev = dest[0].children[dest[1] - 1] if prev.type is TYPE_TEXT @@ -744,38 +920,69 @@ parse_html = (txt, parse_error_cb = null) -> # If foster parenting is enabled and target is a table, tbody, tfoot, # thead, or tr element Foster parenting happens when content is # misnested in tables. - if flag_foster_parenting and target.name in foster_parenting_targets - console.log "foster parenting isn't implemented yet" # TODO - # 1. Let last template be the last template element in the stack of - # open elements, if any. - # 2. Let last table be the last table element in the stack of open - # elements, if any. - - # 3. If there is a last template and either there is no last table, - # or there is one, but last template is lower (more recently added) - # than last table in the stack of open elements, then: let adjusted - # insertion location be inside last template's template contents, - # after its last child (if any), and abort these substeps. - - # 4. If there is no last table, then let adjusted insertion - # location be inside the first element in the stack of open - # elements (the html element), after its last child (if any), and - # abort these substeps. (fragment case) - - # 5. If last table has a parent element, then let adjusted - # insertion location be inside last table's parent element, - # immediately before last table, and abort these substeps. - - # 6. Let previous element be the element immediately above last - # table in the stack of open elements. - - # 7. Let adjusted insertion location be inside previous element, - # after its last child (if any). - - # Note: These steps are involved in part because it's possible for - # elements, the table element in this case in particular, to have - # been moved by a script around in the DOM, or indeed removed from - # the DOM entirely, after the element was inserted by the parser. + if flag_foster_parenting and foster_parenting_targets[target.name] + loop # once. this is here so we can ``break`` to "abort these substeps" + # 1. Let last template be the last template element in the + # stack of open elements, if any. + last_template = null + last_template_i = null + for el, i in open_els + if el.name is 'template' + last_template = el + last_template_i = i + break + # 2. Let last table be the last table element in the stack of + # open elements, if any. + last_table = null + last_table_i + for el, i in open_els + if el.name is 'table' + last_table = el + last_table_i = i + break + # 3. If there is a last template and either there is no last + # table, or there is one, but last template is lower (more + # recently added) than last table in the stack of open + # elements, then: let adjusted insertion location be inside + # last template's template contents, after its last child (if + # any), and abort these substeps. + if last_template and (last_table is null or last_template_i < last_table_i) + target = template # fixfull should be it's contents + target_i = target.children.length + break + # 4. If there is no last table, then let adjusted insertion + # location be inside the first element in the stack of open + # elements (the html element), after its last child (if any), + # and abort these substeps. (fragment case) + if last_table is null + # this is odd + target = open_els[open_els.length - 1] + target_i = target.children.length + # 5. If last table has a parent element, then let adjusted + # insertion location be inside last table's parent element, + # immediately before last table, and abort these substeps. + if last_table.parent? + for c, i in last_table.parent.children + if c is last_table + target = last_table.parent + target_i = i + break + break + # 6. Let previous element be the element immediately above last + # table in the stack of open elements. + # + # huh? how could it not have a parent? + previous_element = open_els[last_table_i + 1] + # 7. Let adjusted insertion location be inside previous + # element, after its last child (if any). + target = previous_element + target_i = target.children.length + # Note: These steps are involved in part because it's possible + # for elements, the table element in this case in particular, + # to have been moved by a script around in the DOM, or indeed + # removed from the DOM entirely, after the element was inserted + # by the parser. + break # don't really loop else # Otherwise Let adjusted insertion location be inside target, after # its last child (if any). @@ -783,7 +990,8 @@ parse_html = (txt, parse_error_cb = null) -> # 3. If the adjusted insertion location is inside a template element, # let it instead be inside the template element's template contents, - # after its last child (if any). TODO + # after its last child (if any). + # fixfull (template) # 4. Return the adjusted insertion location. return [target, target_i] @@ -791,7 +999,7 @@ 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_OPEN_TAG + t.type = TYPE_TAG # not TYPE_START_TAG # convert attributes into a hash attrs = {} while t.attrs_a.length @@ -834,7 +1042,7 @@ parse_html = (txt, parse_error_cb = null) -> if namespace? el.namespace = namespace dest = adjusted_insertion_location override_target - if el.type is TYPE_OPEN_TAG # means it's a "token" + 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 @@ -845,17 +1053,115 @@ parse_html = (txt, parse_error_cb = null) -> return el # http://www.w3.org/TR/html5/syntax.html#insert-a-comment - tree_insert_a_comment = (t) -> - # FIXME read spec for "adjusted insertion location, etc, this might be wrong - open_els[0].children.push t + # position should be [node, index_within_children] + insert_comment = (t, position = null) -> + position ?= adjusted_insertion_location() + position[0].children.splice position[1], 0, t + + # 8.2.5.2 + # http://www.w3.org/TR/html5/syntax.html#generic-raw-text-element-parsing-algorithm + parse_generic_raw_text = (t) -> + insert_html_element t + tok_state = tok_state_rawtext + original_insertion_mode = insertion_mode + insertion_mode = ins_mode_text + parse_generic_rcdata_text = (t) -> + insert_html_element t + tok_state = tok_state_rcdata + original_insertion_mode = insertion_mode + insertion_mode = ins_mode_text # 8.2.5.3 http://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags # http://www.w3.org/TR/html5/syntax.html#generate-implied-end-tags generate_implied_end_tags = (except = null) -> - while end_tag_implied[open_els[0]] and open_els[0].name isnt except + while end_tag_implied[open_els[0].name] and open_els[0].name isnt except + open_els.shift() + + # 8.2.5.4.4 http://www.w3.org/TR/html5/syntax.html#parsing-main-inhead + ins_mode_in_head_else = (t) -> # factored out for same-as-spec flow control + open_els.shift() # spec says this will be a 'head' node + insertion_mode = ins_mode_after_head + insertion_mode t + ins_mode_in_head = (t) -> + if t.type is TYPE_TEXT and (t.text is "\t" or t.text is "\n" or t.text is "\u000c" or t.text is ' ') + insert_character t + return + if t.type is TYPE_COMMENT + insert_comment t + return + if t.type is TYPE_DOCTYPE + parse_error() + return + if t.type is TYPE_START_TAG and t.name is 'html' + ins_mode_in_body t + return + if t.type is TYPE_START_TAG and (t.name is 'base' or t.name is 'basefont' or t.name is 'bgsound' or t.name is 'link') + el = insert_html_element t + open_els.shift() + el.acknowledge_self_closing() + return + if t.type is TYPE_START_TAG and t.name is 'meta' + el = insert_html_element t open_els.shift() + el.acknowledge_self_closing() + # fixfull encoding stuff + return + if t.type is TYPE_START_TAG and t.name is 'title' + parse_generic_rcdata_element t + return + if t.type is TYPE_START_TAG and ((t.name is 'noscript' and flag_scripting) or (t.name is 'noframes' or t.name is 'style')) + parse_generic_raw_text t + return + if t.type is TYPE_START_TAG and t.name is 'noscript' and flag_scripting is false + insert_html_element t + insertion_mode = in_head_noscript # FIXME implement + return + if t.type is TYPE_START_TAG and t.name is 'script' + ail = adjusted_insertion_location() + el = token_to_element t, NS_HTML, ail + el.flag_parser_inserted true # FIXME implement + # fixfull frament case + ail[0].children.splice ail[1], 0, el + open_els.unshift el + tok_state = tok_state_script_data + original_insertion_mode = insertion_mode # make sure orig... is defined + insertion_mode = ins_mode_text # FIXME implement + return + if t.type is TYPE_END_TAG and t.name is 'head' + open_els.shift() # will be a head element... spec says so + insertion_mode = ins_mode_after_head + return + if t.type is TYPE_END_TAG and (t.name is 'body' or t.name is 'html' or t.name is 'br') + ins_mode_in_head_else t + return + if t.type is TYPE_START_TAG and t.name is 'template' + insert_html_element t + afe_push_marker() + flag_frameset_ok = false + insertion_mode = ins_mode_in_template + template_insertion_modes.unshift ins_mode_in_template # FIXME implement + return + if t.type is TYPE_END_TAG and t.name is 'template' + if template_tag_is_open() + generate_implied_end_tags + if open_els[0].name isnt 'template' + parse_error() + loop + el = open_els.shift() + if el.name is 'template' + break + clear_afe_to_marker() + template_insertion_modes.shift() + reset_insertion_mode() + else + parse_error() + return + if (t.type is TYPE_OPEN_TAG and t.name is 'head') or t.type is TYPE_END_TAG + parse_error() + return + ins_mode_in_head_else t - # 8.2.5.4 http://www.w3.org/TR/html5/syntax.html#parsing-main-inbody + # 8.2.5.4.7 http://www.w3.org/TR/html5/syntax.html#parsing-main-inbody in_body_any_other_end_tag = (name) -> # factored out because adoption agency calls it for node, i in open_els if node.name is name # FIXME check namespace too @@ -868,7 +1174,7 @@ parse_html = (txt, parse_error_cb = null) -> if special_elements[node.name]? # FIXME check namespac too parse_error() return - tree_in_body = (t) -> + ins_mode_in_body = (t) -> switch t.type when TYPE_TEXT switch t.text @@ -876,16 +1182,16 @@ parse_html = (txt, parse_error_cb = null) -> parse_error() when "\t", "\u000a", "\u000c", "\u000d", ' ' reconstruct_active_formatting_elements() - tree_insert_text t + insert_character t else reconstruct_active_formatting_elements() - tree_insert_text t + insert_character t flag_frameset_ok = false when TYPE_COMMENT - tree_insert_a_comment t + insert_comment t when TYPE_DOCTYPE parse_error() - when TYPE_OPEN_TAG + when TYPE_START_TAG switch t.name when 'html' parse_error() @@ -895,7 +1201,7 @@ parse_html = (txt, parse_error_cb = null) -> root_attrs[k] = v unless root_attrs[k]? when 'base', 'basefont', 'bgsound', 'link', 'meta', 'noframes', 'script', 'style', 'template', 'title' # FIXME also do this for (end tag) - return tree_in_head t + return ins_mode_in_head t when 'body' parse_error() # TODO @@ -925,7 +1231,7 @@ parse_html = (txt, parse_error_cb = null) -> # is not in table scope). found = false for el in afe - if el.type is TYPE_MARKER + if el.type is TYPE_AFE_MARKER break if el.name is 'a' found = el @@ -939,16 +1245,21 @@ parse_html = (txt, parse_error_cb = null) -> if el is found open_els.splice i, 1 reconstruct_active_formatting_elements() - el = tree_insert_element t - afe.unshift el + el = insert_html_element t + afe_push el when 'b', 'big', 'code', 'em', 'font', 'i', 's', 'small', 'strike', 'strong', 'tt', 'u' reconstruct_active_formatting_elements() - el = tree_insert_element t - afe.unshift el + el = insert_html_element t + afe_push el + when 'table' + # fixfull quirksmode thing + close_p_if_in_button_scope() + insert_html_element t + insertion_mode = ins_mode_in_table # TODO lots more to implement here else # any other start tag reconstruct_active_formatting_elements() - tree_insert_element t + insert_html_element t when TYPE_EOF ok_tags = { dd: true, dt: true, li: true, p: true, tbody: true, td: true, @@ -988,7 +1299,7 @@ parse_html = (txt, parse_error_cb = null) -> unless is_in_button_scope 'p' parse_error() insert_html_element new_open_tag 'p' - close_p_element() + close_p_element() # TODO lots more close tags to implement here when 'a', 'b', 'big', 'code', 'em', 'font', 'i', 'nobr', 's', 'small', 'strike', 'strong', 'tt', 'u' adoption_agency t.name @@ -997,15 +1308,427 @@ parse_html = (txt, parse_error_cb = null) -> in_body_any_other_end_tag t.name return + ins_mode_in_table_else = (t) -> + parse_error() + flag_foster_parenting = true # FIXME + ins_mode_in_body t + flag_foster_parenting = false + can_in_table = { + 'table': true + 'tbody': true + 'tfoot': true + 'thead': true + 'tr': true + } + clear_to_table_stopers = { + 'table': true + 'template': true + 'html': true + } + clear_stack_to_table_context = -> + loop + if clear_to_table_stopers[open_els[0].name]? + break + open_els.shift() + return + clear_to_table_body_stopers = { + 'tbody': true + 'tfoot': true + 'thead': true + 'template': true + 'html': true + } + clear_stack_to_table_body_context = -> + loop + if clear_to_table_body_stopers[open_els[0].name]? + break + open_els.shift() + return + clear_to_table_row_stopers = { + 'tr': true + 'template': true + 'html': true + } + clear_stack_to_table_row_context = -> + loop + if clear_to_table_row_stopers[open_els[0].name]? + break + open_els.shift() + return + clear_afe_to_marker = -> + loop + el = afe.shift() + if el.type is TYPE_AFE_MARKER + return + + # 8.2.5.4.8 http://www.w3.org/TR/html5/syntax.html#parsing-main-incdata + ins_mode_text = (t) -> + if t.type is TYPE_TEXT + insert_character t + return + if t.type is TYPE_EOF + parse_error() + if open_els[0].name is 'script' + open_els[0].flag 'already started', true + open_els.shift() + insertion_mode = original_insertion_mode + insertion_mode t + return + if t.type is TYPE_END_TAG and t.name is 'script' + open_els.shift() + insertion_mode = original_insertion_mode + # fixfull the spec seems to assume that I'm going to run the script + # http://www.w3.org/TR/html5/syntax.html#scriptEndTag + return + if t.type is TYPE_END_TAG + open_els.shift() + insertion_mode = original_insertion_mode + return + console.log 'warning: end of ins_mode_text reached' # the functions below implement the tokenizer stats described here: # http://www.w3.org/TR/html5/syntax.html#tokenization + # 8.2.5.4.9 http://www.w3.org/TR/html5/syntax.html#parsing-main-intable + ins_mode_in_table = (t) -> + switch t.type + when TYPE_TEXT + if can_in_table[t.name] + original_insertion_mode = insertion_mode + insertion_mode = ins_mode_in_table_text + insertion_mode t + else + ins_mode_in_table_else t + when TYPE_COMMENT + insert_comment t + when TYPE_DOCTYPE + parse_error() + when TYPE_START_TAG + switch t.name + when 'caption' + clear_stack_to_table_context() + afe_push_marker() + insert_html_element t + insertion_mode = ins_mode_in_caption + when 'colgroup' + clear_stack_to_table_context() + insert_html_element t + insertion_mode = ins_mode_in_column_group + when 'col' + clear_stack_to_table_context() + insert_html_element new_open_tag 'colgroup' + insertion_mode = ins_mode_in_column_group + insertion_mode t + when 'tbody', 'tfoot', 'thead' + clear_stack_to_table_context() + insert_html_element t + insertion_mode = ins_mode_in_table_body + when 'td', 'th', 'tr' + clear_stack_to_table_context() + insert_html_element new_open_tag 'tbody' + insertion_mode = ins_mode_in_table_body + insertion_mode t + when 'table' + parse_error() + if is_in_table_scope 'table' + loop + el = open_els.shift() + if el.name is 'table' + break + reset_insertion_mode() + insertion_mode t + when 'style', 'script', 'template' + ins_mode_in_head t + when 'input' + if token_is_input_hidden t + ins_mode_in_table_else t + else + parse_error() + el = insert_html_element t + open_els.shift() + el.acknowledge_self_closing() + when 'form' + parse_error() + if form_element_pointer? + return + if template_tag_is_open() + return + form_element_pointer = insert_html_element t + open_els.shift() + else + ins_mode_in_table_else t + when TYPE_END_TAG + switch t.name + when 'table' + if is_in_table_scope 'table' + loop + el = open_els.shift() + if el.name is 'table' + break + reset_insertion_mode() + else + parse_error + when 'body', 'caption', 'col', 'colgroup', 'html', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr' + parse_error() + when 'template' + ins_mode_in_head t + else + ins_mode_in_table_else t + when TYPE_EOF + ins_mode_in_body t + else + ins_mode_in_table_else t + + + # 8.2.5.4.10 http://www.w3.org/TR/html5/syntax.html#parsing-main-intabletext + ins_mode_in_table_text = (t) -> + if t.type is TYPE_TEXT and t.text is "\u0000" + # huh? I thought the tokenizer didn't emit these + parse_error() + return + if t.type is TYPE_TEXT + pending_table_character_tokens.push t + return + # Anything else + all_space = true + for old in pending_table_character_tokens + unless space_chars.indexOf(old.text) > -1 + all_space = false + break + if all_space + for old in pending_table_character_tokens + insert_character old + else + for old in pending_table_character_tokens + ins_mode_table_else old + pending_table_character_tokens = [] # FIXME test (spec doesn't say this) + insertion_mode = original_insertion_mode + insertion_mode t + + # 8.2.5.4.11 http://www.w3.org/TR/html5/syntax.html#parsing-main-incaption + ins_mode_in_caption = (t) -> + if t.type is TYPE_END_TAG and t.name is 'caption' + if is_in_table_scope 'caption' + generate_implied_end_tags() + if open_els[0].name isnt 'caption' + parse_error() + loop + el = open_els.shift() + if el.name is 'caption' + break + clear_afe_to_marker() + insertion_mode = in_table + else + parse_error() + # fragment case + return + if (t.type is TYPE_START_TAG and (t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'tbody' or t.name is 'td' or t.name is 'tfoot' or t.name is 'th' or t.name is 'thead' or t.name is 'tr')) or t.type is TYPE_END_TAG and t.name is 'table' + parse_error() + if is_in_table_scope 'caption' + loop + el = open_els.shift() + if el.name is 'caption' + break + clear_afe_to_marker() + insertion_mode = in_table + insertion_mode t + # else fragment case + return + if t.type is TYPE_END_TAG and (t.name is 'body' or t.name is 'col' or t.name is 'colgroup' or t.name is 'html' or t.name is 'tbody' or t.name is 'td' or t.name is 'tfoot' or t.name is 'th' or t.name is 'thead' or t.name is 'tr') + parse_error() + return + # Anything else + ins_mode_in_body t + + # 8.2.5.4.12 http://www.w3.org/TR/html5/syntax.html#parsing-main-incolgroup + ins_mode_in_column_group = (t) -> + if t.type is TYPE_TEXT and space_chars.indexOf(t.text) > -1 + insert_character t + return + if t.type is TYPE_COMMENT + insert_comment t + return + if t.type is TYPE_DOCTYPE + parse_error() + return + if t.type is TYPE_START_TAG and t.name is 'html' + ins_mode_in_body t + return + if t.type is TYPE_START_TAG and t.name is 'col' + el = insert_html_element t + open_els.shift() + el.acknowledge_self_closing() + return + if t.type is TYPE_END_TAG and t.name is 'colgroup' + if open_els[0].name is 'colgroup' + open_els[0].shift() + insertion_mode = ins_mode_in_table + else + parse_error() + return + if t.type is TYPE_END_TAG and t.name is 'col' + parse_error() + return + if (t.type is TYPE_START_TAG or t.type is TYPE_END_TAG) and t.name is 'template' + ins_mode_in_head t + return + if t.type is TYPE_EOF + ins_mode_in_body t + return + # Anything else + if open_els[0].name isnt 'colgroup' + parse_error() + return + open_els.shift() + insertion_mode = ins_mode_in_table + insertion_mode t + return + + # 8.2.5.4.13 http://www.w3.org/TR/html5/syntax.html#parsing-main-intbody + ins_mode_in_table_body = (t) -> + if t.type is TYPE_START_TAG and t.name is 'tr' + clear_stack_to_table_body_context() + insert_html_element t + insertion_mode = ins_mode_in_row + return + if t.type is TYPE_START_TAG and (t.name is 'th' or t.name is 'td') + parse_error() + clear_stack_to_table_body_context() + insert_html_element new_open_tag 'tr' + insertion_mode = ins_mode_in_row + insertion_mode t + return + if t.type is TYPE_END_TAG and (t.name is 'tbody' or t.name is 'tfoot' or t.name is 'thead') + unless is_in_table_scope t.name # fixfull check namespace + parse_error() + return + clear_stack_to_table_body_context() + open_els.shift() + insertion_mode = ins_mode_in_table + return + if (t.type is TYPE_START_TAG and (t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'tbody' or t.name is 'tfoot' or t.name is 'thead')) or (t.type is TYPE_END_TAG and t.name is 'table') + has = false + for el in open_els + if el.name is 'tbody' or el.name is 'tfoot' or el.name is 'thead' + has = true + break + if table_scopers[el.name] + break + if !has + parse_error() + return + clear_stack_to_table_body_context() + open_els.shift() + insertion_mode = ins_mode_in_table + insertion_mode t + return + if t.type is TYPE_END_TAG and (t.name is 'body' or t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'html' or t.name is 'td' or t.name is 'th' or t.name is 'tr') + parse_error() + return + # Anything else + ins_mode_in_table t + + # 8.2.5.4.14 http://www.w3.org/TR/html5/syntax.html#parsing-main-intr + ins_mode_in_row = (t) -> + if t.type is TYPE_START_TAG and (t.name is 'th' or t.name is 'td') + clear_stack_to_table_row_context() + insert_html_element t + insertion_mode = ins_mode_in_cell + afe_push_marker() + return + if t.type is TYPE_END_TAG and t.name is 'tr' + if is_in_table_scope 'tr' + clear_stack_to_table_row_context() + open_els.shift() + insertion_mode = ins_mode_in_table_body + else + parse_error() + return + if (t.type is TYPE_START_TAG and (t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'tbody' or t.name is 'tfoot' or t.name is 'thead' or t.name is 'tr')) or t.type is TYPE_END_TAG and t.name is 'table' + if is_in_table_scope 'tr' + clear_stack_to_table_row_context() + open_els.shift() + insertion_mode = ins_mode_in_table_body + insertion_mode t + else + parse_error() + return + if t.type is TYPE_END_TAG and (t.name is 'tbody' or t.name is 'tfoot' or t.name is 'thead') + if is_in_table_scope t.name # fixfull namespace + if is_in_table_scope 'tr' + clear_stack_to_table_row_context() + open_els.shift() + insertion_mode = ins_mode_in_table_body + insertion_mode t + else + parse_error() + return + if t.type is TYPE_END_TAG and (t.name is 'body' or t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'html' or t.name is 'td' or t.name is 'th') + parse_error() + return + # Anything else + ins_mode_in_table t + + # http://www.w3.org/TR/html5/syntax.html#close-the-cell + close_the_cell = -> + generate_implied_end_tags() + unless open_els[0].name is 'td' or open_els[0] is 'th' + parse_error() + loop + el = open_els.shift() + if el.name is 'td' or el.name is 'th' + break + clear_afe_to_marker() + insertion_mode = ins_mode_in_row + + # 8.2.5.4.15 http://www.w3.org/TR/html5/syntax.html#parsing-main-intd + ins_mode_in_cell = (t) -> + if t.type is TYPE_END_TAG and (t.name is 'td' or t.name is 'th') + if is_in_table_scope t.name + generate_implied_end_tags() + if open_els[0].name isnt t.name + parse_error + loop + el = open_els.shift() + if el.name is t.name + break + clear_afe_to_marker() + insertion_mode = ins_mode_in_row + else + parse_error() + return + if t.type is TYPE_START_TAG and (t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'tbody' or t.name is 'td' or t.name is 'tfoot' or t.name is 'th' or t.name is 'thead' or t.name is 'tr') + has = false + for el in open_els + if el.name is 'td' or el.name is 'th' + has = true + break + if table_scopers[el.name] + break + if !has + parse_error() + return + close_the_cell() + insertion_mode t + return + if t.type is TYPE_END_TAG and (t.name is 'body' or t.name is 'caption' or t.name is 'col' or t.name is 'colgroup' or t.name is 'html') + parse_error() + return + if t.type is TYPE_END_TAG and (t.name is 'table' or t.name is 'tbody' or t.name is 'tfoot' or t.name is 'thead' or t.name is 'tr') + if is_in_table_scope t.name # fixfull namespace + close_the_cell() + insertion_mode t + else + parse_error() + return + # Anything Else + ins_mode_in_body t + # 8.2.4.1 http://www.w3.org/TR/html5/syntax.html#data-state tok_state_data = -> switch c = txt.charAt(cur++) when '&' - return new_text_node tokenize_character_reference() + return new_text_node parse_character_reference() when '<' tok_state = tok_state_tag_open when "\u0000" @@ -1019,7 +1742,68 @@ parse_html = (txt, parse_error_cb = null) -> # 8.2.4.2 http://www.w3.org/TR/html5/syntax.html#character-reference-in-data-state # not needed: tok_state_character_reference_in_data = -> - # just call tok_state_character_reference_in_data() + # just call parse_character_reference() + + # 8.2.4.3 http://www.w3.org/TR/html5/syntax.html#rcdata-state + tok_state_rcdata = -> + switch c = txt.charAt(cur++) + when '&' + return new_text_node parse_character_reference() + when '<' + tok_state = tok_state_rcdata_less_than_sign + when "\u0000" + parse_error() + return new_character_token "\ufffd" + when '' # EOF + return new_eof_token() + else + return new_character_token c + return null + + # 8.2.4.4 http://www.w3.org/TR/html5/syntax.html#character-reference-in-rcdata-state + # not needed: tok_state_character_reference_in_rcdata = -> + # just call parse_character_reference() + + # 8.2.4.5 http://www.w3.org/TR/html5/syntax.html#rawtext-state + tok_state_rawtext = -> + switch c = txt.charAt(cur++) + when '<' + tok_state = tok_state_rawtext_less_than_sign + when "\u0000" + parse_error() + return new_character_token "\ufffd" + when '' # EOF + return new_eof_token() + else + return new_character_token c + return null + + # 8.2.4.6 http://www.w3.org/TR/html5/syntax.html#script-data-state + tok_state_script_data = -> + switch c = txt.charAt(cur++) + when '<' + tok_state = tok_state_script_data_less_than_sign + when "\u0000" + parse_error() + return new_character_token "\ufffd" + when '' # EOF + return new_eof_token() + else + return new_character_token c + return null + + # 8.2.4.7 http://www.w3.org/TR/html5/syntax.html#plaintext-state + tok_state_plaintext = -> + switch c = txt.charAt(cur++) + when "\u0000" + parse_error() + return new_character_token "\ufffd" + when '' # EOF + return new_eof_token() + else + return new_character_token c + return null + # 8.2.4.8 http://www.w3.org/TR/html5/syntax.html#tag-open-state tok_state_tag_open = -> @@ -1092,6 +1876,140 @@ parse_html = (txt, parse_error_cb = null) -> tok_cur_tag.name += c return null + # 8.2.4.11 http://www.w3.org/TR/html5/syntax.html#rcdata-less-than-sign-state + tok_state_rcdata_less_than_sign = -> + c = txt.charAt(cur++) + if c is '/' + temporary_buffer = '' + tok_state = tok_state_rcdata_end_tag_open + return null + # Anything else + tok_state = tok_state_rcdata + cur -= 1 # reconsume the input character + return new_character_token '<' + + # 8.2.4.12 http://www.w3.org/TR/html5/syntax.html#rcdata-end-tag-open-state + tok_state_rcdata_end_tag_open = -> + c = txt.charAt(cur++) + if uc_alpha.indexOf(c) > -1 + tok_cur_tag = new_end_tag c.toLowerCase() + temporary_buffer += c + tok_state = tok_state_rcdata_end_tag_name + return null + if lc_alpha.indexOf(c) > -1 + tok_cur_tag = new_end_tag c + temporary_buffer += c + tok_state = tok_state_rcdata_end_tag_name + return null + # Anything else + tok_state = tok_state_rcdata + cur -= 1 # reconsume the input character + return new_character_token " + # spec says to check against "the tag name of the last start tag to + # have been emitted from this tokenizer", but this is only called from + # the various "raw" states, which I'm pretty sure all push the start + # token onto open_els. TODO: verify this after the script data states + # are implemented + debug_log "#{t.type}, #{t.name} open_els: #{serialize_els open_els, true, true}" + return t.type is TYPE_END_TAG and t.name is open_els[0].name + + # 8.2.4.13 http://www.w3.org/TR/html5/syntax.html#rcdata-end-tag-name-state + tok_state_rcdata_end_tag_name = -> + c = txt.charAt(cur++) + if c is "\t" or c is "\n" or c is "\u000c" or c is ' ' + if is_appropriate_end_tag tok_cur_tag + tok_state = tok_state_before_attribute_name + return + # else fall through to "Anything else" + if c is '/' + if is_appropriate_end_tag tok_cur_tag + tok_state = tok_state_self_closing_start_tag # FIXME spec typo? + return + # else fall through to "Anything else" + if c is '>' + if is_appropriate_end_tag tok_cur_tag + tok_state = tok_state_data + return tok_cur_tag + # else fall through to "Anything else" + if uc_alpha.indexOf(c) > -1 + tok_cur_tag.name += c.toLowerCase() + temporary_buffer += c + return null + if lc_alpha.indexOf(c) > -1 + tok_cur_tag.name += c + temporary_buffer += c + return null + # Anything else + tok_state = tok_state_rcdata + cur -= 1 # reconsume the input character + return new_character_token ' + c = txt.charAt(cur++) + if c is '/' + temporary_buffer = '' + tok_state = tok_state_rawtext_end_tag_open + return null + # Anything else + tok_state = tok_state_rawtext + cur -= 1 # reconsume the input character + return new_character_token '<' + + # 8.2.4.15 http://www.w3.org/TR/html5/syntax.html#rawtext-end-tag-open-state + tok_state_rawtext_end_tag_open = -> + c = txt.charAt(cur++) + if uc_alpha.indexOf(c) > -1 + tok_cur_tag = new_end_tag c.toLowerCase() + temporary_buffer += c + tok_state = tok_state_rawtext_end_tag_name + return null + if lc_alpha.indexOf(c) > -1 + tok_cur_tag = new_end_tag c + temporary_buffer += c + tok_state = tok_state_rawtext_end_tag_name + return null + # Anything else + tok_state = tok_state_rawtext + cur -= 1 # reconsume the input character + return new_character_token " + c = txt.charAt(cur++) + if c is "\t" or c is "\n" or c is "\u000c" or c is ' ' + if is_appropriate_end_tag tok_cur_tag + tok_state = tok_state_before_attribute_name + return + # else fall through to "Anything else" + if c is '/' + if is_appropriate_end_tag tok_cur_tag + tok_state = tok_state_self_closing_start_tag + return + # else fall through to "Anything else" + if c is '>' + if is_appropriate_end_tag tok_cur_tag + tok_state = tok_state_data + return tok_cur_tag + # else fall through to "Anything else" + if uc_alpha.indexOf(c) > -1 + tok_cur_tag.name += c.toLowerCase() + temporary_buffer += c + return null + if lc_alpha.indexOf(c) > -1 + tok_cur_tag.name += c + temporary_buffer += c + return null + # Anything else + tok_state = tok_state_rawtext + cur -= 1 # reconsume the input character + return new_character_token ' attr_name = null @@ -1155,6 +2073,41 @@ parse_html = (txt, parse_error_cb = null) -> tok_cur_tag.attrs_a[0][0] += c return null + # 8.2.4.36 http://www.w3.org/TR/html5/syntax.html#after-attribute-name-state + tok_state_after_attribute_name = -> + c = txt.charAt(cur++) + if c is "\t" or c is "\n" or c is "\u000c" or c is ' ' + return + if c is '/' + tok_state = tok_state_self_closing_start_tag + return + if c is '=' + tok_state = tok_state_before_attribute_value + return + if c is '>' + tok_state = tok_state_data + return + if uc_alpha.indexOf(c) > -1 + tok_cur_tag.attrs_a.unshift [c.toLowerCase(), ''] + tok_state = tok_state_attribute_name + return + if c is "\u0000" + parse_error() + tok_cur_tag.attrs_a.unshift ["\ufffd", ''] + tok_state = tok_state_attribute_name + return + if c is '' # EOF + parse_error() + tok_state = tok_state_data + cur -= 1 # reconsume + return + if c is '"' or c is "'" or c is '<' + parse_error() + # fall through to Anything else + # Anything else + tok_cur_tag.attrs_a.unshift [c, ''] + tok_state = tok_state_attribute_name + # 8.2.4.37 http://www.w3.org/TR/html5/syntax.html#before-attribute-value-state tok_state_before_attribute_value = -> switch c = txt.charAt(cur++) @@ -1191,7 +2144,7 @@ parse_html = (txt, parse_error_cb = null) -> when '"' tok_state = tok_state_after_attribute_value_quoted when '&' - tok_cur_tag.attrs_a[0][1] += tokenize_character_reference '"', true + tok_cur_tag.attrs_a[0][1] += parse_character_reference '"', true when "\u0000" # Parse error tok_cur_tag.attrs_a[0][1] += "\ufffd" @@ -1208,7 +2161,7 @@ parse_html = (txt, parse_error_cb = null) -> when "'" tok_state = tok_state_after_attribute_value_quoted when '&' - tok_cur_tag.attrs_a[0][1] += tokenize_character_reference "'", true + tok_cur_tag.attrs_a[0][1] += parse_character_reference "'", true when "\u0000" # Parse error tok_cur_tag.attrs_a[0][1] += "\ufffd" @@ -1225,7 +2178,7 @@ parse_html = (txt, parse_error_cb = null) -> when "\t", "\n", "\u000c", ' ' tok_state = tok_state_before_attribute_name when '&' - tok_cur_tag.attrs_a[0][1] += tokenize_character_reference '>', true + tok_cur_tag.attrs_a[0][1] += parse_character_reference '>', true when '>' tok_state = tok_state_data tmp = tok_cur_tag @@ -1265,7 +2218,7 @@ parse_html = (txt, parse_error_cb = null) -> # 8.2.4.69 http://www.w3.org/TR/html5/syntax.html#consume-a-character-reference # Don't set this as a state, just call it # returns a string (NOT a text node) - tokenize_character_reference = (allowed_char = null, in_attr = false) -> + parse_character_reference = (allowed_char = null, in_attr = false) -> if cur >= txt.length return '&' switch c = txt.charAt(cur) @@ -1342,11 +2295,17 @@ parse_html = (txt, parse_error_cb = null) -> # see comments on TYPE_TAG/etc for the structure of this data tree = new Node TYPE_TAG, name: 'html', namespace: NS_HTML open_els = [tree] - tree_state = tree_in_body + afe = [] # active formatting elements + template_insertion_modes = [] + insertion_mode = ins_mode_in_body + original_insertion_mode = insertion_mode # TODO check spec + flag_scripting = true # TODO might need an extra flag to get