textLength improvement.

This commit is contained in:
Christopher Jeffrey 2015-03-31 02:54:01 -07:00
parent 7a3525be77
commit 51c3d88a69
2 changed files with 5 additions and 1 deletions

View File

@ -487,6 +487,7 @@ parameter must be a string.
- __getLines()__ - an array containing the content lines.
- __getScreenLines()__ - an array containing the lines as they are displayed on
the screen.
- __textLength(text)__ - get a string's real length, taking into account tags.
#### Box (from Element)

View File

@ -2307,7 +2307,10 @@ Element.prototype.textLength = function(text) {
if (!this.options.tags && !this.options.parseTags) {
return text.length;
}
return text.replace(/{(\/?)([\w\-,;!#]*)}/g, '').length;
return text
.replace(/{(\/?)([\w\-,;!#]*)}/g, '')
.replace(/\x1b\[[\d;]*m/g, '')
.length;
};
// Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`.