Bug 1886377 - Fix substring attribute selectors for Servo. r=emilio

Substring attribute selectors should never match when an empty string is
provided. This fixes [attr^=""], [attr$=""], [attr*=""] and [attr~=""]
in Servo, which broke when D180529 removed never_matches.

Gecko doesn't use this code at all, so no change in behavior.

Differential Revision: https://phabricator.services.mozilla.com/D205187
This commit is contained in:
Oriol Brufau 2024-03-20 08:51:15 +00:00
Родитель f315309bdc
Коммит c9d0da9645
1 изменённых файлов: 11 добавлений и 6 удалений

Просмотреть файл

@ -111,16 +111,21 @@ impl AttrSelectorOperator {
let case = case_sensitivity;
match self {
AttrSelectorOperator::Equal => case.eq(e, s),
AttrSelectorOperator::Prefix => e.len() >= s.len() && case.eq(&e[..s.len()], s),
AttrSelectorOperator::Prefix => {
!s.is_empty() && e.len() >= s.len() && case.eq(&e[..s.len()], s)
},
AttrSelectorOperator::Suffix => {
e.len() >= s.len() && case.eq(&e[(e.len() - s.len())..], s)
!s.is_empty() && e.len() >= s.len() && case.eq(&e[(e.len() - s.len())..], s)
},
AttrSelectorOperator::Substring => {
case.contains(element_attr_value, attr_selector_value)
!s.is_empty() && case.contains(element_attr_value, attr_selector_value)
},
AttrSelectorOperator::Includes => {
!s.is_empty() &&
element_attr_value
.split(SELECTOR_WHITESPACE)
.any(|part| case.eq(part.as_bytes(), s))
},
AttrSelectorOperator::Includes => element_attr_value
.split(SELECTOR_WHITESPACE)
.any(|part| case.eq(part.as_bytes(), s)),
AttrSelectorOperator::DashMatch => {
case.eq(e, s) || (e.get(s.len()) == Some(&b'-') && case.eq(&e[..s.len()], s))
},