ruby-bbcode 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,5 +2,5 @@ module RubyBBCode
2
2
  # Version of RubyBBCode
3
3
  #
4
4
  # Follows semantic versioning: http://semver.org/
5
- VERSION = "2.0.3"
5
+ VERSION = '2.1.0'.freeze
6
6
  end
@@ -6,127 +6,149 @@ module RubyBBCode
6
6
  # A single item from this file (eg the :b entry) is refered to as a @definition
7
7
  @@tags = {
8
8
  :b => {
9
- :html_open => '<strong>', :html_close => '</strong>',
10
- :description => 'Make text bold',
11
- :example => 'This is [b]bold[/b].'},
9
+ html_open: '<strong>', html_close: '</strong>',
10
+ description: 'Make text bold',
11
+ example: 'This is [b]bold[/b].'
12
+ },
12
13
  :i => {
13
- :html_open => '<em>', :html_close => '</em>',
14
- :description => 'Make text italic',
15
- :example => 'This is [i]italic[/i].'},
14
+ html_open: '<em>', html_close: '</em>',
15
+ description: 'Make text italic',
16
+ example: 'This is [i]italic[/i].'
17
+ },
16
18
  :u => {
17
- :html_open => '<u>', :html_close => '</u>',
18
- :description => 'Underline text',
19
- :example => 'This is [u]underlined[/u].'},
19
+ html_open: '<u>', html_close: '</u>',
20
+ description: 'Underline text',
21
+ example: 'This is [u]underlined[/u].'
22
+ },
20
23
  :s => {
21
- :html_open => '<span style="text-decoration:line-through;">', :html_close => '</span>',
22
- :description => 'Strike-through text',
23
- :example => 'This is [s]wrong[/s] good.'},
24
+ html_open: '<span style="text-decoration:line-through;">', html_close: '</span>',
25
+ description: 'Strike-through text',
26
+ example: 'This is [s]wrong[/s] good.'
27
+ },
24
28
  :center => {
25
- :html_open => '<div style="text-align:center;">', :html_close => '</div>',
26
- :description => 'Center a text',
27
- :example => '[center]This is centered[/center].'},
29
+ html_open: '<div style="text-align:center;">', html_close: '</div>',
30
+ description: 'Center a text',
31
+ example: '[center]This is centered[/center].'
32
+ },
28
33
  :ul => {
29
- :html_open => '<ul>', :html_close => '</ul>',
30
- :description => 'Unordered list',
31
- :example => '[ul][li]List item[/li][li]Another list item[/li][/ul].',
32
- :only_allow => [ :li, "*".to_sym ]},
34
+ html_open: '<ul>', html_close: '</ul>',
35
+ description: 'Unordered list',
36
+ block_tag: true,
37
+ example: '[ul][li]List item[/li][li]Another list item[/li][/ul].',
38
+ only_allow: [:li, '*'.to_sym]
39
+ },
33
40
  :code => {
34
- :html_open => '<pre>', :html_close => '</pre>',
35
- :description => 'Code block with mono-spaced text',
36
- :example => 'This is [code]mono-spaced code[/code].'},
41
+ html_open: '<pre>', html_close: '</pre>',
42
+ description: 'Code block with mono-spaced text',
43
+ example: 'This is [code]mono-spaced code[/code].',
44
+ block_tag: true
45
+ },
37
46
  :ol => {
38
- :html_open => '<ol>', :html_close => '</ol>',
39
- :description => 'Ordered list',
40
- :example => '[ol][li]List item[/li][li]Another list item[/li][/ol].',
41
- :only_allow => [ :li, "*".to_sym ]},
47
+ html_open: '<ol>', html_close: '</ol>',
48
+ description: 'Ordered list',
49
+ example: '[ol][li]List item[/li][li]Another list item[/li][/ol].',
50
+ block_tag: true,
51
+ only_allow: [:li, '*'.to_sym]
52
+ },
42
53
  :li => {
43
- :html_open => '<li>', :html_close => '</li>',
44
- :description => 'List item',
45
- :example => '[ul][li]List item[/li][li]Another list item[/li][/ul].',
46
- :only_in => [ :ul, :ol ]},
54
+ html_open: '<li>', html_close: '</li>',
55
+ description: 'List item',
56
+ example: '[ul][li]List item[/li][li]Another list item[/li][/ul].',
57
+ block_tag: true,
58
+ only_in: %i[ul ol]
59
+ },
47
60
  :list => {
48
- :html_open => '<ul>', :html_close => '</ul>',
49
- :description => 'Unordered list',
50
- :example => '[list][*]List item[*]Another list item[/list].',
51
- :only_allow => [ "*".to_sym ]},
52
- "*".to_sym => {
53
- :html_open => '<li>', :html_close => '</li>',
54
- :description => 'List item',
55
- :example => '[list][*]List item[*]Another list item[/list].',
56
- :self_closable => true,
57
- :only_in => [ :list, :ul, :ol ]},
61
+ html_open: '<ul>', html_close: '</ul>',
62
+ description: 'Unordered list',
63
+ example: '[list][*]List item[*]Another list item[/list].',
64
+ block_tag: true,
65
+ only_allow: ['*'.to_sym]
66
+ },
67
+ '*'.to_sym => {
68
+ html_open: '<li>', html_close: '</li>',
69
+ description: 'List item',
70
+ example: '[list][*]List item[*]Another list item[/list].',
71
+ self_closable: true, block_tag: true,
72
+ only_in: %i[list ul ol]
73
+ },
58
74
  :img => {
59
- :html_open => '<img src="%between%" %width%%height%alt="" />', :html_close => '',
60
- :description => 'Image',
61
- :example => '[img]http://www.google.com/intl/en_ALL/images/logo.gif[/img].',
62
- :only_allow => [],
63
- :require_between => true,
64
- :allow_quick_param => true, :allow_between_as_param => false,
65
- :quick_param_format => /^(\d+)x(\d+)$/,
66
- :param_tokens => [{:token => :width, :prefix => 'width="', :postfix => '" ', :optional => true },
67
- { :token => :height, :prefix => 'height="', :postfix => '" ', :optional => true } ],
68
- :quick_param_format_description => 'The image parameters \'%param%\' are incorrect, \'<width>x<height>\' excepted'},
75
+ html_open: '<img src="%between%" %width%%height%alt="" />', html_close: '',
76
+ description: 'Image',
77
+ example: '[img]http://www.google.com/intl/en_ALL/images/logo.gif[/img].',
78
+ only_allow: [],
79
+ require_between: true,
80
+ allow_quick_param: true, allow_between_as_param: false, block_tag: true,
81
+ quick_param_format: /^(\d+)x(\d+)$/,
82
+ param_tokens: [{ token: :width, prefix: 'width="', postfix: '" ', optional: true },
83
+ { token: :height, prefix: 'height="', postfix: '" ', optional: true }],
84
+ quick_param_format_description: 'The image parameters \'%param%\' are incorrect, \'<width>x<height>\' excepted'
85
+ },
69
86
  :url => {
70
- :html_open => '<a href="%url%">%between%', :html_close => '</a>',
71
- :description => 'Link to another page',
72
- :example => '[url]http://www.google.com/[/url].',
73
- :only_allow => [],
74
- :require_between => true,
75
- :allow_quick_param => true, :allow_between_as_param => true,
76
- :quick_param_format => /^((((http|https|ftp):\/\/)|\/).+)$/,
77
- :quick_param_format_description => 'The URL should start with http:// https://, ftp:// or /, instead of \'%param%\'',
78
- :param_tokens => [{ :token => :url }]},
87
+ html_open: '<a href="%url%">%between%', html_close: '</a>',
88
+ description: 'Link to another page',
89
+ example: '[url]http://www.google.com/[/url].',
90
+ require_between: true,
91
+ allow_quick_param: true, allow_between_as_param: true,
92
+ quick_param_format: %r{^((((http|https|ftp)://)|/).+)$},
93
+ quick_param_format_description: 'The URL should start with http:// https://, ftp:// or /, instead of \'%param%\'',
94
+ param_tokens: [{ token: :url }]
95
+ },
79
96
  :quote => {
80
- :html_open => '<div class="quote">%author%', :html_close => '</div>',
81
- :description => 'Quote another person',
82
- :example => '[quote]BBCode is great[/quote]',
83
- :allow_quick_param => true, :allow_between_as_param => false,
84
- :quick_param_format => /(.*)/,
85
- :param_tokens => [{:token => :author, :prefix => '<strong>', :postfix => ' wrote:</strong>', :optional => true}]},
97
+ html_open: '<div class="quote">%author%', html_close: '</div>',
98
+ description: 'Quote another person',
99
+ example: '[quote]BBCode is great[/quote]',
100
+ allow_quick_param: true, allow_between_as_param: false, block_tag: true,
101
+ quick_param_format: /(.*)/,
102
+ param_tokens: [{ token: :author, prefix: '<strong>', postfix: ' wrote:</strong>', optional: true }]
103
+ },
86
104
  :size => {
87
- :html_open => '<span style="font-size: %size%px;">', :html_close => '</span>',
88
- :description => 'Change the size of the text',
89
- :example => '[size=32]This is 32px[/size]',
90
- :allow_quick_param => true, :allow_between_as_param => false,
91
- :quick_param_format => /(\d+)/,
92
- :quick_param_format_description => 'The size parameter \'%param%\' is incorrect, a number is expected',
93
- :param_tokens => [{:token => :size}]},
105
+ html_open: '<span style="font-size: %size%px;">', html_close: '</span>',
106
+ description: 'Change the size of the text',
107
+ example: '[size=32]This is 32px[/size]',
108
+ allow_quick_param: true, allow_between_as_param: false,
109
+ quick_param_format: /(\d+)/,
110
+ quick_param_format_description: 'The size parameter \'%param%\' is incorrect, a number is expected',
111
+ param_tokens: [{ token: :size }]
112
+ },
94
113
  :color => {
95
- :html_open => '<span style="color: %color%;">', :html_close => '</span>',
96
- :description => 'Change the color of the text',
97
- :example => '[color=red]This is red[/color]',
98
- :allow_quick_param => true, :allow_between_as_param => false,
99
- :quick_param_format => /(([a-z]+)|(#[0-9a-f]{6}))/i,
100
- :param_tokens => [{:token => :color}]},
114
+ html_open: '<span style="color: %color%;">', html_close: '</span>',
115
+ description: 'Change the color of the text',
116
+ example: '[color=red]This is red[/color]',
117
+ allow_quick_param: true, allow_between_as_param: false,
118
+ quick_param_format: /(([a-z]+)|(#[0-9a-f]{6}))/i,
119
+ param_tokens: [{ token: :color }]
120
+ },
101
121
  :youtube => {
102
- :html_open => '<iframe id="player" type="text/html" width="%width%" height="%height%" src="http://www.youtube.com/embed/%between%?enablejsapi=1" frameborder="0"></iframe>', :html_close => '',
103
- :description => 'YouTube video',
104
- :example => '[youtube]E4Fbk52Mk1w[/youtube]',
105
- :only_allow => [],
106
- :url_matches => [/youtube\.com.*[v]=([^&]*)/, /youtu\.be\/([^&]*)/, /y2u\.be\/([^&]*)/],
107
- :require_between => true,
108
- :param_tokens => [
109
- { :token => :width, :optional => true, :default => 400 },
110
- { :token => :height, :optional => true, :default => 320 }
111
- ]},
122
+ html_open: '<iframe id="player" type="text/html" width="%width%" height="%height%" src="http://www.youtube.com/embed/%between%?enablejsapi=1" frameborder="0"></iframe>', html_close: '',
123
+ description: 'YouTube video',
124
+ example: '[youtube]E4Fbk52Mk1w[/youtube]',
125
+ only_allow: [],
126
+ url_matches: [/youtube\.com.*[v]=([^&]*)/, %r{youtu\.be/([^&]*)}, %r{y2u\.be/([^&]*)}],
127
+ require_between: true, block_tag: true,
128
+ param_tokens: [
129
+ { token: :width, optional: true, default: 400 },
130
+ { token: :height, optional: true, default: 320 }
131
+ ]
132
+ },
112
133
  :vimeo => {
113
- :html_open => '<iframe src="http://player.vimeo.com/video/%between%?badge=0" width="%width%" height="%height%" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
114
- :html_close => '',
115
- :description => 'Vimeo video',
116
- :example => '[vimeo]http://vimeo.com/46141955[/vimeo]',
117
- :only_allow => [],
118
- :url_matches => [/vimeo\.com\/([^&]*)/],
119
- :require_between => true,
120
- :param_tokens => [
121
- { :token => :width, :optional => true, :default => 400 },
122
- { :token => :height, :optional => true, :default => 320 }
123
- ]},
134
+ html_open: '<iframe src="http://player.vimeo.com/video/%between%?badge=0" width="%width%" height="%height%" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
135
+ html_close: '',
136
+ description: 'Vimeo video',
137
+ example: '[vimeo]http://vimeo.com/46141955[/vimeo]',
138
+ only_allow: [],
139
+ url_matches: [%r{vimeo\.com/([^&]*)}],
140
+ require_between: true, block_tag: true,
141
+ param_tokens: [
142
+ { token: :width, optional: true, default: 400 },
143
+ { token: :height, optional: true, default: 320 }
144
+ ]
145
+ },
124
146
  :media => {
125
- :multi_tag => true,
126
- :require_between => true,
127
- :supported_tags => [
128
- :youtube,
129
- :vimeo
147
+ multi_tag: true,
148
+ require_between: true,
149
+ supported_tags: %i[
150
+ youtube
151
+ vimeo
130
152
  ]
131
153
  }
132
154
  }
@@ -1,6 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class RubyBbcodeBbcodeTest < Minitest::Test
4
+ def before_setup
5
+ RubyBBCode.reset
6
+ end
4
7
 
5
8
  def test_multiline
6
9
  assert_equal "line1\nline2", "line1\nline2".bbcode_show_errors
@@ -47,14 +50,17 @@ class RubyBbcodeBbcodeTest < Minitest::Test
47
50
 
48
51
  def test_ordered_list
49
52
  assert_equal '[ol][li]item 1[/li][li]item 2[/li][/ol]', '[ol][li]item 1[/li][li]item 2[/li][/ol]'.bbcode_show_errors
53
+ assert_equal "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]", "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]".bbcode_show_errors
50
54
  end
51
55
 
52
56
  def test_unordered_list
53
57
  assert_equal '[ul][li]item 1[/li][li]item 2[/li][/ul]', '[ul][li]item 1[/li][li]item 2[/li][/ul]'.bbcode_show_errors
58
+ assert_equal "[ul]\n\t[li]item 1[/li]\n\t[li]item 2[/li]\n[/ul]", "[ul]\n\t[li]item 1[/li]\n\t[li]item 2[/li]\n[/ul]".bbcode_show_errors
54
59
  end
55
60
 
56
61
  def test_list_common_syntax
57
62
  assert_equal '[list][*]item 1[/*][*]item 2[/*][/list]', '[list][*]item 1[*]item 2[/list]'.bbcode_show_errors
63
+ assert_equal "[list]\n[*]item 1[/*]\n[*]item 2[/*]\n[/list]", "[list]\n[*]item 1\n[*]item 2\n[/list]".bbcode_show_errors
58
64
  end
59
65
 
60
66
  def test_list_common_syntax_explicit_closing
@@ -63,51 +69,53 @@ class RubyBbcodeBbcodeTest < Minitest::Test
63
69
 
64
70
  def test_two_lists
65
71
  assert_equal '[ul][li]item1[/li][li]item2[/li][/ul][ul][li]item1[/li][li]item2[/li][/ul]',
66
- '[ul][li]item1[/li][li]item2[/li][/ul][ul][li]item1[/li][li]item2[/li][/ul]'.bbcode_show_errors
72
+ '[ul][li]item1[/li][li]item2[/li][/ul][ul][li]item1[/li][li]item2[/li][/ul]'.bbcode_show_errors
67
73
  end
68
74
 
69
75
  def test_whitespace_in_only_allowed_tags
70
76
  assert_equal "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]",
71
- "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]".bbcode_show_errors
77
+ "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]".bbcode_show_errors
72
78
  assert_equal "[ol] [li]item 1[/li] [li]item 2[/li]\t[/ol]",
73
- "[ol] [li]item 1[/li] [li]item 2[/li]\t[/ol]".bbcode_show_errors
79
+ "[ol] [li]item 1[/li] [li]item 2[/li]\t[/ol]".bbcode_show_errors
74
80
  end
75
81
 
76
82
  def test_quote
77
83
  assert_equal '[quote]quoting[/quote]', '[quote]quoting[/quote]'.bbcode_show_errors
84
+ assert_equal "[quote]\nquoting\n[/quote]", "[quote]\nquoting\n[/quote]".bbcode_show_errors
78
85
  assert_equal '[quote author=someone]quoting[/quote]', '[quote=someone]quoting[/quote]'.bbcode_show_errors
79
86
  assert_equal '[quote author=Kitten][quote author=creatiu]f1[/quote]f2[/quote]',
80
- '[quote author=Kitten][quote=creatiu]f1[/quote]f2[/quote]'.bbcode_show_errors
87
+ '[quote author=Kitten][quote=creatiu]f1[/quote]f2[/quote]'.bbcode_show_errors
81
88
  end
82
89
 
83
90
  def test_link
84
91
  assert_equal '[url url=http://www.google.com]http://www.google.com[/url]', '[url]http://www.google.com[/url]'.bbcode_show_errors
85
92
  assert_equal '[url url=http://google.com]Google[/url]', '[url=http://google.com]Google[/url]'.bbcode_show_errors
93
+ assert_equal '[url url=http://google.com][b]Bold Google[/b][/url]', '[url=http://google.com][b]Bold Google[/b][/url]'.bbcode_show_errors
86
94
  assert_equal '[url url=/index.html]Home[/url]', '[url=/index.html]Home[/url]'.bbcode_show_errors
87
95
  end
88
96
 
89
97
  def test_image
90
98
  assert_equal '[img]http://www.ruby-lang.org/images/logo.gif[/img]',
91
- '[img]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_show_errors
99
+ '[img]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_show_errors
92
100
  assert_equal '[img width=95 height=96]http://www.ruby-lang.org/images/logo.gif[/img]',
93
- '[img=95x96]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_show_errors
101
+ '[img=95x96]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_show_errors
94
102
  assert_equal '[img width=123 height=456]http://www.ruby-lang.org/images/logo.gif[/img]',
95
- '[img width=123 height=456]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_show_errors
103
+ '[img width=123 height=456]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_show_errors
96
104
  end
97
105
 
98
106
  def test_youtube
99
107
  assert_equal '[youtube]E4Fbk52Mk1w[/youtube]',
100
- '[youtube]E4Fbk52Mk1w[/youtube]'.bbcode_show_errors
108
+ '[youtube]E4Fbk52Mk1w[/youtube]'.bbcode_show_errors
101
109
  end
102
110
 
103
111
  def test_youtube_with_full_url
104
112
  assert_equal '[youtube]E4Fbk52Mk1w[/youtube]',
105
- '[youtube]http://www.youtube.com/watch?feature=player_embedded&v=E4Fbk52Mk1w[/youtube]'.bbcode_show_errors
113
+ '[youtube]http://www.youtube.com/watch?feature=player_embedded&v=E4Fbk52Mk1w[/youtube]'.bbcode_show_errors
106
114
  end
107
115
 
108
116
  def test_youtube_with_url_shortener
109
117
  assert_equal '[youtube]cSohjlYQI2A[/youtube]',
110
- '[youtube]http://www.youtu.be/cSohjlYQI2A[/youtube]'.bbcode_show_errors
118
+ '[youtube]http://www.youtu.be/cSohjlYQI2A[/youtube]'.bbcode_show_errors
111
119
  end
112
120
 
113
121
  def test_disable_tags
@@ -117,17 +125,18 @@ class RubyBbcodeBbcodeTest < Minitest::Test
117
125
  end
118
126
 
119
127
  def test_enable_tags
120
- assert_equal '[b]foobar[/b]' , '[b]foobar[/b]'.bbcode_show_errors({}, :enable, :b)
128
+ assert_equal '[b]foobar[/b]', '[b]foobar[/b]'.bbcode_show_errors({}, :enable, :b)
121
129
  assert_equal '[b][i]foobar[/i][/b]', '[b][i]foobar[/i][/b]'.bbcode_show_errors({}, :enable, :b)
122
130
  assert_equal '[b][i]foobar[/i][/b]', '[b][i]foobar[/i][/b]'.bbcode_show_errors({}, :enable, :b, :i)
123
131
  end
124
132
 
125
133
  def test_addition_of_tags
126
134
  mydef = {
127
- :test => {
128
- :html_open => '<test>', :html_close => '</test>',
129
- :description => 'This is a test',
130
- :example => '[test]Test here[/test]'}
135
+ test: {
136
+ html_open: '<test>', html_close: '</test>',
137
+ description: 'This is a test',
138
+ example: '[test]Test here[/test]'
139
+ }
131
140
  }
132
141
  assert_equal 'pre [test]Test here[/test] post', 'pre [test]Test here[/test] post'.bbcode_show_errors(mydef)
133
142
  assert_equal 'pre [b][test]Test here[/test][/b] post', 'pre [b][test]Test here[/test][/b] post'.bbcode_show_errors(mydef)
@@ -135,7 +144,7 @@ class RubyBbcodeBbcodeTest < Minitest::Test
135
144
 
136
145
  def test_multiple_tag_test
137
146
  assert_equal '[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url url=https://test.com]link[/url]',
138
- '[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=https://test.com]link[/url]'.bbcode_show_errors
147
+ '[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=https://test.com]link[/url]'.bbcode_show_errors
139
148
  end
140
149
 
141
150
  def test_no_xss_hax
@@ -176,12 +185,26 @@ class RubyBbcodeBbcodeTest < Minitest::Test
176
185
  def test_failing_between_texts
177
186
  assert_equal '<span class=\'bbcode_error\' data-bbcode-errors=\'["No text between [img] and [/img] tags."]\'>[img]</span>[/img]', '[img][/img]'.bbcode_show_errors
178
187
  assert_equal '[url]<span class=\'bbcode_error\' data-bbcode-errors=\'["The URL should start with http:// https://, ftp:// or /, instead of &#39;illegal url&#39;"]\'>illegal url</span>[/url]', '[url]illegal url[/url]'.bbcode_show_errors
188
+ assert_equal '[url]<span class=\'bbcode_error\' data-bbcode-errors=\'["between parameter must be plain text"]\'>[b]</span>Bold Google[/b][/url]', '[url][b]Bold Google[/b][/url]'.bbcode_show_errors
179
189
  end
180
190
 
181
191
  def test_missing_parent_tags
182
192
  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
183
193
  end
184
194
 
195
+ def test_unknown_tag
196
+ RubyBBCode.configuration.ignore_unknown_tags = :exception
197
+ assert_raises RuntimeError do
198
+ '[unknown]This is an unknown tag[/unknown]'.bbcode_show_errors
199
+ end
200
+
201
+ RubyBBCode.configuration.ignore_unknown_tags = :ignore
202
+ assert_equal 'This is an unknown tag', '[unknown]This is an unknown tag[/unknown]'.bbcode_show_errors
203
+
204
+ RubyBBCode.configuration.ignore_unknown_tags = :text
205
+ assert_equal '[unknown]This is an unknown tag[/unknown]', '[unknown]This is an unknown tag[/unknown]'.bbcode_show_errors
206
+ end
207
+
185
208
  def test_illegal_unallowed_childs
186
209
  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
187
210
  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
@@ -1,10 +1,15 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class RubyBbcodeHtmlTest < Minitest::Test
4
+ def before_setup
5
+ RubyBBCode.reset
6
+ end
4
7
 
5
8
  def test_multiline
6
9
  assert_equal "line1<br />\nline2", "line1\nline2".bbcode_to_html
7
10
  assert_equal "line1<br />\nline2", "line1\r\nline2".bbcode_to_html
11
+ assert_equal "<ul>\n<li>line1</li>\n<li>line2</li>\n</ul>", "[ul]\n[li]line1[/li]\n[li]line2[/li]\n[/ul]".bbcode_to_html
12
+ assert_equal "<strong><br />\nline 1<br />\nline 2</strong>", "[b]\nline 1\nline 2[/b]".bbcode_to_html
8
13
  end
9
14
 
10
15
  def test_strong
@@ -55,11 +60,11 @@ class RubyBbcodeHtmlTest < Minitest::Test
55
60
 
56
61
  def test_list_common_syntax
57
62
  assert_equal '<ul><li>item 1</li><li>item 2</li></ul>', '[list][*]item 1[*]item 2[/list]'.bbcode_to_html
58
- assert_equal '<ul><li><strong>item 1</strong> test</li><li>item 2</li></ul>', "[list]\n[*][b]item 1[/b] test[*]item 2[/list]".bbcode_to_html
63
+ assert_equal '<ul><li><strong>item 1</strong> test</li><li>item 2</li></ul>', '[list][*][b]item 1[/b] test[*]item 2[/list]'.bbcode_to_html
59
64
  end
60
65
 
61
66
  def test_newline_list_common_syntax
62
- assert_equal '<ul><li>item 1</li><li>item 2</li></ul>', "[list]\n[*]item 1\n[*]item 2\n[/list]".bbcode_to_html
67
+ assert_equal "<ul>\n<li>item 1</li>\n<li>item 2</li>\n\n</ul>", "[list]\n[*]item 1\n[*]item 2\n\n[/list]".bbcode_to_html
63
68
  end
64
69
 
65
70
  def test_list_common_syntax_explicit_closing
@@ -68,55 +73,58 @@ class RubyBbcodeHtmlTest < Minitest::Test
68
73
 
69
74
  def test_two_lists
70
75
  assert_equal '<ul><li>item1</li><li>item2</li></ul><ul><li>item1</li><li>item2</li></ul>',
71
- '[ul][li]item1[/li][li]item2[/li][/ul][ul][li]item1[/li][li]item2[/li][/ul]'.bbcode_to_html
76
+ '[ul][li]item1[/li][li]item2[/li][/ul][ul][li]item1[/li][li]item2[/li][/ul]'.bbcode_to_html
72
77
  end
73
78
 
74
79
  def test_whitespace_in_only_allowed_tags
75
- assert_equal "<ol><br />\n<li>item 1</li><br />\n<li>item 2</li><br />\n</ol>",
76
- "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]".bbcode_to_html
80
+ assert_equal "<ol>\n<li>item 1</li>\n<li>item 2</li>\n</ol>",
81
+ "[ol]\n[li]item 1[/li]\n[li]item 2[/li]\n[/ol]".bbcode_to_html
77
82
  assert_equal "<ol> <li>item 1</li> <li>item 2</li>\t</ol>",
78
- "[ol] [li]item 1[/li] [li]item 2[/li]\t[/ol]".bbcode_to_html
83
+ "[ol] [li]item 1[/li] [li]item 2[/li]\t[/ol]".bbcode_to_html
79
84
  end
80
85
 
81
86
  def test_quote
82
87
  assert_equal '<div class="quote">quoting</div>', '[quote]quoting[/quote]'.bbcode_to_html
88
+ assert_equal "<div class=\"quote\">\nquoting\n</div>", "[quote]\nquoting\n[/quote]".bbcode_to_html
89
+ assert_equal "<div class=\"quote\">\nfirst line<br />\nsecond line\n</div>", "[quote]\nfirst line\nsecond line\n[/quote]".bbcode_to_html
83
90
  assert_equal '<div class="quote"><strong>someone wrote:</strong>quoting</div>', '[quote=someone]quoting[/quote]'.bbcode_to_html
84
91
  assert_equal '<div class="quote"><strong>Kitten wrote:</strong><div class="quote"><strong>creatiu wrote:</strong>f1</div>f2</div>',
85
- '[quote=Kitten][quote=creatiu]f1[/quote]f2[/quote]'.bbcode_to_html
92
+ '[quote=Kitten][quote=creatiu]f1[/quote]f2[/quote]'.bbcode_to_html
86
93
  end
87
94
 
88
95
  def test_link
89
96
  assert_equal '<a href="http://www.google.com">http://www.google.com</a>', '[url]http://www.google.com[/url]'.bbcode_to_html
90
97
  assert_equal '<a href="http://google.com">Google</a>', '[url=http://google.com]Google[/url]'.bbcode_to_html
98
+ assert_equal '<a href="http://google.com"><strong>Bold Google</strong></a>', '[url=http://google.com][b]Bold Google[/b][/url]'.bbcode_to_html
91
99
  assert_equal '<a href="/index.html">Home</a>', '[url=/index.html]Home[/url]'.bbcode_to_html
92
100
  end
93
101
 
94
102
  def test_image
95
103
  assert_equal '<img src="http://www.ruby-lang.org/images/logo.gif" alt="" />',
96
- '[img]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_to_html
104
+ '[img]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_to_html
97
105
  assert_equal '<img src="http://www.ruby-lang.org/images/logo.gif" width="95" height="96" alt="" />',
98
- '[img=95x96]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_to_html
106
+ '[img=95x96]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_to_html
99
107
  assert_equal '<img src="http://www.ruby-lang.org/images/logo.gif" width="123" height="456" alt="" />',
100
- '[img width=123 height=456]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_to_html
108
+ '[img width=123 height=456]http://www.ruby-lang.org/images/logo.gif[/img]'.bbcode_to_html
101
109
  end
102
110
 
103
111
  def test_youtube
104
112
  assert_equal '<iframe id="player" type="text/html" width="400" height="320" src="http://www.youtube.com/embed/E4Fbk52Mk1w?enablejsapi=1" frameborder="0"></iframe>',
105
- '[youtube]E4Fbk52Mk1w[/youtube]'.bbcode_to_html
113
+ '[youtube]E4Fbk52Mk1w[/youtube]'.bbcode_to_html
106
114
  assert_equal '<iframe id="player" type="text/html" width="640" height="480" src="http://www.youtube.com/embed/E4Fbk52Mk1w?enablejsapi=1" frameborder="0"></iframe>',
107
- '[youtube width=640 height=480]E4Fbk52Mk1w[/youtube]'.bbcode_to_html
115
+ '[youtube width=640 height=480]E4Fbk52Mk1w[/youtube]'.bbcode_to_html
108
116
  end
109
117
 
110
118
  def test_youtube_with_full_url
111
- full_url = "http://www.youtube.com/watch?feature=player_embedded&v=E4Fbk52Mk1w"
119
+ full_url = 'http://www.youtube.com/watch?feature=player_embedded&v=E4Fbk52Mk1w'
112
120
  assert_equal '<iframe id="player" type="text/html" width="400" height="320" src="http://www.youtube.com/embed/E4Fbk52Mk1w?enablejsapi=1" frameborder="0"></iframe>',
113
- "[youtube]#{full_url}[/youtube]".bbcode_to_html
121
+ "[youtube]#{full_url}[/youtube]".bbcode_to_html
114
122
  end
115
123
 
116
124
  def test_youtube_with_url_shortener
117
- full_url = "http://www.youtu.be/cSohjlYQI2A"
125
+ full_url = 'http://www.youtu.be/cSohjlYQI2A'
118
126
  assert_equal '<iframe id="player" type="text/html" width="400" height="320" src="http://www.youtube.com/embed/cSohjlYQI2A?enablejsapi=1" frameborder="0"></iframe>',
119
- "[youtube]#{full_url}[/youtube]".bbcode_to_html
127
+ "[youtube]#{full_url}[/youtube]".bbcode_to_html
120
128
  end
121
129
 
122
130
  def test_html_escaping
@@ -131,49 +139,51 @@ class RubyBbcodeHtmlTest < Minitest::Test
131
139
  def test_uri_escaping
132
140
  # There is no tag available, so create our own to test URI escaping
133
141
  escape_param_def = {
134
- :escapequery => {
135
- :html_open => '<a href="%query%">%between%', :html_close => '</a>',
136
- :require_between => true, :allow_quick_param => false, :allow_between_as_param => true,
137
- :param_tokens => [{:token => :query, :uri_escape => true}]}
142
+ escapequery: {
143
+ html_open: '<a href="%query%">%between%', html_close: '</a>',
144
+ require_between: true, allow_quick_param: false, allow_between_as_param: true,
145
+ param_tokens: [{ token: :query, uri_escape: true }]
146
+ }
138
147
  }
139
148
  assert_equal '<a href="Escaped+string+%28to+be+used+as+URL+%26+more%29">Escaped string (to be used as URL & more)</a>',
140
- '[escapequery]Escaped string (to be used as URL & more)[/escapequery]'.bbcode_to_html(true, escape_param_def)
149
+ '[escapequery]Escaped string (to be used as URL & more)[/escapequery]'.bbcode_to_html(true, escape_param_def)
141
150
  assert_equal '<a href="http%3A%3A%2Fwww.text.com%2Fpage.php%3Fparam1%3D1%26param2%3D2">http::/www.text.com/page.php?param1=1&param2=2</a>',
142
- '[escapequery]http::/www.text.com/page.php?param1=1&param2=2[/escapequery]'.bbcode_to_html(true, escape_param_def)
151
+ '[escapequery]http::/www.text.com/page.php?param1=1&param2=2[/escapequery]'.bbcode_to_html(true, escape_param_def)
143
152
  end
144
153
 
145
154
  def test_disable_tags
146
- assert_equal "[b]foobar[/b]", "[b]foobar[/b]".bbcode_to_html(true, {}, :disable, :b)
147
- assert_equal "[b]<em>foobar</em>[/b]", "[b][i]foobar[/i][/b]".bbcode_to_html(true, {}, :disable, :b)
148
- assert_equal "[b][i]foobar[/i][/b]", "[b][i]foobar[/i][/b]".bbcode_to_html(true, {}, :disable, :b, :i)
155
+ assert_equal '[b]foobar[/b]', '[b]foobar[/b]'.bbcode_to_html(true, {}, :disable, :b)
156
+ assert_equal '[b]<em>foobar</em>[/b]', '[b][i]foobar[/i][/b]'.bbcode_to_html(true, {}, :disable, :b)
157
+ assert_equal '[b][i]foobar[/i][/b]', '[b][i]foobar[/i][/b]'.bbcode_to_html(true, {}, :disable, :b, :i)
149
158
  end
150
159
 
151
160
  def test_enable_tags
152
- assert_equal "<strong>foobar</strong>" , "[b]foobar[/b]".bbcode_to_html(true, {}, :enable, :b)
153
- assert_equal "<strong>[i]foobar[/i]</strong>", "[b][i]foobar[/i][/b]".bbcode_to_html(true, {}, :enable, :b)
154
- assert_equal "<strong><em>foobar</em></strong>", "[b][i]foobar[/i][/b]".bbcode_to_html(true, {}, :enable, :b, :i)
161
+ assert_equal '<strong>foobar</strong>', '[b]foobar[/b]'.bbcode_to_html(true, {}, :enable, :b)
162
+ assert_equal '<strong>[i]foobar[/i]</strong>', '[b][i]foobar[/i][/b]'.bbcode_to_html(true, {}, :enable, :b)
163
+ assert_equal '<strong><em>foobar</em></strong>', '[b][i]foobar[/i][/b]'.bbcode_to_html(true, {}, :enable, :b, :i)
155
164
  end
156
165
 
157
166
  def test_to_html_bang_method
158
- foo = "[b]foobar[/b]"
159
- assert_equal "<strong>foobar</strong>", foo.bbcode_to_html!
160
- assert_equal "<strong>foobar</strong>", foo
167
+ foo = '[b]foobar[/b]'
168
+ assert_equal '<strong>foobar</strong>', foo.bbcode_to_html!
169
+ assert_equal '<strong>foobar</strong>', foo
161
170
  end
162
171
 
163
172
  def test_addition_of_tags
164
173
  mydef = {
165
- :test => {
166
- :html_open => '<test>', :html_close => '</test>',
167
- :description => 'This is a test',
168
- :example => '[test]Test here[/test]'}
174
+ test: {
175
+ html_open: '<test>', html_close: '</test>',
176
+ description: 'This is a test',
177
+ example: '[test]Test here[/test]'
178
+ }
169
179
  }
170
180
  assert_equal 'pre <test>Test here</test> post', 'pre [test]Test here[/test] post'.bbcode_to_html(true, mydef)
171
181
  assert_equal 'pre <strong><test>Test here</test></strong> post', 'pre [b][test]Test here[/test][/b] post'.bbcode_to_html(true, mydef)
172
182
  end
173
183
 
174
184
  def test_multiple_tag_test
175
- assert_equal "<strong>bold</strong><em>italic</em><u>underline</u><div class=\"quote\">quote</div><a href=\"https://test.com\">link</a>",
176
- "[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=https://test.com]link[/url]".bbcode_to_html
185
+ assert_equal '<strong>bold</strong><em>italic</em><u>underline</u><div class="quote">quote</div><a href="https://test.com">link</a>',
186
+ '[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=https://test.com]link[/url]'.bbcode_to_html
177
187
  end
178
188
 
179
189
  def test_no_xss_hax
@@ -182,8 +192,8 @@ class RubyBbcodeHtmlTest < Minitest::Test
182
192
  end
183
193
 
184
194
  def test_media_tag
185
- input1 = "[media]http://www.youtube.com/watch?v=cSohjlYQI2A[/media]"
186
- input2 = "[media]http://vimeo.com/46141955[/media]"
195
+ input1 = '[media]http://www.youtube.com/watch?v=cSohjlYQI2A[/media]'
196
+ input2 = '[media]http://vimeo.com/46141955[/media]'
187
197
 
188
198
  output1 = '<iframe id="player" type="text/html" width="400" height="320" src="http://www.youtube.com/embed/cSohjlYQI2A?enablejsapi=1" frameborder="0"></iframe>'
189
199
  output2 = '<iframe src="http://player.vimeo.com/video/46141955?badge=0" width="400" height="320" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
@@ -193,15 +203,28 @@ class RubyBbcodeHtmlTest < Minitest::Test
193
203
  end
194
204
 
195
205
  def test_vimeo_tag
196
- input = "[vimeo]http://vimeo.com/46141955[/vimeo]"
197
- input2 = "[vimeo]46141955[/vimeo]"
206
+ input = '[vimeo]http://vimeo.com/46141955[/vimeo]'
207
+ input2 = '[vimeo]46141955[/vimeo]'
198
208
  output = '<iframe src="http://player.vimeo.com/video/46141955?badge=0" width="400" height="320" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
199
209
 
200
210
  assert_equal output, input.bbcode_to_html
201
211
  assert_equal output, input2.bbcode_to_html
202
212
 
203
213
  assert_equal '<iframe src="http://player.vimeo.com/video/46141955?badge=0" width="640" height="480" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
204
- '[vimeo width=640 height=480]46141955[/vimeo]'.bbcode_to_html
214
+ '[vimeo width=640 height=480]46141955[/vimeo]'.bbcode_to_html
215
+ end
216
+
217
+ def test_unknown_tag
218
+ RubyBBCode.configuration.ignore_unknown_tags = :exception
219
+ assert_raises RuntimeError do
220
+ '[unknown]This is an unknown tag[/unknown]'.bbcode_to_html
221
+ end
222
+
223
+ RubyBBCode.configuration.ignore_unknown_tags = :ignore
224
+ assert_equal 'This is an unknown tag', '[unknown]This is an unknown tag[/unknown]'.bbcode_to_html
225
+
226
+ RubyBBCode.configuration.ignore_unknown_tags = :text
227
+ assert_equal '[unknown]This is an unknown tag[/unknown]', '[unknown]This is an unknown tag[/unknown]'.bbcode_to_html
205
228
  end
206
229
 
207
230
  def test_raised_exceptions