mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
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:
commit
7debfd1f4c
12 changed files with 220 additions and 181 deletions
|
@ -18,6 +18,7 @@ use std::cell::Ref;
|
|||
use std::mem;
|
||||
use std::ops::Deref;
|
||||
use string_cache::{Atom, Namespace};
|
||||
use style::values::specified::Length;
|
||||
use util::str::{DOMString, parse_unsigned_integer, split_html_space_chars, str_join};
|
||||
|
||||
#[derive(JSTraceable, PartialEq, Clone, HeapSizeOf)]
|
||||
|
@ -26,6 +27,7 @@ pub enum AttrValue {
|
|||
TokenList(DOMString, Vec<Atom>),
|
||||
UInt(DOMString, u32),
|
||||
Atom(Atom),
|
||||
Length(DOMString, Option<Length>),
|
||||
}
|
||||
|
||||
impl AttrValue {
|
||||
|
@ -72,6 +74,11 @@ impl AttrValue {
|
|||
AttrValue::Atom(value)
|
||||
}
|
||||
|
||||
/// Assumes the `AttrValue` is a `TokenList` and returns its tokens
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if the `AttrValue` is not a `TokenList`
|
||||
pub fn as_tokens(&self) -> &[Atom] {
|
||||
match *self {
|
||||
AttrValue::TokenList(_, ref tokens) => tokens,
|
||||
|
@ -79,6 +86,11 @@ impl AttrValue {
|
|||
}
|
||||
}
|
||||
|
||||
/// Assumes the `AttrValue` is an `Atom` and returns its value
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if the `AttrValue` is not an `Atom`
|
||||
pub fn as_atom(&self) -> &Atom {
|
||||
match *self {
|
||||
AttrValue::Atom(ref value) => value,
|
||||
|
@ -86,9 +98,25 @@ impl AttrValue {
|
|||
}
|
||||
}
|
||||
|
||||
/// Assumes the `AttrValue` is a `Length` and returns its value
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if the `AttrValue` is not a `Length`
|
||||
pub fn as_length(&self) -> Option<&Length> {
|
||||
match *self {
|
||||
AttrValue::Length(_, ref length) => length.as_ref(),
|
||||
_ => panic!("Length not found"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the AttrValue as its integer representation, if any.
|
||||
/// This corresponds to attribute values returned as `AttrValue::UInt(_)`
|
||||
/// by `VirtualMethods::parse_plain_attribute()`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if the `AttrValue` is not a `UInt`
|
||||
pub fn as_uint(&self) -> u32 {
|
||||
if let AttrValue::UInt(_, value) = *self {
|
||||
value
|
||||
|
@ -105,7 +133,8 @@ impl Deref for AttrValue {
|
|||
match *self {
|
||||
AttrValue::String(ref value) |
|
||||
AttrValue::TokenList(ref value, _) |
|
||||
AttrValue::UInt(ref value, _) => &value,
|
||||
AttrValue::UInt(ref value, _) |
|
||||
AttrValue::Length(ref value, _) => &value,
|
||||
AttrValue::Atom(ref value) => &value,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,6 +77,7 @@ use std::sync::Arc;
|
|||
use std::sync::mpsc::{Receiver, Sender};
|
||||
use string_cache::{Atom, Namespace};
|
||||
use style::properties::PropertyDeclarationBlock;
|
||||
use style::values::specified::Length;
|
||||
use url::Url;
|
||||
use util::str::{LengthOrPercentageOrAuto};
|
||||
|
||||
|
@ -301,6 +302,7 @@ no_jsmanaged_fields!(WebGLError);
|
|||
no_jsmanaged_fields!(TimeProfilerChan);
|
||||
no_jsmanaged_fields!(MemProfilerChan);
|
||||
no_jsmanaged_fields!(PseudoElement);
|
||||
no_jsmanaged_fields!(Length);
|
||||
|
||||
impl JSTraceable for Box<ScriptChan + Send> {
|
||||
#[inline]
|
||||
|
|
|
@ -71,7 +71,7 @@ use std::sync::Arc;
|
|||
use string_cache::{Atom, Namespace, QualName};
|
||||
use style::legacy::{UnsignedIntegerAttribute, from_declaration};
|
||||
use style::properties::DeclaredValue;
|
||||
use style::properties::longhands::{self, background_image, border_spacing, font_family};
|
||||
use style::properties::longhands::{self, background_image, border_spacing, font_family, font_size};
|
||||
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, parse_style_attribute};
|
||||
use style::values::CSSFloat;
|
||||
use style::values::specified::{self, CSSColor, CSSRGBA};
|
||||
|
@ -302,6 +302,19 @@ impl RawLayoutElementHelpers for Element {
|
|||
font_family)])))));
|
||||
}
|
||||
|
||||
let font_size = if let Some(this) = HTMLFontElementCast::to_ref(self) {
|
||||
this.get_size()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(font_size) = font_size {
|
||||
hints.push(from_declaration(
|
||||
PropertyDeclaration::FontSize(
|
||||
DeclaredValue::Value(
|
||||
font_size::SpecifiedValue(font_size)))))
|
||||
}
|
||||
|
||||
let cellspacing = if let Some(this) = HTMLTableElementCast::to_ref(self) {
|
||||
this.get_cellspacing()
|
||||
} else {
|
||||
|
|
|
@ -7,17 +7,18 @@ use dom::attr::{Attr, AttrValue};
|
|||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::HTMLFontElementBinding;
|
||||
use dom::bindings::codegen::Bindings::HTMLFontElementBinding::HTMLFontElementMethods;
|
||||
use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLFontElementDerived};
|
||||
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, HTMLFontElementDerived};
|
||||
use dom::bindings::js::Root;
|
||||
use dom::document::Document;
|
||||
use dom::element::{AttributeMutation, ElementTypeId};
|
||||
use dom::element::{AttributeMutation, ElementTypeId, RawLayoutElementHelpers};
|
||||
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
||||
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
||||
use dom::node::{Node, NodeTypeId};
|
||||
use dom::virtualmethods::VirtualMethods;
|
||||
use std::cell::Cell;
|
||||
use string_cache::Atom;
|
||||
use util::str::{self, DOMString};
|
||||
use style::values::specified;
|
||||
use util::str::{self, DOMString, parse_legacy_font_size};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct HTMLFontElement {
|
||||
|
@ -64,6 +65,12 @@ impl HTMLFontElementMethods for HTMLFontElement {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-font-face
|
||||
make_atomic_setter!(SetFace, "face");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-font-size
|
||||
make_getter!(Size);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-font-size
|
||||
make_setter!(SetSize, "size");
|
||||
}
|
||||
|
||||
impl VirtualMethods for HTMLFontElement {
|
||||
|
@ -92,6 +99,10 @@ impl VirtualMethods for HTMLFontElement {
|
|||
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
|
||||
match name {
|
||||
&atom!("face") => AttrValue::from_atomic(value),
|
||||
&atom!("size") => {
|
||||
let length = parse_legacy_font_size(&value).and_then(|parsed| specified::Length::from_str(&parsed));
|
||||
AttrValue::Length(value, length)
|
||||
},
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
|
||||
}
|
||||
}
|
||||
|
@ -111,4 +122,14 @@ impl HTMLFontElement {
|
|||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn get_size(&self) -> Option<specified::Length> {
|
||||
unsafe {
|
||||
ElementCast::from_ref(self)
|
||||
.get_attr_for_layout(&ns!(""), &atom!("size"))
|
||||
.and_then(AttrValue::as_length)
|
||||
.cloned()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,5 +7,5 @@
|
|||
interface HTMLFontElement : HTMLElement {
|
||||
[TreatNullAs=EmptyString] attribute DOMString color;
|
||||
attribute DOMString face;
|
||||
// attribute DOMString size;
|
||||
attribute DOMString size;
|
||||
};
|
||||
|
|
|
@ -1919,6 +1919,7 @@ pub mod longhands {
|
|||
use app_units::Au;
|
||||
use cssparser::ToCss;
|
||||
use std::fmt;
|
||||
use values::FONT_MEDIUM_PX;
|
||||
use values::computed::Context;
|
||||
|
||||
impl ToCss for SpecifiedValue {
|
||||
|
@ -1933,9 +1934,8 @@ pub mod longhands {
|
|||
use app_units::Au;
|
||||
pub type T = Au;
|
||||
}
|
||||
const MEDIUM_PX: i32 = 16;
|
||||
#[inline] pub fn get_initial_value() -> computed_value::T {
|
||||
Au::from_px(MEDIUM_PX)
|
||||
Au::from_px(FONT_MEDIUM_PX)
|
||||
}
|
||||
|
||||
impl ToComputedValue for SpecifiedValue {
|
||||
|
@ -1958,21 +1958,8 @@ pub mod longhands {
|
|||
specified::LengthOrPercentage::Calc(_) => Err(())
|
||||
})
|
||||
.or_else(|()| {
|
||||
match_ignore_ascii_case! { try!(input.expect_ident()),
|
||||
"xx-small" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX) * 3 / 5)),
|
||||
"x-small" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX) * 3 / 4)),
|
||||
"small" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX) * 8 / 9)),
|
||||
"medium" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX))),
|
||||
"large" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX) * 6 / 5)),
|
||||
"x-large" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX) * 3 / 2)),
|
||||
"xx-large" => Ok(specified::Length::Absolute(Au::from_px(MEDIUM_PX) * 2)),
|
||||
|
||||
// https://github.com/servo/servo/issues/3423#issuecomment-56321664
|
||||
"smaller" => Ok(specified::Length::FontRelative(specified::FontRelativeLength::Em(0.85))),
|
||||
"larger" => Ok(specified::Length::FontRelative(specified::FontRelativeLength::Em(1.2)))
|
||||
|
||||
_ => Err(())
|
||||
}
|
||||
let ident = try!(input.expect_ident());
|
||||
specified::Length::from_str(&ident as &str).ok_or(())
|
||||
})
|
||||
.map(SpecifiedValue)
|
||||
}
|
||||
|
|
|
@ -66,6 +66,8 @@ macro_rules! define_numbered_css_keyword_enum {
|
|||
|
||||
pub type CSSFloat = f32;
|
||||
|
||||
pub const FONT_MEDIUM_PX: i32 = 16;
|
||||
|
||||
|
||||
pub mod specified {
|
||||
use app_units::Au;
|
||||
|
@ -79,7 +81,7 @@ pub mod specified {
|
|||
use std::ops::Mul;
|
||||
use style_traits::values::specified::AllowedNumericType;
|
||||
use super::AuExtensionMethods;
|
||||
use super::CSSFloat;
|
||||
use super::{CSSFloat, FONT_MEDIUM_PX};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, HeapSizeOf)]
|
||||
|
@ -293,6 +295,24 @@ pub mod specified {
|
|||
const AU_PER_PT: CSSFloat = AU_PER_IN / 72.;
|
||||
const AU_PER_PC: CSSFloat = AU_PER_PT * 12.;
|
||||
impl Length {
|
||||
// https://drafts.csswg.org/css-fonts-3/#font-size-prop
|
||||
pub fn from_str(s: &str) -> Option<Length> {
|
||||
Some(match_ignore_ascii_case! { s,
|
||||
"xx-small" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX) * 3 / 5),
|
||||
"x-small" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX) * 3 / 4),
|
||||
"small" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX) * 8 / 9),
|
||||
"medium" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX)),
|
||||
"large" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX) * 6 / 5),
|
||||
"x-large" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX) * 3 / 2),
|
||||
"xx-large" => Length::Absolute(Au::from_px(FONT_MEDIUM_PX) * 2),
|
||||
|
||||
// https://github.com/servo/servo/issues/3423#issuecomment-56321664
|
||||
"smaller" => Length::FontRelative(FontRelativeLength::Em(0.85)),
|
||||
"larger" => Length::FontRelative(FontRelativeLength::Em(1.2))
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn parse_internal(input: &mut Parser, context: &AllowedNumericType) -> Result<Length, ()> {
|
||||
match try!(input.next()) {
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue