diff --git a/scripts/odf/fillOdtTemplate.js b/scripts/odf/fillOdtTemplate.js index 08222b6..085c918 100644 --- a/scripts/odf/fillOdtTemplate.js +++ b/scripts/odf/fillOdtTemplate.js @@ -173,28 +173,70 @@ function extractBlockContent(blockStartNode, blockEndNode){ * @param {Compartment} compartment */ function fillIfBlock(ifOpeningMarkerNode, ifElseMarkerNode, ifClosingMarkerNode, ifBlockConditionExpression, compartment){ - console.log('fillIfBlock pas encore codée') - const conditionValue = compartment.evaluate(ifBlockConditionExpression) - if(conditionValue){ - // récupérer le morceau entre ifOpeningMarkerNode et ifElseMarkerNode - // l'executer + let startChild + let endChild + + let markerNodes = new Set() + + let chosenFragment + + if(ifElseMarkerNode){ + const { + startChild: startIfThenChild, + endChild: endIfThenChild, + content: thenFragment + } = extractBlockContent(ifOpeningMarkerNode, ifElseMarkerNode) + + const { + startChild: startIfElseChild, + endChild: endIfElseChild, + content: elseFragment + } = extractBlockContent(ifElseMarkerNode, ifClosingMarkerNode) + + chosenFragment = conditionValue ? thenFragment : elseFragment + startChild = startIfThenChild + endChild = endIfElseChild + + markerNodes + .add(startIfThenChild).add(endIfThenChild) + .add(startIfElseChild).add(endIfElseChild) } else{ + const { + startChild: startIfThenChild, + endChild: endIfThenChild, + content: thenFragment + } = extractBlockContent(ifOpeningMarkerNode, ifClosingMarkerNode) + chosenFragment = conditionValue ? thenFragment : undefined + startChild = startIfThenChild + endChild = endIfThenChild + + markerNodes + .add(startIfThenChild).add(endIfThenChild) } - // dans tous les cas, recupérer ce qu'il y a entre ifOpeningMarkerNode et ifClosingMarkerNode - // et le supprimer de l'arbre + if(chosenFragment){ + fillTemplatedOdtElement( + chosenFragment, + compartment + ) + endChild.parentNode.insertBefore(chosenFragment, endChild) + } + + for(const markerNode of markerNodes){ + try{ + // may throw if node already out of tree + // might happen if + markerNode.parentNode.removeChild(markerNode) + } + catch(e){} + } - /*throw `PPP - - executer l'expression - - selon la valeur, choisir le bon block à extraire/remplir - - - `*/ } @@ -242,7 +284,7 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c endChild.parentNode.insertBefore(itemFragment, endChild) } - // remove block elements + // remove block marker elements startChild.parentNode.removeChild(startChild) endChild.parentNode.removeChild(endChild) } diff --git a/tests/fill-odt-template/if.js b/tests/fill-odt-template/if.js index f41ddf2..729f0ca 100644 --- a/tests/fill-odt-template/if.js +++ b/tests/fill-odt-template/if.js @@ -10,10 +10,10 @@ test('basic template filling with {#if}', async t => { const templatePath = join(import.meta.dirname, '../fixtures/description-nombre.odt') const templateContent = `Description du nombre {n} -{#if n >5} -n est un grand nombre -{:else} +{#if n<5} n est un petit nombre +{:else} +n est un grand nombre {/if} ` diff --git a/tests/fixtures/description-nombre.odt b/tests/fixtures/description-nombre.odt index 7ede55e..e44bd4f 100644 Binary files a/tests/fixtures/description-nombre.odt and b/tests/fixtures/description-nombre.odt differ