Class: TestTreeWalkers

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
HTML5::TestSupport
Defined in:
lib/feed_tools/vendor/html5/tests/test_treewalkers.rb

Instance Method Summary collapse

Methods included from HTML5::TestSupport

#convertTreeDump, #sortattrs

Instance Method Details

#concatenateCharacterTokens(tokens) {|charactersToken| ... } ⇒ Object

Yields:

  • (charactersToken)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/feed_tools/vendor/html5/tests/test_treewalkers.rb', line 24

def concatenateCharacterTokens(tokens)
  charactersToken = nil
  for token in tokens
      type = token[:type]
      if [:Characters, :SpaceCharacters].include?(type)
          if charactersToken == nil
              charactersToken = {:type => :Characters, :data => token[:data]}
          else
              charactersToken[:data] += token[:data]
          end
      else
          if charactersToken != nil
              yield charactersToken
              charactersToken = nil
          end
          yield token
      end
  end
  yield charactersToken if charactersToken != nil
end

#convertTokens(tokens) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/feed_tools/vendor/html5/tests/test_treewalkers.rb', line 45

def convertTokens(tokens)
  output = []
  indent = 0
  concatenateCharacterTokens(tokens) do |token|
    case token[:type]
    when :StartTag, :EmptyTag
      output << "#{' '*indent}<#{token[:name]}>"
      indent += 2
      for name, value in token[:data].to_a.sort
        next if name=='xmlns'
        output << "#{' '*indent}#{name}=\"#{value}\""
      end
      indent -= 2 if token[:type] == :EmptyTag
    when :EndTag
      indent -= 2
    when :Comment
      output << "#{' '*indent}<!-- #{token[:data]} -->"
    when :Doctype
      if token[:name] and token[:name].any?
        output << "#{' '*indent}<!DOCTYPE #{token[:name]}>"
      else
        output << "#{' '*indent}<!DOCTYPE >"
      end
    when :Characters, :SpaceCharacters
      output << "#{' '*indent}\"#{token[:data]}\""
    end
  end
  output.join("\n")
end

#test_all_tokensObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/feed_tools/vendor/html5/tests/test_treewalkers.rb', line 115

def test_all_tokens
  expected = [
      {:data => [], :type => :StartTag, :name => 'html'},
      {:data => [], :type => :StartTag, :name => 'head'},
      {:data => [], :type => :EndTag,   :name => 'head'},
      {:data => [], :type => :StartTag, :name => 'body'},
      {:data => [], :type => :EndTag,   :name => 'body'},
      {:data => [], :type => :EndTag,   :name => 'html'}]
  for treeName, tree_class in $tree_types_to_test
    p = HTML5::HTMLParser.new(:tree => tree_class[:builder])
    document = p.parse("<html></html>")
    # document = tree_class.get(:adapter)(document)
    output = tree_class[:walker].new(document)
    expected.zip(output) do |expected_token, output_token|
      assert_equal(expected_token, output_token)
    end
  end
end