Skip to content

Commit

Permalink
fix: check for negative rows before moving (shaka-project#4510)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamaddox committed Sep 27, 2022
1 parent db68664 commit b3621c2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/cea/cea608_memory.js
Expand Up @@ -184,6 +184,10 @@ shaka.cea.Cea608Memory = class {
* @param {number} count Count of rows to move.
*/
moveRows(dst, src, count) {
if (src < 0 || dst < 0) {
return;
}

if (dst >= src) {
for (let i = count-1; i >= 0; i--) {
this.rows_[dst + i] = this.rows_[src + i].map((e) => e);
Expand Down
88 changes: 88 additions & 0 deletions test/cea/cea608_memory_unit.js
Expand Up @@ -295,5 +295,93 @@ describe('Cea608Memory', () => {
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});

it('does not move rows if source row index is negative', () => {
const startTime = 1;
const endTime = 2;
const text = 'test';

// Add the text to the buffer, each character on separate rows.
// At this point, the memory looks like:
// [1]: t
// [2]: e
// [3]: s
// [4]: t
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
memory.setRow(memory.getRow() + 1); // increment row
}

const srcRowIdx = -1;
const dstRowIdx = 2;
const rowsToMove = 3;
memory.moveRows(dstRowIdx, srcRowIdx, rowsToMove);

// Expected text is 't\ne\ns\nt'
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, 't'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 'e'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 's'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 't'),
];

const expectedCaption = {
stream,
cue: topLevelCue,
};

// Force out the new memory.
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});

it('does not move rows if destination row index is negative', () => {
const startTime = 1;
const endTime = 2;
const text = 'test';

// Add the text to the buffer, each character on separate rows.
// At this point, the memory looks like:
// [1]: t
// [2]: e
// [3]: s
// [4]: t
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
memory.setRow(memory.getRow() + 1); // increment row
}

const srcRowIdx = 1;
const dstRowIdx = -2;
const rowsToMove = 3;
memory.moveRows(dstRowIdx, srcRowIdx, rowsToMove);

// Expected text is 't\ne\ns\nt'
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, 't'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 'e'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 's'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 't'),
];

const expectedCaption = {
stream,
cue: topLevelCue,
};

// Force out the new memory.
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});
});
});

0 comments on commit b3621c2

Please sign in to comment.