Skip to content

Commit

Permalink
auto merge of #4380 : mttr/servo/textarea_rows_and_cols, r=pcwalton
Browse files Browse the repository at this point in the history
...with a bit of a caveat: sizing has the same problem as seen in #4378, and it is _significantly_ more noticeable when using `rows`.

Fixes #4291
  • Loading branch information
bors-servo committed Dec 15, 2014
2 parents 1bc2c8a + 60ccd9e commit ef90fdc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
17 changes: 17 additions & 0 deletions components/script/dom/element.rs
Expand Up @@ -17,6 +17,7 @@ use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementM
use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use dom::bindings::codegen::InheritTypes::{ElementDerived, HTMLInputElementDerived, HTMLTableCellElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLInputElementCast, NodeCast, EventTargetCast, ElementCast};
use dom::bindings::codegen::InheritTypes::HTMLTextAreaElementDerived;
use dom::bindings::js::{MutNullableJS, JS, JSRef, Temporary, TemporaryPushable};
use dom::bindings::js::{OptionalRootable, Root};
use dom::bindings::utils::{Reflectable, Reflector};
Expand All @@ -33,12 +34,14 @@ use dom::htmlcollection::HTMLCollection;
use dom::htmlinputelement::{HTMLInputElement, RawLayoutHTMLInputElementHelpers};
use dom::htmlserializer::serialize;
use dom::htmltablecellelement::{HTMLTableCellElement, HTMLTableCellElementHelpers};
use dom::htmltextareaelement::{HTMLTextAreaElement, RawLayoutHTMLTextAreaElementHelpers};
use dom::node::{ElementNodeTypeId, Node, NodeHelpers, NodeIterator, document_from_node, CLICK_IN_PROGRESS};
use dom::node::{window_from_node, LayoutNodeHelpers};
use dom::nodelist::NodeList;
use dom::virtualmethods::{VirtualMethods, vtable_for};
use devtools_traits::AttrInfo;
use style::{IntegerAttribute, LengthAttribute, SizeIntegerAttribute, WidthLengthAttribute};
use style::{RowsIntegerAttribute, ColsIntegerAttribute};
use style::{matches, parse_selector_list_from_str};
use style;
use servo_util::namespace;
Expand Down Expand Up @@ -304,6 +307,20 @@ impl RawLayoutElementHelpers for Element {
let this: &HTMLInputElement = mem::transmute(self);
Some(this.get_size_for_layout() as i32)
}
ColsIntegerAttribute => {
if !self.is_htmltextareaelement() {
panic!("I'm not a textarea element!")
}
let this: &HTMLTextAreaElement = mem::transmute(self);
Some(this.get_cols_for_layout() as i32)
}
RowsIntegerAttribute => {
if !self.is_htmltextareaelement() {
panic!("I'm not a textarea element!")
}
let this: &HTMLTextAreaElement = mem::transmute(self);
Some(this.get_rows_for_layout() as i32)
}
}
}

Expand Down
41 changes: 40 additions & 1 deletion components/script/dom/htmltextareaelement.rs
Expand Up @@ -2,7 +2,7 @@
* 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/. */

