BUGFIX: fix username breakup so it works with IE

FEATURE: breakup username based on name as well
This commit is contained in:
Sam 2013-12-30 16:07:35 +11:00
parent b8184d8b92
commit d8c43f7b58
5 changed files with 32 additions and 13 deletions

View File

@ -4,12 +4,13 @@
@method breakUp
@for Handlebars
**/
Handlebars.registerHelper('breakUp', function(property, options) {
Handlebars.registerHelper('breakUp', function(property, hint, options) {
var prop, result, tokens;
prop = Ember.Handlebars.get(this, property, options);
if (!prop) return "";
hint = Ember.Handlebars.get(this, hint, options);
return Discourse.Formatter.breakUp(prop);
return Discourse.Formatter.breakUp(prop, hint);
});
/**

View File

@ -6,22 +6,39 @@ Discourse.Formatter = (function(){
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
shortDate, shortDateNoYear, tinyDateYear, breakUp;
breakUp = function(str){
breakUp = function(str, hint){
var rval = [];
var prev = str[0];
var cur;
var brk = "<wbr>&#8203;";
var hintPos = [];
if(hint) {
hint = hint.toLowerCase().split(/\s+/).reverse();
var current = 0;
while(hint.length > 0) {
var word = hint.pop();
if(word !== str.substr(current, word.length).toLowerCase()) {
break;
}
current += word.length;
hintPos.push(current);
}
}
rval.push(prev);
for (var i=1;i<str.length;i++) {
cur = str[i];
if(prev.match(/[^0-9]/) && cur.match(/[0-9]/)){
rval.push("<wbr>");
rval.push(brk);
} else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){
rval.pop();
rval.push("<wbr>");
rval.push(brk);
rval.push(prev);
} else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){
rval.push("<wbr>");
rval.push(brk);
} else if(hintPos.indexOf(i) > -1) {
rval.push(brk);
}
rval.push(cur);

View File

@ -4,7 +4,7 @@
<div>
<a href='/users/{{unbound username}}'>{{avatar this imageSize="large"}}</a>
</div>
<h5 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}'>{{{breakUp username}}}</a></h5>
<h5 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}'>{{{breakUp username name}}}</a></h5>
</div>
</div>
<div class='span11 topic-body'>

View File

@ -22,7 +22,7 @@
{{#unless userDeleted}}
<div {{bindAttr class=":contents byTopicCreator:topic-creator :trigger-expansion"}}>
<a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{avatar this imageSize="large"}}</a>
<h3 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{{breakUp username}}}</a></h3>
<h3 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{{breakUp username name}}}</a></h3>
{{#if showName}}
<h3><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{breakUp name}}</a></h3>

View File

@ -192,13 +192,14 @@ test("updateRelativeAge", function(){
test("breakUp", function(){
var b = function(s){ return Discourse.Formatter.breakUp(s); };
var b = function(s,hint){ return Discourse.Formatter.breakUp(s,hint); };
equal(b("hello"), "hello");
equal(b("helloworld"), "helloworld");
equal(b("HeMans11"), "He<wbr>Mans<wbr>11");
equal(b("he_man"), "he_<wbr>man");
equal(b("he11111"), "he<wbr>11111");
equal(b("HRCBob"), "HRC<wbr>Bob");
equal(b("HeMans11"), "He<wbr>&#8203;Mans<wbr>&#8203;11");
equal(b("he_man"), "he_<wbr>&#8203;man");
equal(b("he11111"), "he<wbr>&#8203;11111");
equal(b("HRCBob"), "HRC<wbr>&#8203;Bob");
equal(b("bobmarleytoo","Bob Marley Too"), "bob<wbr>&#8203;marley<wbr>&#8203;too");
});