]> git.wincent.com - wikitext.git/commitdiff
Many more specs for internal link edge cases
authorWincent Colaiuta <win@wincent.com>
Wed, 6 Feb 2008 15:36:00 +0000 (16:36 +0100)
committerWincent Colaiuta <win@wincent.com>
Wed, 6 Feb 2008 15:36:00 +0000 (16:36 +0100)
Signed-off-by: Wincent Colaiuta <win@wincent.com>
spec/internal_link_spec.rb
spec/link_encoding_spec.rb
spec/link_sanitizing_spec.rb

index 569e39b923358e4157ec558778a16a264fd97286..69862eeba06e16a225b8eb0a07c3f425d6e81edc 100755 (executable)
@@ -44,6 +44,34 @@ describe Wikitext::Parser, 'internal links' do
     @parser.parse('[[foo bar]]').should == %Q{<p><a href="/wiki/foo%20bar">foo bar</a></p>\n}
   end
 
+  it 'should trim leading whitespace' do
+    @parser.parse('[[ foo]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[  foo]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[   foo]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[    foo]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+  end
+
+  it 'should trim trailing whitespace' do
+    @parser.parse('[[foo ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[foo  ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[foo   ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[foo    ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}   # was a bug (exception)
+    @parser.parse('[[foo     ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}  # was a bug (crash)
+  end
+
+  it 'should trim leading and trailing whitespace (combined)' do
+    @parser.parse('[[ foo    ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[  foo   ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[   foo  ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+    @parser.parse('[[    foo ]]').should == %Q{<p><a href="/wiki/foo">foo</a></p>\n}
+  end
+
+  it 'should leave embedded whitespace intact' do
+    @parser.parse('[[ foo bar ]]').should == %Q{<p><a href="/wiki/foo%20bar">foo bar</a></p>\n}
+    @parser.parse('[[foo bar ]]').should == %Q{<p><a href="/wiki/foo%20bar">foo bar</a></p>\n}
+    @parser.parse('[[ foo bar ]]').should == %Q{<p><a href="/wiki/foo%20bar">foo bar</a></p>\n}
+  end
+
   it 'should encode and sanitize quotes' do
     # note how percent encoding is used in the href, and named entities in the link text
     @parser.parse('[[hello "world"]]').should == %Q{<p><a href="/wiki/hello%20%22world%22">hello &quot;world&quot;</a></p>\n}
@@ -202,12 +230,54 @@ describe Wikitext::Parser, 'internal links' do
       it 'should rollback and show the unterminated link' do
         @parser.parse('[[foo').should == %Q{<p>[[foo</p>\n}
       end
+
+      it 'should not trim leading whitespace when rolling back' do
+        @parser.parse('[[ foo').should    == %Q{<p>[[ foo</p>\n}
+        @parser.parse('[[  foo').should   == %Q{<p>[[  foo</p>\n}
+        @parser.parse('[[   foo').should  == %Q{<p>[[   foo</p>\n}
+        @parser.parse('[[    foo').should == %Q{<p>[[    foo</p>\n}
+      end
+
+      it 'should not trim trailing whitespace when rolling back' do
+        @parser.parse('[[foo ').should    == %Q{<p>[[foo </p>\n}
+        @parser.parse('[[foo  ').should   == %Q{<p>[[foo  </p>\n}
+        @parser.parse('[[foo   ').should  == %Q{<p>[[foo   </p>\n}
+        @parser.parse('[[foo    ').should == %Q{<p>[[foo    </p>\n}
+      end
+
+      it 'should not trim leadig and trailing whitespace (combined) when rolling back' do
+        @parser.parse('[[    foo ').should == %Q{<p>[[    foo </p>\n}
+        @parser.parse('[[   foo  ').should == %Q{<p>[[   foo  </p>\n}
+        @parser.parse('[[  foo   ').should == %Q{<p>[[  foo   </p>\n}
+        @parser.parse('[[ foo    ').should == %Q{<p>[[ foo    </p>\n}
+      end
     end
 
     describe 'unterminated link targets (end-of-line)' do
       it 'should rollback and show the unterminated link' do
         @parser.parse("[[foo\n").should == %Q{<p>[[foo</p>\n}
       end
+
+      it 'should not trim leading whitespace when rolling back' do
+        @parser.parse("[[ foo\n").should    == %Q{<p>[[ foo</p>\n}
+        @parser.parse("[[  foo\n").should   == %Q{<p>[[  foo</p>\n}
+        @parser.parse("[[   foo\n").should  == %Q{<p>[[   foo</p>\n}
+        @parser.parse("[[    foo\n").should == %Q{<p>[[    foo</p>\n}
+      end
+
+      it 'should not trim trailing whitespace when rolling back' do
+        @parser.parse("[[foo \n").should    == %Q{<p>[[foo </p>\n}
+        @parser.parse("[[foo  \n").should   == %Q{<p>[[foo  </p>\n}
+        @parser.parse("[[foo   \n").should  == %Q{<p>[[foo   </p>\n}
+        @parser.parse("[[foo    \n").should == %Q{<p>[[foo    </p>\n}
+      end
+
+      it 'should not trim leading and trailing whitespace (combined) when rolling back' do
+        @parser.parse("[[ foo    \n").should == %Q{<p>[[ foo    </p>\n}
+        @parser.parse("[[  foo   \n").should == %Q{<p>[[  foo   </p>\n}
+        @parser.parse("[[   foo  \n").should == %Q{<p>[[   foo  </p>\n}
+        @parser.parse("[[    foo \n").should == %Q{<p>[[    foo </p>\n}
+      end
     end
 
     describe 'missing link text' do
index d1aad29fc7c86a1b315ee42c3baf515fb7c6dc85..d2b62d6d0ae67fc8ca74ec1b7982fd9d1b6838f4 100755 (executable)
@@ -35,6 +35,8 @@ describe Wikitext, 'encoding a link target' do
     Wikitext::Parser.encode_link_target('  hello world').should == 'hello%20world'
     Wikitext::Parser.encode_link_target('   hello world').should == 'hello%20world'
     Wikitext::Parser.encode_link_target('    hello world').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('     hello world').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('      hello world').should == 'hello%20world'
   end
 
   it 'should eat trailing spaces' do
@@ -42,13 +44,17 @@ describe Wikitext, 'encoding a link target' do
     Wikitext::Parser.encode_link_target('hello world  ').should == 'hello%20world'
     Wikitext::Parser.encode_link_target('hello world   ').should == 'hello%20world'
     Wikitext::Parser.encode_link_target('hello world    ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('hello world     ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('hello world      ').should == 'hello%20world'
   end
 
   it 'should eat leading and trailing spaces combined' do
-    Wikitext::Parser.encode_link_target('    hello world ').should == 'hello%20world'
-    Wikitext::Parser.encode_link_target('   hello world  ').should == 'hello%20world'
-    Wikitext::Parser.encode_link_target('  hello world   ').should == 'hello%20world'
-    Wikitext::Parser.encode_link_target(' hello world    ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('     hello world ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('     hello world ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('    hello world  ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('   hello world   ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target('  hello world    ').should == 'hello%20world'
+    Wikitext::Parser.encode_link_target(' hello world     ').should == 'hello%20world'
   end
 
   it 'should return nothing for input consisting entirely of spaces' do
@@ -56,6 +62,8 @@ describe Wikitext, 'encoding a link target' do
     Wikitext::Parser.encode_link_target('  ').should == ''
     Wikitext::Parser.encode_link_target('   ').should == ''
     Wikitext::Parser.encode_link_target('    ').should == ''
+    Wikitext::Parser.encode_link_target('     ').should == ''
+    Wikitext::Parser.encode_link_target('      ').should == ''
   end
 
   it 'should convert reserved symbols into percent escapes' do
index 23556386c4176e3f7c06a00ebfbc23ecf4e7c148..cccc3675b6e29d15a4f8264389bf30a09ea27334 100755 (executable)
@@ -42,6 +42,8 @@ describe Wikitext, 'sanitizing a link target' do
     Wikitext::Parser.sanitize_link_target('  hello world').should == 'hello world'
     Wikitext::Parser.sanitize_link_target('   hello world').should == 'hello world'
     Wikitext::Parser.sanitize_link_target('    hello world').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('     hello world').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('      hello world').should == 'hello world'
   end
 
   it 'should eat trailing spaces' do
@@ -49,13 +51,21 @@ describe Wikitext, 'sanitizing a link target' do
     Wikitext::Parser.sanitize_link_target('hello world  ').should == 'hello world'
     Wikitext::Parser.sanitize_link_target('hello world   ').should == 'hello world'
     Wikitext::Parser.sanitize_link_target('hello world    ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('hello world     ').should == 'hello world'   # was a crasher
+    Wikitext::Parser.sanitize_link_target('hello world      ').should == 'hello world'  # was a crasher
+
+    # same but with lots of entities to force a reallocation (we were crashing under reallocation)
+    expected = '&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;'
+    Wikitext::Parser.sanitize_link_target('""""""""""      ').should == expected
   end
 
   it 'should eat leading and trailing spaces combined' do
-    Wikitext::Parser.sanitize_link_target(' hello world    ').should == 'hello world'
-    Wikitext::Parser.sanitize_link_target('  hello world   ').should == 'hello world'
-    Wikitext::Parser.sanitize_link_target('   hello world  ').should == 'hello world'
-    Wikitext::Parser.sanitize_link_target('    hello world ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target(' hello world     ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('  hello world    ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('   hello world   ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('    hello world  ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('     hello world  ').should == 'hello world'
+    Wikitext::Parser.sanitize_link_target('      hello world ').should == 'hello world'
   end
 
   it 'should return nothing for input consisting entirely of spaces' do
@@ -63,6 +73,8 @@ describe Wikitext, 'sanitizing a link target' do
     Wikitext::Parser.sanitize_link_target('  ').should == ''
     Wikitext::Parser.sanitize_link_target('   ').should == ''
     Wikitext::Parser.sanitize_link_target('    ').should == ''
+    Wikitext::Parser.sanitize_link_target('     ').should == ''
+    Wikitext::Parser.sanitize_link_target('      ').should == ''
   end
 
   it 'should convert double quotes into named entities' do