1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 16:48:12 +00:00

LibWeb: Fix issues with CSS attribute selector handling

This is three small, related changes:

1. Element::has_attribute() now returns true if the attribute exists but
has no value. (eg, `<div foo />` -> `has_attribute("foo")`)

2. SelectorEngine::matches_attribute() now makes sure there is a first
segment before comparing it, fixing a crash.

3. CSS::Parser now converts attribute names in attribute selectors to
lowercase, to match the expectations of the rest of the system.
Converting to lowercase is not always correct, depending on language,
but since we only currently support HTML, and that expects them to be
case-insensitive, it is fine for now.
This commit is contained in:
Sam Atkins 2021-07-28 12:34:05 +01:00 committed by Andreas Kling
parent 242c342fad
commit 1b72766e4e
3 changed files with 11 additions and 4 deletions

View file

@ -40,9 +40,11 @@ static bool matches_attribute(CSS::Selector::SimpleSelector::Attribute const& at
case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
return element.attribute(attribute.name).contains(attribute.value);
break;
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
return element.attribute(attribute.name).split_view('-').first() == 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;
break;
}
case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
return element.attribute(attribute.name).starts_with(attribute.value);
break;