2 # Copyright 2008-2010 Wincent Colaiuta. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
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.
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.
25 require File.join(File.dirname(__FILE__), 'spec_helper.rb')
27 describe Wikitext::Parser, 'embedding img tags' do
29 @parser = Wikitext::Parser.new
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
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
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
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
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}
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
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
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
77 it 'should pass through empty image tags unchanged' do
78 @parser.parse('{{}}').should == %Q{<p>{{}}</p>\n}
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
86 it 'should work in BLOCKQUOTE blocks' do
87 expected = dedent <<-END
89 <p><img src="/images/foo.png" alt="foo.png"></p>
92 @parser.parse('> {{foo.png}}').should == expected
95 it 'should work in unordered lists' do
101 expected = dedent <<-END
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>
108 @parser.parse(input).should == expected
111 it 'should work in ordered lists' do
112 input = dedent <<-END
117 expected = dedent <<-END
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>
124 @parser.parse(input).should == expected
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
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
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
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
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
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
157 it 'should pass single curly braces through unaltered' do
158 @parser.parse('{foo.png}').should == %Q{<p>{foo.png}</p>\n}
161 it 'should have no effect inside PRE blocks' do
162 @parser.parse(' {{foo.png}}').should == %Q{<pre>{{foo.png}}</pre>\n}
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
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
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
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
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
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
195 it 'should not allow embedded quotes' do
196 expected = %Q{<p>{{"fun".png}}</p>\n}
197 @parser.parse('{{"fun".png}}').should == expected
200 it 'should not allow embedded spaces' do
201 @parser.parse('{{foo bar.png}}').should == %Q{<p>{{foo bar.png}}</p>\n}
204 it 'should not allow characters beyond printable ASCII' do
205 @parser.parse('{{500€.png}}').should == %Q{<p>{{500€.png}}</p>\n}
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
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
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
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
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
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