aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/template/html-scanner/node_test.rb
blob: 5b5d09203697730a24ff413f3d72b4e86c9e3bbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'abstract_unit'

class NodeTest < ActiveSupport::TestCase

  class MockNode
    def initialize(matched, value)
      @matched = matched
      @value = value
    end

    def find(conditions)
      @matched && self
    end

    def to_s
      @value.to_s
    end
  end

  def setup
    @node = HTML::Node.new("parent")
    @node.children.concat [MockNode.new(false,1), MockNode.new(true,"two"), MockNode.new(false,:three)]
  end

  def test_match
    assert !@node.match("foo")
  end

  def test_tag
    assert !@node.tag?
  end

  def test_to_s
    assert_equal "1twothree", @node.to_s
  end

  def test_find
    assert_equal "two", @node.find('blah').to_s
  end

  def test_parse_strict
    s = "<b foo='hello'' bar='baz'>"
    assert_raise(RuntimeError) { HTML::Node.parse(nil,0,0,s) }
  end

  def test_parse_relaxed
    s = "<b foo='hello'' bar='baz'>"
    node = nil
    assert_nothing_raised { node = HTML::Node.parse(nil,0,0,s,false) }
    assert node.attributes.has_key?("foo")
    assert !node.attributes.has_key?("bar")
  end

  def test_to_s_with_boolean_attrs
    s = "<b foo bar>"
    node = HTML::Node.parse(nil,0,0,s)
    assert node.attributes.has_key?("foo")
    assert node.attributes.has_key?("bar")
    assert "<b foo bar>", node.to_s
  end

  def test_parse_with_unclosed_tag
    s = "<span onmouseover='bang'"
    node = nil
    assert_nothing_raised { node = HTML::Node.parse(nil,0,0,s,false) }
    assert node.attributes.has_key?("onmouseover")
  end

  def test_parse_with_valid_cdata_section
    s = "<![CDATA[<span>contents</span>]]>"
    node = nil
    assert_nothing_raised { node = HTML::Node.parse(nil,0,0,s,false) }
    assert_kind_of HTML::CDATA, node
    assert_equal '<span>contents</span>', node.content
  end

  def test_parse_strict_with_unterminated_cdata_section
    s = "<![CDATA[neverending..."
    assert_raise(RuntimeError) { HTML::Node.parse(nil,0,0,s) }
  end

  def test_parse_relaxed_with_unterminated_cdata_section
    s = "<![CDATA[neverending..."
    node = nil
    assert_nothing_raised { node = HTML::Node.parse(nil,0,0,s,false) }
    assert_kind_of HTML::CDATA, node
    assert_equal 'neverending...', node.content
  end
end