-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
check_pseudo_has_fast_reject_filter_test.cc
132 lines (114 loc) · 6.06 KB
/
check_pseudo_has_fast_reject_filter_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/css/check_pseudo_has_fast_reject_filter.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/check_pseudo_has_argument_context.h"
#include "third_party/blink/renderer/core/css/css_selector_list.h"
#include "third_party/blink/renderer/core/css/css_test_helpers.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
namespace blink {
class CheckPseudoHasFastRejectFilterTest : public PageTestBase {
protected:
struct ElementInfo {
const char* tag_name;
const char* id;
const char* class_names;
const char* attribute_name;
const char* attribute_value;
};
template <unsigned length>
void AddElementIdentifierHashes(
CheckPseudoHasFastRejectFilter& filter,
const ElementInfo (&element_info_list)[length]) {
for (unsigned i = 0; i < length; i++) {
NonThrowableExceptionState no_exceptions;
Element* element = GetDocument().CreateElementForBinding(
element_info_list[i].tag_name, nullptr, no_exceptions);
element->setAttribute(html_names::kIdAttr, element_info_list[i].id);
element->setAttribute(html_names::kClassAttr,
element_info_list[i].class_names);
element->setAttribute(element_info_list[i].attribute_name,
element_info_list[i].attribute_value);
filter.AddElementIdentifierHashes(*element);
}
}
bool CheckFastReject(CheckPseudoHasFastRejectFilter& filter,
const char* selector_text) {
CSSSelectorList selector_list =
css_test_helpers::ParseSelectorList(selector_text);
EXPECT_EQ(selector_list.First()->GetPseudoType(), CSSSelector::kPseudoHas);
CheckPseudoHasArgumentContext context(
selector_list.First()->SelectorList()->First());
return filter.FastReject(context.GetPseudoHasArgumentHashes());
}
};
TEST_F(CheckPseudoHasFastRejectFilterTest, CheckFastReject) {
CheckPseudoHasFastRejectFilter filter;
AddElementIdentifierHashes(
filter, {{/* tag_name */ "div", /* id */ "d1", /* class_names */ "a",
/* attribute_name */ "attr1", /* attribute_value */ "val1"},
{/* tag_name */ "div", /* id */ "d2", /* class_names */ "b",
/* attribute_name */ "attr2", /* attribute_value */ "val2"},
{/* tag_name */ "span", /* id */ "s1", /* class_names */ "c",
/* attribute_name */ "attr3", /* attribute_value */ "val3"},
{/* tag_name */ "span", /* id */ "s2", /* class_names */ "d",
/* attribute_name */ "attr4", /* attribute_value */ "val4"}});
EXPECT_FALSE(CheckFastReject(filter, ":has(div)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(span)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(h1)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(#div)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(.div)"));
EXPECT_TRUE(CheckFastReject(filter, ":has([div])"));
EXPECT_TRUE(CheckFastReject(filter, ":has([div=div])"));
EXPECT_FALSE(CheckFastReject(filter, ":has(#d1)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(#d2)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(#d3)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(#s1)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(#s2)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(#s3)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(d1)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(.d1)"));
EXPECT_TRUE(CheckFastReject(filter, ":has([d1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has([d1=d1])"));
EXPECT_FALSE(CheckFastReject(filter, ":has(.a)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(.b)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(.c)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(.d)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(.e)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(a)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(#a)"));
EXPECT_TRUE(CheckFastReject(filter, ":has([a])"));
EXPECT_TRUE(CheckFastReject(filter, ":has([a=a])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr1])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr2])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr3])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr4])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr1=x])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr2=x])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr3=x])"));
EXPECT_FALSE(CheckFastReject(filter, ":has([attr4=x])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(attr1)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(#attr1)"));
EXPECT_TRUE(CheckFastReject(filter, ":has(.attr1)"));
EXPECT_FALSE(CheckFastReject(filter, ":has(div#d1.a[attr1=val1])"));
EXPECT_FALSE(CheckFastReject(filter, ":has(span#d1.a[attr1=val1])"));
EXPECT_FALSE(CheckFastReject(filter, ":has(div#s1.a[attr1=val1])"));
EXPECT_FALSE(CheckFastReject(filter, ":has(div#d1.c[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(h1#d1.a[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d3.a[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.e[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.a[attr5=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#div.a[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.div[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.a[div=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(d1#d1.a[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.d1[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.a[d1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(a#d1.a[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#a.a[attr1=val1])"));
EXPECT_TRUE(CheckFastReject(filter, ":has(div#d1.a[a=val1])"));
}
} // namespace blink