From 2c20c653750a0b28e4dfd76e275d7ac556849e78 Mon Sep 17 00:00:00 2001 From: David Bruant Date: Sun, 27 Apr 2025 12:14:40 +0200 Subject: [PATCH] passing tests except the if one --- scripts/odf/fillOdtTemplate.js | 27 +++++++++++++++++++-------- tests/fill-odt-template/basic.js | 1 - tests/fill-odt-template/each.js | 16 +++++++--------- tests/fill-odt-template/if.js | 3 +-- tests/fill-odt-template/image.js | 2 +- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/scripts/odf/fillOdtTemplate.js b/scripts/odf/fillOdtTemplate.js index d7a84f6..1502ea9 100644 --- a/scripts/odf/fillOdtTemplate.js +++ b/scripts/odf/fillOdtTemplate.js @@ -294,7 +294,8 @@ function fillTemplatedOdtElement(rootElement, compartment){ // @ts-ignore traverse(rootElement, currentNode => { - const insideAnEachBlock = !!eachBlockOpeningNode + //console.log('currentlyUnclosedBlocks', currentlyUnclosedBlocks) + const insideAnOpenBlock = currentlyUnclosedBlocks.length >= 1 if(currentNode.nodeType === Node.TEXT_NODE){ const text = currentNode.textContent || '' @@ -303,9 +304,15 @@ function fillTemplatedOdtElement(rootElement, compartment){ const eachStartRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/; const startMatch = text.match(eachStartRegex); + //console.log('text, match', ) + if(startMatch){ - if(insideAnEachBlock){ - currentlyUnclosedBlocks.push(EACH) + //console.log('startMatch', startMatch) + + currentlyUnclosedBlocks.push(EACH) + + if(insideAnOpenBlock){ + // do nothing } else{ let [_, _iterableExpression, _itemExpression] = startMatch @@ -320,16 +327,18 @@ function fillTemplatedOdtElement(rootElement, compartment){ const eachClosingBlockString = '{/each}' const isEachClosingBlock = text.includes(eachClosingBlockString) - if(isEachClosingBlock){ + if(isEachClosingBlock){ + + //console.log('isEachClosingBlock', isEachClosingBlock) + if(!eachBlockOpeningNode) throw new Error(`{/each} found without corresponding opening {#each x as y}`) if(currentlyUnclosedBlocks.at(-1) !== EACH) throw new Error(`{/each} found while the last opened block was not an opening {#each x as y}`) - if(currentlyUnclosedBlocks.length >= 1){ + if(currentlyUnclosedBlocks.filter(x => x === EACH).length > 1){ // ignore because it will be treated as part of the outer {#each} - currentlyUnclosedBlocks.pop() } else{ eachBlockClosingNode = currentNode @@ -343,11 +352,13 @@ function fillTemplatedOdtElement(rootElement, compartment){ eachBlockItemExpression = undefined eachBlockClosingNode = undefined } + + currentlyUnclosedBlocks.pop() } // Looking for variables for substitutions - if(!insideAnEachBlock){ + if(!insideAnOpenBlock){ // @ts-ignore if (currentNode.data) { // @ts-ignore @@ -369,7 +380,7 @@ function fillTemplatedOdtElement(rootElement, compartment){ if(currentNode.nodeType === Node.ATTRIBUTE_NODE){ // Looking for variables for substitutions - if(!insideAnEachBlock){ + if(!insideAnOpenBlock){ // @ts-ignore if (currentNode.value) { // @ts-ignore diff --git a/tests/fill-odt-template/basic.js b/tests/fill-odt-template/basic.js index 5b2db16..704cb3a 100644 --- a/tests/fill-odt-template/basic.js +++ b/tests/fill-odt-template/basic.js @@ -4,7 +4,6 @@ import {join} from 'node:path'; import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js' import {fillOdtTemplate, getOdtTextContent} from '../../exports.js' -import { listZipEntries } from '../helpers/zip-analysis.js'; test('basic template filling with variable substitution', async t => { diff --git a/tests/fill-odt-template/each.js b/tests/fill-odt-template/each.js index f852cde..c39de73 100644 --- a/tests/fill-odt-template/each.js +++ b/tests/fill-odt-template/each.js @@ -4,11 +4,9 @@ import {join} from 'node:path'; import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js' import {fillOdtTemplate, getOdtTextContent} from '../../exports.js' -import { listZipEntries } from '../helpers/zip-analysis.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 templateContent = `🧺 La liste de courses incroyable 🧺 @@ -45,7 +43,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 templateContent = `🧺 La liste de courses incroyable 🧺 @@ -75,7 +73,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 templateContent = `🧺 La liste de courses incroyable 🧺 @@ -112,7 +110,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 templateContent = `Liste de fruits et légumes @@ -164,7 +162,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 templateContent = `Légumes de saison @@ -249,7 +247,7 @@ Hiver }); -test.skip('template filling {#each ...}{/each} within a single text node', async t => { +test('template filling {#each ...}{/each} within a single text node', async t => { const templatePath = join(import.meta.dirname, '../fixtures/liste-nombres.odt') const templateContent = `Liste de nombres @@ -276,7 +274,7 @@ Les nombres : 1 1 2 3 5 8 13 21  !! }); -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 templateContent = `Évolution énergie en kWh par personne en France diff --git a/tests/fill-odt-template/if.js b/tests/fill-odt-template/if.js index e3d8ca2..f41ddf2 100644 --- a/tests/fill-odt-template/if.js +++ b/tests/fill-odt-template/if.js @@ -4,10 +4,9 @@ import {join} from 'node:path'; import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js' import {fillOdtTemplate, getOdtTextContent} from '../../exports.js' -import { listZipEntries } from '../helpers/zip-analysis.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} diff --git a/tests/fill-odt-template/image.js b/tests/fill-odt-template/image.js index 34c9bfd..9162feb 100644 --- a/tests/fill-odt-template/image.js +++ b/tests/fill-odt-template/image.js @@ -3,7 +3,7 @@ import {join} from 'node:path'; import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js' -import {fillOdtTemplate, getOdtTextContent} from '../../exports.js' +import {fillOdtTemplate} from '../../exports.js' import { listZipEntries } from '../helpers/zip-analysis.js';