|
| 1 | + |
| 2 | +import { XMLParser } from "../src/fxp.js"; |
| 3 | + |
| 4 | +const XML_METADATA = XMLParser.getMetaDataSymbol(); |
| 5 | + |
| 6 | +/** Collect [rawTagName, metadata] for every node carrying metadata, in document order. */ |
| 7 | +function collectMeta(node, out = []) { |
| 8 | + if (node === null || typeof node !== "object") return out; |
| 9 | + if (node[XML_METADATA]) { |
| 10 | + const tag = Object.keys(node).find((k) => k !== ":@" && k !== "#text"); |
| 11 | + out.push([tag, node[XML_METADATA]]); |
| 12 | + } |
| 13 | + if (Array.isArray(node)) { |
| 14 | + node.forEach((n) => collectMeta(n, out)); |
| 15 | + } else { |
| 16 | + for (const k of Object.keys(node)) collectMeta(node[k], out); |
| 17 | + } |
| 18 | + return out; |
| 19 | +} |
| 20 | + |
| 21 | +/** Parse xml with metadata capture on and return a Map of rawTagName -> raw text span. */ |
| 22 | +function parseSpans(xml) { |
| 23 | + const parser = new XMLParser({ preserveOrder: true, ignoreAttributes: false, captureMetaData: true }); |
| 24 | + const result = parser.parse(xml); |
| 25 | + return new Map(collectMeta(result).map(([tag, m]) => [tag, xml.slice(m.startIndex, m.endIndex)])); |
| 26 | +} |
| 27 | + |
| 28 | +describe("XMLParser captureMetaData endIndex", function () { |
| 29 | + it("does not add metadata (start or end) when captureMetaData is off", function () { |
| 30 | + const xml = `<root><child/></root>`; |
| 31 | + const parser = new XMLParser({ preserveOrder: true, ignoreAttributes: false }); |
| 32 | + const result = parser.parse(xml); |
| 33 | + expect(collectMeta(result).length).toBe(0); |
| 34 | + }); |
| 35 | + |
| 36 | + it("records an exclusive endIndex", function () { |
| 37 | + const xml = `<root><foo/><bar type="quux"/><baz type="foo">FOO</baz></root>`; |
| 38 | + const parser = new XMLParser({ preserveOrder: true, ignoreAttributes: false, captureMetaData: true }); |
| 39 | + const result = parser.parse(xml); |
| 40 | + |
| 41 | + const meta = collectMeta(result); |
| 42 | + const spans = meta.map(([tag, m]) => [tag, xml.slice(m.startIndex, m.endIndex)]); |
| 43 | + |
| 44 | + expect(spans).toEqual([ |
| 45 | + ["root", `<root><foo/><bar type="quux"/><baz type="foo">FOO</baz></root>`], |
| 46 | + ["foo", `<foo/>`], |
| 47 | + ["bar", `<bar type="quux"/>`], |
| 48 | + ["baz", `<baz type="foo">FOO</baz>`], |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("covers self-closing elements", function () { |
| 53 | + const xml = `<a><c/></a>`; |
| 54 | + expect(parseSpans(xml).get("c")).toBe(`<c/>`); |
| 55 | + }); |
| 56 | + |
| 57 | + it("covers paired elements with text content", function () { |
| 58 | + const xml = `<a><d>text</d></a>`; |
| 59 | + expect(parseSpans(xml).get("d")).toBe(`<d>text</d>`); |
| 60 | + }); |
| 61 | + |
| 62 | + it("covers elements with inline attributes", function () { |
| 63 | + const xml = `<a><e id="1" name="foo"/><f class="bar">text</f></a>`; |
| 64 | + const byTag = parseSpans(xml); |
| 65 | + expect(byTag.get("e")).toBe(`<e id="1" name="foo"/>`); |
| 66 | + expect(byTag.get("f")).toBe(`<f class="bar">text</f>`); |
| 67 | + }); |
| 68 | + |
| 69 | + it("covers deeply nested elements", function () { |
| 70 | + const xml = `<a><b><c/></b><d>text</d></a>`; |
| 71 | + const byTag = parseSpans(xml); |
| 72 | + expect(byTag.get("a")).toBe(`<a><b><c/></b><d>text</d></a>`); |
| 73 | + expect(byTag.get("b")).toBe(`<b><c/></b>`); |
| 74 | + }); |
| 75 | + |
| 76 | + it("covers processing-instruction nodes", function () { |
| 77 | + const xml = `<?xml version="1.0"?><a><?pi target?><c/></a>`; |
| 78 | + const byTag = parseSpans(xml); |
| 79 | + expect(byTag.get("?xml")).toBe(`<?xml version="1.0"?>`); |
| 80 | + expect(byTag.get("?pi")).toBe(`<?pi target?>`); |
| 81 | + }); |
| 82 | + |
| 83 | + it("does not corrupt a sibling's endIndex when updateTag drops a node", function () { |
| 84 | + const xml = `<a><b>x</b><skip/><?skip pi?><skip>y</skip></a>`; |
| 85 | + const parser = new XMLParser({ |
| 86 | + preserveOrder: true, |
| 87 | + ignoreAttributes: false, |
| 88 | + captureMetaData: true, |
| 89 | + updateTag: (tagName) => (tagName === "skip" || tagName === "?skip" ? false : tagName), |
| 90 | + }); |
| 91 | + const result = parser.parse(xml); |
| 92 | + |
| 93 | + const byTag = new Map(collectMeta(result).map(([tag, m]) => [tag, xml.slice(m.startIndex, m.endIndex)])); |
| 94 | + expect(byTag.get("b")).toBe(`<b>x</b>`); |
| 95 | + expect(byTag.get("a")).toBe(xml); |
| 96 | + }); |
| 97 | +}); |
0 commit comments