Changeset 243012 in webkit


Ignore:
Timestamp:
Mar 15, 2019 2:36:56 PM (5 years ago)
Author:
timothy@apple.com
Message:

Add support to WebPage for getting the contents as an attributed string.
https://bugs.webkit.org/show_bug.cgi?id=195636
rdar://problem/45055697

Reviewed by Tim Horton.

Source/WebKit:

  • Shared/mac/AttributedString.h:

(WebKit::AttributedString::AttributedString):

  • Shared/mac/AttributedString.mm:

(IPC::ArgumentCoder<WebKit::AttributedString>::encode):
(IPC::ArgumentCoder<WebKit::AttributedString>::decode):

  • UIProcess/API/Cocoa/WKWebView.mm:

(-[WKWebView _getContentsAsAttributedStringWithCompletionHandler:]):

  • UIProcess/API/Cocoa/WKWebViewPrivate.h:
  • UIProcess/WebPageProxy.cpp:

(WebKit::WebPageProxy::getContentsAsAttributedString):

  • UIProcess/WebPageProxy.h:
  • WebProcess/WebPage/Cocoa/WebPageCocoa.mm:

(WebKit::WebPage::getContentsAsAttributedString):

  • WebProcess/WebPage/WebPage.h:
  • WebProcess/WebPage/WebPage.messages.in:

Tools:

  • TestWebKitAPI/Tests/WebKitCocoa/WKWebViewGetContents.mm:

(TEST(WKWebView, GetContentsShouldReturnAttributedString): Added.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r243010 r243012  
     12019-03-15  Timothy Hatcher  <timothy@apple.com>
     2
     3        Add support to WebPage for getting the contents as an attributed string.
     4        https://bugs.webkit.org/show_bug.cgi?id=195636
     5        rdar://problem/45055697
     6
     7        Reviewed by Tim Horton.
     8
     9        * Shared/mac/AttributedString.h:
     10        (WebKit::AttributedString::AttributedString):
     11        * Shared/mac/AttributedString.mm:
     12        (IPC::ArgumentCoder<WebKit::AttributedString>::encode):
     13        (IPC::ArgumentCoder<WebKit::AttributedString>::decode):
     14        * UIProcess/API/Cocoa/WKWebView.mm:
     15        (-[WKWebView _getContentsAsAttributedStringWithCompletionHandler:]):
     16        * UIProcess/API/Cocoa/WKWebViewPrivate.h:
     17        * UIProcess/WebPageProxy.cpp:
     18        (WebKit::WebPageProxy::getContentsAsAttributedString):
     19        * UIProcess/WebPageProxy.h:
     20        * WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
     21        (WebKit::WebPage::getContentsAsAttributedString):
     22        * WebProcess/WebPage/WebPage.h:
     23        * WebProcess/WebPage/WebPage.messages.in:
     24
    1252019-03-15  Per Arne Vollan  <pvollan@apple.com>
    226
  • trunk/Source/WebKit/Shared/mac/AttributedString.h

    r242285 r243012  
    3939
    4040#if defined(__OBJC__)
    41     AttributedString(NSAttributedString *attributedString)
     41    AttributedString(NSAttributedString *attributedString, NSDictionary *documentAttributes = nil)
    4242        : string(attributedString)
     43        , documentAttributes(documentAttributes)
    4344    {
    4445    }
     
    4950    }
    5051#endif
    51    
     52
    5253    RetainPtr<NSAttributedString> string;
     54    RetainPtr<NSDictionary> documentAttributes;
    5355};
    5456
  • trunk/Source/WebKit/Shared/mac/AttributedString.mm

    r242908 r243012  
    3535void ArgumentCoder<WebKit::AttributedString>::encode(Encoder& encoder, const WebKit::AttributedString& attributedString)
    3636{
    37     encoder << attributedString.string;
     37    encoder << attributedString.string << attributedString.documentAttributes;
    3838}
    3939
     
    4343    if (!IPC::decode(decoder, attributedString))
    4444        return WTF::nullopt;
    45     return WebKit::AttributedString { attributedString.get() };
     45    RetainPtr<NSDictionary> documentAttributes;
     46    if (!IPC::decode(decoder, documentAttributes))
     47        return WTF::nullopt;
     48    return WebKit::AttributedString { attributedString.get(), documentAttributes.get() };
    4649}
    4750
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm

    r242979 r243012  
    3030#import "APIPageConfiguration.h"
    3131#import "APISerializedScriptValue.h"
     32#import "AttributedString.h"
    3233#import "CompletionHandlerCallChecker.h"
    3334#import "DiagnosticLoggingClient.h"
     
    52545255}
    52555256
     5257- (void)_getContentsAsAttributedStringWithCompletionHandler:(void (^)(NSAttributedString *, NSDictionary<NSAttributedStringDocumentAttributeKey, id> *, NSError *))completionHandler
     5258{
     5259    _page->getContentsAsAttributedString([handler = makeBlockPtr(completionHandler)](auto& attributedString) {
     5260        if (attributedString.string)
     5261            handler([[attributedString.string.get() retain] autorelease], [[attributedString.documentAttributes.get() retain] autorelease], nil);
     5262        else
     5263            handler(nil, nil, createNSError(WKErrorUnknown).get());
     5264    });
     5265}
     5266
    52565267- (void)_getApplicationManifestWithCompletionHandler:(void (^)(_WKApplicationManifest *))completionHandler
    52575268{
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h

    r242979 r243012  
    365365- (void)_getWebArchiveDataWithCompletionHandler:(void (^)(NSData *, NSError *))completionHandler;
    366366- (void)_getContentsAsStringWithCompletionHandler:(void (^)(NSString *, NSError *))completionHandler WK_API_AVAILABLE(macosx(10.13), ios(11.0));
     367- (void)_getContentsAsAttributedStringWithCompletionHandler:(void (^)(NSAttributedString *, NSDictionary<NSAttributedStringDocumentAttributeKey, id> *, NSError *))completionHandler WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_MAC_TBA));
    367368
    368369- (void)_getApplicationManifestWithCompletionHandler:(void (^)(_WKApplicationManifest *))completionHandler WK_API_AVAILABLE(macosx(10.13.4), ios(11.3));
  • trunk/Source/WebKit/UIProcess/WebPageProxy.cpp

    r243009 r243012  
    5353#include "APIURLRequest.h"
    5454#include "APIWebsitePolicies.h"
     55#include "AttributedString.h"
    5556#include "AuthenticationChallengeProxy.h"
    5657#include "AuthenticationDecisionListener.h"
     
    36023603}
    36033604
     3605void WebPageProxy::getContentsAsAttributedString(CompletionHandler<void(const AttributedString&)>&& completionHandler)
     3606{
     3607#if PLATFORM(COCOA)
     3608    if (!isValid()) {
     3609        completionHandler(AttributedString());
     3610        return;
     3611    }
     3612
     3613    m_process->connection()->sendWithAsyncReply(Messages::WebPage::GetContentsAsAttributedString(), WTFMove(completionHandler), m_pageID);
     3614#else
     3615    ASSERT_NOT_REACHED();
     3616    completionHandler(AttributedString());
     3617#endif
     3618}
     3619
    36043620void WebPageProxy::getBytecodeProfile(WTF::Function<void (const String&, CallbackBase::Error)>&& callbackFunction)
    36053621{
  • trunk/Source/WebKit/UIProcess/WebPageProxy.h

    r243009 r243012  
    974974
    975975    void getContentsAsString(WTF::Function<void (const String&, CallbackBase::Error)>&&);
     976    void getContentsAsAttributedString(CompletionHandler<void(const AttributedString&)>&&);
    976977    void getBytecodeProfile(WTF::Function<void (const String&, CallbackBase::Error)>&&);
    977978    void getSamplingProfilerOutput(WTF::Function<void (const String&, CallbackBase::Error)>&&);
  • trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm

    r242748 r243012  
    2828
    2929
     30#import "AttributedString.h"
    3031#import "LoadParameters.h"
    3132#import "PluginView.h"
     
    4344#import <WebCore/RenderElement.h>
    4445#import <WebCore/RenderObject.h>
     46#import <WebCore/TextIterator.h>
    4547
    4648#if PLATFORM(COCOA)
     
    202204#endif
    203205
     206void WebPage::getContentsAsAttributedString(CompletionHandler<void(const AttributedString&)>&& completionHandler)
     207{
     208    Frame& frame = m_page->mainFrame();
     209
     210    RefPtr<Range> range = TextIterator::rangeFromLocationAndLength(frame.document()->documentElement(), 0, INT_MAX);
     211
     212    NSDictionary* documentAttributes = nil;
     213
     214    AttributedString result;
     215    result.string = attributedStringFromRange(*range, &documentAttributes);
     216    result.documentAttributes = documentAttributes;
     217
     218    completionHandler({ result });
     219}
     220
    204221} // namespace WebKit
    205222
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.h

    r243002 r243012  
    13161316
    13171317    void getContentsAsString(CallbackID);
     1318#if PLATFORM(COCOA)
     1319    void getContentsAsAttributedString(CompletionHandler<void(const AttributedString&)>&&);
     1320#endif
    13181321#if ENABLE(MHTML)
    13191322    void getContentsAsMHTMLData(CallbackID);
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in

    r243002 r243012  
    183183    # Callbacks.
    184184    GetContentsAsString(WebKit::CallbackID callbackID)
     185#if PLATFORM(COCOA)
     186    GetContentsAsAttributedString() -> (struct WebKit::AttributedString result) Async
     187#endif
    185188#if ENABLE(MHTML)
    186189    GetContentsAsMHTMLData(WebKit::CallbackID callbackID)
  • trunk/Tools/ChangeLog

    r243009 r243012  
     12019-03-15  Timothy Hatcher  <timothy@apple.com>
     2
     3        Add support to WebPage for getting the contents as an attributed string.
     4        https://bugs.webkit.org/show_bug.cgi?id=195636
     5        rdar://problem/45055697
     6
     7        Reviewed by Tim Horton.
     8
     9        * TestWebKitAPI/Tests/WebKitCocoa/WKWebViewGetContents.mm:
     10        (TEST(WKWebView, GetContentsShouldReturnAttributedString): Added.
     11
    1122019-03-15  Chris Dumez  <cdumez@apple.com>
    213
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewGetContents.mm

    r242339 r243012  
    4949    TestWebKitAPI::Util::run(&finished);
    5050}
     51
     52TEST(WKWebView, GetContentsShouldReturnAttributedString)
     53{
     54    RetainPtr<TestWKWebView> webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
     55
     56    [webView synchronouslyLoadHTMLString:@"<body bgcolor='red'>Hello <b>World!</b>"];
     57
     58    __block bool finished = false;
     59
     60#if USE(APPKIT)
     61    using PlatformFont = NSFont;
     62    using PlatformColor = NSColor;
     63#else
     64    using PlatformFont = UIFont;
     65    using PlatformColor = UIColor;
     66#endif
     67
     68    [webView _getContentsAsAttributedStringWithCompletionHandler:^(NSAttributedString *attributedString, NSDictionary<NSAttributedStringDocumentAttributeKey, id> *documentAttributes, NSError *error) {
     69        EXPECT_NOT_NULL(attributedString);
     70        EXPECT_NOT_NULL(documentAttributes);
     71        EXPECT_NULL(error);
     72
     73        __block size_t i = 0;
     74        [attributedString enumerateAttributesInRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(NSDictionary *attributes, NSRange attributeRange, BOOL *stop) {
     75            auto* substring = [attributedString attributedSubstringFromRange:attributeRange];
     76
     77            if (!i) {
     78                EXPECT_WK_STREQ(@"Hello ", substring.string);
     79                EXPECT_WK_STREQ(@"Times-Roman", dynamic_objc_cast<PlatformFont>(attributes[NSFontAttributeName]).fontName);
     80            } else if (i == 1) {
     81                EXPECT_WK_STREQ(@"World!", substring.string);
     82                EXPECT_WK_STREQ(@"Times-Bold", dynamic_objc_cast<PlatformFont>(attributes[NSFontAttributeName]).fontName);
     83            } else
     84                ASSERT_NOT_REACHED();
     85
     86            ++i;
     87        }];
     88
     89        EXPECT_WK_STREQ(@"sRGB IEC61966-2.1 colorspace 1 0 0 1", dynamic_objc_cast<PlatformColor>(documentAttributes[NSBackgroundColorDocumentAttribute]).description);
     90
     91        finished = true;
     92    }];
     93
     94    TestWebKitAPI::Util::run(&finished);
     95}
Note: See TracChangeset for help on using the changeset viewer.