|
1 |
| -import { parseXml } from "../xml" |
| 1 | +import { parseXml, parseXmlForDiff } from "../xml" |
2 | 2 |
|
3 | 3 | describe("parseXml", () => {
|
4 | 4 | describe("type conversion", () => {
|
@@ -115,3 +115,126 @@ describe("parseXml", () => {
|
115 | 115 | })
|
116 | 116 | })
|
117 | 117 | })
|
| 118 | + |
| 119 | +describe("parseXmlForDiff", () => { |
| 120 | + describe("HTML entity handling", () => { |
| 121 | + it("should NOT decode HTML entities like &", () => { |
| 122 | + const xml = ` |
| 123 | + <root> |
| 124 | + <content>Team Identity & Project Positioning</content> |
| 125 | + </root> |
| 126 | + ` |
| 127 | + |
| 128 | + const result = parseXmlForDiff(xml) as any |
| 129 | + |
| 130 | + // The & should remain as-is, not be decoded to & |
| 131 | + expect(result.root.content).toBe("Team Identity & Project Positioning") |
| 132 | + }) |
| 133 | + |
| 134 | + it("should preserve & character without encoding", () => { |
| 135 | + const xml = ` |
| 136 | + <root> |
| 137 | + <content>Team Identity & Project Positioning</content> |
| 138 | + </root> |
| 139 | + ` |
| 140 | + |
| 141 | + const result = parseXmlForDiff(xml) as any |
| 142 | + |
| 143 | + // The & should remain as-is |
| 144 | + expect(result.root.content).toBe("Team Identity & Project Positioning") |
| 145 | + }) |
| 146 | + |
| 147 | + it("should NOT decode other HTML entities", () => { |
| 148 | + const xml = ` |
| 149 | + <root> |
| 150 | + <content><div> "Hello" 'World'</content> |
| 151 | + </root> |
| 152 | + ` |
| 153 | + |
| 154 | + const result = parseXmlForDiff(xml) as any |
| 155 | + |
| 156 | + // All HTML entities should remain as-is |
| 157 | + expect(result.root.content).toBe("<div> "Hello" 'World'") |
| 158 | + }) |
| 159 | + |
| 160 | + it("should handle mixed content with entities correctly", () => { |
| 161 | + const xml = ` |
| 162 | + <root> |
| 163 | + <code>if (a < b && c > d) { return "test"; }</code> |
| 164 | + </root> |
| 165 | + ` |
| 166 | + |
| 167 | + const result = parseXmlForDiff(xml) as any |
| 168 | + |
| 169 | + // All entities should remain unchanged |
| 170 | + expect(result.root.code).toBe("if (a < b && c > d) { return "test"; }") |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + describe("basic functionality (same as parseXml)", () => { |
| 175 | + it("should correctly parse a simple XML string", () => { |
| 176 | + const xml = ` |
| 177 | + <root> |
| 178 | + <name>Test Name</name> |
| 179 | + <description>Some description</description> |
| 180 | + </root> |
| 181 | + ` |
| 182 | + |
| 183 | + const result = parseXmlForDiff(xml) as any |
| 184 | + |
| 185 | + expect(result).toHaveProperty("root") |
| 186 | + expect(result.root).toHaveProperty("name", "Test Name") |
| 187 | + expect(result.root).toHaveProperty("description", "Some description") |
| 188 | + }) |
| 189 | + |
| 190 | + it("should handle attributes correctly", () => { |
| 191 | + const xml = ` |
| 192 | + <root> |
| 193 | + <item id="1" category="test">Item content</item> |
| 194 | + </root> |
| 195 | + ` |
| 196 | + |
| 197 | + const result = parseXmlForDiff(xml) as any |
| 198 | + |
| 199 | + expect(result.root.item).toHaveProperty("@_id", "1") |
| 200 | + expect(result.root.item).toHaveProperty("@_category", "test") |
| 201 | + expect(result.root.item).toHaveProperty("#text", "Item content") |
| 202 | + }) |
| 203 | + |
| 204 | + it("should support stopNodes parameter", () => { |
| 205 | + const xml = ` |
| 206 | + <root> |
| 207 | + <data> |
| 208 | + <nestedXml><item>Should not parse this</item></nestedXml> |
| 209 | + </data> |
| 210 | + </root> |
| 211 | + ` |
| 212 | + |
| 213 | + const result = parseXmlForDiff(xml, ["nestedXml"]) as any |
| 214 | + |
| 215 | + expect(result.root.data.nestedXml).toBeTruthy() |
| 216 | + expect(result.root.data.nestedXml).toHaveProperty("item", "Should not parse this") |
| 217 | + }) |
| 218 | + }) |
| 219 | + |
| 220 | + describe("diff-specific use case", () => { |
| 221 | + it("should preserve exact content for diff matching", () => { |
| 222 | + // This simulates the actual use case from the issue |
| 223 | + const xml = ` |
| 224 | + <args> |
| 225 | + <file> |
| 226 | + <path>./doc.md</path> |
| 227 | + <diff> |
| 228 | + <content>Team Identity & Project Positioning</content> |
| 229 | + </diff> |
| 230 | + </file> |
| 231 | + </args> |
| 232 | + ` |
| 233 | + |
| 234 | + const result = parseXmlForDiff(xml, ["file.diff.content"]) as any |
| 235 | + |
| 236 | + // The & should remain as-is for exact matching with file content |
| 237 | + expect(result.args.file.diff.content).toBe("Team Identity & Project Positioning") |
| 238 | + }) |
| 239 | + }) |
| 240 | +}) |
0 commit comments