# Features covered: HTML parser
#
# This file contains a collection of tests for the HTML parser.
# Tested functionalities:
#    html-1.*: Character encoding
#    html-2.*: Parsing tests
#    html-3.*: Bad data
#    html-4.*: DOM building
#
# Copyright (c) 2002-2007 Rolf Ade.
#
# RCS: @(#) $Id$

source [file join [file dir [info script]] loadtdom.tcl]

test html-1.1 {HTML character entities} {need_i18n} {
    set doc [dom parse -html {<html><body>&nbsp;&iexcl;&Auml;&uuml;</body></html>}]
    set root [$doc documentElement]
    set body [$root firstChild]
    set result [$body text]
    $doc delete
    set result
} "\u00A0\u00A1\u00c4\u00fc"

test html-1.2 {character entities} {need_i18n} {
    set doc [dom parse -html {<html><body>&#214;&#xC4;&#xc4;</body></html>}]
    set root [$doc documentElement]
    set body [$root firstChild]
    set result [$body text]
    $doc delete
    set result
} "\u00d6\u00c4\u00c4"

test html-1.3 {character entities} {need_i18n} {
    set doc [dom parse -html {<html>&euro;&ni;</html>}]
    set root [$doc documentElement]
    set result [$root text]
    $doc delete
    set result
} "\u20ac\u220b"

test html-2.1 {not closed p tags} {
    set doc [dom parse -html {
        <html><body><p>Para 1<p>Para 2<p>Para 3</body></html>
    }]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<html><body><p>Para 1</p><p>Para 2</p><p>Para 3</p></body></html>}

test html-2.2 {HTML parsing} {
    set doc [dom parse -html {
        <HTML><HEAD></HEAD>
        <BODY>
        <H1>HTML</H1>
        </BODY>
        </HTML>
    }]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<html><head/><body><h1>HTML</h1></body></html>}

test html-2.3 {HTML parsing} {
    set doc [dom parse -html {
        <!-- comment -->
        <HTML><HEAD></HEAD>
        <BODY>
        <H1>HTML</H1>
        </BODY>
        </HTML>
    }]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} {<!-- comment --><html><head/><body><h1>HTML</h1></body></html>}

test html-2.4 {HTML parsing} {
    set doc [dom parse -html {
        <!-- comment -->
        <HTML><HEAD></HEAD>
        <BODY>
        <H1>HTML</H1>
        </BODY>
        </HTML>
        <!-- comment -->
    }]
    $doc documentElement root
    set result [$root nodeName]
    $doc delete
    set result
} {html}

test html-2.5 {HTML parsing} {
    set doc [dom parse -html {<html> <head><title></title></head><body>
        <form>
        <select id="L" name="nls_language">
        <option value="">--</option>
        <option value="en_US" selected="on">en_US</option>
        <option value="es_ES">es_ES</option>
        <option value="de_DE">de_DE</option>
        </select>
        </form>
        </body></html>
    }]
    $doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--</option><option value="en_US" selected="on">en_US</option><option value="es_ES">es_ES</option><option value="de_DE">de_DE</option>
</select></form></body>
</html>}

test html-2.6 {HTML parsing} {
    set doc [dom parse -html {<html> <head><title></title></head><body>
        <form>
        <select id="L" name="nls_language">
        <option value="">--
        <option value="en_US" selected="on">en_US
        <option value="es_ES">es_ES
        <option value="de_DE">de_DE
        </select>
        </form>
        </body></html>
    }]
    $doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--
        </option><option value="en_US" selected="on">en_US
        </option><option value="es_ES">es_ES
        </option><option value="de_DE">de_DE
        </option>
</select></form></body>
</html>}

test html-2.7 {HTML parsing} {
    set doc [dom parse -html {<html> <head><title></title></head><body>
        <form>
        <select id="L" name="nls_language">
        <option value="">--
        <option value="en_US" selected>en_US
        <option value="de_DE">de_DE
        </select>
        </form>
        </body></html>
    }]
    $doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--
        </option><option value="en_US" selected="selected">en_US
        </option><option value="de_DE">de_DE
        </option>
</select></form></body>
</html>}

test html-2.8 {HTML parsing} {
    set doc [dom parse -html {<html> <head><title></title></head><body>
        <form>
        <select id="L" name="nls_language">
        <option value="">--
        <option selected value="en_US">en_US
        <option value="de_DE">de_DE
        </select>
        </form>
        </body></html>
    }]
    $doc asHTML
} {<html>
<head><title></title></head><body><form><select id="L" name="nls_language">
<option value="">--
        </option><option selected="selected" value="en_US">en_US
        </option><option value="de_DE">de_DE
        </option>
</select></form></body>
</html>}

test html-3.1 {Bad data} {
    set data {line 6 column 17 - Warning: <script> lacks "type" attribute
line 10 column 17 - Warning: <script> lacks "type" attribute
        line 11 column 17 - Warning: <table> lacks "summary" attribute}
    catch {set doc [dom parse -html $data]}
} 1

test html-3.2 {Bad data} {
     catch {set doc [dom parse -html {<a>}]}
} 1
        
test html-4.1 {Tag name case normalization} {
    set doc [dom parse -html {<HtmL><boDY></BODy></HTml>}]
    set result [$doc asHTML]
    $doc delete
    set result
} {<html><body></body></html>}

test html-4.2 {Tag name case normalization} {knownBug} {
    set doc [dom parse -html {<HtmL><NotaHTML_Tag/></HTml>}]
    set result [$doc asHTML]
    $doc delete
    set result
} {<html><body></body></html>}

test html-4.3 {Attribute name case normalization} {
    set doc [dom parse -html {<HtmL><Body Id=3 FOO=Bar GriLL></body></html>}]
    set result [$doc asHTML]
    $doc delete
    set result
} {<html><body id="3" foo="Bar" grill="grill"></body></html>}

test html-4.4 {ID Attribute handling} {
    set doc [dom parse -html {<HtmL><p ID=1 f00=bar/><p id="2"/><p/></html>}]
    set result [[$doc getElementById 1] getAttribute id]
    $doc delete
    set result
} {1}


# cleanup
::tcltest::cleanupTests
return
