Branch: refs/heads/main Home: https://github.com/WebKit/WebKit Commit: 87b57ed36164d494c13f5dfec8e6701115a8b8dd https://github.com/WebKit/WebKit/commit/87b57ed36164d494c13f5dfec8e6701115a8b8dd Author: Wenson Hsieh <wenson_hs...@apple.com> Date: 2024-04-09 (Tue, 09 Apr 2024)
Changed paths: A LayoutTests/fast/element-targeting/prefer-succinct-selectors-expected.txt A LayoutTests/fast/element-targeting/prefer-succinct-selectors.html M Source/WebCore/page/ElementTargetingController.cpp Log Message: ----------- [Remote Inspection] Improve the heuristic for extracting CSS selectors for targeted elements https://bugs.webkit.org/show_bug.cgi?id=272413 rdar://123615458 Reviewed by Megan Gardner. Refactor some of the logic around finding CSS selectors for targeted elements, such that we: 1. Prefers shorter selector strings over longer ones. 2. Prefers selectors that reference the target's class, ID or tag name over selectors that are relative to other elements. 3. Allow for sibling-relative selectors as well as parent-relative ones. This also sorts the returned selector strings, in order from most preferred to least preferred selector. To avoid exponential runtime in this new algorithm (since we're now collecting both sibling-relative and parent-relative selectors), we also plumb a `HashMap` as we're recursively walking up the tree for each element, which we use to memoize `selectorForElementRecursive`. This ensures `O(n^2)` performance in the worst case, where `n` is the number of nodes in the DOM (since each visit could potentially trigger a `querySelector(…)` with class or tag names), but in practice, means we'll typically get away with `O(n)`, since we'll stop after hitting the first ancestor or sibling element with unique classes or IDs per target, and are typically a small number of targets. * LayoutTests/fast/element-targeting/prefer-succinct-selectors-expected.txt: Added. * LayoutTests/fast/element-targeting/prefer-succinct-selectors.html: Added. Add a new layout test to exercise the "prefers conciseness" part of selector finding. * Source/WebCore/page/ElementTargetingController.cpp: (WebCore::computeIDSelector): (WebCore::computeClassSelector): (WebCore::shortestSelector): (WebCore::selectorForElementRecursive): (WebCore::siblingRelativeSelectorRecursive): (WebCore::parentRelativeSelectorRecursive): See above for more details. (WebCore::selectorsForTarget): (WebCore::targetedElementInfo): (WebCore::ElementTargetingController::findTargets): Canonical link: https://commits.webkit.org/277270@main To unsubscribe from these emails, change your notification settings at https://github.com/WebKit/WebKit/settings/notifications _______________________________________________ webkit-changes mailing list webkit-changes@lists.webkit.org https://lists.webkit.org/mailman/listinfo/webkit-changes