Create an almost-correct form_owner() for convenience

This commit is contained in:
Manish Goregaokar 2014-10-11 16:44:48 +05:30
parent 834df4e211
commit c92d58980c
2 changed files with 32 additions and 2 deletions

View file

@ -410,3 +410,7 @@ impl<'a> FormSubmitter<'a> {
}
}
}
pub trait FormOwner<'a> {
fn form_owner(self) -> Option<JSRef<'a, HTMLFormElement>>;
}

View file

@ -8,16 +8,17 @@ use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, HTMLInputElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, HTMLFormElementCast, HTMLInputElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{HTMLInputElementDerived, HTMLFieldSetElementDerived};
use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable, ResultRootable};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::attr::{AttrHelpers};
use dom::document::{Document, DocumentHelpers};
use dom::element::{AttributeHandlers, Element, HTMLInputElementTypeId};
use dom::element::{AttributeHandlers, Element, HTMLInputElementTypeId };
use dom::event::Event;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::htmlformelement::{FormOwner, HTMLFormElement};
use dom::node::{DisabledStateHelpers, Node, NodeHelpers, ElementNodeTypeId, document_from_node};
use dom::virtualmethods::VirtualMethods;
@ -380,3 +381,28 @@ impl Reflectable for HTMLInputElement {
self.htmlelement.reflector()
}
}
impl<'a> FormOwner<'a> for JSRef<'a, HTMLInputElement> {
// FIXME: This is wrong (https://github.com/servo/servo/issues/3553)
// but we need html5ever to do it correctly
fn form_owner(self) -> Option<JSRef<'a, HTMLFormElement>> {
// https://html.spec.whatwg.org/multipage/forms.html#reset-the-form-owner
let elem: JSRef<Element> = ElementCast::from_ref(self);
let owner = elem.get_string_attribute("owner");
if !owner.is_empty() {
let doc = document_from_node(self).root();
let owner = doc.GetElementById(owner).root();
match owner {
Some(o) => {
let maybe_form: Option<JSRef<HTMLFormElement>> = HTMLFormElementCast::to_ref(*o);
if maybe_form.is_some() {
return maybe_form;
}
},
_ => ()
}
}
let node: JSRef<Node> = NodeCast::from_ref(self);
node.ancestors().filter_map(|a| HTMLFormElementCast::to_ref(a)).next().clone()
}
}