diff --git a/app/assets/javascripts/discourse/helpers/application_helpers.js b/app/assets/javascripts/discourse/helpers/application_helpers.js index df586a31d40..c65bf622169 100644 --- a/app/assets/javascripts/discourse/helpers/application_helpers.js +++ b/app/assets/javascripts/discourse/helpers/application_helpers.js @@ -9,7 +9,7 @@ Handlebars.registerHelper('breakUp', function(property, options) { prop = Ember.Handlebars.get(this, property, options); if (!prop) return ""; - return Discourse.Formatter.breakUp(prop, 13); + return Discourse.Formatter.breakUp(prop); }); /** diff --git a/app/assets/javascripts/discourse/lib/formatter.js b/app/assets/javascripts/discourse/lib/formatter.js index 85f0de23ad1..40976524884 100644 --- a/app/assets/javascripts/discourse/lib/formatter.js +++ b/app/assets/javascripts/discourse/lib/formatter.js @@ -6,39 +6,30 @@ Discourse.Formatter = (function(){ relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase, shortDate, shortDateNoYear, tinyDateYear, breakUp; - breakUp = function(string, maxLength){ - if(string.length <= maxLength) { - return string; - } + breakUp = function(str){ + var rval = []; + var prev = str[0]; + var cur; - var firstPart = string.substr(0, maxLength); - - // work backward to split stuff like ABPoop to AB Poop - var i; - for(i=firstPart.length-1;i>0;i--){ - if(firstPart[i].match(/[A-Z]/)){ - break; + rval.push(prev); + for (var i=1;i"); + } else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){ + rval.pop(); + rval.push(""); + rval.push(prev); + } else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){ + rval.push(""); } + + rval.push(cur); + prev = cur; } - // work forwards to split stuff like ab111 to ab 111 - if(i===0) { - for(i=1;i 0 && i < firstPart.length) { - var offset = 0; - if(string[i] === "_") { - offset = 1; - } - return string.substr(0, i + offset) + " " + string.substring(i + offset); - } else { - return firstPart + " " + string.substr(maxLength); - } }; shortDate = function(date){ diff --git a/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars b/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars index f8ea4fc9b68..31a3d740a53 100644 --- a/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars +++ b/app/assets/javascripts/discourse/templates/embedded_post.js.handlebars @@ -4,7 +4,7 @@
{{avatar this imageSize="large"}}
-
{{breakUp username}}
+
{{{breakUp username}}}
diff --git a/app/assets/javascripts/discourse/templates/post.js.handlebars b/app/assets/javascripts/discourse/templates/post.js.handlebars index 4e25dca7b63..5349123210c 100644 --- a/app/assets/javascripts/discourse/templates/post.js.handlebars +++ b/app/assets/javascripts/discourse/templates/post.js.handlebars @@ -22,7 +22,7 @@ {{#unless userDeleted}}
{{avatar this imageSize="large"}} -

{{breakUp username}}

+

{{{breakUp username}}}

{{#if showName}}

{{breakUp name}}

@@ -85,4 +85,4 @@ -{{post-gap post=this postStream=controller.postStream before="false"}} \ No newline at end of file +{{post-gap post=this postStream=controller.postStream before="false"}} diff --git a/test/javascripts/lib/formatter_test.js b/test/javascripts/lib/formatter_test.js index 0767ca379e8..fef48e1b115 100644 --- a/test/javascripts/lib/formatter_test.js +++ b/test/javascripts/lib/formatter_test.js @@ -192,13 +192,13 @@ test("updateRelativeAge", function(){ test("breakUp", function(){ - var b = function(s){ return Discourse.Formatter.breakUp(s,5); }; + var b = function(s){ return Discourse.Formatter.breakUp(s); }; equal(b("hello"), "hello"); - equal(b("helloworld"), "hello world"); - equal(b("HeMans"), "He Mans"); - equal(b("he_man"), "he_ man"); - equal(b("he11111"), "he 11111"); - equal(b("HRCBob"), "HRC Bob"); + equal(b("helloworld"), "helloworld"); + equal(b("HeMans11"), "HeMans11"); + equal(b("he_man"), "he_man"); + equal(b("he11111"), "he11111"); + equal(b("HRCBob"), "HRCBob"); });