add setText and getText methods. expose element and node.

This commit is contained in:
Christopher Jeffrey 2013-08-04 04:50:13 -05:00
parent dadf185a87
commit 8ebbbbac65
2 changed files with 27 additions and 4 deletions

View File

@ -372,6 +372,10 @@ parameter must be a string.
and tags will be replaced with SGR codes (if enabled). and tags will be replaced with SGR codes (if enabled).
- **getContent()** - return content, slightly different from `el.content`. - **getContent()** - return content, slightly different from `el.content`.
assume the above formatting. assume the above formatting.
- **setText(text)** - similar to `setContent`, but ignore tags and remove escape
codes.
- **getText()** - similar to `getContent`, but return content with tags and
escape codes removed.
- **insertLine(i, lines)** - insert a line into the box's content. - **insertLine(i, lines)** - insert a line into the box's content.
- **deleteLine(i)** - delete a line from the box's content. - **deleteLine(i)** - delete a line from the box's content.
- **getLine(i)** - get a line from the box's content. - **getLine(i)** - get a line from the box's content.

View File

@ -1948,17 +1948,27 @@ Element.prototype.focus = function() {
return this.screen.focused = this; return this.screen.focused = this;
}; };
Element.prototype.setContent = function(content, noClear) { Element.prototype.setContent = function(content, noClear, noTags) {
if (!noClear) this.clearPos(); if (!noClear) this.clearPos();
this.content = content || ''; this.content = content || '';
this.parseContent(); this.parseContent(noTags);
}; };
Element.prototype.getContent = function() { Element.prototype.getContent = function() {
return this._clines.fake.join('\n'); return this._clines.fake.join('\n');
}; };
Element.prototype.parseContent = function() { Element.prototype.setText = function(content, noClear) {
content = content || '';
content = content.replace(/\x1b\[[\d;]*m/g, '');
return this.setContent(content, noClear, true);
};
Element.prototype.getText = function() {
return this.getContent().replace(/\x1b\[[\d;]*m/g, '');
};
Element.prototype.parseContent = function(noTags) {
if (this.detached) return false; if (this.detached) return false;
var width = this.width - this.iwidth; var width = this.width - this.iwidth;
@ -1966,13 +1976,18 @@ Element.prototype.parseContent = function() {
|| this._clines.width !== width || this._clines.width !== width
|| this._clines.content !== this.content) { || this._clines.content !== this.content) {
var content = this.content; var content = this.content;
content = content content = content
.replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g, '') .replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g, '')
.replace(/\x1b(?!\[[\d;]*m)/g, '') .replace(/\x1b(?!\[[\d;]*m)/g, '')
.replace(/\r\n|\r/g, '\n') .replace(/\r\n|\r/g, '\n')
.replace(/\t/g, this.screen.tabc) .replace(/\t/g, this.screen.tabc)
.replace(dwidthChars, '?'); .replace(dwidthChars, '?');
content = this._parseTags(content);
if (!noTags) {
content = this._parseTags(content);
}
this._clines = this._wrapContent(content, width); this._clines = this._wrapContent(content, width);
this._clines.width = width; this._clines.width = width;
this._clines.content = this.content; this._clines.content = this.content;
@ -1982,8 +1997,10 @@ Element.prototype.parseContent = function() {
this._clines.ci.push(total); this._clines.ci.push(total);
return total + line.length + 1; return total + line.length + 1;
}.bind(this), 0); }.bind(this), 0);
this._pcontent = this._clines.join('\n'); this._pcontent = this._clines.join('\n');
this.emit('parsed content'); this.emit('parsed content');
return true; return true;
} }
@ -5781,7 +5798,9 @@ var dwidthChars = new RegExp('(['
* Expose * Expose
*/ */
exports.Node = exports.node = Node;
exports.Screen = exports.screen = Screen; exports.Screen = exports.screen = Screen;
exports.Element = exports.element = Element;
exports.Box = exports.box = Box; exports.Box = exports.box = Box;
exports.Text = exports.text = Text; exports.Text = exports.text = Text;
exports.Line = exports.line = Line; exports.Line = exports.line = Line;