commit b1895729f74be92a1ea60fbae0f63df0a433f326
parent ac3a9e1efa2105f04dc1ce06530cf313fe72196e
Author: Jonathan Mendez <jmendez@mozilla.com>
Date: Fri, 7 Nov 2025 23:27:37 +0000
Bug 1953524 - Check if leading/trailing text is all whitespace to avoid adding misleading ellipsis. r=emilio,layout-reviewers
Code taken from nsHTMLCopyEncoder::GetPromotedPoint
Differential Revision: https://phabricator.services.mozilla.com/D271616
Diffstat:
3 files changed, 38 insertions(+), 4 deletions(-)
diff --git a/layout/printing/nsPrintJob.cpp b/layout/printing/nsPrintJob.cpp
@@ -1598,8 +1598,14 @@ void SelectionRangeState::SelectNodesExceptInSubtree(const Position& aStart,
if (auto* text = Text::FromNode(aStart.mNode)) {
if (start.mNode != text && aStart.mOffset &&
aStart.mOffset < text->Length()) {
- text->InsertData(aStart.mOffset, kEllipsis, IgnoreErrors());
- ellipsizedStart = true;
+ // Only insert ellipsis if there is any non-whitespace prior to selection.
+ nsAutoString leadingText;
+ text->SubstringData(0, aStart.mOffset, leadingText, IgnoreErrors());
+ leadingText.CompressWhitespace();
+ if (!leadingText.IsEmpty()) {
+ text->InsertData(aStart.mOffset, kEllipsis, IgnoreErrors());
+ ellipsizedStart = true;
+ }
}
}
@@ -1621,8 +1627,15 @@ void SelectionRangeState::SelectNodesExceptInSubtree(const Position& aStart,
// If the end is mid text then add an ellipsis.
if (auto* text = Text::FromNode(start.mNode)) {
if (start.mOffset && start.mOffset < text->Length()) {
- text->InsertData(start.mOffset, kEllipsis, IgnoreErrors());
- start.mOffset += kEllipsis.Length();
+ // Only insert ellipsis if there is any non-whitespace after selection.
+ nsAutoString trailingText;
+ text->SubstringData(start.mOffset, text->Length() - start.mOffset,
+ trailingText, IgnoreErrors());
+ trailingText.CompressWhitespace();
+ if (!trailingText.IsEmpty()) {
+ text->InsertData(start.mOffset, kEllipsis, IgnoreErrors());
+ start.mOffset += kEllipsis.Length();
+ }
}
}
}
diff --git a/layout/reftests/printing/reftest.list b/layout/reftests/printing/reftest.list
@@ -35,3 +35,4 @@ print test-print-selection-shadow-crossing-flat-6.html test-print-selection-shad
print test-print-selection-shadow-crossing-flat-7.html test-print-selection-shadow-crossing-flat-7-ref.html
print test-print-selection-shadow-crossing-flat-8.html test-print-selection-shadow-crossing-flat-8-ref.html
print test-print-selection-shadow-crossing-flat-9.html test-print-selection-shadow-crossing-flat-9-ref.html
+print test-print-selection-leading-trailing-whitespace.html test-text-ref.html
diff --git a/layout/reftests/printing/test-print-selection-leading-trailing-whitespace.html b/layout/reftests/printing/test-print-selection-leading-trailing-whitespace.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html reftest-print-range="selection">
+<head>
+ <meta charset="utf-8">
+ <link href="print.css" rel="stylesheet">
+ <script>
+ function selectNonWhitespaceText() {
+ window.getSelection()
+ .setBaseAndExtent(
+ selection.firstChild,
+ 2,
+ selection.firstChild,
+ 35);
+ }
+ </script>
+</head>
+<body onload="selectNonWhitespaceText()">
+ <p id="selection"> This text should appear on page 1 </p>
+</body>
+</html>