mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +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
|
@ -2,13 +2,18 @@
|
|||
* 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::AttrValue;
|
||||
use dom::bindings::codegen::Bindings::HTMLLabelElementBinding;
|
||||
use dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods;
|
||||
use dom::bindings::conversions::Castable;
|
||||
use dom::bindings::js::Root;
|
||||
use dom::document::Document;
|
||||
use dom::element::Element;
|
||||
use dom::htmlelement::HTMLElement;
|
||||
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;
|
||||
|
||||
#[dom_struct]
|
||||
|
@ -40,6 +45,48 @@ impl HTMLLabelElementMethods for HTMLLabelElement {
|
|||
fn GetForm(&self) -> Option<Root<HTMLFormElement>> {
|
||||
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 {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue