Error thrown by mistake for {#each} inside an {#if} (#19)

* reduced test case

* test case

* adding complex test case
This commit is contained in:
David Bruant 2025-09-12 17:22:19 +02:00 committed by GitHub
parent fbadfc7144
commit 8d3d91da2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 4 deletions

View File

@ -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}`)

View File

@ -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,`)
});

BIN
tests/fixtures/if-then-each.odt vendored Normal file

Binary file not shown.

View File

@ -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)