Skip to content

Commit

Permalink
Fix tests for bbquote -> bbquote! method.
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Feb 5, 2010
1 parent 46c26da commit a11f7ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 8 additions & 9 deletions app/helpers/application_helper.rb
Expand Up @@ -11,13 +11,15 @@ def parse_text(text)

# All this sanitization is probably quite draining. Needs refactoring.
if !text.include?("[code")
text = h(text)
# We would use h here but it escapes quotes, which we need for bbquotes.
# This is the code it uses anyway.
text = text.gsub(/&/, "&amp;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
end
text.gsub!(/(.*?)\[code=?["']?(.*?)["']?\](.*?)\[\/code\](.*?)/mis) { h($1.to_s) + "[code='#{$2}']#{$3}[/code]" + h($4)}
text.gsub!(/\[code=?["']?(.*?)["']?\](.*?)\[\/code\]/mis) { "<strong>#{t(:Code)}:</strong><pre>#{clean_code($2)}</pre>"}

# allows for nested quotes
bbquote(text)
## Quoting
bbquote!(text)

# Parse all similar tags
bbcode("img", "<img src='", "'>") { text }
Expand All @@ -31,7 +33,6 @@ def parse_text(text)
# handle newlines
text.gsub!(/(.*)(\r)+?\n/) { $1 << "<br />\n" }


# handle with care...
bbcode_ext(text)
end
Expand All @@ -45,18 +46,16 @@ def clean_code(text)
text.gsub(/^\r\n/, '').gsub(/\r\n$/, '').gsub("<", "&lt;").gsub(">", "&gt;")
end

# FIXME: this should be named bbquote! with a ! because it blasts the argument out of the water. Or change it to not modify argument
def bbquote(text)
# text = text.dup # uncomment this line (please!) if you don't want it to change the argument
def bbquote!(text)
if md = text.match( /\[\s*quote=(["'])?([^\1\]]+)(\1)\s*\](?!\[\s*quote=(["'])?([^\1\]]+)(\1)\s*\])(.*?)\[\s*\/\s*quote\s*\]/i )
first, last = md.offset(0)[0], md.offset(0)[1]-1
name, content = md[2], md[7]
text[first..last] = content_tag(:div,
content_tag(:b,"%s wrote:" % name) +
content_tag(:strong,"%s wrote:" % name) +
tag(:br) +
content_tag(:span, content),
:class => 'quote')
bbquote(text)
bbquote!(text)
else
text
end
Expand Down
14 changes: 9 additions & 5 deletions spec/helpers/application_helper_spec.rb
@@ -1,9 +1,5 @@
require File.dirname(__FILE__) + '/../spec_helper'

class TestView < ActionView::Base
include ApplicationHelper
end

describe ApplicationHelper, "general" do
include ApplicationHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
Expand All @@ -20,8 +16,16 @@ class TestView < ActionView::Base
breadcrumb(@sub_of_everybody).should eql("<a href=\"/categories/#{@everybody.category.id}/forums\">Public Category</a> &raquo; <a href=\"/forums/#{@everybody.id}\">Public Forum</a> &raquo; <a href=\"/forums/#{@sub_of_everybody.id}\">Sub of Public Forum</a>")
end

it "should accept quotes with single quotes around the name" do
parse_text("[quote='Radar']This is a quote[/quote]").should eql("<div class=\"quote\"><strong>Radar wrote:</strong><br /><span>This is a quote</span></div>")
end

it "should accept quotes with double quotes around the name" do
parse_text("[quote=\"Radar\"]This is a quote[/quote]").should eql("<div class=\"quote\"><strong>Radar wrote:</strong><br /><span>This is a quote</span></div>")
end

it "should correctly display multiple nested quotes" do
TestView.new.parse_text('[quote="Kitten"][quote="Dog"][quote="Turtle"]turtle, turtle[/quote]QUOTE INSIDE[/quote]QUOTE OUTSIDE[/quote]').should eql("<div class='quote'><b>&quot;Kitten&quot; wrote:</b><br /><span><div class='quote'><b>&quot;Dog&quot; wrote:</b><br /><span><div class='quote'><b>&quot;Turtle&quot; wrote:</b><br /><span>turtle, turtle</span></div>QUOTE INSIDE</span></div>QUOTE OUTSIDE</span></div>")
parse_text('[quote="Kitten"][quote="Dog"][quote="Turtle"]turtle, turtle[/quote]QUOTE INSIDE[/quote]QUOTE OUTSIDE[/quote]').should eql("<div class=\"quote\"><strong>Kitten wrote:</strong><br /><span><div class=\"quote\"><strong>Dog wrote:</strong><br /><span><div class=\"quote\"><strong>Turtle wrote:</strong><br /><span>turtle, turtle</span></div>QUOTE INSIDE</span></div>QUOTE OUTSIDE</span></div>")
end

it "correctly formats the bbcode when it contains some code blocks" do
Expand Down

0 comments on commit a11f7ca

Please sign in to comment.