Changeset 243012 in webkit
- Timestamp:
- Mar 15, 2019 2:36:56 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit/ChangeLog
r243010 r243012 1 2019-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 1 25 2019-03-15 Per Arne Vollan <pvollan@apple.com> 2 26 -
trunk/Source/WebKit/Shared/mac/AttributedString.h
r242285 r243012 39 39 40 40 #if defined(__OBJC__) 41 AttributedString(NSAttributedString *attributedString )41 AttributedString(NSAttributedString *attributedString, NSDictionary *documentAttributes = nil) 42 42 : string(attributedString) 43 , documentAttributes(documentAttributes) 43 44 { 44 45 } … … 49 50 } 50 51 #endif 51 52 52 53 RetainPtr<NSAttributedString> string; 54 RetainPtr<NSDictionary> documentAttributes; 53 55 }; 54 56 -
trunk/Source/WebKit/Shared/mac/AttributedString.mm
r242908 r243012 35 35 void ArgumentCoder<WebKit::AttributedString>::encode(Encoder& encoder, const WebKit::AttributedString& attributedString) 36 36 { 37 encoder << attributedString.string ;37 encoder << attributedString.string << attributedString.documentAttributes; 38 38 } 39 39 … … 43 43 if (!IPC::decode(decoder, attributedString)) 44 44 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() }; 46 49 } 47 50 -
trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
r242979 r243012 30 30 #import "APIPageConfiguration.h" 31 31 #import "APISerializedScriptValue.h" 32 #import "AttributedString.h" 32 33 #import "CompletionHandlerCallChecker.h" 33 34 #import "DiagnosticLoggingClient.h" … … 5254 5255 } 5255 5256 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 5256 5267 - (void)_getApplicationManifestWithCompletionHandler:(void (^)(_WKApplicationManifest *))completionHandler 5257 5268 { -
trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h
r242979 r243012 365 365 - (void)_getWebArchiveDataWithCompletionHandler:(void (^)(NSData *, NSError *))completionHandler; 366 366 - (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)); 367 368 368 369 - (void)_getApplicationManifestWithCompletionHandler:(void (^)(_WKApplicationManifest *))completionHandler WK_API_AVAILABLE(macosx(10.13.4), ios(11.3)); -
trunk/Source/WebKit/UIProcess/WebPageProxy.cpp
r243009 r243012 53 53 #include "APIURLRequest.h" 54 54 #include "APIWebsitePolicies.h" 55 #include "AttributedString.h" 55 56 #include "AuthenticationChallengeProxy.h" 56 57 #include "AuthenticationDecisionListener.h" … … 3602 3603 } 3603 3604 3605 void 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 3604 3620 void WebPageProxy::getBytecodeProfile(WTF::Function<void (const String&, CallbackBase::Error)>&& callbackFunction) 3605 3621 { -
trunk/Source/WebKit/UIProcess/WebPageProxy.h
r243009 r243012 974 974 975 975 void getContentsAsString(WTF::Function<void (const String&, CallbackBase::Error)>&&); 976 void getContentsAsAttributedString(CompletionHandler<void(const AttributedString&)>&&); 976 977 void getBytecodeProfile(WTF::Function<void (const String&, CallbackBase::Error)>&&); 977 978 void getSamplingProfilerOutput(WTF::Function<void (const String&, CallbackBase::Error)>&&); -
trunk/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm
r242748 r243012 28 28 29 29 30 #import "AttributedString.h" 30 31 #import "LoadParameters.h" 31 32 #import "PluginView.h" … … 43 44 #import <WebCore/RenderElement.h> 44 45 #import <WebCore/RenderObject.h> 46 #import <WebCore/TextIterator.h> 45 47 46 48 #if PLATFORM(COCOA) … … 202 204 #endif 203 205 206 void 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 204 221 } // namespace WebKit 205 222 -
trunk/Source/WebKit/WebProcess/WebPage/WebPage.h
r243002 r243012 1316 1316 1317 1317 void getContentsAsString(CallbackID); 1318 #if PLATFORM(COCOA) 1319 void getContentsAsAttributedString(CompletionHandler<void(const AttributedString&)>&&); 1320 #endif 1318 1321 #if ENABLE(MHTML) 1319 1322 void getContentsAsMHTMLData(CallbackID); -
trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in
r243002 r243012 183 183 # Callbacks. 184 184 GetContentsAsString(WebKit::CallbackID callbackID) 185 #if PLATFORM(COCOA) 186 GetContentsAsAttributedString() -> (struct WebKit::AttributedString result) Async 187 #endif 185 188 #if ENABLE(MHTML) 186 189 GetContentsAsMHTMLData(WebKit::CallbackID callbackID) -
trunk/Tools/ChangeLog
r243009 r243012 1 2019-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 1 12 2019-03-15 Chris Dumez <cdumez@apple.com> 2 13 -
trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewGetContents.mm
r242339 r243012 49 49 TestWebKitAPI::Util::run(&finished); 50 50 } 51 52 TEST(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.