Auto merge of #8178 - frewsxcv:label, r=nox

Implement a couple attributes for <label> elements



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8178)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-10-26 09:51:56 -05:00
commit af8a1cb8a0
9 changed files with 81 additions and 199 deletions

View file

@ -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())
}
}

View file

@ -296,6 +296,26 @@ impl HTMLElement {
let local_name = Atom::from_slice(&to_snake_case(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 {

View file

@ -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,54 @@ impl HTMLLabelElementMethods for HTMLLabelElement {
fn GetForm(&self) -> Option<Root<HTMLFormElement>> {
self.form_owner()
}
// https://html.spec.whatwg.org/multipage/#dom-label-htmlfor
make_getter!(HtmlFor, "for");
// https://html.spec.whatwg.org/multipage/#dom-label-htmlfor
make_atomic_setter!(SetHtmlFor, "for");
// 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 {}

View file

@ -25,6 +25,7 @@ use dom::htmlheadelement::HTMLHeadElement;
use dom::htmliframeelement::HTMLIFrameElement;
use dom::htmlimageelement::HTMLImageElement;
use dom::htmlinputelement::HTMLInputElement;
use dom::htmllabelelement::HTMLLabelElement;
use dom::htmllinkelement::HTMLLinkElement;
use dom::htmlmetaelement::HTMLMetaElement;
use dom::htmlobjectelement::HTMLObjectElement;
@ -164,6 +165,9 @@ pub fn vtable_for(node: &Node) -> &VirtualMethods {
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLInputElement)) => {
node.downcast::<HTMLInputElement>().unwrap() as &VirtualMethods
}
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLabelElement)) => {
node.downcast::<HTMLLabelElement>().unwrap() as &VirtualMethods
}
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLinkElement)) => {
node.downcast::<HTMLLinkElement>().unwrap() as &VirtualMethods
}

View file

@ -6,6 +6,6 @@
// https://html.spec.whatwg.org/multipage/#htmllabelelement
interface HTMLLabelElement : HTMLElement {
readonly attribute HTMLFormElement? form;
// attribute DOMString htmlFor;
//readonly attribute HTMLElement? control;
attribute DOMString htmlFor;
readonly attribute HTMLElement? control;
};

View file

@ -4881,18 +4881,6 @@
[HTMLLabelElement interface: existence and properties of interface object]
expected: FAIL
[HTMLLabelElement interface: attribute htmlFor]
expected: FAIL
[HTMLLabelElement interface: attribute control]
expected: FAIL
[HTMLLabelElement interface: document.createElement("label") must inherit property "htmlFor" with the proper type (1)]
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]
expected: FAIL

View file

@ -2913,135 +2913,6 @@
[label.tabIndex: IDL set to -2147483648 followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): typeof IDL attribute]
expected: FAIL
[label.htmlFor (<label for>): IDL get with DOM attribute unset]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to "" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to " \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07 \\b\\t\\n\\v\\f\\r\\x0e\\x0f \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17 \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f foo " followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to undefined followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to 7 followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to 1.5 followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to true followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to false followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to object "[object Object\]" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to NaN followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to Infinity followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to -Infinity followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to "\\0" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to null followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to object "test-toString" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): setAttribute() to object "test-valueOf" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to "" followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to " \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07 \\b\\t\\n\\v\\f\\r\\x0e\\x0f \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17 \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f foo " followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to undefined followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to undefined followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to 7 followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to 7 followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to 1.5 followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to 1.5 followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to true followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to true followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to false followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to false followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to object "[object Object\]" followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to object "[object Object\]" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to NaN followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to NaN followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to Infinity followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to Infinity followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to -Infinity followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to -Infinity followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to "\\0" followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to null followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to null followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to object "test-toString" followed by getAttribute()]
expected: FAIL
[label.htmlFor (<label for>): IDL set to object "test-toString" followed by IDL get]
expected: FAIL
[label.htmlFor (<label for>): IDL set to object "test-valueOf" followed by IDL get]
expected: FAIL
[label.itemScope: typeof IDL attribute]
expected: FAIL

View file

@ -1,23 +1,8 @@
[label-attributes.html]
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.]
expected: FAIL
[The 'for' attribute is an empty string.]
expected: FAIL
[A form control has multiple labels.]
expected: FAIL
@ -27,6 +12,3 @@
[A form control has no label 2.]
expected: FAIL
[A label's htmlFor attribute must reflect the for content attribute]
expected: FAIL

View file

@ -1,41 +1,5 @@
[labelable-elements.html]
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]
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