diff --git a/src/BufferLine.test.ts b/src/BufferLine.test.ts index 2018554b..bf4f5a2d 100644 --- a/src/BufferLine.test.ts +++ b/src/BufferLine.test.ts @@ -94,4 +94,25 @@ describe('BufferLine', function(): void { line.replaceCells(2, 4, [6, 'f', 0, 0]); chai.expect(line.toArray()).eql([[1, 'a', 0, 0], [2, 'b', 0, 0], [6, 'f', 0, 0], [6, 'f', 0, 0], [5, 'e', 0, 0]]); }); + it('DEFAULT_CELL is not affected by cell writes', function(): void { + // get default values: + const attr = DEFAULT_CELL[CHAR_DATA_ATTR_INDEX]; + const char = DEFAULT_CELL[CHAR_DATA_CHAR_INDEX]; + const width = DEFAULT_CELL[CHAR_DATA_WIDTH_INDEX]; + const code = DEFAULT_CELL[CHAR_DATA_CODE_INDEX]; + // create a line with DEFAULT_CELL + const line = new TestBufferLine(3); + // alter first cell only + const first = line.get(0); + // this is bad - never edit a cell after a get!!!! (needs to be fixed in InputHandler.print) + // Note this is currently granted in the codebase by the way + // a blankLine was/is created - all cells point to the same + // CharData object + // we test here, that this unique blankLine object is not + // pointing to the DEFAULT_CELL object + first[CHAR_DATA_ATTR_INDEX] = 123456789; + chai.expect(line.toArray()).eql([[123456789, char, width, code], [123456789, char, width, code], [123456789, char, width, code]]); + chai.expect(DEFAULT_CELL[CHAR_DATA_ATTR_INDEX]).equals(attr); + chai.expect(DEFAULT_CELL[CHAR_DATA_ATTR_INDEX]).not.equals(123456789); + }); }); diff --git a/src/BufferLine.ts b/src/BufferLine.ts index e0879b72..a57bdf74 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -24,10 +24,10 @@ export class BufferLine implements IBufferLine { this.length = this._data.length; if (cols) { if (!ch) { - ch = DEFAULT_CELL; + ch = [DEFAULT_CELL[0], DEFAULT_CELL[1], DEFAULT_CELL[2], DEFAULT_CELL[3]]; } for (let i = 0; i < cols; i++) { - this.push(ch); // Note: the ctor ch is not cloned + this.push(ch); // Note: the ctor ch is not cloned (resembles old behavior) } } if (isWrapped) { @@ -79,7 +79,7 @@ export class BufferLine implements IBufferLine { /** replace cells from pos to pos + n - 1 with fill */ public replaceCells(start: number, end: number, fill: CharData): void { while (start < end && start < this.length) { - this.set(start++, fill); // Note: fill is not cloned + this.set(start++, fill); // Note: fill is not cloned (resembles old behavior) } } }