Title: [286894] trunk
Revision
286894
Author
drou...@apple.com
Date
2021-12-10 18:06:54 -0800 (Fri, 10 Dec 2021)

Log Message

WKWebView doesn’t respond to -copyFont: and -pasteFont:
https://bugs.webkit.org/show_bug.cgi?id=191379
<rdar://problem/45878659>

Reviewed by Wenson Hsieh.

Source/WebCore:

Test: editing/execCommand/mac/copyFont-pasteFont.html

* dom/DOMPasteAccess.h:
Add `DOMPasteAccessCategory::Fonts` (which corresponds to `NSPasteboardNameFont` on macOS).

* editing/Editor.h:
* editing/Editor.cpp:
(WebCore::eventNameForClipboardEvent):
(WebCore::createDataTransferForClipboardEvent):
(WebCore::Editor::copyFont): Added.
(WebCore::Editor::pasteFont): Added.
Add platform-agnostic handling for `"CopyFont"` and `"PasteFont"` editor commands.

* editing/mac/EditorMac.mm:
(WebCore::Editor::platformCopyFont): Added.
(WebCore::Editor::platformPasteFont): Added.
Mimic the logic in `-[WebHTMLView copyFont:]`/`-[WebHTMLView pasteFont:]` to be compatible
with WK1 (i.e. to make it easier for clients to switch to WK2).

* editing/gtk/EditorGtk.cpp:
(WebCore::Editor::platformCopyFont): Added.
(WebCore::Editor::platformPasteFont): Added.
* editing/ios/EditorIOS.mm:
(WebCore::Editor::platformCopyFont): Added.
(WebCore::Editor::platformPasteFont): Added.
* editing/libwpe/EditorLibWPE.cpp:
(WebCore::Editor::platformCopyFont): Added.
(WebCore::Editor::platformPasteFont): Added.
* editing/win/EditorWin.cpp:
(WebCore::Editor::platformCopyFont): Added.
(WebCore::Editor::platformPasteFont): Added.
Add stubs for other platforms.

* editing/EditorCommand.cpp:
(WebCore::executeCopyFont): Added.
(WebCore::executePasteFont): Added.
(WebCore::createCommandMap):
Hook `"CopyFont"` and `"PasteFont"` editor commands up to `document.execCommand`.

Source/WebKit:

* UIProcess/API/mac/WKView.mm:
* UIProcess/API/mac/WKWebViewMac.mm:
Add `-copyFont:` and `-pasteFont:` methods via `WEBCORE_COMMAND`.

* UIProcess/WebPageProxy.cpp:
(WebKit::pasteAccessCategoryForCommand):
The `PasteFont` command should be categorized as `DOMPasteAccessCategory::Fonts`.

* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::pasteboardNameForAccessCategory):
(WebKit::pasteboardForAccessCategory):
* UIProcess/mac/WebPageProxyMac.mm:
(WebKit::WebPageProxy::willPerformPasteCommand):
Correlate `DOMPasteAccessCategory::Fonts` with `NSPasteboardNameFont`.

* UIProcess/ios/WKContentViewInteraction.mm:
(pasteboardNameForAccessCategory):
(pasteboardForAccessCategory):
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::willPerformPasteCommand):
Correlate `DOMPasteAccessCategory::Fonts` with `UIPasteboardNameGeneral`.

LayoutTests:

* editing/editing.js:
(execCopyFontCommand): Added.
(copyFontCommand): Added.
(execPasteFontCommand): Added.
(pasteFontCommand): Added.
* editing/execCommand/mac/copyFont-pasteFont.html: Added.
* editing/execCommand/mac/copyFont-pasteFont-expected.html: Added.

* TestExpectations:
* platform/mac/TestExpectations:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (286893 => 286894)


--- trunk/LayoutTests/ChangeLog	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/LayoutTests/ChangeLog	2021-12-11 02:06:54 UTC (rev 286894)
@@ -1,3 +1,22 @@
+2021-12-10  Devin Rousso  <drou...@apple.com>
+
+        WKWebView doesn’t respond to -copyFont: and -pasteFont:
+        https://bugs.webkit.org/show_bug.cgi?id=191379
+        <rdar://problem/45878659>
+
+        Reviewed by Wenson Hsieh.
+
+        * editing/editing.js:
+        (execCopyFontCommand): Added.
+        (copyFontCommand): Added.
+        (execPasteFontCommand): Added.
+        (pasteFontCommand): Added.
+        * editing/execCommand/mac/copyFont-pasteFont.html: Added.
+        * editing/execCommand/mac/copyFont-pasteFont-expected.html: Added.
+
+        * TestExpectations:
+        * platform/mac/TestExpectations:
+
 2021-12-10  Myles C. Maxfield  <mmaxfi...@apple.com>
 
         [Cocoa] OT-SVG glyphs don't draw into canvases (because of the GPU process)

