Title: [287245] trunk/Source/WebCore
- Revision
- 287245
- Author
- [email protected]
- Date
- 2021-12-19 12:24:24 -0800 (Sun, 19 Dec 2021)
Log Message
Add a basic heuristic for collecting and extracting text from controls in ModalContainerObserver
https://bugs.webkit.org/show_bug.cgi?id=234299
Reviewed by Dean Jackson.
Introduce a heuristic for extracting clickable controls from modal containers, that are unlikely to be links
(i.e. trigger navigation when clicked). See below for more details.
* page/ModalContainerObserver.cpp:
(WebCore::ModalContainerObserver::ModalContainerObserver):
(WebCore::ModalContainerObserver::updateModalContainerIfNeeded):
Once a modal container has been detected, schedule a short timer to traverse the modal container's subtree in
search for clickable controls. In a future patch, we may need to lengthen this delay or even introduce a
mechanism for restarting the timer periodically if no clickable controls are discovered in the modal container.
(WebCore::accessibilityRole):
(WebCore::isClickableControl):
(WebCore::removeParentOrChildElements):
(WebCore::removeElementsWithEmptyBounds):
(WebCore::textForControl):
(WebCore::ModalContainerObserver::scheduleClickableElementCollection):
(WebCore::ModalContainerObserver::collectClickableElementsTimerFired):
(WebCore::ModalContainerObserver::collectClickableElements):
This contains the main logic for collecting clickable elements inside modal containers. This heuristic consists
mostly of three phases:
1. We initially scan the modal container subtree in search of any elements that seem like they could be
clickable buttons or links that don't trigger navigation, and build a list of `classifiableControls`.
2. We then filter the list of `classifiableControls` by removing any elements in the list that either contain or
are contained by other elements (using a heuristic to decide whether to remove each parent or child); in this
filtering step, we additionally remove all elements that are effectively unclickable due to having an empty
client bounding rect.
3. In the final phase, we iterate over all of the controls in the filtered list, and attempt to extract text for
each control (looking at attributes such as `title` and `aria-label`, `alt` text for images, and finally falling
back to `outerText()`). If this raw text description is not empty and also not too long (exceeding an
arbitrarily chosen threshold of 100 characters), we add it to the final list of controls and control text
descriptions to send to the client layer for classification.
(WebCore::ModalContainerObserver::shouldHide):
Augment this to `return false` in the case where we're inside the scope of `m_collectingClickableElements`. This
allows us to run heuristics over the modal container without an adjusted `display: none;` style on the container
element.
* page/ModalContainerObserver.h:
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (287244 => 287245)
--- trunk/Source/WebCore/ChangeLog 2021-12-19 19:44:48 UTC (rev 287244)
+++ trunk/Source/WebCore/ChangeLog 2021-12-19 20:24:24 UTC (rev 287245)
@@ -1,3 +1,55 @@
+2021-12-19 Wenson Hsieh <[email protected]>
+
+ Add a basic heuristic for collecting and extracting text from controls in ModalContainerObserver
+ https://bugs.webkit.org/show_bug.cgi?id=234299
+
+ Reviewed by Dean Jackson.
+
+ Introduce a heuristic for extracting clickable controls from modal containers, that are unlikely to be links
+ (i.e. trigger navigation when clicked). See below for more details.
+
+ * page/ModalContainerObserver.cpp:
+ (WebCore::ModalContainerObserver::ModalContainerObserver):
+ (WebCore::ModalContainerObserver::updateModalContainerIfNeeded):
+
+ Once a modal container has been detected, schedule a short timer to traverse the modal container's subtree in
+ search for clickable controls. In a future patch, we may need to lengthen this delay or even introduce a
+ mechanism for restarting the timer periodically if no clickable controls are discovered in the modal container.
+
+ (WebCore::accessibilityRole):
+ (WebCore::isClickableControl):
+ (WebCore::removeParentOrChildElements):
+ (WebCore::removeElementsWithEmptyBounds):
+ (WebCore::textForControl):
+ (WebCore::ModalContainerObserver::scheduleClickableElementCollection):
+ (WebCore::ModalContainerObserver::collectClickableElementsTimerFired):
+ (WebCore::ModalContainerObserver::collectClickableElements):
+
+ This contains the main logic for collecting clickable elements inside modal containers. This heuristic consists
+ mostly of three phases:
+
+ 1. We initially scan the modal container subtree in search of any elements that seem like they could be
+ clickable buttons or links that don't trigger navigation, and build a list of `classifiableControls`.
+
+ 2. We then filter the list of `classifiableControls` by removing any elements in the list that either contain or
+ are contained by other elements (using a heuristic to decide whether to remove each parent or child); in this
+ filtering step, we additionally remove all elements that are effectively unclickable due to having an empty
+ client bounding rect.
+
+ 3. In the final phase, we iterate over all of the controls in the filtered list, and attempt to extract text for
+ each control (looking at attributes such as `title` and `aria-label`, `alt` text for images, and finally falling
+ back to `outerText()`). If this raw text description is not empty and also not too long (exceeding an
+ arbitrarily chosen threshold of 100 characters), we add it to the final list of controls and control text
+ descriptions to send to the client layer for classification.
+
+ (WebCore::ModalContainerObserver::shouldHide):
+
+ Augment this to `return false` in the case where we're inside the scope of `m_collectingClickableElements`. This
+ allows us to run heuristics over the modal container without an adjusted `display: none;` style on the container
+ element.
+
+ * page/ModalContainerObserver.h:
+
2021-12-19 Alan Bujtas <[email protected]>
[LFC][IFC] text-indent is treated as a margin applied to the start edge of the line box.
Modified: trunk/Source/WebCore/page/ModalContainerObserver.cpp (287244 => 287245)
--- trunk/Source/WebCore/page/ModalContainerObserver.cpp 2021-12-19 19:44:48 UTC (rev 287244)
+++ trunk/Source/WebCore/page/ModalContainerObserver.cpp 2021-12-19 20:24:24 UTC (rev 287245)
@@ -26,24 +26,34 @@
#include "config.h"
#include "ModalContainerObserver.h"
+#include "AccessibilityObject.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "DocumentLoader.h"
+#include "ElementIterator.h"
+#include "EventLoop.h"
+#include "EventNames.h"
#include "Frame.h"
#include "FrameView.h"
+#include "HTMLAnchorElement.h"
#include "HTMLBodyElement.h"
#include "HTMLDocument.h"
#include "HTMLElement.h"
+#include "HTMLImageElement.h"
+#include "HTMLInputElement.h"
#include "Page.h"
#include "RenderDescendantIterator.h"
#include "RenderText.h"
#include "RenderView.h"
#include "Text.h"
+#include <wtf/Scope.h>
#include <wtf/URL.h>
namespace WebCore {
+static constexpr size_t maxLengthForClickableElementText = 100;
+
bool ModalContainerObserver::isNeededFor(const Document& document)
{
RefPtr documentLoader = document.loader();
@@ -72,7 +82,11 @@
return true;
}
-ModalContainerObserver::ModalContainerObserver() = default;
+ModalContainerObserver::ModalContainerObserver()
+ : m_collectClickableElementsTimer(*this, &ModalContainerObserver::collectClickableElementsTimerFired)
+{
+}
+
ModalContainerObserver::~ModalContainerObserver() = default;
static bool matchesSearchTerm(const Text& textNode, const AtomString& searchTerm)
@@ -122,6 +136,7 @@
if (RefPtr textNode = textRenderer.textNode(); textNode && matchesSearchTerm(*textNode, searchTerm)) {
m_container = element.get();
element->invalidateStyle();
+ scheduleClickableElementCollection();
return;
}
}
@@ -128,9 +143,175 @@
}
}
+static AccessibilityRole accessibilityRole(const HTMLElement& element)
+{
+ return AccessibilityObject::ariaRoleToWebCoreRole(element.attributeWithoutSynchronization(HTMLNames::roleAttr));
+}
+
+static bool isClickableControl(const HTMLElement& element)
+{
+ if (element.isActuallyDisabled())
+ return false;
+
+ if (!element.renderer())
+ return false;
+
+ if (element.hasTagName(HTMLNames::buttonTag))
+ return true;
+
+ if (is<HTMLInputElement>(element) && downcast<HTMLInputElement>(element).isTextButton())
+ return true;
+
+ switch (accessibilityRole(element)) {
+ case AccessibilityRole::Button:
+ return true;
+ case AccessibilityRole::CheckBox:
+ case AccessibilityRole::Switch:
+ return false;
+ default:
+ break;
+ }
+
+ if (is<HTMLAnchorElement>(element)) {
+ // FIXME: We might need a more comprehensive policy here that attempts to click the link to detect navigation,
+ // but then immediately cancels the navigation.
+ auto href = ""
+ return equalIgnoringFragmentIdentifier(element.document().url(), href) || !href.protocolIsInHTTPFamily();
+ }
+
+ return element.hasEventListeners(eventNames().clickEvent) || element.hasEventListeners(eventNames().mousedownEvent) || element.hasEventListeners(eventNames().mouseupEvent)
+ || element.hasEventListeners(eventNames().touchstartEvent) || element.hasEventListeners(eventNames().touchendEvent)
+ || element.hasEventListeners(eventNames().pointerdownEvent) || element.hasEventListeners(eventNames().pointerupEvent);
+}
+
+static void removeParentOrChildElements(Vector<Ref<HTMLElement>>& elements)
+{
+ HashSet<Ref<HTMLElement>> elementsToRemove;
+ for (auto& outer : elements) {
+ if (elementsToRemove.contains(outer))
+ continue;
+
+ for (auto& inner : elements) {
+ if (elementsToRemove.contains(inner) || outer.ptr() == inner.ptr() || !outer->contains(inner))
+ continue;
+
+ if (accessibilityRole(outer) == AccessibilityRole::Button) {
+ elementsToRemove.add(inner);
+ continue;
+ }
+
+ if (accessibilityRole(inner) == AccessibilityRole::Button) {
+ elementsToRemove.add(outer);
+ continue;
+ }
+
+ if (outer->hasTagName(HTMLNames::divTag) || outer->hasTagName(HTMLNames::spanTag) || outer->hasTagName(HTMLNames::pTag) || outer->hasTagName(HTMLNames::sectionTag))
+ elementsToRemove.add(outer);
+ else
+ elementsToRemove.add(inner);
+ }
+ }
+
+ elements.removeAllMatching([&] (auto& control) {
+ return elementsToRemove.contains(control);
+ });
+}
+
+static void removeElementsWithEmptyBounds(Vector<Ref<HTMLElement>>& elements)
+{
+ elements.removeAllMatching([&] (auto& element) {
+ return element->boundingClientRect().isEmpty();
+ });
+}
+
+static String textForControl(HTMLElement& control)
+{
+ auto ariaLabel = control.attributeWithoutSynchronization(HTMLNames::aria_labelAttr);
+ if (!ariaLabel.isEmpty())
+ return ariaLabel;
+
+ if (is<HTMLInputElement>(control))
+ return downcast<HTMLInputElement>(control).value();
+
+ auto title = control.title();
+ if (!title.isEmpty())
+ return title;
+
+ if (is<HTMLImageElement>(control)) {
+ auto altText = downcast<HTMLImageElement>(control).altText();
+ if (!altText.isEmpty())
+ return altText;
+ }
+
+ return control.outerText();
+}
+
+void ModalContainerObserver::scheduleClickableElementCollection()
+{
+ m_collectClickableElementsTimer.startOneShot(200_ms);
+}
+
+void ModalContainerObserver::collectClickableElementsTimerFired()
+{
+ if (!m_container)
+ return;
+
+ m_container->document().eventLoop().queueTask(TaskSource::InternalAsyncTask, [observer = this, weakDocument = WeakPtr { m_container->document() }] {
+ RefPtr document = weakDocument.get();
+ if (!document)
+ return;
+
+ if (observer != document->modalContainerObserverIfExists() || !observer->m_container.get()) {
+ ASSERT_NOT_REACHED();
+ return;
+ }
+
+ auto [classifiableControls, controlTextsToClassify] = observer->collectClickableElements();
+ if (classifiableControls.isEmpty())
+ return;
+
+ // FIXME(234323): Classify strings by propagating them through the client layer.
+ });
+}
+
+std::pair<Vector<WeakPtr<HTMLElement>>, Vector<String>> ModalContainerObserver::collectClickableElements()
+{
+ Ref container = *m_container;
+ m_collectingClickableElements = true;
+ auto exitCollectClickableElementsScope = makeScopeExit([&] {
+ m_collectingClickableElements = false;
+ container->invalidateStyle();
+ });
+
+ container->invalidateStyle();
+ container->document().updateLayoutIgnorePendingStylesheets();
+
+ Vector<Ref<HTMLElement>> clickableControls;
+ for (auto& child : descendantsOfType<HTMLElement>(container)) {
+ if (isClickableControl(child))
+ clickableControls.append(child);
+ }
+
+ removeElementsWithEmptyBounds(clickableControls);
+ removeParentOrChildElements(clickableControls);
+
+ Vector<WeakPtr<HTMLElement>> classifiableControls;
+ Vector<String> controlTextsToClassify;
+ classifiableControls.reserveInitialCapacity(clickableControls.size());
+ controlTextsToClassify.reserveInitialCapacity(clickableControls.size());
+ for (auto& control : clickableControls) {
+ auto text = textForControl(control).stripWhiteSpace();
+ if (!text.isEmpty() && text.length() < maxLengthForClickableElementText) {
+ classifiableControls.uncheckedAppend({ control });
+ controlTextsToClassify.uncheckedAppend(WTFMove(text));
+ }
+ }
+ return { WTFMove(classifiableControls), WTFMove(controlTextsToClassify) };
+}
+
bool ModalContainerObserver::shouldHide(const Element& element)
{
- return &element == m_container;
+ return &element == m_container && !m_collectingClickableElements;
}
} // namespace WebCore
Modified: trunk/Source/WebCore/page/ModalContainerObserver.h (287244 => 287245)
--- trunk/Source/WebCore/page/ModalContainerObserver.h 2021-12-19 19:44:48 UTC (rev 287244)
+++ trunk/Source/WebCore/page/ModalContainerObserver.h 2021-12-19 20:24:24 UTC (rev 287245)
@@ -25,9 +25,12 @@
#pragma once
+#include "Timer.h"
#include <wtf/FastMalloc.h>
+#include <wtf/Vector.h>
#include <wtf/WeakHashSet.h>
#include <wtf/WeakPtr.h>
+#include <wtf/text/WTFString.h>
namespace WebCore {
@@ -34,6 +37,7 @@
class Document;
class Element;
class FrameView;
+class HTMLElement;
class ModalContainerObserver {
WTF_MAKE_FAST_ALLOCATED;
@@ -47,8 +51,15 @@
void updateModalContainerIfNeeded(const FrameView&);
private:
+ void scheduleClickableElementCollection();
+ void collectClickableElementsTimerFired();
+
+ std::pair<Vector<WeakPtr<HTMLElement>>, Vector<String>> collectClickableElements();
+
WeakHashSet<Element> m_elementsToIgnoreWhenSearching;
WeakPtr<Element> m_container;
+ Timer m_collectClickableElementsTimer;
+ bool m_collectingClickableElements { false };
};
} // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes