git » sdk » commit ed4a968

Merge adjacent cdata

author Stephen Paul Weber
2026-04-03 02:32:46 UTC
committer Stephen Paul Weber
2026-04-03 02:32:58 UTC
parent 26a7068b62f5730bc75d70e982f800b1360fa21f

Merge adjacent cdata

borogove/XEP0393.hx +19 -1
test/TestXEP0393.hx +29 -0

diff --git a/borogove/XEP0393.hx b/borogove/XEP0393.hx
index 9b543be..a66f382 100644
--- a/borogove/XEP0393.hx
+++ b/borogove/XEP0393.hx
@@ -137,7 +137,25 @@ class XEP0393 {
 				}
 			}
 		}
-		return spans;
+		return mergeSpans(spans);
+	}
+
+	private static function mergeSpans(spans: Array<Node>) {
+		final mergedSpans = [];
+		for (span in spans) {
+			if (mergedSpans.length > 0) {
+				final last = mergedSpans[mergedSpans.length - 1];
+				switch [last, span] {
+					case [CData(l), CData(s)]:
+						mergedSpans[mergedSpans.length - 1] = CData(new TextNode(l.content + s.content));
+					case _:
+						mergedSpans.push(span);
+				}
+			} else {
+				mergedSpans.push(span);
+			}
+		}
+		return mergedSpans;
 	}
 
 	public static function parseSpan(tagName: String, marker: String, styled: UnicodeString, start: Int) {
diff --git a/test/TestXEP0393.hx b/test/TestXEP0393.hx
index 75870af..757988b 100644
--- a/test/TestXEP0393.hx
+++ b/test/TestXEP0393.hx
@@ -2,6 +2,8 @@ package test;
 
 import utest.Assert;
 import utest.Async;
+
+import borogove.Stanza;
 import borogove.XEP0393;
 
 class TestXEP0393 extends utest.Test {
@@ -78,6 +80,33 @@ Who?")
 		);
 	}
 
+	public function testPlainSpanOneCdata() {
+		final node = XEP0393.parse("plain span")[0].children[0];
+		switch (node) {
+			case CData(t):
+				Assert.equals("plain span", t.content);
+			case _:
+				Assert.fail("Expected CData, but got " + node);
+		}
+	}
+
+	public function testMergedWithElement() {
+		final children = XEP0393.parse("plain *bold* plain")[0].children;
+		Assert.equals(3, children.length);
+		switch (children[0]) {
+			case CData(t): Assert.equals("plain ", t.content);
+			case _: Assert.fail("Expected CData");
+		}
+		switch (children[1]) {
+			case Element(s): Assert.equals("strong", s.name);
+			case _: Assert.fail("Expected Element");
+		}
+		switch (children[2]) {
+			case CData(t): Assert.equals(" plain", t.content);
+			case _: Assert.fail("Expected CData");
+		}
+	}
+
 	public function testStrongSpan() {
 		Assert.equals(
 			"<div><strong>strong span</strong></div>",