Add a key to the text manipulation userInfo dictionary indicating whether the translated item is on-screen
https://bugs.webkit.org/show_bug.cgi?id=216452
<rdar://problem/68785397>
Reviewed by Darin Adler.
Source/WebCore:
For debugging purposes, and also to provide a hint as to what text should be prioritized when translating
web pages, WebKit clients have requested a new field in the userInfo metadata dictionary associated with each
token that indicates whether or not a translation (i.e. text manipulation) token represents an element that is
currently on-screen. See below for more details.
Test: TextManipulation.StartTextManipulationExtractsUserInfo
* editing/TextManipulationController.cpp:
(WebCore::tokenInfo):
Set the flag by checking whether or not the absolute bounding rect intersects with the visible content rect of
the enclosing frame. Note that since subframe content is currently never extracted for translation, we don't
need logic yet to recursively check that parent iframe elements are visible.
* editing/TextManipulationController.h:
Add a new `bool` flag in `ManipulationTokenInfo`.
(WebCore::TextManipulationController::ManipulationTokenInfo::encode const):
(WebCore::TextManipulationController::ManipulationTokenInfo::decode):
Source/WebKit:
Add `_WKTextManipulationTokenUserInfoVisibilityKey` and set its value to the value of the `isVisible` member in
`ManipulationTokenInfo`. See WebCore ChangeLog for more details.
* UIProcess/API/Cocoa/WKWebView.mm:
(createUserInfo):
* UIProcess/API/Cocoa/_WKTextManipulationToken.h:
* UIProcess/API/Cocoa/_WKTextManipulationToken.mm:
Tools:
Adjust an existing test so that it adds a fourth text paragraph with 2000px of top margin, and also
programmatically scrolls after loading the page so that only this last paragraph is visible. We expect the
metadata to indicate that none of the other tokens except this last one has a value of `YES` for
`_WKTextManipulationTokenUserInfoVisibilityKey`.
* TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm:
(TestWebKitAPI::TEST):
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@266993 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/editing/TextManipulationController.cpp b/Source/WebCore/editing/TextManipulationController.cpp
index ad43870..9baf230 100644
--- a/Source/WebCore/editing/TextManipulationController.cpp
+++ b/Source/WebCore/editing/TextManipulationController.cpp
@@ -297,6 +297,12 @@
result.tagName = element->tagName();
if (element->hasAttributeWithoutSynchronization(HTMLNames::roleAttr))
result.roleAttribute = element->attributeWithoutSynchronization(HTMLNames::roleAttr);
+ if (auto frame = makeRefPtr(node->document().frame()); frame && frame->view() && element->renderer()) {
+ // FIXME: This doesn't account for overflow clip.
+ auto elementRect = element->renderer()->absoluteAnchorRect();
+ auto visibleContentRect = frame->view()->visibleContentRect();
+ result.isVisible = visibleContentRect.intersects(enclosingIntRect(elementRect));
+ }
}
return result;
}