test with partially formatted variable passes

This commit is contained in:
David Bruant 2025-05-08 17:08:35 +02:00
parent 0564c974d2
commit dd09b7a117
5 changed files with 39 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import {traverse, Node} from '../../DOMUtils.js'
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex} from './markers.js'
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js'
/**
* @typedef TextPlaceToFill
@ -14,7 +14,8 @@ import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, if
* @returns {TextPlaceToFill | undefined}
*/
function findPlacesToFillInString(str, compartment) {
const matches = str.matchAll(/\{([^{#\/]+?)\}/g)
const varRexExp = new RegExp(variableRegex.source, 'g');
const matches = str.matchAll(varRexExp)
/** @type {TextPlaceToFill['expressions']} */
const expressions = []

View File

@ -1,4 +1,6 @@
// the regexps below are shared, so they shoudn't have state (no 'g' flag)
export const variableRegex = /\{([^{#\/]+?)\}/
export const ifStartMarkerRegex = /{#if\s+([^}]+?)\s*}/;
export const elseMarker = '{:else}'
export const closingIfMarker = '{/if}'

View File

@ -1,5 +1,5 @@
import {traverse, Node} from "../../DOMUtils.js";
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex} from './markers.js'
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js'
/**
*
@ -183,8 +183,6 @@ function consolidateMarkers(document){
containerTextNodesInTreeOrder.push(/** @type {Text} */(node))
}
})
console.log('containerTextNodesInTreeOrder', containerTextNodesInTreeOrder.map(n => n.textContent))
}
refreshContainerTextNodes()
@ -200,18 +198,19 @@ function consolidateMarkers(document){
...findAllMatches(fullText, elseMarker),
...findAllMatches(fullText, closingIfMarker),
...findAllMatches(fullText, eachStartMarkerRegex),
...findAllMatches(fullText, eachClosingMarker)
...findAllMatches(fullText, eachClosingMarker),
...findAllMatches(fullText, variableRegex)
];
if(positionedMarkers.length >= 1)
console.log('positionedMarkers', positionedMarkers)
/*if(positionedMarkers.length >= 1)
console.log('positionedMarkers', positionedMarkers)*/
while(consolidatedMarkers.length < positionedMarkers.length) {
refreshContainerTextNodes()
// For each marker, check if it's contained within a single text node
for(const positionedMarker of positionedMarkers.slice(consolidatedMarkers.length)) {
console.log('positionedMarker', positionedMarker)
//console.log('positionedMarker', positionedMarker)
let currentPos = 0;
let startNode;
@ -222,8 +221,6 @@ function consolidateMarkers(document){
const nodeStart = currentPos;
const nodeEnd = nodeStart + textNode.textContent.length;
console.log('nodeStart, nodeEnd', nodeStart, nodeEnd)
// If start of marker is in this node
if(!startNode && positionedMarker.index >= nodeStart && positionedMarker.index < nodeEnd) {
startNode = textNode;
@ -239,8 +236,6 @@ function consolidateMarkers(document){
currentPos = nodeEnd;
}
console.log('startNode, endNode', startNode?.textContent, endNode?.textContent)
if(!startNode){
throw new Error(`Could not find startNode for marker '${positionedMarker.marker}'`)
}
@ -251,8 +246,6 @@ function consolidateMarkers(document){
// Check if marker spans multiple nodes
if(startNode !== endNode) {
const commonAncestor = findCommonAncestor(startNode, endNode);
// Calculate relative positions within the nodes
let startNodeTextContent = startNode.textContent || '';
let endNodeTextContent = endNode.textContent || '';

View File

@ -86,7 +86,7 @@ Les nombres : 3 5 8 13  !!
});
//
test('template filling - {/each} and text after partially formatted', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-each-end-and-after-formatted.odt')
const templateContent = `Liste de nombres
@ -112,3 +112,30 @@ Les nombres : 5 8 13 21  !!
`)
});
test('template filling - partially formatted variable', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/partially-formatted-variable.odt')
const templateContent = `Nombre
Voici le nombre : {nombre} !!!
`
const data = {nombre : 37}
const odtTemplate = await getOdtTemplate(templatePath)
const templateTextContent = await getOdtTextContent(odtTemplate)
t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template')
const odtResult = await fillOdtTemplate(odtTemplate, data)
const odtResultTextContent = await getOdtTextContent(odtResult)
t.deepEqual(odtResultTextContent, `Nombre
Voici le nombre : 37 !!!
`)
});

Binary file not shown.