Title: [249517] trunk
Revision
249517
Author
[email protected]
Date
2019-09-04 19:52:40 -0700 (Wed, 04 Sep 2019)

Log Message

Mail appears to be double inverting code copied from Notes, Xcode, or Terminal.
https://bugs.webkit.org/show_bug.cgi?id=201368
rdar://problem/40529867

Reviewed by Ryosuke Niwa.

Source/WebCore:

Dark mode content that is pasted should have the inline styles inverse color
transformed by the color filter to match the color filtered document contents.

Layout Test: editing/pasteboard/paste-dark-mode-color-filtered.html
API Tests: PasteHTML.TransformColorsOfDarkContent, PasteHTML.DoesNotTransformColorsOfLightContent,
    PasteRTFD.TransformColorsOfDarkContent, PasteRTFD.DoesNotTransformColorsOfLightContent

* editing/EditingStyle.cpp:
(WebCore::EditingStyle::inverseTransformColorIfNeeded): Added caret-color to the transformed properties.
* editing/ReplaceSelectionCommand.cpp:
(WebCore::fragmentNeedsColorTransformed): Added.
(WebCore::ReplaceSelectionCommand::inverseTransformColor): Added.
(WebCore::ReplaceSelectionCommand::doApply): Call fragmentNeedsColorTransformed() and inverseTransformColor().
* editing/ReplaceSelectionCommand.h:

Tools:

Added Tests: PasteHTML.TransformColorsOfDarkContent, PasteHTML.DoesNotTransformColorsOfLightContent,
    PasteRTFD.TransformColorsOfDarkContent, PasteRTFD.DoesNotTransformColorsOfLightContent

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm:
(createWebViewWithCustomPasteboardDataSetting): Added argument to enable color filter.
* TestWebKitAPI/Tests/WebKitCocoa/PasteRTFD.mm:
(createWebViewWithCustomPasteboardDataEnabled): Added argument to enable color filter.
* TestWebKitAPI/Tests/WebKitCocoa/rich-color-filtered.html: Added.
* TestWebKitAPI/cocoa/TestWKWebView.h:
* TestWebKitAPI/cocoa/TestWKWebView.mm:
(-[TestWKWebView forceDarkMode]):

LayoutTests:

* TestExpectations:
* editing/pasteboard/paste-dark-mode-color-filtered-expected.txt: Added.
* editing/pasteboard/paste-dark-mode-color-filtered.html: Added.
* platform/ios-12/TestExpectations:
* platform/ios/TestExpectations:
* platform/mac/TestExpectations:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (249516 => 249517)


--- trunk/LayoutTests/ChangeLog	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/LayoutTests/ChangeLog	2019-09-05 02:52:40 UTC (rev 249517)
@@ -1,3 +1,18 @@
+2019-09-04  Timothy Hatcher  <[email protected]>
+
+        Mail appears to be double inverting code copied from Notes, Xcode, or Terminal.
+        https://bugs.webkit.org/show_bug.cgi?id=201368
+        rdar://problem/40529867
+
+        Reviewed by Ryosuke Niwa.
+
+        * TestExpectations:
+        * editing/pasteboard/paste-dark-mode-color-filtered-expected.txt: Added.
+        * editing/pasteboard/paste-dark-mode-color-filtered.html: Added.
+        * platform/ios-12/TestExpectations:
+        * platform/ios/TestExpectations:
+        * platform/mac/TestExpectations:
+
 2019-09-04  Yusuke Suzuki  <[email protected]>
 
         [JSC] Make Promise implementation faster

Modified: trunk/LayoutTests/TestExpectations (249516 => 249517)


--- trunk/LayoutTests/TestExpectations	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/LayoutTests/TestExpectations	2019-09-05 02:52:40 UTC (rev 249517)
@@ -87,10 +87,11 @@
 # These tests don't have to be platform-specific, but they are only implemented on Mac now.
 fast/images/eps-as-image.html [ Skip ]
 
-# Only applicable on macOS
+# Only applicable on platforms with dark mode support
 css-dark-mode [ Skip ]
 fast/css/apple-system-control-colors.html [ Skip ]
 inspector/css/force-page-appearance.html [ Skip ]
+editing/pasteboard/paste-dark-mode-color-filtered.html [ Skip ]
 
 # Only Mac supports force tests.
 fast/events/cancelled-force-click-link-navigation.html [ Skip ]

Added: trunk/LayoutTests/editing/pasteboard/paste-dark-mode-color-filtered-expected.txt (0 => 249517)


--- trunk/LayoutTests/editing/pasteboard/paste-dark-mode-color-filtered-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/editing/pasteboard/paste-dark-mode-color-filtered-expected.txt	2019-09-05 02:52:40 UTC (rev 249517)
@@ -0,0 +1,27 @@
+This test checks that pasting into a color filtered dark mode document inserts light mode transformed content.
+
+PASS pastedMarkup is '<span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(21, 21, 21); color: rgb(21, 21, 21); background-color: rgb(255, 255, 255);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(21, 21, 21); color: rgb(21, 21, 21); background-color: rgb(213, 213, 213);">Hello</span>'
+PASS pastedMarkup is '<span style="color: rgb(106, 106, 106);">Hello</span><span style="color: rgb(127, 128, 127);">Hello 2</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(127, 128, 127); color: rgb(127, 128, 127);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(170, 170, 170); color: rgb(170, 170, 170);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(191, 191, 191); color: rgb(191, 191, 191);">Hello</span>'
+PASS pastedMarkup is '<li>Item 1</li><li><span style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">Hello</span></li><li>Item 2</li>'
+PASS pastedMarkup is '<li>Item 1</li><li style="color: rgb(21, 21, 21); background-color: rgb(213, 213, 213);">Hello 1</li><li>Hello 2</li><li>Item 2</li>'
+PASS pastedMarkup is '<li>Item 1</li><li>Hello 1</li><li style="color: rgb(127, 128, 127);">Hello 2</li><li>Item 2</li>'
+PASS pastedMarkup is '<span style="caret-color: rgb(101, 101, 101); color: rgb(101, 101, 101);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(90, 90, 90); color: rgb(90, 90, 90);">Hello</span>'
+PASS pastedMarkup is '<span style="color: rgb(85, 85, 85);">Hello</span><span style="background-color: black;">Hello 2</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(68, 68, 68); color: rgb(68, 68, 68);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(68, 68, 68); color: rgb(68, 68, 68);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);">Hello</span>'
+PASS pastedMarkup is '<span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">Hello</span>'
+PASS pastedMarkup is '<span style="background-color: rgb(255, 255, 255);">Hello</span>'
+PASS pastedMarkup is '<li>Item 1</li><li><span style="background-color: white;">Hello</span></li><li>Item 2</li>'
+PASS pastedMarkup is '<li>Item 1</li><li style="color: rgb(85, 85, 85); background-color: rgb(235, 235, 235);">Hello 1</li><li>Hello 2</li><li>Item 2</li>'
+PASS pastedMarkup is '<li>Item 1</li><li>Hello 1</li><li style="color: black;">Hello 2</li><li>Item 2</li>'
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/editing/pasteboard/paste-dark-mode-color-filtered.html (0 => 249517)


--- trunk/LayoutTests/editing/pasteboard/paste-dark-mode-color-filtered.html	                        (rev 0)
+++ trunk/LayoutTests/editing/pasteboard/paste-dark-mode-color-filtered.html	2019-09-05 02:52:40 UTC (rev 249517)
@@ -0,0 +1,95 @@
+<!DOCTYPE html><!-- webkit-test-runner [ enableColorFilter=true ] -->
+<html>
+<head>
+    <script src=""
+    <script src=""
+    <style>
+        :root {
+            color-scheme: dark;
+            -apple-color-filter: apple-invert-lightness();
+        }
+    </style>
+</head>
+<body>
+    <p id="description">This test checks that pasting into a color filtered dark mode document inserts light mode transformed content.</p>
+    <div id="console"></div>
+
+    <script>
+    if (window.internals)
+        internals.settings.setUseDarkAppearance(true);
+
+    let sel = document.getSelection();
+    let root = document.createElement("root");
+    document.body.appendChild(root);
+
+    function createEditable(tagName, markup) {
+        let node = document.createElement(tagName);
+        node.contentEditable = true;
+        if (markup)
+            node.innerHTML = markup;
+        return node;
+    }
+
+    function testCopyPaste(originalMarkup, expected, asList = false) {
+        let tagName = asList ? "ul" : "div";
+        let copyNode = createEditable(tagName, originalMarkup);
+        root.appendChild(copyNode);
+        copyNode.focus();
+
+        document.execCommand("SelectAll", false);
+        document.execCommand("Copy", false);
+
+        copyNode.remove();
+
+        let pasteNode = createEditable(tagName);
+        root.appendChild(pasteNode);
+
+        if (asList) {
+            pasteNode.innerHTML = "<li>Item 1</li><li>Item 2</li>";
+            pasteNode.focus();
+            moveSelectionForwardByLineCommand();
+        } else
+            pasteNode.focus();
+
+        document.execCommand("Paste", false);
+
+        pastedMarkup = pasteNode.innerHTML;
+
+        shouldBe("pastedMarkup", "'" + expected + "'");
+    }
+
+    // Transformed on paste
+    testCopyPaste("<span style=\"color: white; background-color: black\">Hello</span>", "<span style=\"caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(238, 238, 238); background-color: rgb(51, 51, 51)\">Hello</span>", "<span style=\"caret-color: rgb(21, 21, 21); color: rgb(21, 21, 21); background-color: rgb(255, 255, 255);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(238, 238, 238); background-color: rgb(85, 85, 85)\">Hello</span>", "<span style=\"caret-color: rgb(21, 21, 21); color: rgb(21, 21, 21); background-color: rgb(213, 213, 213);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(170, 170, 170)\">Hello</span><span style=\"color: rgb(153, 153, 153)\">Hello 2</span>", "<span style=\"color: rgb(106, 106, 106);\">Hello</span><span style=\"color: rgb(127, 128, 127);\">Hello 2</span>");
+    testCopyPaste("<span style=\"color: rgb(153, 153, 153)\">Hello</span>", "<span style=\"caret-color: rgb(127, 128, 127); color: rgb(127, 128, 127);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(119, 119, 119)\">Hello</span>", "<span style=\"caret-color: rgb(170, 170, 170); color: rgb(170, 170, 170);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(102, 102, 102)\">Hello</span>", "<span style=\"caret-color: rgb(191, 191, 191); color: rgb(191, 191, 191);\">Hello</span>");
+
+    // Transformed list items on paste
+    testCopyPaste("<li><span style=\"color: white; background-color: black\">Hello</span></li>", "<li>Item 1</li><li><span style=\"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);\">Hello</span></li><li>Item 2</li>", true);
+    testCopyPaste("<li style=\"color: rgb(238, 238, 238); background-color: rgb(85, 85, 85)\">Hello 1</li><li>Hello 2</li>", "<li>Item 1</li><li style=\"color: rgb(21, 21, 21); background-color: rgb(213, 213, 213);\">Hello 1</li><li>Hello 2</li><li>Item 2</li>", true);
+    testCopyPaste("<li>Hello 1</li><li style=\"color: rgb(153, 153, 153)\">Hello 2</li>", "<li>Item 1</li><li>Hello 1</li><li style=\"color: rgb(127, 128, 127);\">Hello 2</li><li>Item 2</li>", true);
+
+    // Not transformed on paste
+    testCopyPaste("<span style=\"color: rgb(101, 101, 101)\">Hello</span>", "<span style=\"caret-color: rgb(101, 101, 101); color: rgb(101, 101, 101);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(90, 90, 90)\">Hello</span>", "<span style=\"caret-color: rgb(90, 90, 90); color: rgb(90, 90, 90);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(85, 85, 85)\">Hello</span><span style=\"color: white; background-color: black\">Hello 2</span>", "<span style=\"color: rgb(85, 85, 85);\">Hello</span><span style=\"background-color: black;\">Hello 2</span>");
+    testCopyPaste("<span style=\"color: rgb(68, 68, 68)\">Hello</span>", "<span style=\"caret-color: rgb(68, 68, 68); color: rgb(68, 68, 68);\">Hello</span>");
+    testCopyPaste("<span style=\"color: rgb(68, 68, 68)\">Hello</span>", "<span style=\"caret-color: rgb(68, 68, 68); color: rgb(68, 68, 68);\">Hello</span>");
+    testCopyPaste("<span style=\"color: black\">Hello</span>", "<span style=\"caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);\">Hello</span>");
+    testCopyPaste("<span style=\"color: black; background-color: white\">Hello</span>", "<span style=\"caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);\">Hello</span>");
+    testCopyPaste("<span style=\"color: white; background-color: white\">Hello</span>", "<span style=\"background-color: rgb(255, 255, 255);\">Hello</span>");
+
+    // List items not transformed on paste
+    testCopyPaste("<li><span style=\"color: white; background-color: white\">Hello</span></li>", "<li>Item 1</li><li><span style=\"background-color: white;\">Hello</span></li><li>Item 2</li>", true);
+    testCopyPaste("<li style=\"color: rgb(85, 85, 85); background-color: rgb(235, 235, 235)\">Hello 1</li><li>Hello 2</li>", "<li>Item 1</li><li style=\"color: rgb(85, 85, 85); background-color: rgb(235, 235, 235);\">Hello 1</li><li>Hello 2</li><li>Item 2</li>", true);
+    testCopyPaste("<li>Hello 1</li><li style=\"color: black\">Hello 2</li>", "<li>Item 1</li><li>Hello 1</li><li style=\"color: black;\">Hello 2</li><li>Item 2</li>", true);
+
+    root.style.display = "none";
+    </script>
+
+    <script src=""
+</body>
+</html>

Modified: trunk/LayoutTests/platform/ios/TestExpectations (249516 => 249517)


--- trunk/LayoutTests/platform/ios/TestExpectations	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/LayoutTests/platform/ios/TestExpectations	2019-09-05 02:52:40 UTC (rev 249517)
@@ -3242,6 +3242,7 @@
 # Dark Mode is iOS 13 and later.
 css-dark-mode [ Pass ]
 css-dark-mode/older-systems [ Skip ]
+editing/pasteboard/paste-dark-mode-color-filtered.html [ Pass ]
 
 fast/forms/auto-fill-button/caps-lock-indicator-should-be-visible-after-hiding-auto-fill-strong-password-button.html [ Pass ]
 fast/forms/auto-fill-button/caps-lock-indicator-should-not-be-visible-when-auto-fill-strong-password-button-is-visible.html [ Pass ]

Modified: trunk/LayoutTests/platform/ios-12/TestExpectations (249516 => 249517)


--- trunk/LayoutTests/platform/ios-12/TestExpectations	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/LayoutTests/platform/ios-12/TestExpectations	2019-09-05 02:52:40 UTC (rev 249517)
@@ -1,5 +1,6 @@
 css-dark-mode [ Skip ]
 css-dark-mode/older-systems [ Pass ]
