Skip to content

Commit

Permalink
the composer will now count only significant whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed Mar 12, 2013
1 parent cde9d8c commit 439ac95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/discourse/models/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,15 @@ Discourse.Composer = Discourse.Model.extend({
},

/**
Computes the length of the reply minus the quote(s).
Computes the length of the reply minus the quote(s) and non-significant whitespaces
@property replyLength
**/
replyLength: function() {
var reply = this.get('reply');
if(!reply) reply = "";
while (Discourse.BBCode.QUOTE_REGEXP.test(reply)) { reply = reply.replace(Discourse.BBCode.QUOTE_REGEXP, ""); }
return reply.trim().length;
return reply.replace(/\s+/img, " ").trim().length;
}.property('reply')

});
Expand Down
8 changes: 7 additions & 1 deletion spec/javascripts/models/composer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ describe("Discourse.Composer", function() {
});

it("trims whitespaces", function() {
var composer = Discourse.Composer.create({ reply: "\nbasic reply\t" });
var composer = Discourse.Composer.create({ reply: " \nbasic reply\t" });
expect(composer.get('replyLength')).toBe(11);
});

it("count only significant whitespaces", function() {
// this will count the '\n' only once
var composer = Discourse.Composer.create({ reply: "ba sic\n\nreply" });
expect(composer.get('replyLength')).toBe(12);
});

it("removes quotes", function() {
var composer = Discourse.Composer.create({ reply: "1[quote=]not counted[/quote]2[quote=]at all[/quote]3" });
expect(composer.get('replyLength')).toBe(3);
Expand Down

0 comments on commit 439ac95

Please sign in to comment.