odfjs/scripts/DOMUtils.js
David Bruant 0bbc57afb7
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
2025-05-08 17:13:51 +02:00

50 lines
1.2 KiB
JavaScript

import {DOMParser, XMLSerializer} from '#DOM'
/*
Since we're using xmldom in Node.js context, the entire DOM API is not implemented
Functions here are helpers whild xmldom becomes more complete
*/
/**
*
* @param {string} str
* @returns {Document}
*/
export function parseXML(str){
return (new DOMParser()).parseFromString(str, 'application/xml');
}
const serializer = new XMLSerializer()
/** @type { typeof XMLSerializer.prototype.serializeToString } */
export function serializeToString(node){
return serializer.serializeToString(node)
}
/**
* 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
*
* @param {Node} node
* @param {(n : Node) => void} visit
*/
export function traverse(node, visit) {
//console.log('traverse', node.nodeType, node.nodeName)
for (const child of Array.from(node.childNodes)) {
traverse(child, visit);
}
visit(node);
}
export {
DOMParser,
XMLSerializer,
createDocument,
Node
} from '#DOM'