File
Blob: src/worker/xml/dead-props.ts
| 1 | import { parseSafeXml, type ParsedXmlElement } from "./parser"; |
| 2 | |
| 3 | export interface DeadPropertyFragment { |
| 4 | nsUri: string; |
| 5 | localName: string; |
| 6 | xmlValue: string; |
| 7 | } |
| 8 | |
| 9 | function collectNamespaces(element: ParsedXmlElement): Record<string, string> { |
| 10 | const namespaces: Record<string, string> = {}; |
| 11 | for (const [name, value] of Object.entries(element.attributes)) { |
| 12 | if (name === "xmlns") namespaces[""] = value; |
| 13 | if (name.startsWith("xmlns:")) namespaces[name.slice("xmlns:".length)] = value; |
| 14 | } |
| 15 | return namespaces; |
| 16 | } |
| 17 | |
| 18 | function splitName(name: string): { prefix: string; localName: string } { |
| 19 | const index = name.indexOf(":"); |
| 20 | if (index === -1) return { prefix: "", localName: name }; |
| 21 | return { prefix: name.slice(0, index), localName: name.slice(index + 1) }; |
| 22 | } |
| 23 | |
| 24 | export function extractDeadPropertyFragment(xml: string): DeadPropertyFragment { |
| 25 | const { root } = parseSafeXml(xml); |
| 26 | const namespaces = collectNamespaces(root); |
| 27 | const { prefix, localName } = splitName(root.name); |
| 28 | return { |
| 29 | nsUri: namespaces[prefix] ?? "", |
| 30 | localName, |
| 31 | xmlValue: xml, |
| 32 | }; |
| 33 | } |