Compare commits
No commits in common. "main" and "v0.18.0" have entirely different histories.
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,7 +3,5 @@ node_modules/
|
||||
build/*
|
||||
|
||||
.~lock*
|
||||
**/*(Copie)*
|
||||
**/*(Copy)*
|
||||
|
||||
stats.html
|
||||
78
package-lock.json
generated
78
package-lock.json
generated
@ -1,17 +1,16 @@
|
||||
{
|
||||
"name": "@odfjs/odfjs",
|
||||
"version": "0.30.0",
|
||||
"version": "0.18.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@odfjs/odfjs",
|
||||
"version": "0.30.0",
|
||||
"version": "0.18.0",
|
||||
"dependencies": {
|
||||
"@xmldom/xmldom": "^0.9.8",
|
||||
"@zip.js/zip.js": "^2.7.57",
|
||||
"image-size": "^2.0.2",
|
||||
"ses": "^1.14.0"
|
||||
"ses": "^1.12.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^25.0.7",
|
||||
@ -42,22 +41,10 @@
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@endo/cache-map": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@endo/cache-map/-/cache-map-1.1.0.tgz",
|
||||
"integrity": "sha512-owFGshs/97PDw9oguZqU/px8Lv1d0KjAUtDUiPwKHNXRVUE/jyettEbRoTbNJR1OaI8biMn6bHr9kVJsOh6dXw==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/@endo/env-options": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/@endo/env-options/-/env-options-1.1.11.tgz",
|
||||
"integrity": "sha512-p9OnAPsdqoX4YJsE98e3NBVhIr2iW9gNZxHhAI2/Ul5TdRfoOViItzHzTqrgUVopw6XxA1u1uS6CykLMDUxarA==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/@endo/immutable-arraybuffer": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@endo/immutable-arraybuffer/-/immutable-arraybuffer-1.1.2.tgz",
|
||||
"integrity": "sha512-u+NaYB2aqEugQ3u7w3c5QNkPogf8q/xGgsPaqdY6pUiGWtYiTiFspKFcha6+oeZhWXWQ23rf0KrUq0kfuzqYyQ==",
|
||||
"version": "1.1.8",
|
||||
"resolved": "https://registry.npmjs.org/@endo/env-options/-/env-options-1.1.8.tgz",
|
||||
"integrity": "sha512-Xtxw9n33I4guo8q0sDyZiRuxlfaopM454AKiELgU7l3tqsylCut6IBZ0fPy4ltSHsBib7M3yF7OEMoIuLwzWVg==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/@jridgewell/gen-mapping": {
|
||||
@ -2198,18 +2185,6 @@
|
||||
"node": ">=10 <11 || >=12 <13 || >=14"
|
||||
}
|
||||
},
|
||||
"node_modules/image-size": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/image-size/-/image-size-2.0.2.tgz",
|
||||
"integrity": "sha512-IRqXKlaXwgSMAMtpNzZa1ZAe8m+Sa1770Dhk8VkSsP9LS+iHD62Zd8FQKs8fbPiagBE7BzoFX23cxFnwshpV6w==",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"image-size": "bin/image-size.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16.x"
|
||||
}
|
||||
},
|
||||
"node_modules/immutable": {
|
||||
"version": "4.2.4",
|
||||
"resolved": "https://registry.npmjs.org/immutable/-/immutable-4.2.4.tgz",
|
||||
@ -3640,14 +3615,12 @@
|
||||
}
|
||||
},
|
||||
"node_modules/ses": {
|
||||
"version": "1.14.0",
|
||||
"resolved": "https://registry.npmjs.org/ses/-/ses-1.14.0.tgz",
|
||||
"integrity": "sha512-T07hNgOfVRTLZGwSS50RnhqrG3foWP+rM+Q5Du4KUQyMLFI3A8YA4RKl0jjZzhihC1ZvDGrWi/JMn4vqbgr/Jg==",
|
||||
"version": "1.12.0",
|
||||
"resolved": "https://registry.npmjs.org/ses/-/ses-1.12.0.tgz",
|
||||
"integrity": "sha512-jvmwXE2lFxIIY1j76hFjewIIhYMR9Slo3ynWZGtGl5M7VUCw3EA0wetS+JCIbl2UcSQjAT0yGAHkyxPJreuC9w==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@endo/cache-map": "^1.1.0",
|
||||
"@endo/env-options": "^1.1.11",
|
||||
"@endo/immutable-arraybuffer": "^1.1.2"
|
||||
"@endo/env-options": "^1.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/set-blocking": {
|
||||
@ -4598,20 +4571,10 @@
|
||||
"@jridgewell/trace-mapping": "^0.3.9"
|
||||
}
|
||||
},
|
||||
"@endo/cache-map": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@endo/cache-map/-/cache-map-1.1.0.tgz",
|
||||
"integrity": "sha512-owFGshs/97PDw9oguZqU/px8Lv1d0KjAUtDUiPwKHNXRVUE/jyettEbRoTbNJR1OaI8biMn6bHr9kVJsOh6dXw=="
|
||||
},
|
||||
"@endo/env-options": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/@endo/env-options/-/env-options-1.1.11.tgz",
|
||||
"integrity": "sha512-p9OnAPsdqoX4YJsE98e3NBVhIr2iW9gNZxHhAI2/Ul5TdRfoOViItzHzTqrgUVopw6XxA1u1uS6CykLMDUxarA=="
|
||||
},
|
||||
"@endo/immutable-arraybuffer": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@endo/immutable-arraybuffer/-/immutable-arraybuffer-1.1.2.tgz",
|
||||
"integrity": "sha512-u+NaYB2aqEugQ3u7w3c5QNkPogf8q/xGgsPaqdY6pUiGWtYiTiFspKFcha6+oeZhWXWQ23rf0KrUq0kfuzqYyQ=="
|
||||
"version": "1.1.8",
|
||||
"resolved": "https://registry.npmjs.org/@endo/env-options/-/env-options-1.1.8.tgz",
|
||||
"integrity": "sha512-Xtxw9n33I4guo8q0sDyZiRuxlfaopM454AKiELgU7l3tqsylCut6IBZ0fPy4ltSHsBib7M3yF7OEMoIuLwzWVg=="
|
||||
},
|
||||
"@jridgewell/gen-mapping": {
|
||||
"version": "0.3.2",
|
||||
@ -6166,11 +6129,6 @@
|
||||
"integrity": "sha512-yiWd4GVmJp0Q6ghmM2B/V3oZGRmjrKLXvHR3TE1nfoXsmoggllfZUQe74EN0fJdPFZu2NIvNdrMMLm3OsV7Ohw==",
|
||||
"dev": true
|
||||
},
|
||||
"image-size": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/image-size/-/image-size-2.0.2.tgz",
|
||||
"integrity": "sha512-IRqXKlaXwgSMAMtpNzZa1ZAe8m+Sa1770Dhk8VkSsP9LS+iHD62Zd8FQKs8fbPiagBE7BzoFX23cxFnwshpV6w=="
|
||||
},
|
||||
"immutable": {
|
||||
"version": "4.2.4",
|
||||
"resolved": "https://registry.npmjs.org/immutable/-/immutable-4.2.4.tgz",
|
||||
@ -7180,13 +7138,11 @@
|
||||
}
|
||||
},
|
||||
"ses": {
|
||||
"version": "1.14.0",
|
||||
"resolved": "https://registry.npmjs.org/ses/-/ses-1.14.0.tgz",
|
||||
"integrity": "sha512-T07hNgOfVRTLZGwSS50RnhqrG3foWP+rM+Q5Du4KUQyMLFI3A8YA4RKl0jjZzhihC1ZvDGrWi/JMn4vqbgr/Jg==",
|
||||
"version": "1.12.0",
|
||||
"resolved": "https://registry.npmjs.org/ses/-/ses-1.12.0.tgz",
|
||||
"integrity": "sha512-jvmwXE2lFxIIY1j76hFjewIIhYMR9Slo3ynWZGtGl5M7VUCw3EA0wetS+JCIbl2UcSQjAT0yGAHkyxPJreuC9w==",
|
||||
"requires": {
|
||||
"@endo/cache-map": "^1.1.0",
|
||||
"@endo/env-options": "^1.1.11",
|
||||
"@endo/immutable-arraybuffer": "^1.1.2"
|
||||
"@endo/env-options": "^1.1.8"
|
||||
}
|
||||
},
|
||||
"set-blocking": {
|
||||
|
||||
11
package.json
11
package.json
@ -1,12 +1,8 @@
|
||||
{
|
||||
"name": "@odfjs/odfjs",
|
||||
"version": "0.30.0",
|
||||
"version": "0.18.0",
|
||||
"type": "module",
|
||||
"exports": "./exports.js",
|
||||
"files": [
|
||||
"exports.js",
|
||||
"scripts"
|
||||
],
|
||||
"imports": {
|
||||
"#DOM": {
|
||||
"node": "./scripts/DOM/node.js",
|
||||
@ -21,7 +17,7 @@
|
||||
"test": "ava"
|
||||
},
|
||||
"repository": {
|
||||
"url": "https://source.netsyms.com/PostalPortal/odfjs.git"
|
||||
"url": "https://github.com/odfjs/odfjs.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^25.0.7",
|
||||
@ -41,7 +37,6 @@
|
||||
"dependencies": {
|
||||
"@xmldom/xmldom": "^0.9.8",
|
||||
"@zip.js/zip.js": "^2.7.57",
|
||||
"image-size": "^2.0.2",
|
||||
"ses": "^1.14.0"
|
||||
"ses": "^1.12.0"
|
||||
}
|
||||
}
|
||||
|
||||
29
readme.md
29
readme.md
@ -18,7 +18,7 @@ Small lib to parse/understand .odf files (.odt, .ods) in the browser and node.js
|
||||
### Install
|
||||
|
||||
```sh
|
||||
npm i https://github.com/odfjs/odfjs.git#v0.30.0
|
||||
npm i https://github.com/odfjs/odfjs.git#v0.18.0
|
||||
```
|
||||
|
||||
|
||||
@ -30,7 +30,7 @@ import {tableRawContentToObjects, tableWithoutEmptyRows, getODSTableRawContent}
|
||||
/**
|
||||
* @param {ArrayBuffer} odsFile - content of an .ods file
|
||||
* @return {Promise<any[]>}
|
||||
*/
|
||||
*/
|
||||
async function getFileData(odsFile){
|
||||
return getODSTableRawContent(odsFile)
|
||||
.then(tableWithoutEmptyRows)
|
||||
@ -38,9 +38,9 @@ async function getFileData(odsFile){
|
||||
}
|
||||
```
|
||||
|
||||
The return value is an array of objects where
|
||||
the **keys** are the column names in the first row and
|
||||
the **values** are automatically converted from the .ods files (which type numbers, strings, booleans and dates)
|
||||
The return value is an array of objects where
|
||||
the **keys** are the column names in the first row and
|
||||
the **values** are automatically converted from the .ods files (which type numbers, strings, booleans and dates)
|
||||
to the appropriate JavaScript value
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ odf.js proposes a template syntax
|
||||
In an .odt file, write the following:
|
||||
|
||||
```txt
|
||||
Hey {nom}!
|
||||
Hey {nom}!
|
||||
|
||||
Your birthdate is {dateNaissance}
|
||||
```
|
||||
@ -99,7 +99,8 @@ And then run the code:
|
||||
```js
|
||||
import {join} from 'node:path';
|
||||
|
||||
import {getOdtTemplate, fillOdtTemplate} from '@odfjs/odfjs'
|
||||
import {getOdtTemplate} from '../scripts/odf/odtTemplate-forNode.js'
|
||||
import {fillOdtTemplate} from '../scripts/node.js'
|
||||
|
||||
// replace with your template path
|
||||
const templatePath = join(import.meta.dirname, './tests/data/template-anniversaire.odt')
|
||||
@ -125,19 +126,6 @@ There are also loops in the form:
|
||||
They can be used to generate lists or tables in .odt files from data and a template using this syntax
|
||||
|
||||
|
||||
#### Securing calls to fillOdtTemplate
|
||||
|
||||
`fillOdtTemplate` evaluate arbitrary JavaScript code in `{#each <collection> as élément}` and `{#if <condition>}` and in `{<expression>}`
|
||||
|
||||
By default, `fillOdtTemplate` limits access to global functions to only ECMAScript defaults via the use of [ses' Compartment](https://www.npmjs.com/package/ses#compartment), this prevents naïve data exfiltration
|
||||
|
||||
However, `fillOdtTemplate` is vulnerable to [prototype pollution](https://cheatsheetseries.owasp.org/cheatsheets/Prototype_Pollution_Prevention_Cheat_Sheet.html) inside template code. Two main ways to be secure are:
|
||||
- control the set of possible templates
|
||||
- call ses' `lockdown` which freezes Javascript intrinsics before calling `fillOdtTemplate` (this may lead to incompatibilities)
|
||||
|
||||
|
||||
|
||||
|
||||
### Demo
|
||||
|
||||
https://odfjs.github.io/odfjs/
|
||||
@ -158,3 +146,4 @@ npm run dev
|
||||
I hope to be credited for the work on this repo
|
||||
|
||||
Everything written by me and contributors to this repo is licenced under **CC0 1.0 (Public Domain)**
|
||||
|
||||
|
||||
@ -42,51 +42,6 @@ export function traverse(node, visit) {
|
||||
visit(node);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Node} node1
|
||||
* @param {Node} node2
|
||||
* @returns {Node}
|
||||
*/
|
||||
export function findCommonAncestor(node1, node2) {
|
||||
const ancestors1 = getAncestors(node1);
|
||||
const ancestors2 = new Set(getAncestors(node2));
|
||||
|
||||
for(const ancestor of ancestors1) {
|
||||
if(ancestors2.has(ancestor)) {
|
||||
return ancestor;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`node1 and node2 do not have a common ancestor`)
|
||||
}
|
||||
|
||||
/**
|
||||
* returns ancestors youngest first, oldest last
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {Node} [until]
|
||||
* @returns {Node[]}
|
||||
*/
|
||||
export function getAncestors(node, until = undefined) {
|
||||
const ancestors = [];
|
||||
let current = node;
|
||||
|
||||
while(current && current !== until) {
|
||||
ancestors.push(current);
|
||||
current = current.parentNode;
|
||||
}
|
||||
|
||||
if(current === until){
|
||||
ancestors.push(until);
|
||||
}
|
||||
|
||||
return ancestors;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export {
|
||||
DOMParser,
|
||||
XMLSerializer,
|
||||
|
||||
@ -6,15 +6,11 @@ import {serializeToString, createDocument} from './DOMUtils.js'
|
||||
/** @import {SheetCellRawContent, SheetName, SheetRawContent} from './types.js' */
|
||||
|
||||
const stylesXml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<office:document-styles
|
||||
<office:document-styles
|
||||
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
|
||||
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
|
||||
office:version="1.2">
|
||||
<office:styles>
|
||||
<style:style style:name="boldcell" style:family="table-cell">
|
||||
<style:text-properties fo:font-weight="bold"/>
|
||||
</style:style>
|
||||
</office:styles>
|
||||
<office:styles/>
|
||||
<office:automatic-styles/>
|
||||
<office:master-styles/>
|
||||
</office:document-styles>`;
|
||||
@ -31,11 +27,11 @@ const manifestXml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
||||
* @returns {Promise<ArrayBuffer>}
|
||||
*/
|
||||
export async function createOdsFile(sheetsData, currencyData = null) {
|
||||
export async function createOdsFile(sheetsData) {
|
||||
// Create a new zip writer
|
||||
const zipWriter = new ZipWriter(new BlobWriter('application/vnd.oasis.opendocument.spreadsheet'));
|
||||
|
||||
// The “mimetype” file shall be the first file of the zip file.
|
||||
// The “mimetype” file shall be the first file of the zip file.
|
||||
// It shall not be compressed, and it shall not use an 'extra field' in its header.
|
||||
// https://docs.oasis-open.org/office/OpenDocument/v1.3/os/part2-packages/OpenDocument-v1.3-os-part2-packages.html#__RefHeading__752809_826425813
|
||||
zipWriter.add(
|
||||
@ -49,7 +45,7 @@ export async function createOdsFile(sheetsData, currencyData = null) {
|
||||
}
|
||||
);
|
||||
|
||||
const contentXml = generateContentFileXMLString(sheetsData, currencyData);
|
||||
const contentXml = generateContentFileXMLString(sheetsData);
|
||||
zipWriter.add("content.xml", new TextReader(contentXml), {level: 9});
|
||||
|
||||
zipWriter.add("styles.xml", new TextReader(stylesXml));
|
||||
@ -64,10 +60,10 @@ export async function createOdsFile(sheetsData, currencyData = null) {
|
||||
|
||||
/**
|
||||
* Generate the content.xml file with spreadsheet data
|
||||
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
||||
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
||||
* @returns {string}
|
||||
*/
|
||||
function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
function generateContentFileXMLString(sheetsData) {
|
||||
const doc = createDocument('urn:oasis:names:tc:opendocument:xmlns:office:1.0', 'office:document-content');
|
||||
const root = doc.documentElement;
|
||||
|
||||
@ -79,52 +75,6 @@ function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
root.setAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
|
||||
root.setAttribute('office:version', '1.2');
|
||||
|
||||
const styleNode = doc.createElement("office:automatic-styles");
|
||||
|
||||
var currencyStyleName = "currencyStyle";
|
||||
if (currencyData != null) {
|
||||
currencyStyleName = `currency${currencyData.currencyCode.toUpperCase()}`;
|
||||
const numberStyle = doc.createElement("number:currency-style");
|
||||
numberStyle.setAttribute("style:name", currencyStyleName);
|
||||
|
||||
const numberCurrencySymbolStyle = doc.createElement("number:currency-symbol");
|
||||
numberCurrencySymbolStyle.setAttribute("number:language", "en");
|
||||
numberCurrencySymbolStyle.setAttribute("number:country", currencyData.countryCode.toUpperCase());
|
||||
numberCurrencySymbolStyle.textContent = currencyData.currencySymbol;
|
||||
numberStyle.appendChild(numberCurrencySymbolStyle);
|
||||
|
||||
const numberCurrencyStyle = doc.createElement("number:number");
|
||||
numberCurrencyStyle.setAttribute("number:min-integer-digits", "1");
|
||||
numberCurrencyStyle.setAttribute("number:decimal-places", `${currencyData.decimalPlaces}`);
|
||||
numberCurrencyStyle.setAttribute("number:min-decimal-places", `${currencyData.decimalPlaces}`);
|
||||
numberCurrencyStyle.setAttribute("number:grouping", "true");
|
||||
numberStyle.appendChild(numberCurrencyStyle);
|
||||
|
||||
styleNode.appendChild(numberStyle);
|
||||
|
||||
const currencyCellStyleNode = doc.createElement("style:style");
|
||||
currencyCellStyleNode.setAttribute("style:name", "currencycell");
|
||||
currencyCellStyleNode.setAttribute("style:family", "table-cell");
|
||||
currencyCellStyleNode.setAttribute("style:data-style-name", currencyStyleName);
|
||||
|
||||
const currencyCellTableCellProperties = doc.createElement("style:table-cell-properties");
|
||||
|
||||
currencyCellStyleNode.appendChild(currencyCellTableCellProperties);
|
||||
|
||||
styleNode.appendChild(currencyCellStyleNode);
|
||||
}
|
||||
|
||||
const boldCellStyleNode = doc.createElement("style:style");
|
||||
boldCellStyleNode.setAttribute("style:name", "boldcell");
|
||||
boldCellStyleNode.setAttribute("style:family", "table-cell");
|
||||
const boldCellTextPropsNode = doc.createElement("style:text-properties");
|
||||
boldCellTextPropsNode.setAttribute("fo:font-weight", "bold");
|
||||
boldCellStyleNode.appendChild(boldCellTextPropsNode);
|
||||
styleNode.appendChild(boldCellStyleNode);
|
||||
|
||||
|
||||
root.appendChild(styleNode);
|
||||
|
||||
const bodyNode = doc.createElement('office:body');
|
||||
root.appendChild(bodyNode);
|
||||
|
||||
@ -137,30 +87,8 @@ function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
tableNode.setAttribute('table:name', sheetName);
|
||||
spreadsheetNode.appendChild(tableNode);
|
||||
|
||||
var columnsWidthChars = {};
|
||||
for (let r = 0; r < sheetData.length; r++) {
|
||||
for (let c = 0; c < sheetData[r].length; c++) {
|
||||
var len = ((sheetData[r][c].display ?? sheetData[r][c].value) + "").length;
|
||||
if (typeof columnsWidthChars[c] == "undefined") {
|
||||
columnsWidthChars[c] = len;
|
||||
}
|
||||
columnsWidthChars[c] = Math.max(columnsWidthChars[c], len);
|
||||
}
|
||||
}
|
||||
|
||||
for (var prop in columnsWidthChars) {
|
||||
var columnNode = doc.createElement('table:table-column');
|
||||
columnNode.setAttribute("table:style-name", "colwidth" + columnsWidthChars[prop]);
|
||||
tableNode.appendChild(columnNode);
|
||||
|
||||
var columnWidthNode = doc.createElement("style:style");
|
||||
columnWidthNode.setAttribute("style:name", "colwidth" + columnsWidthChars[prop]);
|
||||
columnWidthNode.setAttribute("style:family", "table-column");
|
||||
const columnWidthPropsNode = doc.createElement("style:table-column-properties");
|
||||
columnWidthPropsNode.setAttribute("style:column-width", `${columnsWidthChars[prop] * 0.26}cm`);
|
||||
columnWidthNode.appendChild(columnWidthPropsNode);
|
||||
styleNode.appendChild(columnWidthNode);
|
||||
}
|
||||
const columnNode = doc.createElement('table:table-column');
|
||||
tableNode.appendChild(columnNode);
|
||||
|
||||
// Iterate through rows
|
||||
sheetData.forEach((row) => {
|
||||
@ -173,10 +101,6 @@ function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
const cellType = convertCellType(cell.type);
|
||||
cellNode.setAttribute('office:value-type', cellType);
|
||||
|
||||
if (cell.style && cell.style == "bold") {
|
||||
cellNode.setAttribute('table:style-name', "boldcell");
|
||||
}
|
||||
|
||||
// Add value attribute based on type
|
||||
if (cell.value !== null && cell.value !== undefined) {
|
||||
switch (cellType) {
|
||||
@ -187,14 +111,6 @@ function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
cellNode.setAttribute('office:value', cell.value.toString());
|
||||
cellNode.setAttribute('office:value-type', 'percentage');
|
||||
break;
|
||||
case 'currency':
|
||||
cellNode.setAttribute('office:value', cell.value.toString());
|
||||
cellNode.setAttribute('office:value-type', 'currency');
|
||||
if (currencyData != null) {
|
||||
cellNode.setAttribute("table:style-name", "currencycell");
|
||||
cellNode.setAttribute('office:currency', currencyData.currencyCode.toUpperCase());
|
||||
}
|
||||
break;
|
||||
case 'date':
|
||||
cellNode.setAttribute('office:date-value', cell.value.toString());
|
||||
break;
|
||||
@ -210,11 +126,7 @@ function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
|
||||
if (cellType !== 'string') {
|
||||
const textNode = doc.createElement('text:p');
|
||||
if (typeof cell.display != "undefined") {
|
||||
textNode.textContent = cell.display.toString();
|
||||
} else {
|
||||
textNode.textContent = cell.value.toString();
|
||||
}
|
||||
textNode.textContent = cell.value.toString();
|
||||
cellNode.appendChild(textNode);
|
||||
}
|
||||
}
|
||||
@ -229,7 +141,7 @@ function generateContentFileXMLString(sheetsData, currencyData) {
|
||||
|
||||
/**
|
||||
* Convert cell type to OpenDocument format type
|
||||
* @param {SheetCellRawContent['type']} type
|
||||
* @param {SheetCellRawContent['type']} type
|
||||
* @returns {SheetCellRawContent['type']}
|
||||
*/
|
||||
function convertCellType(type) {
|
||||
|
||||
@ -7,7 +7,7 @@ import {parseXML, Node} from '../../DOMUtils.js'
|
||||
* @param {ODTFile} odtFile
|
||||
* @returns {Promise<Document>}
|
||||
*/
|
||||
export async function getContentDocument(odtFile) {
|
||||
async function getContentDocument(odtFile) {
|
||||
const reader = new ZipReader(new Uint8ArrayReader(new Uint8Array(odtFile)));
|
||||
|
||||
const entries = await reader.getEntries();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -7,10 +7,11 @@ import prepareTemplateDOMTree from './prepareTemplateDOMTree.js';
|
||||
import 'ses'
|
||||
import fillOdtElementTemplate from './fillOdtElementTemplate.js';
|
||||
|
||||
lockdown();
|
||||
|
||||
|
||||
/** @import {Reader, ZipWriterAddDataOptions} from '@zip.js/zip.js' */
|
||||
/** @import {ODFManifest, ODFManifestFileEntry} from '../manifest.js' */
|
||||
/** @import {OdfjsImage} from '../../types.js' */
|
||||
/** @import {ODFManifest} from '../manifest.js' */
|
||||
|
||||
/** @typedef {ArrayBuffer} ODTFile */
|
||||
|
||||
@ -22,12 +23,11 @@ const ODTMimetype = 'application/vnd.oasis.opendocument.text'
|
||||
*
|
||||
* @param {Document} document
|
||||
* @param {Compartment} compartment
|
||||
* @param {(OdfjsImage) => string} addImageToOdtFile
|
||||
* @returns {void}
|
||||
*/
|
||||
function fillOdtDocumentTemplate(document, compartment, addImageToOdtFile) {
|
||||
function fillOdtDocumentTemplate(document, compartment) {
|
||||
prepareTemplateDOMTree(document)
|
||||
fillOdtElementTemplate(document, compartment, addImageToOdtFile)
|
||||
fillOdtElementTemplate(document, compartment)
|
||||
}
|
||||
|
||||
|
||||
@ -64,21 +64,6 @@ export default async function fillOdtTemplate(odtTemplate, data) {
|
||||
|
||||
/** @type {{filename: string, content: Reader, options?: ZipWriterAddDataOptions}[]} */
|
||||
const zipEntriesToAdd = []
|
||||
/** @type {ODFManifestFileEntry[]} */
|
||||
const newManifestEntries = []
|
||||
|
||||
/**
|
||||
* Return href
|
||||
* @param {OdfjsImage} odfjsImage
|
||||
* @returns {string}
|
||||
*/
|
||||
function addImageToOdtFile(odfjsImage) {
|
||||
// console.log({odfjsImage})
|
||||
const filename = `Pictures/${odfjsImage.fileName}`
|
||||
zipEntriesToAdd.push({content: new Uint8ArrayReader(new Uint8Array(odfjsImage.content)), filename})
|
||||
newManifestEntries.push({fullPath: filename, mediaType: odfjsImage.mediaType})
|
||||
return filename
|
||||
}
|
||||
|
||||
// Parcourir chaque entrée du fichier ODT
|
||||
for await(const entry of entries) {
|
||||
@ -111,15 +96,13 @@ export default async function fillOdtTemplate(odtTemplate, data) {
|
||||
// @ts-ignore
|
||||
const contentXml = await entry.getData(new TextWriter());
|
||||
const contentDocument = parseXML(contentXml);
|
||||
|
||||
|
||||
|
||||
const compartment = new Compartment({
|
||||
globals: data,
|
||||
__options__: true
|
||||
})
|
||||
|
||||
fillOdtDocumentTemplate(contentDocument, compartment, addImageToOdtFile)
|
||||
fillOdtDocumentTemplate(contentDocument, compartment)
|
||||
|
||||
const updatedContentXml = serializeToString(contentDocument)
|
||||
|
||||
@ -155,9 +138,6 @@ export default async function fillOdtTemplate(odtTemplate, data) {
|
||||
}
|
||||
}
|
||||
|
||||
for(const {fullPath, mediaType} of newManifestEntries){
|
||||
manifestFileData.fileEntries.set(fullPath, {fullPath, mediaType})
|
||||
}
|
||||
|
||||
for(const {filename, content, options} of zipEntriesToAdd) {
|
||||
await writer.add(filename, content, options);
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
// the regexps below are shared, so they shoudn't have state (no 'g' flag)
|
||||
export const variableRegex = /\{([^{#\/:]+?)\}/
|
||||
export const imageMarkerRegex = /{#image\s+([^}]+?)\s*}/;
|
||||
export const variableRegex = /\{([^{#\/]+?)\}/
|
||||
|
||||
export const ifStartMarkerRegex = /{#if\s+([^}]+?)\s*}/;
|
||||
export const elseMarker = '{:else}'
|
||||
export const closingIfMarker = '{/if}'
|
||||
|
||||
export const eachStartMarkerRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/;
|
||||
export const eachClosingMarker = '{/each}'
|
||||
export const eachClosingMarker = '{/each}'
|
||||
@ -1,9 +1,6 @@
|
||||
//@ts-check
|
||||
|
||||
import {traverse, Node, getAncestors, findCommonAncestor} from "../../DOMUtils.js";
|
||||
import {traverse, Node} from "../../DOMUtils.js";
|
||||
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js'
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} text
|
||||
@ -38,7 +35,41 @@ function findAllMatches(text, pattern) {
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Node} node1
|
||||
* @param {Node} node2
|
||||
* @returns {Node | undefined}
|
||||
*/
|
||||
function findCommonAncestor(node1, node2) {
|
||||
const ancestors1 = getAncestors(node1);
|
||||
const ancestors2 = getAncestors(node2);
|
||||
|
||||
for(const ancestor of ancestors1) {
|
||||
if(ancestors2.includes(ancestor)) {
|
||||
return ancestor;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Node} node
|
||||
* @returns {Node[]}
|
||||
*/
|
||||
function getAncestors(node) {
|
||||
const ancestors = [];
|
||||
let current = node;
|
||||
|
||||
while(current) {
|
||||
ancestors.push(current);
|
||||
current = current.parentNode;
|
||||
}
|
||||
|
||||
return ancestors;
|
||||
}
|
||||
|
||||
/**
|
||||
* text position of a node relative to a text nodes within a container
|
||||
@ -67,14 +98,19 @@ function getNodeTextPosition(node, containerTextNodes) {
|
||||
|
||||
/**
|
||||
* remove nodes between startNode and endNode
|
||||
* including startNode and endNode
|
||||
* but keep startNode and endNode
|
||||
*
|
||||
* returns the common ancestor child in start branch
|
||||
* for the purpose for inserting something between startNode and endNode
|
||||
* with insertionPoint.parentNode.insertBefore(newBetweenContent, insertionPoint)
|
||||
*
|
||||
* @param {Node} startNode
|
||||
* @param {Node} endNode
|
||||
* @param {string} text
|
||||
* @returns {void}
|
||||
* @returns {Node}
|
||||
*/
|
||||
function replaceBetweenNodesWithText(startNode, endNode, text) {
|
||||
function removeNodesBetween(startNode, endNode) {
|
||||
let nodesToRemove = new Set();
|
||||
|
||||
// find both ancestry branch
|
||||
const startNodeAncestors = new Set(getAncestors(startNode))
|
||||
const endNodeAncestors = new Set(getAncestors(endNode))
|
||||
@ -82,42 +118,43 @@ function replaceBetweenNodesWithText(startNode, endNode, text) {
|
||||
// find common ancestor
|
||||
const commonAncestor = findCommonAncestor(startNode, endNode)
|
||||
|
||||
let remove = false
|
||||
let toRemove = []
|
||||
let commonAncestorChild = commonAncestor.firstChild
|
||||
let commonAncestorInsertionChild
|
||||
// remove everything "on the right" of start branch
|
||||
let currentAncestor = startNode
|
||||
let commonAncestorChildInEndNodeBranch
|
||||
|
||||
while(commonAncestorChild){
|
||||
if(startNodeAncestors.has(commonAncestorChild)){
|
||||
remove = true
|
||||
while(currentAncestor !== commonAncestor){
|
||||
let siblingToRemove = currentAncestor.nextSibling
|
||||
|
||||
while(siblingToRemove && !endNodeAncestors.has(siblingToRemove)){
|
||||
nodesToRemove.add(siblingToRemove)
|
||||
siblingToRemove = siblingToRemove.nextSibling
|
||||
}
|
||||
if(endNodeAncestors.has(siblingToRemove)){
|
||||
commonAncestorChildInEndNodeBranch = siblingToRemove
|
||||
}
|
||||
|
||||
if(remove){
|
||||
toRemove.push(commonAncestorChild)
|
||||
currentAncestor = currentAncestor.parentNode;
|
||||
}
|
||||
|
||||
if(endNodeAncestors.has(commonAncestorChild)){
|
||||
commonAncestorInsertionChild = commonAncestorChild.nextSibling
|
||||
break;
|
||||
}
|
||||
// remove everything "on the left" of end branch
|
||||
currentAncestor = endNode
|
||||
|
||||
while(currentAncestor !== commonAncestor){
|
||||
let siblingToRemove = currentAncestor.previousSibling
|
||||
|
||||
while(siblingToRemove && !startNodeAncestors.has(siblingToRemove)){
|
||||
nodesToRemove.add(siblingToRemove)
|
||||
siblingToRemove = siblingToRemove.previousSibling
|
||||
}
|
||||
commonAncestorChild = commonAncestorChild.nextSibling
|
||||
|
||||
currentAncestor = currentAncestor.parentNode;
|
||||
}
|
||||
|
||||
for(const node of toRemove){
|
||||
commonAncestor.removeChild(node)
|
||||
}
|
||||
|
||||
//console.log('replaceBetweenNodesWithText startNode', startNode.textContent)
|
||||
|
||||
const newTextNode = commonAncestor.ownerDocument.createTextNode(text)
|
||||
|
||||
if(commonAncestorInsertionChild){
|
||||
commonAncestor.insertBefore(newTextNode, commonAncestorInsertionChild)
|
||||
}
|
||||
else{
|
||||
commonAncestor.appendChild(newTextNode)
|
||||
for(const node of nodesToRemove){
|
||||
node.parentNode.removeChild(node)
|
||||
}
|
||||
|
||||
return commonAncestorChildInEndNodeBranch
|
||||
}
|
||||
|
||||
/**
|
||||
@ -133,7 +170,6 @@ function consolidateMarkers(document){
|
||||
]
|
||||
|
||||
for(const potentialMarkersContainer of potentialMarkersContainers) {
|
||||
/** @type {{marker: string, index: number}[]} */
|
||||
const consolidatedMarkers = []
|
||||
|
||||
/** @type {Text[]} */
|
||||
@ -166,10 +202,8 @@ function consolidateMarkers(document){
|
||||
...findAllMatches(fullText, variableRegex)
|
||||
];
|
||||
|
||||
|
||||
//if(positionedMarkers.length >= 1)
|
||||
// console.log('positionedMarkers', positionedMarkers)
|
||||
|
||||
/*if(positionedMarkers.length >= 1)
|
||||
console.log('positionedMarkers', positionedMarkers)*/
|
||||
|
||||
while(consolidatedMarkers.length < positionedMarkers.length) {
|
||||
refreshContainerTextNodes()
|
||||
@ -212,21 +246,6 @@ function consolidateMarkers(document){
|
||||
|
||||
// Check if marker spans multiple nodes
|
||||
if(startNode !== endNode) {
|
||||
//console.log('startNode !== endNode', startNode.textContent, endNode.textContent)
|
||||
const commonAncestor = findCommonAncestor(startNode, endNode)
|
||||
|
||||
/** @type {Node} */
|
||||
let commonAncestorStartChild = startNode
|
||||
while(commonAncestorStartChild.parentNode !== commonAncestor){
|
||||
commonAncestorStartChild = commonAncestorStartChild.parentNode
|
||||
}
|
||||
|
||||
/** @type {Node} */
|
||||
let commonAncestorEndChild = endNode
|
||||
while(commonAncestorEndChild.parentNode !== commonAncestor){
|
||||
commonAncestorEndChild = commonAncestorEndChild.parentNode
|
||||
}
|
||||
|
||||
// Calculate relative positions within the nodes
|
||||
let startNodeTextContent = startNode.textContent || '';
|
||||
let endNodeTextContent = endNode.textContent || '';
|
||||
@ -237,51 +256,47 @@ function consolidateMarkers(document){
|
||||
// Calculate the position within the end node
|
||||
let posInEndNode = (positionedMarker.index + positionedMarker.marker.length) - getNodeTextPosition(endNode, containerTextNodesInTreeOrder);
|
||||
|
||||
let newStartNode = startNode
|
||||
/** @type {Node} */
|
||||
let beforeStartNode = startNode
|
||||
|
||||
// if there is before-text, split
|
||||
if(posInStartNode > 0) {
|
||||
// Text exists before the marker - preserve it
|
||||
|
||||
// set newStartNode to a Text node containing only the marker beginning
|
||||
newStartNode = startNode.splitText(posInStartNode)
|
||||
const newStartNode = startNode.splitText(posInStartNode)
|
||||
// startNode/beforeStartNode now contains only non-marker text
|
||||
|
||||
// then, by definition of .splitText(posInStartNode):
|
||||
posInStartNode = 0
|
||||
|
||||
// move the marker beginning part to become a child of commonAncestor
|
||||
// remove the marker beginning part from the tree (since the marker will be inserted in full later)
|
||||
newStartNode.parentNode?.removeChild(newStartNode)
|
||||
|
||||
commonAncestor.insertBefore(newStartNode, commonAncestorStartChild.nextSibling)
|
||||
|
||||
//console.log('commonAncestor after before-text split', commonAncestor.textContent )
|
||||
}
|
||||
|
||||
/** @type {Node} */
|
||||
let afterEndNode
|
||||
|
||||
// if there is after-text, split
|
||||
if(posInEndNode < endNodeTextContent.length) {
|
||||
// Text exists after the marker - preserve it
|
||||
|
||||
endNode.splitText(posInEndNode);
|
||||
// set afterEndNode to a Text node containing only non-marker text
|
||||
afterEndNode = endNode.splitText(posInEndNode);
|
||||
// endNode now contains only the end of marker text
|
||||
|
||||
// then, by definition of .splitText(posInEndNode):
|
||||
posInEndNode = endNodeTextContent.length
|
||||
|
||||
// move the marker ending part to become a child of commonAncestor
|
||||
if(endNode !== commonAncestorEndChild){
|
||||
endNode.parentNode?.removeChild(endNode)
|
||||
commonAncestor.insertBefore(endNode, commonAncestorEndChild)
|
||||
}
|
||||
|
||||
//console.log('commonAncestor after after-text split', commonAncestor.textContent )
|
||||
// remove the marker ending part from the tree (since the marker will be inserted in full later)
|
||||
endNode.parentNode?.removeChild(endNode)
|
||||
}
|
||||
|
||||
// then, replace all nodes between (new)startNode and (new)endNode with a single textNode in commonAncestor
|
||||
replaceBetweenNodesWithText(newStartNode, endNode, positionedMarker.marker)
|
||||
const insertionPoint = removeNodesBetween(beforeStartNode, afterEndNode)
|
||||
const markerTextNode = insertionPoint.ownerDocument.createTextNode(positionedMarker.marker)
|
||||
|
||||
//console.log('commonAncestor after replaceBetweenNodesWithText', commonAncestor.textContent )
|
||||
insertionPoint.parentNode.insertBefore(markerTextNode, insertionPoint)
|
||||
|
||||
// After consolidation, break as the DOM structure has changed
|
||||
// and containerTextNodesInTreeOrder needs to be refreshed
|
||||
@ -292,35 +307,16 @@ function consolidateMarkers(document){
|
||||
consolidatedMarkers.push(positionedMarker)
|
||||
}
|
||||
}
|
||||
|
||||
//console.log('consolidatedMarkers', consolidatedMarkers)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {typeof closingIfMarker | typeof eachClosingMarker | typeof eachStartMarkerRegex.source | typeof elseMarker | typeof ifStartMarkerRegex.source | typeof variableRegex.source} MarkerType
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} MarkerNode
|
||||
* @prop {Node} node
|
||||
* @prop {MarkerType} markerType
|
||||
*/
|
||||
|
||||
/**
|
||||
* isolate markers which are in Text nodes with other texts
|
||||
*
|
||||
* @param {Document} document
|
||||
* @returns {Map<Node, MarkerType>}
|
||||
*/
|
||||
function isolateMarkerText(document){
|
||||
/** @type {ReturnType<isolateMarkerText>} */
|
||||
const markerNodes = new Map()
|
||||
|
||||
function isolateMarkers(document){
|
||||
traverse(document, currentNode => {
|
||||
//console.log('isolateMarkers', currentNode.nodeName, currentNode.textContent)
|
||||
|
||||
if(currentNode.nodeType === Node.TEXT_NODE) {
|
||||
// find all marker starts and ends and split textNode
|
||||
let remainingText = currentNode.textContent || ''
|
||||
@ -328,8 +324,6 @@ function isolateMarkerText(document){
|
||||
while(remainingText.length >= 1) {
|
||||
let matchText;
|
||||
let matchIndex;
|
||||
/** @type {MarkerType} */
|
||||
let markerType;
|
||||
|
||||
// looking for a block marker
|
||||
for(const marker of [ifStartMarkerRegex, elseMarker, closingIfMarker, eachStartMarkerRegex, eachClosingMarker]) {
|
||||
@ -339,7 +333,6 @@ function isolateMarkerText(document){
|
||||
if(index !== -1) {
|
||||
matchText = marker
|
||||
matchIndex = index
|
||||
markerType = marker
|
||||
|
||||
// found the first match
|
||||
break; // get out of loop
|
||||
@ -352,7 +345,6 @@ function isolateMarkerText(document){
|
||||
if(match) {
|
||||
matchText = match[0]
|
||||
matchIndex = match.index
|
||||
markerType = marker.source
|
||||
|
||||
// found the first match
|
||||
break; // get out of loop
|
||||
@ -375,21 +367,11 @@ function isolateMarkerText(document){
|
||||
|
||||
// per spec, currentNode now contains before-match and match text
|
||||
|
||||
/** @type {Node} */
|
||||
let matchTextNode
|
||||
|
||||
// @ts-ignore
|
||||
if(matchIndex > 0) {
|
||||
// @ts-ignore
|
||||
matchTextNode = currentNode.splitText(matchIndex)
|
||||
currentNode.splitText(matchIndex)
|
||||
}
|
||||
else{
|
||||
matchTextNode = currentNode
|
||||
}
|
||||
|
||||
markerNodes.set(matchTextNode, markerType)
|
||||
|
||||
// per spec, currentNode now contains only before-match text
|
||||
|
||||
if(afterMatchTextNode) {
|
||||
currentNode = afterMatchTextNode
|
||||
@ -409,102 +391,8 @@ function isolateMarkerText(document){
|
||||
// skip
|
||||
}
|
||||
})
|
||||
|
||||
//console.log('markerNodes', [...markerNodes].map(([node, markerType]) => [node.textContent, markerType]))
|
||||
|
||||
return markerNodes
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* after isolateMatchingMarkersStructure, matching markers (opening/closing each, if/then/closing if)
|
||||
* are put in isolated branches within their common ancestors
|
||||
*
|
||||
* UNFINISHED - maybe another day if relevant
|
||||
*
|
||||
* @param {Document} document
|
||||
* @param {Map<Node, MarkerType>} markerNodes
|
||||
*/
|
||||
//function isolateMatchingMarkersStructure(document, markerNodes){
|
||||
/** @type {MarkerNode[]} */
|
||||
/* let currentlyOpenBlocks = []
|
||||
|
||||
traverse(document, currentNode => {
|
||||
|
||||
const markerType = markerNodes.get(currentNode)
|
||||
|
||||
if(markerType){
|
||||
switch(markerType){
|
||||
case eachStartMarkerRegex.source:
|
||||
case ifStartMarkerRegex.source: {
|
||||
currentlyOpenBlocks.push({
|
||||
node: currentNode,
|
||||
markerType
|
||||
})
|
||||
break;
|
||||
}
|
||||
case eachClosingMarker: {
|
||||
const lastOpenedBlockMarkerNode = currentlyOpenBlocks.pop()
|
||||
|
||||
if(!lastOpenedBlockMarkerNode)
|
||||
throw new Error(`{/each} found without corresponding opening {#each x as y}`)
|
||||
|
||||
if(lastOpenedBlockMarkerNode.markerType !== eachStartMarkerRegex.source)
|
||||
throw new Error(`{/each} found while the last opened block was not an opening {#each x as y} (it was a ${lastOpenedBlockMarkerNode.markerType})`)
|
||||
|
||||
const openingEachNode = lastOpenedBlockMarkerNode.node
|
||||
const closingEachNode = currentNode
|
||||
|
||||
const commonAncestor = findCommonAncestor(openingEachNode, closingEachNode)
|
||||
|
||||
if(openingEachNode.parentNode !== commonAncestor && openingEachNode.parentNode.childNodes.length >= 2){
|
||||
if(openingEachNode.previousSibling){
|
||||
// create branch for previousSiblings
|
||||
let previousSibling = openingEachNode.previousSibling
|
||||
const previousSiblings = []
|
||||
while(previousSibling){
|
||||
previousSiblings.push(previousSibling.previousSibling)
|
||||
previousSibling = previousSibling.previousSibling
|
||||
}
|
||||
|
||||
// put previous siblings in tree order
|
||||
previousSiblings.reverse()
|
||||
|
||||
const parent = openingEachNode.parentNode
|
||||
const parentClone = parent.cloneNode(false)
|
||||
for(const previousSibling of previousSiblings){
|
||||
previousSibling.parentNode.removeChild(previousSibling)
|
||||
parentClone.appendChild(previousSibling)
|
||||
}
|
||||
|
||||
let openingEachNodeBranch = openingEachNode.parentNode
|
||||
let branchForPreviousSiblings = parentClone
|
||||
|
||||
while(openingEachNodeBranch.parentNode !== commonAncestor){
|
||||
const newParentClone = openingEachNodeBranch.parentNode.cloneNode(false)
|
||||
branchForPreviousSiblings.parentNode.removeChild(branchForPreviousSiblings)
|
||||
newParentClone.appendChild(branchForPreviousSiblings)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new TypeError(`MarkerType not recognized: '${markerType}`)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}*/
|
||||
|
||||
|
||||
/**
|
||||
* This function prepares the template DOM tree in a way that makes it easily processed by the template execution
|
||||
* Specifically, after the call to this function, the document is altered to respect the following property:
|
||||
@ -521,10 +409,5 @@ function isolateMarkerText(document){
|
||||
*/
|
||||
export default function prepareTemplateDOMTree(document){
|
||||
consolidateMarkers(document)
|
||||
// after consolidateMarkers, each marker is in at most one text node
|
||||
// (formatting with markers is removed)
|
||||
|
||||
isolateMarkerText(document)
|
||||
// after isolateMarkerText, each marker is in exactly one text node
|
||||
// (markers are separated from text that was before or after in the same text node)
|
||||
isolateMarkers(document)
|
||||
}
|
||||
@ -4,20 +4,21 @@ import { Uint8ArrayReader, ZipReader, TextWriter } from '@zip.js/zip.js';
|
||||
import {parseXML} from './DOMUtils.js'
|
||||
|
||||
/** @import {Entry} from '@zip.js/zip.js'*/
|
||||
/** @import {SheetName, SheetRawContent, SheetRowRawContent, SheetCellRawContent, OdfjsImage} from './types.js' */
|
||||
/** @import {SheetName, SheetRawContent, SheetRowRawContent, SheetCellRawContent} from './types.js' */
|
||||
|
||||
|
||||
// https://dom.spec.whatwg.org/#interface-node
|
||||
const TEXT_NODE = 3
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Element} cell
|
||||
*
|
||||
* @param {Element} cell
|
||||
* @returns {string}
|
||||
*/
|
||||
function extraxtODSCellText(cell) {
|
||||
let text = '';
|
||||
const childNodes = cell.childNodes;
|
||||
|
||||
for (const child of Array.from(childNodes)) {
|
||||
if (child.nodeType === TEXT_NODE) {
|
||||
// Direct text node, append the text directly
|
||||
@ -33,15 +34,13 @@ function extraxtODSCellText(cell) {
|
||||
text += pChild.nodeValue; // Append text inside <text:p>
|
||||
} else if (pChild.nodeName === 'text:line-break') {
|
||||
text += '\n'; // Append newline for <text:line-break />
|
||||
} else if (pChild.nodeName === 'text:a' || pChild.nodeName === 'text:span') {
|
||||
text += pChild.textContent
|
||||
}
|
||||
}
|
||||
} else if (child.nodeName === 'text:line-break') {
|
||||
text += '\n'; // Append newline for <text:line-break /> directly under <table:table-cell>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return text.trim();
|
||||
}
|
||||
|
||||
@ -127,7 +126,7 @@ export async function getODSTableRawContent(arrayBuffer) {
|
||||
|
||||
/**
|
||||
* Converts a cell value to the appropriate JavaScript type based on its cell type.
|
||||
* @param {SheetCellRawContent} _
|
||||
* @param {SheetCellRawContent} _
|
||||
* @returns {number | boolean | string | Date} The converted value.
|
||||
*/
|
||||
export function convertCellValue({value, type}) {
|
||||
@ -161,37 +160,21 @@ export function convertCellValue({value, type}) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {unknown} value
|
||||
* @returns {value is OdfjsImage}
|
||||
*/
|
||||
export function isOdfjsImage(value) {
|
||||
if (typeof value === 'object' && value!==null
|
||||
&& "content" in value && value.content instanceof ArrayBuffer
|
||||
&& "fileName" in value && typeof value.fileName === 'string'
|
||||
&& "mediaType" in value && typeof value.mediaType === 'string'
|
||||
) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentSheets
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentSheets
|
||||
* @returns {Map<SheetName, ReturnType<convertCellValue>[][]>}
|
||||
*/
|
||||
export function tableRawContentToValues(rawContentSheets){
|
||||
return new Map(
|
||||
[...rawContentSheets].map(([sheetName, rawContent]) => {
|
||||
return [
|
||||
sheetName,
|
||||
sheetName,
|
||||
rawContent
|
||||
.map(row => row.map(c => convertCellValue(c)))
|
||||
]
|
||||
@ -204,7 +187,7 @@ export function tableRawContentToValues(rawContentSheets){
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param {SheetCellRawContent} rawContentCell
|
||||
* @returns {string}
|
||||
*/
|
||||
@ -213,8 +196,8 @@ export function cellRawContentToStrings(rawContentCell){
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {SheetRowRawContent} rawContentRow
|
||||
*
|
||||
* @param {SheetRowRawContent} rawContentRow
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function rowRawContentToStrings(rawContentRow){
|
||||
@ -222,8 +205,8 @@ export function rowRawContentToStrings(rawContentRow){
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {SheetRawContent} rawContentSheet
|
||||
*
|
||||
* @param {SheetRawContent} rawContentSheet
|
||||
* @returns {string[][]}
|
||||
*/
|
||||
export function sheetRawContentToStrings(rawContentSheet){
|
||||
@ -231,8 +214,8 @@ export function sheetRawContentToStrings(rawContentSheet){
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentSheets
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentSheets
|
||||
* @returns {Map<SheetName, string[][]>}
|
||||
*/
|
||||
export function tableRawContentToStrings(rawContentSheets){
|
||||
@ -253,16 +236,16 @@ export function tableRawContentToStrings(rawContentSheets){
|
||||
|
||||
/**
|
||||
* This function expects the first row to contain string values which are used as column names
|
||||
* It outputs an array of objects which keys are
|
||||
* It outputs an array of objects which keys are
|
||||
*
|
||||
* @param {SheetRawContent} rawContent
|
||||
* @param {SheetRawContent} rawContent
|
||||
* @returns {any[]}
|
||||
*/
|
||||
export function sheetRawContentToObjects(rawContent){
|
||||
let [firstRow, ...dataRows] = rawContent
|
||||
|
||||
/** @type {string[]} */
|
||||
|
||||
|
||||
const columns = firstRow.map((r, i) => {
|
||||
if (r.value === undefined || r.value === null || r.value === "") {
|
||||
return `Column ${i+1}`
|
||||
@ -284,8 +267,8 @@ export function sheetRawContentToObjects(rawContent){
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentSheets
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentSheets
|
||||
* @returns {Map<SheetName, any[]>}
|
||||
*/
|
||||
export function tableRawContentToObjects(rawContentSheets){
|
||||
@ -312,7 +295,7 @@ export function isCellFilled({value}){
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {SheetRowRawContent} rawContentRow
|
||||
* @param {SheetRowRawContent} rawContentRow
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isRowNotEmpty(rawContentRow){
|
||||
@ -320,7 +303,7 @@ export function isRowNotEmpty(rawContentRow){
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {SheetRawContent} sheet
|
||||
* @param {SheetRawContent} sheet
|
||||
* @returns {SheetRawContent}
|
||||
*/
|
||||
export function removeEmptyRowsFromSheet(sheet){
|
||||
@ -329,8 +312,8 @@ export function removeEmptyRowsFromSheet(sheet){
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentTable
|
||||
*
|
||||
* @param {Map<SheetName, SheetRawContent>} rawContentTable
|
||||
* @returns {Map<SheetName, SheetRawContent>}
|
||||
*/
|
||||
export function tableWithoutEmptyRows(rawContentTable){
|
||||
@ -339,4 +322,4 @@ export function tableWithoutEmptyRows(rawContentTable){
|
||||
return [sheetName, removeEmptyRowsFromSheet(rawContent)]
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -10,12 +10,4 @@
|
||||
|
||||
/** @typedef {string} SheetName */
|
||||
|
||||
/**
|
||||
* @typedef OdfjsImage
|
||||
* @prop {ArrayBuffer} content
|
||||
* @prop {string} fileName
|
||||
* @prop {string} mediaType
|
||||
*
|
||||
*/
|
||||
|
||||
export {}
|
||||
@ -1,25 +0,0 @@
|
||||
import test from 'ava';
|
||||
import {join} from 'node:path';
|
||||
|
||||
import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
|
||||
|
||||
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
|
||||
|
||||
|
||||
test('template with {#each} inside an {#if}', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/if-then-each.odt')
|
||||
const templateContent = `{#if liste_départements.length >= 2}{#each liste_départements as département}{département}, {/each} {/if}`
|
||||
|
||||
const data = {liste_départements : ['95', '33']}
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent.trim(), templateContent.trim(), 'reconnaissance du template')
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent.trim(), `95, 33,`)
|
||||
|
||||
});
|
||||
|
||||
@ -28,7 +28,7 @@ test('basic template filling with {#each}', async t => {
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
|
||||
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
|
||||
try{
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
@ -38,7 +38,6 @@ Radis
|
||||
Jus d'orange
|
||||
Pâtes à lasagne (fraîches !)
|
||||
`)
|
||||
}catch(e){console.error(e); throw e}
|
||||
|
||||
|
||||
});
|
||||
@ -248,43 +247,6 @@ Hiver
|
||||
});
|
||||
|
||||
|
||||
test('template filling with text after {/each} in same text node', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/text-after-closing-each.odt')
|
||||
const templateContent = `Légumes de saison
|
||||
|
||||
{#each légumes as légume}
|
||||
{légume},
|
||||
{/each} en {saison}
|
||||
`
|
||||
|
||||
const data = {
|
||||
saison: 'Printemps',
|
||||
légumes: [
|
||||
'Asperge',
|
||||
'Betterave',
|
||||
'Blette'
|
||||
]
|
||||
}
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent, `Légumes de saison
|
||||
|
||||
Asperge,
|
||||
Betterave,
|
||||
Blette,
|
||||
en Printemps
|
||||
`)
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('template filling of a table', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/tableau-simple.odt')
|
||||
const templateContent = `Évolution énergie en kWh par personne en France
|
||||
@ -344,32 +306,3 @@ Année
|
||||
`.trim())
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('nested each without common ancestor for inner each', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/nested-each-without-common-ancestor-for-inner-each.odt')
|
||||
const templateContent = `{#each liste_espèces_par_impact as élément}
|
||||
{#each élément.liste_espèces as espèce}
|
||||
{/each}
|
||||
{/each}
|
||||
`
|
||||
|
||||
const data = {
|
||||
liste_espèces_par_impact: [
|
||||
{}
|
||||
]
|
||||
}
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
|
||||
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent, ``)
|
||||
|
||||
|
||||
});
|
||||
@ -5,7 +5,6 @@ import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
|
||||
|
||||
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
|
||||
|
||||
|
||||
test('template filling with several layers of formatting in {#each ...} start marker', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-plusieurs-couches.odt')
|
||||
const templateContent = `Liste de nombres
|
||||
@ -87,6 +86,7 @@ Les nombres : 3 5 8 13 !!
|
||||
});
|
||||
|
||||
|
||||
|
||||
test('template filling - {/each} and text after partially formatted', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-each-end-and-after-formatted.odt')
|
||||
const templateContent = `Liste de nombres
|
||||
@ -114,6 +114,8 @@ Les nombres : 5 8 13 21 !!
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
test('template filling - partially formatted variable', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/partially-formatted-variable.odt')
|
||||
const templateContent = `Nombre
|
||||
@ -127,85 +129,13 @@ Voici le nombre : {nombre} !!!
|
||||
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
|
||||
//try{
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
//}catch(e){console.error(e)}
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent, `Nombre
|
||||
|
||||
Voici le nombre : 37 !!!
|
||||
`)
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('template filling - formatted-start-each-single-paragraph', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/formatted-start-each-single-paragraph.odt')
|
||||
const templateContent = `
|
||||
{#each nombres as n}
|
||||
{n}
|
||||
{/each}
|
||||
`
|
||||
|
||||
const data = {nombres : [37, 38, 39]}
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent.trim(), templateContent.trim(), 'reconnaissance du template')
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent, `
|
||||
|
||||
37
|
||||
38
|
||||
39
|
||||
`)
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('template filling - formatted ghost if then', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/ghost-if.odt')
|
||||
const templateContent = `
|
||||
Utilisation de sources lumineuses : {#if scientifique.source_lumineuses}Oui{:else}Non{/if}
|
||||
`
|
||||
|
||||
const data = {scientifique: {source_lumineuses: true}}
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent.trim(), templateContent.trim(), 'reconnaissance du template')
|
||||
let odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent.trim(), `
|
||||
Utilisation de sources lumineuses : Oui
|
||||
`.trim())
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('template filling - formatted ghost if else', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/ghost-if.odt')
|
||||
const templateContent = `
|
||||
Utilisation de sources lumineuses : {#if scientifique.source_lumineuses}Oui{:else}Non{/if}
|
||||
`
|
||||
|
||||
const data = {scientifique: {source_lumineuses: false}}
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent.trim(), templateContent.trim(), 'reconnaissance du template')
|
||||
let odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent.trim(), `
|
||||
Utilisation de sources lumineuses : Non
|
||||
`.trim())
|
||||
|
||||
});
|
||||
@ -6,7 +6,7 @@ import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
|
||||
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
|
||||
|
||||
|
||||
test('basic template filling with {#if}{:else} - then branch', async t => {
|
||||
test('basic template filling with {#if}', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/description-nombre.odt')
|
||||
const templateContent = `Description du nombre {n}
|
||||
|
||||
@ -28,27 +28,7 @@ n est un grand nombre
|
||||
|
||||
n est un petit nombre
|
||||
`)
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('basic template filling with {#if}{:else} - else branch', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/description-nombre.odt')
|
||||
const templateContent = `Description du nombre {n}
|
||||
|
||||
{#if n<5}
|
||||
n est un petit nombre
|
||||
{:else}
|
||||
n est un grand nombre
|
||||
{/if}
|
||||
`
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
|
||||
|
||||
try{
|
||||
// else branch
|
||||
const odtResult8 = await fillOdtTemplate(odtTemplate, {n: 8})
|
||||
const odtResult8TextContent = await getOdtTextContent(odtResult8)
|
||||
@ -56,36 +36,7 @@ n est un grand nombre
|
||||
|
||||
n est un grand nombre
|
||||
`)
|
||||
}
|
||||
catch(e){console.error(e); throw e}
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
test('complex structured if', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/left-branch-content-and-two-consecutive-ifs.odt')
|
||||
const templateContent = `Utilisation de sources lumineuses : {#if scientifique.source_lumineuses}Oui{:else}Non{/if}
|
||||
{#if scientifique.source_lumineuses && scientifique.modalités_source_lumineuses }
|
||||
Modalités d’utilisation de sources lumineuses : {scientifique.modalités_source_lumineuses}
|
||||
{/if}
|
||||
`
|
||||
|
||||
const data = {
|
||||
scientifique: {
|
||||
source_lumineuses: false,
|
||||
//modalités_source_lumineuses: 'lampes torches'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
t.deepEqual(templateTextContent.trim(), templateContent.trim(), 'reconnaissance du template')
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
const odtResultTextContent = await getOdtTextContent(odtResult)
|
||||
t.deepEqual(odtResultTextContent.trim(), `Utilisation de sources lumineuses : Non`)
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
import test from 'ava';
|
||||
import {join} from 'node:path';
|
||||
import { readFile } from 'node:fs/promises'
|
||||
|
||||
import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
|
||||
|
||||
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
|
||||
import {fillOdtTemplate} from '../../exports.js'
|
||||
import { listZipEntries } from '../helpers/zip-analysis.js';
|
||||
import { getContentDocument } from '../../scripts/odf/odt/getOdtTextContent.js';
|
||||
|
||||
|
||||
test('template filling preserves images', async t => {
|
||||
@ -37,48 +35,4 @@ test('template filling preserves images', async t => {
|
||||
`One zip entry of the result is expected to have a name that starts with 'Pictures/'`
|
||||
)
|
||||
|
||||
})
|
||||
|
||||
test('insert 2 images', async t => {
|
||||
const templatePath = join(import.meta.dirname, '../fixtures/basic-image-insertion.odt')
|
||||
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
const templateContent = `{title}
|
||||
|
||||
{#each photos as photo}
|
||||
{#image photo}
|
||||
{/each}
|
||||
`
|
||||
const templateTextContent = await getOdtTextContent(odtTemplate)
|
||||
|
||||
t.is(templateTextContent, templateContent, 'reconnaissance du template')
|
||||
|
||||
const photo1Path = join(import.meta.dirname, '../fixtures/pitchou-1.png')
|
||||
const photo2Path = join(import.meta.dirname, '../fixtures/pitchou-2.png')
|
||||
|
||||
const photo1Buffer = (await readFile(photo1Path)).buffer
|
||||
const photo2Buffer = (await readFile(photo2Path)).buffer
|
||||
|
||||
const photos = [{content: photo1Buffer, fileName: 'pitchou-1.png', mediaType: 'image/png'}, {content: photo2Buffer, fileName: 'pitchou-2.png', mediaType: 'image/png'}]
|
||||
|
||||
const data = {
|
||||
title: 'Titre de mon projet',
|
||||
photos,
|
||||
}
|
||||
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
const resultEntries = await listZipEntries(odtResult)
|
||||
|
||||
|
||||
t.is(
|
||||
resultEntries.filter(entry => entry.filename.startsWith('Pictures/')).length, 2,
|
||||
`Two pictures in 'Pictures/' folder are expected`
|
||||
)
|
||||
|
||||
const odtContentDocument = await getContentDocument(odtResult)
|
||||
|
||||
const drawImageElements = odtContentDocument.getElementsByTagName('draw:image')
|
||||
t.is(drawImageElements.length, 2, 'Two draw:image elements should be in the generated document.')
|
||||
|
||||
})
|
||||
BIN
tests/fixtures/basic-image-insertion.odt
vendored
BIN
tests/fixtures/basic-image-insertion.odt
vendored
Binary file not shown.
BIN
tests/fixtures/cellule avec style.ods
vendored
BIN
tests/fixtures/cellule avec style.ods
vendored
Binary file not shown.
BIN
tests/fixtures/cellules avec emails.ods
vendored
BIN
tests/fixtures/cellules avec emails.ods
vendored
Binary file not shown.
Binary file not shown.
BIN
tests/fixtures/ghost-if.odt
vendored
BIN
tests/fixtures/ghost-if.odt
vendored
Binary file not shown.
BIN
tests/fixtures/if-then-each.odt
vendored
BIN
tests/fixtures/if-then-each.odt
vendored
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
tests/fixtures/pitchou-1.png
vendored
BIN
tests/fixtures/pitchou-1.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 768 KiB |
BIN
tests/fixtures/pitchou-2.png
vendored
BIN
tests/fixtures/pitchou-2.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 799 KiB |
BIN
tests/fixtures/text-after-closing-each.odt
vendored
BIN
tests/fixtures/text-after-closing-each.odt
vendored
Binary file not shown.
@ -55,34 +55,4 @@ Pourquoi t'aimeraient-ils seulement comme tu es ? (hein)
|
||||
Si t'es pas comme eux quand t'es naturel`
|
||||
|
||||
t.deepEqual(feuille1[0][0].value, expectedValue)
|
||||
});
|
||||
|
||||
test('.ods cells with mails should be recognized', async t => {
|
||||
const odsFileWithEmails = (await readFile('./tests/fixtures/cellules avec emails.ods')).buffer
|
||||
const table = await getODSTableRawContent(odsFileWithEmails);
|
||||
|
||||
const feuille1 = table.get('Feuille1')
|
||||
|
||||
const row1 = feuille1[0]
|
||||
t.deepEqual(row1[0].value, 'Nom')
|
||||
t.deepEqual(row1[1].value, 'Email')
|
||||
|
||||
const row2 = feuille1[1]
|
||||
|
||||
t.deepEqual(row2[0].value, 'Dav')
|
||||
t.deepEqual(row2[1].value, 'david@example.org')
|
||||
|
||||
const row3 = feuille1[2]
|
||||
t.deepEqual(row3[0].value, 'Fanny')
|
||||
t.deepEqual(row3[1].value, 'lemaildeFanny@example.com')
|
||||
});
|
||||
|
||||
test('.ods cells with partially styled content should be recognized', async t => {
|
||||
const odsFileWithStyle = (await readFile('./tests/fixtures/cellule avec style.ods')).buffer;
|
||||
const table = await getODSTableRawContent(odsFileWithStyle);
|
||||
|
||||
const feuille1 = table.get('Feuille1');
|
||||
|
||||
const row1 = feuille1[0];
|
||||
t.deepEqual(row1[0].value, 'Toto titi');
|
||||
});
|
||||
});
|
||||
@ -1,28 +1,31 @@
|
||||
import {writeFile, readFile} from 'node:fs/promises'
|
||||
import {writeFile} from 'node:fs/promises'
|
||||
import {join} from 'node:path';
|
||||
|
||||
import {getOdtTemplate} from '../scripts/odf/odtTemplate-forNode.js'
|
||||
import {fillOdtTemplate} from '../exports.js'
|
||||
import {fillOdtTemplate} from '../scripts/node.js'
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/template-anniversaire.odt')
|
||||
const templatePath = join(import.meta.dirname, '../tests/data/template-anniversaire.odt')
|
||||
const data = {
|
||||
nom: 'David Bruant',
|
||||
dateNaissance: '8 mars 1987'
|
||||
}
|
||||
*/
|
||||
|
||||
/*const templatePath = join(import.meta.dirname, '../tests/fixtures/enum-courses.odt')
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/data/liste-courses.odt')
|
||||
const data = {
|
||||
listeCourses : [
|
||||
'Radis',
|
||||
`Jus d'orange`,
|
||||
'Pâtes à lasagne (fraîches !)'
|
||||
]
|
||||
}*/
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/liste-fruits-et-légumes.odt')
|
||||
const templatePath = join(import.meta.dirname, '../tests/data/liste-fruits-et-légumes.odt')
|
||||
const data = {
|
||||
fruits : [
|
||||
'Pastèque 🍉',
|
||||
@ -37,7 +40,7 @@ const data = {
|
||||
}*/
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/légumes-de-saison.odt')
|
||||
const templatePath = join(import.meta.dirname, '../tests/data/légumes-de-saison.odt')
|
||||
const data = {
|
||||
légumesSaison : [
|
||||
{
|
||||
@ -76,8 +79,8 @@ const data = {
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/tableau-simple.odt')
|
||||
|
||||
const templatePath = join(import.meta.dirname, '../tests/data/tableau-simple.odt')
|
||||
const data = {
|
||||
annéeConsos : [
|
||||
{ année: 1970, conso: 36252.637},
|
||||
@ -88,60 +91,16 @@ const data = {
|
||||
{ année: 2020, conso: 37859.246},
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/template-avec-image.odt')
|
||||
const templatePath = join(import.meta.dirname, '../tests/data/template-avec-image.odt')
|
||||
|
||||
const data = {
|
||||
commentaire : `J'adooooooore 🤩 West covinaaaaaaaaaaa 🎶`
|
||||
}
|
||||
*/
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/partially-formatted-variable.odt')
|
||||
const data = {nombre : 37}
|
||||
*/
|
||||
|
||||
/*
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/text-after-closing-each.odt')
|
||||
const data = {
|
||||
saison: 'Printemps',
|
||||
légumes: [
|
||||
'Asperge',
|
||||
'Betterave',
|
||||
'Blette'
|
||||
]
|
||||
}
|
||||
*/
|
||||
// const templatePath = join(import.meta.dirname, '../tests/fixtures/text-after-closing-each.odt')
|
||||
// const data = {
|
||||
// saison: 'Printemps',
|
||||
// légumes: [
|
||||
// 'Asperge',
|
||||
// 'Betterave',
|
||||
// 'Blette'
|
||||
// ]
|
||||
// }
|
||||
|
||||
// const templatePath = join(import.meta.dirname, '../tests/fixtures/if-then-each.odt')
|
||||
// const data = {liste_départements : ['95', '33']}
|
||||
|
||||
|
||||
const templatePath = join(import.meta.dirname, '../tests/fixtures/basic-image-insertion.odt')
|
||||
const photo1Path = join(import.meta.dirname, '../tests/fixtures/pitchou-1.png')
|
||||
const photo2Path = join(import.meta.dirname, '../tests/fixtures/pitchou-2.png')
|
||||
|
||||
const photo1Buffer = (await readFile(photo1Path)).buffer
|
||||
const photo2Buffer = (await readFile(photo2Path)).buffer
|
||||
|
||||
const photos = [{content: photo1Buffer, fileName: 'pitchou-1.png', mediaType: 'image/png'}, {content: photo2Buffer, fileName: 'pitchou-2.png', mediaType: 'image/png'}]
|
||||
|
||||
const data = {
|
||||
title: 'Titre de mon projet',
|
||||
photos,
|
||||
}
|
||||
|
||||
|
||||
const odtTemplate = await getOdtTemplate(templatePath)
|
||||
const odtResult = await fillOdtTemplate(odtTemplate, data)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user