Modified: trunk/LayoutTests/TestExpectations (286893 => 286894)


--- trunk/LayoutTests/TestExpectations	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/LayoutTests/TestExpectations	2021-12-11 02:06:54 UTC (rev 286894)
@@ -20,6 +20,7 @@
 editing/caret/ios [ Skip ]
 editing/editable-region [ Skip ]
 editing/execCommand/ios [ Skip ]
+editing/execCommand/mac [ Skip ]
 editing/input/cocoa [ Skip ]
 editing/input/ios [ Skip ]
 editing/find [ Skip ]

Modified: trunk/LayoutTests/editing/editing.js (286893 => 286894)


--- trunk/LayoutTests/editing/editing.js	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/LayoutTests/editing/editing.js	2021-12-11 02:06:54 UTC (rev 286894)
@@ -811,6 +811,34 @@
 
 //-------------------------------------------------------------------------------------------------------
 
+function execCopyFontCommand() {
+    document.execCommand("CopyFont");
+    debugForDumpAsText("execCopyFontCommand");
+}
+function copyFontCommand() {
+    if (commandDelay > 0) {
+        queueCommand(execCopyFontCommand, commandCount * commandDelay);
+        commandCount++;
+    } else
+        execCopyFontCommand();
+}
+
+//-------------------------------------------------------------------------------------------------------
+
+function execPasteFontCommand() {
+    document.execCommand("PasteFont");
+    debugForDumpAsText("execPasteFontCommand");
+}
+function pasteFontCommand() {
+    if (commandDelay > 0) {
+        queueCommand(execPasteFontCommand, commandCount * commandDelay);
+        commandCount++;
+    } else
+        execPasteFontCommand();
+}
+
+//-------------------------------------------------------------------------------------------------------
+
 function execCreateLinkCommand(url) {
     document.execCommand("CreateLink", false, url);
     debugForDumpAsText("execCreateLinkCommand");

Added: trunk/LayoutTests/editing/execCommand/mac/copyFont-pasteFont-expected.html (0 => 286894)


--- trunk/LayoutTests/editing/execCommand/mac/copyFont-pasteFont-expected.html	                        (rev 0)
+++ trunk/LayoutTests/editing/execCommand/mac/copyFont-pasteFont-expected.html	2021-12-11 02:06:54 UTC (rev 286894)
@@ -0,0 +1,20 @@
+<html>
+<head>
+    <style>
+#copy {
+    background-color: green;
+    font-family: Arial;
+    font-size: 20px;
+    font-weight: bold;
+    font-style: italic;
+    color: yellow;
+    text-shadow: black 1px 1px;
+    vertical-align: sub;
+}
+    </style>
+</head>
+<body>
+    <div id="copy">copy this</div>
+    <div id="paste" contenteditable=""><font color="#ffff00" face="Arial"><span style="background-color: rgb(0, 128, 0); font-size: 20px; text-shadow: rgb(0, 0, 0) 1px 1px;"><b><i><sub>paste here</sub></i></b></span></font></div>
+</body>
+</html>

Added: trunk/LayoutTests/editing/execCommand/mac/copyFont-pasteFont.html (0 => 286894)


--- trunk/LayoutTests/editing/execCommand/mac/copyFont-pasteFont.html	                        (rev 0)
+++ trunk/LayoutTests/editing/execCommand/mac/copyFont-pasteFont.html	2021-12-11 02:06:54 UTC (rev 286894)
@@ -0,0 +1,46 @@
+<html>
+<head>
+    <script src=""
+    <script>
+function editingTest() {
+    let selection = window.getSelection();
+
+    let copyRange = document.createRange();
+    copyRange.selectNodeContents(document.getElementById("copy"));
+    selection.removeAllRanges();
+    selection.addRange(copyRange);
+    copyFontCommand();
+
+    let pasteRange = document.createRange();
+    pasteRange.selectNodeContents(document.getElementById("paste"));
+    selection.removeAllRanges();
+    selection.addRange(pasteRange);
+    pasteFontCommand();
+
+    selection.removeAllRanges();
+}
+    </script>
+    <style>
+#copy {
+    background-color: green;
+    font-family: Arial;
+    font-size: 20px;
+    font-weight: bold;
+    font-style: italic;
+    color: yellow;
+    text-shadow: black 1px 1px;
+    vertical-align: sub;
+}
+#paste {
+    outline: none;
+}
+    </style>
+</head>
+<body>
+    <div id="copy">copy this</div>
+    <div id="paste" contenteditable>paste here</div>
+    <script>
+runEditingTest();
+    </script>
+</body>
+</html>

Modified: trunk/LayoutTests/platform/mac/TestExpectations (286893 => 286894)


--- trunk/LayoutTests/platform/mac/TestExpectations	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/LayoutTests/platform/mac/TestExpectations	2021-12-11 02:06:54 UTC (rev 286894)
@@ -9,6 +9,7 @@
 accessibility/mac [ Pass ]
 compositing/scrolling/async-overflow-scrolling/mac [ Pass ]
 displaylists [ Pass ]
