diff --git a/readme.md b/readme.md index c99243e..93bfa68 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ Small lib to parse/understand .ods and .xsls files in the browser and node.js ### Install ```sh -npm i https://github.com/DavidBruant/ods-xlsx.git#v0.7.0 +npm i https://github.com/DavidBruant/ods-xlsx.git#v0.8.0 ``` diff --git a/scripts/shared.js b/scripts/shared.js index e996da6..6bd8c52 100644 --- a/scripts/shared.js +++ b/scripts/shared.js @@ -3,6 +3,44 @@ import { unzip } from 'unzipit'; import './types.js' +// https://dom.spec.whatwg.org/#interface-node +const TEXT_NODE = 3 + +/** + * + * @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 + text += child.nodeValue; + } else if (child.nodeName === 'text:p') { + if (text.length > 0) { + // Add a newline before appending new paragraph if text already exists + text += '\n'; + } + const pNodes = child.childNodes; + for (const pChild of Array.from(pNodes)) { + if (pChild.nodeType === TEXT_NODE) { + text += pChild.nodeValue; // Append text inside + } else if (pChild.nodeName === 'text:line-break') { + text += '\n'; // Append newline for + } + } + } else if (child.nodeName === 'text:line-break') { + text += '\n'; // Append newline for directly under + } + } + + return text.trim(); +} + + /** * Extracts raw table content from an ODS file. * @param {ArrayBuffer} arrayBuffer - The ODS file. @@ -35,7 +73,7 @@ export async function _getODSTableRawContent(arrayBuffer, parseXML) { let cellValue; if (cellType === 'string') { - cellValue = cell.textContent; + cellValue = extraxtODSCellText(cell) } else if (cellType === 'date') { cellValue = cell.getAttribute('office:date-value'); } else { diff --git a/tests/data/cellule avec sauts.ods b/tests/data/cellule avec sauts.ods new file mode 100644 index 0000000..ca5a419 Binary files /dev/null and b/tests/data/cellule avec sauts.ods differ diff --git a/tests/ods-files.js b/tests/ods-files.js index 05b8190..6635de9 100644 --- a/tests/ods-files.js +++ b/tests/ods-files.js @@ -35,4 +35,24 @@ test('.ods cells with dates should be recognized', async t => { t.deepEqual(row3[0].value, 'Fanny') t.deepEqual(row3[1].type, 'date') t.deepEqual(row3[1].value, '1986-06-01') +}); + + +test('.ods file with new lines in content is ', async t => { + const repeatedCellFileContent = (await readFile('./tests/data/cellule avec sauts.ods')).buffer + + const table = await getODSTableRawContent(repeatedCellFileContent); + + const feuille1 = table.get('Feuille1') + +const expectedValue = `Deviens génial, deviens génial +Tu n'sais pas encore l'enfer qui t'attend +Le regard des uns, le rejet des autres +Si t'es bizarre, si t'es pas marrant +Deviens génial, deviens génial +Deviens génial, deviens génial +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) }); \ No newline at end of file