summaryrefslogtreecommitdiffstats
path: root/comm/chat/content/conversation-browser.js
blob: baa7f574473234c406d76c8c4f8cdd278d0f2ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/* global MozXULElement */

// Wrap in a block to prevent leaking to window scope.
{
  const LazyModules = {};
  ChromeUtils.defineESModuleGetters(LazyModules, {
    cleanupImMarkup: "resource:///modules/imContentSink.sys.mjs",
    getCurrentTheme: "resource:///modules/imThemes.sys.mjs",
    getDocumentFragmentFromHTML: "resource:///modules/imThemes.sys.mjs",
    getHTMLForMessage: "resource:///modules/imThemes.sys.mjs",
    initHTMLDocument: "resource:///modules/imThemes.sys.mjs",
    insertHTMLForMessage: "resource:///modules/imThemes.sys.mjs",
    isNextMessage: "resource:///modules/imThemes.sys.mjs",
    wasNextMessage: "resource:///modules/imThemes.sys.mjs",
    replaceHTMLForMessage: "resource:///modules/imThemes.sys.mjs",
    removeMessage: "resource:///modules/imThemes.sys.mjs",
    serializeSelection: "resource:///modules/imThemes.sys.mjs",
    smileTextNode: "resource:///modules/imSmileys.sys.mjs",
  });

  (function () {
    // <browser> is lazily set up through setElementCreationCallback,
    // i.e. put into customElements the first time it's really seen.
    // Create a fake to ensure browser exists in customElements, since otherwise
    // we can't extend it. Then make sure this fake doesn't stay around.
    if (!customElements.get("browser")) {
      delete document.createXULElement("browser");
    }
  })();

  /**
   * The chat conversation browser, i.e. the main content on the chat tab.
   *
   * @augments {MozBrowser}
   */
  class MozConversationBrowser extends customElements.get("browser") {
    constructor() {
      super();

      this._conv = null;

      // Make sure to load URLs externally.
      this.addEventListener("click", event => {
        // Right click should open the context menu.
        if (event.button == 2) {
          return;
        }

        // The 'click' event is fired even when the link is
        // activated with the keyboard.

        // The event target may be a descendant of the actual link.
        let url;
        for (let elem = event.target; elem; elem = elem.parentNode) {
          if (HTMLAnchorElement.isInstance(elem)) {
            url = elem.href;
            if (url) {
              break;
            }
          }
        }
        if (!url) {
          return;
        }

        let uri = Services.io.newURI(url);

        // http and https are the only schemes that are both
        // allowed by our IM filters and exposed.
        if (!uri.schemeIs("http") && !uri.schemeIs("https")) {
          return;
        }

        event.preventDefault();
        event.stopPropagation();

        // loadURI can throw if the default browser is misconfigured.
        Cc["@mozilla.org/uriloader/external-protocol-service;1"]
          .getService(Ci.nsIExternalProtocolService)
          .loadURI(uri);
      });

      this.addEventListener("keypress", event => {
        switch (event.keyCode) {
          case KeyEvent.DOM_VK_PAGE_UP: {
            if (event.shiftKey) {
              this.contentWindow.scrollByPages(-1);
            } else if (event.altKey) {
              this.scrollToPreviousSection();
            }
            break;
          }
          case KeyEvent.DOM_VK_PAGE_DOWN: {
            if (event.shiftKey) {
              this.contentWindow.scrollByPages(1);
            } else if (event.altKey) {
              this.scrollToNextSection();
            }
            break;
          }
          case KeyEvent.DOM_VK_HOME: {
            this.scrollToPreviousSection();
            event.preventDefault();
            break;
          }
          case KeyEvent.DOM_VK_END: {
            this.scrollToNextSection();
            event.preventDefault();
            break;
          }
        }
      });
    }

    connectedCallback() {
      if (this.hasConnected) {
        return;
      }
      this.hasConnected = true;

      super.connectedCallback();

      this._theme = null;

      this.autoCopyEnabled = false;

      this.magicCopyPref =
        "messenger.conversations.selections.magicCopyEnabled";

      this.magicCopyInitialized = false;

      this._destroyed = false;

      // Makes the chat browser scroll to the bottom automatically when we append
      // a new message. This behavior gets disabled when the user scrolls up to
      // look at the history, and we re-enable it when the user scrolls to
      // (within 10px) of the bottom.
      this._convScrollEnabled = true;

      this._textModifiers = [LazyModules.smileTextNode];

      // These variables are reset in onStateChange:
      this._lastMessage = null;
      this._lastMessageIsContext = true;
      this._firstNonContextElt = null;
      this._messageDisplayPending = false;
      this._pendingMessages = [];
      this._nextPendingMessageIndex = 0;
      this._pendingMessagesDisplayed = 0;
      this._displayPendingMessagesCalls = 0;
      this._sessions = [];

      this.progressBar = null;

      this.addEventListener("scroll", this.browserScroll);
      this.addEventListener("resize", this.browserResize);

      // @implements {nsIObserver}
      this.prefObserver = (subject, topic, data) => {
        if (this.magicCopyEnabled) {
          this.enableMagicCopy();
        } else {
          this.disableMagicCopy();
        }
      };

      // @implements {nsIController}
      this.copyController = {
        supportsCommand(command) {
          return command == "cmd_copy" || command == "cmd_cut";
        },
        isCommandEnabled: command => {
          return (
            command == "cmd_copy" &&
            !this.contentWindow.getSelection().isCollapsed
          );
        },
        doCommand: command => {
          let selection = this.contentWindow.getSelection();
          if (selection.isCollapsed) {
            return;
          }

          Cc["@mozilla.org/widget/clipboardhelper;1"]
            .getService(Ci.nsIClipboardHelper)
            .copyString(LazyModules.serializeSelection(selection));
        },
        onEvent(command) {},
        QueryInterface: ChromeUtils.generateQI(["nsIController"]),
      };

      // @implements {nsISelectionListener}
      this.chatSelectionListener = {
        notifySelectionChanged(document, selection, reason) {
          if (
            !(
              reason & Ci.nsISelectionListener.MOUSEUP_REASON ||
              reason & Ci.nsISelectionListener.SELECTALL_REASON ||
              reason & Ci.nsISelectionListener.KEYPRESS_REASON
            )
          ) {
            // We are still dragging, don't bother with the selection.
            return;
          }

          Cc["@mozilla.org/widget/clipboardhelper;1"]
            .getService(Ci.nsIClipboardHelper)
            .copyStringToClipboard(
              LazyModules.serializeSelection(selection),
              Ci.nsIClipboard.kSelectionClipboard
            );
        },
        QueryInterface: ChromeUtils.generateQI(["nsISelectionListener"]),
      };
    }

    init(conversation) {
      // Magic Copy may be initialized if the convbrowser is already
      // displaying a conversation.
      this.uninitMagicCopy();

      this._conv = conversation;

      // init is called when the message style preview is
      // reloaded so we need to reset _theme.
      this._theme = null;

      // Prevent ongoing asynchronous message display from continuing.
      this._messageDisplayPending = false;

      this.addEventListener(
        "load",
        () => {
          LazyModules.initHTMLDocument(
            this._conv,
            this.theme,
            this.contentDocument
          );

          this._exposeMethodsToContent();
          this.initMagicCopy();

          // We need to reset these variables here to avoid a race
          // condition if we are starting to display a new conversation
          // but the display of the previous conversation wasn't finished.
          // This can happen if the user quickly changes the selected
          // conversation in the log viewer.
          this._lastMessage = null;
          this._lastMessageIsContext = true;
          this._firstNonContextElt = null;
          this._messageDisplayPending = false;
          this._pendingMessages = [];
          this._nextPendingMessageIndex = 0;
          this._pendingMessagesDisplayed = 0;
          this._displayPendingMessagesCalls = 0;
          this._sessions = [];
          if (this.progressBar) {
            this.progressBar.hidden = true;
          }

          this.onChatNodeContentLoad = this.onContentElementLoad.bind(this);
          this.contentChatNode.addEventListener(
            "load",
            this.onChatNodeContentLoad,
            true
          );

          // Notify observers to get the conversation shown.
          Services.obs.notifyObservers(this, "conversation-loaded");
        },
        {
          once: true,
          capture: true,
        }
      );
      this.loadURI(Services.io.newURI("chrome://chat/content/conv.html"), {
        triggeringPrincipal:
          Services.scriptSecurityManager.getSystemPrincipal(),
      });
    }

    get theme() {
      return this._theme || (this._theme = LazyModules.getCurrentTheme());
    }

    get contentDocument() {
      return this.webNavigation.document;
    }

    get contentChatNode() {
      return this.contentDocument.getElementById("Chat");
    }

    get magicCopyEnabled() {
      return Services.prefs.getBoolPref(this.magicCopyPref);
    }

    enableMagicCopy() {
      this.contentWindow.controllers.insertControllerAt(0, this.copyController);
      this.autoCopyEnabled =
        Services.clipboard.isClipboardTypeSupported(
          Services.clipboard.kSelectionClipboard
        ) && Services.prefs.getBoolPref("clipboard.autocopy");
      if (this.autoCopyEnabled) {
        let selection = this.contentWindow.getSelection();
        if (selection) {
          selection.addSelectionListener(this.chatSelectionListener);
        }
      }
    }

    disableMagicCopy() {
      this.contentWindow.controllers.removeController(this.copyController);
      if (this.autoCopyEnabled) {
        let selection = this.contentWindow.getSelection();
        if (selection) {
          selection.removeSelectionListener(this.chatSelectionListener);
        }
      }
    }

    initMagicCopy() {
      if (this.magicCopyInitialized) {
        return;
      }
      Services.prefs.addObserver(this.magicCopyPref, this.prefObserver);
      this.magicCopyInitialized = true;
      if (this.magicCopyEnabled) {
        this.enableMagicCopy();
      }
    }

    uninitMagicCopy() {
      if (!this.magicCopyInitialized) {
        return;
      }
      Services.prefs.removeObserver(this.magicCopyPref, this.prefObserver);
      if (this.magicCopyEnabled) {
        this.disableMagicCopy();
      }
      this.magicCopyInitialized = false;
    }

    destroy() {
      super.destroy();
      if (this._destroyed) {
        return;
      }
      this._destroyed = true;
      this._messageDisplayPending = false;

      this.uninitMagicCopy();

      if (this.contentChatNode) {
        // Remove the listener only if the conversation was initialized.
        this.contentChatNode.removeEventListener(
          "load",
          this.onChatNodeContentLoad,
          true
        );
      }
    }

    _updateConvScrollEnabled() {
      // Enable auto-scroll if the scrollbar is at the bottom.
      let body = this.contentDocument.querySelector("body");
      this._convScrollEnabled =
        body.scrollHeight <= body.scrollTop + body.clientHeight + 10;
      return this._convScrollEnabled;
    }

    convScrollEnabled() {
      return this._convScrollEnabled || this._updateConvScrollEnabled();
    }

    _scrollToElement(aElt) {
      aElt.scrollIntoView(true);
      this._scrollingIntoView = true;
    }

    _exposeMethodsToContent() {
      // Expose scrollToElement and convScrollEnabled to the message styles.
      this.contentWindow.scrollToElement = this._scrollToElement.bind(this);
      this.contentWindow.convScrollEnabled = this.convScrollEnabled.bind(this);
    }

    addTextModifier(aModifier) {
      if (!this._textModifiers.includes(aModifier)) {
        this._textModifiers.push(aModifier);
      }
    }

    set isActive(value) {
      if (!value && !this.browsingContext) {
        return;
      }
      this.browsingContext.isActive = value;
      if (value && this._pendingMessages.length) {
        this.startDisplayingPendingMessages(false);
      }
    }

    appendMessage(aMsg, aContext, aFirstUnread) {
      this._pendingMessages.push({
        msg: aMsg,
        context: aContext,
        firstUnread: aFirstUnread,
      });
      if (this.browsingContext.isActive) {
        this.startDisplayingPendingMessages(true);
      }
    }

    /**
     * Replace an existing message in the conversation based on the remote ID.
     *
     * @param {imIMessage} msg - Message to use as replacement.
     */
    replaceMessage(msg) {
      if (!msg.remoteId) {
        // No remote id, nothing existing to replace.
        return;
      }
      if (this._messageDisplayPending || this._pendingMessages.length) {
        let pendingIndex = this._pendingMessages.findIndex(
          ({ msg: pendingMsg }) => pendingMsg.remoteId === msg.remoteId
        );
        if (
          pendingIndex > -1 &&
          pendingIndex >= this._nextPendingMessageIndex
        ) {
          this._pendingMessages[pendingIndex].msg = msg;
        }
      }
      if (this.browsingContext.isActive) {
        msg.message = this.prepareMessageContent(msg);
        const isNext = LazyModules.wasNextMessage(msg, this.contentDocument);
        const htmlMessage = LazyModules.getHTMLForMessage(
          msg,
          this.theme,
          isNext,
          false
        );
        let ruler = this.contentDocument.getElementById("unread-ruler");
        if (ruler?._originalMsg?.remoteId === msg.remoteId) {
          ruler._originalMsg = msg;
          ruler.nextMsgHtml = htmlMessage;
        }
        LazyModules.replaceHTMLForMessage(
          msg,
          htmlMessage,
          this.contentDocument,
          isNext
        );
      }
      if (this._lastMessage?.remoteId === msg.remoteId) {
        this._lastMessage = msg;
      }
    }

    /**
     * Remove an existing message in the conversation based on the remote ID.
     *
     * @param {string} remoteId - Remote ID of the message to remove.
     */
    removeMessage(remoteId) {
      if (this.browsingContext.isActive) {
        LazyModules.removeMessage(remoteId, this.contentDocument);
      }
      if (this._lastMessage?.remoteId === remoteId) {
        // Reset last message info if we removed the last message.
        this._lastMessage = null;
      }
    }

    startDisplayingPendingMessages(delayed) {
      if (this._messageDisplayPending) {
        return;
      }
      this._messageDisplayPending = true;
      this.contentWindow.messageInsertPending = true;
      if (delayed) {
        requestIdleCallback(this.displayPendingMessages.bind(this));
      } else {
        // 200ms here is a generous amount of time. The conversation switch
        // should take no more than 100ms to feel 'immediate', but the perceived
        // performance if we flicker is likely even worse than having a barely
        // perceptible delay.
        let deadline = Cu.now() + 200;
        this.displayPendingMessages({
          timeRemaining() {
            return deadline - Cu.now();
          },
        });
      }
    }

    // getNextPendingMessage and getPendingMessagesCount are the
    // only 2 methods accessing the this._pendingMessages array
    // directly during the chunked display of messages. It is
    // possible to override these 2 methods to replace the array
    // with something else. The log viewer for example uses an
    // enumerator that creates message objects lazily to avoid
    // jank when displaying lots of messages.
    getNextPendingMessage() {
      let length = this._pendingMessages.length;
      if (this._nextPendingMessageIndex == length) {
        return null;
      }

      let result = this._pendingMessages[this._nextPendingMessageIndex++];

      if (this._nextPendingMessageIndex == length) {
        this._pendingMessages = [];
        this._nextPendingMessageIndex = 0;
      }

      return result;
    }

    getPendingMessagesCount() {
      return this._pendingMessages.length;
    }

    displayPendingMessages(timing) {
      if (!this._messageDisplayPending) {
        return;
      }

      let max = this.getPendingMessagesCount();
      do {
        // One message takes less than 2ms on average.
        let msg = this.getNextPendingMessage();
        if (!msg) {
          break;
        }
        this.displayMessage(
          msg.msg,
          msg.context,
          ++this._pendingMessagesDisplayed < max,
          msg.firstUnread
        );
      } while (timing.timeRemaining() > 2);

      let event = document.createEvent("UIEvents");
      event.initUIEvent("MessagesDisplayed", false, false, window, 0);
      if (this._pendingMessagesDisplayed < max) {
        if (this.progressBar) {
          // Show progress bar if after the third call (ca. 120ms)
          // less than half the messages have been displayed.
          if (
            ++this._displayPendingMessagesCalls > 2 &&
            max > 2 * this._pendingMessagesDisplayed
          ) {
            this.progressBar.hidden = false;
          }
          this.progressBar.max = max;
          this.progressBar.value = this._pendingMessagesDisplayed;
        }
        requestIdleCallback(this.displayPendingMessages.bind(this));
        this.dispatchEvent(event);
        return;
      }
      this.contentWindow.messageInsertPending = false;
      this._messageDisplayPending = false;
      this._pendingMessagesDisplayed = 0;
      this._displayPendingMessagesCalls = 0;
      if (this.progressBar) {
        this.progressBar.hidden = true;
      }
      this.dispatchEvent(event);
    }

    displayMessage(aMsg, aContext, aNoAutoScroll, aFirstUnread) {
      let doc = this.contentDocument;

      if (aMsg.noLog && aMsg.notification && aMsg.who == "sessionstart") {
        // New session log.
        if (this._lastMessage) {
          let ruler = doc.createElement("hr");
          ruler.className = "sessionstart-ruler";
          this.contentChatNode.appendChild(ruler);
          this._sessions.push(ruler);
          // Close any open bubble.
          this._lastMessage = null;
        }
        // Suppress this message unless it was an error message.
        if (!aMsg.error) {
          return;
        }
      }

      if (aFirstUnread) {
        this.setUnreadRuler();
      }

      aMsg.message = this.prepareMessageContent(aMsg);

      let next =
        (aContext == this._lastMessageIsContext || aMsg.system) &&
        LazyModules.isNextMessage(this.theme, aMsg, this._lastMessage);
      let newElt;
      if (next && aFirstUnread) {
        // If there wasn't an unread ruler, this would be a Next message.
        // Therefore, save that version for later.
        let html = LazyModules.getHTMLForMessage(
          aMsg,
          this.theme,
          next,
          aContext
        );
        let ruler = doc.getElementById("unread-ruler");
        ruler.nextMsgHtml = html;
        ruler._originalMsg = aMsg;

        // Remember where the Next message(s) would have gone.
        let insert = doc.getElementById("insert");
        if (!insert) {
          insert = doc.createElement("div");
          ruler.parentNode.insertBefore(insert, ruler);
        }
        insert.id = "insert-before";

        next = false;
        html = LazyModules.getHTMLForMessage(aMsg, this.theme, next, aContext);
        newElt = LazyModules.insertHTMLForMessage(aMsg, html, doc, next);
        let marker = doc.createElement("div");
        marker.id = "end-of-split-block";
        newElt.parentNode.appendChild(marker);

        // Bracket the place where additional Next messages will be added,
        // if that's not after the end-of-split-block element.
        insert = doc.getElementById("insert");
        if (insert) {
          marker = doc.createElement("div");
          marker.id = "next-messages-start";
          insert.parentNode.insertBefore(marker, insert);
          marker = doc.createElement("div");
          marker.id = "next-messages-end";
          insert.parentNode.insertBefore(marker, insert.nextElementSibling);
        }
      } else {
        let html = LazyModules.getHTMLForMessage(
          aMsg,
          this.theme,
          next,
          aContext
        );
        newElt = LazyModules.insertHTMLForMessage(aMsg, html, doc, next);
      }

      if (!aNoAutoScroll) {
        newElt.getBoundingClientRect(); // avoid ireflow bugs
        if (this.convScrollEnabled()) {
          this._scrollToElement(newElt);
        }
      }
      this._lastElement = newElt;
      this._lastMessage = aMsg;
      if (!aContext && !this._firstNonContextElt && !aMsg.system) {
        this._firstNonContextElt = newElt;
      }
      this._lastMessageIsContext = aContext;
    }

    /**
     * Prepare the message text for display. Transforms plain text formatting
     * and removes any unwanted formatting.
     *
     * @param {imIMessage} message - Raw message.
     * @returns {string} Message content ready for insertion.
     */
    prepareMessageContent(message) {
      let cs = Cc["@mozilla.org/txttohtmlconv;1"].getService(
        Ci.mozITXTToHTMLConv
      );

      // kStructPhrase creates tags for plaintext-markup like *bold*,
      // /italics/, etc. We always use this; the content filter will
      // filter it out if the user does not want styling.
      let csFlags = cs.kStructPhrase;
      // Automatically find and link freetext URLs
      if (!message.noLinkification) {
        csFlags |= cs.kURLs;
      }

      // Right trim before displaying. This removes any OTR related
      // whitespace when the extension isn't enabled.
      let msg = message.displayMessage?.trimRight() ?? "";
      msg = cs
        .scanHTML(msg.replace(/&/g, "FROM-DTD-amp"), csFlags)
        .replace(/FROM-DTD-amp/g, "&");

      return LazyModules.cleanupImMarkup(
        msg.replace(/\r?\n/g, "<br/>"),
        null,
        this._textModifiers
      );
    }

    setUnreadRuler() {
      // Remove any existing ruler (occurs when the window has lost focus).
      this.removeUnreadRuler();

      let ruler = this.contentDocument.createElement("hr");
      ruler.id = "unread-ruler";
      this.contentChatNode.appendChild(ruler);
    }

    removeUnreadRuler() {
      if (this._lastMessage) {
        this._lastMessage.whenRead();
      }

      let doc = this.contentDocument;
      let ruler = doc.getElementById("unread-ruler");
      if (!ruler) {
        return;
      }

      // If a message block was split by the ruler, rejoin it.
      let moveTo = doc.getElementById("insert-before");
      if (moveTo) {
        // Protect an existing insert node.
        let actualInsert = doc.getElementById("insert");
        if (actualInsert) {
          actualInsert.id = "actual-insert";
        }

        // Add first message following the ruler as a Next type message.
        // Replicates the relevant parts of insertHTMLForMessage().
        let range = doc.createRange();
        let moveToParent = moveTo.parentNode;
        range.selectNode(moveToParent);
        // eslint-disable-next-line no-unsanitized/method
        let documentFragment = LazyModules.getDocumentFragmentFromHTML(
          doc,
          ruler.nextMsgHtml
        );
        for (
          let root = documentFragment.firstElementChild;
          root;
          root = root.nextElementSibling
        ) {
          root._originalMsg = ruler._originalMsg;
          root.dataset.remoteId = ruler._originalMsg.remoteId;
        }
        moveToParent.insertBefore(documentFragment, moveTo);

        // If this added an insert node, insert the next messages there.
        let insert = doc.getElementById("insert");
        if (insert) {
          moveTo.remove();
          moveTo = insert;
          moveToParent = moveTo.parentNode;
        }

        // Move remaining messages from the message block following the ruler.
        let nextMessagesStart = doc.getElementById("next-messages-start");
        if (nextMessagesStart) {
          range = doc.createRange();
          range.setStartAfter(nextMessagesStart);
          range.setEndBefore(doc.getElementById("next-messages-end"));
          moveToParent.insertBefore(range.extractContents(), moveTo);
        }
        moveTo.remove();

        // Restore existing insert node.
        if (actualInsert) {
          actualInsert.id = "insert";
        }

        // Delete surplus message block.
        range = doc.createRange();
        range.setStartAfter(ruler);
        range.setEndAfter(doc.getElementById("end-of-split-block"));
        range.deleteContents();
      }
      ruler.remove();
    }

    _getSections() {
      // If a section is displayed below this point, we assume not enough of
      // it is visible, so we must scroll to it.
      // The 3/4 constant is arbitrary, but it has to be greater than 1/2.
      this._maximalSectionOffset = Math.round((this.clientHeight * 3) / 4);

      // Get list of current section elements.
      let sectionElements = [];
      if (this._firstNonContextElt) {
        sectionElements.push(this._firstNonContextElt);
      }
      let ruler = this.contentDocument.getElementById("unread-ruler");
      if (ruler) {
        sectionElements.push(ruler);
      }
      sectionElements = sectionElements.concat(this._sessions);

      // Return ordered array of sections with entries
      // [Y, scrollY such that Y is centered]
      let sections = [];
      let maxY = this.contentWindow.scrollMaxY;
      for (let i = 0; i < sectionElements.length; ++i) {
        let y = sectionElements[i].offsetTop;
        // The section is unnecessary if close to top/bottom of conversation.
        if (y < this._maximalSectionOffset || maxY < y) {
          continue;
        }
        sections.push([y, y - Math.round(this.clientHeight / 2)]);
      }
      sections.sort((a, b) => a[0] - b[0]);
      return sections;
    }

    scrollToPreviousSection() {
      let sections = this._getSections();
      let y = this.contentWindow.scrollY;
      let newY = 0;
      for (let i = sections.length - 1; i >= 0; --i) {
        let section = sections[i];
        if (y > section[0]) {
          newY = section[1];
          break;
        }
      }
      this.contentWindow.scrollTo(0, newY);
    }

    scrollToNextSection() {
      let sections = this._getSections();
      let y = this.contentWindow.scrollY;
      let newY = this.contentWindow.scrollMaxY;
      for (let i = 0; i < sections.length; ++i) {
        let section = sections[i];
        if (y + this._maximalSectionOffset < section[0]) {
          newY = section[1];
          break;
        }
      }
      this.contentWindow.scrollTo(0, newY);
    }

    browserScroll(event) {
      if (this._scrollingIntoView) {
        // We have explicitly requested a scrollIntoView, ignore the event.
        this._scrollingIntoView = false;
        this._lastScrollHeight = this.scrollHeight;
        this._lastScrollWidth = this.scrollWidth;
        return;
      }

      if (
        !("_lastScrollHeight" in this) ||
        this._lastScrollHeight != this.scrollHeight ||
        this._lastScrollWidth != this.scrollWidth
      ) {
        // Ensure scroll events triggered by a change of the
        // content area size (eg. resizing the window or moving the
        // textbox splitter) don't affect the auto-scroll behavior.
        this._lastScrollHeight = this.scrollHeight;
        this._lastScrollWidth = this.scrollWidth;
      }

      // If images higher than one line of text load they will trigger a
      // scroll event, which shouldn't disable auto-scroll while messages
      // are being appended without being scrolled.
      if (this._messageDisplayPending) {
        return;
      }

      // Enable or disable auto-scroll based on the scrollbar position.
      this._updateConvScrollEnabled();
    }

    browserResize(event) {
      if (this._convScrollEnabled && this._lastElement) {
        // The content area was resized and auto-scroll is enabled,
        // make sure the last inserted element is still visible
        this._scrollToElement(this._lastElement);
      }
    }

    onContentElementLoad(event) {
      if (
        event.target.localName == "img" &&
        this._convScrollEnabled &&
        !this._messageDisplayPending &&
        this._lastElement
      ) {
        // An image loaded while auto-scroll is enabled and no further
        // messages are currently being appended. So we need to scroll
        // the last element fully back into view.
        this._scrollToElement(this._lastElement);
      }
    }
  }
  customElements.define("conversation-browser", MozConversationBrowser, {
    extends: "browser",
  });
}