Formatted markers (#5)

* Adding failing test case

* test runs and fails

* passing formatting test

* passing test

* refactoring moving different parts to their own files

* Refactoring - doc in prepareTemplateDOMTree

* Test of 2 formatted markers within same Text node passes

* passing test with {#each ...} and text before partially formatted

* Test with {/each} and text after partially formatted passing

* woops with proper test case

* test with partially formatted variable passes
This commit is contained in:
David Bruant 2025-05-08 17:13:51 +02:00 committed by GitHub
parent 428d230666
commit 0bbc57afb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 812 additions and 346 deletions

View File

@ -1,6 +1,6 @@
//@ts-check //@ts-check
export {default as fillOdtTemplate} from './scripts/odf/fillOdtTemplate.js' export {default as fillOdtTemplate} from './scripts/odf/templating/fillOdtTemplate.js'
export {getOdtTextContent} from './scripts/odf/odt/getOdtTextContent.js' export {getOdtTextContent} from './scripts/odf/odt/getOdtTextContent.js'
export { createOdsFile } from './scripts/createOdsFile.js' export { createOdsFile } from './scripts/createOdsFile.js'

View File

@ -24,7 +24,7 @@ export function serializeToString(node){
/** /**
* Traverses a DOM tree starting from the given element and applies the visit function * Traverses a DOM tree starting from the given node and applies the visit function
* to each Element node encountered in tree order (depth-first). * to each Element node encountered in tree order (depth-first).
* *
* This should probably be replace by the TreeWalker API when implemented by xmldom * This should probably be replace by the TreeWalker API when implemented by xmldom

View File

@ -1,7 +1,7 @@
import { ZipReader, Uint8ArrayReader, TextWriter } from '@zip.js/zip.js'; import { ZipReader, Uint8ArrayReader, TextWriter } from '@zip.js/zip.js';
import {parseXML, Node} from '../../DOMUtils.js' import {parseXML, Node} from '../../DOMUtils.js'
/** @import {ODTFile} from '../fillOdtTemplate.js' */ /** @import {ODTFile} from '../templating/fillOdtTemplate.js' */
/** /**
* @param {ODTFile} odtFile * @param {ODTFile} odtFile

View File

@ -1,24 +1,5 @@
import { ZipReader, ZipWriter, BlobReader, BlobWriter, TextReader, Uint8ArrayReader, TextWriter, Uint8ArrayWriter } from '@zip.js/zip.js'; import {traverse, Node} from '../../DOMUtils.js'
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js'
import {traverse, parseXML, serializeToString, Node} from '../DOMUtils.js'
import {makeManifestFile, getManifestFileData} from './manifest.js';
import 'ses'
lockdown();
/** @import {Reader, ZipWriterAddDataOptions} from '@zip.js/zip.js' */
/** @import {ODFManifest} from './manifest.js' */
/** @typedef {ArrayBuffer} ODTFile */
const ODTMimetype = 'application/vnd.oasis.opendocument.text'
// For a given string, split it into fixed parts and parts to replace
/** /**
* @typedef TextPlaceToFill * @typedef TextPlaceToFill
@ -27,14 +8,14 @@ const ODTMimetype = 'application/vnd.oasis.opendocument.text'
*/ */
/** /**
* @param {string} str * @param {string} str
* @param {Compartment} compartment * @param {Compartment} compartment
* @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 = []
@ -220,7 +201,7 @@ function fillIfBlock(ifOpeningMarkerNode, ifElseMarkerNode, ifClosingMarkerNode,
if(chosenFragment) { if(chosenFragment) {
fillTemplatedOdtElement( fillOdtElementTemplate(
chosenFragment, chosenFragment,
compartment compartment
) )
@ -276,7 +257,7 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
}) })
// recursive call to fillTemplatedOdtElement on itemFragment // recursive call to fillTemplatedOdtElement on itemFragment
fillTemplatedOdtElement( fillOdtElementTemplate(
itemFragment, itemFragment,
insideCompartment insideCompartment
) )
@ -293,23 +274,13 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, c
const IF = 'IF' const IF = 'IF'
const EACH = 'EACH' const EACH = 'EACH'
// the regexps below are shared, so they shoudn't have state (no 'g' flag)
const ifStartRegex = /{#if\s+([^}]+?)\s*}/;
const elseMarker = '{:else}'
const closingIfMarker = '{/if}'
const eachStartMarkerRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/;
const eachClosingBlockString = '{/each}'
/** /**
* *
* @param {Element | DocumentFragment | Document} rootElement * @param {Element | DocumentFragment | Document} rootElement
* @param {Compartment} compartment * @param {Compartment} compartment
* @returns {void} * @returns {void}
*/ */
function fillTemplatedOdtElement(rootElement, compartment){ export default function fillOdtElementTemplate(rootElement, compartment) {
//console.log('fillTemplatedOdtElement', rootElement.nodeType, rootElement.nodeName) //console.log('fillTemplatedOdtElement', rootElement.nodeType, rootElement.nodeName)
let currentlyOpenBlocks = [] let currentlyOpenBlocks = []
@ -366,7 +337,7 @@ function fillTemplatedOdtElement(rootElement, compartment){
/** /**
* Looking for {/each} * Looking for {/each}
*/ */
const isEachClosingBlock = text.includes(eachClosingBlockString) const isEachClosingBlock = text.includes(eachClosingMarker)
if(isEachClosingBlock) { if(isEachClosingBlock) {
@ -401,7 +372,7 @@ function fillTemplatedOdtElement(rootElement, compartment){
/** /**
* Looking for {#if ...} * Looking for {#if ...}
*/ */
const ifStartMatch = text.match(ifStartRegex); const ifStartMatch = text.match(ifStartMarkerRegex);
if(ifStartMatch) { if(ifStartMatch) {
currentlyOpenBlocks.push(IF) currentlyOpenBlocks.push(IF)
@ -513,237 +484,3 @@ function fillTemplatedOdtElement(rootElement, compartment){
} }
}) })
} }
/**
*
* @param {Document} document
* @param {Compartment} compartment
* @returns {void}
*/
function fillTemplatedOdtDocument(document, compartment){
// prepare tree to be used as template
// Perform a first traverse to split textnodes when they contain several block markers
traverse(document, 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 matchText;
let matchIndex;
// looking for a block marker
for(const marker of [ifStartRegex, elseMarker, closingIfMarker, eachStartMarkerRegex, eachClosingBlockString]){
if(typeof marker === 'string'){
const index = remainingText.indexOf(marker)
if(index !== -1){
matchText = marker
matchIndex = index
// found the first match
break; // get out of loop
}
}
else{
// marker is a RegExp
const match = remainingText.match(marker)
if(match){
matchText = match[0]
matchIndex = match.index
// found the first match
break; // get out of loop
}
}
}
if(matchText){
// split 3-way : before-match, match and after-match
if(matchText.length < remainingText.length){
// @ts-ignore
let afterMatchTextNode = currentNode.splitText(matchIndex + matchText.length)
if(afterMatchTextNode.textContent && afterMatchTextNode.textContent.length >= 1){
remainingText = afterMatchTextNode.textContent
}
else{
remainingText = ''
}
// per spec, currentNode now contains before-match and match text
// @ts-ignore
if(matchIndex > 0){
// @ts-ignore
currentNode.splitText(matchIndex)
}
if(afterMatchTextNode){
currentNode = afterMatchTextNode
}
}
else{
remainingText = ''
}
}
else{
remainingText = ''
}
}
}
else{
// skip
}
})
// now, each Node contains at most one block marker
fillTemplatedOdtElement(document, compartment)
}
const keptFiles = new Set(['content.xml', 'styles.xml', 'mimetype', 'META-INF/manifest.xml'])
/**
*
* @param {string} filename
* @returns {boolean}
*/
function keepFile(filename){
return keptFiles.has(filename) || filename.startsWith('Pictures/')
}
/**
* @param {ODTFile} odtTemplate
* @param {any} data
* @returns {Promise<ODTFile>}
*/
export default async function fillOdtTemplate(odtTemplate, data) {
const reader = new ZipReader(new Uint8ArrayReader(new Uint8Array(odtTemplate)));
// Lire toutes les entrées du fichier ODT
const entries = reader.getEntriesGenerator();
// Créer un ZipWriter pour le nouveau fichier ODT
const writer = new ZipWriter(new Uint8ArrayWriter());
/** @type {ODFManifest} */
let manifestFileData;
/** @type {{filename: string, content: Reader, options?: ZipWriterAddDataOptions}[]} */
const zipEntriesToAdd = []
// Parcourir chaque entrée du fichier ODT
for await (const entry of entries) {
const filename = entry.filename
//console.log('entry', filename, entry.directory)
// remove other files
if(!keepFile(filename)){
// ignore, do not create a corresponding entry in the new zip
}
else{
let content
let options
switch(filename){
case 'mimetype':
content = new TextReader(ODTMimetype)
options = {
level: 0,
compressionMethod: 0,
dataDescriptor: false,
extendedTimestamp: false,
}
zipEntriesToAdd.push({filename, content, options})
break;
case 'content.xml':
// @ts-ignore
const contentXml = await entry.getData(new TextWriter());
const contentDocument = parseXML(contentXml);
const compartment = new Compartment({
globals: data,
__options__: true
})
fillTemplatedOdtDocument(contentDocument, compartment)
const updatedContentXml = serializeToString(contentDocument)
content = new TextReader(updatedContentXml)
options = {
lastModDate: entry.lastModDate,
level: 9
};
zipEntriesToAdd.push({filename, content, options})
break;
case 'META-INF/manifest.xml':
// @ts-ignore
const manifestXml = await entry.getData(new TextWriter());
const manifestDocument = parseXML(manifestXml);
manifestFileData = getManifestFileData(manifestDocument)
break;
case 'styles.xml':
default:
const blobWriter = new BlobWriter();
// @ts-ignore
await entry.getData(blobWriter);
const blob = await blobWriter.getData();
content = new BlobReader(blob)
zipEntriesToAdd.push({filename, content})
break;
}
}
}
for(const {filename, content, options} of zipEntriesToAdd){
await writer.add(filename, content, options);
}
const newZipFilenames = new Set(zipEntriesToAdd.map(ze => ze.filename))
if(!manifestFileData){
throw new Error(`'META-INF/manifest.xml' zip entry missing`)
}
// remove ignored files from manifest.xml
for(const filename of manifestFileData.fileEntries.keys()){
if(!newZipFilenames.has(filename)){
manifestFileData.fileEntries.delete(filename)
}
}
const manifestFileXml = makeManifestFile(manifestFileData)
await writer.add('META-INF/manifest.xml', new TextReader(manifestFileXml));
await reader.close();
return writer.close();
}

View File

@ -0,0 +1,166 @@
import {ZipReader, ZipWriter, BlobReader, BlobWriter, TextReader, Uint8ArrayReader, TextWriter, Uint8ArrayWriter} from '@zip.js/zip.js';
import {parseXML, serializeToString} from '../../DOMUtils.js'
import {makeManifestFile, getManifestFileData} from '../manifest.js';
import prepareTemplateDOMTree from './prepareTemplateDOMTree.js';
import 'ses'
import fillOdtElementTemplate from './fillOdtElementTemplate.js';
lockdown();
/** @import {Reader, ZipWriterAddDataOptions} from '@zip.js/zip.js' */
/** @import {ODFManifest} from '../manifest.js' */
/** @typedef {ArrayBuffer} ODTFile */
const ODTMimetype = 'application/vnd.oasis.opendocument.text'
/**
*
* @param {Document} document
* @param {Compartment} compartment
* @returns {void}
*/
function fillOdtDocumentTemplate(document, compartment) {
prepareTemplateDOMTree(document)
fillOdtElementTemplate(document, compartment)
}
const keptFiles = new Set(['content.xml', 'styles.xml', 'mimetype', 'META-INF/manifest.xml'])
/**
*
* @param {string} filename
* @returns {boolean}
*/
function keepFile(filename) {
return keptFiles.has(filename) || filename.startsWith('Pictures/')
}
/**
* @param {ODTFile} odtTemplate
* @param {any} data
* @returns {Promise<ODTFile>}
*/
export default async function fillOdtTemplate(odtTemplate, data) {
const reader = new ZipReader(new Uint8ArrayReader(new Uint8Array(odtTemplate)));
// Lire toutes les entrées du fichier ODT
const entries = reader.getEntriesGenerator();
// Créer un ZipWriter pour le nouveau fichier ODT
const writer = new ZipWriter(new Uint8ArrayWriter());
/** @type {ODFManifest} */
let manifestFileData;
/** @type {{filename: string, content: Reader, options?: ZipWriterAddDataOptions}[]} */
const zipEntriesToAdd = []
// Parcourir chaque entrée du fichier ODT
for await(const entry of entries) {
const filename = entry.filename
//console.log('entry', filename, entry.directory)
// remove other files
if(!keepFile(filename)) {
// ignore, do not create a corresponding entry in the new zip
}
else {
let content
let options
switch(filename) {
case 'mimetype':
content = new TextReader(ODTMimetype)
options = {
level: 0,
compressionMethod: 0,
dataDescriptor: false,
extendedTimestamp: false,
}
zipEntriesToAdd.push({filename, content, options})
break;
case 'content.xml':
// @ts-ignore
const contentXml = await entry.getData(new TextWriter());
const contentDocument = parseXML(contentXml);
const compartment = new Compartment({
globals: data,
__options__: true
})
fillOdtDocumentTemplate(contentDocument, compartment)
const updatedContentXml = serializeToString(contentDocument)
content = new TextReader(updatedContentXml)
options = {
lastModDate: entry.lastModDate,
level: 9
};
zipEntriesToAdd.push({filename, content, options})
break;
case 'META-INF/manifest.xml':
// @ts-ignore
const manifestXml = await entry.getData(new TextWriter());
const manifestDocument = parseXML(manifestXml);
manifestFileData = getManifestFileData(manifestDocument)
break;
case 'styles.xml':
default:
const blobWriter = new BlobWriter();
// @ts-ignore
await entry.getData(blobWriter);
const blob = await blobWriter.getData();
content = new BlobReader(blob)
zipEntriesToAdd.push({filename, content})
break;
}
}
}
for(const {filename, content, options} of zipEntriesToAdd) {
await writer.add(filename, content, options);
}
const newZipFilenames = new Set(zipEntriesToAdd.map(ze => ze.filename))
if(!manifestFileData) {
throw new Error(`'META-INF/manifest.xml' zip entry missing`)
}
// remove ignored files from manifest.xml
for(const filename of manifestFileData.fileEntries.keys()) {
if(!newZipFilenames.has(filename)) {
manifestFileData.fileEntries.delete(filename)
}
}
const manifestFileXml = makeManifestFile(manifestFileData)
await writer.add('META-INF/manifest.xml', new TextReader(manifestFileXml));
await reader.close();
return writer.close();
}

View File

@ -0,0 +1,9 @@
// 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 elseMarker = '{:else}'
export const closingIfMarker = '{/if}'
export const eachStartMarkerRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/;
export const eachClosingMarker = '{/each}'

View File

@ -0,0 +1,413 @@
import {traverse, Node} from "../../DOMUtils.js";
import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js'
/**
*
* @param {string} text
* @param {string | RegExp} pattern
* @returns {{marker: string, index: number}[]}
*/
function findAllMatches(text, pattern) {
const results = [];
let match;
if(typeof pattern === 'string') {
// For string markers like elseMarker and closingIfMarker
let index = 0;
while((index = text.indexOf(pattern, index)) !== -1) {
results.push({
marker: pattern,
index: index
});
index += pattern.length;
}
} else {
// For regex patterns
pattern = new RegExp(pattern.source, 'g');
while((match = pattern.exec(text)) !== null) {
results.push({
marker: match[0],
index: match.index
});
}
}
return results;
}
/**
*
* @param {Node} node1
* @param {Node} node2
* @returns {Node | undefined}
*/
function findCommonAncestor(node1, node2) {
const ancestors1 = getAncestors(node1);
const ancestors2 = getAncestors(node2);
for(const ancestor of ancestors1) {
if(ancestors2.includes(ancestor)) {
return ancestor;
}
}
return undefined;
}
/**
*
* @param {Node} node
* @returns {Node[]}
*/
function getAncestors(node) {
const ancestors = [];
let current = node;
while(current) {
ancestors.push(current);
current = current.parentNode;
}
return ancestors;
}
/**
* text position of a node relative to a text nodes within a container
*
* @param {Text} node
* @param {Text[]} containerTextNodes
* @returns {number}
*/
function getNodeTextPosition(node, containerTextNodes) {
let position = 0;
for(const currentTextNode of containerTextNodes) {
if(currentTextNode === node) {
return position
}
else {
position += (currentTextNode.textContent || '').length;
}
}
throw new Error(`[${getNodeTextPosition.name}] None of containerTextNodes elements is equal to node`)
}
/** @typedef {Node[]} DOMPath */
/**
* remove nodes between startNode and endNode
* but keep startNode and endNode
*
* returns the common ancestor child in start branch
* for the purpose for inserting something between startNode and endNode
* with insertionPoint.parentNode.insertBefore(newBetweenContent, insertionPoint)
*
* @param {Node} startNode
* @param {Node} endNode
* @returns {Node}
*/
function removeNodesBetween(startNode, endNode) {
let nodesToRemove = new Set();
// find both ancestry branch
const startNodeAncestors = new Set(getAncestors(startNode))
const endNodeAncestors = new Set(getAncestors(endNode))
// find common ancestor
const commonAncestor = findCommonAncestor(startNode, endNode)
// remove everything "on the right" of start branch
let currentAncestor = startNode
let commonAncestorChildInEndNodeBranch
while(currentAncestor !== commonAncestor){
let siblingToRemove = currentAncestor.nextSibling
while(siblingToRemove && !endNodeAncestors.has(siblingToRemove)){
nodesToRemove.add(siblingToRemove)
siblingToRemove = siblingToRemove.nextSibling
}
if(endNodeAncestors.has(siblingToRemove)){
commonAncestorChildInEndNodeBranch = siblingToRemove
}
currentAncestor = currentAncestor.parentNode;
}
// remove everything "on the left" of end branch
currentAncestor = endNode
while(currentAncestor !== commonAncestor){
let siblingToRemove = currentAncestor.previousSibling
while(siblingToRemove && !startNodeAncestors.has(siblingToRemove)){
nodesToRemove.add(siblingToRemove)
siblingToRemove = siblingToRemove.previousSibling
}
currentAncestor = currentAncestor.parentNode;
}
for(const node of nodesToRemove){
node.parentNode.removeChild(node)
}
return commonAncestorChildInEndNodeBranch
}
/**
* Consolidate markers which are split among several Text nodes
*
* @param {Document} document
*/
function consolidateMarkers(document){
// Perform a first pass to detect templating markers with formatting to remove it
const potentialMarkersContainers = [
...Array.from(document.getElementsByTagName('text:p')),
...Array.from(document.getElementsByTagName('text:h'))
]
for(const potentialMarkersContainer of potentialMarkersContainers) {
const consolidatedMarkers = []
/** @type {Text[]} */
let containerTextNodesInTreeOrder = [];
function refreshContainerTextNodes(){
containerTextNodesInTreeOrder = []
traverse(potentialMarkersContainer, node => {
if(node.nodeType === Node.TEXT_NODE) {
containerTextNodesInTreeOrder.push(/** @type {Text} */(node))
}
})
}
refreshContainerTextNodes()
let fullText = ''
for(const node of containerTextNodesInTreeOrder){
fullText = fullText + node.textContent
}
// Check for each template marker
const positionedMarkers = [
...findAllMatches(fullText, ifStartMarkerRegex),
...findAllMatches(fullText, elseMarker),
...findAllMatches(fullText, closingIfMarker),
...findAllMatches(fullText, eachStartMarkerRegex),
...findAllMatches(fullText, eachClosingMarker),
...findAllMatches(fullText, variableRegex)
];
/*if(positionedMarkers.length >= 1)
console.log('positionedMarkers', positionedMarkers)*/
while(consolidatedMarkers.length < positionedMarkers.length) {
refreshContainerTextNodes()
// For each marker, check if it's contained within a single text node
for(const positionedMarker of positionedMarkers.slice(consolidatedMarkers.length)) {
//console.log('positionedMarker', positionedMarker)
let currentPos = 0;
let startNode;
let endNode;
// Find which text node(s) contain this marker
for(const textNode of containerTextNodesInTreeOrder) {
const nodeStart = currentPos;
const nodeEnd = nodeStart + textNode.textContent.length;
// If start of marker is in this node
if(!startNode && positionedMarker.index >= nodeStart && positionedMarker.index < nodeEnd) {
startNode = textNode;
}
// If end of marker is in this node
if(startNode && positionedMarker.index + positionedMarker.marker.length > nodeStart &&
positionedMarker.index + positionedMarker.marker.length <= nodeEnd) {
endNode = textNode;
break;
}
currentPos = nodeEnd;
}
if(!startNode){
throw new Error(`Could not find startNode for marker '${positionedMarker.marker}'`)
}
if(!endNode){
throw new Error(`Could not find endNode for marker '${positionedMarker.marker}'`)
}
// Check if marker spans multiple nodes
if(startNode !== endNode) {
// Calculate relative positions within the nodes
let startNodeTextContent = startNode.textContent || '';
let endNodeTextContent = endNode.textContent || '';
// Calculate the position within the start node
let posInStartNode = positionedMarker.index - getNodeTextPosition(startNode, containerTextNodesInTreeOrder);
// Calculate the position within the end node
let posInEndNode = (positionedMarker.index + positionedMarker.marker.length) - getNodeTextPosition(endNode, containerTextNodesInTreeOrder);
/** @type {Node} */
let beforeStartNode = startNode
// if there is before-text, split
if(posInStartNode > 0) {
// Text exists before the marker - preserve it
// set newStartNode to a Text node containing only the marker beginning
const newStartNode = startNode.splitText(posInStartNode)
// startNode/beforeStartNode now contains only non-marker text
// then, by definition of .splitText(posInStartNode):
posInStartNode = 0
// remove the marker beginning part from the tree (since the marker will be inserted in full later)
newStartNode.parentNode?.removeChild(newStartNode)
}
/** @type {Node} */
let afterEndNode
// if there is after-text, split
if(posInEndNode < endNodeTextContent.length) {
// Text exists after the marker - preserve it
// set afterEndNode to a Text node containing only non-marker text
afterEndNode = endNode.splitText(posInEndNode);
// endNode now contains only the end of marker text
// then, by definition of .splitText(posInEndNode):
posInEndNode = endNodeTextContent.length
// remove the marker ending part from the tree (since the marker will be inserted in full later)
endNode.parentNode?.removeChild(endNode)
}
// then, replace all nodes between (new)startNode and (new)endNode with a single textNode in commonAncestor
const insertionPoint = removeNodesBetween(beforeStartNode, afterEndNode)
const markerTextNode = insertionPoint.ownerDocument.createTextNode(positionedMarker.marker)
insertionPoint.parentNode.insertBefore(markerTextNode, insertionPoint)
// After consolidation, break as the DOM structure has changed
// and containerTextNodesInTreeOrder needs to be refreshed
consolidatedMarkers.push(positionedMarker)
break;
}
consolidatedMarkers.push(positionedMarker)
}
}
}
}
/**
* isolate markers which are in Text nodes with other texts
*
* @param {Document} document
*/
function isolateMarkers(document){
traverse(document, currentNode => {
if(currentNode.nodeType === Node.TEXT_NODE) {
// find all marker starts and ends and split textNode
let remainingText = currentNode.textContent || ''
while(remainingText.length >= 1) {
let matchText;
let matchIndex;
// looking for a block marker
for(const marker of [ifStartMarkerRegex, elseMarker, closingIfMarker, eachStartMarkerRegex, eachClosingMarker]) {
if(typeof marker === 'string') {
const index = remainingText.indexOf(marker)
if(index !== -1) {
matchText = marker
matchIndex = index
// found the first match
break; // get out of loop
}
}
else {
// marker is a RegExp
const match = remainingText.match(marker)
if(match) {
matchText = match[0]
matchIndex = match.index
// found the first match
break; // get out of loop
}
}
}
if(matchText) {
// split 3-way : before-match, match and after-match
if(matchText.length < remainingText.length) {
// @ts-ignore
let afterMatchTextNode = currentNode.splitText(matchIndex + matchText.length)
if(afterMatchTextNode.textContent && afterMatchTextNode.textContent.length >= 1) {
remainingText = afterMatchTextNode.textContent
}
else {
remainingText = ''
}
// per spec, currentNode now contains before-match and match text
// @ts-ignore
if(matchIndex > 0) {
// @ts-ignore
currentNode.splitText(matchIndex)
}
if(afterMatchTextNode) {
currentNode = afterMatchTextNode
}
}
else {
remainingText = ''
}
}
else {
remainingText = ''
}
}
}
else {
// skip
}
})
}
/**
* This function prepares the template DOM tree in a way that makes it easily processed by the template execution
* Specifically, after the call to this function, the document is altered to respect the following property:
*
* each template marker ({#each ... as ...}, {/if}, etc.) placed within a single Text node
*
* If the template marker was partially formatted in the original document, the formatting is removed so the
* marker can be within a single Text node
*
* If the template marker was in a Text node with other text, the Text node is split in a way to isolate the marker
* from the rest of the text
*
* @param {Document} document
*/
export default function prepareTemplateDOMTree(document){
consolidateMarkers(document)
isolateMarkers(document)
}

View File

@ -0,0 +1,141 @@
import test from 'ava';
import {join} from 'node:path';
import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
test('template filling with several layers of formatting in {#each ...} start marker', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-plusieurs-couches.odt')
const templateContent = `Liste de nombres
Les nombres : {#each nombres as n}{n} {/each} !!
`
const data = {
nombres : [1,2,3,5]
}
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 2 3 5  !!
`)
});
test('template filling - both {#each ...} and {/each} within the same Text node are formatted', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-2-markeurs-formatted.odt')
const templateContent = `Liste de nombres
Les nombres : {#each nombres as n}{n} {/each} !!
`
const data = {
nombres : [2,3,5,8]
}
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 : 2 3 5 8  !!
`)
});
test('template filling - {#each ...} and text before partially formatted', async t => {
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-each-start-and-before-formatted.odt')
const templateContent = `Liste de nombres
Les nombres : {#each nombres as n}{n} {/each} !!
`
const data = {
nombres : [3,5,8, 13]
}
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 : 3 5 8 13  !!
`)
});
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 templateContent = `Liste de nombres
Les nombres : {#each nombres as n}{n} {/each} !!
`
const data = {
nombres : [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 : 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 !!!
`)
});

Binary file not shown.

Binary file not shown.

Binary file not shown.