From 6143753fefb8f3345cbd52197caa06520355b8be Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Mon, 3 Mar 2014 11:59:57 -0500
Subject: [PATCH] Support uppercase bbcode too.

---
 .../discourse/dialects/bbcode_dialect.js      | 65 ++++++++-----------
 test/javascripts/lib/bbcode_test.js           |  1 +
 2 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/app/assets/javascripts/discourse/dialects/bbcode_dialect.js b/app/assets/javascripts/discourse/dialects/bbcode_dialect.js
index 9cdb9de55..51357be81 100644
--- a/app/assets/javascripts/discourse/dialects/bbcode_dialect.js
+++ b/app/assets/javascripts/discourse/dialects/bbcode_dialect.js
@@ -4,13 +4,27 @@
   @method replaceBBCode
   @param {tag} tag the tag we want to match
   @param {function} emitter the function that creates JsonML for the tag
+  @param {Object} hash of options to pass to `inlineBetween`
 **/
-function replaceBBCode(tag, emitter) {
-  Discourse.Dialect.inlineBetween({
-    start: "[" + tag + "]",
-    stop: "[/" + tag + "]",
-    emitter: emitter
-  });
+function replaceBBCode(tag, emitter, opts) {
+  opts = opts || {};
+  opts = _.merge(opts, { start: "[" + tag + "]", stop: "[/" + tag + "]", emitter: emitter });
+  Discourse.Dialect.inlineBetween(opts);
+
+  tag = tag.toUpperCase();
+  opts = _.merge(opts, { start: "[" + tag + "]", stop: "[/" + tag + "]", emitter: emitter });
+  Discourse.Dialect.inlineBetween(opts);
+}
+
+/**
+  Shortcut to call replaceBBCode with `rawContents` as true.
+
+  @method replaceBBCode
+  @param {tag} tag the tag we want to match
+  @param {function} emitter the function that creates JsonML for the tag
+**/
+function rawBBCode(tag, emitter) {
+  replaceBBCode(tag, emitter, { rawContents: true });
 }
 
 /**
@@ -57,37 +71,14 @@ replaceBBCode('ul', function(contents) { return ['ul'].concat(contents); });
 replaceBBCode('ol', function(contents) { return ['ol'].concat(contents); });
 replaceBBCode('li', function(contents) { return ['li'].concat(contents); });
 
-Discourse.Dialect.inlineBetween({
-  start: '[img]',
-  stop: '[/img]',
-  rawContents: true,
-  emitter: function(contents) { return ['img', {href: contents}]; }
-});
-
-Discourse.Dialect.inlineBetween({
-  start: '[email]',
-  stop: '[/email]',
-  rawContents: true,
-  emitter: function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; }
-});
-
-Discourse.Dialect.inlineBetween({
-  start: '[url]',
-  stop: '[/url]',
-  rawContents: true,
-  emitter: function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; }
-});
-
-Discourse.Dialect.inlineBetween({
-  start: '[spoiler]',
-  stop: '[/spoiler]',
-  rawContents: true,
-  emitter: function(contents) {
-    if (/<img/i.test(contents)) {
-      return ['div', { 'class': 'spoiler' }, contents];
-    } else {
-      return ['span', { 'class': 'spoiler' }, contents];
-    }
+rawBBCode('img', function(contents) { return ['img', {href: contents}]; });
+rawBBCode('email', function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; });
+rawBBCode('url', function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; });
+rawBBCode('spoiler', function(contents) {
+  if (/<img/i.test(contents)) {
+    return ['div', { 'class': 'spoiler' }, contents];
+  } else {
+    return ['span', { 'class': 'spoiler' }, contents];
   }
 });
 
diff --git a/test/javascripts/lib/bbcode_test.js b/test/javascripts/lib/bbcode_test.js
index d1b059180..6eddd0a66 100644
--- a/test/javascripts/lib/bbcode_test.js
+++ b/test/javascripts/lib/bbcode_test.js
@@ -16,6 +16,7 @@ test('basic bbcode', function() {
   format("[b]evil [i]trout[/i][/b]",
          "<span class=\"bbcode-b\">evil <span class=\"bbcode-i\">trout</span></span>",
          "allows embedding of tags");
+  format("[EMAIL]eviltrout@mailinator.com[/EMAIL]", "<a href=\"mailto:eviltrout@mailinator.com\">eviltrout@mailinator.com</a>", "supports upper case bbcode");
 });
 
 test('invalid bbcode', function() {