Skip to content

Commit

Permalink
Optionally raise exception if unknown tag is encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
veger committed May 26, 2019
1 parent f96cb1c commit 8e2dbab
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
17 changes: 17 additions & 0 deletions lib/ruby-bbcode.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'tags/tags'
require 'ruby-bbcode/configuration'
require 'ruby-bbcode/tag_info'
require 'ruby-bbcode/tag_sifter'
require 'ruby-bbcode/tag_node'
Expand All @@ -11,6 +12,22 @@
module RubyBBCode
include ::RubyBBCode::Tags

class << self
attr_writer :configuration
end

def self.configuration
@configuration ||= Configuration.new
end

def self.reset
@configuration = Configuration.new
end

def self.configure
yield(configuration)
end

# This method converts the given text (with BBCode tags) into a HTML representation
# The escape_html parameter (default: true) escapes HTML tags that were present in the given text and therefore blocking (mallicious) HTML in the original text
# The additional_tags parameter is used to add additional BBCode tags that should be accepted
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby-bbcode/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Configuration holds RubyBBCode configuration
class Configuration
# When true unknown tags are treated as text (default), otherwise an exception is raised
attr_accessor :ignore_unknown_tags

def initialize
@ignore_unknown_tags = true
end
end
21 changes: 17 additions & 4 deletions lib/ruby-bbcode/tag_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,33 @@ def invalid_quick_param?

protected

# Returns a default info structure used by all tags
def default_tag_info(tag_info)
{
errors: [],
complete_match: tag_info[0]
}
end

# Convert the result of the TagSifter#process_text regex into a more usable hash, that is used by the rest of the parser.
# tag_info should a result of the regex of TagSifter#process_text
# Returns the tag hash
def find_tag_info(tag_info, dictionary)
ti = {}
ti[:errors] = []
ti[:complete_match] = tag_info[0]
ti = default_tag_info(tag_info)
ti[:is_tag] = (tag_info[0].start_with? '[')
if ti[:is_tag]
ti[:closing_tag] = (tag_info[2] == '/')
ti[:tag] = tag_info[3].to_sym.downcase
ti[:params] = {}
@definition = dictionary[ti[:tag]]
if (tag_info[5][0] == '=') && can_have_quick_param?
if !tag_in_dictionary?
# Tag is not defined in dictionary, so treat as text
raise "unknown tag #{ti[:tag]}" unless RubyBBCode.configuration.ignore_unknown_tags

ti = default_tag_info(tag_info)
ti[:is_tag] = false
ti[:text] = tag_info[0]
elsif (tag_info[5][0] == '=') && can_have_quick_param?
quick_param = tag_info[5][1..-1]
# Get list of parameter values and add them as (regular) parameters
value_array = quick_param.scan(@definition[:quick_param_format])[0]
Expand Down
14 changes: 14 additions & 0 deletions test/ruby_bbcode_bbcode_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'test_helper'

class RubyBbcodeBbcodeTest < Minitest::Test
def before_setup
RubyBBCode.reset
end

def test_multiline
assert_equal "line1\nline2", "line1\nline2".bbcode_show_errors
assert_equal "line1\r\nline2", "line1\r\nline2".bbcode_show_errors
Expand Down Expand Up @@ -182,6 +186,16 @@ def test_missing_parent_tags
assert_equal '<span class=\'bbcode_error\' data-bbcode-errors=\'["[li] can only be used in [ul] and [ol]"]\'>[li]</span>[/li]', '[li][/li]'.bbcode_show_errors
end

def test_unknown_tag
RubyBBCode.configuration.ignore_unknown_tags = false
assert_raises RuntimeError do
'[unknown]This is an unknown tag[/unknown]'.bbcode_show_errors
end

RubyBBCode.configuration.ignore_unknown_tags = true
assert_equal '[unknown]This is an unknown tag[/unknown]', '[unknown]This is an unknown tag[/unknown]'.bbcode_show_errors
end

def test_illegal_unallowed_childs
assert_equal '[ul]<span class=\'bbcode_error\' data-bbcode-errors=\'["[ul] can only contain [li] and [*] tags, so &quot;Illegal text&quot; is not allowed"]\'>Illegal text</span>[/ul]', '[ul]Illegal text[/ul]'.bbcode_show_errors
assert_equal '[ul]<span class=\'bbcode_error\' data-bbcode-errors=\'["[ul] can only contain [li] and [*] tags, so [b] is not allowed"]\'>[b]</span>Illegal tag[/b][/ul]', '[ul][b]Illegal tag[/b][/ul]'.bbcode_show_errors
Expand Down
14 changes: 14 additions & 0 deletions test/ruby_bbcode_html_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'test_helper'

class RubyBbcodeHtmlTest < Minitest::Test
def before_setup
RubyBBCode.reset
end

def test_multiline
assert_equal "line1<br />\nline2", "line1\nline2".bbcode_to_html
assert_equal "line1<br />\nline2", "line1\r\nline2".bbcode_to_html
Expand Down Expand Up @@ -205,6 +209,16 @@ def test_vimeo_tag
'[vimeo width=640 height=480]46141955[/vimeo]'.bbcode_to_html
end

def test_unknown_tag
RubyBBCode.configuration.ignore_unknown_tags = false
assert_raises RuntimeError do
'[unknown]This is an unknown tag[/unknown]'.bbcode_to_html
end

RubyBBCode.configuration.ignore_unknown_tags = true
assert_equal '[unknown]This is an unknown tag[/unknown]', '[unknown]This is an unknown tag[/unknown]'.bbcode_to_html
end

def test_raised_exceptions
# Test whether exceptions are raised when the BBCode contains errors
assert_raises RuntimeError do
Expand Down
4 changes: 4 additions & 0 deletions test/ruby_bbcode_validity_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'test_helper'

class RubyBbcodeValidityTest < Minitest::Test
def before_setup
RubyBBCode.reset
end

def test_multiple_errors
input = '[b]Bold not closed, [li]Illegal list item[/li]'
errors = input.bbcode_check_validity
Expand Down

0 comments on commit 8e2dbab

Please sign in to comment.