From 5265275d0d2f78432237d48877760dcc8064a8c1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 20 Jun 2013 12:07:35 -0500 Subject: [PATCH] cleaner content parsing. --- lib/widget.js | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index 85471a3..822e838 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -826,22 +826,6 @@ Element.prototype.emit = function(type) { }; */ -Element.prototype.parseContent = function() { - if (this.detached) return false; - - var w = this.width - (this.border ? 2 : 0); - if (this._clines == null - || this._clines.width !== w - || this._clines.content !== this.content) { - this._clines = wrapContent(this.content, w, this.parseTags, this.align); - this._pcontent = this._clines.join('\n'); - this.emit('parsed content'); - return true; - } - - return false; -}; - Element.prototype.hide = function() { if (this.hidden) return; this.hidden = true; @@ -880,18 +864,36 @@ Element.prototype.focus = function() { }; Element.prototype.setContent = function(content, noClear) { - //var ret = this.render(true); var ret = this._lastPos; - // TODO: Maybe simply set _pcontent with _parseTags result. - // text = text.replace(/\x1b(?!\[[\d;]*m)/g, ''); - this.content = this._parseTags(content || ''); + this.content = content || ''; this.parseContent(); if (ret && !noClear) { - //if (ret && !this.hidden) { this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl); } }; +Element.prototype.parseContent = function() { + if (this.detached) return false; + + var width = this.width - (this.border ? 2 : 0); + if (this._clines == null + || this._clines.width !== width + || this._clines.content !== this.content) { + var content = this.content; + // Could move these 2 lines back to setContent (?) + content = content.replace(/\x1b(?!\[[\d;]*m)/g, ''); + content = this._parseTags(content || ''); + this._clines = wrapContent(content, width, this.parseTags, this.align); + this._clines.width = width; + this._clines.content = this.content; + this._pcontent = this._clines.join('\n'); + this.emit('parsed content'); + return true; + } + + return false; +}; + // Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`. Element.prototype._parseTags = function(text) { if (!this.parseTags) return text; @@ -2477,9 +2479,6 @@ function wrapContent(content, width, tags, state) { out.push(sp(line, width, align)); }); - out.width = width; - out.content = content; - return out; }