+editing/execCommand/mac [ Pass ]
 editing/input/cocoa [ Pass ]
 editing/mac [ Pass ]
 editing/pasteboard/mac [ Pass ]

Modified: trunk/Source/WebCore/ChangeLog (286893 => 286894)


--- trunk/Source/WebCore/ChangeLog	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/ChangeLog	2021-12-11 02:06:54 UTC (rev 286894)
@@ -1,3 +1,50 @@
+2021-12-10  Devin Rousso  <drou...@apple.com>
+
+        WKWebView doesn’t respond to -copyFont: and -pasteFont:
+        https://bugs.webkit.org/show_bug.cgi?id=191379
+        <rdar://problem/45878659>
+
+        Reviewed by Wenson Hsieh.
+
+        Test: editing/execCommand/mac/copyFont-pasteFont.html
+
+        * dom/DOMPasteAccess.h:
+        Add `DOMPasteAccessCategory::Fonts` (which corresponds to `NSPasteboardNameFont` on macOS).
+
+        * editing/Editor.h:
+        * editing/Editor.cpp:
+        (WebCore::eventNameForClipboardEvent):
+        (WebCore::createDataTransferForClipboardEvent):
+        (WebCore::Editor::copyFont): Added.
+        (WebCore::Editor::pasteFont): Added.
+        Add platform-agnostic handling for `"CopyFont"` and `"PasteFont"` editor commands.
+
+        * editing/mac/EditorMac.mm:
+        (WebCore::Editor::platformCopyFont): Added.
+        (WebCore::Editor::platformPasteFont): Added.
+        Mimic the logic in `-[WebHTMLView copyFont:]`/`-[WebHTMLView pasteFont:]` to be compatible
+        with WK1 (i.e. to make it easier for clients to switch to WK2).
+
+        * editing/gtk/EditorGtk.cpp:
+        (WebCore::Editor::platformCopyFont): Added.
+        (WebCore::Editor::platformPasteFont): Added.
+        * editing/ios/EditorIOS.mm:
+        (WebCore::Editor::platformCopyFont): Added.
+        (WebCore::Editor::platformPasteFont): Added.
+        * editing/libwpe/EditorLibWPE.cpp:
+        (WebCore::Editor::platformCopyFont): Added.
+        (WebCore::Editor::platformPasteFont): Added.
+        * editing/win/EditorWin.cpp:
+        (WebCore::Editor::platformCopyFont): Added.
+        (WebCore::Editor::platformPasteFont): Added.
+        Add stubs for other platforms.
+
+        * editing/EditorCommand.cpp:
+        (WebCore::executeCopyFont): Added.
+        (WebCore::executePasteFont): Added.
+        (WebCore::createCommandMap):
+        Hook `"CopyFont"` and `"PasteFont"` editor commands up to `document.execCommand`.
+
 2021-12-10  Eric Carlson  <eric.carl...@apple.com>
 
         [macOS] Add new screen and window capture backend

Modified: trunk/Source/WebCore/dom/DOMPasteAccess.h (286893 => 286894)


