# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH.unshift("../../compat") unless $LOAD_PATH.include?("../../compat")
require "htree"
$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

module HTree
  module Node
    def format_xml(i=-1)
      str = ""
      display_xml(str)
      str.gsub!(%r! xmlns=\"http://www.w3.org/1999/xhtml\"!, "")
      str.gsub!(%r! xmlns=\"\"!, "")
      if i < 0
	str.gsub!(/\n>/, ">")
	str.gsub!(/\n\/>/, "/>")
      end
      str
    end
  end
end

if $0 == __FILE__
  require "test/unit"
  $debug = true
end

if $debug
  class TC_HTree_format_xml < Test::Unit::TestCase
    def assert(e, s)
      assert_equal(e, s.format_xml)
    end

    def test_all
      # check_extract_text
      e = HTree::Elem.new("a", "t")
      assert("<a>t</a>", e)
      assert_equal("t", e.extract_text.to_s)

      e = HTree::Elem.new("b", "s", e, "u")
      assert("<b>s<a>t</a>u</b>", e)
      assert_equal("stu", e.extract_text.to_s)

      # test_htree_format_xml
      e = HTree::Elem.new("a")
      assert_equal("<a/>", e.format_xml)
      assert_equal("<a\n/>", e.format_xml(0))

      e = HTree::Text.new("a")
      assert_equal("a", e.format_xml)
      assert_equal("a", e.format_xml(0))
    end
  end
end
