implement previousElementSibling and nextElementSibling

This commit is contained in:
Vincent Sanders 2012-11-18 12:59:10 +00:00
parent bec8711528
commit 99d3633e03
3 changed files with 77 additions and 2 deletions

View File

@ -123,7 +123,67 @@ getter lastElementChild %{
%}
getter previousElementSibling %{
%}
dom_node *element;
dom_exception exc;
dom_node_type node_type;
dom_node *sib_node;
exc = dom_node_get_previous_sibling(private->node, &element);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
while (element != NULL) {
exc = dom_node_get_node_type(element, &node_type);
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
/* found it */
jsret = jsapi_new_HTMLElement(cx,
NULL,
NULL,
(dom_element *)element,
private->htmlc);
break;
}
exc = dom_node_get_previous_sibling(element, &sib_node);
dom_node_unref(element);
if (exc == DOM_NO_ERR) {
element = sib_node;
} else {
element = NULL;
}
}
%}
getter nextElementSibling %{
%}
dom_node *element;
dom_exception exc;
dom_node_type node_type;
dom_node *sib_node;
exc = dom_node_get_next_sibling(private->node, &element);
if (exc != DOM_NO_ERR) {
return JS_FALSE;
}
while (element != NULL) {
exc = dom_node_get_node_type(element, &node_type);
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
/* found it */
jsret = jsapi_new_HTMLElement(cx,
NULL,
NULL,
(dom_element *)element,
private->htmlc);
break;
}
exc = dom_node_get_next_sibling(element, &sib_node);
dom_node_unref(element);
if (exc == DOM_NO_ERR) {
element = sib_node;
} else {
element = NULL;
}
}
%}

View File

@ -0,0 +1,14 @@
<html>
<head>
<title>DOM previousElementSibling and nextElementSibling reference</title>
<!-- comment node should be skipped -->
<link rel="stylesheet" type="text/css" href="tst.css">
</head>
<body>
<h1>DOM previousElementSibling and nextElementSibling reference</h1>
<!-- comment node should be skipped -->
<p><b>head.lastElementChild.previousElementSibling:</b> <script>document.write(document.head.lastElementChild.previousElementSibling.textContent);</script></p>
<!-- comment node should be skipped -->
<p><b>body.firstElementChild.nextElementSibling:</b> <script>document.write(document.body.firstElementChild.nextElementSibling.textContent);</script></p>
</body>
</html>

View File

@ -26,6 +26,7 @@
<ul>
<li><a href="dom-element-firstElementChild.html">firstElementChild</a></li>
<li><a href="dom-element-lastElementChild.html">lastElementChild</a></li>
<li><a href="dom-element-next_prev_ElementSibling.html">previousElementSibling nextElementSibling</a></li>
<li><a href="doc-dom2.html">getElementById</a></li>
<li><a href="dom-getElementsByTagName.html">getElementsByTagName</a></li>