
#----------------------------------------------------------------------
# A "warm-body" test to check that the infrastructure is working.
# Count the anchors in a document.
#
do_browser_test warmbody.1 -html {
  <BODY>
    <A name="one">One</A>
    <A id="two">Two</A>          <!-- Not an anchor (no "name" attribute) -->
    <A name="three">Three</A>
} -javascript {
  return document.anchors.length
}
#----------------------------------------------------------------------

proc body_tree_test {name doc} {

  # Uses:
  #
  #     Node.childNodes
  #     Node.nodeType
  #     Document.getElementsByTagName()
  #
  #     NodeList.length
  #     NodeList.item()
  #
  set body_tree_function {
    <SCRIPT>

      function elem_to_string (elem) {
        var res = "<" + elem.tagName + ">"
        for (var ii = 0; ii < elem.childNodes.length; ii++) {
          var node = elem.childNodes.item(ii)
          if (node.nodeType == Node.ELEMENT_NODE) {
            res += elem_to_string(node)
          }
        }
        res += "</" + elem.tagName + ">"
        return res
      }

      function body_tree () {
        body = document.getElementsByTagName("BODY").item(0)
        return elem_to_string(body)
      }

      function node_to_string (N) {
        /*
         * Known incompatibility between Hv3 and firefox: Hv3 chops <SCRIPT>
         * tags out of the tree completely, firefox leaves them in (but
         * chops out their contents). Not sure if this is ever significant
         * or not...
         */
        if (N.nodeType == Node.ELEMENT_NODE && N.nodeName == "SCRIPT") {
          return ""
        }

        /*
         * Another known incompatibility: Hv3 tends to add white-space nodes
         * in a few places where fire-fox does not. This is probably more
         * serious than the <SCRIPT> problem above, but still not too
         * much of a concern in the short term. Ignore any text-node
         * that consists entirely of white-space.
         */
        if (N.nodeType == Node.TEXT_NODE && !/[^\t\n\r ]/.test(N.nodeValue)) {
          return ""
        }

        var res = "<" + N.nodeType + " " + N.nodeName + ">"

        for (var ii = 0; ii < N.childNodes.length; ii++) {
          var N2 = N.childNodes.item(ii)
          res += node_to_string(N2)
        }

        res += "</>"
        return res
      }

      function dom_tree () {
        return node_to_string(document)
      }

    </SCRIPT>
  }

  browsertest $name utf-8 [string trim "
    $body_tree_function
    $doc
    <SCRIPT>
      function browsertest () { return dom_tree() }
    </SCRIPT>
  "]

#  ::browsertest::do_test $name -timeout 10000000 -html [subst {
#    $body_tree_function
#    $doc
#  }] -javascript {
#    // return body_tree()
#    return dom_tree()
#  } -browsers {Hv3 Firefox}
}


#----------------------------------------------------------------------
# The following test cases - tables.X - examine the document trees
# created when incorrectly (or partially) specified tables are parsed.
#
# From the HTML 4.0.1 DTD:
#
# <!ELEMENT TABLE    - - (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)> 
# <!ELEMENT CAPTION  - - (%inline;)*  -- table caption --> 
# <!ELEMENT THEAD    - O (TR)+        -- table header --> 
# <!ELEMENT TFOOT    - O (TR)+        -- table footer --> 
# <!ELEMENT TBODY    O O (TR)+        -- table body --> 
# <!ELEMENT COLGROUP - O (COL)*       -- table column group --> 
# <!ELEMENT COL      - O EMPTY        -- table column --> 
# <!ELEMENT TR       - O (TH|TD)+     -- table row --> 
# <!ELEMENT (TH|TD)  - O (%flow;)*    -- table header cell, table data cell-->
#
# In other words, all table sub-trees should look like:
#
# <TABLE>
#     <CAPTION>...</CAPTION>)
#     <COLGROUP>...</COLGROUP>   (etc.)
#     <THEAD>
#         (<TR> <TD|TH>*)*
#     <TFOOT>
#         (<TR> <TD|TH>*)*
#     <TBODY>
#         (<TR> <TD|TH>*)*
#
# The rules for how tag-soup is converted to this structure are 
# specified as part of:
#

body_tree_test tables.1 { <BODY> <TBODY><TR><TD><DIV> }
body_tree_test tables.2 { <BODY> <TABLE><TBODY><TR><TD><DIV> }

body_tree_test tables.3 { <BODY> <TABLE><DIV><SPAN></SPAN><TD> }
body_tree_test tables.4 { <BODY> <TABLE> <DIV> <DIV> <TR> <SPAN> }

body_tree_test tables.5 { <BODY> <TABLE><TD> }
body_tree_test tables.6 { <BODY> <TABLE><TR> }

body_tree_test tables.7 { <BODY> <TABLE><DIV><TR></DIV> }
body_tree_test tables.8 { <BODY> <TABLE><DIV><TD></DIV> }
body_tree_test tables.9 { <BODY> <TABLE><DIV><SPAN></DIV> }

body_tree_test tables.10 { <BODY> <TD><SPAN> }
body_tree_test tables.11 { <BODY> <TR><SPAN> }
body_tree_test tables.12 { <BODY> <TR><TD><SPAN> }
body_tree_test tables.13 { <BODY> <TBODY><TR><TD><SPAN> }

body_tree_test tables.14 { <BODY> <TABLE><DIV><DIV><SPAN></DIV><SPAN> }

# This structure is found in the mail.yahoo.com application.
#
body_tree_test tables.15 { <TABLE><FORM><TR><TR></TABLE> }
body_tree_test tables.16 { <TABLE><TBODY><FORM><TR><TR></TABLE> }

body_tree_test tree.1 { <BODY> <DIV> }
body_tree_test attr.1 { <BODY> <DIV id="two"> hello }

#---------------------------------------------------------------------- 

