|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +use crate::parser::SelectorImpl; |
| 6 | +use cssparser::ToCss; |
| 7 | +use std::fmt; |
| 8 | + |
| 9 | +#[derive(Clone, Eq, PartialEq)] |
| 10 | +pub struct AttrSelectorWithOptionalNamespace<Impl: SelectorImpl> { |
| 11 | + pub namespace: Option<NamespaceConstraint<(Impl::NamespacePrefix, Impl::NamespaceUrl)>>, |
| 12 | + pub local_name: Impl::LocalName, |
| 13 | + pub local_name_lower: Impl::LocalName, |
| 14 | + pub operation: ParsedAttrSelectorOperation<Impl::AttrValue>, |
| 15 | + pub never_matches: bool, |
| 16 | +} |
| 17 | + |
| 18 | +impl<Impl: SelectorImpl> AttrSelectorWithOptionalNamespace<Impl> { |
| 19 | + pub fn namespace(&self) -> Option<NamespaceConstraint<&Impl::NamespaceUrl>> { |
| 20 | + self.namespace.as_ref().map(|ns| match ns { |
| 21 | + NamespaceConstraint::Any => NamespaceConstraint::Any, |
| 22 | + NamespaceConstraint::Specific((_, ref url)) => NamespaceConstraint::Specific(url), |
| 23 | + }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Clone, Eq, PartialEq)] |
| 28 | +pub enum NamespaceConstraint<NamespaceUrl> { |
| 29 | + Any, |
| 30 | + |
| 31 | + /// Empty string for no namespace |
| 32 | + Specific(NamespaceUrl), |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Clone, Eq, PartialEq)] |
| 36 | +pub enum ParsedAttrSelectorOperation<AttrValue> { |
| 37 | + Exists, |
| 38 | + WithValue { |
| 39 | + operator: AttrSelectorOperator, |
| 40 | + case_sensitivity: ParsedCaseSensitivity, |
| 41 | + expected_value: AttrValue, |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Clone, Eq, PartialEq)] |
| 46 | +pub enum AttrSelectorOperation<AttrValue> { |
| 47 | + Exists, |
| 48 | + WithValue { |
| 49 | + operator: AttrSelectorOperator, |
| 50 | + case_sensitivity: CaseSensitivity, |
| 51 | + expected_value: AttrValue, |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +impl<AttrValue> AttrSelectorOperation<AttrValue> { |
| 56 | + pub fn eval_str(&self, element_attr_value: &str) -> bool |
| 57 | + where |
| 58 | + AttrValue: AsRef<str>, |
| 59 | + { |
| 60 | + match *self { |
| 61 | + AttrSelectorOperation::Exists => true, |
| 62 | + AttrSelectorOperation::WithValue { |
| 63 | + operator, |
| 64 | + case_sensitivity, |
| 65 | + ref expected_value, |
| 66 | + } => operator.eval_str( |
| 67 | + element_attr_value, |
| 68 | + expected_value.as_ref(), |
| 69 | + case_sensitivity, |
| 70 | + ), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[derive(Clone, Copy, Eq, PartialEq)] |
| 76 | +pub enum AttrSelectorOperator { |
| 77 | + Equal, |
| 78 | + Includes, |
| 79 | + DashMatch, |
| 80 | + Prefix, |
| 81 | + Substring, |
| 82 | + Suffix, |
| 83 | +} |
| 84 | + |
| 85 | +impl ToCss for AttrSelectorOperator { |
| 86 | + fn to_css<W>(&self, dest: &mut W) -> fmt::Result |
| 87 | + where |
| 88 | + W: fmt::Write, |
| 89 | + { |
| 90 | + // https://drafts.csswg.org/cssom/#serializing-selectors |
| 91 | + // See "attribute selector". |
| 92 | + dest.write_str(match *self { |
| 93 | + AttrSelectorOperator::Equal => "=", |
| 94 | + AttrSelectorOperator::Includes => "~=", |
| 95 | + AttrSelectorOperator::DashMatch => "|=", |
| 96 | + AttrSelectorOperator::Prefix => "^=", |
| 97 | + AttrSelectorOperator::Substring => "*=", |
| 98 | + AttrSelectorOperator::Suffix => "$=", |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl AttrSelectorOperator { |
| 104 | + pub fn eval_str( |
| 105 | + self, |
| 106 | + element_attr_value: &str, |
| 107 | + attr_selector_value: &str, |
| 108 | + case_sensitivity: CaseSensitivity, |
| 109 | + ) -> bool { |
| 110 | + let e = element_attr_value.as_bytes(); |
| 111 | + let s = attr_selector_value.as_bytes(); |
| 112 | + let case = case_sensitivity; |
| 113 | + match self { |
| 114 | + AttrSelectorOperator::Equal => case.eq(e, s), |
| 115 | + AttrSelectorOperator::Prefix => e.len() >= s.len() && case.eq(&e[..s.len()], s), |
| 116 | + AttrSelectorOperator::Suffix => { |
| 117 | + e.len() >= s.len() && case.eq(&e[(e.len() - s.len())..], s) |
| 118 | + }, |
| 119 | + AttrSelectorOperator::Substring => { |
| 120 | + case.contains(element_attr_value, attr_selector_value) |
| 121 | + }, |
| 122 | + AttrSelectorOperator::Includes => element_attr_value |
| 123 | + .split(SELECTOR_WHITESPACE) |
| 124 | + .any(|part| case.eq(part.as_bytes(), s)), |
| 125 | + AttrSelectorOperator::DashMatch => { |
| 126 | + case.eq(e, s) || (e.get(s.len()) == Some(&b'-') && case.eq(&e[..s.len()], s)) |
| 127 | + }, |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// The definition of whitespace per CSS Selectors Level 3 § 4. |
| 133 | +pub static SELECTOR_WHITESPACE: &[char] = &[' ', '\t', '\n', '\r', '\x0C']; |
| 134 | + |
| 135 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 136 | +pub enum ParsedCaseSensitivity { |
| 137 | + // 's' was specified. |
| 138 | + ExplicitCaseSensitive, |
| 139 | + // 'i' was specified. |
| 140 | + AsciiCaseInsensitive, |
| 141 | + // No flags were specified and HTML says this is a case-sensitive attribute. |
| 142 | + CaseSensitive, |
| 143 | + // No flags were specified and HTML says this is a case-insensitive attribute. |
| 144 | + AsciiCaseInsensitiveIfInHtmlElementInHtmlDocument, |
| 145 | +} |
| 146 | + |
| 147 | +impl ParsedCaseSensitivity { |
| 148 | + pub fn to_unconditional(self, is_html_element_in_html_document: bool) -> CaseSensitivity { |
| 149 | + match self { |
| 150 | + ParsedCaseSensitivity::AsciiCaseInsensitiveIfInHtmlElementInHtmlDocument |
| 151 | + if is_html_element_in_html_document => |
| 152 | + { |
| 153 | + CaseSensitivity::AsciiCaseInsensitive |
| 154 | + }, |
| 155 | + ParsedCaseSensitivity::AsciiCaseInsensitiveIfInHtmlElementInHtmlDocument => { |
| 156 | + CaseSensitivity::CaseSensitive |
| 157 | + }, |
| 158 | + ParsedCaseSensitivity::CaseSensitive | ParsedCaseSensitivity::ExplicitCaseSensitive => { |
| 159 | + CaseSensitivity::CaseSensitive |
| 160 | + }, |
| 161 | + ParsedCaseSensitivity::AsciiCaseInsensitive => CaseSensitivity::AsciiCaseInsensitive, |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 167 | +pub enum CaseSensitivity { |
| 168 | + CaseSensitive, |
| 169 | + AsciiCaseInsensitive, |
| 170 | +} |
| 171 | + |
| 172 | +impl CaseSensitivity { |
| 173 | + pub fn eq(self, a: &[u8], b: &[u8]) -> bool { |
| 174 | + match self { |
| 175 | + CaseSensitivity::CaseSensitive => a == b, |
| 176 | + CaseSensitivity::AsciiCaseInsensitive => a.eq_ignore_ascii_case(b), |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + pub fn contains(self, haystack: &str, needle: &str) -> bool { |
| 181 | + match self { |
| 182 | + CaseSensitivity::CaseSensitive => haystack.contains(needle), |
| 183 | + CaseSensitivity::AsciiCaseInsensitive => { |
| 184 | + if let Some((&n_first_byte, n_rest)) = needle.as_bytes().split_first() { |
| 185 | + haystack.bytes().enumerate().any(|(i, byte)| { |
| 186 | + if !byte.eq_ignore_ascii_case(&n_first_byte) { |
| 187 | + return false; |
| 188 | + } |
| 189 | + let after_this_byte = &haystack.as_bytes()[i + 1..]; |
| 190 | + match after_this_byte.get(..n_rest.len()) { |
| 191 | + None => false, |
| 192 | + Some(haystack_slice) => haystack_slice.eq_ignore_ascii_case(n_rest), |
| 193 | + } |
| 194 | + }) |
| 195 | + } else { |
| 196 | + // any_str.contains("") == true, |
| 197 | + // though these cases should be handled with *NeverMatches and never go here. |
| 198 | + true |
| 199 | + } |
| 200 | + }, |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments