# Tkhtml test file
proc sourcefile {file} {
  set fname [file join [file dirname [info script]] $file] 
  uplevel #0 [list source $fname]
}
sourcefile common.tcl

set ::script_handler_count 0
proc scriptHandler {attr data} {
  incr ::script_handler_count
  return ""
}

proc get_tree {{indent 0} {node ""}} {
  if {$node eq ""} {
    set node [.h node]
  }

  set output [string repeat " " $indent]

  set tag [$node tag]
  if {$tag eq ""} {
    append output "[$node text -tokens]\n"
  } else {
    append output "<${tag}>\n"
  }

  foreach child [$node children] {
    append output [get_tree [expr $indent + 2] $child]
  }

  if {$tag ne ""} {
    append output [string repeat " " $indent]
    append output "</${tag}>\n"
  }

  return $output
}

#--------------------------------------------------------------------------
# Test cases tree-1.* test the [widget handler script ...] command,
# including in conjunction with incremental document parsing.
#
tcltest::test tree-1.0 {} -body {
  html .h
  .h handler script script scriptHandler 
  set ::script_handler_count
} -result 0

tcltest::test tree-1.1 {} -body {
  .h parse -final [string trim {
    <html><body>
    <p>Hello World
    <script>
      A whole bunch of script!!!
      A whole bunch of script!!!
      A whole bunch of script!!!
    </script>
    <p>Hello Again World
    </body>
    </html>
  }]
  set ::script_handler_count
} -result 1

tcltest::test tree-1.2 {} -body {
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {space 4}
    <p>
      {text Hello} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
    <p>
      {text Hello} {space 1} {text Again} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
  </body>
</html>
}]

tcltest::test tree-1.3 {} -body {
  .h reset
  .h parse [string trim {
    <html>
    <body>
    <p>Hello World
    <script>
      A whole bunch of script!!!
      A whole bunch of script!!!
  }]

  .h parse -final [string trim {
      A whole bunch of script!!!
    </script>
    <p>Hello Again World
    </body>
    </html>
  }]
  set ::script_handler_count
} -result 2

tcltest::test tree-1.4 {} -body {
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {space 4}
    {space 4}
    <p>
      {text Hello} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
    <p>
      {text Hello} {space 1} {text Again} {space 1} {text World} {newline 1} {space 4}
      {newline 1} {space 4}
    </p>
  </body>
</html>
}]

tcltest::test tree-1.5 {} -body {
  .h reset
  .h parse [string trim {
    <html><body>
    <script>
      First line of script
  }]
  .h parse [string trim {
      .
      Second line of script.
    </script>
    <p>Paragraph one
  }]
  .h parse -final [string trim {
    <p>Paragraph two
  }]
  set ::script_handler_count
} -result 3

tcltest::test tree-1.6 {} -body {
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {space 4}
    {newline 1} {space 4}
    <p>
      {text Paragraph} {space 1} {text one}
    </p>
    <p>
      {text Paragraph} {space 1} {text two}
    </p>
  </body>
</html>
}]

tcltest::test tree-1.7 {} -body {
  .h reset
  .h parse -final {
    <html><body><script src=hello></script></body>
  }
  set ::script_handler_count
} -result 4

#--------------------------------------------------------------------------
# Test cases tree-2.* test that ticket #12 has been fixed.
#
tcltest::test tree-2.1 {} -body {
  .h reset
  .h parse -final {<html><body>Hello World}
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    {text Hello} {space 1} {text World}
  </body>
</html>
}]

#--------------------------------------------------------------------------
# Test cases tree-3.* test the [fragment] API.
#
tcltest::test tree-3.1.1 {} -body {
  set ::nodelist [.h fragment {<img src="bg.gif">}]
  llength $::nodelist
} -result 1
tcltest::test tree-3.1.2 {} -body {
  set ::node [lindex $::nodelist 0]
  $::node tag
} -result img
tcltest::test tree-3.1.3 {} -body {
  $::node attr src
} -result bg.gif
tcltest::test tree-3.1.4 {} -body {
  $::node parent
} -result ""
tcltest::test tree-3.1.5 {} -body {
  .h reset
  set rc [catch {$::node attr src} msg]
  list $rc $msg
} -result [list 1 "invalid command name \"$::node\""]

tcltest::test tree-3.2.1 {} -body {
  set ::nodelist [.h fragment {<div>hello</div>  text}]
  llength $::nodelist
} -result 2
tcltest::test tree-3.2.2 {} -body {
  set ::textnode [lindex $::nodelist 1]
  $::textnode text -tokens
} -result [list {space 2} {text text}]
tcltest::test tree-3.2.3 {} -body {
  set ::divnode [lindex $::nodelist 0]
  $::divnode tag
} -result div
tcltest::test tree-3.2.4 {} -body {
  set ::divchild [lindex [$::divnode children] 0]
  $::divchild text
} -result hello
tcltest::test tree-3.2.5 {} -body {
  .h reset
  set rc [catch {$::divchild text} msg]
  list $rc $msg
} -result [list 1 "invalid command name \"$::divchild\""]

tcltest::test tree-3.3.1 {} -body {
  .h parse -final "<div>"
  set ::nodelist [.h fragment "<span>some text</span><img src=src.gif>"]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.2 {} -body {
  set div [.h search div]
  $div insert [lindex $::nodelist 1]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <img>
      </img>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.3 {} -body {
  set div [.h search div]
  $div insert -before [lindex [$div children] 0] [lindex $::nodelist 0]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <span>
        {text some} {space 1} {text text}
      </span>
      <img>
      </img>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.4 {} -body {
  set div [.h search div]
  $div insert [lindex $::nodelist 0]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <img>
      </img>
      <span>
        {text some} {space 1} {text text}
      </span>
    </div>
  </body>
</html>
}]
tcltest::test tree-3.3.5 {} -body {
  set div [.h search div]
  $div remove [lindex $::nodelist 0]
  string trim [get_tree]
} -result [string trim {
<html>
  <head>
  </head>
  <body>
    <div>
      <img>
      </img>
    </div>
  </body>
</html>
}]

finish_test




