#
# Test script for the Tk HTML widget
#
wm withdraw .
if {[lsearch [namespace children] ::tcltest] == -1} {
  source [file dirname $argv0]/engine.tcl
  namespace import ::tcltest::*
}

::tcltest::test html-3.1 {Check the HTML parser for sanity} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<p align=center>Test1 \nEnd\n"
  $h token list 1.0 end
} {0 {{Markup p align center} {Text Test1} {Space 1 1} {Text End} {Space 1 1}}}

::tcltest::test html-3.2 {Check that markup arguments are parsed correctly} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<p align="center" id='"5"' id2 = "'4'">}
  $h token list 1.0 end
} {0 {{Markup p align center id {"5"} id2 '4'}}}

::tcltest::test html-3.3 {Check that comments are ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<p><!-- This is comment --></p>}
  $h token list 1.0 end
} {0 {{Markup p} {Markup /p}}}

::tcltest::test html-3.4 {Check that entities are resolved} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {&amp;&nbsp;&lt;&gt;&#72;<p>}
  $h token list 1.0 end
} {0 {{Text {& <>H}} {Markup p}}}

::tcltest::test html-3.5 {Check that spacing is recorded properly} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<pre>\n\tHi\tThere\n</pre>"
  $h token list 1.0 end
} {0 {{Markup pre} {Space 1 1} {Space 8 0} {Text Hi} {Space 6 0} {Text There} {Space 1 1} {Markup /pre}}}

::tcltest::test html-3.6 {Script markup should become a single token} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<script>Contents of script are not tokenized</script>\n"
  $h token list 1.0 end
} {0 {{Markup script} {Space 1 1}}}

::tcltest::test html-3.6b {Style markup should become a single token} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<style>Contents of style are not tokenized</style>\n"
  $h token list 1.0 end
} {0 {{Markup style} {Space 1 1}}}

::tcltest::test html-3.7 {All markup within listing is normal text} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<listing><p>Hello</p></listing>\n"
  $h token list 1.0 end
} {0 {{Markup listing} {Text <p>Hello} {Text </p>} {Markup /listing} {Space 1 1}}}

::tcltest::test html-3.8 {All markup within xmp is normal text} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<xmp><p>Hello</p></xmp>\n"
  $h token list 1.0 end
} {0 {{Markup xmp} {Text <p>Hello} {Text </p>} {Markup /xmp} {Space 1 1}}}

::tcltest::test html-3.9 {All markup within textarea is treated as normal
text. This is not valid HTML, but it is what Netscape does.} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<textarea><p>Hello</p></textarea>\n"
  $h token list 1.0 end
} {0 {{Markup textarea} {Text <p>Hello} {Text </p>} {Markup /textarea} {Space 1 1}}}

::tcltest::test html-3.10 {All markup after plaintext become normal text} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<plaintext><p>Hello</p></plaintext>\n"
  $h token list 1.0 end
} {0 {{Markup plaintext} {Text <p>Hello} {Text </p>} {Text </plaintext>} {Space 1 1}}}

::tcltest::test html-3.11 {Unrecognized markup is ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<unknown>Text\n"
  $h token list 1.0 end
} {0 {{Text Text} {Space 1 1}}}

::tcltest::test html-3.12 {Entities are resolved within markup arguments} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<p id="&lt;hi&gt;">}
  $h token list 1.0 end
} {0 {{Markup p id <hi>}}}

::tcltest::test html-3.13 {Entites are resolved within markup arguments} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<p id=&lt;hi&gt;>}
  $h token list 1.0 end
} {0 {{Markup p id <hi>}}}

::tcltest::test html-3.14 {White space within markup is ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br >}
  $h token list 1.0 end
} {0 {{Markup br}}}

::tcltest::test html-3.15 {Whitespace within markup is ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br id = 5>}
  $h token list 1.0 end
} {0 {{Markup br id 5}}}

::tcltest::test html-3.16 {Whitespace in markup is ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br id = 5 >}
  $h token list 1.0 end
} {0 {{Markup br id 5}}}

::tcltest::test html-3.17 {Whitespace in markup is ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br id =5>}
  $h token list 1.0 end
} {0 {{Markup br id 5}}}

::tcltest::test html-3.18 {Whitespace in markup is ignored} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br id= 5 >}
  $h token list 1.0 end
} {0 {{Markup br id 5}}}

::tcltest::test html-3.19 {Whitespace in markup is ignored, except when quoted} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br id= " 5 " >}
  $h token list 1.0 end
} {0 {{Markup br id { 5 }}}}

::tcltest::test html-3.20 {Tabs in markup are treated like whitespace} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<br id\t=\t' 5 '\t>"
  $h token list 1.0 end
} {0 {{Markup br id { 5 }}}}

::tcltest::test html-3.21 {Newlines in  markup are treated like whitespace} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<br id\n=\n\" 5 \"\n>"
  $h token list 1.0 end
} {0 {{Markup br id { 5 }}}}

::tcltest::test html-3.22 {Markup arguments without values} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<br clear >"
  $h token list 1.0 end
} {0 {{Markup br clear {}}}}

::tcltest::test html-3.23 {Markup arguments without values} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<br clear id=9>"
  $h token list 1.0 end
} {0 {{Markup br clear {} id 9}}}

::tcltest::test html-3.24 {XHTML style / at the end of markup} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br clear="both"/>}
  $h token list 1.0 end
} {0 {{Markup br clear both}}}

::tcltest::test html-3.25 {XHTML style / at the end of markup} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br />}
  $h token list 1.0 end
} {0 {{Markup br}}}

::tcltest::test html-3.26 {XHTML style / at the end of markup} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<br/>}
  $h token list 1.0 end
} {0 {{Markup br}}}

::tcltest::test html-3.27 {/ At the end of markup adds a second token} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<p />}
  $h token list 1.0 end
} {0 {{Markup p} {Markup /p}}}

::tcltest::test html-3.28 {/ at the end of markup adds a second token} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {<p/>}
  $h token list 1.0 end
} {0 {{Markup p} {Markup /p}}}

::tcltest::test html-3.29 {/ not at the end of markup still work} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse "<A href=\"\" /privacy.html=\"/privacy.html\">\n"
  $h token list 1.0 end
} {0 {{Markup a href {} /privacy.html /privacy.html} {Space 1 1}}}

::tcltest::test html-3.30 {Incremental parsing} {
  set h [tkhtml_test_widget]
  $h clear
  set txt {<a href="" /privacy.html="none"/>Hello&amp;<p>}
  set len [string length $txt]
  for {set i 0} {$i<$len} {incr i} {
    $h parse [string index $txt $i]
  }
  $h token list 1.0 end
} {0 {{Markup a href {} /privacy.html none} {Markup /a} {Text Hello&} {Markup p}}}

::tcltest::test html-3.31 {Searching for anchors} {
  set h [tkhtml_test_widget]
  $h clear
  $h parse {
    <a name="one">One</a>
    <a id="two">Two</a>
    <a href="three">Three</a>
    <a name="four" id="vier">Four</a>
  }
  update
  $h names
} {0 {one two four}}
