test with partially formatted variable passes
This commit is contained in:
parent
0564c974d2
commit
dd09b7a117
@ -1,5 +1,5 @@
|
|||||||
import {traverse, Node} from '../../DOMUtils.js'
|
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
|
* @typedef TextPlaceToFill
|
||||||
@ -14,7 +14,8 @@ import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, if
|
|||||||
* @returns {TextPlaceToFill | undefined}
|
* @returns {TextPlaceToFill | undefined}
|
||||||
*/
|
*/
|
||||||
function findPlacesToFillInString(str, compartment) {
|
function findPlacesToFillInString(str, compartment) {
|
||||||
const matches = str.matchAll(/\{([^{#\/]+?)\}/g)
|
const varRexExp = new RegExp(variableRegex.source, 'g');
|
||||||
|
const matches = str.matchAll(varRexExp)
|
||||||
|
|
||||||
/** @type {TextPlaceToFill['expressions']} */
|
/** @type {TextPlaceToFill['expressions']} */
|
||||||
const expressions = []
|
const expressions = []
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
// the regexps below are shared, so they shoudn't have state (no 'g' flag)
|
// 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 ifStartMarkerRegex = /{#if\s+([^}]+?)\s*}/;
|
||||||
export const elseMarker = '{:else}'
|
export const elseMarker = '{:else}'
|
||||||
export const closingIfMarker = '{/if}'
|
export const closingIfMarker = '{/if}'
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import {traverse, Node} from "../../DOMUtils.js";
|
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))
|
containerTextNodesInTreeOrder.push(/** @type {Text} */(node))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('containerTextNodesInTreeOrder', containerTextNodesInTreeOrder.map(n => n.textContent))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshContainerTextNodes()
|
refreshContainerTextNodes()
|
||||||
@ -200,18 +198,19 @@ function consolidateMarkers(document){
|
|||||||
...findAllMatches(fullText, elseMarker),
|
...findAllMatches(fullText, elseMarker),
|
||||||
...findAllMatches(fullText, closingIfMarker),
|
...findAllMatches(fullText, closingIfMarker),
|
||||||
...findAllMatches(fullText, eachStartMarkerRegex),
|
...findAllMatches(fullText, eachStartMarkerRegex),
|
||||||
...findAllMatches(fullText, eachClosingMarker)
|
...findAllMatches(fullText, eachClosingMarker),
|
||||||
|
...findAllMatches(fullText, variableRegex)
|
||||||
];
|
];
|
||||||
|
|
||||||
if(positionedMarkers.length >= 1)
|
/*if(positionedMarkers.length >= 1)
|
||||||
console.log('positionedMarkers', positionedMarkers)
|
console.log('positionedMarkers', positionedMarkers)*/
|
||||||
|
|
||||||
while(consolidatedMarkers.length < positionedMarkers.length) {
|
while(consolidatedMarkers.length < positionedMarkers.length) {
|
||||||
refreshContainerTextNodes()
|
refreshContainerTextNodes()
|
||||||
|
|
||||||
// For each marker, check if it's contained within a single text node
|
// For each marker, check if it's contained within a single text node
|
||||||
for(const positionedMarker of positionedMarkers.slice(consolidatedMarkers.length)) {
|
for(const positionedMarker of positionedMarkers.slice(consolidatedMarkers.length)) {
|
||||||
console.log('positionedMarker', positionedMarker)
|
//console.log('positionedMarker', positionedMarker)
|
||||||
|
|
||||||
let currentPos = 0;
|
let currentPos = 0;
|
||||||
let startNode;
|
let startNode;
|
||||||
@ -222,8 +221,6 @@ function consolidateMarkers(document){
|
|||||||
const nodeStart = currentPos;
|
const nodeStart = currentPos;
|
||||||
const nodeEnd = nodeStart + textNode.textContent.length;
|
const nodeEnd = nodeStart + textNode.textContent.length;
|
||||||
|
|
||||||
console.log('nodeStart, nodeEnd', nodeStart, nodeEnd)
|
|
||||||
|
|
||||||
// If start of marker is in this node
|
// If start of marker is in this node
|
||||||
if(!startNode && positionedMarker.index >= nodeStart && positionedMarker.index < nodeEnd) {
|
if(!startNode && positionedMarker.index >= nodeStart && positionedMarker.index < nodeEnd) {
|
||||||
startNode = textNode;
|
startNode = textNode;
|
||||||
@ -239,8 +236,6 @@ function consolidateMarkers(document){
|
|||||||
currentPos = nodeEnd;
|
currentPos = nodeEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('startNode, endNode', startNode?.textContent, endNode?.textContent)
|
|
||||||
|
|
||||||
if(!startNode){
|
if(!startNode){
|
||||||
throw new Error(`Could not find startNode for marker '${positionedMarker.marker}'`)
|
throw new Error(`Could not find startNode for marker '${positionedMarker.marker}'`)
|
||||||
}
|
}
|
||||||
@ -251,8 +246,6 @@ function consolidateMarkers(document){
|
|||||||
|
|
||||||
// Check if marker spans multiple nodes
|
// Check if marker spans multiple nodes
|
||||||
if(startNode !== endNode) {
|
if(startNode !== endNode) {
|
||||||
const commonAncestor = findCommonAncestor(startNode, endNode);
|
|
||||||
|
|
||||||
// Calculate relative positions within the nodes
|
// Calculate relative positions within the nodes
|
||||||
let startNodeTextContent = startNode.textContent || '';
|
let startNodeTextContent = startNode.textContent || '';
|
||||||
let endNodeTextContent = endNode.textContent || '';
|
let endNodeTextContent = endNode.textContent || '';
|
||||||
|
|||||||
@ -86,7 +86,7 @@ Les nombres : 3 5 8 13 !!
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
test('template filling - {/each} and text after partially formatted', async t => {
|
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 templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-each-end-and-after-formatted.odt')
|
||||||
const templateContent = `Liste de nombres
|
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 !!!
|
||||||
|
`)
|
||||||
|
|
||||||
|
});
|
||||||
BIN
tests/fixtures/partially-formatted-variable.odt
vendored
Normal file
BIN
tests/fixtures/partially-formatted-variable.odt
vendored
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user