+editing/pasteboard/paste-dark-mode-color-filtered.html [ Skip ]
 
 # <rdar://problem/48781098> HEIF and HEICS images are supported only on iOS 13
 fast/images/animated-heics-draw.html [ Skip ]

Modified: trunk/LayoutTests/platform/mac/TestExpectations (249516 => 249517)


--- trunk/LayoutTests/platform/mac/TestExpectations	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/LayoutTests/platform/mac/TestExpectations	2019-09-05 02:52:40 UTC (rev 249517)
@@ -1749,6 +1749,7 @@
 [ Mojave+ ] css-dark-mode [ Pass ]
 [ Mojave+ ] css-dark-mode/older-systems [ Skip ]
 [ Mojave+ ] inspector/css/force-page-appearance.html [ Pass ]
+[ Mojave+ ] editing/pasteboard/paste-dark-mode-color-filtered.html [ Pass ]
 
 # Test Dark Mode CSS support for light mode on older systems.
 [ Sierra HighSierra ] css-dark-mode/older-systems [ Pass ]

Modified: trunk/Source/WebCore/ChangeLog (249516 => 249517)


--- trunk/Source/WebCore/ChangeLog	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Source/WebCore/ChangeLog	2019-09-05 02:52:40 UTC (rev 249517)
@@ -1,3 +1,26 @@
+2019-09-04  Timothy Hatcher  <[email protected]>
+
+        Mail appears to be double inverting code copied from Notes, Xcode, or Terminal.
+        https://bugs.webkit.org/show_bug.cgi?id=201368
+        rdar://problem/40529867
+
+        Reviewed by Ryosuke Niwa.
+
+        Dark mode content that is pasted should have the inline styles inverse color
+        transformed by the color filter to match the color filtered document contents.
+
+        Layout Test: editing/pasteboard/paste-dark-mode-color-filtered.html
+        API Tests: PasteHTML.TransformColorsOfDarkContent, PasteHTML.DoesNotTransformColorsOfLightContent,
+            PasteRTFD.TransformColorsOfDarkContent, PasteRTFD.DoesNotTransformColorsOfLightContent
+
+        * editing/EditingStyle.cpp:
+        (WebCore::EditingStyle::inverseTransformColorIfNeeded): Added caret-color to the transformed properties.
+        * editing/ReplaceSelectionCommand.cpp:
+        (WebCore::fragmentNeedsColorTransformed): Added.
+        (WebCore::ReplaceSelectionCommand::inverseTransformColor): Added.
+        (WebCore::ReplaceSelectionCommand::doApply): Call fragmentNeedsColorTransformed() and inverseTransformColor().
+        * editing/ReplaceSelectionCommand.h:
+
 2019-09-04  Chris Dumez  <[email protected]>
 
         Unreviewed, drop extra whitespace added in r249515.

Modified: trunk/Source/WebCore/editing/EditingStyle.cpp (249516 => 249517)


--- trunk/Source/WebCore/editing/EditingStyle.cpp	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Source/WebCore/editing/EditingStyle.cpp	2019-09-05 02:52:40 UTC (rev 249517)
@@ -1580,8 +1580,9 @@
         return *this;
 
     bool hasColor = m_mutableStyle->getPropertyCSSValue(CSSPropertyColor);
+    bool hasCaretColor = m_mutableStyle->getPropertyCSSValue(CSSPropertyCaretColor);
     bool hasBackgroundColor = m_mutableStyle->getPropertyCSSValue(CSSPropertyBackgroundColor);
-    if (!hasColor && !hasBackgroundColor)
+    if (!hasColor && !hasCaretColor && !hasBackgroundColor)
         return *this;
 
     auto styleWithInvertedColors = copy();
@@ -1597,6 +1598,9 @@
     if (hasColor)
         invertedColor(CSSPropertyColor);
 
+    if (hasCaretColor)
+        invertedColor(CSSPropertyCaretColor);
+
     if (hasBackgroundColor)
         invertedColor(CSSPropertyBackgroundColor);
 

Modified: trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp (249516 => 249517)


--- trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp	2019-09-05 02:52:40 UTC (rev 249517)
@@ -55,6 +55,7 @@
 #include "HTMLTitleElement.h"
 #include "NodeList.h"
 #include "NodeRenderStyle.h"
+#include "Position.h"
 #include "RenderInline.h"
 #include "RenderText.h"
 #include "ScriptElement.h"
@@ -520,6 +521,82 @@
         && !isBlock(sourceNode) && !isBlock(destinationNode);
 }
 
