2014-02-14 16:25:58 +04:00
|
|
|
/* 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 http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2014-10-15 00:06:36 +04:00
|
|
|
use geometry::Au;
|
|
|
|
|
2014-12-18 04:45:49 +03:00
|
|
|
use cssparser::{mod, RGBA, Color};
|
2014-12-16 05:33:46 +03:00
|
|
|
use std::ascii::AsciiExt;
|
2014-04-05 12:04:34 +04:00
|
|
|
use std::iter::Filter;
|
2014-12-18 04:45:49 +03:00
|
|
|
use std::num::Int;
|
|
|
|
use std::str::{CharEq, CharSplits, FromStr};
|
2014-11-04 04:30:35 +03:00
|
|
|
use unicode::char::to_lowercase;
|
2014-04-05 12:04:34 +04:00
|
|
|
|
2014-06-05 21:58:44 +04:00
|
|
|
pub type DOMString = String;
|
2014-04-05 12:04:34 +04:00
|
|
|
pub type StaticCharVec = &'static [char];
|
2014-03-26 19:46:47 +04:00
|
|
|
pub type StaticStringVec = &'static [&'static str];
|
2014-02-14 16:25:58 +04:00
|
|
|
|
|
|
|
pub fn null_str_as_empty(s: &Option<DOMString>) -> DOMString {
|
2014-06-08 20:46:31 +04:00
|
|
|
// We don't use map_default because it would allocate "".to_string() even for Some.
|
2014-02-14 16:25:58 +04:00
|
|
|
match *s {
|
|
|
|
Some(ref s) => s.clone(),
|
2014-06-05 21:58:44 +04:00
|
|
|
None => "".to_string()
|
2014-02-14 16:25:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn null_str_as_empty_ref<'a>(s: &'a Option<DOMString>) -> &'a str {
|
|
|
|
match *s {
|
|
|
|
Some(ref s) => s.as_slice(),
|
2014-05-23 04:14:24 +04:00
|
|
|
None => ""
|
2014-02-14 16:25:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:06:36 +04:00
|
|
|
/// Whitespace as defined by HTML5 § 2.4.1.
|
|
|
|
struct Whitespace;
|
|
|
|
|
|
|
|
impl CharEq for Whitespace {
|
|
|
|
#[inline]
|
|
|
|
fn matches(&mut self, ch: char) -> bool {
|
|
|
|
match ch {
|
|
|
|
' ' | '\t' | '\x0a' | '\x0c' | '\x0d' => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn only_ascii(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 17:29:03 +04:00
|
|
|
pub fn is_whitespace(s: &str) -> bool {
|
2014-10-15 00:06:36 +04:00
|
|
|
s.chars().all(|c| Whitespace.matches(c))
|
2014-02-21 17:29:03 +04:00
|
|
|
}
|
2014-03-14 18:16:57 +04:00
|
|
|
|
|
|
|
/// A "space character" according to:
|
|
|
|
///
|
2014-09-18 04:05:29 +04:00
|
|
|
/// http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#space-character
|
2014-04-05 12:04:34 +04:00
|
|
|
pub static HTML_SPACE_CHARACTERS: StaticCharVec = &[
|
2014-03-14 18:16:57 +04:00
|
|
|
'\u0020',
|
|
|
|
'\u0009',
|
|
|
|
'\u000a',
|
|
|
|
'\u000c',
|
|
|
|
'\u000d',
|
|
|
|
];
|
|
|
|
|
2014-12-16 05:33:46 +03:00
|
|
|
pub fn split_html_space_chars<'a>(s: &'a str)
|
|
|
|
-> Filter<'a, &'a str, CharSplits<'a, StaticCharVec>> {
|
2014-04-05 12:04:34 +04:00
|
|
|
s.split(HTML_SPACE_CHARACTERS).filter(|&split| !split.is_empty())
|
|
|
|
}
|
2014-09-04 11:20:54 +04:00
|
|
|
|
|
|
|
/// Shared implementation to parse an integer according to
|
|
|
|
/// <http://www.whatwg.org/html/#rules-for-parsing-integers> or
|
|
|
|
/// <http://www.whatwg.org/html/#rules-for-parsing-non-negative-integers>.
|
|
|
|
fn do_parse_integer<T: Iterator<char>>(input: T) -> Option<i64> {
|
2014-09-07 17:11:41 +04:00
|
|
|
fn is_ascii_digit(c: &char) -> bool {
|
|
|
|
match *c {
|
2014-11-13 06:48:31 +03:00
|
|
|
'0'...'9' => true,
|
2014-09-07 17:11:41 +04:00
|
|
|
_ => false,
|
2014-09-04 11:20:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut input = input.skip_while(|c| {
|
|
|
|
HTML_SPACE_CHARACTERS.iter().any(|s| s == c)
|
|
|
|
}).peekable();
|
|
|
|
|
|
|
|
let sign = match input.peek() {
|
|
|
|
None => return None,
|
|
|
|
Some(&'-') => {
|
|
|
|
input.next();
|
|
|
|
-1
|
|
|
|
},
|
|
|
|
Some(&'+') => {
|
|
|
|
input.next();
|
|
|
|
1
|
|
|
|
},
|
|
|
|
Some(_) => 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
match input.peek() {
|
2014-09-07 17:11:41 +04:00
|
|
|
Some(c) if is_ascii_digit(c) => (),
|
2014-09-04 11:20:54 +04:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
|
2014-09-07 17:11:41 +04:00
|
|
|
let value = input.take_while(is_ascii_digit).map(|d| {
|
|
|
|
d as i64 - '0' as i64
|
|
|
|
}).fold(Some(0i64), |accumulator, d| {
|
2014-09-04 11:20:54 +04:00
|
|
|
accumulator.and_then(|accumulator| {
|
2014-12-18 04:45:49 +03:00
|
|
|
accumulator.checked_mul(10)
|
2014-09-04 11:20:54 +04:00
|
|
|
}).and_then(|accumulator| {
|
2014-12-18 04:45:49 +03:00
|
|
|
accumulator.checked_add(d)
|
2014-09-04 11:20:54 +04:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2014-12-18 04:45:49 +03:00
|
|
|
return value.and_then(|value| value.checked_mul(sign));
|
2014-09-04 11:20:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an integer according to
|
|
|
|
/// <http://www.whatwg.org/html/#rules-for-parsing-integers>.
|
|
|
|
pub fn parse_integer<T: Iterator<char>>(input: T) -> Option<i32> {
|
|
|
|
do_parse_integer(input).and_then(|result| {
|
|
|
|
result.to_i32()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an integer according to
|
|
|
|
/// <http://www.whatwg.org/html/#rules-for-parsing-non-negative-integers>.
|
|
|
|
pub fn parse_unsigned_integer<T: Iterator<char>>(input: T) -> Option<u32> {
|
|
|
|
do_parse_integer(input).and_then(|result| {
|
|
|
|
result.to_u32()
|
|
|
|
})
|
|
|
|
}
|
2014-10-15 00:06:36 +04:00
|
|
|
|
|
|
|
pub enum LengthOrPercentageOrAuto {
|
2014-12-18 04:45:49 +03:00
|
|
|
Auto,
|
|
|
|
Percentage(f64),
|
|
|
|
Length(Au),
|
2014-10-15 00:06:36 +04:00
|
|
|
}
|
|
|
|
|
2014-12-18 04:45:49 +03:00
|
|
|
/// Parses a length per HTML5 § 2.4.4.4. If unparseable, `Auto` is returned.
|
2014-10-15 00:06:36 +04:00
|
|
|
pub fn parse_length(mut value: &str) -> LengthOrPercentageOrAuto {
|
|
|
|
value = value.trim_left_chars(Whitespace);
|
|
|
|
if value.len() == 0 {
|
2014-12-18 04:45:49 +03:00
|
|
|
return LengthOrPercentageOrAuto::Auto
|
2014-10-15 00:06:36 +04:00
|
|
|
}
|
|
|
|
if value.starts_with("+") {
|
|
|
|
value = value.slice_from(1)
|
|
|
|
}
|
|
|
|
value = value.trim_left_chars('0');
|
|
|
|
if value.len() == 0 {
|
2014-12-18 04:45:49 +03:00
|
|
|
return LengthOrPercentageOrAuto::Auto
|
2014-10-15 00:06:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut end_index = value.len();
|
|
|
|
let (mut found_full_stop, mut found_percent) = (false, false);
|
|
|
|
for (i, ch) in value.chars().enumerate() {
|
|
|
|
match ch {
|
2014-11-13 06:48:31 +03:00
|
|
|
'0'...'9' => continue,
|
2014-10-15 00:06:36 +04:00
|
|
|
'%' => {
|
|
|
|
found_percent = true;
|
|
|
|
end_index = i;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'.' if !found_full_stop => {
|
|
|
|
found_full_stop = true;
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
end_index = i;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = value.slice_to(end_index);
|
|
|
|
|
|
|
|
if found_percent {
|
|
|
|
let result: Option<f64> = FromStr::from_str(value);
|
|
|
|
match result {
|
2014-12-18 04:45:49 +03:00
|
|
|
Some(number) => return LengthOrPercentageOrAuto::Percentage((number as f64) / 100.0),
|
|
|
|
None => return LengthOrPercentageOrAuto::Auto,
|
2014-10-15 00:06:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match FromStr::from_str(value) {
|
2014-12-18 04:45:49 +03:00
|
|
|
Some(number) => LengthOrPercentageOrAuto::Length(Au::from_px(number)),
|
|
|
|
None => LengthOrPercentageOrAuto::Auto,
|
2014-10-15 00:06:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 05:33:46 +03:00
|
|
|
/// Parses a legacy color per HTML5 § 2.4.6. If unparseable, `Err` is returned.
|
|
|
|
pub fn parse_legacy_color(mut input: &str) -> Result<RGBA,()> {
|
|
|
|
// Steps 1 and 2.
|
|
|
|
if input.len() == 0 {
|
|
|
|
return Err(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3.
|
|
|
|
input = input.trim_left_chars(Whitespace).trim_right_chars(Whitespace);
|
|
|
|
|
|
|
|
// Step 4.
|
|
|
|
if input.eq_ignore_ascii_case("transparent") {
|
|
|
|
return Err(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 5.
|
|
|
|
match cssparser::parse_color_keyword(input) {
|
2014-12-18 04:45:49 +03:00
|
|
|
Ok(Color::RGBA(rgba)) => return Ok(rgba),
|
2014-12-16 05:33:46 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 6.
|
|
|
|
if input.len() == 4 {
|
|
|
|
match (input.as_bytes()[0],
|
|
|
|
hex(input.as_bytes()[1] as char),
|
|
|
|
hex(input.as_bytes()[2] as char),
|
|
|
|
hex(input.as_bytes()[3] as char)) {
|
|
|
|
(b'#', Ok(r), Ok(g), Ok(b)) => {
|
|
|
|
return Ok(RGBA {
|
|
|
|
red: (r as f32) * 17.0 / 255.0,
|
|
|
|
green: (g as f32) * 17.0 / 255.0,
|
|
|
|
blue: (b as f32) * 17.0 / 255.0,
|
|
|
|
alpha: 1.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 7.
|
|
|
|
let mut new_input = String::new();
|
|
|
|
for ch in input.chars() {
|
|
|
|
if ch as u32 > 0xffff {
|
|
|
|
new_input.push_str("00")
|
|
|
|
} else {
|
|
|
|
new_input.push(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut input = new_input.as_slice();
|
|
|
|
|
|
|
|
// Step 8.
|
|
|
|
for (char_count, (index, _)) in input.char_indices().enumerate() {
|
|
|
|
if char_count == 128 {
|
|
|
|
input = input.slice_to(index);
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 9.
|
|
|
|
if input.char_at(0) == '#' {
|
|
|
|
input = input.slice_from(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 10.
|
|
|
|
let mut new_input = Vec::new();
|
|
|
|
for ch in input.chars() {
|
|
|
|
if hex(ch).is_ok() {
|
|
|
|
new_input.push(ch as u8)
|
|
|
|
} else {
|
|
|
|
new_input.push(b'0')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut input = new_input;
|
|
|
|
|
|
|
|
// Step 11.
|
|
|
|
while input.len() == 0 || (input.len() % 3) != 0 {
|
|
|
|
input.push(b'0')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 12.
|
|
|
|
let mut length = input.len() / 3;
|
|
|
|
let (mut red, mut green, mut blue) = (input.slice_to(length),
|
|
|
|
input.slice(length, length * 2),
|
|
|
|
input.slice_from(length * 2));
|
|
|
|
|
|
|
|
// Step 13.
|
|
|
|
if length > 8 {
|
|
|
|
red = red.slice_from(length - 8);
|
|
|
|
green = green.slice_from(length - 8);
|
|
|
|
blue = blue.slice_from(length - 8);
|
|
|
|
length = 8
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 14.
|
|
|
|
while length > 2 && red[0] == b'0' && green[0] == b'0' && blue[0] == b'0' {
|
|
|
|
red = red.slice_from(1);
|
|
|
|
green = green.slice_from(1);
|
|
|
|
blue = blue.slice_from(1);
|
|
|
|
length -= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Steps 15-20.
|
|
|
|
return Ok(RGBA {
|
|
|
|
red: hex_string(red).unwrap() as f32 / 255.0,
|
|
|
|
green: hex_string(green).unwrap() as f32 / 255.0,
|
|
|
|
blue: hex_string(blue).unwrap() as f32 / 255.0,
|
|
|
|
alpha: 1.0,
|
|
|
|
});
|
|
|
|
|
|
|
|
fn hex(ch: char) -> Result<u8,()> {
|
|
|
|
match ch {
|
|
|
|
'0'...'9' => Ok((ch as u8) - b'0'),
|
|
|
|
'a'...'f' => Ok((ch as u8) - b'a' + 10),
|
|
|
|
'A'...'F' => Ok((ch as u8) - b'A' + 10),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hex_string(string: &[u8]) -> Result<u8,()> {
|
|
|
|
match string.len() {
|
|
|
|
0 => Err(()),
|
|
|
|
1 => hex(string[0] as char),
|
|
|
|
_ => {
|
|
|
|
let upper = try!(hex(string[0] as char));
|
|
|
|
let lower = try!(hex(string[1] as char));
|
|
|
|
Ok((upper << 4) | lower)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:06:36 +04:00
|
|
|
|
2014-11-04 04:30:35 +03:00
|
|
|
#[deriving(Clone, Eq, PartialEq, Hash, Show)]
|
|
|
|
pub struct LowercaseString {
|
|
|
|
inner: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LowercaseString {
|
|
|
|
pub fn new(s: &str) -> LowercaseString {
|
|
|
|
LowercaseString {
|
|
|
|
inner: s.chars().map(to_lowercase).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Str for LowercaseString {
|
|
|
|
#[inline]
|
|
|
|
fn as_slice(&self) -> &str {
|
|
|
|
self.inner.as_slice()
|
|
|
|
}
|
|
|
|
}
|