Auto merge of #7965 - frewsxcv:labelable-elements-label-attribute, r=nox

Implement 'labels' attribute on 'labelable elements'



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7965)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-11-01 20:51:18 +05:30
commit 0e70a1f8a8
22 changed files with 184 additions and 63 deletions

View file

@ -17,6 +17,7 @@ use dom::htmlfieldsetelement::HTMLFieldSetElement;
use dom::htmlformelement::{FormControl, FormSubmitter};
use dom::htmlformelement::{SubmittedFrom, HTMLFormElement};
use dom::node::{Node, document_from_node, window_from_node};
use dom::nodelist::NodeList;
use dom::validitystate::ValidityState;
use dom::virtualmethods::VirtualMethods;
use selectors::states::*;
@ -131,6 +132,11 @@ impl HTMLButtonElementMethods for HTMLButtonElement {
// https://html.spec.whatwg.org/multipage/#dom-button-value
make_setter!(SetValue, "value");
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
}
impl VirtualMethods for HTMLButtonElement {

View file

@ -4,6 +4,7 @@
use dom::attr::Attr;
use dom::attr::AttrValue;
use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::HTMLElementBinding;
use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
@ -12,7 +13,7 @@ use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::{ElementTypeId, HTMLElementTypeId, NodeTypeId};
use dom::bindings::conversions::Castable;
use dom::bindings::error::{Error, ErrorResult};
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::utils::Reflectable;
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration};
use dom::document::Document;
@ -23,8 +24,10 @@ use dom::htmlbodyelement::HTMLBodyElement;
use dom::htmlframesetelement::HTMLFrameSetElement;
use dom::htmlhtmlelement::HTMLHtmlElement;
use dom::htmlinputelement::HTMLInputElement;
use dom::htmllabelelement::HTMLLabelElement;
use dom::node::{Node, SEQUENTIALLY_FOCUSABLE};
use dom::node::{document_from_node, window_from_node};
use dom::nodelist::NodeList;
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::FocusType;
use selectors::states::*;
@ -365,6 +368,45 @@ impl HTMLElement {
to_camel_case(&raw_name)
}).collect()
}
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
pub fn labels(&self) -> Root<NodeList> {
debug_assert!(self.is_labelable_element());
let element = self.upcast::<Element>();
let window = window_from_node(element);
// Traverse ancestors for implicitly associated <label> elements
// https://html.spec.whatwg.org/multipage/#the-label-element:attr-label-for-4
let ancestors =
self.upcast::<Node>()
.ancestors()
.filter_map(Root::downcast::<HTMLElement>)
// If we reach a labelable element, we have a guarantee no ancestors above it
// will be a label for this HTMLElement
.take_while(|elem| !elem.is_labelable_element())
.filter_map(Root::downcast::<HTMLLabelElement>)
.filter(|elem| !elem.upcast::<Element>().has_attribute(&atom!("for")))
.filter(|elem| elem.first_labelable_descendant().r() == Some(self))
.map(Root::upcast::<Node>);
let id = element.Id();
let id = match &id as &str {
"" => return NodeList::new_simple_list(window.r(), ancestors),
id => id,
};
// Traverse entire tree for <label> elements with `for` attribute matching `id`
let root_element = element.get_root_element();
let root_node = root_element.upcast::<Node>();
let children = root_node.traverse_preorder()
.filter_map(Root::downcast::<Element>)
.filter(|elem| elem.is::<HTMLLabelElement>())
.filter(|elem| elem.get_string_attribute(&atom!("for")) == id)
.map(Root::upcast::<Node>);
NodeList::new_simple_list(window.r(), children.chain(ancestors))
}
}
impl VirtualMethods for HTMLElement {

View file

@ -25,6 +25,7 @@ use dom::htmlformelement::{ResetFrom, SubmittedFrom};
use dom::keyboardevent::KeyboardEvent;
use dom::node::{Node, NodeDamage};
use dom::node::{document_from_node, window_from_node};
use dom::nodelist::NodeList;
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan;
use selectors::states::*;
@ -333,6 +334,16 @@ impl HTMLInputElementMethods for HTMLInputElement {
fn SetIndeterminate(&self, val: bool) {
self.upcast::<Element>().set_state(IN_INDETERMINATE_STATE, val)
}
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
if self.Type() == "hidden" {
let window = window_from_node(self);
NodeList::empty(&window)
} else {
self.upcast::<HTMLElement>().labels()
}
}
}

View file

