Test of 2 formatted markers within same Text node passes
This commit is contained in:
parent
0825243b41
commit
3793923c79
@ -1,6 +1,6 @@
|
|||||||
import {ZipReader, ZipWriter, BlobReader, BlobWriter, TextReader, Uint8ArrayReader, TextWriter, Uint8ArrayWriter} from '@zip.js/zip.js';
|
import {ZipReader, ZipWriter, BlobReader, BlobWriter, TextReader, Uint8ArrayReader, TextWriter, Uint8ArrayWriter} from '@zip.js/zip.js';
|
||||||
|
|
||||||
import {parseXML, serializeToString, Node} from '../../DOMUtils.js'
|
import {parseXML, serializeToString} from '../../DOMUtils.js'
|
||||||
import {makeManifestFile, getManifestFileData} from '../manifest.js';
|
import {makeManifestFile, getManifestFileData} from '../manifest.js';
|
||||||
import prepareTemplateDOMTree from './prepareTemplateDOMTree.js';
|
import prepareTemplateDOMTree from './prepareTemplateDOMTree.js';
|
||||||
|
|
||||||
@ -164,8 +164,3 @@ export default async function fillOdtTemplate(odtTemplate, data) {
|
|||||||
return writer.close();
|
return writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -35,7 +35,6 @@ function findAllMatches(text, pattern) {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Node} node1
|
* @param {Node} node1
|
||||||
@ -72,7 +71,6 @@ function getAncestors(node) {
|
|||||||
return ancestors;
|
return ancestors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* text position of a node relative to a text nodes within a container
|
* text position of a node relative to a text nodes within a container
|
||||||
*
|
*
|
||||||
@ -268,8 +266,6 @@ function removeNodesBetween(startBranch, endBranch, commonAncestor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Consolidate markers which are split among several Text nodes
|
* Consolidate markers which are split among several Text nodes
|
||||||
*
|
*
|
||||||
@ -277,23 +273,33 @@ function removeNodesBetween(startBranch, endBranch, commonAncestor) {
|
|||||||
*/
|
*/
|
||||||
function consolidateMarkers(document){
|
function consolidateMarkers(document){
|
||||||
// Perform a first pass to detect templating markers with formatting to remove it
|
// Perform a first pass to detect templating markers with formatting to remove it
|
||||||
const potentialMarkerContainers = [
|
const potentialMarkersContainers = [
|
||||||
...Array.from(document.getElementsByTagName('text:p')),
|
...Array.from(document.getElementsByTagName('text:p')),
|
||||||
...Array.from(document.getElementsByTagName('text:h'))
|
...Array.from(document.getElementsByTagName('text:h'))
|
||||||
]
|
]
|
||||||
|
|
||||||
for(const potentialMarkerContainer of potentialMarkerContainers) {
|
for(const potentialMarkersContainer of potentialMarkersContainers) {
|
||||||
// Check if any template marker is split across multiple text nodes
|
const consolidatedMarkers = []
|
||||||
// Get all text nodes within this container
|
|
||||||
/** @type {Text[]} */
|
/** @type {Text[]} */
|
||||||
const containerTextNodesInTreeOrder = [];
|
let containerTextNodesInTreeOrder = [];
|
||||||
|
|
||||||
|
function refreshContainerTextNodes(){
|
||||||
|
containerTextNodesInTreeOrder = []
|
||||||
|
|
||||||
|
traverse(potentialMarkersContainer, node => {
|
||||||
|
if(node.nodeType === Node.TEXT_NODE) {
|
||||||
|
containerTextNodesInTreeOrder.push(/** @type {Text} */(node))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshContainerTextNodes()
|
||||||
|
|
||||||
let fullText = ''
|
let fullText = ''
|
||||||
traverse(potentialMarkerContainer, node => {
|
for(const node of containerTextNodesInTreeOrder){
|
||||||
if(node.nodeType === Node.TEXT_NODE) {
|
fullText = fullText + node.textContent
|
||||||
containerTextNodesInTreeOrder.push(/** @type {Text} */(node))
|
}
|
||||||
fullText = fullText + node.textContent
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Check for each template marker
|
// Check for each template marker
|
||||||
const positionedMarkers = [
|
const positionedMarkers = [
|
||||||
@ -307,10 +313,11 @@ function consolidateMarkers(document){
|
|||||||
console.log('positionedMarkers', positionedMarkers)
|
console.log('positionedMarkers', positionedMarkers)
|
||||||
|
|
||||||
// If no markers found, skip this container
|
// If no markers found, skip this container
|
||||||
if(positionedMarkers.length >= 1) {
|
while(consolidatedMarkers.length < positionedMarkers.length) {
|
||||||
|
refreshContainerTextNodes()
|
||||||
|
|
||||||
// For each marker, check if it's contained within a single text node
|
// For each marker, check if it's contained within a single text node
|
||||||
for(const positionedMarker of positionedMarkers) {
|
for(const positionedMarker of positionedMarkers.slice(consolidatedMarkers.length)) {
|
||||||
console.log('positionedMarker', positionedMarker)
|
console.log('positionedMarker', positionedMarker)
|
||||||
|
|
||||||
let markerStart = -1;
|
let markerStart = -1;
|
||||||
@ -342,9 +349,16 @@ function consolidateMarkers(document){
|
|||||||
currentPos = nodeEnd;
|
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
|
// Check if marker spans multiple nodes
|
||||||
if(startNode !== endNode) {
|
if(startNode !== endNode) {
|
||||||
console.log('startNode !== endNode')
|
|
||||||
const commonAncestor = findCommonAncestor(startNode, endNode);
|
const commonAncestor = findCommonAncestor(startNode, endNode);
|
||||||
|
|
||||||
// Calculate relative positions within the nodes
|
// Calculate relative positions within the nodes
|
||||||
@ -364,8 +378,6 @@ function consolidateMarkers(document){
|
|||||||
// Find the diverging point in the paths
|
// Find the diverging point in the paths
|
||||||
const lowestCommonAncestorChild = findDivergingPoint(pathToStart, pathToEnd);
|
const lowestCommonAncestorChild = findDivergingPoint(pathToStart, pathToEnd);
|
||||||
|
|
||||||
console.log('lowestCommonAncestorChild', lowestCommonAncestorChild)
|
|
||||||
|
|
||||||
if(!lowestCommonAncestorChild) {
|
if(!lowestCommonAncestorChild) {
|
||||||
// Direct parent-child relationship or other simple case
|
// Direct parent-child relationship or other simple case
|
||||||
// Handle separately
|
// Handle separately
|
||||||
@ -392,6 +404,8 @@ function consolidateMarkers(document){
|
|||||||
// Create a new node for the start of the marker
|
// Create a new node for the start of the marker
|
||||||
const startOfMarkerNode = document.createTextNode(positionedMarker.marker);
|
const startOfMarkerNode = document.createTextNode(positionedMarker.marker);
|
||||||
|
|
||||||
|
console.log('parentOfStartNode', parentOfStartNode)
|
||||||
|
|
||||||
// Insert after the modified start node
|
// Insert after the modified start node
|
||||||
if(startNode.nextSibling) {
|
if(startNode.nextSibling) {
|
||||||
parentOfStartNode.insertBefore(startOfMarkerNode, startNode.nextSibling);
|
parentOfStartNode.insertBefore(startOfMarkerNode, startNode.nextSibling);
|
||||||
@ -437,15 +451,18 @@ function consolidateMarkers(document){
|
|||||||
removeNodesBetween(startBranch, endBranch, commonAncestor);
|
removeNodesBetween(startBranch, endBranch, commonAncestor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// After consolidation, we can break as the DOM structure has changed
|
// After consolidation, break as the DOM structure has changed
|
||||||
|
// and containerTextNodesInTreeOrder needs to be refreshed
|
||||||
|
consolidatedMarkers.push(positionedMarker)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
consolidatedMarkers.push(positionedMarker)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* isolate markers which are in Text nodes with other texts
|
* isolate markers which are in Text nodes with other texts
|
||||||
*
|
*
|
||||||
@ -529,8 +546,6 @@ function isolateMarkers(document){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function prepares the template DOM tree in a way that makes it easily processed by the template execution
|
* 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:
|
* Specifically, after the call to this function, the document is altered to respect the following property:
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import {getOdtTemplate} from '../../scripts/odf/odtTemplate-forNode.js'
|
|||||||
|
|
||||||
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
|
import {fillOdtTemplate, getOdtTextContent} from '../../exports.js'
|
||||||
|
|
||||||
test('template filling {#each ...}{/each} with formating in {#each ...} start marker', async t => {
|
test('template filling with several layers of formatting in {#each ...} start marker', async t => {
|
||||||
const templatePath = join(import.meta.dirname, '../fixtures/liste-nombres-avec-formattage.odt')
|
const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-plusieurs-couches.odt')
|
||||||
const templateContent = `Liste de nombres
|
const templateContent = `Liste de nombres
|
||||||
|
|
||||||
Les nombres : {#each nombres as n}{n} {/each} !!
|
Les nombres : {#each nombres as n}{n} {/each} !!
|
||||||
@ -29,4 +29,31 @@ Les nombres : {#each nombres as n}{n} {/each} !!
|
|||||||
Les nombres : 1 2 3 5 !!
|
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 !!
|
||||||
|
`)
|
||||||
|
|
||||||
});
|
});
|
||||||
BIN
tests/fixtures/formatting-liste-nombres-2-markeurs-formatted.odt
vendored
Normal file
BIN
tests/fixtures/formatting-liste-nombres-2-markeurs-formatted.odt
vendored
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user