63 lines
4.0 KiB
JavaScript
63 lines
4.0 KiB
JavaScript
const test=require('node:test'),assert=require('node:assert/strict');
|
|
require('./csv-core.js');const csv=globalThis.PagesCSV;
|
|
test('CSV parsing preserves untouched bytes and logical field values',()=>{
|
|
for(const source of ['', '\ufeff', 'a', 'a,', '\n', '""', 'a\r\nb\n', '\ufeff"name",description\r\n日本語,"a,b\r\nnext"\r\n', '"a""b",=1+1\n'])assert.equal(csv.serialize(csv.parse(source)),source);
|
|
const s='\ufeff"name",description\r\n日本語,"a,b\r\nnext"\r\n';
|
|
assert.equal(csv.parse(s).rows[1].cells[1].value,'a,b\nnext');
|
|
assert.equal(csv.edit(s,'cell',1,0,'新しい,名前'),'\ufeff"name",description\r\n"新しい,名前","a,b\r\nnext"\r\n');
|
|
assert.equal(csv.edit(s,'cell',1,1,'a,b\nnext'),s);
|
|
assert.equal(csv.edit('a','cell',0,0,''),'\r\n');
|
|
});
|
|
test('Row and column operations preserve fields; empty and ragged data are explicit',()=>{
|
|
assert.equal(csv.edit('a,b\nc\n','add-column'),'a,b,\nc,,\n');
|
|
assert.equal(csv.edit('a,b\nc\n','delete-column',0,1),'a\nc\n');
|
|
assert.equal(csv.edit('a,b\r\n"c",d','delete-row',0),'"c",d');
|
|
assert.equal(csv.edit('a,b','add-row'),'a,b\r\n,\r\n');
|
|
assert.equal(csv.parse(csv.edit('','add-row')).rows.length,1);
|
|
assert.equal(csv.edit('a\n','delete-row',0),'');
|
|
});
|
|
test('Malformed and excessive data fail without truncation',()=>{
|
|
for(const source of ['"unfinished','a"b','"a"x','a\rb','a\0b','x'.repeat(256*1024+1),'x'.repeat(16385), 'a,'.repeat(32), 'a\n'.repeat(201)])assert.throws(()=>csv.parse(source));
|
|
assert.throws(()=>csv.edit('a\n'.repeat(200),'add-row'));
|
|
assert.throws(()=>csv.edit('a','cell',0,0,'x'.repeat(16385)));
|
|
assert.throws(()=>csv.edit('a','cell',2,0,'bad'));
|
|
});
|
|
test('Quotes, HTML, formulas and newlines remain inert CSV data',()=>{
|
|
for(const value of ['"','a,b','<img src=x onerror=alert(1)>','{{children}}','=SUM(A1:A9)',' leading','日本語\n次の行','a\r\nb']){
|
|
const source=csv.edit('old,unchanged\r\n','cell',0,0,value);
|
|
assert.equal(csv.parse(source).rows[0].cells[0].value,value.replace(/\r\n/g,'\n'));
|
|
assert.ok(source.endsWith(',unchanged\r\n'));
|
|
}
|
|
});
|
|
test('Tab-separated paste preserves untouched CSV bytes and treats commas as data',()=>{
|
|
const source='\ufeff"keep",old,last\r\n"unchanged",x,y\n';
|
|
assert.equal(csv.paste(source,0,1,'日本語,値\t"a""b"\r\nnext\tfinal\r\n'),
|
|
'\ufeff"keep","日本語,値","a""b"\r\n"unchanged",next,final\n');
|
|
assert.equal(csv.paste(source,0,0,'keep\told\tlast\nunchanged\tx\ty\n'),source);
|
|
assert.equal(csv.paste('a,b\n',0,0,'"line1\r\nline2"\t"tab\tvalue"\n'),'"line1\nline2",tab\tvalue\n');
|
|
});
|
|
test('Paste grows only required rows and cells, preserving ragged data',()=>{
|
|
assert.equal(csv.paste('a,b\nc\nz\n',0,1,'x\ty\nz'),'a,x,y\nc,z\nz\n');
|
|
assert.equal(csv.paste('a,b',0,1,'x\ty\nz\tw'),'a,x,y\r\n,z,w\r\n');
|
|
assert.equal(csv.paste('',0,0,'a\tb\n'),'a,b\r\n');
|
|
assert.equal(csv.paste('a,b\n',0,0,'\t\n'),',\n');
|
|
assert.equal(csv.paste('a,b\n',0,0,''),'a,b\n');
|
|
});
|
|
test('Paste validates both input and final CSV size without partial writes',()=>{
|
|
const source='original,untouched\r\n';
|
|
for(const text of ['a\tb\n"unfinished','a\0b\tc','x'.repeat(16385)+'\tb','a\n'.repeat(201),'a\t'.repeat(32)])assert.throws(()=>csv.paste(source,0,0,text));
|
|
for(const [row,col] of [[-1,0],[0,-1],[0.5,0],[0,NaN],[200,0],[0,32]])assert.throws(()=>csv.paste(source,row,col,'a\tb'));
|
|
assert.throws(()=>csv.paste(source,199,0,'a\nb'));
|
|
assert.throws(()=>csv.paste(source,0,31,'a\tb'));
|
|
// Valid individual cells can still exceed the total document limit.
|
|
const big=Array(16).fill('x'.repeat(16000)).join(',');
|
|
assert.throws(()=>csv.paste(big,0,16,'y'.repeat(16000)));
|
|
// Quoting comma-heavy clipboard data expands the resulting CSV.
|
|
assert.throws(()=>csv.paste('',0,0,Array(16).fill(','.repeat(16383)).join('\t')));
|
|
assert.equal(source,'original,untouched\r\n');
|
|
});
|
|
test('Pasted HTML, macros and formulas remain values rather than executable content',()=>{
|
|
const values=['<img src=x onerror=alert(1)>','{{children}}','=SUM(A1:A9)'];
|
|
assert.deepEqual(csv.parse(csv.paste('',0,0,values.join('\t'))).rows[0].cells.map(c=>c.value),values);
|
|
});
|