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/. */
5 //! Parsed representations of [DOM attributes][attr].
7 //! [attr]: https://dom.spec.whatwg.org/#interface-attr
9 use crate::properties::PropertyDeclarationBlock;
10 use crate::shared_lock::Locked;
11 use crate::str::str_join;
12 use crate::str::{read_exponent, read_fraction, HTML_SPACE_CHARACTERS};
13 use crate::str::{read_numbers, split_commas, split_html_space_chars};
14 use crate::values::specified::Length;
15 use crate::values::AtomString;
16 use crate::{Atom, LocalName, Namespace, Prefix};
18 use cssparser::{self, Color, RGBA};
19 use euclid::num::Zero;
20 use num_traits::ToPrimitive;
21 use selectors::attr::AttrSelectorOperation;
23 use servo_url::ServoUrl;
24 use std::str::FromStr;
26 // Duplicated from script::dom::values.
27 const UNSIGNED_LONG_MAX: u32 = 2147483647;
29 #[derive(Clone, Copy, Debug, PartialEq)]
30 #[cfg_attr(feature = "servo", derive(MallocSizeOf))]
31 pub enum LengthOrPercentageOrAuto {
37 #[derive(Clone, Debug)]
38 #[cfg_attr(feature = "servo", derive(MallocSizeOf))]
41 TokenList(String, Vec<Atom>),
46 Length(String, Option<Length>),
47 Color(String, Option<RGBA>),
48 Dimension(String, LengthOrPercentageOrAuto),
50 /// Stores a URL, computed from the input string and a document's base URL.
52 /// The URL is resolved at setting-time, so this kind of attribute value is
53 /// not actually suitable for most URL-reflecting IDL attributes.
54 ResolvedUrl(String, Option<ServoUrl>),
56 /// Note that this variant is only used transitively as a fast path to set
57 /// the property declaration block relevant to the style of an element when
58 /// set from the inline declaration of that element (that is,
61 /// This can, as of this writing, only correspond to the value of the
62 /// `style` element, and is set from its relevant CSSInlineStyleDeclaration,
63 /// and then converted to a string in Element::attribute_mutated.
65 /// Note that we don't necessarily need to do that (we could just clone the
66 /// declaration block), but that avoids keeping a refcounted
67 /// declarationblock for longer than needed.
70 #[ignore_malloc_size_of = "Arc"] Arc<Locked<PropertyDeclarationBlock>>,
74 /// Shared implementation to parse an integer according to
75 /// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers> or
76 /// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-negative-integers>
77 fn do_parse_integer<T: Iterator<Item = char>>(input: T) -> Result<i64, ()> {
79 .skip_while(|c| HTML_SPACE_CHARACTERS.iter().any(|s| s == c))
82 let sign = match input.peek() {
83 None => return Err(()),
95 let (value, _) = read_numbers(input);
97 value.and_then(|value| value.checked_mul(sign)).ok_or(())
100 /// Parse an integer according to
101 /// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers>.
102 pub fn parse_integer<T: Iterator<Item = char>>(input: T) -> Result<i32, ()> {
103 do_parse_integer(input).and_then(|result| result.to_i32().ok_or(()))
106 /// Parse an integer according to
107 /// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-negative-integers>
108 pub fn parse_unsigned_integer<T: Iterator<Item = char>>(input: T) -> Result<u32, ()> {
109 do_parse_integer(input).and_then(|result| result.to_u32().ok_or(()))
112 /// Parse a floating-point number according to
113 /// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-floating-point-number-values>
114 pub fn parse_double(string: &str) -> Result<f64, ()> {
115 let trimmed = string.trim_matches(HTML_SPACE_CHARACTERS);
116 let mut input = trimmed.chars().peekable();
118 let (value, divisor, chars_skipped) = match input.peek() {
119 None => return Err(()),
128 _ => (1f64, 1f64, 0),
131 let (value, value_digits) = if let Some(&'.') = input.peek() {
134 let (read_val, read_digits) = read_numbers(input);
136 value * read_val.and_then(|result| result.to_f64()).unwrap_or(1f64),
143 .skip(value_digits + chars_skipped)
146 let (mut value, fraction_digits) = read_fraction(input, divisor, value);
150 .skip(value_digits + chars_skipped + fraction_digits)
153 if let Some(exp) = read_exponent(input) {
154 value *= 10f64.powi(exp)
161 pub fn from_serialized_tokenlist(tokens: String) -> AttrValue {
163 split_html_space_chars(&tokens)
165 .fold(vec![], |mut acc, atom| {
166 if !acc.contains(&atom) {
171 AttrValue::TokenList(tokens, atoms)
174 pub fn from_comma_separated_tokenlist(tokens: String) -> AttrValue {
175 let atoms = split_commas(&tokens)
177 .fold(vec![], |mut acc, atom| {
178 if !acc.contains(&atom) {
183 AttrValue::TokenList(tokens, atoms)
186 pub fn from_atomic_tokens(atoms: Vec<Atom>) -> AttrValue {
187 // TODO(ajeffrey): effecient conversion of Vec<Atom> to String
188 let tokens = String::from(str_join(&atoms, "\x20"));
189 AttrValue::TokenList(tokens, atoms)
192 // https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-unsigned-long
193 pub fn from_u32(string: String, default: u32) -> AttrValue {
194 let result = parse_unsigned_integer(string.chars()).unwrap_or(default);
195 let result = if result > UNSIGNED_LONG_MAX {
200 AttrValue::UInt(string, result)
203 pub fn from_i32(string: String, default: i32) -> AttrValue {
204 let result = parse_integer(string.chars()).unwrap_or(default);
205 AttrValue::Int(string, result)
208 // https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-double
209 pub fn from_double(string: String, default: f64) -> AttrValue {
210 let result = parse_double(&string).unwrap_or(default);
212 if result.is_normal() {
213 AttrValue::Double(string, result)
215 AttrValue::Double(string, default)
219 // https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers
220 pub fn from_limited_i32(string: String, default: i32) -> AttrValue {
221 let result = parse_integer(string.chars()).unwrap_or(default);
224 AttrValue::Int(string, default)
226 AttrValue::Int(string, result)
230 // https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers-greater-than-zero
231 pub fn from_limited_u32(string: String, default: u32) -> AttrValue {
232 let result = parse_unsigned_integer(string.chars()).unwrap_or(default);
233 let result = if result == 0 || result > UNSIGNED_LONG_MAX {
238 AttrValue::UInt(string, result)
241 pub fn from_atomic(string: String) -> AttrValue {
242 let value = Atom::from(string);
243 AttrValue::Atom(value)
246 pub fn from_resolved_url(base: &ServoUrl, url: String) -> AttrValue {
247 let joined = base.join(&url).ok();
248 AttrValue::ResolvedUrl(url, joined)
251 pub fn from_legacy_color(string: String) -> AttrValue {
252 let parsed = parse_legacy_color(&string).ok();
253 AttrValue::Color(string, parsed)
256 pub fn from_dimension(string: String) -> AttrValue {
257 let parsed = parse_length(&string);
258 AttrValue::Dimension(string, parsed)
261 pub fn from_nonzero_dimension(string: String) -> AttrValue {
262 let parsed = parse_nonzero_length(&string);
263 AttrValue::Dimension(string, parsed)
266 /// Assumes the `AttrValue` is a `TokenList` and returns its tokens
270 /// Panics if the `AttrValue` is not a `TokenList`
271 pub fn as_tokens(&self) -> &[Atom] {
273 AttrValue::TokenList(_, ref tokens) => tokens,
274 _ => panic!("Tokens not found"),
278 /// Assumes the `AttrValue` is an `Atom` and returns its value
282 /// Panics if the `AttrValue` is not an `Atom`
283 pub fn as_atom(&self) -> &Atom {
285 AttrValue::Atom(ref value) => value,
286 _ => panic!("Atom not found"),
290 /// Assumes the `AttrValue` is a `Color` and returns its value
294 /// Panics if the `AttrValue` is not a `Color`
295 pub fn as_color(&self) -> Option<&RGBA> {
297 AttrValue::Color(_, ref color) => color.as_ref(),
298 _ => panic!("Color not found"),
302 /// Assumes the `AttrValue` is a `Dimension` and returns its value
306 /// Panics if the `AttrValue` is not a `Dimension`
307 pub fn as_dimension(&self) -> &LengthOrPercentageOrAuto {
309 AttrValue::Dimension(_, ref l) => l,
310 _ => panic!("Dimension not found"),
314 /// Assumes the `AttrValue` is a `ResolvedUrl` and returns its value.
318 /// Panics if the `AttrValue` is not a `ResolvedUrl`
319 pub fn as_resolved_url(&self) -> Option<&ServoUrl> {
321 AttrValue::ResolvedUrl(_, ref url) => url.as_ref(),
322 _ => panic!("Url not found"),
326 /// Return the AttrValue as its integer representation, if any.
327 /// This corresponds to attribute values returned as `AttrValue::UInt(_)`
328 /// by `VirtualMethods::parse_plain_attribute()`.
332 /// Panics if the `AttrValue` is not a `UInt`
333 pub fn as_uint(&self) -> u32 {
334 if let AttrValue::UInt(_, value) = *self {
337 panic!("Uint not found");
341 /// Return the AttrValue as a dimension computed from its integer
342 /// representation, assuming that integer representation specifies pixels.
344 /// This corresponds to attribute values returned as `AttrValue::UInt(_)`
345 /// by `VirtualMethods::parse_plain_attribute()`.
349 /// Panics if the `AttrValue` is not a `UInt`
350 pub fn as_uint_px_dimension(&self) -> LengthOrPercentageOrAuto {
351 if let AttrValue::UInt(_, value) = *self {
352 LengthOrPercentageOrAuto::Length(Au::from_px(value as i32))
354 panic!("Uint not found");
358 pub fn eval_selector(&self, selector: &AttrSelectorOperation<&AtomString>) -> bool {
359 // FIXME(SimonSapin) this can be more efficient by matching on `(self, selector)` variants
360 // and doing Atom comparisons instead of string comparisons where possible,
361 // with SelectorImpl::AttrValue changed to Atom.
362 selector.eval_str(self)
366 impl ::std::ops::Deref for AttrValue {
369 fn deref(&self) -> &str {
371 AttrValue::String(ref value) |
372 AttrValue::TokenList(ref value, _) |
373 AttrValue::UInt(ref value, _) |
374 AttrValue::Double(ref value, _) |
375 AttrValue::Length(ref value, _) |
376 AttrValue::Color(ref value, _) |
377 AttrValue::Int(ref value, _) |
378 AttrValue::ResolvedUrl(ref value, _) |
379 AttrValue::Declaration(ref value, _) |
380 AttrValue::Dimension(ref value, _) => &value,
381 AttrValue::Atom(ref value) => &value,
386 impl PartialEq<Atom> for AttrValue {
387 fn eq(&self, other: &Atom) -> bool {
389 AttrValue::Atom(ref value) => value == other,
390 _ => other == &**self,
395 /// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-zero-dimension-values>
396 pub fn parse_nonzero_length(value: &str) -> LengthOrPercentageOrAuto {
397 match parse_length(value) {
398 LengthOrPercentageOrAuto::Length(x) if x == Au::zero() => LengthOrPercentageOrAuto::Auto,
399 LengthOrPercentageOrAuto::Percentage(x) if x == 0. => LengthOrPercentageOrAuto::Auto,
404 /// Parses a [legacy color][color]. If unparseable, `Err` is returned.
406 /// [color]: https://html.spec.whatwg.org/multipage/#rules-for-parsing-a-legacy-colour-value
407 pub fn parse_legacy_color(mut input: &str) -> Result<RGBA, ()> {
409 if input.is_empty() {
414 input = input.trim_matches(HTML_SPACE_CHARACTERS);
417 if input.eq_ignore_ascii_case("transparent") {
422 if let Ok(Color::RGBA(rgba)) = cssparser::parse_color_keyword(input) {
427 if input.len() == 4 {
428 if let (b'#', Ok(r), Ok(g), Ok(b)) = (
430 hex(input.as_bytes()[1] as char),
431 hex(input.as_bytes()[2] as char),
432 hex(input.as_bytes()[3] as char),
434 return Ok(RGBA::new(r * 17, g * 17, b * 17, 255));
439 let mut new_input = String::new();
440 for ch in input.chars() {
441 if ch as u32 > 0xffff {
442 new_input.push_str("00")
447 let mut input = &*new_input;
450 for (char_count, (index, _)) in input.char_indices().enumerate() {
451 if char_count == 128 {
452 input = &input[..index];
458 if input.as_bytes()[0] == b'#' {
463 let mut new_input = Vec::new();
464 for ch in input.chars() {
466 new_input.push(ch as u8)
471 let mut input = new_input;
474 while input.is_empty() || (input.len() % 3) != 0 {
479 let mut length = input.len() / 3;
480 let (mut red, mut green, mut blue) = (
482 &input[length..length * 2],
483 &input[length * 2..],
488 red = &red[length - 8..];
489 green = &green[length - 8..];
490 blue = &blue[length - 8..];
495 while length > 2 && red[0] == b'0' && green[0] == b'0' && blue[0] == b'0' {
504 hex_string(red).unwrap(),
505 hex_string(green).unwrap(),
506 hex_string(blue).unwrap(),
510 fn hex(ch: char) -> Result<u8, ()> {
512 '0'..='9' => Ok((ch as u8) - b'0'),
513 'a'..='f' => Ok((ch as u8) - b'a' + 10),
514 'A'..='F' => Ok((ch as u8) - b'A' + 10),
519 fn hex_string(string: &[u8]) -> Result<u8, ()> {
522 1 => hex(string[0] as char),
524 let upper = hex(string[0] as char)?;
525 let lower = hex(string[1] as char)?;
526 Ok((upper << 4) | lower)
532 /// Parses a [dimension value][dim]. If unparseable, `Auto` is returned.
534 /// [dim]: https://html.spec.whatwg.org/multipage/#rules-for-parsing-dimension-values
535 // TODO: this function can be rewritten to return Result<LengthPercentage, _>
536 pub fn parse_length(mut value: &str) -> LengthOrPercentageOrAuto {
537 // Steps 1 & 2 are not relevant
540 value = value.trim_start_matches(HTML_SPACE_CHARACTERS);
543 match value.chars().nth(0) {
544 Some('0'..='9') => {},
545 _ => return LengthOrPercentageOrAuto::Auto,
549 // We trim the string length to the minimum of:
550 // 1. the end of the string
551 // 2. the first occurence of a '%' (U+0025 PERCENT SIGN)
552 // 3. the second occurrence of a '.' (U+002E FULL STOP)
553 // 4. the occurrence of a character that is neither a digit nor '%' nor '.'
554 // Note: Step 7.4 is directly subsumed by FromStr::from_str
555 let mut end_index = value.len();
556 let (mut found_full_stop, mut found_percent) = (false, false);
557 for (i, ch) in value.chars().enumerate() {
559 '0'..='9' => continue,
561 found_percent = true;
565 '.' if !found_full_stop => {
566 found_full_stop = true;
575 value = &value[..end_index];
578 let result: Result<f32, _> = FromStr::from_str(value);
580 Ok(number) => return LengthOrPercentageOrAuto::Percentage((number as f32) / 100.0),
581 Err(_) => return LengthOrPercentageOrAuto::Auto,
585 match FromStr::from_str(value) {
586 Ok(number) => LengthOrPercentageOrAuto::Length(Au::from_f64_px(number)),
587 Err(_) => LengthOrPercentageOrAuto::Auto,
591 /// A struct that uniquely identifies an element's attribute.
592 #[derive(Clone, Debug)]
593 #[cfg_attr(feature = "servo", derive(MallocSizeOf))]
594 pub struct AttrIdentifier {
595 pub local_name: LocalName,
597 pub namespace: Namespace,
598 pub prefix: Option<Prefix>,