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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/wabisabi-get"
require "qwik/style"

class Array

  VALID_TAGS = %w(
div
h2 h3 h4 h5 h6
p blockquote pre
ul ol li
dl dt dd
table tbody tr td 
span
a
em strong del
b i
img
plugin
hr
sup sub
)
  TAGS = VALID_TAGS.map {|t| t.intern }

  VALID_ATTR = %w(
href
src alt
method
param
align border hspace vspace
style
)
  ATTR = VALID_ATTR.map {|t| t.intern }

  URL_ATTR = %w(
href
src
)
  URL = URL_ATTR.map {|t| t.intern }

  STYLE_ATTR = %w(
style
)
  STYLE = STYLE_ATTR.map {|t| t.intern }

  def check_valid
    self.traverse_element {|e|
      element_name = e[0]
      next unless element_name.is_a?(Symbol)
      unless TAGS.include?(element_name)
	return element_name # not valid
      end

      attr = e.get_attr
      #p attr
      attr.each {|k, v|
	unless ATTR.include?(k)
	  return k # not valid
	end

	if URL.include?(k) # this is url
	  vs = v.to_s
	  if /\Ajavascript/i =~ vs
	    return "script" # not valid
	  end
	end

	if STYLE.include?(k) # this is style
	  style = v.to_s
	  unless Qwik::CSS.valid?(style)
	    return "not valid css" # not valid
	  end
	end
      }
    }
    return true
  end
end

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

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

    def test_valid
      assert(true, [:p, "t"])
      assert(true, [:ul, "t"])
      assert(:script, [:script, "t"])
      assert(true, [:img, {:src=>"s", :alt=>"a"}, "t"])
      assert(:onload, [:img, {:onload=>"o"}, "t"])
      assert("script", [:img, {:src=>"javascript:do something"}, "t"])
    end
  end
end