@ -123,7 +123,7 @@ impl VirtualMethods for HTMLLabelElement {
}
impl HTMLLabelElement {
fn first_labelable_descendant(&self) -> Option<Root<HTMLElement>> {
pub fn first_labelable_descendant(&self) -> Option<Root<HTMLElement>> {
self.upcast::<Node>()
.traverse_preorder()
.filter_map(Root::downcast::<HTMLElement>)

View file

@ -2,11 +2,13 @@
* 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::bindings::codegen::Bindings::HTMLMeterElementBinding;
use dom::bindings::codegen::Bindings::HTMLMeterElementBinding::{self, HTMLMeterElementMethods};
use dom::bindings::conversions::Castable;
use dom::bindings::js::Root;
use dom::document::Document;
use dom::htmlelement::HTMLElement;
use dom::node::Node;
use dom::nodelist::NodeList;
use util::str::DOMString;
#[dom_struct]
@ -31,3 +33,10 @@ impl HTMLMeterElement {
Node::reflect_node(box element, document, HTMLMeterElementBinding::Wrap)
}
}
impl HTMLMeterElementMethods for HTMLMeterElement {
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
}

View file

@ -4,11 +4,13 @@
use dom::bindings::codegen::Bindings::HTMLOutputElementBinding;
use dom::bindings::codegen::Bindings::HTMLOutputElementBinding::HTMLOutputElementMethods;
use dom::bindings::conversions::Castable;
use dom::bindings::js::Root;
use dom::document::Document;
use dom::htmlelement::HTMLElement;
use dom::htmlformelement::{FormControl, HTMLFormElement};
use dom::node::{Node, window_from_node};
use dom::nodelist::NodeList;
use dom::validitystate::ValidityState;
use util::str::DOMString;
@ -47,6 +49,11 @@ impl HTMLOutputElementMethods for HTMLOutputElement {
fn GetForm(&self) -> Option<Root<HTMLFormElement>> {
self.form_owner()
}
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
}
impl FormControl for HTMLOutputElement {}

View file

@ -2,11 +2,13 @@
* 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::bindings::codegen::Bindings::HTMLProgressElementBinding;
use dom::bindings::codegen::Bindings::HTMLProgressElementBinding::{self, HTMLProgressElementMethods};
use dom::bindings::conversions::Castable;
use dom::bindings::js::Root;
use dom::document::Document;
use dom::htmlelement::HTMLElement;
use dom::node::Node;
use dom::nodelist::NodeList;
use util::str::DOMString;
#[dom_struct]
@ -32,3 +34,10 @@ impl HTMLProgressElement {
Node::reflect_node(box element, document, HTMLProgressElementBinding::Wrap)
}
}
impl HTMLProgressElementMethods for HTMLProgressElement {
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
}

View file

@ -17,6 +17,7 @@ use dom::htmlfieldsetelement::HTMLFieldSetElement;
use dom::htmlformelement::{FormControl, HTMLFormElement};
use dom::htmloptionelement::HTMLOptionElement;
use dom::node::{Node, window_from_node};
use dom::nodelist::NodeList;
use dom::validitystate::ValidityState;
use dom::virtualmethods::VirtualMethods;
use selectors::states::*;
@ -158,6 +159,11 @@ impl HTMLSelectElementMethods for HTMLSelectElement {
"select-one".to_owned()
}
}
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
}
impl VirtualMethods for HTMLSelectElement {

View file

@ -22,6 +22,7 @@ use dom::htmlformelement::{FormControl, HTMLFormElement};
use dom::keyboardevent::KeyboardEvent;
use dom::node::{ChildrenMutation, Node, NodeDamage};
use dom::node::{document_from_node, window_from_node};
use dom::nodelist::NodeList;
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan;
use script_task::ScriptTaskEventCategory::InputEvent;
@ -205,6 +206,11 @@ impl HTMLTextAreaElementMethods for HTMLTextAreaElement {
self.force_relayout();
}
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
fn Labels(&self) -> Root<NodeList> {
self.upcast::<HTMLElement>().labels()
}
}

View file

@ -51,6 +51,10 @@ impl NodeList {
pub fn new_child_list(window: &Window, node: &Node) -> Root<NodeList> {
NodeList::new(window, NodeListType::Children(ChildrenList::new(node)))
}
pub fn empty(window: &Window) -> Root<NodeList> {
NodeList::new(window, NodeListType::Simple(vec![]))
}
}
impl NodeListMethods for NodeList {

View file

@ -25,5 +25,5 @@ interface HTMLButtonElement : HTMLElement {
//boolean reportValidity();
//void setCustomValidity(DOMString error);
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
};

View file

@ -57,7 +57,7 @@ interface HTMLInputElement : HTMLElement {
//boolean reportValidity();
//void setCustomValidity(DOMString error);
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
//void select();
// attribute unsigned long selectionStart;

View file

@ -11,5 +11,5 @@ interface HTMLMeterElement : HTMLElement {
// attribute double low;
// attribute double high;
// attribute double optimum;
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
};

View file

@ -20,5 +20,5 @@ interface HTMLOutputElement : HTMLElement {
//boolean reportValidity();
//void setCustomValidity(DOMString error);
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
};

View file

@ -8,5 +8,5 @@ interface HTMLProgressElement : HTMLElement {
// attribute double value;
// attribute double max;
//readonly attribute double position;
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
};

View file

@ -36,5 +36,5 @@ interface HTMLSelectElement : HTMLElement {
//boolean reportValidity();
//void setCustomValidity(DOMString error);
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
};

View file

@ -35,7 +35,7 @@ interface HTMLTextAreaElement : HTMLElement {
//boolean reportValidity();
//void setCustomValidity(DOMString error);
//readonly attribute NodeList labels;
readonly attribute NodeList labels;
//void select();
// attribute unsigned long selectionStart;