+static bool fragmentNeedsColorTransformed(ReplacementFragment& fragment, const Position& insertionPos)
+{
+    // Dark mode content that is inserted should have the inline styles inverse color
+    // transformed by the color filter to match the color filtered document contents.
+    // This applies to Mail and Notes when pasting from Xcode. <rdar://problem/40529867>
+
+    RefPtr<Element> editableRoot = insertionPos.rootEditableElement();
+    ASSERT(editableRoot);
+    if (!editableRoot)
+        return false;
+
+    auto* editableRootRenderer = editableRoot->renderer();
+    if (!editableRootRenderer || !editableRootRenderer->style().hasAppleColorFilter())
+        return false;
+
+    const auto& colorFilter = editableRootRenderer->style().appleColorFilter();
+    for (const auto& colorFilterOperation : colorFilter.operations()) {
+        if (colorFilterOperation->type() != FilterOperation::APPLE_INVERT_LIGHTNESS)
+            return false;
+    }
+
+    auto propertyLightness = [&](const StyleProperties& inlineStyle, CSSPropertyID propertyID) -> Optional<double> {
+        auto color = inlineStyle.propertyAsColor(propertyID);
+        if (!color || !color.value().isVisible() || color.value().isSemantic())
+            return { };
+
+        double hue, saturation, lightness;
+        color.value().getHSL(hue, saturation, lightness);
+        return lightness;
+    };
+
+    const double lightnessDarkEnoughForText = 0.4;
+    const double lightnessLightEnoughForBackground = 0.6;
+
+    for (RefPtr<Node> node = fragment.firstChild(); node; node = NodeTraversal::next(*node)) {
+        if (!is<StyledElement>(*node))
+            continue;
+
+        auto& element = downcast<StyledElement>(*node);
+        auto* inlineStyle = element.inlineStyle();
+        if (!inlineStyle)
+            continue;
+
+        auto textLightness = propertyLightness(*inlineStyle, CSSPropertyColor);
+        if (textLightness && *textLightness < lightnessDarkEnoughForText)
+            return false;
+
+        auto backgroundLightness = propertyLightness(*inlineStyle, CSSPropertyBackgroundColor);
+        if (backgroundLightness && *backgroundLightness > lightnessLightEnoughForBackground)
+            return false;
+    }
+
+    return true;
+}
+
+void ReplaceSelectionCommand::inverseTransformColor(InsertedNodes& insertedNodes)
+{
+    RefPtr<Node> pastEndNode = insertedNodes.pastLastLeaf();
+    for (RefPtr<Node> node = insertedNodes.firstNodeInserted(); node && node != pastEndNode; node = NodeTraversal::next(*node)) {
+        if (!is<StyledElement>(*node))
+            continue;
+
+        auto& element = downcast<StyledElement>(*node);
+        auto* inlineStyle = element.inlineStyle();
+        if (!inlineStyle)
+            continue;
+
+        auto editingStyle = EditingStyle::create(inlineStyle);
+        auto transformedStyle = editingStyle->inverseTransformColorIfNeeded(element);
+        if (editingStyle.ptr() == transformedStyle.ptr())
+            continue;
+
+        setNodeAttribute(element, styleAttr, transformedStyle->style()->asText());
+    }
+}
+
 // Style rules that match just inserted elements could change their appearance, like
 // a div inserted into a document with div { display:inline; }.
 void ReplaceSelectionCommand::removeRedundantStylesAndKeepStyleSpanInline(InsertedNodes& insertedNodes)
@@ -1115,8 +1192,8 @@
     insertionPos = positionOutsideTabSpan(insertionPos);
 
     bool hasBlankLinesBetweenParagraphs = hasBlankLineBetweenParagraphs(insertionPos);
-    
     bool handledStyleSpans = handleStyleSpansBeforeInsertion(fragment, insertionPos);
+    bool needsColorTransformed = fragmentNeedsColorTransformed(fragment, insertionPos);
 
     // We're finished if there is nothing to add.
     if (fragment.isEmpty() || !fragment.firstChild())
@@ -1227,11 +1304,14 @@
             removeNode(*nodeToRemove);
         }
     }
-    
+
     makeInsertedContentRoundTrippableWithHTMLTreeBuilder(insertedNodes);
     if (insertedNodes.isEmpty())
         return;
 
+    if (needsColorTransformed)
+        inverseTransformColor(insertedNodes);
+
     removeRedundantStylesAndKeepStyleSpanInline(insertedNodes);
     if (insertedNodes.isEmpty())
         return;

Modified: trunk/Source/WebCore/editing/ReplaceSelectionCommand.h (249516 => 249517)


--- trunk/Source/WebCore/editing/ReplaceSelectionCommand.h	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Source/WebCore/editing/ReplaceSelectionCommand.h	2019-09-05 02:52:40 UTC (rev 249517)
@@ -99,6 +99,7 @@
     void removeUnrenderedTextNodesAtEnds(InsertedNodes&);
     
     void removeRedundantStylesAndKeepStyleSpanInline(InsertedNodes&);
+    void inverseTransformColor(InsertedNodes&);
     void makeInsertedContentRoundTrippableWithHTMLTreeBuilder(InsertedNodes&);
     void moveNodeOutOfAncestor(Node&, Node& ancestor, InsertedNodes&);
     void handleStyleSpans(InsertedNodes&);

Modified: trunk/Tools/ChangeLog (249516 => 249517)


