| 233 | } |
| 234 | |
| 235 | void StandardDocumentationView::setOverrideCssCode(const QByteArray& cssCode) |
| 236 | { |
| 237 | Q_D(StandardDocumentationView); |
| 238 | |
| 239 | const auto scriptName = QStringLiteral("OverrideCss"); |
| 240 | auto& scripts = d->m_view->page()->scripts(); |
| 241 | |
| 242 | const auto oldScripts = scripts.find(scriptName); |
| 243 | Q_ASSERT_X(oldScripts.size() <= 1, Q_FUNC_INFO, |
| 244 | "There should be at most one OverrideCss script, ours, if we set it before."); |
| 245 | if (!oldScripts.empty()) { |
| 246 | scripts.remove(oldScripts.front()); |
| 247 | } |
| 248 | |
| 249 | if (cssCode.isEmpty()) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // The loading of CSS via JavaScript has a downside: pages are first loaded as is, then |
| 254 | // reloaded with the style applied. When a page is large, the reloading is conspicuous |
| 255 | // or causes flickering. For example, this can be seen on cmake-modules man page. |
| 256 | // This cannot be fixed by specifying an earlier injection point - DocumentCreation - |
| 257 | // because, according to QWebEngineScript documentation, this is not suitable for any |
| 258 | // DOM operation. So with the DocumentCreation injection point the CSS style is not |
| 259 | // applied and the following error appears in KDevelop's output: |
| 260 | // js: Uncaught TypeError: Cannot read property 'appendChild' of null |
| 261 | QWebEngineScript script; |
| 262 | script.setInjectionPoint(QWebEngineScript::DocumentReady); |
| 263 | script.setName(scriptName); |
| 264 | script.setRunsOnSubFrames(false); |
| 265 | script.setSourceCode(QLatin1String("const css = document.createElement('style');" |
| 266 | "css.innerText = '%1';" |
| 267 | "document.head.appendChild(css);") |
| 268 | .arg(QString::fromUtf8(escapeJavaScriptString(cssCode)))); |
| 269 | script.setWorldId(QWebEngineScript::ApplicationWorld); |
| 270 | |
| 271 | scripts.insert(script); |
| 272 | } |
| 273 | |
| 274 | void KDevelop::StandardDocumentationView::load(const QUrl& url) |
| 275 | { |