test/parser_test.rb in trac-wiki-0.1.12 vs test/parser_test.rb in trac-wiki-0.2.16
- old
+ new
@@ -1,13 +1,32 @@
+# encoding: UTF-8
require 'trac-wiki'
require 'pp'
class Bacon::Context
def tc(html, wiki, options = {})
+ #options[:template_handler] = proc {|tname,env| template_handler(tname, env) }
+ options[:template_handler] = self.method(:template_handler)
TracWiki.render(wiki, options).should.equal html
end
+
+ def template_handler(tname, env)
+ case tname
+ when 'test'
+ "{{west}}"
+ when 'west'
+ "WEST"
+ when 'deep'
+ "{{deep}}"
+ when 'wide'
+ "0123456789{{wide}}" * 10
+ else
+ nil
+ #"UNK_TEMPL(#{tname})"
+ end
+ end
def h(hash, wiki, opts = {})
parser = TracWiki.parser(wiki, opts)
parser.to_html
#pp parser.headers
parser.headings.should == hash
@@ -56,11 +75,12 @@
end
it 'should be toc' do
tc "<p>{{toc}}</p>\n", "{{toc}}"
tc "<h2>ahoj</h2><p>{{toc}}</p>\n<h2>ahoj</h2>", "==ahoj==\n{{toc}}\n\n==ahoj==\n"
- tc "<h2>ahoj</h2><p>{{toc}}</p>\n<h2>ahoj</h2>", "==ahoj==\r\n{{toc}}\r\n\r\n==ahoj==\r\n"
+ #tc "{{toc}}", "{{toc}}"
+ #tc "<h2>ahoj</h2>{{toc}}<h2>ahoj</h2>", "==ahoj==\r\n{{toc}}\r\n\r\n==ahoj==\r\n"
end
it 'should parse bolditalic' do
tc "<p>This is <strong><em>bolditallic</em></strong>.</p>\n", "This is '''''bolditallic'''''."
tc "<p>This is <strong> <em>bolditallic</em> </strong>.</p>\n", "This is ''' ''bolditallic'' '''."
@@ -68,10 +88,11 @@
tc "<p>This is <strong>bold</strong>.</p>\n", "This is '''bold'''."
#fuj tc '<p>This is <strong><em>bolditallic</em></strong>.</p>\n', "This is **''bolditallic**''."
end
it 'should parse monospace' do
tc "<p>This is <tt>monospace</tt>.</p>\n", "This is {{{monospace}}}."
+ tc "<p>This is not {{{monospace}}}.</p>\n", "This is not !{{{monospace}}}."
tc "<p>This is <tt>mon**o**space</tt>.</p>\n", "This is {{{mon**o**space}}}."
tc "<p>This is <tt>mon<o>space</tt>.</p>\n", "This is {{{mon<o>space}}}."
tc "<p>This is <tt>mon''o''space</tt>.</p>\n", "This is {{{mon''o''space}}}."
tc "<p>This is <tt>mon''o''space</tt>.</p>\n", "This is `mon''o''space`."
tc "<p>This is <tt>mon{{o}}space</tt>.</p>\n", "This is {{{mon{{o}}space}}}."
@@ -147,10 +168,11 @@
it 'should parse headings' do
# Only three differed sized levels of heading are required.
tc "<h1>Heading 1</h1>", "= Heading 1 ="
tc "<h2>Heading 2</h2>", "== Heading 2 =="
tc "<h3>Heading 3</h3>", "=== Heading 3 ==="
+ tc "<h3>Heading 3\u00a0B</h3>", "=== Heading 3~B ==="
tc "<h3 id=\"HE3\">Heading 3</h3>", "=== Heading 3 === #HE3"
tc "<h3 id=\"Heading-3\">Heading 3</h3>", "=== Heading 3 === #Heading-3"
tc "<h3 id=\"Heading/3\">Heading 3</h3>", "=== Heading 3 === #Heading/3"
tc "<h3 id=\"Heading/3\">Heading 3</h3>", "=== Heading 3 === #Heading/3 "
tc "<h3 id=\"Heading/3\">Heading 3</h3><h3 id=\"Heading/3.2\">Heading 3</h3>",
@@ -190,10 +212,12 @@
end
it 'should parse links' do
# Links
tc "<p><a href=\"link\">link</a></p>\n", "[[link]]"
+ tc "<p><a href=\"BASE/link\">link</a></p>\n", "[[link]]", base: 'BASE'
+ tc "<p><a href=\"BASE/link\">link</a></p>\n", "[[link]]", base: 'BASE/'
tc "<p><a href=\"link#link\">link#link</a></p>\n", "[[link#link]]"
tc "<p><a href=\"#link\">#link</a></p>\n", "[[#link]]"
# Links can appear in paragraphs (i.e. inline item)
tc "<p>Hello, <a href=\"world\">world</a></p>\n", "Hello, [[world]]"
@@ -215,11 +239,11 @@
tc("<p><a href=\"http://www.example.org/\">Visit the Example website</a></p>\n",
"[[http://www.example.org/|Visit the Example website]]")
# WRNING: Parsing markup within a link is optional
tc "<p><a href=\"Weird+Stuff\"><strong>Weird</strong> <em>Stuff</em></a></p>\n", "[[Weird Stuff|**Weird** ''Stuff'']]"
- #tc("<p><a href=\"http://example.org/\"><img src='image.jpg'/></a></p>\n", "[[http://example.org/|{{image.jpg}}]]")
+ #tc("<p><a href=\"http://example.org/\"><img src=\"image.jpg\"/></a></p>\n", "[[http://example.org/|{{image.jpg}}]]")
# Inside bold
tc "<p><strong><a href=\"link\">link</a></strong></p>\n", "**[[link]]**"
# Whitespace inside [[ ]] should be ignored
@@ -250,10 +274,11 @@
end
it 'should parse paragraphs' do
# One or more blank lines end paragraphs.
tc "<p>This is my text.</p>\n<p>This is more text.</p>\n", "This is\nmy text.\n\nThis is\nmore text."
+ tc "<p>This is my text.</p>\n<p>This is more text.</p>\n", "This is \nmy text.\n\nThis is\nmore text."
tc "<p>This is my text.</p>\n<p>This is more text.</p>\n", "This is\nmy text.\n\n\nThis is\nmore text."
tc "<p>This is my text.</p>\n<p>This is more text.</p>\n", "This is\nmy text.\n\n\n\nThis is\nmore text."
# A list end paragraphs too.
tc "<p>Hello</p>\n<ul><li>Item</li></ul>", "Hello\n* Item\n"
@@ -457,10 +482,11 @@
end
it 'should parse ambiguious bold and lists' do
tc "<p><strong> bold text </strong></p>\n", "** bold text **"
tc "<p><blockquote><strong> bold text </strong></blockquote></p>\n", " ** bold text **"
+ tc "<p><blockquote><strong> bold text </strong></blockquote></p>\n", " ** bold\ntext **"
end
it 'should parse nowiki' do
# ... works as block
tc "<pre>Hello</pre>", "{{{\nHello\n}}}\n"
@@ -496,23 +522,23 @@
tc("<p><b>not bold</b></p>\n", "<b>not bold</b>")
# Image tags should be escape
tc("<p><img src=\"image.jpg\"/></p>\n", "[[Image(image.jpg)]]")
tc("<p><img src=\"image.jpg\"/></p>\n", "[[Image(image.jpg)]]", :no_link=>true)
- tc("<p><img src=\"image.jpg\" alt=\"a%22tag%22\"/></p>\n", "[[Image(image.jpg,alt=a\"tag\")]]")
- tc("<p><img src=\"image.jpg\" alt=\"a%22tag%22\"/></p>\n", "[[Image(image.jpg,alt=a\"tag\")]]", :no_link=>true)
+ tc("<p><img alt=\"a%22tag%22\" src=\"image.jpg\"/></p>\n", "[[Image(image.jpg,alt=a\"tag\")]]")
+ tc("<p><img alt=\"a%22tag%22\" src=\"image.jpg\"/></p>\n", "[[Image(image.jpg,alt=a\"tag\")]]", :no_link=>true)
# Malicious links should not be converted.
tc("<p><a href=\"javascript%3Aalert%28%22Boo%21%22%29\">Click</a></p>\n", "[[javascript:alert(\"Boo!\")|Click]]")
end
it 'should support character escape' do
tc "<p>** Not Bold **</p>\n", "!** Not Bold !**"
tc "<p>// Not Italic //</p>\n", "!// Not Italic !//"
tc "<p>* Not Bullet</p>\n", "!* Not Bullet"
# Following char is not a blank (space or line feed)
- tc "<p>Hello ~ world</p>\n", "Hello ~ world\n"
+ tc "<p>Hello \u00a0 world</p>\n", "Hello ~ world\n"
tc "<p>Hello ! world</p>\n", "Hello ! world\n"
tc "<p>Hello ! world</p>\n", "Hello ! world\n"
tc "<p>Hello ! world</p>\n", "Hello !\nworld\n"
# Not escaping inside URLs
tc "<p><a href=\"http://example.org/~user/\">http://example.org/~user/</a></p>\n", "http://example.org/~user/"
@@ -548,23 +574,23 @@
it 'should parse table' do
tc "<table><tr><td>Hello</td><td>World!</td></tr></table>", "||Hello||World!||"
tc "<table><tr><td>Hello</td><td>World!</td></tr></table>", "||Hello||\\\n||World!||"
tc "<table><tr><td>He</td><td>llo</td><td>World!</td></tr></table>", "||He||llo||\\\n||World!||"
- tc "<table><tr><td>Hello</td><td colspan='2'>World!</td></tr></table>", "||Hello||||World!||"
- tc "<table><tr><td>Hello</td><td colspan='2'>kuk</td><td>World!</td></tr></table>", "||Hello||||kuk||\\\n||World!||"
- tc "<table><tr><td>1</td><td>2</td><td>3</td></tr><tr><td colspan='2'>1-2</td><td>3</td></tr><tr><td>1</td><td colspan='2'>2-3</td></tr><tr><td colspan='3'>1-2-3</td></tr></table>", "|| 1 || 2 || 3 ||\n|||| 1-2 || 3 ||\n|| 1 |||| 2-3 ||\n|||||| 1-2-3 ||\n"
+ tc "<table><tr><td>Hello</td><td colspan=\"2\">World!</td></tr></table>", "||Hello||||World!||"
+ tc "<table><tr><td>Hello</td><td colspan=\"2\">kuk</td><td>World!</td></tr></table>", "||Hello||||kuk||\\\n||World!||"
+ tc "<table><tr><td>1</td><td>2</td><td>3</td></tr><tr><td colspan=\"2\">1-2</td><td>3</td></tr><tr><td>1</td><td colspan=\"2\">2-3</td></tr><tr><td colspan=\"3\">1-2-3</td></tr></table>", "|| 1 || 2 || 3 ||\n|||| 1-2 || 3 ||\n|| 1 |||| 2-3 ||\n|||||| 1-2-3 ||\n"
- tc "<table><tr><td>table</td><td style='text-align:center'>center</td></tr></table>", "||table|| center ||"
- tc "<table><tr><td>table</td><td style='text-align:right'>right</td></tr></table>", "||table|| right||"
- tc "<table><tr><td>table</td><td style='text-align:center'>center</td><td style='text-align:right'>right</td></tr></table>", "||table|| center || right||"
+ tc "<table><tr><td>table</td><td style=\"text-align:center\">center</td></tr></table>", "||table|| center ||"
+ tc "<table><tr><td>table</td><td style=\"text-align:right\">right</td></tr></table>", "||table|| right||"
+ tc "<table><tr><td>table</td><td style=\"text-align:center\">center</td><td style=\"text-align:right\">right</td></tr></table>", "||table|| center || right||"
tc "<table><tr><td>Hello, World!</td></tr></table>", "||Hello, World!||"
- tc "<table><tr><td style='text-align:right'>Hello, Right World!</td></tr></table>", "|| Hello, Right World!||"
- tc "<table><tr><th style='text-align:right'>Hello, Right World!</th></tr></table>", "||= Hello, Right World!=||"
- tc "<table><tr><td style='text-align:center'>Hello, Centered World!</td></tr></table>", "|| Hello, Centered World! ||"
- tc "<table><tr><th style='text-align:center'>Hello, Centered World!</th></tr></table>", "||= Hello, Centered World! =||"
+ tc "<table><tr><td style=\"text-align:right\">Hello, Right World!</td></tr></table>", "|| Hello, Right World!||"
+ tc "<table><tr><th style=\"text-align:right\">Hello, Right World!</th></tr></table>", "||= Hello, Right World!=||"
+ tc "<table><tr><td style=\"text-align:center\">Hello, Centered World!</td></tr></table>", "|| Hello, Centered World! ||"
+ tc "<table><tr><th style=\"text-align:center\">Hello, Centered World!</th></tr></table>", "||= Hello, Centered World! =||"
# Multiple columns
tc "<table><tr><td>c1</td><td>c2</td><td>c3</td></tr></table>", "||c1||c2||c3||"
# Multiple rows
tc "<table><tr><td>c11</td><td>c12</td></tr><tr><td>c21</td><td>c22</td></tr></table>", "||c11||c12||\n||c21||c22||\n"
# End pipe is optional
@@ -755,20 +781,20 @@
tc("<pre>nowiki</pre><table><tr><td>table</td></tr></table>", "{{{\nnowiki\n}}}\n\n||table||\n")
end
it 'should parse image' do
tc("<p><img src=\"image.jpg\"/></p>\n", "[[Image(image.jpg)]]")
- tc("<p><img src=\"javascript%3Aimage.jpg\" alt=\"tag\"/></p>\n", "[[Image(javascript:image.jpg,alt=tag)]]")
- tc("<p><img src=\"image.jpg\" alt=\"tag\"/></p>\n", "[[Image(image.jpg,alt=tag)]]")
+ tc("<p><img alt=\"tag\" src=\"javascript%3Aimage.jpg\"/></p>\n", "[[Image(javascript:image.jpg,alt=tag)]]")
+ tc("<p><img alt=\"tag\" src=\"image.jpg\"/></p>\n", "[[Image(image.jpg,alt=tag)]]")
tc("<p><img src=\"image.jpg\" width=\"120px\"/></p>\n", "[[Image(image.jpg, 120px )]]")
tc("<p><img src=\"image.jpg\" width=\"120px\"/></p>\n", "[[Image(image.jpg, \t120px )]]")
- tc("<p><img src=\"image.jpg\" align=\"right\"/></p>\n", "[[Image(image.jpg, right)]]")
- tc("<p><img src=\"image.jpg\" align=\"right\" valign=\"top\"/></p>\n", "[[Image(image.jpg, right,top)]]")
- tc("<p><img src=\"image.jpg\" align=\"right\" valign=\"top\"/></p>\n", "[[Image(image.jpg, top,right)]]")
+ tc("<p><img align=\"right\" src=\"image.jpg\"/></p>\n", "[[Image(image.jpg, right)]]")
+ tc("<p><img align=\"right\" src=\"image.jpg\" valign=\"top\"/></p>\n", "[[Image(image.jpg, right,top)]]")
+ tc("<p><img align=\"right\" src=\"image.jpg\" valign=\"top\"/></p>\n", "[[Image(image.jpg, top,right)]]")
tc("<p><img src=\"image.jpg\" valign=\"top\"/></p>\n", "[[Image(image.jpg, top)]]")
tc("<p><img src=\"image.jpg\" valign=\"top\"/></p>\n", "[[Image(image.jpg, valign=top)]]")
- tc("<p><img src=\"image.jpg\" align=\"center\"/></p>\n", "[[Image(image.jpg, center)]]")
+ tc("<p><img align=\"center\" src=\"image.jpg\"/></p>\n", "[[Image(image.jpg, center)]]")
tc("<p><img src=\"image.jpg\" valign=\"middle\"/></p>\n", "[[Image(image.jpg, middle)]]")
tc("<p><img src=\"image.jpg\" title=\"houhouhou\"/></p>\n", "[[Image(image.jpg, title=houhouhou)]]")
tc("<p><img src=\"image.jpg\" width=\"120px\"/></p>\n", "[[Image(image.jpg,width=120px)]]")
tc("<p><img src=\"image.jpg\" width=\"120%25\"/></p>\n", "[[Image(image.jpg, width=120%)]]")
tc("<p><img src=\"image.jpg\" style=\"margin:5\"/></p>\n", "[[Image(image.jpg,margin=5)]]")
@@ -805,16 +831,16 @@
tc("<p><a href=\"a%2Fb%2Fc\">a/b/c</a></p>\n", "[[a/b/c]]")
tc("<p><a href=\"a%2Fb%2Fc\">a/b/c</a></p>\n", "[a/b/c]")
tc("<p><a href=\"a/b/c\">a/b/c</a></p>\n", "[[a/b/c]]", :no_escape => true)
end
it 'should support merge' do
- tc "<div class='merge merge-orig'>orig</div>\n", "||||||| orig", :merge => true
- tc "<div class='merge merge-mine'>mine</div>\n", "<<<<<<< mine", :merge => true
- tc "<div class='merge merge-your'>your</div>\n", ">>>>>>> your", :merge => true
- tc "<p>bhoj</p>\n<div class='merge merge-your'>your</div>\n<p>ahoj</p>\n", "bhoj\n>>>>>>> your\nahoj", :merge => true
- tc "<div class='merge merge-split'></div>\n<p>ahoj</p>\n", "=======\nahoj\n", :merge => true
- tc "<div class='merge merge-split'>split</div>\n", "======= split", :merge => true
+ tc "<div class=\"merge merge-orig\">orig</div>\n", "||||||| orig", :merge => true
+ tc "<div class=\"merge merge-mine\">mine</div>\n", "<<<<<<< mine", :merge => true
+ tc "<div class=\"merge merge-your\">your</div>\n", ">>>>>>> your", :merge => true
+ tc "<p>bhoj</p>\n<div class=\"merge merge-your\">your</div>\n<p>ahoj</p>\n", "bhoj\n>>>>>>> your\nahoj", :merge => true
+ tc "<div class=\"merge merge-split\"></div>\n<p>ahoj</p>\n", "=======\nahoj\n", :merge => true
+ tc "<div class=\"merge merge-split\">split</div>\n", "======= split", :merge => true
tc "<h6></h6><p>ahoj</p>\n", "=======\nahoj\n", :merge => false
end
it 'should compute headers' do
h( [ {:level=>0, :sline=>1, :eline=>2},
@@ -851,8 +877,54 @@
===*dhoj ===#ble
kuk
eos
+
+ it 'should support macro' do
+ tc "<p>ahoj</p>\n" , "{{#echo \nahoj\n}}"
+ tc "<h2>H2</h2>" , "{{#echo == H2 ==}}"
+ tc "<h2>H2</h2>" , "{{#echo =={{#echo H2}}==}}"
+ tc "<h3 id=\"test\">H3</h3>" , "{{#echo =={{#echo =H3=}}=={{#echo #test}}}}"
+
+ tc "<p>This is correct</p>\n" , "This is {{# NOT}} correct"
+ tc "<h1>h1</h1>" , "{{# comment }}\n= h1 =\n"
+ tc "<h1>h1</h1>" , "{{# comment }}\n\n\n= h1 =\n"
+ tc "<h1>h1</h1>" , "{{# comment }}\n\n\n= h1 =\n{{# Comment2}}\n"
+
+ tc "<h1>h1</h1>" , "{{# co{{HUU}}mment }}\n\n\n= h1 =\n{{# Comment2}}\n"
+
+ tc "<p>UMACRO(macr ahoj )</p>\n" , "{{macr\nahoj\n}}"
+ tc "<p>ahoj UMACRO(macrUMACRO(o))</p>\n" , "ahoj {{macr{{o}}}}"
+ tc "<p>ahoj UMACRO(macro)</p>\n" , "ahoj {{macro}}"
+ tc "<p>ahoj {{%macrUMACRO(o)}}</p>\n" , "ahoj {{%macr{{o}}}}"
+ tc "<p>ahoj UMACRO(macrUMACRO(mac <strong>o</strong>))</p>\n" , "ahoj {{macr{{mac **o**}}}}"
+ tc "<p>ahoj VAR($mac)</p>\n" , "ahoj {{$mac|ahoj}}"
+ end
+
+ it 'should do temlate' do
+ tc "<p>1WEST</p>\n", "1{{west}}"
+ tc "<p>2WEST</p>\n", "2{{test}}"
+
+ # macro errors:
+ tc "<p>TOO_DEEP_RECURSION(<tt>{{deep}}</tt>) 3</p>\n", "{{deep}}3"
+ tc "<p>TOO_LONG_EXPANSION_OF_MACRO(wide)QUIT</p>\n", "{{wide}}3"
+ tc "<p>UMACRO(unknown)3</p>\n", "{{unknown}}3"
+ end
+ it 'should support options' do
+ tc "<h3>h1<a class=\"editheading\" href=\"?edit=1\">edit</a></h3>", "=== h1 ==", edit_heading: true
+ end
+ it 'should not html' do
+ tc "<p>{{{! <a></a> }}}</p>\n", "{{{!\n<a></a>\n}}}\n"
+ tc '<a></a>', "{{{!\n<a></a>\n}}}\n", raw_html: true
+ tc '<a></a>', "{{{!\n<a></a>\n}}}\n", raw_html: true
+ tc "<form meth=\"POST\" action=\"http://www.example.com\"><input type=\"hidden\" value=\"VAL\"></form>",
+ "{{{!\n<form meth=\"POST\" action=\"http://www.example.com\"><input type=\"hidden\" value=\"VAL\"/></form>\n}}}\n", raw_html: true
+ tc 'alert(444);', "{{{!\n<script>alert(444);</script>\n}}}\n", raw_html: true
+ end
+ it 'should not entity' do
+ tc "<p>\u00a0</p>\n", " "
+ tc "<p>„text“</p>\n", "„text“"
+ end
end
end
# vim: tw=0