]> git.wincent.com - wikitext.git/blob - spec/img_spec.rb
Backend support for HTML5/XML output styles
[wikitext.git] / spec / img_spec.rb
1 # encoding: utf-8
2 # Copyright 2008-2010 Wincent Colaiuta. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # 1. Redistributions of source code must retain the above copyright notice,
8 #    this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright notice,
10 #    this list of conditions and the following disclaimer in the documentation
11 #    and/or other materials provided with the distribution.
12
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24
25 require File.join(File.dirname(__FILE__), 'spec_helper.rb')
26
27 describe Wikitext::Parser, 'embedding img tags' do
28   before do
29     @parser = Wikitext::Parser.new
30   end
31
32   it 'should convert valid markup into inline image tags' do
33     expected = %Q{<p><img src="/images/foo.png" alt="foo.png"></p>\n}
34     @parser.parse('{{foo.png}}').should == expected
35   end
36
37   it 'should produce XML output if passed option at parse time' do
38     expected = %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
39     @parser.parse('{{foo.png}}', :output_style => :xml).should == expected
40   end
41
42   it 'should produce XML output if set via instance variable' do
43     expected = %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
44     parser = Wikitext::Parser.new
45     parser.output_style = :xml
46     parser.parse('{{foo.png}}').should == expected
47   end
48
49   it 'should produce XML output if option set during initialization' do
50     expected = %Q{<p><img src="/images/foo.png" alt="foo.png" /></p>\n}
51     parser = Wikitext::Parser.new :output_style => :xml
52     parser.parse('{{foo.png}}').should == expected
53   end
54
55   it 'should produce HTML output if passed unrecognized output style' do
56     expected = %Q{<p><img src="/images/foo.png" alt="foo.png"></p>\n}
57
58     # the _only_ recognized override is :xml (case-sensitive)
59     @parser.parse('{{foo.png}}', :output_style => :html).should == expected
60     @parser.parse('{{foo.png}}', :output_style => false).should == expected
61     @parser.parse('{{foo.png}}', :output_style => nil).should == expected
62     @parser.parse('{{foo.png}}', :output_style => 42).should == expected
63     @parser.parse('{{foo.png}}', :output_style => 'XML').should == expected
64     @parser.parse('{{foo.png}}', :output_style => :XML).should == expected
65   end
66
67   it 'should appear embedded in an inline flow' do
68     expected = %Q{<p>before <img src="/images/foo.png" alt="foo.png"> after</p>\n}
69     @parser.parse('before {{foo.png}} after').should == expected
70   end
71
72   it 'should allow images in subdirectories' do
73     expected = %Q{<p><img src="/images/foo/bar.png" alt="foo/bar.png"></p>\n}
74     @parser.parse('{{foo/bar.png}}').should == expected
75   end
76
77   it 'should pass through empty image tags unchanged' do
78     @parser.parse('{{}}').should == %Q{<p>{{}}</p>\n}
79   end
80
81   it 'should not append prefix if img src starts with a slash' do
82     expected = %Q{<p><img src="/foo.png" alt="/foo.png"></p>\n}
83     @parser.parse('{{/foo.png}}').should == expected
84   end
85
86   it 'should work in BLOCKQUOTE blocks' do
87     expected = dedent <<-END
88       <blockquote>
89         <p><img src="/images/foo.png" alt="foo.png"></p>
90       </blockquote>
91     END
92     @parser.parse('> {{foo.png}}').should == expected
93   end
94
95   it 'should work in unordered lists' do
96     input = dedent <<-END
97       * {{foo.png}}
98       * {{bar.png}}
99       * {{baz.png}}
100     END
101     expected = dedent <<-END
102       <ul>
103         <li><img src="/images/foo.png" alt="foo.png"></li>
104         <li><img src="/images/bar.png" alt="bar.png"></li>
105         <li><img src="/images/baz.png" alt="baz.png"></li>
106       </ul>
107     END
108     @parser.parse(input).should == expected
109   end
110
111   it 'should work in ordered lists' do
112     input = dedent <<-END
113       # {{foo.png}}
114       # {{bar.png}}
115       # {{baz.png}}
116     END
117     expected = dedent <<-END
118       <ol>
119         <li><img src="/images/foo.png" alt="foo.png"></li>
120         <li><img src="/images/bar.png" alt="bar.png"></li>
121         <li><img src="/images/baz.png" alt="baz.png"></li>
122       </ol>
123     END
124     @parser.parse(input).should == expected
125   end
126
127   it 'should work in <h1> headings' do
128     expected = %Q{<h1><img src="/images/foo.png" alt="foo.png"></h1>\n}
129     @parser.parse('= {{foo.png}} =').should == expected
130   end
131
132   it 'should work in <h2> headings' do
133     expected = %Q{<h2><img src="/images/foo.png" alt="foo.png"></h2>\n}
134     @parser.parse('== {{foo.png}} ==').should == expected
135   end
136
137   it 'should work in <h3> headings' do
138     expected = %Q{<h3><img src="/images/foo.png" alt="foo.png"></h3>\n}
139     @parser.parse('=== {{foo.png}} ===').should == expected
140   end
141
142   it 'should work in <h4> headings' do
143     expected = %Q{<h4><img src="/images/foo.png" alt="foo.png"></h4>\n}
144     @parser.parse('==== {{foo.png}} ====').should == expected
145   end
146
147   it 'should work in <h5> headings' do
148     expected = %Q{<h5><img src="/images/foo.png" alt="foo.png"></h5>\n}
149     @parser.parse('===== {{foo.png}} =====').should == expected
150   end
151
152   it 'should work in <h6> headings' do
153     expected = %Q{<h6><img src="/images/foo.png" alt="foo.png"></h6>\n}
154     @parser.parse('====== {{foo.png}} ======').should == expected
155   end
156
157   it 'should pass single curly braces through unaltered' do
158     @parser.parse('{foo.png}').should == %Q{<p>{foo.png}</p>\n}
159   end
160
161   it 'should have no effect inside PRE blocks' do
162     @parser.parse(' {{foo.png}}').should == %Q{<pre>{{foo.png}}</pre>\n}
163   end
164
165   it 'should have no effect inside PRE_START blocks' do
166     expected = %Q{<pre>{{foo.png}}</pre>\n}
167     @parser.parse('<pre>{{foo.png}}</pre>').should == expected
168   end
169
170   it 'should have no effect inside NO_WIKI spans' do
171     expected = %Q{<p>{{foo.png}}</p>\n}
172     @parser.parse('<nowiki>{{foo.png}}</nowiki>').should == expected
173   end
174
175   it 'should be passed through in internal link targets' do
176     expected = %Q{<p><a href="/wiki/%7b%7bfoo.png%7d%7d">{{foo.png}}</a></p>\n}
177     @parser.parse('[[{{foo.png}}]]').should == expected
178   end
179
180   it 'should be passed through in internal link text' do
181     expected = %Q{<p><a href="/wiki/article">{{foo.png}}</a></p>\n}
182     @parser.parse('[[article|{{foo.png}}]]').should == expected
183   end
184
185   it 'should not be allowed as an external link target' do
186     expected = %Q{<p>[<img src="/images/foo.png" alt="foo.png"> the link]</p>\n}
187     @parser.parse('[{{foo.png}} the link]').should == expected
188   end
189
190   it 'should be passed through in external link text' do
191     expected = %Q{<p><a href="http://example.com/" class="external">{{foo.png}}</a></p>\n}
192     @parser.parse('[http://example.com/ {{foo.png}}]').should == expected
193   end
194
195   it 'should not allow embedded quotes' do
196     expected = %Q{<p>{{&quot;fun&quot;.png}}</p>\n}
197     @parser.parse('{{"fun".png}}').should == expected
198   end
199
200   it 'should not allow embedded spaces' do
201     @parser.parse('{{foo bar.png}}').should == %Q{<p>{{foo bar.png}}</p>\n}
202   end
203
204   it 'should not allow characters beyond printable ASCII' do
205     @parser.parse('{{500€.png}}').should == %Q{<p>{{500&#x20ac;.png}}</p>\n}
206   end
207
208   it 'should allow overrides of the image prefix at initialization time' do
209     parser = Wikitext::Parser.new(:img_prefix => '/gfx/')
210     expected = %Q{<p><img src="/gfx/foo.png" alt="foo.png"></p>\n}
211     parser.parse('{{foo.png}}').should == expected
212   end
213
214   it 'should suppress the image prefix if passed an empty string at initialization time' do
215     parser = Wikitext::Parser.new(:img_prefix => '')
216     expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
217     parser.parse('{{foo.png}}').should == expected
218   end
219
220   it 'should suppress image prefix if passed nil at initialization time' do
221     parser = Wikitext::Parser.new(:img_prefix => nil)
222     expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
223     parser.parse('{{foo.png}}').should == expected
224   end
225
226   it 'should allow overrides of the image prefix after initialization' do
227     @parser.img_prefix = '/gfx/'
228     expected = %Q{<p><img src="/gfx/foo.png" alt="foo.png"></p>\n}
229     @parser.parse('{{foo.png}}').should == expected
230   end
231
232   it 'should suppress image if prefix set to an empty string after initialization' do
233     @parser.img_prefix = ''
234     expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
235     @parser.parse('{{foo.png}}').should == expected
236   end
237
238   it 'should suppress image if prefix set to nil after initialization' do
239     @parser.img_prefix = nil
240     expected = %Q{<p><img src="foo.png" alt="foo.png"></p>\n}
241     @parser.parse('{{foo.png}}').should == expected
242   end
243 end