gamedata_xml.py: count comments from XML parser events

This commit is contained in:
bbareth
2026-11-22 00:59:52 +01:00
parent 4abd54571b
commit 9321157890
2 changed files with 16 additions and 3 deletions

View File

@ -159,6 +159,17 @@ class GamedataXmlTest(unittest.TestCase):
self.assertIn("1 malformed", p.stdout)
self.assertIn("line 1", p.stdout)
def test_comments_inside_cdata_are_not_counted(self):
doc = self.write("comments.xml", b"<!-- real --><Root><![CDATA[<!-- text -->]]></Root><!-- real -->")
out = os.path.join(self.root, "catalog.json")
p = self.run_tool(doc, "--json", out)
self.assertEqual(p.returncode, 0, p.stderr)
with open(out, encoding="utf-8") as f:
report = json.load(f)
self.assertTrue(report["rows"][0]["well_formed"])
self.assertEqual(report["rows"][0]["comments"], 2)
self.assertEqual(report["summary"]["comments"], 2)
def test_carve_directory_joins_chunks_and_marks_fragment(self):
carve = os.path.join(self.root, "carve")
os.makedirs(carve)

View File

@ -132,7 +132,7 @@ def scan_xml(data: bytes) -> dict:
"element_count": 0,
"max_depth": 0,
"attributes": [],
"comments": data.count(b"<!--"),
"comments": 0,
"elements": Counter(),
"attrs": Counter(),
"refs": Counter(),
@ -148,9 +148,11 @@ def scan_xml(data: bytes) -> dict:
}
depth = 0
try:
parser = ET.iterparse(io.BytesIO(data), events=("start", "end"))
parser = ET.iterparse(io.BytesIO(data), events=("start", "end", "comment"))
for event, elem in parser:
if event == "start":
if event == "comment":
report["comments"] += 1
elif event == "start":
depth += 1
report["max_depth"] = max(report["max_depth"], depth)
if not report["root"]: