2025-04-17 17:39:08 +02:00
|
|
|
import {DOMParser, XMLSerializer} from '#DOM'
|
|
|
|
|
|
2025-04-07 11:00:18 +02:00
|
|
|
/*
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-17 17:39:08 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-07 11:00:18 +02:00
|
|
|
/**
|
2025-05-08 13:08:37 +02:00
|
|
|
* Traverses a DOM tree starting from the given node and applies the visit function
|
2025-04-07 11:00:18 +02:00
|
|
|
* 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);
|
|
|
|
|
}
|
2025-04-17 17:39:08 +02:00
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
DOMParser,
|
|
|
|
|
XMLSerializer,
|
|
|
|
|
createDocument,
|
|
|
|
|
Node
|
|
|
|
|
} from '#DOM'
|