From 97dc744db3eadcb1b895f19f3d0977b5bb503caf Mon Sep 17 00:00:00 2001
From: SpiritCroc <dev@spiritcroc.de>
Date: Tue, 5 Sep 2023 23:10:00 +0200
Subject: [PATCH] Fix message render failures caused by Element's ListHandler

Fixes https://github.com/SchildiChat/SchildiChat-android/issues/206

On repeated render, we sometimes get
`java.lang.NumberFormatException: For input string: ""`

Note: this doesn't necessarily happen on messages with lists, but any,
not sure when...

Change-Id: Icaa5b505c6f8a732d0a04378e0872e82ae40e16d
---
 .../features/html/ListHandlerWithInitialStart.java | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/html/ListHandlerWithInitialStart.java b/vector/src/main/java/im/vector/app/features/html/ListHandlerWithInitialStart.java
index c7ba881da0..0ae8f8592c 100644
--- a/vector/src/main/java/im/vector/app/features/html/ListHandlerWithInitialStart.java
+++ b/vector/src/main/java/im/vector/app/features/html/ListHandlerWithInitialStart.java
@@ -32,6 +32,7 @@ import io.noties.markwon.core.CoreProps;
 import io.noties.markwon.html.HtmlTag;
 import io.noties.markwon.html.MarkwonHtmlRenderer;
 import io.noties.markwon.html.TagHandler;
+import timber.log.Timber;
 
 /**
  * Copied from https://github.com/noties/Markwon/blob/master/markwon-html/src/main/java/io/noties/markwon/html/tag/ListHandler.java#L44
@@ -63,8 +64,17 @@ public class ListHandlerWithInitialStart extends TagHandler {
         final RenderProps renderProps = visitor.renderProps();
         final SpanFactory spanFactory = configuration.spansFactory().get(ListItem.class);
 
-        // Modified line
-        int number = Integer.parseInt(block.attributes().containsKey(START_KEY) ? block.attributes().get(START_KEY) : "1");
+        // Modified part start
+        int number;
+        try {
+            number = Integer.parseInt(block.attributes().containsKey(START_KEY) ? block.attributes().get(START_KEY) : "1");
+        } catch (Exception e) {
+            // On repeated render, we get `java.lang.NumberFormatException: For input string: ""`
+            // See also https://github.com/SchildiChat/SchildiChat-android/issues/206
+            //Timber.v(e, "Failed to read list start");
+            number = 1;
+        }
+        // Modified part end
 
         final int bulletLevel = currentBulletListLevel(block);