public
Rubygem
Description: a humane, eval-safe templating system using Hpricot
Clone URL: git://github.com/mattly/hpreserve.git
Click here to lend your support to: hpreserve and make a donation at www.pledgie.com !
mattly (author)
Mon May 12 15:18:22 -0700 2008
commit  e88d87815eff3d624235646dcbd8fb3f5e5b571c
tree    ca24be32c5c31f9582f499b95a58dc24fb7dbe63
parent  272ff8c5beade3371f627bdc8118ead3bfcf4296
hpreserve / spec / standard_filters_spec.rb
100644 140 lines (109 sloc) 4.007 kb
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require File.dirname(__FILE__) + '/spec_helper.rb'
 
describe Hpreserve::StandardFilters do
 
  describe "upcase" do
    before { @f = Hpreserve::Filters.create }
    
    it "capitalizes the text of the node" do
      @doc = Hpricot("<span>foo</span>")
      @f.run 'capitalize', @doc.at('span')
      @doc.at('span').inner_text.should == 'Foo'
    end
    
  end
  
  describe "date" do
    before { @f = Hpreserve::Filters.create }
    
    it "handles strftime arguments" do
      @doc = Hpricot("<span>Wed, 26 Mar 2008 23:45:55 -0700</span>")
      @f.run 'date', @doc.at('span'), '%e %b %y'
      @doc.at('span').inner_html.should == '26 Mar 08'
    end
  end
  
  
  describe "remove" do
    before { @f = Hpreserve::Filters.create }
    
    it "removes the node from the document" do
      @doc = Hpricot("<div>blah <div>Attention Client: This will be cooler</div><div>blah</div></div>")
      @f.run 'remove', @doc.at('div div')
      @doc.to_plain_text.should == "blah blah"
    end
  end
  
  describe "unwrap" do
    before { @f = Hpreserve::Filters.create }
    
    it "replaces the node with its content" do
      @doc = Hpricot("<div>Value</div>")
      @f.run 'unwrap', @doc.at('div')
      @doc.to_s.should == 'Value'
    end
  end
  
  describe "link" do
    before { @f = Hpreserve::Filters.create }
    
    it "sets the href attribute to the url value" do
      @doc = Hpricot("<a href=''>Foo</a>")
      @f.run 'link', @doc.at('a'), 'foo.com'
      @doc.at('a')['href'].should == 'foo.com'
    end
  end
  
  describe "add_class" do
    before { @f = Hpreserve::Filters.create }
    
    it "appends the class to the element's classes" do
      @doc = Hpricot("<span class='foo'>Foo</span>")
      @f.run 'add_class', @doc.at('span'), 'bar'
      @doc.at('span').classes.should == ['foo', 'bar']
    end
  end
  
  describe "set_class" do
    before do
      @f = Hpreserve::Filters.create
      @doc = Hpricot("<span class='foo'>Foo</span>")
    end
    
    it "replacess the element's classes" do
      @f.run 'set_class', @doc.at('span'), 'bar'
      @doc.at('span').classes.should == ['bar']
    end
    
    it "santizes the class name" do
      @f.run 'set_class', @doc.at('span'), '.foo and bar'
      @doc.at('span').classes.should == ['foo-and-bar']
    end
    
    it "handles multiple class names with commas" do
      @f.run 'set_class', @doc.at('span'), 'foo', 'bar'
      @doc.at('span').classes.should == %w(foo bar)
    end
    
  end
  
  describe "set_id" do
    before { @f = Hpreserve::Filters.create }
    
    it "sets the element's id" do
      @doc = Hpricot("<span>foo</span>")
      @f.run 'set_id', @doc.at('span'), 'bar'
      @doc.at('span')['id'].should == 'bar'
    end
    
    it "replaces the element's id" do
      @doc = Hpricot("<span id='foo'>foo</span>")
      @f.run 'set_id', @doc.at('span'), 'bar'
      @doc.at('span')['id'].should == 'bar'
    end
  end
 
  
  describe "attr" do
    before { @f = Hpreserve::Filters.create }
    
    it "sets the attr for src" do
      @doc = Hpricot('<span>foo</span>')
      @f.run 'attr', @doc.at('span'), 'src', '/lolcat.jpg'
      @doc.at('span')['src'].should == '/lolcat.jpg'
    end
    
    it "clobbers the attr for src" do
      @doc = Hpricot('<img src="/loldog.jpg" />')
      @f.run 'attr', @doc.at('img'), 'src', '/lolcat.jpg'
      @doc.at('img')['src'].should == '/lolcat.jpg'
    end
  end
  
  describe "attr_on_child" do
    before { @f = Hpreserve::Filters.create }
    
    it "sets the attr on the named child element" do
      @doc = Hpricot('<div><span id="foo">foo</span><span id="bar">bar</span></div>')
      @f.run 'attr_on_child', @doc.at('div'), 'foo', 'class', 'active'
      @doc.at('#foo').classes.should == ['active']
    end
    
    it "handles a child not found" do
      html = '<div><span id="bar">bar</span></div>'
      @doc = Hpricot(html)
      @f.run 'attr_on_child', @doc.at('div'), 'foo', 'class', 'active'
      @doc.to_s.should == html
    end
  end
  
end