[macOS] [WK2] Support changing foreground colors via color panel
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 11 Sep 2018 21:56:17 +0000 (21:56 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 11 Sep 2018 21:56:17 +0000 (21:56 +0000)
commit176fa781c2b60922603aa694840f8cf5b711fba8
treeef6364cd81bdc082468167a5e9883380ef8149ab
parenteef58d7fc238c6604fb306054325f2f9cf542447
[macOS] [WK2] Support changing foreground colors via color panel
https://bugs.webkit.org/show_bug.cgi?id=189382
<rdar://problem/44227311>

Reviewed by Ryosuke Niwa.

Source/WebCore:

Small adjustments to support changing foreground text color using NSColorPanel in WebKit2. See comments below.
Tested by FontManagerTests.ChangeFontColorWithColorPanel.

* editing/EditingStyle.cpp:
(WebCore::StyleChange::extractTextStyles):

Support setting foreground text color with alpha by using a styled span element rather than a font element with
attributes. To do this, only populate `StyleChange::m_applyFontColor` if the color is opaque. This is because
the font element does not support `rgba()` syntax, so any font colors here with alpha that are serialized to
`rgba()` result in a garbage value for the computed color style.

* editing/FontAttributeChanges.cpp:
(WebCore::FontAttributeChanges::editAction const):

Add a helper to return the relevant EditAction describing this set of FontAttributeChanges.

* editing/FontAttributeChanges.h:
(WebCore::FontChanges::isEmpty const):

Source/WebKit:

Adds support for changing font color in a richly editable element in WebKit2 via NSColorPanel. See below for
more detail, as well as the WebCore ChangeLog.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView changeColor:]):

Implement this selector; AppKit calls into this when changing font color using NSColorPanel.

* UIProcess/Cocoa/WebViewImpl.h:
* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::WebViewImpl::changeFontColorFromSender):

Creates and populates new FontAttributeChanges, with only the foreground color determined by asking the sender
object for its -color.

* WebProcess/WebPage/mac/WebPageMac.mm:

Use the relevant EditAction for the incoming FontAttributeChanges, instead of always specifying
EditAction::ChangeAttributes.

(WebKit::WebPage::changeFontAttributes):

Tools:

Add an API test that uses NSColorPanel to change the color of selected text, and also apply typing styles when
the selection is collapsed. The test also exercises switching between opaque colors (alpha = 1) and transparent
colors, as well as making different parts of a word different colors.

* TestWebKitAPI/Tests/mac/FontManagerTests.mm:
(-[TestWKWebView collapseToEnd]):
(webViewForFontManagerTesting):
(TestWebKitAPI::TEST):
* TestWebKitAPI/cocoa/TestWKWebView.h:

LayoutTests:

Adjust an existing layout test that applies a text color with alpha. Currently, this results in a font element
being emitted with the `color` attribute, but this is incorrect, since the `color` attribute of a font element
does not support "rgba(…)" values.

Instead, split this into two cases: verify that setting the color to an opaque color (with alpha = 1) emits a
font element with the correct `color` attribute, and fall back to using an inline style when the color is
partially transparent.

* editing/style/inline-style-container-expected.txt:
* editing/style/inline-style-container.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@235914 268f45cc-cd09-0410-ab3c-d52691b4dbfc
15 files changed:
LayoutTests/ChangeLog
LayoutTests/editing/style/inline-style-container-expected.txt
LayoutTests/editing/style/inline-style-container.html
Source/WebCore/ChangeLog
Source/WebCore/editing/EditingStyle.cpp
Source/WebCore/editing/FontAttributeChanges.cpp
Source/WebCore/editing/FontAttributeChanges.h
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit/UIProcess/Cocoa/WebViewImpl.h
Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm
Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/mac/FontManagerTests.mm
Tools/TestWebKitAPI/cocoa/TestWKWebView.h