mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-04 04:59:17 +08:00
61eede4501
* fix: 修复 office-viewer 不支持问边框组合问题 * 修复表格行替换导致内容插入到最后问题 * 优化正则性能
82 lines
1.8 KiB
TypeScript
82 lines
1.8 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import Word from '../../src/Word';
|
|
import {replaceVar} from '../../src/util/replaceVar';
|
|
import {buildXML} from '../../src/util/xml';
|
|
import {mergeRun} from '../../src/util/mergeRun';
|
|
import xmlFormat from 'xml-formatter';
|
|
|
|
export function createWord(fileName: string, data: any) {
|
|
const xmlContent = new Uint8Array(
|
|
fs.readFileSync(path.join(__dirname, fileName), null).buffer
|
|
);
|
|
return new Word(xmlContent, {
|
|
enableVar: true,
|
|
data,
|
|
evalVar: (name, d: any) => {
|
|
if (name in d) {
|
|
return d[name];
|
|
}
|
|
return undefined;
|
|
}
|
|
});
|
|
}
|
|
|
|
test('table-list', async () => {
|
|
const data = {
|
|
users: [
|
|
{
|
|
name: 'u1',
|
|
age: 10,
|
|
location: 'l1'
|
|
},
|
|
{
|
|
name: 'u2',
|
|
age: 11,
|
|
location: 'l2'
|
|
}
|
|
]
|
|
};
|
|
|
|
const word = createWord('./var/table-list.docx', data);
|
|
|
|
const documentData = word.getXML('word/document.xml');
|
|
mergeRun(word, documentData);
|
|
replaceVar(word, documentData);
|
|
|
|
const xmlResult = xmlFormat(
|
|
buildXML(documentData.getElementsByTagName('w:tbl').item(0)!),
|
|
{lineSeparator: '\n'}
|
|
);
|
|
|
|
const expectResult = fs.readFileSync(
|
|
path.join(__dirname, './var/table-list.xml'),
|
|
'utf-8'
|
|
);
|
|
|
|
expect(xmlResult).toBe(expectResult);
|
|
});
|
|
|
|
test('var-space', async () => {
|
|
// 之前空格会影响变量
|
|
const data = {
|
|
date: '2020-01-01'
|
|
};
|
|
|
|
const word = createWord('./var/space.docx', data);
|
|
|
|
document.body.innerHTML = `
|
|
<div id="root"></div>
|
|
`;
|
|
const root = document.getElementById('root')!;
|
|
await word.render(root);
|
|
|
|
const spans = document.getElementsByTagName('span');
|
|
const lastSpan = spans.item(spans.length - 1)!;
|
|
|
|
expect(lastSpan!.innerHTML).toBe(
|
|
' 2020-01-01'
|
|
);
|
|
});
|