fillOdtElementTemplate now takes an array of nodes as argument for the situation of calling it on the middleContent of a block with several elements so they're evaluated together

This commit is contained in:
David Bruant 2025-07-19 14:03:19 +02:00
parent 820823ff49
commit 67e245fe5a
3 changed files with 193 additions and 172 deletions

View File

@ -14,7 +14,7 @@ class TemplateDOMBranch{
/** @type {Node} */ /** @type {Node} */
#leafNode #leafNode
// ancestors with this.#ancestors[0] === this.#startNode and this.#ancestors.at(-1) === this.#leafNode // ancestors with this.#ancestors[0] === this.#branchBaseNode and this.#ancestors.at(-1) === this.#leafNode
/** @type {Node[]} */ /** @type {Node[]} */
#ancestors #ancestors
@ -235,9 +235,12 @@ class TemplateBlock{
} }
//console.log('[fillBlockContentTemplate] after startChild') //console.log('[fillBlockContentTemplate] after startChild')
for(const content of this.#middleContent){
fillOdtElementTemplate(content, compartement) // if content consists of several parts of an {#each}{/each}
} // when arriving to the {/each}, it will be alone (and imbalanced)
// and will trigger an error
fillOdtElementTemplate(Array.from(this.#middleContent), compartement)
//console.log('[fillBlockContentTemplate] after middleContent') //console.log('[fillBlockContentTemplate] after middleContent')
const endChild = this.endBranch.at(1) const endChild = this.endBranch.at(1)
@ -554,14 +557,22 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
const IF = ifStartMarkerRegex.source const IF = ifStartMarkerRegex.source
const EACH = eachStartMarkerRegex.source const EACH = eachStartMarkerRegex.source
/** @typedef {Element | DocumentFragment | Document} RootElementArgument */
/** /**
* *
* @param {Element | DocumentFragment | Document} rootElement * @param {RootElementArgument | RootElementArgument[]} rootElements
* @param {Compartment} compartment * @param {Compartment} compartment
* @returns {void} * @returns {void}
*/ */
export default function fillOdtElementTemplate(rootElement, compartment) { export default function fillOdtElementTemplate(rootElements, compartment) {
//console.log('[fillTemplatedOdtElement]', rootElement.nodeType, rootElement.nodeName, rootElement.textContent)
if(!Array.isArray(rootElements)){
rootElements = [rootElements]
}
//console.log('[fillTemplatedOdtElement]', rootElements.length, rootElements[0].nodeType, rootElements[0].nodeName, rootElements[0].textContent)
//console.log('[fillTemplatedOdtElement]', rootElement.documentElement && rootElement.documentElement.textContent) //console.log('[fillTemplatedOdtElement]', rootElement.documentElement && rootElement.documentElement.textContent)
let currentlyOpenBlocks = [] let currentlyOpenBlocks = []
@ -585,6 +596,8 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
// Traverse "in document order" // Traverse "in document order"
for(const rootElement of rootElements){
// @ts-ignore // @ts-ignore
traverse(rootElement, currentNode => { traverse(rootElement, currentNode => {
//console.log('currentlyOpenBlocks', currentlyOpenBlocks) //console.log('currentlyOpenBlocks', currentlyOpenBlocks)
@ -626,8 +639,9 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
//console.log('isEachClosingBlock', isEachClosingBlock) //console.log('isEachClosingBlock', isEachClosingBlock)
if(!eachOpeningMarkerNode) if(!eachOpeningMarkerNode){
throw new Error(`{/each} found without corresponding opening {#each x as y}`) throw new Error(`{/each} found without corresponding opening {#each x as y}`)
}
if(currentlyOpenBlocks.at(-1) !== EACH) if(currentlyOpenBlocks.at(-1) !== EACH)
throw new Error(`{/each} found while the last opened block was not an opening {#each x as y}`) throw new Error(`{/each} found while the last opened block was not an opening {#each x as y}`)
@ -637,8 +651,12 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
// found an {#each} and its corresponding {/each} // found an {#each} and its corresponding {/each}
// execute replacement loop // execute replacement loop
//console.log('start of fillEachBlock')
fillEachBlock(eachOpeningMarkerNode, eachBlockIterableExpression, eachBlockItemExpression, eachClosingMarkerNode, compartment) fillEachBlock(eachOpeningMarkerNode, eachBlockIterableExpression, eachBlockItemExpression, eachClosingMarkerNode, compartment)
//console.log('end of fillEachBlock')
eachOpeningMarkerNode = undefined eachOpeningMarkerNode = undefined
eachBlockIterableExpression = undefined eachBlockIterableExpression = undefined
eachBlockItemExpression = undefined eachBlockItemExpression = undefined
@ -648,6 +666,7 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
// ignore because it will be treated as part of the outer {#each} // ignore because it will be treated as part of the outer {#each}
} }
//console.log('popping currentlyOpenBlocks')
currentlyOpenBlocks.pop() currentlyOpenBlocks.pop()
} }
@ -768,4 +787,6 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
} }
} }
}) })
}
} }

View File

@ -6,7 +6,7 @@ import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js' import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
test.skip('basic template filling with {#each}', async t => { test('basic template filling with {#each}', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/enum-courses.odt') const templatePath = join(import.meta.dirname, '../fixtures/enum-courses.odt')
const templateContent = `🧺 La liste de courses incroyable 🧺 const templateContent = `🧺 La liste de courses incroyable 🧺
@ -44,7 +44,7 @@ Pâtes à lasagne (fraîches !)
}); });
test.skip('Filling with {#each} and non-iterable value results in no error and empty result', async t => { test('Filling with {#each} and non-iterable value results in no error and empty result', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/enum-courses.odt') const templatePath = join(import.meta.dirname, '../fixtures/enum-courses.odt')
const templateContent = `🧺 La liste de courses incroyable 🧺 const templateContent = `🧺 La liste de courses incroyable 🧺
@ -74,7 +74,7 @@ test.skip('Filling with {#each} and non-iterable value results in no error and e
}); });
test.skip('template filling with {#each} generating a list', async t => { test('template filling with {#each} generating a list', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/liste-courses.odt') const templatePath = join(import.meta.dirname, '../fixtures/liste-courses.odt')
const templateContent = `🧺 La liste de courses incroyable 🧺 const templateContent = `🧺 La liste de courses incroyable 🧺
@ -111,7 +111,7 @@ test.skip('template filling with {#each} generating a list', async t => {
}); });
test.skip('template filling with 2 sequential {#each}', async t => { test('template filling with 2 sequential {#each}', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/liste-fruits-et-légumes.odt') const templatePath = join(import.meta.dirname, '../fixtures/liste-fruits-et-légumes.odt')
const templateContent = `Liste de fruits et légumes const templateContent = `Liste de fruits et légumes
@ -163,7 +163,7 @@ Poivron 🫑
}); });
test.skip('template filling with nested {#each}s', async t => { test('template filling with nested {#each}s', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/légumes-de-saison.odt') const templatePath = join(import.meta.dirname, '../fixtures/légumes-de-saison.odt')
const templateContent = `Légumes de saison const templateContent = `Légumes de saison
@ -248,7 +248,7 @@ Hiver
}); });
test.skip('template filling with text after {/each} in same text node', async t => { test('template filling with text after {/each} in same text node', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/text-after-closing-each.odt') const templatePath = join(import.meta.dirname, '../fixtures/text-after-closing-each.odt')
const templateContent = `Légumes de saison const templateContent = `Légumes de saison
@ -285,7 +285,7 @@ Blette,
}); });
test.skip('template filling of a table', async t => { test('template filling of a table', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/tableau-simple.odt') const templatePath = join(import.meta.dirname, '../fixtures/tableau-simple.odt')
const templateContent = `Évolution énergie en kWh par personne en France const templateContent = `Évolution énergie en kWh par personne en France
@ -346,8 +346,8 @@ Année
}); });
test('Mysterious {#each} bug', async t => { test('nested each without common ancestor for inner each', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/minimal-testcase-audrey.odt') const templatePath = join(import.meta.dirname, '../fixtures/nested-each-without-common-ancestor-for-inner-each.odt')
const templateContent = `{#each liste_espèces_par_impact as élément} const templateContent = `{#each liste_espèces_par_impact as élément}
{#each élément.liste_espèces as espèce} {#each élément.liste_espèces as espèce}
{/each} {/each}