blob: 694d59f6568c061b86e32c248a706ac8afe5ec4e [file] [log] [blame]
// Copyright 2014 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/css_test_helpers.h"
#include "third_party/blink/renderer/core/css/rule_set.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
using namespace css_test_helpers;
TEST(CSSSelector, Representations) {
TestStyleSheet sheet;
const char* css_rules =
"summary::-webkit-details-marker { }"
"* {}"
"div {}"
"#id {}"
".class {}"
"[attr] {}"
"div:hover {}"
"div:nth-child(2){}"
".class#id { }"
"#id.class { }"
"[attr]#id { }"
"div[attr]#id { }"
"div::content { }"
"div::first-line { }"
".a.b.c { }"
"div:not(.a) { }" // without class a
"div:not(:visited) { }" // without the visited pseudo class
"[attr=\"value\"] { }" // Exact equality
"[attr~=\"value\"] { }" // One of a space-separated list
"[attr^=\"value\"] { }" // Begins with
"[attr$=\"value\"] { }" // Ends with
"[attr*=\"value\"] { }" // Substring equal to
"[attr|=\"value\"] { }" // One of a hyphen-separated list
".a .b { }" // .b is a descendant of .a
".a > .b { }" // .b is a direct descendant of .a
".a ~ .b { }" // .a precedes .b in sibling order
".a + .b { }" // .a element immediately precedes .b in sibling order
".a, .b { }" // matches .a or .b
".a.b .c {}";
sheet.AddCSSRules(css_rules);
EXPECT_EQ(30u,
sheet.GetRuleSet().RuleCount()); // .a, .b counts as two rules.
#ifndef NDEBUG
sheet.GetRuleSet().Show();
#endif
}
TEST(CSSSelector, OverflowRareDataMatchNth) {
int max_int = std::numeric_limits<int>::max();
int min_int = std::numeric_limits<int>::min();
CSSSelector selector;
// Overflow count - b (max_int - -1 = max_int + 1)
selector.SetNth(1, -1);
EXPECT_FALSE(selector.MatchNth(max_int));
// 0 - (min_int) = max_int + 1
selector.SetNth(1, min_int);
EXPECT_FALSE(selector.MatchNth(0));
// min_int - 1
selector.SetNth(-1, min_int);
EXPECT_FALSE(selector.MatchNth(1));
// a shouldn't negate to itself (and min_int negates to itself).
// Note: This test can only fail when using ubsan.
selector.SetNth(min_int, 10);
EXPECT_FALSE(selector.MatchNth(2));
}
} // namespace blink