summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Glazman <daniel@glazman.org>2022-03-29 17:46:32 +0200
committerAndreas Kling <kling@serenityos.org>2022-03-29 18:53:20 +0200
commit049d847230e034a485fa8f2e99c593ef5b4078bd (patch)
treea6a323fb2b712355aad65bb12bced4338b596604
parent8f3326616a54d9dc597d7d8576f73f3e23b1ef3e (diff)
downloadserenity-049d847230e034a485fa8f2e99c593ef5b4078bd.zip
LibWeb: Clarify attribute selectors when needle is empty
-rw-r--r--Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp25
1 files changed, 19 insertions, 6 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp
index 504e44eae4..0801e0432f 100644
--- a/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp
+++ b/Userland/Libraries/LibWeb/CSS/SelectorEngine.cpp
@@ -84,17 +84,30 @@ static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute co
case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
return element.attribute(attribute.name) == attribute.value;
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
- return element.attribute(attribute.name).split_view(' ').contains_slow(attribute.value);
+ return !attribute.value.is_empty()
+ && element.attribute(attribute.name).split_view(' ').contains_slow(attribute.value);
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
- return element.attribute(attribute.name).contains(attribute.value);
+ return !attribute.value.is_empty()
+ && element.attribute(attribute.name).contains(attribute.value);
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment: {
- auto segments = element.attribute(attribute.name).split_view('-');
- return !segments.is_empty() && segments.first() == attribute.value;
+ const auto element_attr_value = element.attribute(attribute.name);
+ if (element_attr_value.is_empty()) {
+ // If the attribute value on element is empty, the selector is true
+ // if the match value is also empty and false otherwise.
+ return attribute.value.is_empty();
+ }
+ if (attribute.value.is_empty()) {
+ return false;
+ }
+ auto segments = element_attr_value.split_view('-');
+ return segments.first() == attribute.value;
}
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
- return element.attribute(attribute.name).starts_with(attribute.value);
+ return !attribute.value.is_empty()
+ && element.attribute(attribute.name).starts_with(attribute.value);
case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
- return element.attribute(attribute.name).ends_with(attribute.value);
+ return !attribute.value.is_empty()
+ && element.attribute(attribute.name).ends_with(attribute.value);
}
return false;