[webkit-changes] [WebKit/WebKit] b29e92: Implement an initial heuristic to find targeted el...

Wenson Hsieh noreply at github.com
Thu Mar 21 17:08:13 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: b29e92ba5f100cd7808852be5e206b2df4bba6ea
      https://github.com/WebKit/WebKit/commit/b29e92ba5f100cd7808852be5e206b2df4bba6ea
  Author: Wenson Hsieh <wenson_hsieh at apple.com>
  Date:   2024-03-21 (Thu, 21 Mar 2024)

  Changed paths:
    M Source/WebCore/page/ElementTargeting.cpp
    M Source/WebCore/page/text-extraction/TextExtraction.cpp
    M Source/WebCore/page/text-extraction/TextExtraction.h
    M Tools/TestWebKitAPI/SourcesCocoa.txt
    M Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
    A Tools/TestWebKitAPI/Tests/WebKitCocoa/ElementTargetingTests.mm
    A Tools/TestWebKitAPI/Tests/WebKitCocoa/element-targeting-1.html

  Log Message:
  -----------
  Implement an initial heuristic to find targeted elements for remote inspection
https://bugs.webkit.org/show_bug.cgi?id=271344

Reviewed by Abrar Rahman Protyasha.

Implement an initial, basic version of an element targeting heuristic in `ElementTargeting.cpp`.
This heuristic performs a piercing hit-test for main frame content at the given location, and
returns an array of `_WKTargetedElementInfo` results, with the frontmost hit-tested element first.

Test: ElementTargeting.BasicElementTargeting

* Source/WebCore/page/ElementTargeting.cpp:
(WebCore::elementAndAncestorsAreOnlyChildren):
(WebCore::querySelectorMatchesOneElement):
(WebCore::childIndexByType):
(WebCore::computeIDSelector):
(WebCore::computeClassSelector):
(WebCore::selectorForElementRecursive):
(WebCore::parentRelativeSelectorRecursive):
(WebCore::selectorsForTarget):

Implement an algorithm to find one or more suitable CSS selectors that uniquely match the given
target element. This works by checking whether:

1.  The ID (if present) is unique in the document.
2.  The first several classes (if present) can be combined to form a selector that uniquely matches
    the target.
3.  The tag name is unique in the document.
4.  If none of the above are true, but we've found a unique selector for the parent element
    (recursively using this definition), then use an `nth-child` selector (or `last`/`first-child`,
    in the case where the element is the last or first of its type) to establish a unique selector
    for this element, relative to the parent.

(WebCore::computeOffsetEdges):
(WebCore::targetedElementInfo):
(WebCore::findTargetedElements):

Perform the piercing hit-test here, discarding some elements that are too large or small.

* Source/WebCore/page/text-extraction/TextExtraction.cpp:
(WebCore::TextExtraction::extractRenderedText):

Refactor this helper method, so that we're able to perform rendered text extraction for a specific
DOM element. Use this in the `ElementTargeting` heuristic, to compute the `-renderedText` property
on the targeted element info.

* Source/WebCore/page/text-extraction/TextExtraction.h:
* Tools/TestWebKitAPI/SourcesCocoa.txt:
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/ElementTargetingTests.mm: Added.
(-[WKWebView targetedElementInfoAt:]):
(TestWebKitAPI::TEST):
* Tools/TestWebKitAPI/Tests/WebKitCocoa/element-targeting-1.html: Added.

Canonical link: https://commits.webkit.org/276508@main



To unsubscribe from these emails, change your notification settings at https://github.com/WebKit/WebKit/settings/notifications


More information about the webkit-changes mailing list