remove open if block after {/if}

This commit is contained in:
David Bruant 2025-05-21 12:36:02 +02:00
parent 788acba66b
commit 86c1676614
4 changed files with 18 additions and 32 deletions

View File

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

View File

@ -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)
}
})*/
}

View File

@ -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 dutilisation de sources lumineuses : {scientifique.modalités_source_lumineuses}