Skip to content

Commit

Permalink
Treat unknown tags as text
Browse files Browse the repository at this point in the history
  • Loading branch information
veger committed May 26, 2019
1 parent cdf7002 commit bf051e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/ruby-bbcode/tag_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,31 @@ 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
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
4 changes: 4 additions & 0 deletions test/ruby_bbcode_bbcode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ 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
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
4 changes: 4 additions & 0 deletions test/ruby_bbcode_html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ def test_vimeo_tag
'[vimeo width=640 height=480]46141955[/vimeo]'.bbcode_to_html
end

def test_unknown_tag
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

0 comments on commit bf051e7

Please sign in to comment.