--- trunk/Source/WebCore/dom/DOMPasteAccess.h	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/dom/DOMPasteAccess.h	2021-12-11 02:06:54 UTC (rev 286894)
@@ -31,6 +31,7 @@
 
 enum class DOMPasteAccessCategory : uint8_t {
     General,
+    Fonts,
 };
 
 enum class DOMPasteAccessPolicy : uint8_t {
@@ -52,7 +53,8 @@
 template<> struct EnumTraits<WebCore::DOMPasteAccessCategory> {
     using values = EnumValues<
         WebCore::DOMPasteAccessCategory,
-        WebCore::DOMPasteAccessCategory::General
+        WebCore::DOMPasteAccessCategory::General,
+        WebCore::DOMPasteAccessCategory::Fonts
     >;
 };
 

Modified: trunk/Source/WebCore/editing/Editor.cpp (286893 => 286894)


--- trunk/Source/WebCore/editing/Editor.cpp	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/Editor.cpp	2021-12-11 02:06:54 UTC (rev 286894)
@@ -372,8 +372,10 @@
 
 enum class ClipboardEventKind {
     Copy,
+    CopyFont,
     Cut,
     Paste,
+    PasteFont,
     PasteAsPlainText,
     PasteAsQuotation,
     BeforeCopy,
@@ -385,6 +387,7 @@
 {
     switch (kind) {
     case ClipboardEventKind::Copy:
+    case ClipboardEventKind::CopyFont:
         return eventNames().copyEvent;
     case ClipboardEventKind::Cut:
         return eventNames().cutEvent;
@@ -391,6 +394,7 @@
     case ClipboardEventKind::Paste:
     case ClipboardEventKind::PasteAsPlainText:
     case ClipboardEventKind::PasteAsQuotation:
+    case ClipboardEventKind::PasteFont:
         return eventNames().pasteEvent;
     case ClipboardEventKind::BeforeCopy:
         return eventNames().beforecopyEvent;
@@ -407,6 +411,7 @@
 {
     switch (kind) {
     case ClipboardEventKind::Copy:
+    case ClipboardEventKind::CopyFont:
     case ClipboardEventKind::Cut:
         return DataTransfer::createForCopyAndPaste(document, DataTransfer::StoreMode::ReadWrite, makeUnique<StaticPasteboard>());
     case ClipboardEventKind::PasteAsPlainText:
@@ -420,6 +425,7 @@
         FALLTHROUGH;
     case ClipboardEventKind::Paste:
     case ClipboardEventKind::PasteAsQuotation:
+    case ClipboardEventKind::PasteFont:
         return DataTransfer::createForCopyAndPaste(document, DataTransfer::StoreMode::Readonly, Pasteboard::createForCopyAndPaste(PagePasteboardContext::create(document.pageID())));
     case ClipboardEventKind::BeforeCopy:
     case ClipboardEventKind::BeforeCut:
@@ -1404,6 +1410,21 @@
     performCutOrCopy(CopyAction);
 }
 
+void Editor::copyFont(FromMenuOrKeyBinding fromMenuOrKeyBinding)
+{
+    SetForScope<bool> copyScope { m_copyingFromMenuOrKeyBinding, fromMenuOrKeyBinding == FromMenuOrKeyBinding::Yes };
+    if (tryDHTMLCopy())
+        return; // DHTML did the whole operation
+    if (!canCopy()) {
+        SystemSoundManager::singleton().systemBeep();
+        return;
+    }
+
+    willWriteSelectionToPasteboard(selectedRange());
+    platformCopyFont();
+    didWriteSelectionToPasteboard();
+}
+
 void Editor::postTextStateChangeNotificationForCut(const String& text, const VisibleSelection& selection)
 {
     if (!AXObjectCache::accessibilityEnabled())
@@ -1512,6 +1533,19 @@
         pasteAsPlainTextWithPasteboard(*pasteboard);
 }
 
+void Editor::pasteFont(FromMenuOrKeyBinding fromMenuOrKeyBinding)
+{
+    SetForScope<bool> pasteScope { m_pastingFromMenuOrKeyBinding, fromMenuOrKeyBinding == FromMenuOrKeyBinding::Yes };
+
+    if (!dispatchClipboardEvent(findEventTargetFromSelection(), ClipboardEventKind::PasteFont))
+        return;
+    if (!canPaste())
+        return;
+    updateMarkersForWordsAffectedByEditing(false);
+    ResourceCacheValidationSuppressor validationSuppressor(document().cachedResourceLoader());
+    platformPasteFont();
+}
+
 void Editor::quoteFragmentForPasting(DocumentFragment& fragment)
 {
     auto blockQuote = HTMLQuoteElement::create(blockquoteTag, document());

Modified: trunk/Source/WebCore/editing/Editor.h (286893 => 286894)


--- trunk/Source/WebCore/editing/Editor.h	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/Editor.h	2021-12-11 02:06:54 UTC (rev 286894)
@@ -197,11 +197,13 @@
     enum class FromMenuOrKeyBinding : bool { No, Yes };
     WEBCORE_EXPORT void cut(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
     WEBCORE_EXPORT void copy(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
+    void copyFont(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
 
     WEBCORE_EXPORT void paste(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
     void paste(Pasteboard&, FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
     WEBCORE_EXPORT void pasteAsPlainText(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
     void pasteAsQuotation(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
+    void pasteFont(FromMenuOrKeyBinding = FromMenuOrKeyBinding::No);
     WEBCORE_EXPORT void performDelete();
 
     WEBCORE_EXPORT void copyURL(const URL&, const String& title);
@@ -592,6 +594,9 @@
     void pasteWithPasteboard(Pasteboard*, OptionSet<PasteOption>);
     String plainTextFromPasteboard(const PasteboardPlainText&);
 
+    void platformCopyFont();
+    void platformPasteFont();
+
     void quoteFragmentForPasting(DocumentFragment&);
 
     void revealSelectionAfterEditingOperation(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent);

Modified: trunk/Source/WebCore/editing/EditorCommand.cpp (286893 => 286894)


--- trunk/Source/WebCore/editing/EditorCommand.cpp	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/EditorCommand.cpp	2021-12-11 02:06:54 UTC (rev 286894)
@@ -242,6 +242,12 @@
     return true;
 }
 
+static bool executeCopyFont(Frame& frame, Event*, EditorCommandSource source, const String&)
+{
+    frame.editor().copyFont(source == CommandFromMenuOrKeyBinding ? Editor::FromMenuOrKeyBinding::Yes : Editor::FromMenuOrKeyBinding::No);
+    return true;
+}
+
 static bool executeCreateLink(Frame& frame, Event*, EditorCommandSource, const String& value)
 {
     // FIXME: If userInterface is true, we should display a dialog box to let the user enter a URL.
@@ -906,6 +912,21 @@
     return true;
 }
 
+static bool executePasteFont(Frame& frame, Event*, EditorCommandSource source, const String&)
+{
+    if (source == CommandFromMenuOrKeyBinding) {
+        UserTypingGestureIndicator typingGestureIndicator(frame);
+        frame.editor().pasteFont(Editor::FromMenuOrKeyBinding::Yes);
+        return true;
+    }
+
+    if (!frame.requestDOMPasteAccess(DOMPasteAccessCategory::Fonts))
+        return false;
+
+    frame.editor().pasteFont();
+    return true;
+}
+
 #if PLATFORM(GTK)
 
 static bool executePasteGlobalSelection(Frame& frame, Event*, EditorCommandSource source, const String&)
@@ -1614,6 +1635,7 @@
         { "Bold"_s, { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
         { "ClearText"_s, { executeClearText, supported, enabledClearText, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
         { "Copy"_s, { executeCopy, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledCopyCut } },
+        { "CopyFont"_s, { executeCopyFont, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledCopyCut } },
         { "CreateLink"_s, { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
         { "Cut"_s, { executeCut, supportedCopyCut, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledCopyCut } },
         { "DefaultParagraphSeparator"_s, { executeDefaultParagraphSeparator, supported, enabled, stateNone, valueDefaultParagraphSeparator, notTextInsertion, doNotAllowExecutionWhenDisabled} },
@@ -1713,6 +1735,7 @@
         { "PasteAndMatchStyle"_s, { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledPaste } },
         { "PasteAsPlainText"_s, { executePasteAsPlainText, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledPaste } },
         { "PasteAsQuotation"_s, { executePasteAsQuotation, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledPaste } },
+        { "PasteFont"_s, { executePasteFont, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabledPaste } },
         { "Print"_s, { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
         { "Redo"_s, { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
         { "RemoveFormat"_s, { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },

Modified: trunk/Source/WebCore/editing/gtk/EditorGtk.cpp (286893 => 286894)


--- trunk/Source/WebCore/editing/gtk/EditorGtk.cpp	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/gtk/EditorGtk.cpp	2021-12-11 02:06:54 UTC (rev 286894)
@@ -65,6 +65,14 @@
         pasteAsFragment(fragment.releaseNonNull(), canSmartReplaceWithPasteboard(*pasteboard), chosePlainText, options.contains(PasteOption::IgnoreMailBlockquote) ? MailBlockquoteHandling::IgnoreBlockquote : MailBlockquoteHandling::RespectBlockquote);
 }
 
+void Editor::platformCopyFont()
+{
+}
+
+void Editor::platformPasteFont()
+{
+}
+
 static const AtomString& elementURL(Element& element)
 {
     if (is<HTMLImageElement>(element) || is<HTMLInputElement>(element))

Modified: trunk/Source/WebCore/editing/ios/EditorIOS.mm (286893 => 286894)


--- trunk/Source/WebCore/editing/ios/EditorIOS.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/ios/EditorIOS.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -238,6 +238,14 @@
         pasteAsFragment(fragment.releaseNonNull(), canSmartReplaceWithPasteboard(*pasteboard), false, options.contains(PasteOption::IgnoreMailBlockquote) ? MailBlockquoteHandling::IgnoreBlockquote : MailBlockquoteHandling::RespectBlockquote);
 }
 
+void Editor::platformCopyFont()
+{
+}
+
+void Editor::platformPasteFont()
+{
+}
+
 void Editor::insertDictationPhrases(Vector<Vector<String>>&& dictationPhrases, id metadata)
 {
     if (m_document.selection().isNone())

Modified: trunk/Source/WebCore/editing/libwpe/EditorLibWPE.cpp (286893 => 286894)


--- trunk/Source/WebCore/editing/libwpe/EditorLibWPE.cpp	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/libwpe/EditorLibWPE.cpp	2021-12-11 02:06:54 UTC (rev 286894)
@@ -91,6 +91,14 @@
         pasteAsFragment(*fragment, canSmartReplaceWithPasteboard(*pasteboard), chosePlainText, options.contains(PasteOption::IgnoreMailBlockquote) ? MailBlockquoteHandling::IgnoreBlockquote : MailBlockquoteHandling::RespectBlockquote);
 }
 
+void Editor::platformCopyFont()
+{
+}
+
+void Editor::platformPasteFont()
+{
+}
+
 } // namespace WebCore
 
 #endif // USE(LIBWPE)

Modified: trunk/Source/WebCore/editing/mac/EditorMac.mm (286893 => 286894)


--- trunk/Source/WebCore/editing/mac/EditorMac.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/mac/EditorMac.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -31,10 +31,15 @@
 #import "Blob.h"
 #import "CSSPrimitiveValueMappings.h"
 #import "CSSValuePool.h"
+#import "Color.h"
+#import "ColorCocoa.h"
+#import "ColorSerialization.h"
 #import "DataTransfer.h"
 #import "DocumentFragment.h"
 #import "Editing.h"
 #import "EditorClient.h"
+#import "FontAttributes.h"
+#import "FontShadow.h"
 #import "Frame.h"
 #import "FrameView.h"
 #import "HTMLConverter.h"
@@ -50,11 +55,13 @@
 #import "RenderImage.h"
 #import "RuntimeApplicationChecks.h"
 #import "RuntimeEnabledFeatures.h"
+#import "SharedBuffer.h"
 #import "StyleProperties.h"
 #import "WebContentReader.h"
 #import "WebNSAttributedStringExtras.h"
 #import "markup.h"
 #import <AppKit/AppKit.h>
+#import <wtf/RetainPtr.h>
 #import <wtf/cocoa/NSURLExtras.h>
 
 namespace WebCore {
@@ -99,6 +106,83 @@
     client()->setInsertionPasteboard(String());
 }
 
+void Editor::platformCopyFont()
+{
+    Pasteboard pasteboard(PagePasteboardContext::create(m_document.pageID()), NSPasteboardNameFont);
+
+    auto fontSampleString = adoptNS([[NSAttributedString alloc] initWithString:@"x" attributes:fontAttributesAtSelectionStart().createDictionary().get()]);
+    auto fontData = RetainPtr([fontSampleString RTFFromRange:NSMakeRange(0, [fontSampleString length]) documentAttributes:@{ }]);
+
+    PasteboardBuffer pasteboardBuffer;
+    pasteboardBuffer.contentOrigin = m_document.originIdentifierForPasteboard();
+    pasteboardBuffer.type = legacyFontPasteboardType();
+    pasteboardBuffer.data = ""
+    pasteboard.write(pasteboardBuffer);
+}
+
+void Editor::platformPasteFont()
+{
+    Pasteboard pasteboard(PagePasteboardContext::create(m_document.pageID()), NSPasteboardNameFont);
+
+    client()->setInsertionPasteboard(pasteboard.name());
+
+    RetainPtr<NSData> fontData;
+    if (auto buffer = pasteboard.readBuffer(std::nullopt, legacyFontPasteboardType()))
+        fontData = buffer->createNSData();
+    auto fontSampleString = adoptNS([[NSAttributedString alloc] initWithRTF:fontData.get() documentAttributes:nil]);
+    auto fontAttributes = RetainPtr([fontSampleString fontAttributesInRange:NSMakeRange(0, 1)]);
+
+    auto style = MutableStyleProperties::create();
+
+    Color backgroundColor;
+    if (NSColor *nsBackgroundColor = dynamic_objc_cast<NSColor>([fontAttributes objectForKey:NSBackgroundColorAttributeName]))
+        backgroundColor = colorFromCocoaColor(nsBackgroundColor);
+    if (!backgroundColor.isValid())
+        backgroundColor = Color::transparentBlack;
+    style->setProperty(CSSPropertyBackgroundColor, CSSPrimitiveValue::create(backgroundColor));
+
+    if (NSFont *font = dynamic_objc_cast<NSFont>([fontAttributes objectForKey:NSFontAttributeName])) {
+        // FIXME: Need more sophisticated escaping code if we want to handle family names
+        // with characters like single quote or backslash in their names.
+        style->setProperty(CSSPropertyFontFamily, [NSString stringWithFormat:@"'%@'", [font familyName]]);
+        style->setProperty(CSSPropertyFontSize, CSSPrimitiveValue::create([font pointSize], CSSUnitType::CSS_PX));
+        // FIXME: Map to the entire range of CSS weight values.
+        style->setProperty(CSSPropertyFontWeight, ([NSFontManager.sharedFontManager weightOfFont:font] >= 7) ? CSSValueBold : CSSValueNormal);
+        style->setProperty(CSSPropertyFontStyle, ([NSFontManager.sharedFontManager traitsOfFont:font] & NSItalicFontMask) ? CSSValueItalic : CSSValueNormal);
+    } else {
+        style->setProperty(CSSPropertyFontFamily, "Helvetica"_s);
+        style->setProperty(CSSPropertyFontSize, CSSPrimitiveValue::create(12, CSSUnitType::CSS_PX));
+        style->setProperty(CSSPropertyFontWeight, CSSValueNormal);
+        style->setProperty(CSSPropertyFontStyle, CSSValueNormal);
+    }
+
+    Color foregroundColor;
+    if (NSColor *nsForegroundColor = dynamic_objc_cast<NSColor>([fontAttributes objectForKey:NSForegroundColorAttributeName])) {
+        foregroundColor = colorFromCocoaColor(nsForegroundColor);
+        if (!foregroundColor.isValid())
+            foregroundColor = Color::transparentBlack;
+    } else
+        foregroundColor = Color::black;
+    style->setProperty(CSSPropertyColor, CSSPrimitiveValue::create(foregroundColor));
+
+    FontShadow fontShadow;
+    if (NSShadow *nsFontShadow = dynamic_objc_cast<NSShadow>([fontAttributes objectForKey:NSShadowAttributeName]))
+        fontShadow = fontShadowFromNSShadow(nsFontShadow);
+    style->setProperty(CSSPropertyTextShadow, serializationForCSS(fontShadow));
+
+    auto superscriptStyle = [[fontAttributes objectForKey:NSSuperscriptAttributeName] intValue];
+    style->setProperty(CSSPropertyVerticalAlign, (superscriptStyle > 0) ? CSSValueSuper : ((superscriptStyle < 0) ? CSSValueSub : CSSValueBaseline));
+
+    // FIXME: Underline wins here if we have both (see bug 3790443).
+    auto underlineStyle = [[fontAttributes objectForKey:NSUnderlineStyleAttributeName] intValue];
+    auto strikethroughStyle = [[fontAttributes objectForKey:NSStrikethroughStyleAttributeName] intValue];
+    style->setProperty(CSSPropertyWebkitTextDecorationsInEffect, (underlineStyle != NSUnderlineStyleNone) ? CSSValueUnderline : ((strikethroughStyle != NSUnderlineStyleNone) ? CSSValueLineThrough : CSSValueNone));
+
+    applyStyleToSelection(style.ptr(), EditAction::PasteFont);
+
+    client()->setInsertionPasteboard(String());
+}
+
 void Editor::readSelectionFromPasteboard(const String& pasteboardName)
 {
     Pasteboard pasteboard(PagePasteboardContext::create(m_document.pageID()), pasteboardName);

Modified: trunk/Source/WebCore/editing/win/EditorWin.cpp (286893 => 286894)


--- trunk/Source/WebCore/editing/win/EditorWin.cpp	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebCore/editing/win/EditorWin.cpp	2021-12-11 02:06:54 UTC (rev 286894)
@@ -52,6 +52,14 @@
         pasteAsFragment(fragment.releaseNonNull(), canSmartReplaceWithPasteboard(*pasteboard), chosePlainText, options.contains(PasteOption::IgnoreMailBlockquote) ? MailBlockquoteHandling::IgnoreBlockquote : MailBlockquoteHandling::RespectBlockquote);
 }
 
+void Editor::platformCopyFont()
+{
+}
+
+void Editor::platformPasteFont()
+{
+}
+
 template <typename PlatformDragData>
 static RefPtr<DocumentFragment> createFragmentFromPlatformData(PlatformDragData& platformDragData, Frame& frame)
 {

Modified: trunk/Source/WebKit/ChangeLog (286893 => 286894)


--- trunk/Source/WebKit/ChangeLog	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/ChangeLog	2021-12-11 02:06:54 UTC (rev 286894)
@@ -1,3 +1,33 @@
+2021-12-10  Devin Rousso  <drou...@apple.com>
+
+        WKWebView doesn’t respond to -copyFont: and -pasteFont:
+        https://bugs.webkit.org/show_bug.cgi?id=191379
+        <rdar://problem/45878659>
+
+        Reviewed by Wenson Hsieh.
+
+        * UIProcess/API/mac/WKView.mm:
+        * UIProcess/API/mac/WKWebViewMac.mm:
+        Add `-copyFont:` and `-pasteFont:` methods via `WEBCORE_COMMAND`.
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::pasteAccessCategoryForCommand):
+        The `PasteFont` command should be categorized as `DOMPasteAccessCategory::Fonts`.
+
+        * UIProcess/Cocoa/WebViewImpl.mm:
+        (WebKit::pasteboardNameForAccessCategory):
+        (WebKit::pasteboardForAccessCategory):
+        * UIProcess/mac/WebPageProxyMac.mm:
+        (WebKit::WebPageProxy::willPerformPasteCommand):
+        Correlate `DOMPasteAccessCategory::Fonts` with `NSPasteboardNameFont`.
+
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (pasteboardNameForAccessCategory):
+        (pasteboardForAccessCategory):
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::willPerformPasteCommand):
+        Correlate `DOMPasteAccessCategory::Fonts` with `UIPasteboardNameGeneral`.
+
 2021-12-10  Eric Carlson  <eric.carl...@apple.com>
 
         [macOS] Add new screen and window capture backend

Modified: trunk/Source/WebKit/UIProcess/API/mac/WKView.mm (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/API/mac/WKView.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/API/mac/WKView.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -205,6 +205,7 @@
 WEBCORE_COMMAND(alignLeft)
 WEBCORE_COMMAND(alignRight)
 WEBCORE_COMMAND(copy)
+WEBCORE_COMMAND(copyFont)
 WEBCORE_COMMAND(cut)
 WEBCORE_COMMAND(delete)
 WEBCORE_COMMAND(deleteBackward)
@@ -278,6 +279,7 @@
 WEBCORE_COMMAND(pageUpAndModifySelection)
 WEBCORE_COMMAND(paste)
 WEBCORE_COMMAND(pasteAsPlainText)
+WEBCORE_COMMAND(pasteFont)
 WEBCORE_COMMAND(scrollPageDown)
 WEBCORE_COMMAND(scrollPageUp)
 WEBCORE_COMMAND(scrollLineDown)
@@ -339,11 +341,9 @@
 Editing-related methods still unimplemented that are implemented in WebKit1:
 
 - (void)complete:(id)sender;
-- (void)copyFont:(id)sender;
 - (void)makeBaseWritingDirectionLeftToRight:(id)sender;
 - (void)makeBaseWritingDirectionNatural:(id)sender;
 - (void)makeBaseWritingDirectionRightToLeft:(id)sender;
-- (void)pasteFont:(id)sender;
 - (void)scrollLineDown:(id)sender;
 - (void)scrollLineUp:(id)sender;
 - (void)showGuessPanel:(id)sender;

Modified: trunk/Source/WebKit/UIProcess/API/mac/WKWebViewMac.mm (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/API/mac/WKWebViewMac.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/API/mac/WKWebViewMac.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -167,6 +167,7 @@
 WEBCORE_COMMAND(alignLeft)
 WEBCORE_COMMAND(alignRight)
 WEBCORE_COMMAND(copy)
+WEBCORE_COMMAND(copyFont)
 WEBCORE_COMMAND(cut)
 WEBCORE_COMMAND(delete)
 WEBCORE_COMMAND(deleteBackward)
@@ -240,6 +241,7 @@
 WEBCORE_COMMAND(pageUpAndModifySelection)
 WEBCORE_COMMAND(paste)
 WEBCORE_COMMAND(pasteAsPlainText)
+WEBCORE_COMMAND(pasteFont)
 WEBCORE_COMMAND(scrollLineDown)
 WEBCORE_COMMAND(scrollLineUp)
 WEBCORE_COMMAND(scrollToBeginningOfDocument)

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -4590,6 +4590,9 @@
     switch (pasteAccessCategory) {
     case WebCore::DOMPasteAccessCategory::General:
         return NSPasteboardNameGeneral;
+
+    case WebCore::DOMPasteAccessCategory::Fonts:
+        return NSPasteboardNameFont;
     }
 }
 
@@ -4598,6 +4601,9 @@
     switch (pasteAccessCategory) {
     case WebCore::DOMPasteAccessCategory::General:
         return NSPasteboard.generalPasteboard;
+
+    case WebCore::DOMPasteAccessCategory::Fonts:
+        return [NSPasteboard pasteboardWithName:NSPasteboardNameFont];
     }
 }
 

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2021-12-11 02:06:54 UTC (rev 286894)
@@ -2521,6 +2521,7 @@
         { "PasteAndMatchStyle", DOMPasteAccessCategory::General },
         { "PasteAsQuotation", DOMPasteAccessCategory::General },
         { "PasteAsPlainText", DOMPasteAccessCategory::General },
+        { "PasteFont", DOMPasteAccessCategory::Fonts },
     };
 
     auto it = pasteCommandNames->find(commandName);

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -4173,6 +4173,7 @@
 {
     switch (pasteAccessCategory) {
     case WebCore::DOMPasteAccessCategory::General:
+    case WebCore::DOMPasteAccessCategory::Fonts:
         return UIPasteboardNameGeneral;
     }
 }
@@ -4181,6 +4182,7 @@
 {
     switch (pasteAccessCategory) {
     case WebCore::DOMPasteAccessCategory::General:
+    case WebCore::DOMPasteAccessCategory::Fonts:
         return UIPasteboard.generalPasteboard;
     }
 }

Modified: trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -1548,6 +1548,7 @@
 {
     switch (pasteAccessCategory) {
     case DOMPasteAccessCategory::General:
+    case DOMPasteAccessCategory::Fonts:
         grantAccessToCurrentPasteboardData(UIPasteboardNameGeneral);
         return;
     }

Modified: trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm (286893 => 286894)


--- trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm	2021-12-11 01:25:58 UTC (rev 286893)
+++ trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm	2021-12-11 02:06:54 UTC (rev 286894)
@@ -657,6 +657,10 @@
     case DOMPasteAccessCategory::General:
         grantAccessToCurrentPasteboardData(NSPasteboardNameGeneral);
         return;
+
+    case DOMPasteAccessCategory::Fonts:
+        grantAccessToCurrentPasteboardData(NSPasteboardNameFont);
+        return;
     }
 }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to