This commit is contained in:
David Bruant 2025-05-26 19:34:56 +02:00
parent 815f5e5adc
commit f8e24006d9
2 changed files with 41 additions and 39 deletions

View File

@ -40,6 +40,8 @@ class TemplateDOMBranch{
} }
removeLeafAndEmptyAncestors(){ removeLeafAndEmptyAncestors(){
//this.logBranch('[removeLeafAndEmptyAncestors] branch at the start')
// it may happen (else marker of if/else/endif) that the leaf was already removed as part of another block // it may happen (else marker of if/else/endif) that the leaf was already removed as part of another block
// so before removing anything, let's update #ancestors and #leaf // so before removing anything, let's update #ancestors and #leaf
@ -47,18 +49,24 @@ class TemplateDOMBranch{
if(!ancestor.parentNode){ if(!ancestor.parentNode){
// ancestor already removed from tree // ancestor already removed from tree
this.#ancestors = this.#ancestors.slice(0, i) this.#ancestors = this.#ancestors.slice(0, i)
this.#leafNode = this.#ancestors.at(-1)
return false; return false;
} }
return true // continue return true // continue
}) })
this.#leafNode = this.#ancestors.at(-1)
//this.logBranch('[removeLeafAndEmptyAncestors] after adjusting this.#ancestors')
//console.log('removeLeafAndEmptyAncestors', this.#startNode.textContent) //console.log('removeLeafAndEmptyAncestors', this.#startNode.textContent)
let nextLeaf = this.#leafNode.parentNode let nextLeaf
if(this.#leafNode !== this.#branchBaseNode){
nextLeaf = this.#leafNode.parentNode
//console.log('nextLeaf', !!nextLeaf) //console.log('nextLeaf', !!nextLeaf)
nextLeaf.removeChild(this.#leafNode) nextLeaf.removeChild(this.#leafNode)
this.#leafNode = nextLeaf this.#leafNode = nextLeaf
}
while(this.#leafNode !== this.#branchBaseNode && while(this.#leafNode !== this.#branchBaseNode &&
(this.#leafNode.textContent === null || this.#leafNode.textContent.trim() === '')) (this.#leafNode.textContent === null || this.#leafNode.textContent.trim() === ''))
@ -76,10 +84,10 @@ class TemplateDOMBranch{
* @param {number} [startIndex] * @param {number} [startIndex]
*/ */
removeRightContent(startIndex = 0){ removeRightContent(startIndex = 0){
console.log('[removeRightContent]', startIndex, this.#ancestors.slice(startIndex).length) //console.log('[removeRightContent]', startIndex, this.#ancestors.slice(startIndex).length)
for(const branchNode of this.#ancestors.slice(startIndex)){ for(const branchNode of this.#ancestors.slice(startIndex)){
console.log('[removeRightContent]', branchNode.nodeType, branchNode.nodeName) //console.log('[removeRightContent]', branchNode.nodeType, branchNode.nodeName)
let toRemove = branchNode.nextSibling let toRemove = branchNode.nextSibling
@ -154,7 +162,7 @@ class TemplateDOMBranch{
console.group('[TemplateDOMBranch] Showing branch') console.group('[TemplateDOMBranch] Showing branch')
console.log(message) console.log(message)
for(const node of this.#ancestors){ for(const node of this.#ancestors){
console.log('branch node', node.nodeType, node.nodeName) console.log('branch node', node.nodeType, node.nodeName, node.nodeType === node.TEXT_NODE ? node.textContent : '')
} }
console.groupEnd() console.groupEnd()
} }
@ -183,9 +191,9 @@ class TemplateBlock{
// @ts-expect-error xmldom.Node // @ts-expect-error xmldom.Node
this.#commonAncestor = findCommonAncestor(startNode, endNode) this.#commonAncestor = findCommonAncestor(startNode, endNode)
console.log('create start branch') //console.log('create start branch')
this.startBranch = new TemplateDOMBranch(this.#commonAncestor, startNode) this.startBranch = new TemplateDOMBranch(this.#commonAncestor, startNode)
console.log('create end branch') //console.log('create end branch')
this.endBranch = new TemplateDOMBranch(this.#commonAncestor, endNode) this.endBranch = new TemplateDOMBranch(this.#commonAncestor, endNode)
@ -198,11 +206,12 @@ class TemplateBlock{
content = content.nextSibling content = content.nextSibling
} }
console.log('\n== TemplateBlock ==') //console.group('\n== TemplateBlock ==')
console.log('startBranch', this.startBranch.at(1).nodeName, this.startBranch.at(1).textContent) //console.log('startBranch', this.startBranch.at(1).nodeName, this.startBranch.at(1).textContent)
console.log('middleContent', this.#middleContent.map(n => n.textContent).join('')) //console.log('middleContent', this.#middleContent.map(n => n.textContent).join(''))
console.log('endBranch', this.startBranch.at(1).nodeName, this.endBranch.at(1).textContent) //console.log('endBranch', this.startBranch.at(1).nodeName, this.endBranch.at(1).textContent)
console.log('common ancestor', this.#commonAncestor.nodeName, '\n') //console.log('common ancestor', this.#commonAncestor.nodeName, '\n')
//console.groupEnd()
} }
removeMarkersAndEmptyAncestors(){ removeMarkersAndEmptyAncestors(){
@ -217,31 +226,31 @@ class TemplateBlock{
* @param {Compartment} compartement * @param {Compartment} compartement
*/ */
fillBlockContentTemplate(compartement){ fillBlockContentTemplate(compartement){
console.log('[fillBlockContentTemplate] start') //console.log('[fillBlockContentTemplate] start')
const startChild = this.startBranch.at(1) const startChild = this.startBranch.at(1)
if(startChild /*&& startChild !== */){ if(startChild /*&& startChild !== */){
console.log('[fillBlockContentTemplate] startChild', startChild.nodeName, startChild.textContent) //console.log('[fillBlockContentTemplate] startChild', startChild.nodeName, startChild.textContent)
fillOdtElementTemplate(startChild, compartement) fillOdtElementTemplate(startChild, compartement)
} }
console.log('[fillBlockContentTemplate] after startChild') //console.log('[fillBlockContentTemplate] after startChild')
for(const content of this.#middleContent){ for(const content of this.#middleContent){
fillOdtElementTemplate(content, compartement) fillOdtElementTemplate(content, compartement)
} }
console.log('[fillBlockContentTemplate] after middleContent') //console.log('[fillBlockContentTemplate] after middleContent')
const endChild = this.endBranch.at(1) const endChild = this.endBranch.at(1)
console.log('fillBlockContentTemplate] [endBranch]') //console.log('fillBlockContentTemplate] [endBranch]')
this.endBranch.logBranch('endBranch') //this.endBranch.logBranch('endBranch')
if(endChild){ if(endChild){
console.log('[fillBlockContentTemplate] endChild', endChild.nodeName, endChild.textContent) //console.log('[fillBlockContentTemplate] endChild', endChild.nodeName, endChild.textContent)
fillOdtElementTemplate(endChild, compartement) fillOdtElementTemplate(endChild, compartement)
} }
console.log('[fillBlockContentTemplate] after endChild') //console.log('[fillBlockContentTemplate] after endChild')
console.log('[fillBlockContentTemplate] end') //console.log('[fillBlockContentTemplate] end')
} }
removeContent(){ removeContent(){
@ -472,7 +481,7 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
//console.log('fillEachBlock', iterableExpression, itemExpression) //console.log('fillEachBlock', iterableExpression, itemExpression)
const docEl = startNode.ownerDocument.documentElement const docEl = startNode.ownerDocument.documentElement
console.log('[fillEachBlock] docEl', docEl.textContent) //console.log('[fillEachBlock] docEl', docEl.textContent)
const repeatedTemplateBlock = new TemplateBlock(startNode, endNode) const repeatedTemplateBlock = new TemplateBlock(startNode, endNode)
@ -496,12 +505,12 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
let nextTemplateBlock = repeatedTemplateBlock let nextTemplateBlock = repeatedTemplateBlock
iterable.forEach((item, i) => { iterable.forEach((item, i) => {
console.log('[fillEachBlock] loop i', i, docEl.textContent) //console.log('[fillEachBlock] loop i', i, docEl.textContent)
const firstItem = i === 0 const firstItem = i === 0
const lastItem = i === iterable.length - 1 const lastItem = i === iterable.length - 1
let currentTemplateBlock = nextTemplateBlock; let currentTemplateBlock = nextTemplateBlock;
console.log('currentTemplateBlock', currentTemplateBlock.startBranch.at(0).textContent) //console.log('currentTemplateBlock', currentTemplateBlock.startBranch.at(0).textContent)
if(!lastItem){ if(!lastItem){
nextTemplateBlock = currentTemplateBlock.cloneAndAppendAfter() nextTemplateBlock = currentTemplateBlock.cloneAndAppendAfter()
@ -516,21 +525,18 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
currentTemplateBlock.startBranch.removeLeftContent(2) currentTemplateBlock.startBranch.removeLeftContent(2)
} }
if(!lastItem){ if(!lastItem){
console.log('[fillEachBlock] removeRightContent') //console.log('[fillEachBlock] removeRightContent')
currentTemplateBlock.endBranch.removeRightContent(2) currentTemplateBlock.endBranch.removeRightContent(2)
} }
console.log('[fillEachBlock] docEl i before removeMarkers', i, docEl.textContent) //console.log('[fillEachBlock] docEl i before removeMarkers', i, docEl.textContent)
currentTemplateBlock.removeMarkersAndEmptyAncestors() currentTemplateBlock.removeMarkersAndEmptyAncestors()
console.log('[fillEachBlock] docEl i after removeMarkers', i, docEl.textContent) //console.log('[fillEachBlock] docEl i after removeMarkers', i, docEl.textContent)
console.log('\nrecursive call to fillBlockContentTemplate') //console.log('\nrecursive call to fillBlockContentTemplate')
currentTemplateBlock.fillBlockContentTemplate(insideCompartment) currentTemplateBlock.fillBlockContentTemplate(insideCompartment)
//console.log('[fillEachBlock] docEl i after remove contents', i, docEl.textContent)
console.log('[fillEachBlock] docEl i after remove contents', i, docEl.textContent)
}) })
} }

View File

@ -26,9 +26,7 @@ n est un grand nombre
const odtResult3TextContent = await getOdtTextContent(odtResult3) const odtResult3TextContent = await getOdtTextContent(odtResult3)
t.deepEqual(odtResult3TextContent, `Description du nombre 3 t.deepEqual(odtResult3TextContent, `Description du nombre 3
n est un petit nombre n est un petit nombre
`) `)
@ -56,9 +54,7 @@ n est un grand nombre
const odtResult8TextContent = await getOdtTextContent(odtResult8) const odtResult8TextContent = await getOdtTextContent(odtResult8)
t.deepEqual(odtResult8TextContent, `Description du nombre 8 t.deepEqual(odtResult8TextContent, `Description du nombre 8
n est un grand nombre n est un grand nombre
`) `)
} }
catch(e){console.error(e); throw e} catch(e){console.error(e); throw e}