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

@ -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,
}
}

View file

@ -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]

View file

@ -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 {

View file

@ -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()
}
}
}

View file

@ -7,5 +7,5 @@
interface HTMLFontElement : HTMLElement {
[TreatNullAs=EmptyString] attribute DOMString color;
attribute DOMString face;
// attribute DOMString size;
attribute DOMString size;
};