diff options
author | Ralf Habacker <[email protected]> | 2021-11-17 13:54:32 +0100 |
---|---|---|
committer | Shawn Rutledge <[email protected]> | 2021-12-15 11:36:37 +0000 |
commit | 2e2f1e2af76d94a41f382e81ab8a6fdccf8ca579 (patch) | |
tree | ead0f36bf1d959a1cf71f7af538de513bad9f30e | |
parent | ff97c8164253a882037836ae4381f6e669d3fce5 (diff) |
Add css media rule support for QTextDocument::setHtml()
CSS styles can contain '@media <rule> {...}' blocks, which were
previously ignored for all values except "screen".
To use a media rule other than the default "screen" rule,
specify it before calling setHtml() with setMetaInformation()
and the new info value 'CssMedia'.
[ChangeLog][Gui][QTextDocument] Add css media rule support
for QTextDocument::setHtml()
Pick-to: 6.3
Fixes: QTBUG-98408
Change-Id: Ie05f815a6dedbd970210f467e26b116f6ee3b9ca
Reviewed-by: Shawn Rutledge <[email protected]>
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 16 | ||||
-rw-r--r-- | src/gui/text/qtextdocument.h | 3 | ||||
-rw-r--r-- | src/gui/text/qtextdocument_p.cpp | 3 | ||||
-rw-r--r-- | src/gui/text/qtextdocument_p.h | 1 | ||||
-rw-r--r-- | src/gui/text/qtexthtmlparser.cpp | 2 | ||||
-rw-r--r-- | tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp | 38 |
6 files changed, 58 insertions, 5 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 68cdbc092e3..0fed8c98d3d 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -351,6 +351,7 @@ QTextDocument *QTextDocument::clone(QObject *parent) const QTextDocumentPrivate *priv = doc->d_func(); priv->title = d->title; priv->url = d->url; + priv->cssMedia = d->cssMedia; priv->pageSize = d->pageSize; priv->indentWidth = d->indentWidth; priv->defaultTextOption = d->defaultTextOption; @@ -1135,6 +1136,8 @@ QString QTextDocument::metaInformation(MetaInformation info) const return d->title; case DocumentUrl: return d->url; + case CssMedia: + return d->cssMedia; } return QString(); } @@ -1155,6 +1158,9 @@ void QTextDocument::setMetaInformation(MetaInformation info, const QString &stri case DocumentUrl: d->url = string; break; + case CssMedia: + d->cssMedia = string; + break; } } @@ -1242,11 +1248,14 @@ void QTextDocument::setPlainText(const QString &text) "<b>bold</b> text" will produce text where the first word has a font weight that gives it a bold appearance: "\b{bold} text". + To select a css media rule other than the default "screen" rule, + use setMetaInformation() with 'CssMedia' as "info" parameter. + \note It is the responsibility of the caller to make sure that the text is correctly decoded when a QString containing HTML is created and passed to setHtml(). - \sa setPlainText(), {Supported HTML Subset} + \sa setPlainText(), {Supported HTML Subset}, setMetaInformation() */ #ifndef QT_NO_TEXTHTMLPARSER @@ -1286,8 +1295,11 @@ void QTextDocument::setHtml(const QString &html) \value DocumentTitle The title of the document. \value DocumentUrl The url of the document. The loadResource() function uses this url as the base when loading relative resources. + \value CssMedia This value is used to select the corresponding '@media' + rule, if any, from a specified CSS stylesheet when setHtml() + is called. This enum value has been introduced in Qt 6.3. - \sa metaInformation(), setMetaInformation() + \sa metaInformation(), setMetaInformation(), setHtml() */ static bool findInBlock(const QTextBlock &block, const QString &expression, int offset, diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index dc4d15f3a88..4324c15643c 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -136,7 +136,8 @@ public: enum MetaInformation { DocumentTitle, - DocumentUrl + DocumentUrl, + CssMedia }; void setMetaInformation(MetaInformation info, const QString &); QString metaInformation(MetaInformation info) const; diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp index f73475226fd..01e73dd66f3 100644 --- a/src/gui/text/qtextdocument_p.cpp +++ b/src/gui/text/qtextdocument_p.cpp @@ -187,7 +187,8 @@ QTextDocumentPrivate::QTextDocumentPrivate() framesDirty(true), rtFrame(nullptr), initialBlockCharFormatIndex(-1), // set correctly later in init() - resourceProvider(nullptr) + resourceProvider(nullptr), + cssMedia(QStringLiteral("screen")) { editBlock = 0; editBlockCursorPosition = -1; diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index b26d7657ec9..2b26897017d 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -384,6 +384,7 @@ public: QSizeF pageSize; QString title; QString url; + QString cssMedia; qreal indentWidth; qreal documentMargin; QUrl baseUrl; diff --git a/src/gui/text/qtexthtmlparser.cpp b/src/gui/text/qtexthtmlparser.cpp index ae2550b69ef..85877bfe475 100644 --- a/src/gui/text/qtexthtmlparser.cpp +++ b/src/gui/text/qtexthtmlparser.cpp @@ -2165,7 +2165,7 @@ QList<QCss::Declaration> QTextHtmlParser::declarationsForNode(int node) const for (int i = 0; i < inlineStyleSheets.count(); ++i, ++idx) selector.styleSheets[idx] = inlineStyleSheets.at(i); - selector.medium = QLatin1String("screen"); + selector.medium = resourceProvider ? resourceProvider->metaInformation(QTextDocument::CssMedia) : QLatin1String("screen"); QCss::StyleSelector::NodePtr n; n.id = node; diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 922226a3a3c..a38defa6562 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -96,6 +96,7 @@ private slots: void toHtml2(); void setFragmentMarkersInHtmlExport(); + void setMediaRule(); void toHtmlBodyBgColor(); void toHtmlBodyBgColorRgba(); @@ -1910,6 +1911,39 @@ void tst_QTextDocument::setFragmentMarkersInHtmlExport() } } +void tst_QTextDocument::setMediaRule() +{ + { + CREATE_DOC_AND_CURSOR(); + doc.setDefaultStyleSheet("@media screen { p { background:#000000 } } @media print { p { background:#ffffff } }"); + doc.setHtml("<p>Hello World</p>"); + + QString expected = htmlHead; + expected += QString("<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#000000;\"><span style=\" background-color:#000000;\">Hello World</span></p>") + htmlTail; + QCOMPARE(doc.toHtml(), expected); + } + { + CREATE_DOC_AND_CURSOR(); + doc.setDefaultStyleSheet("@media screen { p { background:#000000 } } @media print { p { background:#ffffff } }"); + doc.setMetaInformation(QTextDocument::CssMedia, "screen"); + doc.setHtml("<p>Hello World</p>"); + + QString expected = htmlHead; + expected += QString("<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#000000;\"><span style=\" background-color:#000000;\">Hello World</span></p>") + htmlTail; + QCOMPARE(doc.toHtml(), expected); + } + { + CREATE_DOC_AND_CURSOR(); + doc.setDefaultStyleSheet("@media screen { p { background:#000000 } } @media print { p { background:#ffffff } }"); + doc.setMetaInformation(QTextDocument::CssMedia, "print"); + doc.setHtml("<p>Hello World</p>"); + + QString expected = htmlHead; + expected += QString("<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; background-color:#ffffff;\"><span style=\" background-color:#ffffff;\">Hello World</span></p>") + htmlTail; + QCOMPARE(doc.toHtml(), expected); + } +} + void tst_QTextDocument::toHtmlBodyBgColor() { CREATE_DOC_AND_CURSOR(); @@ -2294,14 +2328,18 @@ void tst_QTextDocument::clonePreservesMetaInformation() { const QString title("Foobar"); const QString url("about:blank"); + const QString media("print"); doc->setHtml("<html><head><title>" + title + "</title></head><body>Hrm</body></html>"); doc->setMetaInformation(QTextDocument::DocumentUrl, url); + doc->setMetaInformation(QTextDocument::CssMedia, media); QCOMPARE(doc->metaInformation(QTextDocument::DocumentTitle), title); QCOMPARE(doc->metaInformation(QTextDocument::DocumentUrl), url); + QCOMPARE(doc->metaInformation(QTextDocument::CssMedia), media); QTextDocument *clone = doc->clone(); QCOMPARE(clone->metaInformation(QTextDocument::DocumentTitle), title); QCOMPARE(clone->metaInformation(QTextDocument::DocumentUrl), url); + QCOMPARE(clone->metaInformation(QTextDocument::CssMedia), media); delete clone; } |