use dom::attr::{Attr, AttrValue};
use dom::attr::{Attr, AttrValue, UIntAttrValue};
use dom::attr::AttrHelpers;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
Expand Down Expand Up @@ -33,6 +33,8 @@ use std::cell::Cell;
pub struct HTMLTextAreaElement {
htmlelement: HTMLElement,
textinput: DOMRefCell<TextInput>,
cols: Cell<u32>,
rows: Cell<u32>,

// https://html.spec.whatwg.org/multipage/forms.html#concept-textarea-dirty
value_changed: Cell<bool>,
Expand All @@ -48,13 +50,30 @@ pub trait LayoutHTMLTextAreaElementHelpers {
unsafe fn get_value_for_layout(self) -> String;
}

pub trait RawLayoutHTMLTextAreaElementHelpers {
unsafe fn get_cols_for_layout(&self) -> u32;
unsafe fn get_rows_for_layout(&self) -> u32;
}

impl LayoutHTMLTextAreaElementHelpers for JS<HTMLTextAreaElement> {
#[allow(unrooted_must_root)]
unsafe fn get_value_for_layout(self) -> String {
(*self.unsafe_get()).textinput.borrow_for_layout().get_content()
}
}

impl RawLayoutHTMLTextAreaElementHelpers for HTMLTextAreaElement {
#[allow(unrooted_must_root)]
unsafe fn get_cols_for_layout(&self) -> u32 {
self.cols.get()
}

#[allow(unrooted_must_root)]
unsafe fn get_rows_for_layout(&self) -> u32 {
self.rows.get()
}
}

static DEFAULT_COLS: u32 = 20;
static DEFAULT_ROWS: u32 = 2;

Expand All @@ -63,6 +82,8 @@ impl HTMLTextAreaElement {
HTMLTextAreaElement {
htmlelement: HTMLElement::new_inherited(HTMLTextAreaElementTypeId, localName, prefix, document),
textinput: DOMRefCell::new(TextInput::new(Multiple, "".to_string())),
cols: Cell::new(DEFAULT_COLS),
rows: Cell::new(DEFAULT_ROWS),
value_changed: Cell::new(false),
}
}
Expand Down Expand Up @@ -185,6 +206,18 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
node.set_disabled_state(true);
node.set_enabled_state(false);
},
&atom!("cols") => {
match *attr.value() {
UIntAttrValue(_, value) => self.cols.set(value),
_ => panic!("Expected a UIntAttrValue"),
}
},
&atom!("rows") => {
match *attr.value() {
UIntAttrValue(_, value) => self.rows.set(value),
_ => panic!("Expected a UIntAttrValue"),
}
},
_ => ()
}
}
Expand All @@ -202,6 +235,12 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
node.set_enabled_state(true);
node.check_ancestors_disabled_state_for_form_control();
},
&atom!("cols") => {
self.cols.set(DEFAULT_COLS);
},
&atom!("rows") => {
self.rows.set(DEFAULT_ROWS);
},
_ => ()
}
}
Expand Down
2 changes: 2 additions & 0 deletions components/style/legacy.rs
Expand Up @@ -15,5 +15,7 @@ pub enum LengthAttribute {
pub enum IntegerAttribute {
/// `<input size>`
SizeIntegerAttribute,
ColsIntegerAttribute,
RowsIntegerAttribute,
}

3 changes: 2 additions & 1 deletion components/style/lib.rs
Expand Up @@ -53,7 +53,8 @@ pub use selectors::{PseudoElement, Before, After, SelectorList, parse_selector_l
pub use selectors::{AttrSelector, NamespaceConstraint, SpecificNamespace, AnyNamespace};
pub use selectors::{SimpleSelector,LocalNameSelector};
pub use cssparser::{Color, RGBA};
pub use legacy::{IntegerAttribute, LengthAttribute, SizeIntegerAttribute, WidthLengthAttribute};
pub use legacy::{IntegerAttribute, LengthAttribute, SizeIntegerAttribute};
pub use legacy::{ColsIntegerAttribute, RowsIntegerAttribute, WidthLengthAttribute};
pub use font_face::{Source, LocalSource, UrlSource_};

mod stylesheets;
Expand Down
33 changes: 32 additions & 1 deletion components/style/selector_matching.rs
Expand Up @@ -19,10 +19,11 @@ use servo_util::str::{AutoLpa, LengthLpa, PercentageLpa};
use string_cache::Atom;

use legacy::{SizeIntegerAttribute, WidthLengthAttribute};
use legacy::{ColsIntegerAttribute, RowsIntegerAttribute};
use media_queries::Device;
use node::{TElement, TElementAttributes, TNode};
use properties::{PropertyDeclaration, PropertyDeclarationBlock, SpecifiedValue, WidthDeclaration};
use properties::{specified};
use properties::{HeightDeclaration, specified, CSSFloat};
use selectors::*;
use stylesheets::{Stylesheet, iter_stylesheet_media_rules, iter_stylesheet_style_rules};

Expand Down Expand Up @@ -530,6 +531,36 @@ impl Stylist {
Some(_) | None => {}
}
}
name if *name == atom!("textarea") => {
match element.get_integer_attribute(ColsIntegerAttribute) {
Some(value) if value != 0 => {
// TODO(mttr) ServoCharacterWidth uses the size math for <input type="text">, but
// the math for <textarea> is a little different since we need to take
// scrollbar size into consideration (but we don't have a scrollbar yet!)
//
// https://html.spec.whatwg.org/multipage/rendering.html#textarea-effective-width
let value = specified::ServoCharacterWidth(value);
matching_rules_list.vec_push(DeclarationBlock::from_declaration(
WidthDeclaration(SpecifiedValue(specified::LPA_Length(
value)))));
*shareable = false
}
Some(_) | None => {}
}
match element.get_integer_attribute(RowsIntegerAttribute) {
Some(value) if value != 0 => {
// TODO(mttr) This should take scrollbar size into consideration.
//
// https://html.spec.whatwg.org/multipage/rendering.html#textarea-effective-height
let value = specified::Em(value as CSSFloat);
matching_rules_list.vec_push(DeclarationBlock::from_declaration(
HeightDeclaration(SpecifiedValue(specified::LPA_Length(
value)))));
*shareable = false
}
Some(_) | None => {}
}
}
_ => {}
}
}
Expand Down

0 comments on commit ef90fdc

Please sign in to comment.