2 * Copyright (c) 2014, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of Opera Software ASA nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "core/css/RuleSet.h"
33 #include "core/css/CSSTestHelper.h"
35 #include <gtest/gtest.h>
39 TEST(RuleSetTest
, findBestRuleSetAndAdd_CustomPseudoElements
)
43 helper
.addCSSRules("summary::-webkit-details-marker { }");
44 RuleSet
& ruleSet
= helper
.ruleSet();
45 AtomicString
str("-webkit-details-marker");
46 const TerminatedArray
<RuleData
>* rules
= ruleSet
.shadowPseudoElementRules(str
);
47 ASSERT_EQ(1u, rules
->size());
48 ASSERT_EQ(str
, rules
->at(0).selector().value());
51 TEST(RuleSetTest
, findBestRuleSetAndAdd_Id
)
55 helper
.addCSSRules("#id { }");
56 RuleSet
& ruleSet
= helper
.ruleSet();
57 AtomicString
str("id");
58 const TerminatedArray
<RuleData
>* rules
= ruleSet
.idRules(str
);
59 ASSERT_EQ(1u, rules
->size());
60 ASSERT_EQ(str
, rules
->at(0).selector().value());
63 TEST(RuleSetTest
, findBestRuleSetAndAdd_NthChild
)
67 helper
.addCSSRules("div:nth-child(2) { }");
68 RuleSet
& ruleSet
= helper
.ruleSet();
69 AtomicString
str("div");
70 const TerminatedArray
<RuleData
>* rules
= ruleSet
.tagRules(str
);
71 ASSERT_EQ(1u, rules
->size());
72 ASSERT_EQ(str
, rules
->at(0).selector().tagQName().localName());
75 TEST(RuleSetTest
, findBestRuleSetAndAdd_ClassThenId
)
79 helper
.addCSSRules(".class#id { }");
80 RuleSet
& ruleSet
= helper
.ruleSet();
81 AtomicString
str("id");
82 // id is prefered over class even if class preceeds it in the selector.
83 const TerminatedArray
<RuleData
>* rules
= ruleSet
.idRules(str
);
84 ASSERT_EQ(1u, rules
->size());
85 AtomicString
classStr("class");
86 ASSERT_EQ(classStr
, rules
->at(0).selector().value());
89 TEST(RuleSetTest
, findBestRuleSetAndAdd_IdThenClass
)
93 helper
.addCSSRules("#id.class { }");
94 RuleSet
& ruleSet
= helper
.ruleSet();
95 AtomicString
str("id");
96 const TerminatedArray
<RuleData
>* rules
= ruleSet
.idRules(str
);
97 ASSERT_EQ(1u, rules
->size());
98 ASSERT_EQ(str
, rules
->at(0).selector().value());
101 TEST(RuleSetTest
, findBestRuleSetAndAdd_AttrThenId
)
103 CSSTestHelper helper
;
105 helper
.addCSSRules("[attr]#id { }");
106 RuleSet
& ruleSet
= helper
.ruleSet();
107 AtomicString
str("id");
108 const TerminatedArray
<RuleData
>* rules
= ruleSet
.idRules(str
);
109 ASSERT_EQ(1u, rules
->size());
110 AtomicString
attrStr("attr");
111 ASSERT_EQ(attrStr
, rules
->at(0).selector().attribute().localName());
114 TEST(RuleSetTest
, findBestRuleSetAndAdd_TagThenAttrThenId
)
116 CSSTestHelper helper
;
118 helper
.addCSSRules("div[attr]#id { }");
119 RuleSet
& ruleSet
= helper
.ruleSet();
120 AtomicString
str("id");
121 const TerminatedArray
<RuleData
>* rules
= ruleSet
.idRules(str
);
122 ASSERT_EQ(1u, rules
->size());
123 AtomicString
tagStr("div");
124 ASSERT_EQ(tagStr
, rules
->at(0).selector().tagQName().localName());
127 TEST(RuleSetTest
, findBestRuleSetAndAdd_DivWithContent
)
129 CSSTestHelper helper
;
131 helper
.addCSSRules("div::content { }");
132 RuleSet
& ruleSet
= helper
.ruleSet();
133 AtomicString
str("div");
134 const TerminatedArray
<RuleData
>* rules
= ruleSet
.tagRules(str
);
135 ASSERT_EQ(1u, rules
->size());
136 AtomicString
valueStr("content");
137 ASSERT_EQ(valueStr
, rules
->at(0).selector().value());
140 TEST(RuleSetTest
, findBestRuleSetAndAdd_Host
)
142 CSSTestHelper helper
;
144 helper
.addCSSRules(":host { }");
145 RuleSet
& ruleSet
= helper
.ruleSet();
146 const WillBeHeapVector
<RuleData
>* rules
= ruleSet
.shadowHostRules();
147 ASSERT_EQ(1u, rules
->size());
150 TEST(RuleSetTest
, findBestRuleSetAndAdd_HostWithId
)
152 CSSTestHelper helper
;
154 helper
.addCSSRules(":host(#x) { }");
155 RuleSet
& ruleSet
= helper
.ruleSet();
156 const WillBeHeapVector
<RuleData
>* rules
= ruleSet
.shadowHostRules();
157 ASSERT_EQ(1u, rules
->size());
160 TEST(RuleSetTest
, findBestRuleSetAndAdd_HostContext
)
162 CSSTestHelper helper
;
164 helper
.addCSSRules(":host-context(*) { }");
165 RuleSet
& ruleSet
= helper
.ruleSet();
166 const WillBeHeapVector
<RuleData
>* rules
= ruleSet
.shadowHostRules();
167 ASSERT_EQ(1u, rules
->size());
170 TEST(RuleSetTest
, findBestRuleSetAndAdd_HostContextWithId
)
172 CSSTestHelper helper
;
174 helper
.addCSSRules(":host-context(#x) { }");
175 RuleSet
& ruleSet
= helper
.ruleSet();
176 const WillBeHeapVector
<RuleData
>* rules
= ruleSet
.shadowHostRules();
177 ASSERT_EQ(1u, rules
->size());
180 TEST(RuleSetTest
, findBestRuleSetAndAdd_HostAndHostContextNotInRightmost
)
182 CSSTestHelper helper
;
184 helper
.addCSSRules(":host-context(#x) .y, :host(.a) > #b { }");
185 RuleSet
& ruleSet
= helper
.ruleSet();
186 const WillBeHeapVector
<RuleData
>* shadowRules
= ruleSet
.shadowHostRules();
187 const TerminatedArray
<RuleData
>* idRules
= ruleSet
.idRules("b");
188 const TerminatedArray
<RuleData
>* classRules
= ruleSet
.classRules("y");
189 ASSERT_EQ(0u, shadowRules
->size());
190 ASSERT_EQ(1u, idRules
->size());
191 ASSERT_EQ(1u, classRules
->size());
194 TEST(RuleSetTest
, findBestRuleSetAndAdd_HostAndClass
)
196 CSSTestHelper helper
;
198 helper
.addCSSRules(".foo:host { }");
199 RuleSet
& ruleSet
= helper
.ruleSet();
200 const WillBeHeapVector
<RuleData
>* rules
= ruleSet
.shadowHostRules();
201 ASSERT_EQ(0u, rules
->size());
204 TEST(RuleSetTest
, findBestRuleSetAndAdd_HostContextAndClass
)
206 CSSTestHelper helper
;
208 helper
.addCSSRules(".foo:host-context(*) { }");
209 RuleSet
& ruleSet
= helper
.ruleSet();
210 const WillBeHeapVector
<RuleData
>* rules
= ruleSet
.shadowHostRules();
211 ASSERT_EQ(0u, rules
->size());