From 7fa3737552dd0d6c56c271159a30556997241d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 12 Aug 2024 10:52:50 +0200 Subject: [PATCH] QMacMimeRtfText: Remove stray newline when converting RTF to HTML MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 (cherry picked from commit 3dceef8e23787d3aded207e4f6ca554de4a1c2d4) Reviewed-by: Qt Cherry-pick Bot --- src/gui/platform/darwin/qutimimeconverter.mm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gui/platform/darwin/qutimimeconverter.mm b/src/gui/platform/darwin/qutimimeconverter.mm index ee643fd0c61..294a617f3b0 100644 --- a/src/gui/platform/darwin/qutimimeconverter.mm +++ b/src/gui/platform/darwin/qutimimeconverter.mm @@ -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