mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
Implement 'control' attribute for <label> elements
This commit is contained in:
parent
3b50f21963
commit
f97d1d148b
8 changed files with 74 additions and 60 deletions
|
@ -1147,7 +1147,7 @@ impl Document {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_element_by_id(&self, id: &Atom) -> Option<Root<Element>> {
|
pub fn get_element_by_id(&self, id: &Atom) -> Option<Root<Element>> {
|
||||||
self.idmap.borrow().get(&id).map(|ref elements| (*elements)[0].root())
|
self.idmap.borrow().get(&id).map(|ref elements| (*elements)[0].root())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -296,6 +296,26 @@ impl HTMLElement {
|
||||||
let local_name = Atom::from_slice(&to_snake_case(local_name));
|
let local_name = Atom::from_slice(&to_snake_case(local_name));
|
||||||
self.upcast::<Element>().remove_attribute(&ns!(""), &local_name);
|
self.upcast::<Element>().remove_attribute(&ns!(""), &local_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/#category-label
|
||||||
|
pub fn is_labelable_element(&self) -> bool {
|
||||||
|
// Note: HTMLKeygenElement is omitted because Servo doesn't currently implement it
|
||||||
|
match self.upcast::<Node>().type_id() {
|
||||||
|
NodeTypeId::Element(ElementTypeId::HTMLElement(type_id)) =>
|
||||||
|
match type_id {
|
||||||
|
HTMLElementTypeId::HTMLInputElement =>
|
||||||
|
self.downcast::<HTMLInputElement>().unwrap().Type() != "hidden",
|
||||||
|
HTMLElementTypeId::HTMLButtonElement |
|
||||||
|
HTMLElementTypeId::HTMLMeterElement |
|
||||||
|
HTMLElementTypeId::HTMLOutputElement |
|
||||||
|
HTMLElementTypeId::HTMLProgressElement |
|
||||||
|
HTMLElementTypeId::HTMLSelectElement |
|
||||||
|
HTMLElementTypeId::HTMLTextAreaElement => true,
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtualMethods for HTMLElement {
|
impl VirtualMethods for HTMLElement {
|
||||||
|
|
|
@ -2,13 +2,18 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use dom::attr::AttrValue;
|
||||||
use dom::bindings::codegen::Bindings::HTMLLabelElementBinding;
|
use dom::bindings::codegen::Bindings::HTMLLabelElementBinding;
|
||||||
use dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods;
|
use dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods;
|
||||||
|
use dom::bindings::conversions::Castable;
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::Root;
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
|
use dom::element::Element;
|
||||||
use dom::htmlelement::HTMLElement;
|
use dom::htmlelement::HTMLElement;
|
||||||
use dom::htmlformelement::{FormControl, HTMLFormElement};
|
use dom::htmlformelement::{FormControl, HTMLFormElement};
|
||||||
use dom::node::Node;
|
use dom::node::{document_from_node, Node};
|
||||||
|
use dom::virtualmethods::VirtualMethods;
|
||||||
|
use string_cache::Atom;
|
||||||
use util::str::DOMString;
|
use util::str::DOMString;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
|
@ -40,6 +45,48 @@ impl HTMLLabelElementMethods for HTMLLabelElement {
|
||||||
fn GetForm(&self) -> Option<Root<HTMLFormElement>> {
|
fn GetForm(&self) -> Option<Root<HTMLFormElement>> {
|
||||||
self.form_owner()
|
self.form_owner()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/#dom-label-control
|
||||||
|
fn GetControl(&self) -> Option<Root<HTMLElement>> {
|
||||||
|
if !self.upcast::<Node>().is_in_doc() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let for_attr = match self.upcast::<Element>().get_attribute(&ns!(""), &atom!("for")) {
|
||||||
|
Some(for_attr) => for_attr,
|
||||||
|
None => return self.first_labelable_descendant(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let for_value = for_attr.value();
|
||||||
|
document_from_node(self).get_element_by_id(for_value.as_atom())
|
||||||
|
.and_then(Root::downcast::<HTMLElement>)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|e| e.is_labelable_element())
|
||||||
|
.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VirtualMethods for HTMLLabelElement {
|
||||||
|
fn super_type(&self) -> Option<&VirtualMethods> {
|
||||||
|
Some(self.upcast::<HTMLElement>() as &VirtualMethods)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
|
||||||
|
match name {
|
||||||
|
&atom!("for") => AttrValue::from_atomic(value),
|
||||||
|
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HTMLLabelElement {
|
||||||
|
fn first_labelable_descendant(&self) -> Option<Root<HTMLElement>> {
|
||||||
|
self.upcast::<Node>()
|
||||||
|
.traverse_preorder()
|
||||||
|
.filter_map(Root::downcast::<HTMLElement>)
|
||||||
|
.filter(|elem| elem.is_labelable_element())
|
||||||
|
.next()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormControl for HTMLLabelElement {}
|
impl FormControl for HTMLLabelElement {}
|
||||||
|
|
|
@ -25,6 +25,7 @@ use dom::htmlheadelement::HTMLHeadElement;
|
||||||
use dom::htmliframeelement::HTMLIFrameElement;
|
use dom::htmliframeelement::HTMLIFrameElement;
|
||||||
use dom::htmlimageelement::HTMLImageElement;
|
use dom::htmlimageelement::HTMLImageElement;
|
||||||
use dom::htmlinputelement::HTMLInputElement;
|
use dom::htmlinputelement::HTMLInputElement;
|
||||||
|
use dom::htmllabelelement::HTMLLabelElement;
|
||||||
use dom::htmllinkelement::HTMLLinkElement;
|
use dom::htmllinkelement::HTMLLinkElement;
|
||||||
use dom::htmlmetaelement::HTMLMetaElement;
|
use dom::htmlmetaelement::HTMLMetaElement;
|
||||||
use dom::htmlobjectelement::HTMLObjectElement;
|
use dom::htmlobjectelement::HTMLObjectElement;
|
||||||
|
@ -164,6 +165,9 @@ pub fn vtable_for(node: &Node) -> &VirtualMethods {
|
||||||
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLInputElement)) => {
|
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLInputElement)) => {
|
||||||
node.downcast::<HTMLInputElement>().unwrap() as &VirtualMethods
|
node.downcast::<HTMLInputElement>().unwrap() as &VirtualMethods
|
||||||
}
|
}
|
||||||
|
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLabelElement)) => {
|
||||||
|
node.downcast::<HTMLLabelElement>().unwrap() as &VirtualMethods
|
||||||
|
}
|
||||||
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLinkElement)) => {
|
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLinkElement)) => {
|
||||||
node.downcast::<HTMLLinkElement>().unwrap() as &VirtualMethods
|
node.downcast::<HTMLLinkElement>().unwrap() as &VirtualMethods
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,5 @@
|
||||||
interface HTMLLabelElement : HTMLElement {
|
interface HTMLLabelElement : HTMLElement {
|
||||||
readonly attribute HTMLFormElement? form;
|
readonly attribute HTMLFormElement? form;
|
||||||
// attribute DOMString htmlFor;
|
// attribute DOMString htmlFor;
|
||||||
//readonly attribute HTMLElement? control;
|
readonly attribute HTMLElement? control;
|
||||||
};
|
};
|
||||||
|
|
|
@ -4884,15 +4884,9 @@
|
||||||
[HTMLLabelElement interface: attribute htmlFor]
|
[HTMLLabelElement interface: attribute htmlFor]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[HTMLLabelElement interface: attribute control]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[HTMLLabelElement interface: document.createElement("label") must inherit property "htmlFor" with the proper type (1)]
|
[HTMLLabelElement interface: document.createElement("label") must inherit property "htmlFor" with the proper type (1)]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[HTMLLabelElement interface: document.createElement("label") must inherit property "control" with the proper type (2)]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[HTMLInputElement interface: existence and properties of interface object]
|
[HTMLInputElement interface: existence and properties of interface object]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,8 @@
|
||||||
[label-attributes.html]
|
[label-attributes.html]
|
||||||
type: testharness
|
type: testharness
|
||||||
[A label element with a 'for' attribute should only be associated with a labelable element.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[A label element not in a document can not label any element in the document.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[The labeled control for a label element that has no 'for' attribute is the first labelable element which is a descendant of that label element.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[The 'for' attribute points to an inexistent id.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[A non-control follows by a control with same ID.]
|
[A non-control follows by a control with same ID.]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[The 'for' attribute is an empty string.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[A form control has multiple labels.]
|
[A form control has multiple labels.]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
|
@ -1,41 +1,5 @@
|
||||||
[labelable-elements.html]
|
[labelable-elements.html]
|
||||||
type: testharness
|
type: testharness
|
||||||
[Check if the output element is a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the progress element is a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the select element is a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the textarea element is a labelable form-element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the button element is a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the hidden input element is not a labelable element.]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the input element in radio state is a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the keygen element is a labelable element]
|
[Check if the keygen element is a labelable element]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
[Check if the meter element is a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the fieldset element is not a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the label element is not a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the object element is not a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Check if the img element is not a labelable element]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue