Changeset 266993 in webkit


Ignore:
Timestamp:
Sep 13, 2020 8:43:58 AM (4 years ago)
Author:
Wenson Hsieh
Message:

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):

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r266992 r266993  
     12020-09-13  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        Add a key to the text manipulation userInfo dictionary indicating whether the translated item is on-screen
     4        https://bugs.webkit.org/show_bug.cgi?id=216452
     5        <rdar://problem/68785397>
     6
     7        Reviewed by Darin Adler.
     8
     9        For debugging purposes, and also to provide a hint as to what text should be prioritized when translating
     10        web pages, WebKit clients have requested a new field in the userInfo metadata dictionary associated with each
     11        token that indicates whether or not a translation (i.e. text manipulation) token represents an element that is
     12        currently on-screen. See below for more details.
     13
     14        Test: TextManipulation.StartTextManipulationExtractsUserInfo
     15
     16        * editing/TextManipulationController.cpp:
     17        (WebCore::tokenInfo):
     18
     19        Set the flag by checking whether or not the absolute bounding rect intersects with the visible content rect of
     20        the enclosing frame. Note that since subframe content is currently never extracted for translation, we don't
     21        need logic yet to recursively check that parent iframe elements are visible.
     22
     23        * editing/TextManipulationController.h:
     24
     25        Add a new `bool` flag in `ManipulationTokenInfo`.
     26
     27        (WebCore::TextManipulationController::ManipulationTokenInfo::encode const):
     28        (WebCore::TextManipulationController::ManipulationTokenInfo::decode):
     29
    1302020-09-13  Philippe Normand  <pnormand@igalia.com>
    231
  • trunk/Source/WebCore/editing/TextManipulationController.cpp

    r266075 r266993  
    298298        if (element->hasAttributeWithoutSynchronization(HTMLNames::roleAttr))
    299299            result.roleAttribute = element->attributeWithoutSynchronization(HTMLNames::roleAttr);
     300        if (auto frame = makeRefPtr(node->document().frame()); frame && frame->view() && element->renderer()) {
     301            // FIXME: This doesn't account for overflow clip.
     302            auto elementRect = element->renderer()->absoluteAnchorRect();
     303            auto visibleContentRect = frame->view()->visibleContentRect();
     304            result.isVisible = visibleContentRect.intersects(enclosingIntRect(elementRect));
     305        }
    300306    }
    301307    return result;
  • trunk/Source/WebCore/editing/TextManipulationController.h

    r265286 r266993  
    5353        String roleAttribute;
    5454        URL documentURL;
     55        bool isVisible { false };
    5556
    5657        template<class Encoder> void encode(Encoder&) const;
     
    203204    encoder << roleAttribute;
    204205    encoder << documentURL;
     206    encoder << isVisible;
    205207}
    206208
     
    216218
    217219    if (!decoder.decode(result.documentURL))
     220        return WTF::nullopt;
     221
     222    if (!decoder.decode(result.isVisible))
    218223        return WTF::nullopt;
    219224
  • trunk/Source/WebKit/ChangeLog

    r266990 r266993  
     12020-09-13  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        Add a key to the text manipulation userInfo dictionary indicating whether the translated item is on-screen
     4        https://bugs.webkit.org/show_bug.cgi?id=216452
     5        <rdar://problem/68785397>
     6
     7        Reviewed by Darin Adler.
     8
     9        Add `_WKTextManipulationTokenUserInfoVisibilityKey` and set its value to the value of the `isVisible` member in
     10        `ManipulationTokenInfo`. See WebCore ChangeLog for more details.
     11
     12        * UIProcess/API/Cocoa/WKWebView.mm:
     13        (createUserInfo):
     14        * UIProcess/API/Cocoa/_WKTextManipulationToken.h:
     15        * UIProcess/API/Cocoa/_WKTextManipulationToken.mm:
     16
    1172020-09-13  Pablo Saavedra  <psaavedra@igalia.com>
    218
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm

    r266890 r266993  
    16231623    if (!info->roleAttribute.isNull())
    16241624        [result setObject:(NSString *)info->roleAttribute forKey:_WKTextManipulationTokenUserInfoRoleAttributeKey];
     1625    [result setObject:@(info->isVisible) forKey:_WKTextManipulationTokenUserInfoVisibilityKey];
    16251626
    16261627    return result;
  • trunk/Source/WebKit/UIProcess/API/Cocoa/_WKTextManipulationToken.h

    r260865 r266993  
    3232WK_EXTERN NSString * const _WKTextManipulationTokenUserInfoTagNameKey WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
    3333WK_EXTERN NSString * const _WKTextManipulationTokenUserInfoRoleAttributeKey WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
     34WK_EXTERN NSString * const _WKTextManipulationTokenUserInfoVisibilityKey WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
    3435
    3536WK_CLASS_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA))
  • trunk/Source/WebKit/UIProcess/API/Cocoa/_WKTextManipulationToken.mm

    r260865 r266993  
    3232NSString * const _WKTextManipulationTokenUserInfoTagNameKey = @"_WKTextManipulationTokenUserInfoTagNameKey";
    3333NSString * const _WKTextManipulationTokenUserInfoRoleAttributeKey = @"_WKTextManipulationTokenUserInfoRoleAttributeKey";
     34NSString * const _WKTextManipulationTokenUserInfoVisibilityKey = @"_WKTextManipulationTokenUserInfoVisibilityKey";
    3435
    3536@implementation _WKTextManipulationToken {
  • trunk/Tools/ChangeLog

    r266988 r266993  
     12020-09-13  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        Add a key to the text manipulation userInfo dictionary indicating whether the translated item is on-screen
     4        https://bugs.webkit.org/show_bug.cgi?id=216452
     5        <rdar://problem/68785397>
     6
     7        Reviewed by Darin Adler.
     8
     9        Adjust an existing test so that it adds a fourth text paragraph with 2000px of top margin, and also
     10        programmatically scrolls after loading the page so that only this last paragraph is visible. We expect the
     11        metadata to indicate that none of the other tokens except this last one has a value of `YES` for
     12        `_WKTextManipulationTokenUserInfoVisibilityKey`.
     13
     14        * TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm:
     15        (TestWebKitAPI::TEST):
     16
    1172020-09-12  Darin Adler  <darin@apple.com>
    218
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/TextManipulation.mm

    r266075 r266993  
    802802        "    <div role='button'>Second</div>"
    803803        "    <span>Third</span>"
     804        "    <div style='margin-top: 2000px;'>Fourth</div>"
     805        "    <script>scrollTo(0, 2000);</script>"
    804806        "</body>"];
    805807
     808    [webView waitForNextPresentationUpdate];
     809
    806810    done = false;
    807811    [webView _startTextManipulationsWithConfiguration:nil completion:^{
     
    811815
    812816    auto items = [delegate items];
    813     EXPECT_EQ(items.count, 4UL);
     817    EXPECT_EQ(items.count, 5UL);
    814818    EXPECT_EQ(items[0].tokens.count, 1UL);
    815819    EXPECT_EQ(items[1].tokens.count, 1UL);
    816820    EXPECT_EQ(items[2].tokens.count, 1UL);
    817821    EXPECT_EQ(items[3].tokens.count, 1UL);
     822    EXPECT_EQ(items[4].tokens.count, 1UL);
    818823    EXPECT_WK_STREQ("This is a test", items[0].tokens[0].content);
    819824    EXPECT_WK_STREQ("First", items[1].tokens[0].content);
    820825    EXPECT_WK_STREQ("Second", items[2].tokens[0].content);
    821826    EXPECT_WK_STREQ("Third", items[3].tokens[0].content);
     827    EXPECT_WK_STREQ("Fourth", items[4].tokens[0].content);
    822828    {
    823829        auto userInfo = items[0].tokens[0].userInfo;
    824830        EXPECT_WK_STREQ("TestWebKitAPI.resources", [(NSURL *)userInfo[_WKTextManipulationTokenUserInfoDocumentURLKey] lastPathComponent]);
    825831        EXPECT_WK_STREQ("TITLE", (NSString *)userInfo[_WKTextManipulationTokenUserInfoTagNameKey]);
     832        EXPECT_FALSE([userInfo[_WKTextManipulationTokenUserInfoVisibilityKey] boolValue]);
    826833    }
    827834    {
     
    829836        EXPECT_WK_STREQ("TestWebKitAPI.resources", [(NSURL *)userInfo[_WKTextManipulationTokenUserInfoDocumentURLKey] lastPathComponent]);
    830837        EXPECT_WK_STREQ("P", (NSString *)userInfo[_WKTextManipulationTokenUserInfoTagNameKey]);
     838        EXPECT_FALSE([userInfo[_WKTextManipulationTokenUserInfoVisibilityKey] boolValue]);
    831839    }
    832840    {
     
    835843        EXPECT_WK_STREQ("DIV", (NSString *)userInfo[_WKTextManipulationTokenUserInfoTagNameKey]);
    836844        EXPECT_WK_STREQ("button", (NSString *)userInfo[_WKTextManipulationTokenUserInfoRoleAttributeKey]);
     845        EXPECT_FALSE([userInfo[_WKTextManipulationTokenUserInfoVisibilityKey] boolValue]);
    837846    }
    838847    {
     
    840849        EXPECT_WK_STREQ("TestWebKitAPI.resources", [(NSURL *)userInfo[_WKTextManipulationTokenUserInfoDocumentURLKey] lastPathComponent]);
    841850        EXPECT_WK_STREQ("SPAN", (NSString *)userInfo[_WKTextManipulationTokenUserInfoTagNameKey]);
     851        EXPECT_FALSE([userInfo[_WKTextManipulationTokenUserInfoVisibilityKey] boolValue]);
     852    }
     853    {
     854        auto userInfo = items[4].tokens[0].userInfo;
     855        EXPECT_WK_STREQ("TestWebKitAPI.resources", [(NSURL *)userInfo[_WKTextManipulationTokenUserInfoDocumentURLKey] lastPathComponent]);
     856        EXPECT_WK_STREQ("DIV", (NSString *)userInfo[_WKTextManipulationTokenUserInfoTagNameKey]);
     857        EXPECT_TRUE([userInfo[_WKTextManipulationTokenUserInfoVisibilityKey] boolValue]);
    842858    }
    843859}
Note: See TracChangeset for help on using the changeset viewer.