QMacMimeRtfText: Remove stray newline when converting RTF to HTML

NSAttributedString wrongly inserts a newline at the end of the string
when generating HTML from RTF. The rules for HTML parsing specify that
this newline, after the closing </html> tag, should be treated the same
way as if it had been part of the body.

  - https://html.spec.whatwg.org/multipage/syntax.html#start-tags
  - https://html.spec.whatwg.org/multipage/parsing.html#the-after-after-body-insertion-mode

This results in an additional space character if the HTML is converted
to a QTextDocument by QTextHtmlParser, for example when inserting text
from another application via the clipboard or drag-and-drop.

Pick-to: 6.5 6.2 5.15
Change-Id: If17cfcdc9ad5452e7b516fc6b50d834bd5184e23
Reviewed-by: Michael Brüning <michael.bruning@qt.io>
(cherry picked from commit 3dceef8e23787d3aded207e4f6ca554de4a1c2d4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2024-08-12 10:52:50 +02:00 committed by Qt Cherry-pick Bot
parent eeb0740f2e
commit 7fa3737552

View File

@ -505,7 +505,12 @@ QMacMimeRtfText::convertToMime(const QString &mimeType,
NSRange range = NSMakeRange(0, [string length]);
NSDictionary *dict = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *htmlData = [string dataFromRange:range documentAttributes:dict error:&error];
return QByteArray::fromNSData(htmlData);
// Note: We trim the data here, as NSAttributedString wrongly inserts a newline at the
// end when generating HTML, which HTML parsers (including our own QTextHtmlParser)
// correctly treat as an additional white space in the body of the HTML document
// (see HTML5 § 13.2.6.4.22 - The "after after body" insertion mode).
return QByteArray::fromNSData(htmlData).trimmed();
}
QList<QByteArray>