summaryrefslogtreecommitdiffstats
path: root/servo/components/selectors/attr.rs
blob: fee296223772863e21400326c6849fa0391ece49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::parser::SelectorImpl;
use cssparser::ToCss;
use std::fmt;

#[derive(Clone, Eq, PartialEq, ToShmem)]
#[shmem(no_bounds)]
pub struct AttrSelectorWithOptionalNamespace<Impl: SelectorImpl> {
    #[shmem(field_bound)]
    pub namespace: Option<NamespaceConstraint<(Impl::NamespacePrefix, Impl::NamespaceUrl)>>,
    #[shmem(field_bound)]
    pub local_name: Impl::LocalName,
    pub local_name_lower: Impl::LocalName,
    #[shmem(field_bound)]
    pub operation: ParsedAttrSelectorOperation<Impl::AttrValue>,
}

impl<Impl: SelectorImpl> AttrSelectorWithOptionalNamespace<Impl> {
    pub fn namespace(&self) -> Option<NamespaceConstraint<&Impl::NamespaceUrl>> {
        self.namespace.as_ref().map(|ns| match ns {
            NamespaceConstraint::Any => NamespaceConstraint::Any,
            NamespaceConstraint::Specific((_, ref url)) => NamespaceConstraint::Specific(url),
        })
    }
}

#[derive(Clone, Eq, PartialEq, ToShmem)]
pub enum NamespaceConstraint<NamespaceUrl> {
    Any,

    /// Empty string for no namespace
    Specific(NamespaceUrl),
}

#[derive(Clone, Eq, PartialEq, ToShmem)]
pub enum ParsedAttrSelectorOperation<AttrValue> {
    Exists,
    WithValue {
        operator: AttrSelectorOperator,
        case_sensitivity: ParsedCaseSensitivity,
        value: AttrValue,
    },
}

#[derive(Clone, Eq, PartialEq)]
pub enum AttrSelectorOperation<AttrValue> {
    Exists,
    WithValue {
        operator: AttrSelectorOperator,
        case_sensitivity: CaseSensitivity,
        value: AttrValue,
    },
}

impl<AttrValue> AttrSelectorOperation<AttrValue> {
    pub fn eval_str(&self, element_attr_value: &str) -> bool
    where
        AttrValue: AsRef<str>,
    {
        match *self {
            AttrSelectorOperation::Exists => true,
            AttrSelectorOperation::WithValue {
                operator,
                case_sensitivity,
                ref value,
            } => operator.eval_str(element_attr_value, value.as_ref(), case_sensitivity),
        }
    }
}

#[derive(Clone, Copy, Eq, PartialEq, ToShmem)]
pub enum AttrSelectorOperator {
    Equal,
    Includes,
    DashMatch,
    Prefix,
    Substring,
    Suffix,
}

impl ToCss for AttrSelectorOperator {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
    where
        W: fmt::Write,
    {
        // https://drafts.csswg.org/cssom/#serializing-selectors
        // See "attribute selector".
        dest.write_str(match *self {
            AttrSelectorOperator::Equal => "=",
            AttrSelectorOperator::Includes => "~=",
            AttrSelectorOperator::DashMatch => "|=",
            AttrSelectorOperator::Prefix => "^=",
            AttrSelectorOperator::Substring => "*=",
            AttrSelectorOperator::Suffix => "$=",
        })
    }
}

impl AttrSelectorOperator {
    pub fn eval_str(
        self,
        element_attr_value: &str,
        attr_selector_value: &str,
        case_sensitivity: CaseSensitivity,
    ) -> bool {
        let e = element_attr_value.as_bytes();
        let s = attr_selector_value.as_bytes();
        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::Suffix => {
                e.len() >= s.len() && case.eq(&e[(e.len() - s.len())..], s)
            },
            AttrSelectorOperator::Substring => {
                case.contains(element_attr_value, attr_selector_value)
            },
            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))
            },
        }
    }
}

/// The definition of whitespace per CSS Selectors Level 3 § 4.
pub static SELECTOR_WHITESPACE: &[char] = &[' ', '\t', '\n', '\r', '\x0C'];

#[derive(Clone, Copy, Debug, Eq, PartialEq, ToShmem)]
pub enum ParsedCaseSensitivity {
    /// 's' was specified.
    ExplicitCaseSensitive,
    /// 'i' was specified.
    AsciiCaseInsensitive,
    /// No flags were specified and HTML says this is a case-sensitive attribute.
    CaseSensitive,
    /// No flags were specified and HTML says this is a case-insensitive attribute.
    AsciiCaseInsensitiveIfInHtmlElementInHtmlDocument,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CaseSensitivity {
    CaseSensitive,
    AsciiCaseInsensitive,
}

impl CaseSensitivity {
    pub fn eq(self, a: &[u8], b: &[u8]) -> bool {
        match self {
            CaseSensitivity::CaseSensitive => a == b,
            CaseSensitivity::AsciiCaseInsensitive => a.eq_ignore_ascii_case(b),
        }
    }

    pub fn contains(self, haystack: &str, needle: &str) -> bool {
        match self {
            CaseSensitivity::CaseSensitive => haystack.contains(needle),
            CaseSensitivity::AsciiCaseInsensitive => {
                if let Some((&n_first_byte, n_rest)) = needle.as_bytes().split_first() {
                    haystack.bytes().enumerate().any(|(i, byte)| {
                        if !byte.eq_ignore_ascii_case(&n_first_byte) {
                            return false;
                        }
                        let after_this_byte = &haystack.as_bytes()[i + 1..];
                        match after_this_byte.get(..n_rest.len()) {
                            None => false,
                            Some(haystack_slice) => haystack_slice.eq_ignore_ascii_case(n_rest),
                        }
                    })
                } else {
                    // any_str.contains("") == true,
                    // though these cases should be handled with *NeverMatches and never go here.
                    true
                }
            },
        }
    }
}