office: tree CRDT handles the <?xml?> declaration (and PIs/comments) as leaf nodes — fixes broken flat-ODF round-trip in online sync
- Commit
01d6474abc60769083929c6b79819fe531a66b79- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-06 21:23 UTC (5 months ago)
- Parents
766e558
Diff
office/src/main/java/com/vayunmathur/office/util/DocumentTreeCrdt.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/DocumentTreeCrdt.kt b/office/src/main/java/com/vayunmathur/office/util/DocumentTreeCrdt.ktindex 225ca5015f..2da0dfe953 100644--- a/office/src/main/java/com/vayunmathur/office/util/DocumentTreeCrdt.kt+++ b/office/src/main/java/com/vayunmathur/office/util/DocumentTreeCrdt.kt@@ -208,6 +208,8 @@ class DocumentTreeCrdt(private val device: String) { val tag = xml.substring(i, minOf(j + 1, n)) when { tag.startsWith("</") -> { if (stack.size > 1) stack.removeLast(); if (tag.startsWith("</office:binary-data")) inBinary = false }+ // XML declaration, processing instructions, comments, doctype: opaque leaves (no close tag).+ tag.startsWith("<?") || tag.startsWith("<!") -> stack.last().children.add(Desired.El(tag, true)) tag.endsWith("/>") -> stack.last().children.add(Desired.El(tag, true)) else -> { val el = Desired.El(tag, false); stack.last().children.add(el); stack.addLast(el)
office/src/test/java/com/vayunmathur/office/util/DocumentTreeCrdtTest.kt
diff --git a/office/src/test/java/com/vayunmathur/office/util/DocumentTreeCrdtTest.kt b/office/src/test/java/com/vayunmathur/office/util/DocumentTreeCrdtTest.ktindex 2d617d026b..658a30a168 100644--- a/office/src/test/java/com/vayunmathur/office/util/DocumentTreeCrdtTest.kt+++ b/office/src/test/java/com/vayunmathur/office/util/DocumentTreeCrdtTest.kt@@ -14,6 +14,26 @@ class DocumentTreeCrdtTest { return a to b } + @Test+ fun xml_declaration_and_full_document_roundtrip() {+ // Mirrors real serializeFlat output: an <?xml?> declaration then the document element.+ val xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><office:document office:version=\"1.3\">" ++ "<office:body><office:text><text:p>Hi</text:p></office:text></office:body></office:document>"+ val a = DocumentTreeCrdt("A")+ a.update(xml)+ assertEquals(xml, a.render())+ // And a concurrent edit still merges + keeps the declaration + valid structure.+ val b = DocumentTreeCrdt("B"); b.apply(a.toState().nodes)+ val opsA = a.update(xml.replace("<text:p>Hi</text:p>", "<text:p>Hi there</text:p>"))+ val opsB = b.update(xml.replace("<text:p>Hi</text:p>", "<text:p>Oh Hi</text:p>"))+ a.apply(opsB); b.apply(opsA)+ assertEquals(a.render(), b.render())+ val r = a.render()+ assertTrue(r.startsWith("<?xml"))+ assertTrue(r.contains("there")); assertTrue(r.contains("Oh"))+ assertEquals(1, Regex("<office:document ").findAll(r).count())+ }+ @Test fun roundtrip_render_equals_input() { val a = DocumentTreeCrdt("A")