8000 Safari Content Blocker doesn't support :has() selector · WebKit/WebKit@355ce06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 355ce06

Browse files
committed
Safari Content Blocker doesn't support :has() selector
https://bugs.webkit.org/show_bug.cgi?id=250609 rdar://103976010 Reviewed by Antti Koivisto and Alex Christensen. When creating the CSSParsers for content blocker parsing and compilation, make sure to opt into hasPseudoClassEnabled so selectors like :has work. The places this was needed were: - ContentExtensionParser (for parsing the rules) - ContentExtension (for the global display none rules) - ContentExtensionStyleSheet (for the other display none rules) Also update the contentextensions css-display-none test to test this functionality. * Source/WebCore/contentextensions/ContentExtension.cpp: (WebCore::ContentExtensions::ContentExtension::compileGlobalDisplayNoneStyleSheet): * Source/WebCore/contentextensions/ContentExtensionParser.cpp: (WebCore::ContentExtensions::isValidCSSSelector): Canonical link: https://commits.webkit.org/259068@main
1 parent 9721073 commit 355ce06

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

LayoutTests/http/tests/contentextensions/css-display-none.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
<p class="hidden_global2">This text should not be visible once the global css selector is applied.</p>
77
<p class="hidden">This text should not be visible once the particular css selector is applied.</p>
88
<p class="hidden_Ž">This text should not be visible once the particular css selector with non-ascii characters is applied.</p>
9+
<div><p class="hidden_hasTestMatchingEverything">This text should not be visible once the global css selector with :has is applied.</p></div>
10+
<div><p class="hidden_hasTestNotMatchingEverything">This text should not be visible once the particular css selector with :has is applied.</p></div>
911
<p class="not_hidden">This text should be visible.</p>
1012
</body>

LayoutTests/http/tests/contentextensions/css-display-none.html.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@
3535
"url-filter": ".*"
3636
}
3737
},
38+
{
39+
"action": {
40+
"type": "css-display-none",
41+
"selector": "div:has(.hidden_hasTestNotMatchingEverything)"
42+
},
43+
"trigger": {
44+
"url-filter": ".*css-display-none.html"
45+
}
46+
},
47+
{
48+
"action": {
49+
"type": "css-display-none",
50+
"selector": "div:has(.hidden_hasTestMatchingEverything)"
51+
},
52+
"trigger": {
53+
"url-filter": ".*"
54+
}
55+
},
3856
{
3957
"action": {
4058
"type": "ignore-previous-rules"

Source/WebCore/contentextensions/ContentExtension.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include "config.h"
2727
#include "ContentExtension.h"
2828

29+
#include "CSSParserContext.h"
2930
#include "CompiledContentExtension.h"
31+
#include "ContentExtensionParser.h"
3032
#include "ContentExtensionsBackend.h"
3133
#include "StyleSheetContents.h"
3234
#include <wtf/text/StringBuilder.h>
@@ -98,7 +100,7 @@ void ContentExtension::compileGlobalDisplayNoneStyleSheet()
98100
css.append(ContentExtensionsBackend::displayNoneCSSRule());
99101
css.append('}');
100102

101-
m_globalDisplayNoneStyleSheet = StyleSheetContents::create();
103+
m_globalDisplayNoneStyleSheet = StyleSheetContents::create(contentExtensionCSSParserContext());
102104
m_globalDisplayNoneStyleSheet->setIsUserStyleSheet(true);
103105
if (!m_globalDisplayNoneStyleSheet->parseString(css.toString()))
104106
m_globalDisplayNoneStyleSheet = nullptr;

Source/WebCore/contentextensions/ContentExtensionParser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,17 @@ bool isValidCSSSelector(const String& selector)
206206
{
207207
ASSERT(isMainThread());
208208
ProcessWarming::initializeNames();
209-
CSSParserContext context(HTMLQuirksMode);
210-
CSSParser parser(context);
209+
CSSParser parser(contentExtensionCSSParserContext());
211210
return !!parser.parseSelector(selector);
212211
}
213212

213+
WebCore::CSSParserContext contentExtensionCSSParserContext()
214+
{
215+
WebCore::CSSParserContext context(HTMLQuirksMode);
216+
context.hasPseudoClassEnabled = true;
217+
return context;
218+
}
219+
214220
static std::optional<Expected<Action, std::error_code>> loadAction(const JSON::Object& ruleObject, const String& urlFilter)
215221
{
216222
auto actionObject = ruleObject.getObject("action"_s);

Source/WebCore/contentextensions/ContentExtensionParser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@
3434

3535
namespace WebCore {
3636

37+
struct CSSParserContext;
38+
3739
namespace ContentExtensions {
3840

3941
class ContentExtensionRule;
4042

4143
WEBCORE_EXPORT Expected<Vector<ContentExtensionRule>, std::error_code> parseRuleList(const String&);
4244
WEBCORE_EXPORT bool isValidCSSSelector(const String&);
4345

46+
CSSParserContext contentExtensionCSSParserContext();
47+
4448
} // namespace ContentExtensions
4549
} // namespace WebCore
4650

Source/WebCore/contentextensions/ContentExtensionStyleSheet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#if ENABLE(CONTENT_EXTENSIONS)
3030

3131
#include "CSSStyleSheet.h"
32+
#include "ContentExtensionParser.h"
3233
#include "ContentExtensionsBackend.h"
3334
#include "Document.h"
3435
#include "StyleSheetContents.h"
@@ -38,7 +39,7 @@ namespace WebCore {
3839
namespace ContentExtensions {
3940

4041
ContentExtensionStyleSheet::ContentExtensionStyleSheet(Document& document)
41-
: m_styleSheet(CSSStyleSheet::create(StyleSheetContents::create(), document))
42+
: m_styleSheet(CSSStyleSheet::create(StyleSheetContents::create(contentExtensionCSSParserContext()), document))
4243
{
4344
m_styleSheet->contents().setIsUserStyleSheet(true);
4445
}

Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "Utilities.h"
3131
#include <JavaScriptCore/InitializeThreading.h>
32+
#include <WebCore/CSSParserContext.h>
3233
#include <WebCore/CombinedURLFilters.h>
3334
#include <WebCore/CommonAtomStrings.h>
3435
#include <WebCore/ContentExtensionActions.h>

0 commit comments

Comments
 (0)
0