Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: e1bc930edef95d5fe08fe76b2a195b82c9123790
https://github.com/WebKit/WebKit/commit/e1bc930edef95d5fe08fe76b2a195b82c9123790
Author: Wenson Hsieh <[email protected]>
Date: 2026-01-09 (Fri, 09 Jan 2026)
Changed paths:
M Source/WebCore/editing/ReplaceSelectionCommand.cpp
M Source/WebCore/editing/ReplaceSelectionCommand.h
M Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm
Log Message:
-----------
[iOS] Text is illegible when copying dark text in a light webpage and pasting
into mail compose in dark mode
https://bugs.webkit.org/show_bug.cgi?id=305232
rdar://165743699
Reviewed by Richard Robinson.
After the changes in rdar://81883755 (in iOS 26), the Mail compose body field
no longer uses
`-apple-color-filter: apple-invert-lightness();`, but still honors dark mode
via the meta tag
`color-scheme: light dark;` (and additionally punches out white backgrounds in
dark mode using
`WKPreferences` SPI).
This means that when copying and pasting dark text on a white background in
Safari into Mail
compose, we now end up with something like:
```
<span style="color: black; background-color: white;"></span>
```
While this _looks_ like it should be legible due to the white background color,
the background color
effectively becomes transparent when painting due to
`_punchOutWhiteBackgroundsInDarkMode`. The end
result is black text on a dark background, which is barely readable.
To fix this, we heuristically remove `color` and `caret-color` style properties
when pasting dark
text into Mail compose in dark mode, and instead fall back on default styling
(which should result
in white text against a dark background).
See below for more details.
Test: PasteHTML.PasteDarkTextOnWhiteBackgroundIntoDarkModeEditor
* Source/WebCore/editing/ReplaceSelectionCommand.cpp:
(WebCore::ReplaceSelectionCommand::doApply):
(WebCore::collectStylesToRemove):
(WebCore::ReplaceSelectionCommand::removeForegroundColorsInDarkModeIfNeeded):
Add a heuristic to remove inline `color` and `caret-color` styles from pasted
elements that
otherwise have no background or a punched out background color, only for
editable web views to limit
compat risk. We determine if the color or caret color are non-legible by
comparing luminance against
the document's background color; if it's too close (with an arbitrary threshold
of 0.1), we simply
remove those inline CSS properties and allow the color to fall back to default
colors.
Note that I also first collect all the style properties to remove and elements
to adjust up front
first, (prior to mutating any DOM state), to ensure that style invalidation
doesn't trigger layout
or style recalc in the middle of iterating through the DOM.
* Source/WebCore/editing/ReplaceSelectionCommand.h:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm:
(TEST(PasteHTML, PasteDarkTextOnWhiteBackgroundIntoDarkModeEditor)):
Add an API test to exercise the change by copying light text in a white webpage
and pasting into a
dark mode editable web view where white backgrounds are punched out (simulating
Mail compose). The
resulting text (and caret) color should be white, instead of dark.
Canonical link: https://commits.webkit.org/305399@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications