Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
fix render: rowSpan next to colSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 16, 2014
1 parent 898bc66 commit 1abc5cb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ Cell.prototype.drawLine = function(lineNum,drawRight,forceTruncationSymbol,spann
var left = this.chars[this.x == 0 ? 'left' : 'middle'];
if(this.x && spanningCell && this.cells){
var cellLeft = this.cells[this.y+spanningCell][this.x-1];
while(cellLeft instanceof NoOpCell){
cellLeft = this.cells[cellLeft.y][cellLeft.x-1];
}
if(!(cellLeft instanceof RowSpanCell)){
left = this.chars['right-mid'];
}
Expand Down Expand Up @@ -222,6 +225,9 @@ Cell.prototype.drawEmpty = function(drawRight,spanningCell){
var left = this.chars[this.x == 0 ? 'left' : 'middle'];
if(this.x && spanningCell && this.cells){
var cellLeft = this.cells[this.y+spanningCell][this.x-1];
while(cellLeft instanceof NoOpCell){
cellLeft = this.cells[cellLeft.y][cellLeft.x-1];
}
if(!(cellLeft instanceof RowSpanCell)){
left = this.chars['right-mid'];
}
Expand All @@ -242,6 +248,12 @@ NoOpCell.prototype.draw = function(){
return '';
};

NoOpCell.prototype.init = function(tableOptions, x, y){
this.x = x;
this.y = y;
};


/**
* A placeholder Cell for a Cell that spans multiple rows.
* It delegates rendering to the original cell, but adds the appropriate offset.
Expand All @@ -268,7 +280,6 @@ RowSpanCell.prototype.draw = function(lineNum){
return this.originalCell.draw(this.offset + 1 + lineNum);
};

NoOpCell.prototype.init =
NoOpCell.prototype.mergeTableOptions =
RowSpanCell.prototype.mergeTableOptions = function(){};

Expand Down
44 changes: 44 additions & 0 deletions test/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,50 @@ describe('Table', function () {
expect(table.toString()).to.equal(expected.join('\n'));
});

it('rowSpan to the right of a colspan',function(){
var table = new Table({style:{head:[],border:[]}});

table.push(
[{content:'hello',colSpan:2},{rowSpan:2, colSpan:2,content:'sup'},{rowSpan:3,content:'hi'}],
[{content:'howdy',colSpan:2}],
['o','k','','']
);

var expected = [
'┌───────┬─────┬────┐'
, '│ hello │ sup │ hi │'
, '├───────┤ │ │'
, '│ howdy │ │ │'
, '├───┬───┼──┬──┤ │'
, '│ o │ k │ │ │ │'
, '└───┴───┴──┴──┴────┘'
];

expect(table.toString()).to.equal(expected.join('\n'));
});

it('rowSpan to the right of a non-empty line',function(){
var table = new Table({style:{head:[],border:[]}});

table.push(
[{content:'hello',colSpan:2},{rowSpan:2, colSpan:2,content:'sup\nsup'},{rowSpan:3,content:'hi\nhi'}],
[{content:'howdy',colSpan:2}],
['o','k','','']
);

var expected = [
'┌───────┬─────┬────┐'
, '│ hello │ sup │ hi │'
, '├───────┤ sup │ hi │'
, '│ howdy │ │ │'
, '├───┬───┼──┬──┤ │'
, '│ o │ k │ │ │ │'
, '└───┴───┴──┴──┴────┘'
];

expect(table.toString()).to.equal(expected.join('\n'));
});

it('rowSpan to the right - multiline content',function(){
var table = new Table({style:{head:[],border:[]}});

Expand Down

0 comments on commit 1abc5cb

Please sign in to comment.