Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/_WKUserStyleSheet.h (260253 => 260254)
--- trunk/Source/WebKit/UIProcess/API/Cocoa/_WKUserStyleSheet.h 2020-04-17 15:41:55 UTC (rev 260253)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/_WKUserStyleSheet.h 2020-04-17 15:47:39 UTC (rev 260254)
@@ -32,6 +32,11 @@
@class _WKUserContentWorld;
@class WKWebView;
+typedef NS_ENUM(NSInteger, _WKUserStyleLevel) {
+ _WKUserStyleUserLevel,
+ _WKUserStyleAuthorLevel
+} WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
+
WK_CLASS_AVAILABLE(macos(10.12), ios(10.0))
@interface _WKUserStyleSheet : NSObject <NSCopying>
@@ -44,9 +49,10 @@
- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-- (instancetype)initWithSource:(NSString *)source forWKWebView:(WKWebView *)webView forMainFrameOnly:(BOOL)forMainFrameOnly userContentWorld:(_WKUserContentWorld *)userContentWorld;
+- (instancetype)initWithSource:(NSString *)source forWKWebView:(WKWebView *)webView forMainFrameOnly:(BOOL)forMainFrameOnly level:(_WKUserStyleLevel)level userContentWorld:(_WKUserContentWorld *)userContentWorld WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly legacyWhitelist:(NSArray<NSString *> *)legacyWhitelist legacyBlacklist:(NSArray<NSString *> *)legacyBlacklist userContentWorld:(_WKUserContentWorld *)userContentWorld;
- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly legacyWhitelist:(NSArray<NSString *> *)legacyWhitelist legacyBlacklist:(NSArray<NSString *> *)legacyBlacklist baseURL:(NSURL *)baseURL userContentWorld:(_WKUserContentWorld *)userContentWorld;
+- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly legacyWhitelist:(NSArray<NSString *> *)legacyWhitelist legacyBlacklist:(NSArray<NSString *> *)legacyBlacklist baseURL:(NSURL *)baseURL level:(_WKUserStyleLevel)level userContentWorld:(_WKUserContentWorld *)userContentWorld WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
#pragma clang diagnostic pop
@end
Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/_WKUserStyleSheet.mm (260253 => 260254)
--- trunk/Source/WebKit/UIProcess/API/Cocoa/_WKUserStyleSheet.mm 2020-04-17 15:41:55 UTC (rev 260253)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/_WKUserStyleSheet.mm 2020-04-17 15:47:39 UTC (rev 260254)
@@ -51,7 +51,7 @@
}
ALLOW_DEPRECATED_DECLARATIONS_BEGIN
-- (instancetype)initWithSource:(NSString *)source forWKWebView:(WKWebView *)webView forMainFrameOnly:(BOOL)forMainFrameOnly userContentWorld:(_WKUserContentWorld *)userContentWorld
+- (instancetype)initWithSource:(NSString *)source forWKWebView:(WKWebView *)webView forMainFrameOnly:(BOOL)forMainFrameOnly level:(_WKUserStyleLevel)level userContentWorld:(_WKUserContentWorld *)userContentWorld
{
if (!(self = [super init]))
return nil;
@@ -59,7 +59,7 @@
// FIXME: In the API test, we can use generateUniqueURL below before the API::Object constructor has done this... where should this really be?
WebKit::InitializeWebKit2();
- API::Object::constructInWrapper<API::UserStyleSheet>(self, WebCore::UserStyleSheet { WTF::String(source), API::UserStyleSheet::generateUniqueURL(), { }, { }, forMainFrameOnly ? WebCore::UserContentInjectedFrames::InjectInTopFrameOnly : WebCore::UserContentInjectedFrames::InjectInAllFrames, WebCore::UserStyleUserLevel, [webView _page]->webPageID() }, *userContentWorld->_contentWorld->_contentWorld);
+ API::Object::constructInWrapper<API::UserStyleSheet>(self, WebCore::UserStyleSheet { WTF::String(source), API::UserStyleSheet::generateUniqueURL(), { }, { }, forMainFrameOnly ? WebCore::UserContentInjectedFrames::InjectInTopFrameOnly : WebCore::UserContentInjectedFrames::InjectInAllFrames, API::toWebCoreUserStyleLevel(level), [webView _page]->webPageID() }, *userContentWorld->_contentWorld->_contentWorld);
return self;
}
@@ -89,6 +89,19 @@
return self;
}
+
+- (instancetype)initWithSource:(NSString *)source forMainFrameOnly:(BOOL)forMainFrameOnly legacyWhitelist:(NSArray<NSString *> *)legacyWhitelist legacyBlacklist:(NSArray<NSString *> *)legacyBlacklist baseURL:(NSURL *)baseURL level:(_WKUserStyleLevel)level userContentWorld:(_WKUserContentWorld *)userContentWorld
+{
+ if (!(self = [super init]))
+ return nil;
+
+ // FIXME: In the API test, we can use generateUniqueURL below before the API::Object constructor has done this... where should this really be?
+ WebKit::InitializeWebKit2();
+
+ API::Object::constructInWrapper<API::UserStyleSheet>(self, WebCore::UserStyleSheet { WTF::String(source), { URL(), WTF::String([baseURL _web_originalDataAsWTFString]) }, makeVector<String>(legacyWhitelist), makeVector<String>(legacyBlacklist), forMainFrameOnly ? WebCore::UserContentInjectedFrames::InjectInTopFrameOnly : WebCore::UserContentInjectedFrames::InjectInAllFrames, API::toWebCoreUserStyleLevel(level) }, *userContentWorld->_contentWorld->_contentWorld);
+
+ return self;
+}
ALLOW_DEPRECATED_DECLARATIONS_END
- (void)dealloc
Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm (260253 => 260254)
--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm 2020-04-17 15:41:55 UTC (rev 260253)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/UserContentController.mm 2020-04-17 15:47:39 UTC (rev 260254)
@@ -328,6 +328,7 @@
static NSString *styleSheetSource = @"body { background-color: green !important; }";
static NSString *backgroundColorScript = @"window.getComputedStyle(document.body, null).getPropertyValue('background-color')";
static NSString *frameBackgroundColorScript = @"window.getComputedStyle(document.getElementsByTagName('iframe')[0].contentDocument.body, null).getPropertyValue('background-color')";
+static NSString *styleSheetSourceForSpecificityLevelTests = @"#body { background-color: green; }";
static const char* greenInRGB = "rgb(0, 128, 0)";
static const char* redInRGB = "rgb(255, 0, 0)";
static const char* whiteInRGB = "rgba(0, 0, 0, 0)";
@@ -522,7 +523,7 @@
expectScriptEvaluatesToColor(otherWebView.get(), backgroundColorScript, redInRGB);
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[targetWebView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
expectScriptEvaluatesToColor(targetWebView.get(), backgroundColorScript, greenInRGB);
@@ -535,7 +536,7 @@
[targetWebView loadHTMLString:@"<body style='background-color: red;'></body>" baseURL:nil];
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[targetWebView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
[targetWebView _test_waitForDidFinishNavigation];
@@ -549,7 +550,7 @@
[targetWebView loadHTMLString:@"<body style='background-color: red;'></body>" baseURL:nil];
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[targetWebView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
[[targetWebView configuration].userContentController _removeUserStyleSheet:styleSheet.get()];
@@ -575,7 +576,7 @@
expectScriptEvaluatesToColor(otherWebView.get(), backgroundColorScript, redInRGB);
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:targetWebView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[targetWebView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
expectScriptEvaluatesToColor(targetWebView.get(), backgroundColorScript, greenInRGB);
@@ -592,7 +593,7 @@
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, redInRGB);
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[webView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, greenInRGB);
@@ -612,7 +613,7 @@
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, redInRGB);
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[webView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, greenInRGB);
@@ -636,7 +637,7 @@
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, redInRGB);
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[webView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, greenInRGB);
@@ -672,7 +673,7 @@
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, redInRGB);
RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
- RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES userContentWorld:world.get()]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forWKWebView:webView.get() forMainFrameOnly:YES level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
[[webView configuration].userContentController _addUserStyleSheet:styleSheet.get()];
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, greenInRGB);
@@ -683,6 +684,52 @@
expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, blueInRGB);
}
+TEST(WKUserContentController, UserStyleSheetSpecificityLevelNotSpecified)
+{
+ RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSourceForSpecificityLevelTests forMainFrameOnly:YES]);
+ [[configuration userContentController] _addUserStyleSheet:styleSheet.get()];
+
+ RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+ [webView loadHTMLString:@"<style>body { background-color: red }</style><body id='body'></body>" baseURL:nil];
+ [webView _test_waitForDidFinishNavigation];
+
+ // By default, the injected style sheet will not be able to override due to the "user" level being used.
+ expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, redInRGB);
+}
+
+TEST(WKUserContentController, UserStyleSheetSpecificityLevelUser)
+{
+ RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
+
+ RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSourceForSpecificityLevelTests forMainFrameOnly:YES legacyWhitelist:@[] legacyBlacklist:@[] baseURL:[[NSURL alloc] initWithString:@"http://example.com/"] level:_WKUserStyleUserLevel userContentWorld:world.get()]);
+ [[configuration userContentController] _addUserStyleSheet:styleSheet.get()];
+
+ RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+ [webView loadHTMLString:@"<style>body { background-color: red }</style><body id='body'></body>" baseURL:nil];
+ [webView _test_waitForDidFinishNavigation];
+
+ // The injected style sheet will not be able to override due to the "user" level being used.
+ expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, redInRGB);
+}
+
+TEST(WKUserContentController, UserStyleSheetSpecificityLevelAuthor)
+{
+ RetainPtr<_WKUserContentWorld> world = [_WKUserContentWorld worldWithName:@"TestWorld"];
+
+ RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+ RetainPtr<_WKUserStyleSheet> styleSheet = adoptNS([[_WKUserStyleSheet alloc] initWithSource:styleSheetSourceForSpecificityLevelTests forMainFrameOnly:YES legacyWhitelist:@[] legacyBlacklist:@[] baseURL:[[NSURL alloc] initWithString:@"http://example.com/"] level:_WKUserStyleAuthorLevel userContentWorld:world.get()]);
+ [[configuration userContentController] _addUserStyleSheet:styleSheet.get()];
+
+ RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+ [webView loadHTMLString:@"<style>body { background-color: red }</style><body id='body'></body>" baseURL:nil];
+ [webView _test_waitForDidFinishNavigation];
+
+ // The injected style sheet *will* be able to override due to the "author" level being used.
+ expectScriptEvaluatesToColor(webView.get(), backgroundColorScript, greenInRGB);
+}
+
TEST(WKUserContentController, UserScriptRemove)
{
RetainPtr<WKUserContentController> userContentController = adoptNS([[WKUserContentController alloc] init]);