test runs and fails

This commit is contained in:
David Bruant 2025-05-08 13:08:37 +02:00
parent 9191de5369
commit b51304c394
2 changed files with 519 additions and 115 deletions

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).
*
* This should probably be replace by the TreeWalker API when implemented by xmldom

View File

@ -294,12 +294,12 @@ const IF = 'IF'
const EACH = 'EACH'
// the regexps below are shared, so they shoudn't have state (no 'g' flag)
const ifStartRegex = /{#if\s+([^}]+?)\s*}/;
const ifStartMarkerRegex = /{#if\s+([^}]+?)\s*}/;
const elseMarker = '{:else}'
const closingIfMarker = '{/if}'
const eachStartMarkerRegex = /{#each\s+([^}]+?)\s+as\s+([^}]+?)\s*}/;
const eachClosingBlockString = '{/each}'
const eachClosingBlockMarker = '{/each}'
@ -366,7 +366,7 @@ function fillTemplatedOdtElement(rootElement, compartment){
/**
* Looking for {/each}
*/
const isEachClosingBlock = text.includes(eachClosingBlockString)
const isEachClosingBlock = text.includes(eachClosingBlockMarker)
if(isEachClosingBlock) {
@ -401,7 +401,7 @@ function fillTemplatedOdtElement(rootElement, compartment){
/**
* Looking for {#if ...}
*/
const ifStartMatch = text.match(ifStartRegex);
const ifStartMatch = text.match(ifStartMarkerRegex);
if(ifStartMatch) {
currentlyOpenBlocks.push(IF)
@ -515,6 +515,242 @@ function fillTemplatedOdtElement(rootElement, compartment){
}
// Helper function to find all regex matches with positions
/**
*
* @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}
*/
function findCommonAncestor(node1, node2) {
const ancestors1 = getAncestors(node1);
const ancestors2 = getAncestors(node2);
for(const ancestor of ancestors1) {
if(ancestors2.includes(ancestor)) {
return ancestor;
}
}
return null;
}
/**
*
* @param {Node} node
* @returns {Node[]}
*/
function getAncestors(node) {
const ancestors = [];
let current = node;
while(current) {
ancestors.push(current);
current = current.parentNode;
}
return ancestors;
}
// Helper function to find nodes between start and end (inclusive)
function findNodesBetween(startNode, endNode) {
const commonAncestor = findCommonAncestor(startNode, endNode);
if(!commonAncestor) return [];
const result = [];
let capturing = false;
function traverse(node) {
if(node === startNode) {
capturing = true;
}
if(capturing) {
result.push(node);
}
if(node === endNode) {
capturing = false;
return true;
}
for(let child = node.firstChild; child; child = child.nextSibling) {
if(traverse(child)) return true;
}
return false;
}
traverse(commonAncestor);
return result;
}
/**
* 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`)
}
// Helper function to get the path from ancestor to descendant
function getPathToNode(node, ancestor) {
const path = [];
let current = node;
while(current && current !== ancestor) {
path.unshift(current);
current = current.parentNode;
}
return path;
}
// Helper function to find the point where two paths diverge
function findDivergingPoint(path1, path2) {
for(let i = 0; i < Math.min(path1.length, path2.length); i++) {
if(path1[i] !== path2[i]) {
return path1[i - 1] || null; // Return the last common node
}
}
// One path is a prefix of the other
return path1[Math.min(path1.length, path2.length) - 1];
}
// Helper function to handle the case where start and end nodes have a direct relationship
function consolidateDirectRelationship(startNode, endNode, posInStartNode, posInEndNode, markerText) {
const startNodeParent = startNode.parentNode;
const endNodeParent = endNode.parentNode;
const document = startNode.ownerDocument;
if(startNodeParent === endNodeParent) {
// Siblings case
// Preserve text before marker in start node
if(posInStartNode > 0) {
startNode.textContent = startNode.textContent.substring(0, posInStartNode);
} else {
startNodeParent.removeChild(startNode);
}
// Create marker node
const markerNode = document.createTextNode(markerText);
startNodeParent.insertBefore(markerNode, endNode);
// Preserve text after marker in end node
if(posInEndNode < endNode.textContent.length) {
endNode.textContent = endNode.textContent.substring(posInEndNode);
} else {
endNodeParent.removeChild(endNode);
}
} else {
// Handle nested case (one is ancestor of other)
// This is more complex and needs customized handling
// Simplified approach: replace everything with marker
// A more sophisticated approach would be needed for production
const isStartAncestorOfEnd = isAncestor(startNode, endNode);
if(isStartAncestorOfEnd) {
replaceWithMarker(startNode, markerText);
} else {
replaceWithMarker(endNode, markerText);
}
}
}
// Helper function to check if one node is ancestor of another
function isAncestor(potentialAncestor, node) {
let current = node.parentNode;
while(current) {
if(current === potentialAncestor) return true;
current = current.parentNode;
}
return false;
}
// Helper function to replace a node with marker text
function replaceWithMarker(node, markerText) {
const document = node.ownerDocument;
const markerNode = document.createTextNode(markerText);
node.parentNode.replaceChild(markerNode, node);
}
// Helper function to remove nodes between two sibling branches
function removeNodesBetween(startBranch, endBranch, commonAncestor) {
let removing = false;
let nodesToRemove = [];
for(let child = commonAncestor.firstChild; child; child = child.nextSibling) {
if(child === startBranch) {
removing = true;
continue; // Don't remove the start branch
}
if(removing) {
if(child === endBranch) {
break; // Stop when we reach end branch
}
nodesToRemove.push(child);
}
}
// Remove all nodes marked for removal
for(const nodeToRemove of nodesToRemove) {
commonAncestor.removeChild(nodeToRemove);
}
}
/**
*
@ -524,12 +760,180 @@ function fillTemplatedOdtElement(rootElement, compartment){
*/
function fillTemplatedOdtDocument(document, compartment) {
// prepare tree to be used as template
// Perform a first traverse to split textnodes when they contain several block markers
// Prepare tree to be used as template
// Perform a first pass to detect templating markers with formatting to remove it
const potentialMarkerContainers = [
...Array.from(document.getElementsByTagName('text:p')),
...Array.from(document.getElementsByTagName('text:h'))
]
for(const potentialMarkerContainer of potentialMarkerContainers) {
// Check if any template marker is split across multiple text nodes
// Get all text nodes within this container
/** @type {Text[]} */
const containerTextNodesInTreeOrder = [];
let fullText = ''
traverse(potentialMarkerContainer, node => {
if(node.nodeType === Node.TEXT_NODE) {
containerTextNodesInTreeOrder.push(/** @type {Text} */ (node))
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, eachClosingBlockMarker)
];
// If no markers found, skip this container
if(positionedMarkers.length >= 1) {
// For each marker, check if it's contained within a single text node
for(const positionedMarker of positionedMarkers) {
let markerStart = -1;
let markerEnd = -1;
let currentPos = 0;
let markerSpansNodes = false;
let startNode = null;
let endNode = null;
// 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(markerStart === -1 && positionedMarker.index >= nodeStart && positionedMarker.index < nodeEnd) {
markerStart = positionedMarker.index;
startNode = textNode;
}
// If end of marker is in this node
if(markerStart !== -1 && positionedMarker.index + positionedMarker.marker.length > nodeStart &&
positionedMarker.index + positionedMarker.marker.length <= nodeEnd) {
markerEnd = positionedMarker.index + positionedMarker.marker.length;
endNode = textNode;
break;
}
currentPos = nodeEnd;
}
// Check if marker spans multiple nodes
if(startNode !== endNode) {
console.log('startNode !== endNode')
const commonAncestor = findCommonAncestor(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);
// Get the path from common ancestor to start and end nodes
const pathToStart = getPathToNode(startNode, commonAncestor);
const pathToEnd = getPathToNode(endNode, commonAncestor);
// Find the diverging point in the paths
const lowestCommonAncestorChild = findDivergingPoint(pathToStart, pathToEnd);
if(!lowestCommonAncestorChild) {
// Direct parent-child relationship or other simple case
// Handle separately
consolidateDirectRelationship(startNode, endNode, posInStartNode, posInEndNode, positionedMarker.marker);
} else {
// Complex case: we need to:
// 1. Preserve text before marker in startNode
// 2. Preserve text after marker in endNode
// 3. Replace everything in-between with marker text
// Get all nodes between the diverging branches (including the branches)
const startBranch = pathToStart[pathToStart.indexOf(lowestCommonAncestorChild)];
const endBranch = pathToEnd[pathToEnd.indexOf(lowestCommonAncestorChild)];
// First, handle the start node - split if necessary
if(posInStartNode > 0) {
// Text exists before the marker - preserve it
const textBeforeMarker = startNodeTextContent.substring(0, posInStartNode);
const parentOfStartNode = startNode.parentNode;
// Replace the start node with the text before marker
startNode.textContent = textBeforeMarker;
// Create a new node for the start of the marker
const startOfMarkerNode = document.createTextNode(positionedMarker.marker);
// Insert after the modified start node
if(startNode.nextSibling) {
parentOfStartNode.insertBefore(startOfMarkerNode, startNode.nextSibling);
} else {
parentOfStartNode.appendChild(startOfMarkerNode);
}
} else {
// No text before marker, just replace the content
startNode.textContent = positionedMarker.marker;
}
// Handle the end node - split if necessary
if(posInEndNode < endNodeTextContent.length) {
// Text exists after the marker - preserve it
const textAfterMarker = endNodeTextContent.substring(posInEndNode);
const parentOfEndNode = endNode.parentNode;
// Replace the end node with just the text after marker
endNode.textContent = textAfterMarker;
// Create a new node for the end of the marker if needed
// Only needed if we haven't already added the full marker to the start node
if(posInStartNode > 0) {
const endOfMarkerNode = document.createTextNode(""); // Empty as marker is in start node
// Insert before the modified end node
parentOfEndNode.insertBefore(endOfMarkerNode, endNode);
}
} else {
// No text after marker
if(posInStartNode > 0) {
// If we preserved text before the marker, remove the end node
// as the marker is now fully in the start branch
endNode.parentNode.removeChild(endNode);
} else {
// Otherwise just replace the content
endNode.textContent = "";
}
}
// Now remove all nodes between start branch and end branch
// but not the branches themselves
removeNodesBetween(startBranch, endBranch, commonAncestor);
}
// After consolidation, we can break as the DOM structure has changed
break;
}
}
}
}
// Perform a second pass 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
// find all marker starts and ends and split textNode
let remainingText = currentNode.textContent || ''
while(remainingText.length >= 1) {
@ -537,7 +941,7 @@ function fillTemplatedOdtDocument(document, compartment){
let matchIndex;
// looking for a block marker
for(const marker of [ifStartRegex, elseMarker, closingIfMarker, eachStartMarkerRegex, eachClosingBlockString]){
for(const marker of [ifStartMarkerRegex, elseMarker, closingIfMarker, eachStartMarkerRegex, eachClosingBlockMarker]) {
if(typeof marker === 'string') {
const index = remainingText.indexOf(marker)