[fractal] room-history: Fix parsing of nested mentions



commit 7e212947feddb7b099dc515e0286fcd7489e87d3
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Sun Oct 9 11:36:51 2022 +0200

    room-history: Fix parsing of nested mentions

 src/session/content/room_history/message_row/text.rs | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
---
diff --git a/src/session/content/room_history/message_row/text.rs 
b/src/session/content/room_history/message_row/text.rs
index 99ac6be24..6951a1d6c 100644
--- a/src/session/content/room_history/message_row/text.rs
+++ b/src/session/content/room_history/message_row/text.rs
@@ -357,13 +357,13 @@ fn strip_reply(text: &str) -> &str {
 fn extract_mentions(s: &str, room: &Room) -> (String, Vec<Pill>) {
     let session = room.session();
     let mut label = s.to_owned();
-    let mut widgets = vec![];
+    let mut widgets: Vec<(usize, usize, Pill)> = vec![];
 
     // The markup has been normalized by html2pango so we are sure of the format of
     // links.
     for (start, _) in s.rmatch_indices("<a href=") {
         let uri_start = start + 9;
-        let link = &s[uri_start..];
+        let link = &label[uri_start..];
 
         let uri_end = if let Some(end) = link.find('"') {
             end
@@ -415,10 +415,18 @@ fn extract_mentions(s: &str, room: &Room) -> (String, Vec<Pill>) {
             continue;
         };
 
+        // Remove nested Pills. Only occurs with nested links in invalid HTML.
+        widgets = widgets
+            .into_iter()
+            .filter(|(w_start, ..)| end < *w_start)
+            .collect();
+
+        widgets.insert(0, (start, end, pill));
         label.replace_range(start..end, DEFAULT_PLACEHOLDER);
-        widgets.insert(0, pill);
     }
 
+    let widgets = widgets.into_iter().map(|(_, _, widget)| widget).collect();
+
     (label, widgets)
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]