Split text nodes so they contain at most one structuring block (#3)

* rename test folders

* failing test

* First promising result of splitting textNodes to enable {#each}{/each} block within a single text node

* tests passing

* cleanup
This commit is contained in:
David Bruant 2025-04-26 19:59:44 +02:00 committed by GitHub
parent 28559585ba
commit 3da7f29cb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 113 additions and 26 deletions

View File

@ -217,6 +217,7 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, d
} }
/** /**
* *
* @param {Element | DocumentFragment} rootElement * @param {Element | DocumentFragment} rootElement
@ -227,6 +228,70 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, d
function fillTemplatedOdtElement(rootElement, data, Node){ function fillTemplatedOdtElement(rootElement, data, Node){
//console.log('fillTemplatedOdtElement', rootElement.nodeType, rootElement.nodeName) //console.log('fillTemplatedOdtElement', rootElement.nodeType, rootElement.nodeName)
// Perform a first traverse to split textnodes when they contain several block markers
traverse(rootElement, currentNode => {
if(currentNode.nodeType === Node.TEXT_NODE){
// trouver tous les débuts et fin de each et découper le textNode
let remainingText = currentNode.textContent || ''
while(remainingText.length >= 1){
let match;
// looking for opening {#each ...} block
const eachBlockOpeningRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/;
const eachBlockClosingRegex = /{\/each}/;
for(const regexp of [eachBlockOpeningRegex, eachBlockClosingRegex]){
let thisMatch = remainingText.match(regexp)
// trying to find only the first match in remainingText string
if(thisMatch && (!match || match.index > thisMatch.index)){
match = thisMatch
}
}
if(match){
// split 3-way : before-match, match and after-match
if(match[0].length < remainingText.length){
let afterMatchTextNode = currentNode.splitText(match.index + match[0].length)
if(afterMatchTextNode.textContent && afterMatchTextNode.textContent.length >= 1){
remainingText = afterMatchTextNode.textContent
}
else{
remainingText = ''
}
// per spec, currentNode now contains before-match and match text
if(match.index > 0){
currentNode.splitText(match.index)
}
if(afterMatchTextNode){
currentNode = afterMatchTextNode
}
}
else{
remainingText = ''
}
}
else{
remainingText = ''
}
}
}
else{
// skip
}
})
// now, each Node contains at most one block marker
/** @type {Node | undefined} */ /** @type {Node | undefined} */
let eachBlockStartNode let eachBlockStartNode
/** @type {Node | undefined} */ /** @type {Node | undefined} */
@ -246,17 +311,15 @@ function fillTemplatedOdtElement(rootElement, data, Node){
const text = currentNode.textContent || '' const text = currentNode.textContent || ''
// looking for {#each x as y} // looking for {#each x as y}
const eachStartRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/g; const eachStartRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/;
const startMatches = [...text.matchAll(eachStartRegex)]; const startMatch = text.match(eachStartRegex);
if(startMatches && startMatches.length >= 1){ if(startMatch){
if(insideAnEachBlock){ if(insideAnEachBlock){
nestedEach = nestedEach + 1 nestedEach = nestedEach + 1
} }
else{ else{
// PPP for now, consider only the first set of matches let [_, _iterableExpression, _itemExpression] = startMatch
// eventually, consider all of them for in-text-node {#each}...{/each}
let [_, _iterableExpression, _itemExpression] = startMatches[0]
iterableExpression = _iterableExpression iterableExpression = _iterableExpression
itemExpression = _itemExpression itemExpression = _itemExpression
@ -265,10 +328,10 @@ function fillTemplatedOdtElement(rootElement, data, Node){
} }
// trying to find an {/each} // trying to find an {/each}
const eachEndRegex = /{\/each}/g const eachEndRegex = /{\/each}/
const endMatches = [...text.matchAll(eachEndRegex)]; const endMatch = text.match(eachEndRegex)
if(endMatches && endMatches.length >= 1){ if(endMatch){
if(!eachBlockStartNode) if(!eachBlockStartNode)
throw new TypeError(`{/each} found without corresponding opening {#each x as y}`) throw new TypeError(`{/each} found without corresponding opening {#each x as y}`)

View File

@ -4,7 +4,7 @@ import test from 'ava';
import {getODSTableRawContent} from '../exports.js' import {getODSTableRawContent} from '../exports.js'
const nomAgeContent = (await readFile('./tests/data/nom-age.ods')).buffer const nomAgeContent = (await readFile('./tests/fixtures/nom-age.ods')).buffer
test('basic', async t => { test('basic', async t => {
const table = await getODSTableRawContent(nomAgeContent); const table = await getODSTableRawContent(nomAgeContent);

View File

@ -4,11 +4,11 @@ import {join} from 'node:path';
import {getOdtTemplate} from '../scripts/odf/odtTemplate-forNode.js' import {getOdtTemplate} from '../scripts/odf/odtTemplate-forNode.js'
import {fillOdtTemplate, getOdtTextContent} from '../exports.js' import {fillOdtTemplate, getOdtTextContent} from '../exports.js'
import { listZipEntries } from './_helpers/zip-analysis.js'; import { listZipEntries } from './helpers/zip-analysis.js';
test('basic template filling with variable substitution', async t => { test('basic template filling with variable substitution', async t => {
const templatePath = join(import.meta.dirname, './data/template-anniversaire.odt') const templatePath = join(import.meta.dirname, './fixtures/template-anniversaire.odt')
const templateContent = `Yo {nom} ! const templateContent = `Yo {nom} !
Tu es .e le {dateNaissance} Tu es .e le {dateNaissance}
@ -36,9 +36,8 @@ Bonjoir ☀️
}); });
test('basic template filling with {#each}', async t => { test('basic template filling with {#each}', async t => {
const templatePath = join(import.meta.dirname, './data/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 🧺
{#each listeCourses as élément} {#each listeCourses as élément}
@ -73,8 +72,9 @@ Pâtes à lasagne (fraîches !)
}); });
test('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, './data/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 🧺
{#each listeCourses as élément} {#each listeCourses as élément}
@ -103,9 +103,8 @@ test('Filling with {#each} and non-iterable value results in no error and empty
}); });
test('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, './data/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 🧺
- {#each listeCourses as élément} - {#each listeCourses as élément}
@ -142,7 +141,7 @@ test('template filling with {#each} generating a list', async t => {
test('template filling with 2 sequential {#each}', async t => { test('template filling with 2 sequential {#each}', async t => {
const templatePath = join(import.meta.dirname, './data/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
Fruits Fruits
@ -193,9 +192,8 @@ Poivron 🫑
}); });
test('template filling with nested {#each}s', async t => { test('template filling with nested {#each}s', async t => {
const templatePath = join(import.meta.dirname, './data/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
{#each légumesSaison as saisonLégumes} {#each légumesSaison as saisonLégumes}
@ -279,9 +277,36 @@ Hiver
}); });
test('template filling {#each ...}{/each} within a single text node', async t => {
const templatePath = join(import.meta.dirname, './fixtures/liste-nombres.odt')
const templateContent = `Liste de nombres
Les nombres : {#each nombres as n}{n} {/each} !!
`
const data = {
nombres : [1,1,2,3,5,8,13,21]
}
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, `Liste de nombres
Les nombres : 1 1 2 3 5 8 13 21  !!
`)
});
test('template filling of a table', async t => { test('template filling of a table', async t => {
const templatePath = join(import.meta.dirname, './data/tableau-simple.odt') const templatePath = join(import.meta.dirname, './fixtures/tableau-simple.odt')
const templateContent = `Évolution énergie en kWh par personne en France const templateContent = `Évolution énergie en kWh par personne en France
Année Année
@ -341,9 +366,8 @@ Année
}); });
test('template filling preserves images', async t => { test('template filling preserves images', async t => {
const templatePath = join(import.meta.dirname, './data/template-avec-image.odt') const templatePath = join(import.meta.dirname, './fixtures/template-avec-image.odt')
const data = { const data = {
commentaire : `J'adooooooore 🤩 West covinaaaaaaaaaaa 🎶` commentaire : `J'adooooooore 🤩 West covinaaaaaaaaaaa 🎶`

BIN
tests/fixtures/liste-nombres.odt vendored Normal file

Binary file not shown.

View File

@ -5,7 +5,7 @@ import test from 'ava';
import {getODSTableRawContent} from '../exports.js' import {getODSTableRawContent} from '../exports.js'
test('.ods file with table:number-columns-repeated attribute in cell', async t => { test('.ods file with table:number-columns-repeated attribute in cell', async t => {
const repeatedCellFileContent = (await readFile('./tests/data/cellules-répétées.ods')).buffer const repeatedCellFileContent = (await readFile('./tests/fixtures/cellules-répétées.ods')).buffer
const table = await getODSTableRawContent(repeatedCellFileContent); const table = await getODSTableRawContent(repeatedCellFileContent);
@ -17,7 +17,7 @@ test('.ods file with table:number-columns-repeated attribute in cell', async t =
test('.ods cells with dates should be recognized', async t => { test('.ods cells with dates should be recognized', async t => {
const odsFileWithDates = (await readFile('./tests/data/cellules avec dates.ods')).buffer const odsFileWithDates = (await readFile('./tests/fixtures/cellules avec dates.ods')).buffer
const table = await getODSTableRawContent(odsFileWithDates); const table = await getODSTableRawContent(odsFileWithDates);
const feuille1 = table.get('Feuille1') const feuille1 = table.get('Feuille1')
@ -39,7 +39,7 @@ test('.ods cells with dates should be recognized', async t => {
test('.ods file with new lines in content is ', async t => { test('.ods file with new lines in content is ', async t => {
const repeatedCellFileContent = (await readFile('./tests/data/cellule avec sauts.ods')).buffer const repeatedCellFileContent = (await readFile('./tests/fixtures/cellule avec sauts.ods')).buffer
const table = await getODSTableRawContent(repeatedCellFileContent); const table = await getODSTableRawContent(repeatedCellFileContent);