From 7b12bb4cf5f3e225b54f380f65932646cf8f0c62 Mon Sep 17 00:00:00 2001
From: Ilyas Hallak
Date: Tue, 7 Oct 2025 20:41:43 +0200
Subject: [PATCH] fix: Improve WebView performance by sanitizing problematic
HTML attributes
- Remove jsaction, jscontroller, jsname attributes that trigger navigation events
- Strip unnecessary id attributes to reduce DOM size
- Remove tabindex from non-interactive elements
- Fix invalid nested p tags inside pre/span blocks
- Prevents WebKit crashes on complex HTML content
---
readeck/UI/Components/WebView.swift | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/readeck/UI/Components/WebView.swift b/readeck/UI/Components/WebView.swift
index 20d0f55..d8f6c28 100644
--- a/readeck/UI/Components/WebView.swift
+++ b/readeck/UI/Components/WebView.swift
@@ -37,11 +37,27 @@ struct WebView: UIViewRepresentable {
func updateUIView(_ webView: WKWebView, context: Context) {
context.coordinator.onHeightChange = onHeightChange
context.coordinator.onScroll = onScroll
-
+
let isDarkMode = colorScheme == .dark
let fontSize = getFontSize(from: settings.fontSize ?? .extraLarge)
let fontFamily = getFontFamily(from: settings.fontFamily ?? .serif)
-
+
+ // Clean up problematic HTML that kills performance
+ let cleanedHTML = htmlContent
+ // Remove Google attributes that cause navigation events
+ .replacingOccurrences(of: #"\s*jsaction="[^"]*""#, with: "", options: .regularExpression)
+ .replacingOccurrences(of: #"\s*jscontroller="[^"]*""#, with: "", options: .regularExpression)
+ .replacingOccurrences(of: #"\s*jsname="[^"]*""#, with: "", options: .regularExpression)
+ // Remove unnecessary IDs that bloat the DOM
+ .replacingOccurrences(of: #"\s*id="[^"]*""#, with: "", options: .regularExpression)
+ // Remove tabindex from non-interactive elements
+ .replacingOccurrences(of: #"\s*tabindex="[^"]*""#, with: "", options: .regularExpression)
+ // Remove role=button from figures (causes false click targets)
+ .replacingOccurrences(of: #"\s*role="button""#, with: "", options: .regularExpression)
+ // Fix invalid nested tags inside
+ .replacingOccurrences(of: #"]*>([^<]*)"#, with: "
$1\n", options: .regularExpression)
+ .replacingOccurrences(of: #"
([^<]*)"#, with: "\n$1", options: .regularExpression)
+
let styledHTML = """
@@ -222,7 +238,7 @@ struct WebView: UIViewRepresentable {
- \(htmlContent)
+ \(cleanedHTML)