Compare commits
3 Commits
main
...
bug-audrey
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67e245fe5a | ||
|
|
820823ff49 | ||
|
|
9332c14323 |
@ -14,7 +14,7 @@ class TemplateDOMBranch{
|
|||||||
/** @type {Node} */
|
/** @type {Node} */
|
||||||
#leafNode
|
#leafNode
|
||||||
|
|
||||||
// ancestors with this.#ancestors[0] === this.#startNode and this.#ancestors.at(-1) === this.#leafNode
|
// ancestors with this.#ancestors[0] === this.#branchBaseNode and this.#ancestors.at(-1) === this.#leafNode
|
||||||
/** @type {Node[]} */
|
/** @type {Node[]} */
|
||||||
#ancestors
|
#ancestors
|
||||||
|
|
||||||
@ -235,9 +235,12 @@ class TemplateBlock{
|
|||||||
}
|
}
|
||||||
//console.log('[fillBlockContentTemplate] after startChild')
|
//console.log('[fillBlockContentTemplate] after startChild')
|
||||||
|
|
||||||
for(const content of this.#middleContent){
|
|
||||||
fillOdtElementTemplate(content, compartement)
|
// if content consists of several parts of an {#each}{/each}
|
||||||
}
|
// when arriving to the {/each}, it will be alone (and imbalanced)
|
||||||
|
// and will trigger an error
|
||||||
|
fillOdtElementTemplate(Array.from(this.#middleContent), compartement)
|
||||||
|
|
||||||
//console.log('[fillBlockContentTemplate] after middleContent')
|
//console.log('[fillBlockContentTemplate] after middleContent')
|
||||||
|
|
||||||
const endChild = this.endBranch.at(1)
|
const endChild = this.endBranch.at(1)
|
||||||
@ -554,16 +557,24 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
|
|||||||
const IF = ifStartMarkerRegex.source
|
const IF = ifStartMarkerRegex.source
|
||||||
const EACH = eachStartMarkerRegex.source
|
const EACH = eachStartMarkerRegex.source
|
||||||
|
|
||||||
|
/** @typedef {Element | DocumentFragment | Document} RootElementArgument */
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Element | DocumentFragment | Document} rootElement
|
* @param {RootElementArgument | RootElementArgument[]} rootElements
|
||||||
* @param {Compartment} compartment
|
* @param {Compartment} compartment
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
export default function fillOdtElementTemplate(rootElement, compartment) {
|
export default function fillOdtElementTemplate(rootElements, compartment) {
|
||||||
//console.log('[fillTemplatedOdtElement]', rootElement.nodeType, rootElement.nodeName, rootElement.textContent)
|
|
||||||
//console.log('[fillTemplatedOdtElement]', rootElement.documentElement && rootElement.documentElement.textContent)
|
|
||||||
|
|
||||||
|
if(!Array.isArray(rootElements)){
|
||||||
|
rootElements = [rootElements]
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log('[fillTemplatedOdtElement]', rootElements.length, rootElements[0].nodeType, rootElements[0].nodeName, rootElements[0].textContent)
|
||||||
|
//console.log('[fillTemplatedOdtElement]', rootElement.documentElement && rootElement.documentElement.textContent)
|
||||||
|
|
||||||
let currentlyOpenBlocks = []
|
let currentlyOpenBlocks = []
|
||||||
|
|
||||||
/** @type {Node | undefined} */
|
/** @type {Node | undefined} */
|
||||||
@ -585,187 +596,197 @@ export default function fillOdtElementTemplate(rootElement, compartment) {
|
|||||||
// Traverse "in document order"
|
// Traverse "in document order"
|
||||||
|
|
||||||
|
|
||||||
// @ts-ignore
|
for(const rootElement of rootElements){
|
||||||
traverse(rootElement, currentNode => {
|
|
||||||
//console.log('currentlyOpenBlocks', currentlyOpenBlocks)
|
|
||||||
|
|
||||||
const insideAnOpenBlock = currentlyOpenBlocks.length >= 1
|
|
||||||
|
|
||||||
if(currentNode.nodeType === Node.TEXT_NODE) {
|
// @ts-ignore
|
||||||
const text = currentNode.textContent || ''
|
traverse(rootElement, currentNode => {
|
||||||
|
//console.log('currentlyOpenBlocks', currentlyOpenBlocks)
|
||||||
|
|
||||||
/**
|
const insideAnOpenBlock = currentlyOpenBlocks.length >= 1
|
||||||
* looking for {#each x as y}
|
|
||||||
*/
|
|
||||||
const eachStartMatch = text.match(eachStartMarkerRegex);
|
|
||||||
|
|
||||||
if(eachStartMatch) {
|
if(currentNode.nodeType === Node.TEXT_NODE) {
|
||||||
//console.log('startMatch', startMatch)
|
const text = currentNode.textContent || ''
|
||||||
|
|
||||||
currentlyOpenBlocks.push(EACH)
|
/**
|
||||||
|
* looking for {#each x as y}
|
||||||
|
*/
|
||||||
|
const eachStartMatch = text.match(eachStartMarkerRegex);
|
||||||
|
|
||||||
if(insideAnOpenBlock) {
|
if(eachStartMatch) {
|
||||||
// do nothing
|
//console.log('startMatch', startMatch)
|
||||||
}
|
|
||||||
else {
|
|
||||||
let [_, _iterableExpression, _itemExpression] = eachStartMatch
|
|
||||||
|
|
||||||
eachBlockIterableExpression = _iterableExpression
|
currentlyOpenBlocks.push(EACH)
|
||||||
eachBlockItemExpression = _itemExpression
|
|
||||||
eachOpeningMarkerNode = currentNode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if(insideAnOpenBlock) {
|
||||||
/**
|
// do nothing
|
||||||
* Looking for {/each}
|
}
|
||||||
*/
|
else {
|
||||||
const isEachClosingBlock = text.includes(eachClosingMarker)
|
let [_, _iterableExpression, _itemExpression] = eachStartMatch
|
||||||
|
|
||||||
if(isEachClosingBlock) {
|
eachBlockIterableExpression = _iterableExpression
|
||||||
|
eachBlockItemExpression = _itemExpression
|
||||||
//console.log('isEachClosingBlock', isEachClosingBlock)
|
eachOpeningMarkerNode = currentNode
|
||||||
|
|
||||||
if(!eachOpeningMarkerNode)
|
|
||||||
throw new Error(`{/each} found without corresponding opening {#each x as y}`)
|
|
||||||
|
|
||||||
if(currentlyOpenBlocks.at(-1) !== EACH)
|
|
||||||
throw new Error(`{/each} found while the last opened block was not an opening {#each x as y}`)
|
|
||||||
|
|
||||||
if(currentlyOpenBlocks.length === 1) {
|
|
||||||
eachClosingMarkerNode = currentNode
|
|
||||||
|
|
||||||
// found an {#each} and its corresponding {/each}
|
|
||||||
// execute replacement loop
|
|
||||||
fillEachBlock(eachOpeningMarkerNode, eachBlockIterableExpression, eachBlockItemExpression, eachClosingMarkerNode, compartment)
|
|
||||||
|
|
||||||
eachOpeningMarkerNode = undefined
|
|
||||||
eachBlockIterableExpression = undefined
|
|
||||||
eachBlockItemExpression = undefined
|
|
||||||
eachClosingMarkerNode = undefined
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// ignore because it will be treated as part of the outer {#each}
|
|
||||||
}
|
|
||||||
|
|
||||||
currentlyOpenBlocks.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looking for {#if ...}
|
|
||||||
*/
|
|
||||||
const ifStartMatch = text.match(ifStartMarkerRegex);
|
|
||||||
|
|
||||||
if(ifStartMatch) {
|
|
||||||
currentlyOpenBlocks.push(IF)
|
|
||||||
|
|
||||||
if(insideAnOpenBlock) {
|
|
||||||
// do nothing because the marker is too deep
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
let [_, _ifBlockConditionExpression] = ifStartMatch
|
|
||||||
|
|
||||||
ifBlockConditionExpression = _ifBlockConditionExpression
|
|
||||||
ifOpeningMarkerNode = currentNode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looking for {:else}
|
|
||||||
*/
|
|
||||||
const hasElseMarker = text.includes(elseMarker);
|
|
||||||
|
|
||||||
if(hasElseMarker) {
|
|
||||||
if(!insideAnOpenBlock)
|
|
||||||
throw new Error('{:else} without a corresponding {#if}')
|
|
||||||
|
|
||||||
if(currentlyOpenBlocks.length === 1) {
|
|
||||||
if(currentlyOpenBlocks[0] === IF) {
|
|
||||||
ifElseMarkerNode = currentNode
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
throw new Error('{:else} inside an {#each} but without a corresponding {#if}')
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// do nothing because the marker is too deep
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Looking for {/if}
|
* Looking for {/each}
|
||||||
*/
|
*/
|
||||||
const ifClosingMarker = text.includes(closingIfMarker);
|
const isEachClosingBlock = text.includes(eachClosingMarker)
|
||||||
|
|
||||||
if(ifClosingMarker) {
|
if(isEachClosingBlock) {
|
||||||
if(!insideAnOpenBlock)
|
|
||||||
throw new Error('{/if} without a corresponding {#if}')
|
|
||||||
|
|
||||||
if(currentlyOpenBlocks.length === 1) {
|
//console.log('isEachClosingBlock', isEachClosingBlock)
|
||||||
if(currentlyOpenBlocks[0] === IF) {
|
|
||||||
ifClosingMarkerNode = currentNode
|
|
||||||
|
|
||||||
// found an {#if} and its corresponding {/if}
|
if(!eachOpeningMarkerNode){
|
||||||
|
throw new Error(`{/each} found without corresponding opening {#each x as y}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if(currentlyOpenBlocks.at(-1) !== EACH)
|
||||||
|
throw new Error(`{/each} found while the last opened block was not an opening {#each x as y}`)
|
||||||
|
|
||||||
|
if(currentlyOpenBlocks.length === 1) {
|
||||||
|
eachClosingMarkerNode = currentNode
|
||||||
|
|
||||||
|
// found an {#each} and its corresponding {/each}
|
||||||
// execute replacement loop
|
// execute replacement loop
|
||||||
fillIfBlock(ifOpeningMarkerNode, ifElseMarkerNode, ifClosingMarkerNode, ifBlockConditionExpression, compartment)
|
//console.log('start of fillEachBlock')
|
||||||
|
|
||||||
ifOpeningMarkerNode = undefined
|
fillEachBlock(eachOpeningMarkerNode, eachBlockIterableExpression, eachBlockItemExpression, eachClosingMarkerNode, compartment)
|
||||||
ifElseMarkerNode = undefined
|
|
||||||
ifClosingMarkerNode = undefined
|
//console.log('end of fillEachBlock')
|
||||||
ifBlockConditionExpression = undefined
|
|
||||||
|
eachOpeningMarkerNode = undefined
|
||||||
|
eachBlockIterableExpression = undefined
|
||||||
|
eachBlockItemExpression = undefined
|
||||||
|
eachClosingMarkerNode = undefined
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// ignore because it will be treated as part of the outer {#each}
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log('popping currentlyOpenBlocks')
|
||||||
|
currentlyOpenBlocks.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looking for {#if ...}
|
||||||
|
*/
|
||||||
|
const ifStartMatch = text.match(ifStartMarkerRegex);
|
||||||
|
|
||||||
|
if(ifStartMatch) {
|
||||||
|
currentlyOpenBlocks.push(IF)
|
||||||
|
|
||||||
|
if(insideAnOpenBlock) {
|
||||||
|
// do nothing because the marker is too deep
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let [_, _ifBlockConditionExpression] = ifStartMatch
|
||||||
|
|
||||||
|
ifBlockConditionExpression = _ifBlockConditionExpression
|
||||||
|
ifOpeningMarkerNode = currentNode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looking for {:else}
|
||||||
|
*/
|
||||||
|
const hasElseMarker = text.includes(elseMarker);
|
||||||
|
|
||||||
|
if(hasElseMarker) {
|
||||||
|
if(!insideAnOpenBlock)
|
||||||
|
throw new Error('{:else} without a corresponding {#if}')
|
||||||
|
|
||||||
|
if(currentlyOpenBlocks.length === 1) {
|
||||||
|
if(currentlyOpenBlocks[0] === IF) {
|
||||||
|
ifElseMarkerNode = currentNode
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw new Error('{:else} inside an {#each} but without a corresponding {#if}')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// do nothing because the marker is too deep
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looking for {/if}
|
||||||
|
*/
|
||||||
|
const ifClosingMarker = text.includes(closingIfMarker);
|
||||||
|
|
||||||
|
if(ifClosingMarker) {
|
||||||
|
if(!insideAnOpenBlock)
|
||||||
|
throw new Error('{/if} without a corresponding {#if}')
|
||||||
|
|
||||||
|
if(currentlyOpenBlocks.length === 1) {
|
||||||
|
if(currentlyOpenBlocks[0] === IF) {
|
||||||
|
ifClosingMarkerNode = currentNode
|
||||||
|
|
||||||
|
// found an {#if} and its corresponding {/if}
|
||||||
|
// execute replacement loop
|
||||||
|
fillIfBlock(ifOpeningMarkerNode, ifElseMarkerNode, ifClosingMarkerNode, ifBlockConditionExpression, compartment)
|
||||||
|
|
||||||
|
ifOpeningMarkerNode = undefined
|
||||||
|
ifElseMarkerNode = undefined
|
||||||
|
ifClosingMarkerNode = undefined
|
||||||
|
ifBlockConditionExpression = undefined
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw new Error('{/if} inside an {#each} but without a corresponding {#if}')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// do nothing because the marker is too deep
|
||||||
|
}
|
||||||
|
|
||||||
|
currentlyOpenBlocks.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looking for variables for substitutions
|
||||||
|
*/
|
||||||
|
if(!insideAnOpenBlock) {
|
||||||
|
// @ts-ignore
|
||||||
|
if(currentNode.data) {
|
||||||
|
// @ts-ignore
|
||||||
|
const placesToFill = findPlacesToFillInString(currentNode.data, compartment)
|
||||||
|
|
||||||
|
if(placesToFill) {
|
||||||
|
const newText = placesToFill.fill()
|
||||||
|
// @ts-ignore
|
||||||
|
const newTextNode = currentNode.ownerDocument?.createTextNode(newText)
|
||||||
|
// @ts-ignore
|
||||||
|
currentNode.parentNode?.replaceChild(newTextNode, currentNode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
throw new Error('{/if} inside an {#each} but without a corresponding {#if}')
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// do nothing because the marker is too deep
|
// ignore because it will be treated as part of the outer {#each} block
|
||||||
}
|
}
|
||||||
|
|
||||||
currentlyOpenBlocks.pop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(currentNode.nodeType === Node.ATTRIBUTE_NODE) {
|
||||||
/**
|
// Looking for variables for substitutions
|
||||||
* Looking for variables for substitutions
|
if(!insideAnOpenBlock) {
|
||||||
*/
|
|
||||||
if(!insideAnOpenBlock) {
|
|
||||||
// @ts-ignore
|
|
||||||
if(currentNode.data) {
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const placesToFill = findPlacesToFillInString(currentNode.data, compartment)
|
if(currentNode.value) {
|
||||||
|
|
||||||
if(placesToFill) {
|
|
||||||
const newText = placesToFill.fill()
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const newTextNode = currentNode.ownerDocument?.createTextNode(newText)
|
const placesToFill = findPlacesToFillInString(currentNode.value, compartment)
|
||||||
// @ts-ignore
|
if(placesToFill) {
|
||||||
currentNode.parentNode?.replaceChild(newTextNode, currentNode)
|
// @ts-ignore
|
||||||
|
currentNode.value = placesToFill.fill()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
else {
|
// ignore because it will be treated as part of the {#each} block
|
||||||
// ignore because it will be treated as part of the outer {#each} block
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(currentNode.nodeType === Node.ATTRIBUTE_NODE) {
|
|
||||||
// Looking for variables for substitutions
|
|
||||||
if(!insideAnOpenBlock) {
|
|
||||||
// @ts-ignore
|
|
||||||
if(currentNode.value) {
|
|
||||||
// @ts-ignore
|
|
||||||
const placesToFill = findPlacesToFillInString(currentNode.value, compartment)
|
|
||||||
if(placesToFill) {
|
|
||||||
// @ts-ignore
|
|
||||||
currentNode.value = placesToFill.fill()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
})
|
||||||
// ignore because it will be treated as part of the {#each} block
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -344,3 +344,32 @@ Année
|
|||||||
`.trim())
|
`.trim())
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('nested each without common ancestor for inner each', async t => {
|
||||||
|
const templatePath = join(import.meta.dirname, '../fixtures/nested-each-without-common-ancestor-for-inner-each.odt')
|
||||||
|
const templateContent = `{#each liste_espèces_par_impact as élément}
|
||||||
|
{#each élément.liste_espèces as espèce}
|
||||||
|
{/each}
|
||||||
|
{/each}
|
||||||
|
`
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
liste_espèces_par_impact: [
|
||||||
|
{}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
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, ``)
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
BIN
tests/fixtures/nested-each-without-common-ancestor-for-inner-each.odt
vendored
Normal file
BIN
tests/fixtures/nested-each-without-common-ancestor-for-inner-each.odt
vendored
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user