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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -4
- data/Rakefile +2 -4
- data/lib/ruby-bbcode.rb +48 -28
- data/lib/ruby-bbcode/bbtree.rb +15 -13
- data/lib/ruby-bbcode/configuration.rb +18 -0
- data/lib/ruby-bbcode/tag_collection.rb +9 -9
- data/lib/ruby-bbcode/tag_info.rb +49 -18
- data/lib/ruby-bbcode/tag_node.rb +4 -4
- data/lib/ruby-bbcode/tag_sifter.rb +80 -56
- data/lib/ruby-bbcode/templates/bbcode_errors_template.rb +20 -17
- data/lib/ruby-bbcode/templates/html_template.rb +36 -10
- data/lib/ruby-bbcode/version.rb +1 -1
- data/lib/tags/tags.rb +126 -104
- data/test/ruby_bbcode_bbcode_test.rb +39 -16
- data/test/ruby_bbcode_html_test.rb +65 -42
- data/test/ruby_bbcode_validity_test.rb +22 -19
- data/test/test_helper.rb +4 -5
- data/test/unit/configuration_test.rb +37 -0
- data/test/unit/tag_sifter_test.rb +7 -8
- metadata +43 -12
data/lib/ruby-bbcode/version.rb
CHANGED
data/lib/tags/tags.rb
CHANGED
@@ -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
|
-
:
|
10
|
-
:
|
11
|
-
:
|
9
|
+
html_open: '<strong>', html_close: '</strong>',
|
10
|
+
description: 'Make text bold',
|
11
|
+
example: 'This is [b]bold[/b].'
|
12
|
+
},
|
12
13
|
:i => {
|
13
|
-
:
|
14
|
-
:
|
15
|
-
:
|
14
|
+
html_open: '<em>', html_close: '</em>',
|
15
|
+
description: 'Make text italic',
|
16
|
+
example: 'This is [i]italic[/i].'
|
17
|
+
},
|
16
18
|
:u => {
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
19
|
+
html_open: '<u>', html_close: '</u>',
|
20
|
+
description: 'Underline text',
|
21
|
+
example: 'This is [u]underlined[/u].'
|
22
|
+
},
|
20
23
|
:s => {
|
21
|
-
:
|
22
|
-
:
|
23
|
-
:
|
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
|
-
:
|
26
|
-
:
|
27
|
-
:
|
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
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
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
|
-
:
|
35
|
-
:
|
36
|
-
:
|
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
|
-
:
|
39
|
-
:
|
40
|
-
:
|
41
|
-
:
|
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
|
-
:
|
44
|
-
:
|
45
|
-
:
|
46
|
-
:
|
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
|
-
:
|
49
|
-
:
|
50
|
-
:
|
51
|
-
:
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
:
|
56
|
-
:
|
57
|
-
:
|
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
|
-
:
|
60
|
-
:
|
61
|
-
:
|
62
|
-
:
|
63
|
-
:
|
64
|
-
:
|
65
|
-
:
|
66
|
-
:
|
67
|
-
|
68
|
-
:
|
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
|
-
:
|
71
|
-
:
|
72
|
-
:
|
73
|
-
:
|
74
|
-
:
|
75
|
-
:
|
76
|
-
:
|
77
|
-
:
|
78
|
-
|
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
|
-
:
|
81
|
-
:
|
82
|
-
:
|
83
|
-
:
|
84
|
-
:
|
85
|
-
:
|
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
|
-
:
|
88
|
-
:
|
89
|
-
:
|
90
|
-
:
|
91
|
-
:
|
92
|
-
:
|
93
|
-
:
|
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
|
-
:
|
96
|
-
:
|
97
|
-
:
|
98
|
-
:
|
99
|
-
:
|
100
|
-
:
|
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
|
-
:
|
103
|
-
:
|
104
|
-
:
|
105
|
-
:
|
106
|
-
:
|
107
|
-
:
|
108
|
-
:
|
109
|
-
{ :
|
110
|
-
{ :
|
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
|
-
:
|
114
|
-
:
|
115
|
-
:
|
116
|
-
:
|
117
|
-
:
|
118
|
-
:
|
119
|
-
:
|
120
|
-
:
|
121
|
-
{ :
|
122
|
-
{ :
|
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
|
-
:
|
126
|
-
:
|
127
|
-
:
|
128
|
-
|
129
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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]'
|
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
|
-
:
|
128
|
-
:
|
129
|
-
:
|
130
|
-
:
|
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
|
-
|
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 'illegal url'"]\'>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 "Illegal text" 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>',
|
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
|
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
|
-
|
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
|
76
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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 =
|
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
|
-
|
121
|
+
"[youtube]#{full_url}[/youtube]".bbcode_to_html
|
114
122
|
end
|
115
123
|
|
116
124
|
def test_youtube_with_url_shortener
|
117
|
-
full_url =
|
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
|
-
|
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
|
-
:
|
135
|
-
:
|
136
|
-
:
|
137
|
-
:
|
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
|
-
|
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¶m2=2</a>',
|
142
|
-
|
151
|
+
'[escapequery]http::/www.text.com/page.php?param1=1¶m2=2[/escapequery]'.bbcode_to_html(true, escape_param_def)
|
143
152
|
end
|
144
153
|
|
145
154
|
def test_disable_tags
|
146
|
-
assert_equal
|
147
|
-
assert_equal
|
148
|
-
assert_equal
|
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
|
153
|
-
assert_equal
|
154
|
-
assert_equal
|
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 =
|
159
|
-
assert_equal
|
160
|
-
assert_equal
|
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
|
-
:
|
166
|
-
:
|
167
|
-
:
|
168
|
-
:
|
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
|
176
|
-
|
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 =
|
186
|
-
input2 =
|
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 =
|
197
|
-
input2 =
|
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
|
-
|
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
|