From 86c167661447b0822d57bd6109b18b3d57508374 Mon Sep 17 00:00:00 2001 From: David Bruant Date: Wed, 21 May 2025 12:36:02 +0200 Subject: [PATCH] remove open if block after {/if} --- .../odf/templating/fillOdtElementTemplate.js | 25 +++++++++--------- .../odf/templating/prepareTemplateDOMTree.js | 21 +++------------ tests/fill-odt-template/if.js | 4 +-- ...ranch-content-and-two-consecutive-ifs.odt} | Bin 4 files changed, 18 insertions(+), 32 deletions(-) rename tests/fixtures/{weird-if-bug.odt => left-branch-content-and-two-consecutive-ifs.odt} (100%) diff --git a/scripts/odf/templating/fillOdtElementTemplate.js b/scripts/odf/templating/fillOdtElementTemplate.js index b864d41..6f53630 100644 --- a/scripts/odf/templating/fillOdtElementTemplate.js +++ b/scripts/odf/templating/fillOdtElementTemplate.js @@ -85,8 +85,8 @@ function findPlacesToFillInString(str, compartment) { * @returns {{startChild: Node, endChild:Node, content: DocumentFragment}} */ function extractBlockContent(blockStartNode, blockEndNode) { - console.log('[extractBlockContent] blockStartNode', blockStartNode.textContent) - console.log('[extractBlockContent] blockEndNode', blockEndNode.textContent) + //console.log('[extractBlockContent] blockStartNode', blockStartNode.textContent) + //console.log('[extractBlockContent] blockEndNode', blockEndNode.textContent) // find common ancestor of blockStartNode and blockEndNode let commonAncestor @@ -123,8 +123,8 @@ function extractBlockContent(blockStartNode, blockEndNode) { const startChild = startAncestryToCommonAncestor.at(-1) const endChild = endAncestryToCommonAncestor.at(-1) - console.log('[extractBlockContent] startChild', startChild.textContent) - console.log('[extractBlockContent] endChild', endChild.textContent) + //console.log('[extractBlockContent] startChild', startChild.textContent) + //console.log('[extractBlockContent] endChild', endChild.textContent) // Extract DOM content in a documentFragment /** @type {DocumentFragment} */ @@ -178,7 +178,7 @@ function extractBlockContent(blockStartNode, blockEndNode) { } - console.log('repeatedPatternArray', repeatedPatternArray.map(n => n.textContent)) + //console.log('repeatedPatternArray', repeatedPatternArray.map(n => n.textContent)) for(const sibling of repeatedPatternArray) { @@ -186,7 +186,7 @@ function extractBlockContent(blockStartNode, blockEndNode) { contentFragment.appendChild(sibling) } - console.log('extractBlockContent contentFragment', contentFragment.textContent) + //console.log('extractBlockContent contentFragment', contentFragment.textContent) return { startChild, @@ -252,9 +252,6 @@ function fillIfBlock(ifOpeningMarkerNode, ifElseMarkerNode, ifClosingMarkerNode, .add(startIfThenChild).add(endIfThenChild) } - console.log('chosen fragment', chosenFragment?.textContent) - - if(chosenFragment) { fillOdtElementTemplate( chosenFragment, @@ -410,9 +407,11 @@ export default function fillOdtElementTemplate(rootElement, compartment) { let ifBlockConditionExpression // Traverse "in document order" + // @ts-ignore traverse(rootElement, currentNode => { - //console.log('currentlyUnclosedBlocks', currentlyUnclosedBlocks) + //console.log('currentlyOpenBlocks', currentlyOpenBlocks) + const insideAnOpenBlock = currentlyOpenBlocks.length >= 1 if(currentNode.nodeType === Node.TEXT_NODE) { @@ -521,9 +520,9 @@ export default function fillOdtElementTemplate(rootElement, compartment) { /** * Looking for {/if} */ - const hasClosingMarker = text.includes(closingIfMarker); + const ifClosingMarker = text.includes(closingIfMarker); - if(hasClosingMarker) { + if(ifClosingMarker) { if(!insideAnOpenBlock) throw new Error('{/if} without a corresponding {#if}') @@ -546,6 +545,8 @@ export default function fillOdtElementTemplate(rootElement, compartment) { else { // do nothing because the marker is too deep } + + currentlyOpenBlocks.pop() } diff --git a/scripts/odf/templating/prepareTemplateDOMTree.js b/scripts/odf/templating/prepareTemplateDOMTree.js index 1bb9dc3..97aca53 100644 --- a/scripts/odf/templating/prepareTemplateDOMTree.js +++ b/scripts/odf/templating/prepareTemplateDOMTree.js @@ -200,8 +200,8 @@ function consolidateMarkers(document){ ]; - if(positionedMarkers.length >= 1) - console.log('positionedMarkers', positionedMarkers) + //if(positionedMarkers.length >= 1) + // console.log('positionedMarkers', positionedMarkers) while(consolidatedMarkers.length < positionedMarkers.length) { @@ -324,7 +324,7 @@ function consolidateMarkers(document){ } } - console.log('consolidatedMarkers', consolidatedMarkers) + //console.log('consolidatedMarkers', consolidatedMarkers) } /** @@ -551,22 +551,7 @@ export default function prepareTemplateDOMTree(document){ // after consolidateMarkers, each marker is in at most one text node // (formatting with markers is removed) - console.log('document text after consolidateMarkers', document.documentElement.textContent) - /*traverse(document, (node) => { - if(node.nodeType === Node.TEXT_NODE || node.nodeName.startsWith('text')){ - console.log('node', node.nodeName, node.textContent) - } - })*/ - - 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) - - console.log('document text after isolateMarkerText', document.documentElement.textContent) - /*traverse(document, (node) => { - if(node.nodeType === Node.TEXT_NODE || node.nodeName.startsWith('text')){ - console.log('node', node.nodeName, node.textContent) - } - })*/ } \ No newline at end of file diff --git a/tests/fill-odt-template/if.js b/tests/fill-odt-template/if.js index 6cf30c8..c934313 100644 --- a/tests/fill-odt-template/if.js +++ b/tests/fill-odt-template/if.js @@ -6,7 +6,7 @@ import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js' import {fillOdtTemplate, getOdtTextContent} from '../../exports.js' -test.skip('basic template filling with {#if}', 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} @@ -42,7 +42,7 @@ n est un grand nombre test('weird bug', async t => { - const templatePath = join(import.meta.dirname, '../fixtures/weird-if-bug.odt') + 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} diff --git a/tests/fixtures/weird-if-bug.odt b/tests/fixtures/left-branch-content-and-two-consecutive-ifs.odt similarity index 100% rename from tests/fixtures/weird-if-bug.odt rename to tests/fixtures/left-branch-content-and-two-consecutive-ifs.odt