Skip to content

Commit 00f4507

Browse files
committed
Merge pull request ncb000gt#9 from dpolivy/master
Add support for upper and mixed-case bbcode tags
2 parents 140a937 + 8c0b6c7 commit 00f4507

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

lib/bbcode.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// -----------------------------------------------------------------------
2-
// Copyright (c) 2008, Stone Steps Inc.
2+
// Copyright (c) 2008, Stone Steps Inc.
33
// All rights reserved
44
// http://www.stonesteps.ca/legal/bsd-license/
55
//
66
// This is a BBCode parser written in JavaScript. The parser is intended
7-
// to demonstrate how to parse text containing BBCode tags in one pass
7+
// to demonstrate how to parse text containing BBCode tags in one pass
88
// using regular expressions.
99
//
10-
// The parser may be used as a backend component in ASP or in the browser,
11-
// after the text containing BBCode tags has been served to the client.
10+
// The parser may be used as a backend component in ASP or in the browser,
11+
// after the text containing BBCode tags has been served to the client.
1212
//
1313
// Following BBCode expressions are recognized:
1414
//
@@ -31,9 +31,9 @@
3131
// [blockquote=http://blogs.stonesteps.ca/showpost.asp?pid=33]block quote[/blockquote]
3232
// [blockquote]block quote[/blockquote]
3333
//
34-
// [pre]formatted
34+
// [pre]formatted
3535
// text[/pre]
36-
// [code]if(a == b)
36+
// [code]if(a == b)
3737
// print("done");[/code]
3838
//
3939
// text containing [noparse] [brackets][/noparse]
@@ -51,7 +51,7 @@ exports.parse = function(post, cb) {
5151
var urlstart = -1; // beginning of the URL if zero or greater (ignored if -1)
5252

5353
// aceptable BBcode tags, optionally prefixed with a slash
54-
var tagname_re = /^\/?(?:b|i|u|pre|samp|code|colou?r|size|noparse|url|link|s|q|blockquote|img|u?list|li)$/;
54+
var tagname_re = /^\/?(?:b|i|u|pre|samp|code|colou?r|size|noparse|url|link|s|q|blockquote|img|u?list|li)$/i;
5555

5656
// color names or hex color
5757
var color_re = /^(:?black|silver|gray|white|maroon|red|purple|fuchsia|green|lime|olive|yellow|navy|blue|teal|aqua|#(?:[0-9a-f]{3})?[0-9a-f]{3})$/i;
@@ -110,6 +110,8 @@ exports.parse = function(post, cb) {
110110
// handle start tags
111111
//
112112
if(isValidTag(m2)) {
113+
var m2l = m2.toLowerCase();
114+
113115
// if in the noparse state, just echo the tag
114116
if(noparse)
115117
return "[" + m2 + "]";
@@ -118,32 +120,32 @@ exports.parse = function(post, cb) {
118120
if(opentags.length && (opentags[opentags.length-1].bbtag == "url" || opentags[opentags.length-1].bbtag == "link") && urlstart >= 0)
119121
return "[" + m2 + "]";
120122

121-
switch (m2) {
123+
switch (m2l) {
122124
case "code":
123-
opentags.push(new taginfo_t(m2, "</code></pre>"));
125+
opentags.push(new taginfo_t(m2l, "</code></pre>"));
124126
crlf2br = false;
125127
return "<pre><code>";
126128

127129
case "pre":
128-
opentags.push(new taginfo_t(m2, "</pre>"));
130+
opentags.push(new taginfo_t(m2l, "</pre>"));
129131
crlf2br = false;
130132
return "<pre>";
131133

132134
case "color":
133135
case "colour":
134136
if(!m3 || !color_re.test(m3))
135137
m3 = "inherit";
136-
opentags.push(new taginfo_t(m2, "</span>"));
138+
opentags.push(new taginfo_t(m2l, "</span>"));
137139
return "<span style=\"color: " + m3 + "\">";
138140

139141
case "size":
140142
if(!m3 || !number_re.test(m3))
141143
m3 = "1";
142-
opentags.push(new taginfo_t(m2, "</span>"));
144+
opentags.push(new taginfo_t(m2l, "</span>"));
143145
return "<span style=\"font-size: " + Math.min(Math.max(m3, 0.7), 3) + "em\">";
144146

145147
case "s":
146-
opentags.push(new taginfo_t(m2, "</span>"));
148+
opentags.push(new taginfo_t(m2l, "</span>"));
147149
return "<span style=\"text-decoration: line-through\">";
148150

149151
case "noparse":
@@ -152,7 +154,7 @@ exports.parse = function(post, cb) {
152154

153155
case "link":
154156
case "url":
155-
opentags.push(new taginfo_t(m2, "</a>"));
157+
opentags.push(new taginfo_t(m2l, "</a>"));
156158

157159
// check if there's a valid option
158160
if(m3 && uri_re.test(m3)) {
@@ -161,25 +163,25 @@ exports.parse = function(post, cb) {
161163
return "<a target=\"_blank\" href=\"" + m3 + "\">";
162164
}
163165

164-
// otherwise, remember the URL offset
166+
// otherwise, remember the URL offset
165167
urlstart = mstr.length + offset;
166168

167169
// and treat the text following [url] as a URL
168170
return "<a target=\"_blank\" href=\"";
169171
case "img":
170-
opentags.push(new taginfo_t(m2, "\" />"));
172+
opentags.push(new taginfo_t(m2l, "\" />"));
171173

172174
if (m3 && uri_re.test(m3)) {
173175
urlstart = -1;
174-
return "<" + m2 + " src=\"" + m3 + "";
176+
return "<" + m2l + " src=\"" + m3 + "";
175177
}
176178

177-
return "<"+m2+" src=\"";
179+
return "<"+m2l+" src=\"";
178180

179181
case "q":
180182
case "blockquote":
181-
opentags.push(new taginfo_t(m2, "</" + m2 + ">"));
182-
return m3 && m3.length && uri_re.test(m3) ? "<" + m2 + " cite=\"" + m3 + "\">" : "<" + m2 + ">";
183+
opentags.push(new taginfo_t(m2l, "</" + m2l + ">"));
184+
return m3 && m3.length && uri_re.test(m3) ? "<" + m2l + " cite=\"" + m3 + "\">" : "<" + m2l + ">";
183185

184186
case "list":
185187
opentags.push(new taginfo_t('list', '</ol>'));
@@ -188,19 +190,19 @@ exports.parse = function(post, cb) {
188190
case "ulist":
189191
opentags.push(new taginfo_t('ulist', '</ul>'));
190192
return '<ul>';
191-
193+
192194
case "b":
193195
opentags.push(new taginfo_t('b', '</strong>'));
194196
return '<strong>';
195-
197+
196198
case "i":
197199
opentags.push(new taginfo_t('i', '</em>'));
198200
return '<em>';
199201

200202
default:
201203
// [samp] and [u] don't need special processing
202-
opentags.push(new taginfo_t(m2, "</" + m2 + ">"));
203-
return "<" + m2 + ">";
204+
opentags.push(new taginfo_t(m2l, "</" + m2l + ">"));
205+
return "<" + m2l + ">";
204206

205207
}
206208
}
@@ -209,6 +211,8 @@ exports.parse = function(post, cb) {
209211
// process end tags
210212
//
211213
if(isValidTag(m4)) {
214+
var m4l = m4.toLowerCase();
215+
212216
if(noparse) {
213217
// if it's the closing noparse tag, flip the noparse state
214218
if(m4 == "noparse") {
@@ -221,18 +225,18 @@ exports.parse = function(post, cb) {
221225
}
222226

223227
// highlight mismatched end tags
224-
if(!opentags.length || opentags[opentags.length-1].bbtag != m4)
228+
if(!opentags.length || opentags[opentags.length-1].bbtag != m4l)
225229
return "<span style=\"color: red\">[/" + m4 + "]</span>";
226230

227-
if(m4 == "url" || m4 == "link") {
231+
if(m4l == "url" || m4l == "link") {
228232
// if there was no option, use the content of the [url] tag
229233
if(urlstart > 0)
230234
return "\">" + string.substr(urlstart, offset-urlstart) + opentags.pop().etag;
231235

232236
// otherwise just close the tag
233237
return opentags.pop().etag;
234238
}
235-
else if(m4 == "code" || m4 == "pre")
239+
else if(m4l == "code" || m4l == "pre")
236240
crlf2br = true;
237241

238242
// other tags require no special processing, just output the end tag

tests/parse.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,23 @@ describe('bcrypt', function() {
189189
var parse = bbcode.parse('[b][i][u]Hai[/u][/i][/b]');
190190
parse.should.equal('<strong><em><u>Hai</u></em></strong>');
191191
});
192+
193+
describe('should parse uppercase tags properly - ', function() {
194+
it('should parse uppercase [I] to <em>', function() {
195+
bbcode.parse('[I]italics[/I]', function(parse) {
196+
parse.should.equal('<em>italics</em>');
197+
});
198+
});
199+
it('should parse uppercase [IMG] to <img>', function() {
200+
bbcode.parse('[IMG]http://example.com/img.png[/IMG]', function(parse) {
201+
parse.should.equal('<img src="http://example.com/img.png" />');
202+
});
203+
});
204+
it('should parse mixed case [IMG][/img] to <img>', function() {
205+
bbcode.parse('[Img]http://example.com/img.png[/img]', function(parse) {
206+
parse.should.equal('<img src="http://example.com/img.png" />');
207+
});
208+
});
209+
});
192210
});
193211
});

0 commit comments

Comments
 (0)