--- trunk/Tools/ChangeLog	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Tools/ChangeLog	2019-09-05 02:52:40 UTC (rev 249517)
@@ -1,3 +1,24 @@
+2019-09-04  Timothy Hatcher  <[email protected]>
+
+        Mail appears to be double inverting code copied from Notes, Xcode, or Terminal.
+        https://bugs.webkit.org/show_bug.cgi?id=201368
+        rdar://problem/40529867
+
+        Reviewed by Ryosuke Niwa.
+
+        Added Tests: PasteHTML.TransformColorsOfDarkContent, PasteHTML.DoesNotTransformColorsOfLightContent,
+            PasteRTFD.TransformColorsOfDarkContent, PasteRTFD.DoesNotTransformColorsOfLightContent
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm:
+        (createWebViewWithCustomPasteboardDataSetting): Added argument to enable color filter.
+        * TestWebKitAPI/Tests/WebKitCocoa/PasteRTFD.mm:
+        (createWebViewWithCustomPasteboardDataEnabled): Added argument to enable color filter.
+        * TestWebKitAPI/Tests/WebKitCocoa/rich-color-filtered.html: Added.
+        * TestWebKitAPI/cocoa/TestWKWebView.h:
+        * TestWebKitAPI/cocoa/TestWKWebView.mm:
+        (-[TestWKWebView forceDarkMode]):
+
 2019-09-04  Wenson Hsieh  <[email protected]>
 
         MobileSafari may crash when invoking the C++ lambda in -[WKContentView _shareForWebView:]

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (249516 => 249517)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2019-09-05 02:52:40 UTC (rev 249517)
@@ -106,6 +106,7 @@
 		1C7FEB20207C0F2E00D23278 /* BackgroundColor.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1C7FEB1F207C0F2D00D23278 /* BackgroundColor.mm */; };
 		1C9EB8411E380DA1005C6442 /* ComplexTextController.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C9EB8401E380DA1005C6442 /* ComplexTextController.cpp */; };
 		1CACADA1230620AE0007D54C /* WKWebViewOpaque.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1CACADA0230620AD0007D54C /* WKWebViewOpaque.mm */; };
+		1CE6FAC32320267C00E48F6E /* rich-color-filtered.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1CE6FAC12320264F00E48F6E /* rich-color-filtered.html */; };
 		1CF59AE221E68925006E37EC /* ForceLightAppearanceInBundle_Bundle.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1CF59AE021E68925006E37EC /* ForceLightAppearanceInBundle_Bundle.mm */; };
 		1CF59AE321E68932006E37EC /* ForceLightAppearanceInBundle.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1CF59ADF21E68925006E37EC /* ForceLightAppearanceInBundle.mm */; };
 		1CF59AE521E6977D006E37EC /* dark-mode.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1CF59AE421E696FB006E37EC /* dark-mode.html */; };
@@ -1372,6 +1373,7 @@
 				F6FDDDD614241C6F004F1729 /* push-state.html in Copy Resources */,
 				A12DDC001E8373E700CF6CAE /* rendered-image-excluding-overflow.html in Copy Resources */,
 				F46849C01EEF5EF300B937FE /* rich-and-plain-text.html in Copy Resources */,
+				1CE6FAC32320267C00E48F6E /* rich-color-filtered.html in Copy Resources */,
 				2E92B8F7216490D4005B64F0 /* rich-text-attributes.html in Copy Resources */,
 				0F5651F91FCE513500310FBC /* scroll-to-anchor.html in Copy Resources */,
 				F4E0A296211FC5FB00AF7C7F /* selected-text-and-textarea.html in Copy Resources */,
@@ -1524,6 +1526,7 @@
 		1C9EB8401E380DA1005C6442 /* ComplexTextController.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ComplexTextController.cpp; sourceTree = "<group>"; };
 		1CACADA0230620AD0007D54C /* WKWebViewOpaque.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebViewOpaque.mm; sourceTree = "<group>"; };
 		1CB9BC371A67482300FE5678 /* WeakPtr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WeakPtr.cpp; sourceTree = "<group>"; };
+		1CE6FAC12320264F00E48F6E /* rich-color-filtered.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "rich-color-filtered.html"; sourceTree = "<group>"; };
 		1CF0D3781BBF2F3D00B4EF54 /* WKRetainPtr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKRetainPtr.cpp; sourceTree = "<group>"; };
 		1CF59ADF21E68925006E37EC /* ForceLightAppearanceInBundle.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ForceLightAppearanceInBundle.mm; sourceTree = "<group>"; };
 		1CF59AE021E68925006E37EC /* ForceLightAppearanceInBundle_Bundle.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ForceLightAppearanceInBundle_Bundle.mm; sourceTree = "<group>"; };
@@ -3303,6 +3306,7 @@
 				F41AB99A1EF4692C0083FA08 /* prevent-start.html */,
 				A12DDBFF1E8373C100CF6CAE /* rendered-image-excluding-overflow.html */,
 				F46849BF1EEF5EDC00B937FE /* rich-and-plain-text.html */,
