diff --git a/dom/serializers/nsPlainTextSerializer.cpp b/dom/serializers/nsPlainTextSerializer.cpp index f64dd902bd5f..3cc7489062cb 100644 --- a/dom/serializers/nsPlainTextSerializer.cpp +++ b/dom/serializers/nsPlainTextSerializer.cpp @@ -131,7 +131,19 @@ int32_t nsPlainTextSerializer::CurrentLine::FindWrapIndexForContent( // mContent until we find a width less than or equal to wrap column. uint32_t width = 0; intl::LineBreakIteratorUtf16 lineBreakIter(mContent); - while (const Maybe nextGoodSpace = lineBreakIter.Next()) { + while (Maybe nextGoodSpace = lineBreakIter.Next()) { + // Trim space at the tail. UAX#14 doesn't have break opportunity for + // ASCII space at the tail. + const Maybe originalNextGoodSpace = nextGoodSpace; + while (*nextGoodSpace > 0 && + mContent.CharAt(*nextGoodSpace - 1) == 0x20) { + nextGoodSpace = Some(*nextGoodSpace - 1); + } + if (*nextGoodSpace == 0) { + // Restore the original nextGoodSpace. + nextGoodSpace = originalNextGoodSpace; + } + width += GetUnicharStringWidth(Span( mContent.get() + goodSpace, *nextGoodSpace - goodSpace)); if (prefixwidth + width > aWrapColumn) { diff --git a/dom/serializers/nsXMLContentSerializer.cpp b/dom/serializers/nsXMLContentSerializer.cpp index 08cf7fda56a3..ab0fcdf4135b 100644 --- a/dom/serializers/nsXMLContentSerializer.cpp +++ b/dom/serializers/nsXMLContentSerializer.cpp @@ -1560,6 +1560,19 @@ bool nsXMLContentSerializer::AppendWrapped_NonWhitespaceSequence( MOZ_ASSERT(nextWrapPosition.isSome(), "We should've exited the loop when reaching the end of " "text in the previous iteration!"); + + // Trim space at the tail. UAX#14 doesn't have break opportunity + // for ASCII space at the tail. + const Maybe originalNextWrapPosition = nextWrapPosition; + while (*nextWrapPosition > 0 && + subSeq.at(*nextWrapPosition - 1) == 0x20) { + nextWrapPosition = Some(*nextWrapPosition - 1); + } + if (*nextWrapPosition == 0) { + // Restore the original nextWrapPosition. + nextWrapPosition = originalNextWrapPosition; + } + if (aSequenceStart + *nextWrapPosition > aPos) { break; }