diff --git a/scripts/odf/templating/fillOdtElementTemplate.js b/scripts/odf/templating/fillOdtElementTemplate.js index 51203ba..6e25366 100644 --- a/scripts/odf/templating/fillOdtElementTemplate.js +++ b/scripts/odf/templating/fillOdtElementTemplate.js @@ -601,6 +601,7 @@ export default function fillOdtElementTemplate(rootElements, compartment) { // @ts-ignore traverse(rootElement, currentNode => { //console.log('currentlyOpenBlocks', currentlyOpenBlocks) + //console.log('eachOpeningMarkerNode', eachOpeningMarkerNode) const insideAnOpenBlock = currentlyOpenBlocks.length >= 1 @@ -637,11 +638,10 @@ export default function fillOdtElementTemplate(rootElements, compartment) { if(isEachClosingBlock) { - //console.log('isEachClosingBlock', isEachClosingBlock) + //console.log('isEachClosingBlock', isEachClosingBlock, currentlyOpenBlocks) - if(!eachOpeningMarkerNode){ - throw new Error(`{/each} found without corresponding opening {#each x as y}`) - } + if(!insideAnOpenBlock) + throw new Error('{/each} found without corresponding opening {#each x as y}') if(currentlyOpenBlocks.at(-1) !== EACH) throw new Error(`{/each} found while the last opened block was not an opening {#each x as y}`) diff --git a/tests/fill-odt-template/complex.js b/tests/fill-odt-template/complex.js new file mode 100644 index 0000000..970cd7b --- /dev/null +++ b/tests/fill-odt-template/complex.js @@ -0,0 +1,25 @@ +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,`) + +}); + diff --git a/tests/fixtures/if-then-each.odt b/tests/fixtures/if-then-each.odt new file mode 100644 index 0000000..1b6f1ae Binary files /dev/null and b/tests/fixtures/if-then-each.odt differ diff --git a/tools/create-odt-file-from-template.js b/tools/create-odt-file-from-template.js index 1c1a8f3..6af5ea8 100644 --- a/tools/create-odt-file-from-template.js +++ b/tools/create-odt-file-from-template.js @@ -103,6 +103,7 @@ const templatePath = join(import.meta.dirname, '../tests/fixtures/partially-form const data = {nombre : 37} */ +/* const templatePath = join(import.meta.dirname, '../tests/fixtures/text-after-closing-each.odt') const data = { saison: 'Printemps', @@ -112,7 +113,10 @@ const data = { 'Blette' ] } +*/ +const templatePath = join(import.meta.dirname, '../tests/fixtures/if-then-each.odt') +const data = {liste_départements : ['95', '33']} const odtTemplate = await getOdtTemplate(templatePath)