I investigated how CSS styling works for HTML. QTextHtmlParser::parseTag() calls QTextHtmlParserNode::applyCssDeclarations() and the result is that QTextHtmlParserNode::charFormat and blockFormat will be modified. QTextHtmlImporter::import() will then apply those changes while calling QTextHtmlImporter::appendBlock() etc. The result is the documented limitation in QTextDocument::setDefaultStyleSheet(): "Changing the default style sheet does not have any effect to the existing content of the document."
In order to support CSS for formatting both HTML and Markdown with reusable code, and also to remove that limitation, we probably need to refactor it to iterate all document nodes and apply CSS styles to them. But this is difficult because of the way that QTextDocument is not a DOM: it mostly doesn't remember which types of HTML tags go with which QTextBlocks etc. In 310daae53926628f80c08e4415b94b90ad525c8f for example I made it remember the <Hx> heading that was declared, in addition to setting default font sizes for headings. Next, markdown rewriting support depends on remembering <blockquote>s and <code> blocks in a similar way (by adding more attributes to the QTextBlock). But there are probably a few more cases like that, that need to be done before we will be able to apply a CSS stylesheet later on, after the parsing is already done and we only have a QTextDocument.
Otherwise, maybe we should make it into a DOM.
I suppose Konqueror must have done something like that early on, when they first got started and ran into the same problem.
|For Gerrit Dashboard: QTBUG-75662|
|262305,7||WIP teach QTextDocument::setDefaultStyleSheet() to restyle at runtime||dev||qt/qtbase||Status: NEW||-2||0|