branch: elpa/jabber
commit a7fb5a827bddc084484a8fd02718dc42b870a542
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    chat: Match reply fallback by for attribute, not first in namespace
---
 lisp/jabber-chat.el       | 18 +++++++++++++-----
 tests/jabber-test-chat.el | 17 +++++++++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/lisp/jabber-chat.el b/lisp/jabber-chat.el
index 37e7c2a223..87e62708f8 100644
--- a/lisp/jabber-chat.el
+++ b/lisp/jabber-chat.el
@@ -1005,16 +1005,24 @@ Returns a list of (URL . DESC) cons cells, or nil."
        (string-match-p "\\`[0-9]+\\'" value)
        (string-to-number value)))
 
+(defun jabber-chat--reply-fallback-element (xml-data)
+  "Return the XEP-0428 <fallback/> element for replies in XML-DATA.
+A stanza may carry several fallback elements with different `for'
+attributes; a non-reply one must not mask the reply one."
+  (seq-find
+   (lambda (child)
+     (and (eq (jabber-xml-node-name child) 'fallback)
+          (equal (jabber-xml-get-xmlns child) jabber-chat--fallback-xmlns)
+          (equal (jabber-xml-get-attribute child 'for)
+                 jabber-chat--reply-xmlns)))
+   (jabber-xml-node-children xml-data)))
+
 (defun jabber-chat--reply-fallback-range (xml-data)
   "Return the XEP-0461 fallback body range in XML-DATA.
 Return `all' when the fallback applies to the whole body: no <body/>
 child, or one without offsets (XEP-0428: missing start/end attributes
 mean the entire element; Dino emits bare <body/> for full quotes)."
-  (when-let* ((fallback (jabber-xml-child-with-xmlns
-                         xml-data jabber-chat--fallback-xmlns))
-              ((eq (jabber-xml-node-name fallback) 'fallback))
-              ((string= (jabber-xml-get-attribute fallback 'for)
-                        jabber-chat--reply-xmlns)))
+  (when-let* ((fallback (jabber-chat--reply-fallback-element xml-data)))
     (if-let* ((body (car (jabber-xml-get-children fallback 'body))))
         (let ((start (jabber-xml-get-attribute body 'start))
               (end (jabber-xml-get-attribute body 'end)))
diff --git a/tests/jabber-test-chat.el b/tests/jabber-test-chat.el
index bc589e5560..c3423c0e0c 100644
--- a/tests/jabber-test-chat.el
+++ b/tests/jabber-test-chat.el
@@ -261,6 +261,23 @@
          (plist (jabber-chat--msg-plist-from-stanza stanza)))
     (should-not (plist-get plist :fallback-range))))
 
+(ert-deftest jabber-test-chat-plist-reply-fallback-not-masked ()
+  "A non-reply <fallback> before the reply one must not mask it."
+  (let* ((stanza '(message ((from . "[email protected]/phone")
+                            (type . "chat"))
+                           (body () "> Alice:\n> Hello\nanswer")
+                           (reply ((xmlns . "urn:xmpp:reply:0")
+                                   (to . "[email protected]/phone")
+                                   (id . "orig-1")))
+                           (fallback ((xmlns . "urn:xmpp:fallback:0")
+                                      (for . "urn:xmpp:reactions:0")))
+                           (fallback ((xmlns . "urn:xmpp:fallback:0")
+                                      (for . "urn:xmpp:reply:0"))
+                                     (body ((start . "0")
+                                            (end . "17"))))))
+         (plist (jabber-chat--msg-plist-from-stanza stanza)))
+    (should (equal '(0 17) (plist-get plist :fallback-range)))))
+
 ;;; Group 2: jabber-chat--oob-field
 
 (ert-deftest jabber-test-chat-oob-field-url ()

Reply via email to