added support for :read-only and :read-write pseudo-classes

partial fix for https://github.com/servo/servo/issues/10732
This commit is contained in:
Yoav Alon 2016-04-25 18:55:31 +03:00
parent f773dc182b
commit 9bf909ac2f
9 changed files with 82 additions and 31 deletions

View file

@ -2161,13 +2161,17 @@ impl<'a> ::selectors::Element for Root<Element> {
}
},
NonTSPseudoClass::ReadOnly =>
!Element::state(self).contains(pseudo_class.state_flag()),
NonTSPseudoClass::Active |
NonTSPseudoClass::Focus |
NonTSPseudoClass::Hover |
NonTSPseudoClass::Enabled |
NonTSPseudoClass::Disabled |
NonTSPseudoClass::Checked |
NonTSPseudoClass::Indeterminate =>
NonTSPseudoClass::Indeterminate |
NonTSPseudoClass::ReadWrite =>
Element::state(self).contains(pseudo_class.state_flag()),
}
}
@ -2430,6 +2434,14 @@ impl Element {
pub fn set_disabled_state(&self, value: bool) {
self.set_state(IN_DISABLED_STATE, value)
}
pub fn read_write_state(&self) -> bool {
self.state.get().contains(IN_READ_WRITE_STATE)
}
pub fn set_read_write_state(&self, value: bool) {
self.set_state(IN_READ_WRITE_STATE, value)
}
}
impl Element {

View file

@ -124,7 +124,7 @@ impl HTMLInputElement {
let chan = document.window().constellation_chan();
HTMLInputElement {
htmlelement:
HTMLElement::new_inherited_with_state(IN_ENABLED_STATE,
HTMLElement::new_inherited_with_state(IN_ENABLED_STATE | IN_READ_WRITE_STATE,
localName, prefix, document),
input_type: Cell::new(InputType::InputText),
placeholder: DOMRefCell::new(DOMString::new()),
@ -710,6 +710,11 @@ impl VirtualMethods for HTMLInputElement {
el.set_disabled_state(disabled_state);
el.set_enabled_state(!disabled_state);
el.check_ancestors_disabled_state_for_form_control();
if self.input_type.get() == InputType::InputText {
let read_write = !(self.ReadOnly() || el.disabled_state());
el.set_read_write_state(read_write);
}
},
&atom!("checked") if !self.checked_changed.get() => {
let checked_state = match mutation {
@ -745,6 +750,15 @@ impl VirtualMethods for HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#input-type-change
let (old_value_mode, old_idl_value) = (self.value_mode(), self.Value());
self.input_type.set(new_type);
let el = self.upcast::<Element>();
if new_type == InputType::InputText {
let read_write = !(self.ReadOnly() || el.disabled_state());
el.set_read_write_state(read_write);
} else {
el.set_read_write_state(false);
}
let new_value_mode = self.value_mode();
match (&old_value_mode, old_idl_value.is_empty(), new_value_mode) {
@ -789,6 +803,10 @@ impl VirtualMethods for HTMLInputElement {
self.radio_group_name().as_ref());
}
self.input_type.set(InputType::InputText);
let el = self.upcast::<Element>();
let read_write = !(self.ReadOnly() || el.disabled_state());
el.set_read_write_state(read_write);
}
}
},
@ -822,6 +840,17 @@ impl VirtualMethods for HTMLInputElement {
attr.value().chars().filter(|&c| c != '\n' && c != '\r'));
}
},
&atom!("readonly") if self.input_type.get() == InputType::InputText => {
let el = self.upcast::<Element>();
match mutation {
AttributeMutation::Set(_) => {
el.set_read_write_state(false);
},
AttributeMutation::Removed => {
el.set_read_write_state(!el.disabled_state());
}
}
}
_ => {},
}
}

View file

@ -104,7 +104,7 @@ impl HTMLTextAreaElement {
let chan = document.window().constellation_chan();
HTMLTextAreaElement {
htmlelement:
HTMLElement::new_inherited_with_state(IN_ENABLED_STATE,
HTMLElement::new_inherited_with_state(IN_ENABLED_STATE | IN_READ_WRITE_STATE,
localName, prefix, document),
textinput: DOMRefCell::new(TextInput::new(
Lines::Multiple, DOMString::new(), chan, None, SelectionDirection::None)),
@ -290,14 +290,31 @@ impl VirtualMethods for HTMLTextAreaElement {
AttributeMutation::Set(_) => {
el.set_disabled_state(true);
el.set_enabled_state(false);
el.set_read_write_state(false);
},
AttributeMutation::Removed => {
el.set_disabled_state(false);
el.set_enabled_state(true);
el.check_ancestors_disabled_state_for_form_control();
if !el.disabled_state() && !el.read_write_state() {
el.set_read_write_state(true);
}
}
}
},
atom!("readonly") => {
let el = self.upcast::<Element>();
match mutation {
AttributeMutation::Set(_) => {
el.set_read_write_state(false);
},
AttributeMutation::Removed => {
el.set_read_write_state(!el.disabled_state());
}
}
}
_ => {},
}
}