auto merge of #4152 : mttr/servo/textview, r=jdm

Fixes #3918 

Can be tested in `tests/html/textarea.html`. Also implemented some content reflecting IDL attributes for HTMLTextAreaElement while I was in there.

There are some major problems with TextInput when Multiple is enabled that I haven't addressed here, but I'm prepared to open up a follow-up issue.
This commit is contained in:
bors-servo 2014-12-05 16:25:07 -07:00
commit a369dcfa01
9 changed files with 203 additions and 832 deletions

View file

@ -49,6 +49,7 @@ use script::dom::element::{HTMLObjectElementTypeId, HTMLInputElementTypeId};
use script::dom::element::{HTMLTableColElementTypeId, HTMLTableDataCellElementTypeId};
use script::dom::element::{HTMLTableElementTypeId, HTMLTableHeaderCellElementTypeId};
use script::dom::element::{HTMLTableRowElementTypeId, HTMLTableSectionElementTypeId};
use script::dom::element::HTMLTextAreaElementTypeId;
use script::dom::node::{CommentNodeTypeId, DoctypeNodeTypeId, DocumentFragmentNodeTypeId};
use script::dom::node::{DocumentNodeTypeId, ElementNodeTypeId, ProcessingInstructionNodeTypeId};
use script::dom::node::{TextNodeTypeId};
@ -487,7 +488,16 @@ impl<'a> FlowConstructor<'a> {
// Special case: If this is generated content, then we need to initialize the accumulator
// with the fragment corresponding to that content.
if node.get_pseudo_element_type() != Normal ||
node.type_id() == Some(ElementNodeTypeId(HTMLInputElementTypeId)) {
node.type_id() == Some(ElementNodeTypeId(HTMLInputElementTypeId)) ||
node.type_id() == Some(ElementNodeTypeId(HTMLTextAreaElementTypeId)) {
// A TextArea's text contents are displayed through the input text
// box, so don't construct them.
// TODO Maybe this belongs somewhere else?
if node.type_id() == Some(ElementNodeTypeId(HTMLTextAreaElementTypeId)) {
for kid in node.children() {
kid.set_flow_construction_result(NoConstructionResult)
}
}
let fragment_info = UnscannedTextFragment(UnscannedTextFragmentInfo::new(node));
let fragment = Fragment::new_from_specific_info(node, fragment_info);
inline_fragment_accumulator.fragments.push_back(fragment);

View file

@ -5,7 +5,7 @@
#![comment = "The Servo Parallel Browser Project"]
#![license = "MPL"]
#![feature(globs, macro_rules, phase, thread_local, unsafe_destructor)]
#![feature(globs, macro_rules, phase, thread_local, unsafe_destructor, if_let)]
#![deny(unused_imports)]
#![deny(unused_variables)]

View file

@ -39,13 +39,14 @@ use util::{PrivateLayoutData};
use gfx::display_list::OpaqueNode;
use script::dom::bindings::codegen::InheritTypes::{ElementCast, HTMLIFrameElementCast};
use script::dom::bindings::codegen::InheritTypes::{HTMLImageElementCast, HTMLInputElementCast};
use script::dom::bindings::codegen::InheritTypes::{NodeCast, TextCast};
use script::dom::bindings::codegen::InheritTypes::{HTMLTextAreaElementCast, NodeCast, TextCast};
use script::dom::bindings::js::JS;
use script::dom::element::{Element, HTMLAreaElementTypeId, HTMLAnchorElementTypeId};
use script::dom::element::{HTMLLinkElementTypeId, LayoutElementHelpers, RawLayoutElementHelpers};
use script::dom::htmliframeelement::HTMLIFrameElement;
use script::dom::htmlimageelement::LayoutHTMLImageElementHelpers;
use script::dom::htmlinputelement::LayoutHTMLInputElementHelpers;
use script::dom::htmltextareaelement::LayoutHTMLTextAreaElementHelpers;
use script::dom::node::{DocumentNodeTypeId, ElementNodeTypeId, Node, NodeTypeId};
use script::dom::node::{LayoutNodeHelpers, RawLayoutNodeHelpers, SharedLayoutData};
use script::dom::node::{HAS_CHANGED, IS_DIRTY, HAS_DIRTY_SIBLINGS, HAS_DIRTY_DESCENDANTS};
@ -183,13 +184,14 @@ impl<'ln> TLayoutNode for LayoutNode<'ln> {
fn text(&self) -> String {
unsafe {
let text_opt: Option<JS<Text>> = TextCast::to_js(self.get_jsmanaged());
match text_opt {
Some(text) => (*text.unsafe_get()).characterdata().data_for_layout().to_string(),
None => match HTMLInputElementCast::to_js(self.get_jsmanaged()) {
Some(input) => input.get_value_for_layout(),
None => panic!("not text!")
}
if let Some(text) = TextCast::to_js(self.get_jsmanaged()) {
(*text.unsafe_get()).characterdata().data_for_layout().to_string()
} else if let Some(input) = HTMLInputElementCast::to_js(self.get_jsmanaged()) {
input.get_value_for_layout()
} else if let Some(area) = HTMLTextAreaElementCast::to_js(self.get_jsmanaged()) {
area.get_value_for_layout()
} else {
panic!("not text!")
}
}
}

View file

@ -2,27 +2,40 @@
* 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;
use dom::attr::{Attr, AttrValue};
use dom::attr::AttrHelpers;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use dom::bindings::codegen::InheritTypes::{HTMLElementCast, NodeCast};
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{HTMLTextAreaElementDerived, HTMLFieldSetElementDerived};
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::codegen::InheritTypes::{KeyboardEventCast, TextDerived};
use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document;
use dom::document::{Document, DocumentHelpers};
use dom::element::{AttributeHandlers, HTMLTextAreaElementTypeId};
use dom::event::Event;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{DisabledStateHelpers, Node, NodeHelpers, ElementNodeTypeId};
use dom::keyboardevent::KeyboardEvent;
use dom::node::{DisabledStateHelpers, Node, NodeHelpers, ElementNodeTypeId, document_from_node};
use textinput::{Multiple, TextInput, TriggerDefaultAction, DispatchInput, Nothing};
use dom::virtualmethods::VirtualMethods;
use servo_util::str::DOMString;
use string_cache::Atom;
use std::cell::Cell;
#[dom_struct]
pub struct HTMLTextAreaElement {
htmlelement: HTMLElement,
textinput: DOMRefCell<TextInput>,
// https://html.spec.whatwg.org/multipage/forms.html#concept-textarea-dirty
value_changed: Cell<bool>,
}
impl HTMLTextAreaElementDerived for EventTarget {
@ -31,10 +44,26 @@ impl HTMLTextAreaElementDerived for EventTarget {
}
}
pub trait LayoutHTMLTextAreaElementHelpers {
unsafe fn get_value_for_layout(self) -> String;
}
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()
}
}
static DEFAULT_COLS: u32 = 20;
static DEFAULT_ROWS: u32 = 2;
impl HTMLTextAreaElement {
fn new_inherited(localName: DOMString, prefix: Option<DOMString>, document: JSRef<Document>) -> HTMLTextAreaElement {
HTMLTextAreaElement {
htmlelement: HTMLElement::new_inherited(HTMLTextAreaElementTypeId, localName, prefix, document)
htmlelement: HTMLElement::new_inherited(HTMLTextAreaElementTypeId, localName, prefix, document),
textinput: DOMRefCell::new(TextInput::new(Multiple, "".to_string())),
value_changed: Cell::new(false),
}
}
@ -46,16 +75,96 @@ impl HTMLTextAreaElement {
}
impl<'a> HTMLTextAreaElementMethods for JSRef<'a, HTMLTextAreaElement> {
// TODO A few of these attributes have default values and additional
// constraints
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-cols
make_uint_getter!(Cols)
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-cols
make_uint_setter!(SetCols, "cols")
// http://www.whatwg.org/html/#dom-fe-disabled
make_bool_getter!(Disabled)
// http://www.whatwg.org/html/#dom-fe-disabled
make_bool_setter!(SetDisabled, "disabled")
// https://html.spec.whatwg.org/multipage/forms.html#attr-fe-name
make_getter!(Name)
// https://html.spec.whatwg.org/multipage/forms.html#attr-fe-name
make_setter!(SetName, "name")
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-placeholder
make_getter!(Placeholder)
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-placeholder
make_setter!(SetPlaceholder, "placeholder")
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-required
make_bool_getter!(Required)
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-required
make_bool_setter!(SetRequired, "required")
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-rows
make_uint_getter!(Rows)
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-rows
make_uint_setter!(SetRows, "rows")
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-wrap
make_getter!(Wrap)
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-wrap
make_setter!(SetWrap, "wrap")
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-type
fn Type(self) -> DOMString {
"textarea".to_string()
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-defaultvalue
fn DefaultValue(self) -> DOMString {
let node: JSRef<Node> = NodeCast::from_ref(self);
node.GetTextContent().unwrap()
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-defaultvalue
fn SetDefaultValue(self, value: DOMString) {
let node: JSRef<Node> = NodeCast::from_ref(self);
node.SetTextContent(Some(value));
// if the element's dirty value flag is false, then the element's
// raw value must be set to the value of the element's textContent IDL attribute
if !self.value_changed.get() {
self.SetValue(node.GetTextContent().unwrap());
}
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-value
fn Value(self) -> DOMString {
self.textinput.borrow().get_content()
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-value
fn SetValue(self, value: DOMString) {
self.textinput.borrow_mut().set_content(value);
self.force_relayout();
}
}
trait PrivateHTMLTextAreaElementHelpers {
fn force_relayout(self);
}
impl<'a> PrivateHTMLTextAreaElementHelpers for JSRef<'a, HTMLTextAreaElement> {
fn force_relayout(self) {
let doc = document_from_node(self).root();
let node: JSRef<Node> = NodeCast::from_ref(self);
doc.content_changed(node)
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
@ -107,6 +216,14 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
node.check_ancestors_disabled_state_for_form_control();
}
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
match name {
&atom!("cols") => AttrValue::from_u32(value, DEFAULT_COLS),
&atom!("rows") => AttrValue::from_u32(value, DEFAULT_ROWS),
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
}
}
fn unbind_from_tree(&self, tree_in_doc: bool) {
match self.super_type() {
Some(ref s) => s.unbind_from_tree(tree_in_doc),
@ -120,6 +237,48 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
node.check_disabled_attribute();
}
}
fn child_inserted(&self, child: JSRef<Node>) {
match self.super_type() {
Some(s) => {
s.child_inserted(child);
}
_ => (),
}
if child.is_text() {
self.SetValue(self.DefaultValue());
}
}
// copied and modified from htmlinputelement.rs
fn handle_event(&self, event: JSRef<Event>) {
match self.super_type() {
Some(s) => {
s.handle_event(event);
}
_ => (),
}
if "click" == event.Type().as_slice() && !event.DefaultPrevented() {
//TODO: set the editing position for text inputs
let doc = document_from_node(*self).root();
doc.request_focus(ElementCast::from_ref(*self));
} else if "keydown" == event.Type().as_slice() && !event.DefaultPrevented() {
let keyevent: Option<JSRef<KeyboardEvent>> = KeyboardEventCast::to_ref(event);
keyevent.map(|event| {
match self.textinput.borrow_mut().handle_keydown(event) {
TriggerDefaultAction => (),
DispatchInput => {
self.force_relayout();
self.value_changed.set(true);
}
Nothing => (),
}
});
}
}
}
impl Reflectable for HTMLTextAreaElement {

View file

@ -7,23 +7,23 @@
interface HTMLTextAreaElement : HTMLElement {
// attribute DOMString autocomplete;
// attribute boolean autofocus;
// attribute unsigned long cols;
attribute unsigned long cols;
// attribute DOMString dirName;
attribute boolean disabled;
//readonly attribute HTMLFormElement? form;
// attribute DOMString inputMode;
// attribute long maxLength;
// attribute long minLength;
// attribute DOMString name;
// attribute DOMString placeholder;
attribute DOMString name;
attribute DOMString placeholder;
// attribute boolean readOnly;
// attribute boolean required;
// attribute unsigned long rows;
// attribute DOMString wrap;
attribute boolean required;
attribute unsigned long rows;
attribute DOMString wrap;
readonly attribute DOMString type;
// attribute DOMString defaultValue;
//[TreatNullAs=EmptyString] attribute DOMString value;
attribute DOMString defaultValue;
[TreatNullAs=EmptyString] attribute DOMString value;
//readonly attribute unsigned long textLength;
//readonly attribute boolean willValidate;