Skip to content
  • Igor Kushnir's avatar
    Embed CSS code into HTML instead of injecting via JavaScript · 60df20c0
    Igor Kushnir authored
    This improves performance and eliminates flickering when large man pages
    are loaded.
    
    Remove mostly obsolete CSS workarounds in
    QtHelpDocumentation::setUserStyleSheet(), which where implemented in
    245f8e98 10 years ago. Would be a pity
    if these practically useless workarounds caused page flickering.
    Rewriting this code in a way that prevents flickering just to improve
    the looks of documentation pages from older Qt versions does not seem
    worthwhile. Especially since, as far as I can tell, overriding CSS has
    never worked with Qt WebEngine, until my recent commit. And all these
    years no one bothered to fix it.
    
    If overriding CSS in QtHelp pages is ever needed again, it can be
    embedded into a page directly as is done in ManPageDocumentation. In
    HelpNetworkReply::HelpNetworkReply(), inside the
    `if (request.url().fileName().endsWith(QLatin1String(".html")))` block,
    add this code:
        constexpr char headEndTag[] = "</head>";
        const auto headEndTagPos = data.indexOf(headEndTag, 0);
        if (headEndTagPos == -1) {
            qCWarning(QTHELP) << "missing" << headEndTag << "on the HTML page.";
        } else {
            auto cssCode = QByteArrayLiteral("html { background: white !important; }\n");
            if (request.url().scheme() == QLatin1String("qthelp")
                && request.url().host().startsWith(QLatin1String("com.trolltech.qt."))) {
                cssCode += ".content .toc + .title + p { clear:left; }\n"
                           "#qtdocheader .qtref { position: absolute !important; top: 5px !important; right: 0 "
                           "!important; }\n";
            }
            data.insert(headEndTagPos, "<style>" + cssCode + "</style>");
        }
    
    I have verified that this proof-of-concept code works in practice by
    replacing "background: white" with "background: green". However, a
    proper implementation would have to search for headEndTag in QByteArray
    data case-insensitively, which could be done with
    std::boyer_moore_horspool_searcher and a custom equality BinaryPredicate
    based on <cctype>.
    
    Keep no longer used StandardDocumentationView::setOverrideCss*() because
    CSS code cannot be embedded into an external website page. For example,
    if kdev-php needs to override CSS in the future, it would have to use
    this KDevPlatform API.
    60df20c0