tests passing

This commit is contained in:
David Bruant 2025-05-09 18:45:57 +02:00
parent 56576a589d
commit ccdcb8744d
2 changed files with 10 additions and 32 deletions

View File

@ -1,6 +1,5 @@
import {traverse, Node} from '../../DOMUtils.js' import {traverse, Node} from '../../DOMUtils.js'
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js' import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js'
import prepareTemplateDOMTree from './prepareTemplateDOMTree.js';
/** /**
* @typedef TextPlaceToFill * @typedef TextPlaceToFill
@ -86,7 +85,7 @@ function findPlacesToFillInString(str, compartment) {
* @returns {{startChild: Node, endChild:Node, content: DocumentFragment}} * @returns {{startChild: Node, endChild:Node, content: DocumentFragment}}
*/ */
function extractBlockContent(blockStartNode, blockEndNode) { function extractBlockContent(blockStartNode, blockEndNode) {
console.log('[extractBlockContent] blockEndNode', blockEndNode.textContent) //console.log('[extractBlockContent] blockEndNode', blockEndNode.textContent)
// find common ancestor of blockStartNode and blockEndNode // find common ancestor of blockStartNode and blockEndNode
let commonAncestor let commonAncestor
@ -121,7 +120,7 @@ function extractBlockContent(blockStartNode, blockEndNode) {
const startChild = startAncestryToCommonAncestor.at(-1) const startChild = startAncestryToCommonAncestor.at(-1)
const endChild = endAncestryToCommonAncestor.at(-1) const endChild = endAncestryToCommonAncestor.at(-1)
console.log('[extractBlockContent] endChild', endChild.textContent) //console.log('[extractBlockContent] endChild', endChild.textContent)
// Extract DOM content in a documentFragment // Extract DOM content in a documentFragment
/** @type {DocumentFragment} */ /** @type {DocumentFragment} */
@ -141,7 +140,7 @@ function extractBlockContent(blockStartNode, blockEndNode) {
contentFragment.appendChild(sibling) contentFragment.appendChild(sibling)
} }
console.log('extractBlockContent contentFragment', contentFragment.textContent) //console.log('extractBlockContent contentFragment', contentFragment.textContent)
return { return {
startChild, startChild,
@ -239,19 +238,11 @@ function fillIfBlock(ifOpeningMarkerNode, ifElseMarkerNode, ifClosingMarkerNode,
*/ */
function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, compartment) { function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, compartment) {
//console.log('fillEachBlock', iterableExpression, itemExpression) //console.log('fillEachBlock', iterableExpression, itemExpression)
console.log('startNode', startNode.nodeName, startNode.textContent)
console.log('endNode', endNode.nodeName, endNode.textContent)
console.log('endNode parent', endNode.parentNode.childNodes.length, endNode.parentNode.textContent)
const doc = startNode.ownerDocument.documentElement const doc = startNode.ownerDocument.documentElement
console.log('doc text', doc.textContent)
const {startChild, endChild, content: repeatedFragment} = extractBlockContent(startNode, endNode) const {startChild, endChild, content: repeatedFragment} = extractBlockContent(startNode, endNode)
console.log('endChild after extractBlockContent', endChild.textContent)
console.log('doc text', doc.textContent)
// Find the iterable in the data // Find the iterable in the data
// PPP eventually, evaluate the expression as a JS expression // PPP eventually, evaluate the expression as a JS expression
let iterable = compartment.evaluate(iterableExpression) let iterable = compartment.evaluate(iterableExpression)
@ -266,13 +257,10 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
// create each loop result // create each loop result
// using a for-of loop to accept all iterable values // using a for-of loop to accept all iterable values
for(const item of iterable) { for(const item of iterable) {
console.log('{#each}', itemExpression, item)
console.log('doc text', doc.textContent)
/** @type {DocumentFragment} */ /** @type {DocumentFragment} */
// @ts-ignore // @ts-ignore
const itemFragment = repeatedFragment.cloneNode(true) const itemFragment = repeatedFragment.cloneNode(true)
console.log('itemFragment', itemFragment.textContent)
let insideCompartment = new Compartment({ let insideCompartment = new Compartment({
globals: Object.assign({}, compartment.globalThis, {[itemExpression]: item}), globals: Object.assign({}, compartment.globalThis, {[itemExpression]: item}),
@ -292,8 +280,6 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
// eventually, will be set to the last item's last child // eventually, will be set to the last item's last child
lastItemLastChild = itemFragment.lastChild lastItemLastChild = itemFragment.lastChild
console.log('{#each} fragment', itemFragment.textContent)
endChild.parentNode.insertBefore(itemFragment, endChild) endChild.parentNode.insertBefore(itemFragment, endChild)
} }
@ -341,19 +327,15 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
} }
console.log('doc text after each', doc.textContent)
// remove block marker elements // remove block marker elements
startChild.parentNode.removeChild(startChild) startChild.parentNode.removeChild(startChild)
endChild.parentNode.removeChild(endChild) endChild.parentNode.removeChild(endChild)
console.log('doc text after removes', doc.textContent)
} }
const IF = 'IF' const IF = ifStartMarkerRegex.source
const EACH = 'EACH' const EACH = eachStartMarkerRegex.source
/** /**
* *
@ -433,14 +415,10 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
if(currentlyOpenBlocks.length === 1) { if(currentlyOpenBlocks.length === 1) {
eachClosingMarkerNode = currentNode eachClosingMarkerNode = currentNode
console.log('before fillEachBlock', eachClosingMarkerNode.parentNode.textContent)
// found an {#each} and its corresponding {/each} // found an {#each} and its corresponding {/each}
// execute replacement loop // execute replacement loop
fillEachBlock(eachOpeningMarkerNode, eachBlockIterableExpression, eachBlockItemExpression, eachClosingMarkerNode, compartment) fillEachBlock(eachOpeningMarkerNode, eachBlockIterableExpression, eachBlockItemExpression, eachClosingMarkerNode, compartment)
console.log('after fillEachBlock', rootElement.documentElement.textContent)
eachOpeningMarkerNode = undefined eachOpeningMarkerNode = undefined
eachBlockIterableExpression = undefined eachBlockIterableExpression = undefined
eachBlockItemExpression = undefined eachBlockItemExpression = undefined

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 🧺
@ -43,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 templatePath = join(import.meta.dirname, '../fixtures/enum-courses.odt')
const templateContent = `🧺 La liste de courses incroyable 🧺 const templateContent = `🧺 La liste de courses incroyable 🧺
@ -73,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 templatePath = join(import.meta.dirname, '../fixtures/liste-courses.odt')
const templateContent = `🧺 La liste de courses incroyable 🧺 const templateContent = `🧺 La liste de courses incroyable 🧺
@ -110,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 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
@ -162,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 templatePath = join(import.meta.dirname, '../fixtures/légumes-de-saison.odt')
const templateContent = `Légumes de saison const templateContent = `Légumes de saison