Browse Source

room-history: Ignore empty inline elements when ellipsizing to a single line

merge-requests/2003/merge
Kévin Commaille 10 months ago
parent
commit
4a5a4b4cde
No known key found for this signature in database
GPG Key ID: F26F4BE20A08255B
  1. 9
      src/session/view/content/room_history/message_row/text/inline_html.rs
  2. 6
      src/session/view/content/room_history/message_row/text/tests.rs
  3. 19
      src/session/view/content/room_history/message_row/text/widgets.rs

9
src/session/view/content/room_history/message_row/text/inline_html.rs

@ -75,7 +75,10 @@ impl<'a> InlineHtmlBuilder<'a> {
/// constructed, if any.
pub(super) fn build(self) -> (String, Option<Vec<Pill>>) {
let mut inner = self.inner;
let ellipsis = self.ellipsis | self.truncated;
// Do not add an ellipsis on an empty inline element, we just want to get rid of
// it.
let ellipsis = (self.ellipsis && !inner.is_empty()) | self.truncated;
if ellipsis {
inner.append_ellipsis();
@ -222,8 +225,8 @@ impl<'a> InlineHtmlBuilder<'a> {
/// Append the given text node content.
fn append_text_node(&mut self, text: &str, context: NodeContext) {
// Remove spaces at the beginning and end of an HTML element, and after a
// newline.
// Collapse whitespaces and remove them at the beginning and end of an HTML
// element, and after a newline.
let text = text.collapse_whitespaces(
context.is_first_child || self.inner.ends_with('\n'),
context.is_last_child,

6
src/session/view/content/room_history/message_row/text/tests.rs

@ -18,6 +18,12 @@ fn single_line() {
assert_eq!(s, "A simple text…");
assert!(pills.is_none());
let html = Html::parse("\nThis is a paragraph<br />\n\nThis is another paragraph\n");
let (s, pills) = InlineHtmlBuilder::new(true, false).build_with_nodes(html.children());
assert_eq!(s, "This is a paragraph…");
assert!(pills.is_none());
}
#[test]

19
src/session/view/content/room_history/message_row/text/widgets.rs

@ -64,13 +64,15 @@ pub(super) fn widget_for_html_nodes(
let is_last = i == (len - 1);
let add_ellipsis = add_ellipsis || (config.ellipsize && !is_last);
match group {
let widget = match group {
NodeGroup::Inline(inline_nodes) => {
if let Some(widget) =
let Some(widget) =
label_for_inline_html(inline_nodes, config, add_ellipsis, sender_name)
{
children.push(widget);
}
else {
continue;
};
widget
}
NodeGroup::Block(block_node) => {
let Some(widget) =
@ -90,9 +92,11 @@ pub(super) fn widget_for_html_nodes(
children.push(label.upcast());
}
children.push(widget);
widget
}
}
};
children.push(widget);
if config.ellipsize {
// Stop at the first constructed child.
@ -121,6 +125,7 @@ pub(super) fn widget_for_html_nodes(
}
/// A group of nodes, representing the nodes contained in a single widget.
#[derive(Debug)]
enum NodeGroup {
/// A group of inline nodes.
Inline(Vec<NodeRef>),

Loading…
Cancel
Save