+				1CE6FAC12320264F00E48F6E /* rich-color-filtered.html */,
 				2E92B8F6216490C3005B64F0 /* rich-text-attributes.html */,
 				F4E0A295211FC5A300AF7C7F /* selected-text-and-textarea.html */,
 				F4D65DA71F5E46C0009D8C27 /* selected-text-image-link-and-editable.html */,

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm (249516 => 249517)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteHTML.mm	2019-09-05 02:52:40 UTC (rev 249517)
@@ -57,9 +57,12 @@
 }
 #endif
 
-static RetainPtr<TestWKWebView> createWebViewWithCustomPasteboardDataSetting(bool enabled)
+static RetainPtr<TestWKWebView> createWebViewWithCustomPasteboardDataSetting(bool enabled, bool colorFilterEnabled = false)
 {
-    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]);
+    auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [webViewConfiguration _setColorFilterEnabled:colorFilterEnabled];
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400) configuration:webViewConfiguration.get()]);
     auto preferences = (__bridge WKPreferencesRef)[[webView configuration] preferences];
     WKPreferencesSetDataTransferItemsEnabled(preferences, true);
     WKPreferencesSetCustomPasteboardDataEnabled(preferences, enabled);
@@ -357,4 +360,39 @@
         [webView stringByEvaluatingJavaScript:@"getComputedStyle(document.body).fontFamily"]);
 }
 
+#if ENABLE(DARK_MODE_CSS) && HAVE(OS_DARK_MODE_SUPPORT)
+
+TEST(PasteHTML, TransformColorsOfDarkContent)
+{
+    auto webView = createWebViewWithCustomPasteboardDataSetting(true, true);
+    [webView forceDarkMode];
+
+    [webView synchronouslyLoadTestPageNamed:@"rich-color-filtered"];
+
+    writeHTMLToPasteboard(@"<span style=\"color: rgb(238, 238, 238); background-color: rgb(51, 51, 51)\">Hello</span>");
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView paste:nil];
+
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('span').style.color"], @"rgb(21, 21, 21)");
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('span').style.backgroundColor"], @"rgb(255, 255, 255)");
+}
+
+TEST(PasteHTML, DoesNotTransformColorsOfLightContent)
+{
+    auto webView = createWebViewWithCustomPasteboardDataSetting(true, true);
+    [webView forceDarkMode];
+
+    [webView synchronouslyLoadTestPageNamed:@"rich-color-filtered"];
+
+    writeHTMLToPasteboard(@"<span style=\"color: rgb(101, 101, 101)\">Hello</span>");
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView paste:nil];
+
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('span').style.color"], @"rgb(101, 101, 101)");
+}
+
+#endif // ENABLE(DARK_MODE_CSS) && HAVE(OS_DARK_MODE_SUPPORT)
+
 #endif // PLATFORM(COCOA)

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteRTFD.mm (249516 => 249517)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteRTFD.mm	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteRTFD.mm	2019-09-05 02:52:40 UTC (rev 249517)
@@ -39,6 +39,12 @@
 #include <MobileCoreServices/MobileCoreServices.h>
 #endif
 
+#if USE(APPKIT)
+using PlatformColor = NSColor;
+#else
+using PlatformColor = UIColor;
+#endif
+
 @interface WKWebView ()
 - (void)paste:(id)sender;
 @end
@@ -76,9 +82,12 @@
 }
 #endif
 
-static RetainPtr<TestWKWebView> createWebViewWithCustomPasteboardDataEnabled()
+static RetainPtr<TestWKWebView> createWebViewWithCustomPasteboardDataEnabled(bool colorFilterEnabled = false)
 {
-    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]);
+    auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [webViewConfiguration _setColorFilterEnabled:colorFilterEnabled];
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400) configuration:webViewConfiguration.get()]);
     auto preferences = (__bridge WKPreferencesRef)[[webView configuration] preferences];
     WKPreferencesSetDataTransferItemsEnabled(preferences, true);
     WKPreferencesSetCustomPasteboardDataEnabled(preferences, true);
@@ -176,7 +185,68 @@
     EXPECT_WK_STREQ("blob:", [webView stringByEvaluatingJavaScript:@"new URL(imageElement.src).protocol"]);
 }
 
-#endif // PLATFORM(MAC)
+#if ENABLE(DARK_MODE_CSS) && HAVE(OS_DARK_MODE_SUPPORT)
 
