1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 22:38:12 +00:00

LibWeb: Add 'Attribute' as a CSS SimpleSelector::Type

Previously, SimpleSelectors optionally had Attribute-selector data
as well as their main type. Now, they're either one or the other,
which better matches the spec, and makes parsing and matching more
straightforward.
This commit is contained in:
Sam Atkins 2021-07-12 14:58:03 +01:00 committed by Andreas Kling
parent dadcb46344
commit 96b2356cbb
5 changed files with 136 additions and 139 deletions

View file

@ -424,29 +424,31 @@ public:
if (!peek() || peek() == '{' || peek() == ',' || is_combinator(peek()))
return {};
CSS::Selector::SimpleSelector::Type type;
CSS::Selector::SimpleSelector simple_selector;
if (peek() == '*') {
type = CSS::Selector::SimpleSelector::Type::Universal;
simple_selector.type = CSS::Selector::SimpleSelector::Type::Universal;
consume_one();
CSS::Selector::SimpleSelector result;
result.type = type;
return result;
return simple_selector;
}
if (peek() == '.') {
type = CSS::Selector::SimpleSelector::Type::Class;
simple_selector.type = CSS::Selector::SimpleSelector::Type::Class;
consume_one();
} else if (peek() == '#') {
type = CSS::Selector::SimpleSelector::Type::Id;
simple_selector.type = CSS::Selector::SimpleSelector::Type::Id;
consume_one();
} else if (isalpha(peek())) {
type = CSS::Selector::SimpleSelector::Type::TagName;
simple_selector.type = CSS::Selector::SimpleSelector::Type::TagName;
} else if (peek() == '[') {
simple_selector.type = CSS::Selector::SimpleSelector::Type::Attribute;
} else {
type = CSS::Selector::SimpleSelector::Type::Universal;
simple_selector.type = CSS::Selector::SimpleSelector::Type::Universal;
}
if (type != CSS::Selector::SimpleSelector::Type::Universal) {
if ((simple_selector.type != CSS::Selector::SimpleSelector::Type::Universal)
&& (simple_selector.type != CSS::Selector::SimpleSelector::Type::Attribute)) {
while (is_valid_selector_char(peek()))
buffer.append(consume_one());
PARSE_VERIFY(!buffer.is_empty());
@ -454,18 +456,16 @@ public:
auto value = String::copy(buffer);
if (type == CSS::Selector::SimpleSelector::Type::TagName) {
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::TagName) {
// Some stylesheets use uppercase tag names, so here's a hack to just lowercase them internally.
value = value.to_lowercase();
}
CSS::Selector::SimpleSelector simple_selector;
simple_selector.type = type;
simple_selector.value = value;
buffer.clear();
if (peek() == '[') {
CSS::Selector::SimpleSelector::AttributeMatchType attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute;
if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
CSS::Selector::SimpleSelector::Attribute::MatchType attribute_match_type = CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute;
String attribute_name;
String attribute_value;
bool in_value = false;
@ -475,10 +475,10 @@ public:
char ch = consume_one();
if (ch == '=' || (ch == '~' && peek() == '=')) {
if (ch == '=') {
attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch;
attribute_match_type = CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch;
} else if (ch == '~') {
consume_one();
attribute_match_type = CSS::Selector::SimpleSelector::AttributeMatchType::ContainsWord;
attribute_match_type = CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord;
}
attribute_name = String::copy(buffer);
buffer.clear();
@ -503,9 +503,9 @@ public:
else
attribute_name = String::copy(buffer);
buffer.clear();
simple_selector.attribute_match_type = attribute_match_type;
simple_selector.attribute_name = attribute_name;
simple_selector.attribute_value = attribute_value;
simple_selector.attribute.match_type = attribute_match_type;
simple_selector.attribute.name = attribute_name;
simple_selector.attribute.value = attribute_value;
if (expected_end_of_attribute_selector != ']') {
if (!consume_specific(expected_end_of_attribute_selector))
return {};