diff options
author | Ralf Habacker <ralf.habacker@freenet.de> | 2021-11-17 13:54:32 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-12-15 15:13:03 +0000 |
commit | 699e661b2be8fbd8b0ba65aa3356bf0585f760e1 (patch) | |
tree | 4f0b54deeeffad492af81668581431a333003df4 /src/gui/text | |
parent | 96b31b6a58f88a26ccf9ea1c5db79f69a990ef8b (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()
Fixes: QTBUG-98408
Change-Id: Ie05f815a6dedbd970210f467e26b116f6ee3b9ca
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit 2e2f1e2af76d94a41f382e81ab8a6fdccf8ca579)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/gui/text')
-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 |
5 files changed, 20 insertions, 5 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 68cdbc092e..0fed8c98d3 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 dc4d15f3a8..4324c15643 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 f73475226f..01e73dd66f 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 b26d7657ec..2b26897017 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 ae2550b69e..85877bfe47 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; |