Title: [282769] trunk/Source
Revision
282769
Author
wenson_hs...@apple.com
Date
2021-09-20 12:32:34 -0700 (Mon, 20 Sep 2021)

Log Message

Refactor some code that controls Live Text selection behavior
https://bugs.webkit.org/show_bug.cgi?id=230482
rdar://83173597

Reviewed by Megan Gardner.

Source/WebCore:

Make a few adjustments to Live Text code. See below for more details.

* html/HTMLElement.cpp:
(WebCore::HTMLElement::shouldExtendSelectionToTargetNode):

Adjust this rule so that instead of checking whether we're hit-testing to the image overlay container `div`, we
only allow the existing selection in an image overlay to extend when performing a mouse drag if the target node
(to which we're extending the selection) is a text node inside the image overlay.

This tweak is needed to deal with the stylesheet adjustment below, where the root `#image-overlay` container now
has `pointer-events: none;`.

* html/shadow/imageOverlay.css:
(div#image-overlay):
(div.image-overlay-line):
(div.image-overlay-data-detector-result):

Mark the root `#image-overlay` container as `pointer-events: none;`, but mark the individual text containers in
the image overlay as `pointer-events: auto;` to ensure that they can still be selected via mouse events. This
change ensures that image overlays can be safely overlaid on top of other user agent shadow root content without
breaking pointer-based interactions on the shadow root content beneath it.

* page/EventHandler.cpp:
(WebCore::EventHandler::textRecognitionCandidateElement const):

Add a WebKitAdditions extension point for determining whether or not an element is a candidate for text
recognition.

Source/WTF:

Add an (off-by-default) internal feature to enable certain enhancements to Live Text.

* Scripts/Preferences/WebPreferencesInternal.yaml:

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (282768 => 282769)


--- trunk/Source/WTF/ChangeLog	2021-09-20 19:23:54 UTC (rev 282768)
+++ trunk/Source/WTF/ChangeLog	2021-09-20 19:32:34 UTC (rev 282769)
@@ -1,3 +1,15 @@
+2021-09-20  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        Refactor some code that controls Live Text selection behavior
+        https://bugs.webkit.org/show_bug.cgi?id=230482
+        rdar://83173597
+
+        Reviewed by Megan Gardner.
+
+        Add an (off-by-default) internal feature to enable certain enhancements to Live Text.
+
+        * Scripts/Preferences/WebPreferencesInternal.yaml:
+
 2021-09-20  Brent Fulgham  <bfulg...@apple.com>
 
         Remove XSS Auditor: Part 1 (Turn off by default)

Modified: trunk/Source/WTF/Scripts/Preferences/WebPreferencesInternal.yaml (282768 => 282769)


--- trunk/Source/WTF/Scripts/Preferences/WebPreferencesInternal.yaml	2021-09-20 19:23:54 UTC (rev 282768)
+++ trunk/Source/WTF/Scripts/Preferences/WebPreferencesInternal.yaml	2021-09-20 19:32:34 UTC (rev 282769)
@@ -740,6 +740,20 @@
     WebCore:
       default: false
 
+TextRecognitionEnhancementsEnabled:
+  type: bool
+  humanReadableName: "Text Recognition Enhancements"
+  humanReadableDescription: "Enable Text Recognition Enhancements"
+  condition: ENABLE(IMAGE_ANALYSIS)
+  exposed: [ WebKit ]
+  defaultValue:
+    WebCore:
+      default: false
+    WebKitLegacy:
+      default: false
+    WebKit:
+      default: false
+
 # FIXME: Is this implemented for WebKitLegacy? If not, this should be excluded from WebKitLegacy entirely.
 UndoManagerAPIEnabled:
   type: bool

Modified: trunk/Source/WebCore/ChangeLog (282768 => 282769)


--- trunk/Source/WebCore/ChangeLog	2021-09-20 19:23:54 UTC (rev 282768)
+++ trunk/Source/WebCore/ChangeLog	2021-09-20 19:32:34 UTC (rev 282769)
@@ -1,3 +1,39 @@
+2021-09-20  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        Refactor some code that controls Live Text selection behavior
+        https://bugs.webkit.org/show_bug.cgi?id=230482
+        rdar://83173597
+
+        Reviewed by Megan Gardner.
+
+        Make a few adjustments to Live Text code. See below for more details.
+
+        * html/HTMLElement.cpp:
+        (WebCore::HTMLElement::shouldExtendSelectionToTargetNode):
+
+        Adjust this rule so that instead of checking whether we're hit-testing to the image overlay container `div`, we
+        only allow the existing selection in an image overlay to extend when performing a mouse drag if the target node
+        (to which we're extending the selection) is a text node inside the image overlay.
+
+        This tweak is needed to deal with the stylesheet adjustment below, where the root `#image-overlay` container now
+        has `pointer-events: none;`.
+
+        * html/shadow/imageOverlay.css:
+        (div#image-overlay):
+        (div.image-overlay-line):
+        (div.image-overlay-data-detector-result):
+
+        Mark the root `#image-overlay` container as `pointer-events: none;`, but mark the individual text containers in
+        the image overlay as `pointer-events: auto;` to ensure that they can still be selected via mouse events. This
+        change ensures that image overlays can be safely overlaid on top of other user agent shadow root content without
+        breaking pointer-based interactions on the shadow root content beneath it.
+
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::textRecognitionCandidateElement const):
+
+        Add a WebKitAdditions extension point for determining whether or not an element is a candidate for text
+        recognition.
+
 2021-09-20  Antoine Quint  <grao...@webkit.org>
 
         box-shadow and text-shadow do not yield float values while interpolating

Modified: trunk/Source/WebCore/html/HTMLElement.cpp (282768 => 282769)


--- trunk/Source/WebCore/html/HTMLElement.cpp	2021-09-20 19:23:54 UTC (rev 282768)
+++ trunk/Source/WebCore/html/HTMLElement.cpp	2021-09-20 19:32:34 UTC (rev 282769)
@@ -1254,25 +1254,9 @@
 
 bool HTMLElement::shouldExtendSelectionToTargetNode(const Node& targetNode, const VisibleSelection& selectionBeforeUpdate)
 {
-    if (!is<HTMLDivElement>(targetNode))
-        return true;
+    if (auto range = selectionBeforeUpdate.range(); range && isInsideImageOverlay(*range))
+        return isImageOverlayText(targetNode);
 
-    auto shadowHost = makeRefPtr(targetNode.shadowHost());
-    if (!is<HTMLElement>(shadowHost))
-        return true;
-
-    auto& host = downcast<HTMLElement>(*shadowHost);
-    if (!host.hasImageOverlay())
-        return true;
-
-    if (!targetNode.contains(selectionBeforeUpdate.start().containerNode()))
-        return true;
-
-    for (auto& child : childrenOfType<HTMLDivElement>(*host.userAgentShadowRoot())) {
-        if (child.getIdAttribute() == imageOverlayElementIdentifier())
-            return &targetNode != &child;
-    }
-
     return true;
 }
 

Modified: trunk/Source/WebCore/html/shadow/imageOverlay.css (282768 => 282769)


--- trunk/Source/WebCore/html/shadow/imageOverlay.css	2021-09-20 19:23:54 UTC (rev 282768)
+++ trunk/Source/WebCore/html/shadow/imageOverlay.css	2021-09-20 19:32:34 UTC (rev 282769)
@@ -24,6 +24,7 @@
 
 div#image-overlay {
     display: inline-block;
+    pointer-events: none;
     position: relative;
     overflow: hidden;
     color: transparent;
@@ -40,6 +41,10 @@
     overflow: hidden;
 }
 
+div.image-overlay-line {
+    pointer-events: auto;
+}
+
 .image-overlay-text::selection {
     color: transparent;
     background-color: highlight;
@@ -48,5 +53,4 @@
 div.image-overlay-data-detector-result {
     position: absolute;
     -webkit-user-select: none;
-    pointer-events: none;
 }

Modified: trunk/Source/WebCore/page/EventHandler.cpp (282768 => 282769)


--- trunk/Source/WebCore/page/EventHandler.cpp	2021-09-20 19:23:54 UTC (rev 282768)
+++ trunk/Source/WebCore/page/EventHandler.cpp	2021-09-20 19:32:34 UTC (rev 282769)
@@ -143,6 +143,10 @@
 #include "PointerLockController.h"
 #endif
 
+#if USE(APPLE_INTERNAL_SDK)
+#include <WebKitAdditions/EventHandlerAdditions.cpp>
+#endif
+
 namespace WebCore {
 
 using namespace HTMLNames;
@@ -2516,18 +2520,28 @@
 
 RefPtr<Element> EventHandler::textRecognitionCandidateElement() const
 {
-    RefPtr shadowHost = m_elementUnderMouse ? m_elementUnderMouse->shadowHost() : nullptr;
-    if (!shadowHost)
+    RefPtr candidateElement = m_elementUnderMouse;
+    if (candidateElement) {
+        if (auto shadowHost = candidateElement->shadowHost())
+            candidateElement = shadowHost;
+    }
+
+    if (!candidateElement)
         return nullptr;
 
-    auto renderer = shadowHost->renderer();
+    auto renderer = candidateElement->renderer();
     if (!is<RenderImage>(renderer))
         return nullptr;
 
-    if (is<HTMLVideoElement>(*shadowHost))
+#if USE(APPLE_INTERNAL_SDK)
+    if (isAdditionalTextRecognitionCandidateElement(*candidateElement))
+        return candidateElement;
+#endif
+
+    if (is<HTMLVideoElement>(*candidateElement))
         return nullptr;
 
-    return shadowHost;
+    return candidateElement;
 }
 
 void EventHandler::updateMouseEventTargetNode(const AtomString& eventType, Node* targetNode, const PlatformMouseEvent& platformMouseEvent, FireMouseOverOut fireMouseOverOut)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to