|
| 1 | +// A simple XML parser to replace @xmldom/xmldom dependency |
| 2 | +// This implements just enough functionality to support the existing code |
| 3 | + |
| 4 | +/** |
| 5 | + * A simple XML Element implementation |
| 6 | + * @class |
| 7 | + */ |
| 8 | +class Element { |
| 9 | + /** |
| 10 | + * Create a new Element |
| 11 | + * @param {string} nodeName - The name of the node/tag |
| 12 | + */ |
| 13 | + constructor(nodeName) { |
| 14 | + /** @type {string} */ |
| 15 | + this.nodeName = nodeName; |
| 16 | + /** @type {Array<Element>} */ |
| 17 | + this.childNodes = []; |
| 18 | + /** @type {Object.<string, string>} */ |
| 19 | + this.attributes = {}; |
| 20 | + /** @type {string} */ |
| 21 | + this.textContent = ""; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Get attribute value by name |
| 26 | + * @param {string} name - The attribute name |
| 27 | + * @returns {string|null} The attribute value or null |
| 28 | + */ |
| 29 | + getAttribute(name) { |
| 30 | + return this.attributes[name] || null; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Get elements by tag name |
| 35 | + * @param {string} tagName - The tag name to search for |
| 36 | + * @returns {Array<Element>} The matching elements |
| 37 | + */ |
| 38 | + getElementsByTagName(tagName) { |
| 39 | + /** @type {Array<Element>} */ |
| 40 | + let results = []; |
| 41 | + |
| 42 | + // Check if this element matches |
| 43 | + if (this.nodeName === tagName) { |
| 44 | + results.push(this); |
| 45 | + } |
| 46 | + |
| 47 | + // Check child elements recursively |
| 48 | + for (const child of this.childNodes) { |
| 49 | + if (child instanceof Element) { |
| 50 | + if (tagName === "*" || child.nodeName === tagName) { |
| 51 | + results.push(child); |
| 52 | + } |
| 53 | + |
| 54 | + // Add matching descendants |
| 55 | + const childMatches = child.getElementsByTagName(tagName); |
| 56 | + results = results.concat(childMatches); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return results; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * A simple XML Document implementation |
| 66 | + * @class |
| 67 | + */ |
| 68 | +class Document { |
| 69 | + constructor() { |
| 70 | + /** @type {Element|null} */ |
| 71 | + this.documentElement = null; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * A minimal DOMParser implementation that supports the basic features needed |
| 77 | + * @class |
| 78 | + */ |
| 79 | +class SimpleDOMParser { |
| 80 | + /** |
| 81 | + * Parse XML string into a Document |
| 82 | + * @param {string} xmlString - The XML string to parse |
| 83 | + * @returns {Document} The parsed document |
| 84 | + */ |
| 85 | + parseFromString(xmlString) { |
| 86 | + const doc = new Document(); |
| 87 | + |
| 88 | + // Remove XML declaration if present |
| 89 | + xmlString = xmlString.replace(/<\?xml[^?]*\?>/, "").trim(); |
| 90 | + |
| 91 | + // Parse the document |
| 92 | + doc.documentElement = this.parseElement(xmlString); |
| 93 | + |
| 94 | + return doc; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Parse an XML element |
| 99 | + * @param {string} xmlString - The XML string to parse |
| 100 | + * @returns {Element|null} The parsed element or null |
| 101 | + */ |
| 102 | + parseElement(xmlString) { |
| 103 | + // Regular expressions for parsing XML |
| 104 | + const startTagRegex = /<([^\s/>]+)([^>]*)>/; |
| 105 | + const attributeRegex = /([^\s=]+)=(?:"([^"]*)"|'([^']*)')/g; |
| 106 | + |
| 107 | + // Find the start tag |
| 108 | + const startMatch = xmlString.match(startTagRegex); |
| 109 | + if (!startMatch) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + const tagName = startMatch[1]; |
| 114 | + const attributeString = startMatch[2]; |
| 115 | + |
| 116 | + // Create the element |
| 117 | + const element = new Element(tagName); |
| 118 | + |
| 119 | + // Parse attributes |
| 120 | + let attributeMatch; |
| 121 | + while ((attributeMatch = attributeRegex.exec(attributeString)) !== null) { |
| 122 | + const attrName = attributeMatch[1]; |
| 123 | + const attrValue = attributeMatch[2] || attributeMatch[3]; // Use whichever capture group matched |
| 124 | + element.attributes[attrName] = attrValue; |
| 125 | + } |
| 126 | + |
| 127 | + // Find the content between start and end tags |
| 128 | + const startTagEnd = startMatch[0].length; |
| 129 | + const endTagSearch = new RegExp(`</${tagName}>`); |
| 130 | + const endMatch = xmlString.slice(startTagEnd).search(endTagSearch); |
| 131 | + |
| 132 | + if (endMatch === -1) { |
| 133 | + // Self-closing or malformed tag |
| 134 | + return element; |
| 135 | + } |
| 136 | + |
| 137 | + const contentString = xmlString.slice(startTagEnd, startTagEnd + endMatch); |
| 138 | + |
| 139 | + // Parse child elements |
| 140 | + let remainingContent = contentString.trim(); |
| 141 | + while (remainingContent.length > 0) { |
| 142 | + // Check if there's a child element |
| 143 | + if (remainingContent.startsWith("<") && !remainingContent.startsWith("</")) { |
| 144 | + // Find the next child element |
| 145 | + const childStartMatch = remainingContent.match(startTagRegex); |
| 146 | + if (childStartMatch) { |
| 147 | + const childTagName = childStartMatch[1]; |
| 148 | + const childEndTagSearch = new RegExp(`</${childTagName}>`); |
| 149 | + const childEndIndex = remainingContent.search(childEndTagSearch); |
| 150 | + |
| 151 | + if (childEndIndex !== -1) { |
| 152 | + // Extract the complete child element string (including its end tag) |
| 153 | + const childEndTagLength = childTagName.length + 3; // "</tag>" |
| 154 | + const childXmlString = remainingContent.slice(0, childEndIndex + childEndTagLength); |
| 155 | + |
| 156 | + // Parse the child element and add it to parent |
| 157 | + const childElement = this.parseElement(childXmlString); |
| 158 | + if (childElement) { |
| 159 | + element.childNodes.push(childElement); |
| 160 | + } |
| 161 | + |
| 162 | + // Remove the processed child from remaining content |
| 163 | + remainingContent = remainingContent.slice(childXmlString.length).trim(); |
| 164 | + continue; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Handle text content |
| 170 | + const nextTagIndex = remainingContent.indexOf("<"); |
| 171 | + if (nextTagIndex === -1) { |
| 172 | + // The rest is all text |
| 173 | + element.textContent += remainingContent.trim(); |
| 174 | + break; |
| 175 | + } else if (nextTagIndex > 0) { |
| 176 | + // There's some text before the next tag |
| 177 | + element.textContent += remainingContent.slice(0, nextTagIndex).trim(); |
| 178 | + remainingContent = remainingContent.slice(nextTagIndex).trim(); |
| 179 | + } else { |
| 180 | + // Can't parse further, just break |
| 181 | + break; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return element; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +// Export DOMParser as a class |
| 190 | +export { SimpleDOMParser as DOMParser }; |
0 commit comments