Skip to content

Commit

Permalink
Implement HTMLTableElement.caption
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnirp committed Aug 16, 2014
1 parent fe3b62e commit 795fd68
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/components/script/dom/htmltableelement.rs
Expand Up @@ -3,14 +3,18 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::HTMLTableElementBinding;
use dom::bindings::codegen::InheritTypes::HTMLTableElementDerived;
use dom::bindings::codegen::Bindings::HTMLTableElementBinding::HTMLTableElementMethods;
use dom::bindings::codegen::InheritTypes::{HTMLTableElementDerived, NodeCast, HTMLTableCaptionElementCast};
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document;
use dom::element::HTMLTableCaptionElementTypeId;
use dom::element::HTMLTableElementTypeId;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::node::{Node, ElementNodeTypeId};
use dom::htmltablecaptionelement::HTMLTableCaptionElement;
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
use servo_util::str::DOMString;

#[deriving(Encodable)]
Expand Down Expand Up @@ -42,3 +46,36 @@ impl Reflectable for HTMLTableElement {
self.htmlelement.reflector()
}
}

impl<'a> HTMLTableElementMethods for JSRef<'a, HTMLTableElement> {

// http://www.whatwg.org/html/#dom-table-caption
fn GetCaption(&self) -> Option<Temporary<HTMLTableCaptionElement>> {
let node: &JSRef<Node> = NodeCast::from_ref(self);
node.children().find(|child| {
child.type_id() == ElementNodeTypeId(HTMLTableCaptionElementTypeId)
}).map(|node| {
Temporary::from_rooted(HTMLTableCaptionElementCast::to_ref(&node).unwrap())
})
}

// http://www.whatwg.org/html/#dom-table-caption
fn SetCaption(&self, new_caption: Option<JSRef<HTMLTableCaptionElement>>) {
let node: &JSRef<Node> = NodeCast::from_ref(self);
let old_caption = self.GetCaption();

match old_caption {
Some(htmlelem) => {
let htmlelem_jsref = &*htmlelem.root();
let old_caption_node: &JSRef<Node> = NodeCast::from_ref(htmlelem_jsref);
assert!(node.RemoveChild(old_caption_node).is_ok());
}
None => ()
}

new_caption.map(|caption| {
let new_caption_node: &JSRef<Node> = NodeCast::from_ref(&caption);
assert!(node.AppendChild(new_caption_node).is_ok());
});
}
}
2 changes: 1 addition & 1 deletion src/components/script/dom/webidls/HTMLTableElement.webidl
Expand Up @@ -5,7 +5,7 @@

// http://www.whatwg.org/html/#htmltableelement
interface HTMLTableElement : HTMLElement {
// attribute HTMLTableCaptionElement? caption;
attribute HTMLTableCaptionElement? caption;
//HTMLElement createCaption();
//void deleteCaption();
// attribute HTMLTableSectionElement? tHead;
Expand Down
23 changes: 23 additions & 0 deletions src/test/content/test_caption.html
@@ -0,0 +1,23 @@
<html>
<head>
<script src="harness.js"></script>
</head>
<table id="t">
<caption id="tcaption">old caption</caption>
</table>
<script>
var t = document.getElementById("t");
var tcaption = document.getElementById("tcaption");
is(t.caption, tcaption);
is(t.caption.innerHTML, "old caption");

var newCaption = document.createElement("caption");
newCaption.innerHTML = "new caption";

t.caption = newCaption;
is(newCaption.parentNode, t);
is(t.caption, newCaption);

finish();
</script>
</html>

0 comments on commit 795fd68

Please sign in to comment.