+TEST(PasteRTFD, TransformColorsOfDarkContent)
+{
+    auto webView = createWebViewWithCustomPasteboardDataEnabled(true);
+    [webView forceDarkMode];
 
+    [webView synchronouslyLoadTestPageNamed:@"rich-color-filtered"];
 
+    PlatformColor *textColor = [PlatformColor lightGrayColor];
+    PlatformColor *backgroundColor = [PlatformColor darkGrayColor];
+
+    auto hello = adoptNS([[NSAttributedString alloc] initWithString:@"Hello" attributes:@{ NSBackgroundColorAttributeName : backgroundColor, NSForegroundColorAttributeName : textColor }]);
+    auto world = adoptNS([[NSAttributedString alloc] initWithString:@" World" attributes:@{ NSForegroundColorAttributeName : textColor }]);
+    auto string = adoptNS([[NSMutableAttributedString alloc] init]);
+    [string appendAttributedString:hello.get()];
+    [string appendAttributedString:world.get()];
+
+    writeRTFToPasteboard([string RTFFromRange:NSMakeRange(0, [string length]) documentAttributes:@{ }]);
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView paste:nil];
+
+#if USE(APPKIT)
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p').style.color"], @"rgb(126, 126, 126)");
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p > span').style.backgroundColor"], @"rgb(235, 235, 235)");
+#else
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p').style.color"], @"rgb(106, 106, 106)");
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p > span').style.backgroundColor"], @"rgb(213, 213, 213)");
+#endif
+}
+
+TEST(PasteRTFD, DoesNotTransformColorsOfLightContent)
+{
+    auto webView = createWebViewWithCustomPasteboardDataEnabled(true);
+    [webView forceDarkMode];
+
+    [webView synchronouslyLoadTestPageNamed:@"rich-color-filtered"];
+
+    PlatformColor *textColor = [PlatformColor darkGrayColor];
+    PlatformColor *backgroundColor = [PlatformColor lightGrayColor];
+
+    auto hello = adoptNS([[NSAttributedString alloc] initWithString:@"Hello" attributes:@{ NSBackgroundColorAttributeName : backgroundColor, NSForegroundColorAttributeName : textColor }]);
+    auto world = adoptNS([[NSAttributedString alloc] initWithString:@" World" attributes:@{ NSForegroundColorAttributeName : textColor }]);
+    auto string = adoptNS([[NSMutableAttributedString alloc] init]);
+    [string appendAttributedString:hello.get()];
+    [string appendAttributedString:world.get()];
+
+    writeRTFToPasteboard([string RTFFromRange:NSMakeRange(0, [string length]) documentAttributes:@{ }]);
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView paste:nil];
+
+#if USE(APPKIT)
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p').style.color"], @"rgb(67, 67, 67)");
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p > span').style.backgroundColor"], @"rgb(154, 154, 154)");
+#else
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p').style.color"], @"rgb(85, 85, 85)");
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"rich.querySelector('p > span').style.backgroundColor"], @"rgb(170, 170, 170)");
+#endif
+}
+
+#endif // ENABLE(DARK_MODE_CSS) && HAVE(OS_DARK_MODE_SUPPORT)
+
+#endif // PLATFORM(COCOA)

Added: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/rich-color-filtered.html (0 => 249517)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/rich-color-filtered.html	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/rich-color-filtered.html	2019-09-05 02:52:40 UTC (rev 249517)
@@ -0,0 +1,27 @@
+<head>
+    <meta name="viewport" content="width=device-width">
+    <style>
+    :root {
+        color-scheme: dark;
+        -apple-color-filter: apple-invert-lightness();
+    }
+    </style>
+</head>
+
+<body>
+    <div id="rich" contenteditable>Hello world</div>
+    <script>
+    function deselectText()
+    {
+        blur();
+        getSelection().removeAllRanges();
+    }
+
+    function selectRichText()
+    {
+        deselectText();
+        let text = rich.childNodes[0];
+        getSelection().setBaseAndExtent(text, 0, text, text.data.length);
+    }
+    </script>
+</body>

Modified: trunk/Tools/TestWebKitAPI/cocoa/TestWKWebView.h (249516 => 249517)


--- trunk/Tools/TestWebKitAPI/cocoa/TestWKWebView.h	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Tools/TestWebKitAPI/cocoa/TestWKWebView.h	2019-09-05 02:52:40 UTC (rev 249517)
@@ -75,6 +75,7 @@
 - (void)waitForMessage:(NSString *)message;
 - (void)performAfterLoading:(dispatch_block_t)actions;
 - (void)waitForNextPresentationUpdate;
+- (void)forceDarkMode;
 - (NSString *)stylePropertyAtSelectionStart:(NSString *)propertyName;
 - (NSString *)stylePropertyAtSelectionEnd:(NSString *)propertyName;
 - (void)collapseToStart;

Modified: trunk/Tools/TestWebKitAPI/cocoa/TestWKWebView.mm (249516 => 249517)


--- trunk/Tools/TestWebKitAPI/cocoa/TestWKWebView.mm	2019-09-05 02:38:03 UTC (rev 249516)
+++ trunk/Tools/TestWebKitAPI/cocoa/TestWKWebView.mm	2019-09-05 02:52:40 UTC (rev 249517)
@@ -434,6 +434,17 @@
     TestWebKitAPI::Util::run(&done);
 }
 
+- (void)forceDarkMode
+{
+#if HAVE(OS_DARK_MODE_SUPPORT)
+#if USE(APPKIT)
+    [self setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
+#else
+    [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
+#endif
+#endif
+}
+
 - (NSString *)stylePropertyAtSelectionStart:(NSString *)propertyName
 {
     NSString *script = [NSString stringWithFormat:@"getComputedStyle(getSelection().getRangeAt(0).startContainer.parentElement)['%@']", propertyName];
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to