2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-08 20:19:25 +00:00
|
|
|
#include "Selector.h"
|
|
|
|
#include <AK/StringUtils.h>
|
2020-03-07 09:32:51 +00:00
|
|
|
#include <LibWeb/CSS/Selector.h>
|
2021-05-08 20:19:25 +00:00
|
|
|
#include <ctype.h>
|
2019-06-20 21:25:25 +00:00
|
|
|
|
2020-07-26 18:01:35 +00:00
|
|
|
namespace Web::CSS {
|
2020-03-07 09:27:02 +00:00
|
|
|
|
2019-11-27 19:37:36 +00:00
|
|
|
Selector::Selector(Vector<ComplexSelector>&& component_lists)
|
|
|
|
: m_complex_selectors(move(component_lists))
|
2019-06-20 21:25:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Selector::~Selector()
|
|
|
|
{
|
|
|
|
}
|
2019-06-29 15:32:32 +00:00
|
|
|
|
2020-06-25 14:43:49 +00:00
|
|
|
u32 Selector::specificity() const
|
2019-06-29 15:32:32 +00:00
|
|
|
{
|
|
|
|
unsigned ids = 0;
|
|
|
|
unsigned tag_names = 0;
|
|
|
|
unsigned classes = 0;
|
|
|
|
|
2019-11-27 19:37:36 +00:00
|
|
|
for (auto& list : m_complex_selectors) {
|
|
|
|
for (auto& simple_selector : list.compound_selector) {
|
|
|
|
switch (simple_selector.type) {
|
|
|
|
case SimpleSelector::Type::Id:
|
|
|
|
++ids;
|
|
|
|
break;
|
|
|
|
case SimpleSelector::Type::Class:
|
|
|
|
++classes;
|
|
|
|
break;
|
|
|
|
case SimpleSelector::Type::TagName:
|
|
|
|
++tag_names;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-06-29 15:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:43:49 +00:00
|
|
|
return ids * 0x10000 + classes * 0x100 + tag_names;
|
2019-06-29 15:32:32 +00:00
|
|
|
}
|
2020-03-07 09:27:02 +00:00
|
|
|
|
2021-07-12 13:13:29 +00:00
|
|
|
Selector::SimpleSelector::NthChildPattern Selector::SimpleSelector::NthChildPattern::parse(StringView const& args)
|
2021-05-08 20:19:25 +00:00
|
|
|
{
|
2021-07-12 15:18:00 +00:00
|
|
|
// FIXME: Remove this when the DeprecatedCSSParser is gone.
|
|
|
|
// The new Parser::parse_nth_child_pattern() does the same as this, using Tokens.
|
2021-05-08 20:19:25 +00:00
|
|
|
CSS::Selector::SimpleSelector::NthChildPattern pattern;
|
|
|
|
if (args.equals_ignoring_case("odd")) {
|
|
|
|
pattern.step_size = 2;
|
|
|
|
pattern.offset = 1;
|
|
|
|
} else if (args.equals_ignoring_case("even")) {
|
|
|
|
pattern.step_size = 2;
|
|
|
|
} else {
|
2021-07-12 13:13:29 +00:00
|
|
|
auto const consume_int = [](GenericLexer& lexer) -> Optional<int> {
|
2021-05-08 20:19:25 +00:00
|
|
|
return AK::StringUtils::convert_to_int(lexer.consume_while([](char c) -> bool {
|
|
|
|
return isdigit(c) || c == '+' || c == '-';
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Try to match any of following patterns:
|
|
|
|
// 1. An+B
|
|
|
|
// 2. An
|
|
|
|
// 3. B
|
|
|
|
// ...where "A" is "step_size", "B" is "offset" and rest are literals.
|
|
|
|
// "A" can be omitted, in that case "A" = 1.
|
|
|
|
// "A" may have "+" or "-" sign, "B" always must be predated by sign for pattern (1).
|
|
|
|
|
|
|
|
int step_size_or_offset = 0;
|
|
|
|
GenericLexer lexer { args };
|
|
|
|
|
|
|
|
// "When a=1, or a=-1, the 1 may be omitted from the rule."
|
|
|
|
if (lexer.consume_specific("n") || lexer.consume_specific("+n")) {
|
|
|
|
step_size_or_offset = +1;
|
|
|
|
lexer.retreat();
|
|
|
|
} else if (lexer.consume_specific("-n")) {
|
|
|
|
step_size_or_offset = -1;
|
|
|
|
lexer.retreat();
|
|
|
|
} else {
|
2021-07-12 13:13:29 +00:00
|
|
|
auto const value = consume_int(lexer);
|
2021-05-08 20:19:25 +00:00
|
|
|
if (!value.has_value())
|
|
|
|
return {};
|
|
|
|
step_size_or_offset = value.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lexer.consume_specific("n")) {
|
|
|
|
lexer.ignore_while(isspace);
|
|
|
|
if (lexer.next_is('+') || lexer.next_is('-')) {
|
2021-07-12 13:13:29 +00:00
|
|
|
auto const sign = lexer.next_is('+') ? 1 : -1;
|
2021-05-08 20:19:25 +00:00
|
|
|
lexer.ignore();
|
|
|
|
lexer.ignore_while(isspace);
|
|
|
|
|
|
|
|
// "An+B" pattern
|
2021-07-12 13:13:29 +00:00
|
|
|
auto const offset = consume_int(lexer);
|
2021-05-08 20:19:25 +00:00
|
|
|
if (!offset.has_value())
|
|
|
|
return {};
|
|
|
|
pattern.step_size = step_size_or_offset;
|
|
|
|
pattern.offset = sign * offset.value();
|
|
|
|
} else {
|
|
|
|
// "An" pattern
|
|
|
|
pattern.step_size = step_size_or_offset;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// "B" pattern
|
|
|
|
pattern.offset = step_size_or_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lexer.remaining().length() > 0)
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
2020-03-07 09:27:02 +00:00
|
|
|
}
|