# Features covered: international issues 
#
# Copyright (c) 2002 Rolf Ade.
#
# RCS: @(#) $Id$

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

testConstraint beyondBMP [expr {[dom featureinfo TCL_UTF_MAX] > 3}]
if {[join [lrange [split [package present Tcl] .] 0 1] .] <= 8.4} {
    testConstraint 8.6 false
} else {
    testConstraint 8.6 [package vsatisfies [package present Tcl] 8.6-]
}

test i18n-1.1 {parse utf-8 string} {
    set russian "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439"
    set doc [dom parse "<test>$russian</test>"]
    set root [$doc documentElement]
    set text [$root text]
    $doc delete
    string compare $text "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439"
} {0}

test i18n-1.2 {parse utf-8 channel} {
    set fd [open [file join [pwd] [file dir [info script]] data/i18n_1.xml]]
    fconfigure $fd -encoding utf-8
    set doc [dom parse -channel $fd]
    close $fd
    set root [$doc documentElement]
    set text [$root text]
    $doc delete
    string compare $text "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439"
} {0}
    
test i18n-1.3 {parse utf-8 readFile} {
    set doc [dom parse [::tdom::xmlReadFile [file join [pwd] [file dir [info script]] data/i18n_1.xml]]]
    set root [$doc documentElement]
    set text [$root text]
    $doc delete
    string compare $text "\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439"
} {0}
    
test i18n-1.4 {pcdata outside BMP} -body {
    set doc [dom parse "<doc>&#x1F46E;&#x1F47E;&#x1F494;</doc>"]
    set result [$doc asXML -indent none -escapeNonASCII]
    $doc delete
    set result
} -result "<doc>&#128110;&#128126;&#128148;</doc>"

test i18n-1.5 {pcdata outside BMP} -body {
    set doc [dom parse "<doc>&#x1F46E;&#x1F47E;&#x1F494;</doc>"]
    set result [$doc selectNodes string-length(/doc)]
    $doc delete
    set result
} -result 3

test i18n-1.6 {pcdata outside BMP} -constraints {
    8.6
    beyondBMP
} -body {
    set doc [dom parse "<doc>\U1F46E\U1F47E\U1F494</doc>"]
    set result [$doc asXML -indent none -escapeNonASCII]
    $doc delete
    set result
} -result "<doc>&#128110;&#128126;&#128148;</doc>"

test i18n-1.7 {pcdata outside BMP} -body {
    set doc [dom parse "<doc>&#x1F46E;&#x1F47E;&#x1F494;</doc>"]
    set result [dom isBMPCharData [$doc selectNodes string(/doc)]]
    $doc delete
    set result
} -result 0

test i18n-1.8 {pcdata outside BMP} -constraints {
    beyondBMP
    8.6
} -body {
    set doc [dom parse "<doc>\U1F46E\U1F47E\U1F494</doc>"]
    set result [$doc asXML -indent none]
    $doc delete
    set result
} -result "<doc>&#128110;&#128126;&#128148;</doc>"

test i18n-1.10 {pcdata outside unicode} -body {
    set doc [dom parse "<doc>&#x10FFFF;</doc>"]
    set result [$doc asXML -indent none -escapeNonASCII]
    $doc delete
    set result
} -result "<doc>&#1114111;</doc>"

test i18n-1.11 {pcdata outside unicode} -body {
    set result [catch {dom parse "<doc>&#x110000;</doc>"} msg]
    set msg
} -match glob -result "*reference to invalid character number*"

# cleanup
::tcltest::cleanupTests
return

