Auto merge of #7683 - frewsxcv:html-font-element-size-attr, r=nox

Implement `size` attribute for <font> element



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7683)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-10-05 02:33:02 -06:00
commit 7debfd1f4c
12 changed files with 220 additions and 181 deletions

View file

@ -9,7 +9,7 @@ use num_lib::ToPrimitive;
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::ffi::CStr;
use std::iter::Filter;
use std::iter::{Filter, Peekable};
use std::ops::Deref;
use std::str::{FromStr, Split, from_utf8};
@ -47,17 +47,37 @@ pub fn split_html_space_chars<'a>(s: &'a str) ->
s.split(HTML_SPACE_CHARACTERS).filter(not_empty as fn(&&str) -> bool)
}
fn is_ascii_digit(c: &char) -> bool {
match *c {
'0'...'9' => true,
_ => false,
}
}
fn read_numbers<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> Option<i64> {
match iter.peek() {
Some(c) if is_ascii_digit(c) => (),
_ => return None,
}
iter.take_while(is_ascii_digit).map(|d| {
d as i64 - '0' as i64
}).fold(Some(0i64), |accumulator, d| {
accumulator.and_then(|accumulator| {
accumulator.checked_mul(10)
}).and_then(|accumulator| {
accumulator.checked_add(d)
})
})
}
/// Shared implementation to parse an integer according to
/// <https://html.spec.whatwg.org/#rules-for-parsing-integers> or
/// <https://html.spec.whatwg.org/#rules-for-parsing-non-negative-integers>
fn do_parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i64> {
fn is_ascii_digit(c: &char) -> bool {
match *c {
'0'...'9' => true,
_ => false,
}
}
let mut input = input.skip_while(|c| {
HTML_SPACE_CHARACTERS.iter().any(|s| s == c)
}).peekable();
@ -75,20 +95,7 @@ fn do_parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i64> {
Some(_) => 1,
};
match input.peek() {
Some(c) if is_ascii_digit(c) => (),
_ => return None,
}
let value = input.take_while(is_ascii_digit).map(|d| {
d as i64 - '0' as i64
}).fold(Some(0i64), |accumulator, d| {
accumulator.and_then(|accumulator| {
accumulator.checked_mul(10)
}).and_then(|accumulator| {
accumulator.checked_add(d)
})
});
let value = read_numbers(input);
return value.and_then(|value| value.checked_mul(sign));
}
@ -166,6 +173,61 @@ pub fn parse_length(mut value: &str) -> LengthOrPercentageOrAuto {
}
}
/// https://html.spec.whatwg.org/multipage/#rules-for-parsing-a-legacy-font-size
pub fn parse_legacy_font_size(mut input: &str) -> Option<&'static str> {
// Steps 1 & 2 are not relevant
// Step 3
input = input.trim_matches(WHITESPACE);
enum ParseMode {
RelativePlus,
RelativeMinus,
Absolute,
}
let mut input_chars = input.chars().peekable();
let parse_mode = match input_chars.peek() {
// Step 4
None => return None,
// Step 5
Some(&'+') => {
let _ = input_chars.next(); // consume the '+'
ParseMode::RelativePlus
}
Some(&'-') => {
let _ = input_chars.next(); // consume the '-'
ParseMode::RelativeMinus
}
Some(_) => ParseMode::Absolute,
};
// Steps 6, 7, 8
let mut value = match read_numbers(input_chars) {
Some(v) => v,
None => return None,
};
// Step 9
match parse_mode {
ParseMode::RelativePlus => value = 3 + value,
ParseMode::RelativeMinus => value = 3 - value,
ParseMode::Absolute => (),
}
// Steps 10, 11, 12
Some(match value {
n if n >= 7 => "xxx-large",
6 => "xx-large",
5 => "x-large",
4 => "large",
3 => "medium",
2 => "small",
n if n <= 1 => "x-small",
_ => unreachable!(),
})
}
/// 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.