Skip to content

Commit b9e6c91

Browse files
committed
Merge pull request ncb000gt#4 from scotthovestadt/master
Support for [link] and quoted attributes [url="http://"]link[/url]
2 parents a859651 + be88922 commit b9e6c91

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

lib/bbcode.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//
2525
// [url]http://blogs.stonesteps.ca/showpost.asp?pid=33[/url]
2626
// [url=http://blogs.stonesteps.ca/showpost.asp?pid=33][b]BBCode[/b] Parser[/url]
27+
// [url="http://blogs.stonesteps.ca/showpost.asp?pid=33"][b]BBCode[/b] Parser[/url]
2728
//
2829
// [q=http://blogs.stonesteps.ca/showpost.asp?pid=33]inline quote[/q]
2930
// [q]inline quote[/q]
@@ -50,7 +51,7 @@ exports.parse = function(post, cb) {
5051
var urlstart = -1; // beginning of the URL if zero or greater (ignored if -1)
5152

5253
// aceptable BBcode tags, optionally prefixed with a slash
53-
var tagname_re = /^\/?(?:b|i|u|pre|samp|code|colou?r|size|noparse|url|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)$/;
5455

5556
// color names or hex color
5657
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;
@@ -61,8 +62,8 @@ exports.parse = function(post, cb) {
6162
// reserved, unreserved, escaped and alpha-numeric [RFC2396]
6263
var uri_re = /^[-;\/\?:@&=\+\$,_\.!~\*'\(\)%0-9a-z]{1,512}$/i;
6364

64-
// main regular expression: CRLF, [tag=option], [tag] or [/tag]
65-
var postfmt_re = /([\r\n])|(?:\[([a-z]{1,16})(?:=([^\x00-\x1F"'\(\)<>\[\]]{1,256}))?\])|(?:\[\/([a-z]{1,16})\])/ig;
65+
// main regular expression: CRLF, [tag=option], [tag="option"] [tag] or [/tag]
66+
var postfmt_re = /([\r\n])|(?:\[([a-z]{1,16})(?:=(?:"|'|)([^\x00-\x1F"'\(\)<>\[\]]{1,256}))?(?:"|'|)\])|(?:\[\/([a-z]{1,16})\])/ig;
6667

6768
// stack frame object
6869
function taginfo_t(bbtag, etag)
@@ -114,7 +115,7 @@ exports.parse = function(post, cb) {
114115
return "[" + m2 + "]";
115116

116117
// ignore any tags if there's an open option-less [url] tag
117-
if(opentags.length && opentags[opentags.length-1].bbtag == "url" && urlstart >= 0)
118+
if(opentags.length && (opentags[opentags.length-1].bbtag == "url" || opentags[opentags.length-1].bbtag == "link") && urlstart >= 0)
118119
return "[" + m2 + "]";
119120

120121
switch (m2) {
@@ -149,6 +150,7 @@ exports.parse = function(post, cb) {
149150
noparse = true;
150151
return "";
151152

153+
case "link":
152154
case "url":
153155
opentags.push(new taginfo_t(m2, "</a>"));
154156

@@ -214,7 +216,7 @@ exports.parse = function(post, cb) {
214216
if(!opentags.length || opentags[opentags.length-1].bbtag != m4)
215217
return "<span style=\"color: red\">[/" + m4 + "]</span>";
216218

217-
if(m4 == "url") {
219+
if(m4 == "url" || m4 == "link") {
218220
// if there was no option, use the content of the [url] tag
219221
if(urlstart > 0)
220222
return "\">" + string.substr(urlstart, offset-urlstart) + opentags.pop().etag;
@@ -262,7 +264,7 @@ exports.parse = function(post, cb) {
262264
endtags = new String();
263265

264266
// if there's an open [url] at the top, close it
265-
if(opentags[opentags.length-1].bbtag == "url") {
267+
if(opentags[opentags.length-1].bbtag == "url" || opentags[opentags.length-1].bbtag == "link") {
266268
opentags.pop();
267269
endtags += "\">" + post.substr(urlstart, post.length-urlstart) + "</a>";
268270
}
@@ -278,4 +280,4 @@ exports.parse = function(post, cb) {
278280
} else {
279281
return ret;
280282
}
281-
}
283+
}

tests/parse.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,41 @@ describe('bcrypt', function() {
5757
});
5858
});
5959

60-
it('should parse [url=<url>] to <a href=<url>>', function() {
61-
bbcode.parse('[url=http://example.com]url[/url]', function(parse) {
62-
parse.should.equal('<a href="http://example.com">url</a>');
60+
describe('should parse [url] and [link]', function() {
61+
it('should parse [url=<url>] to <a href=<url>>', function() {
62+
bbcode.parse('[url=http://example.com]url[/url]', function(parse) {
63+
parse.should.equal('<a href="http://example.com">url</a>');
64+
});
65+
});
66+
67+
it('should parse [url="<url>"] to <a href=<url>>', function() {
68+
bbcode.parse('[url="http://example.com"]url[/url]', function(parse) {
69+
parse.should.equal('<a href="http://example.com">url</a>');
70+
});
71+
});
72+
73+
it('should parse [url=\'<url>\'] to <a href=<url>>', function() {
74+
bbcode.parse('[url=\'http://example.com\']url[/url]', function(parse) {
75+
parse.should.equal('<a href="http://example.com">url</a>');
76+
});
77+
});
78+
79+
it('should parse [link=<url>]test[/link]', function() {
80+
bbcode.parse('[link=http://example.com]url[/link]', function(parse) {
81+
parse.should.equal('<a href="http://example.com">url</a>');
82+
});
83+
});
84+
85+
it('should parse [link="<url>"]test[/link]', function() {
86+
bbcode.parse('[link="http://example.com"]url[/link]', function(parse) {
87+
parse.should.equal('<a href="http://example.com">url</a>');
88+
});
89+
});
90+
91+
it('should parse [link=\'<url>\']test[/link]', function() {
92+
bbcode.parse('[link=\'http://example.com\']url[/link]', function(parse) {
93+
parse.should.equal('<a href="http://example.com">url</a>');
94+
});
6395
});
6496
});
6597

@@ -70,6 +102,18 @@ describe('bcrypt', function() {
70102
});
71103
});
72104

105+
it('as attribute with quotes - [img="<img>"] to <img src=<img>>', function() {
106+
bbcode.parse('[img="http://example.com/img.png"][/img]', function(parse) {
107+
parse.should.equal('<img src="http://example.com/img.png" />');
108+
});
109+
});
110+
111+
it('as attribute with single quotes - [img=\'<img>\'] to <img src=<img>>', function() {
112+
bbcode.parse('[img=\'http://example.com/img.png\'][/img]', function(parse) {
113+
parse.should.equal('<img src="http://example.com/img.png" />');
114+
});
115+
});
116+
73117
it('as content - [img]<img>[/img] to <img src=<img>>', function() {
74118
bbcode.parse('[img]http://example.com/img.png[/img]', function(parse) {
75119
parse.should.equal('<img src="http://example.com/img.png" />');
@@ -141,4 +185,4 @@ describe('bcrypt', function() {
141185
});
142186
});
143187
});
144-
});
188+
});

0 commit comments

Comments
 (0)