Bug 522601. Make inDeepTreeWalker closer to spec behavior and implement various unimplemented methods. r=sicking,sdwilsh

This commit is contained in:
Boris Zbarsky 2009-10-20 13:27:46 -04:00
parent 85b235ad3d
commit 4fe287e289
4 changed files with 343 additions and 41 deletions

View File

@ -1,3 +1,5 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et tw=79: */
/* ***** BEGIN LICENSE BLOCK ***** /* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
* *
@ -47,8 +49,8 @@
/***************************************************************************** /*****************************************************************************
* This implementation does not currently operaate according to the W3C spec. * This implementation does not currently operaate according to the W3C spec.
* So far, only parentNode() and nextNode() are implemented, and are not * In particular it does NOT handle DOM mutations during the walk. It also
* interoperable. * ignores whatToShow and the filter.
*****************************************************************************/ *****************************************************************************/
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
@ -162,78 +164,195 @@ inDeepTreeWalker::ParentNode(nsIDOMNode** _retval)
*_retval = nsnull; *_retval = nsnull;
if (!mCurrentNode) return NS_OK; if (!mCurrentNode) return NS_OK;
if (!mDOMUtils) { if (mStack.Length() == 1) {
mDOMUtils = do_GetService("@mozilla.org/inspector/dom-utils;1"); // No parent
if (!mDOMUtils) { return NS_OK;
return NS_ERROR_OUT_OF_MEMORY;
}
} }
nsresult rv = mDOMUtils->GetParentForNode(mCurrentNode, mShowAnonymousContent, // Pop off the current node, and push the new one
_retval); mStack.RemoveElementAt(mStack.Length()-1);
mCurrentNode = *_retval; DeepTreeStackItem& top = mStack.ElementAt(mStack.Length() - 1);
return rv; mCurrentNode = top.node;
top.lastIndex = 0;
NS_ADDREF(*_retval = mCurrentNode);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
inDeepTreeWalker::FirstChild(nsIDOMNode **_retval) inDeepTreeWalker::FirstChild(nsIDOMNode **_retval)
{ {
return NS_ERROR_NOT_IMPLEMENTED; *_retval = nsnull;
if (!mCurrentNode) {
return NS_OK;
}
DeepTreeStackItem& top = mStack.ElementAt(mStack.Length() - 1);
nsCOMPtr<nsIDOMNode> kid;
top.kids->Item(0, getter_AddRefs(kid));
if (!kid) {
return NS_OK;
}
top.lastIndex = 1;
PushNode(kid);
kid.forget(_retval);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
inDeepTreeWalker::LastChild(nsIDOMNode **_retval) inDeepTreeWalker::LastChild(nsIDOMNode **_retval)
{ {
return NS_ERROR_NOT_IMPLEMENTED; *_retval = nsnull;
if (!mCurrentNode) {
return NS_OK;
}
DeepTreeStackItem& top = mStack.ElementAt(mStack.Length() - 1);
nsCOMPtr<nsIDOMNode> kid;
PRUint32 length;
top.kids->GetLength(&length);
top.kids->Item(length - 1, getter_AddRefs(kid));
if (!kid) {
return NS_OK;
}
top.lastIndex = length;
PushNode(kid);
kid.forget(_retval);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
inDeepTreeWalker::PreviousSibling(nsIDOMNode **_retval) inDeepTreeWalker::PreviousSibling(nsIDOMNode **_retval)
{ {
return NS_ERROR_NOT_IMPLEMENTED; *_retval = nsnull;
if (!mCurrentNode) {
return NS_OK;
}
NS_ASSERTION(mStack.Length() > 0, "Should have things in mStack");
if (mStack.Length() == 1) {
// No previous sibling
return NS_OK;
}
DeepTreeStackItem& parent = mStack.ElementAt(mStack.Length()-2);
nsCOMPtr<nsIDOMNode> previousSibling;
parent.kids->Item(parent.lastIndex-2, getter_AddRefs(previousSibling));
if (!previousSibling) {
return NS_OK;
}
// Our mStack's topmost element is our current node. Since we're trying to
// change that to the previous sibling, pop off the current node, and push
// the new one.
mStack.RemoveElementAt(mStack.Length() - 1);
parent.lastIndex--;
PushNode(previousSibling);
previousSibling.forget(_retval);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
inDeepTreeWalker::NextSibling(nsIDOMNode **_retval) inDeepTreeWalker::NextSibling(nsIDOMNode **_retval)
{ {
return NS_ERROR_NOT_IMPLEMENTED; *_retval = nsnull;
if (!mCurrentNode) {
return NS_OK;
}
NS_ASSERTION(mStack.Length() > 0, "Should have things in mStack");
if (mStack.Length() == 1) {
// No next sibling
return NS_OK;
}
DeepTreeStackItem& parent = mStack.ElementAt(mStack.Length()-2);
nsCOMPtr<nsIDOMNode> nextSibling;
parent.kids->Item(parent.lastIndex, getter_AddRefs(nextSibling));
if (!nextSibling) {
return NS_OK;
}
// Our mStack's topmost element is our current node. Since we're trying to
// change that to the next sibling, pop off the current node, and push
// the new one.
mStack.RemoveElementAt(mStack.Length() - 1);
parent.lastIndex++;
PushNode(nextSibling);
nextSibling.forget(_retval);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
inDeepTreeWalker::PreviousNode(nsIDOMNode **_retval) inDeepTreeWalker::PreviousNode(nsIDOMNode **_retval)
{ {
return NS_ERROR_NOT_IMPLEMENTED; if (!mCurrentNode || mStack.Length() == 1) {
// Nowhere to go from here
*_retval = nsnull;
return NS_OK;
}
nsCOMPtr<nsIDOMNode> node;
PreviousSibling(getter_AddRefs(node));
if (!node) {
return ParentNode(_retval);
}
// Now we're positioned at our previous sibling. But since the DOM tree
// traversal is depth-first, the previous node is its most deeply nested last
// child. Just loop until LastChild() returns null; since the LastChild()
// call that returns null won't affect our position, we will then be
// positioned at the correct node.
while (node) {
LastChild(getter_AddRefs(node));
}
NS_ADDREF(*_retval = mCurrentNode);
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
inDeepTreeWalker::NextNode(nsIDOMNode **_retval) inDeepTreeWalker::NextNode(nsIDOMNode **_retval)
{ {
if (!mCurrentNode) return NS_OK; // First try our kids
FirstChild(_retval);
nsCOMPtr<nsIDOMNode> next;
while (1) {
DeepTreeStackItem& top = mStack.ElementAt(mStack.Length()-1);
nsCOMPtr<nsIDOMNodeList> kids = top.kids;
PRUint32 childCount;
kids->GetLength(&childCount);
if (top.lastIndex == childCount) { if (*_retval) {
mStack.RemoveElementAt(mStack.Length()-1); return NS_OK;
if (mStack.Length() == 0) { }
mCurrentNode = nsnull;
break; // Now keep trying next siblings up the parent chain, but if we
} // discover there's nothing else restore our state.
} else { #ifdef DEBUG
kids->Item(top.lastIndex++, getter_AddRefs(next)); nsIDOMNode* origCurrentNode = mCurrentNode;
PushNode(next); #endif
break; PRUint32 lastChildCallsToMake = 0;
while (1) {
NextSibling(_retval);
if (*_retval) {
return NS_OK;
} }
}
nsCOMPtr<nsIDOMNode> parent;
*_retval = next; ParentNode(getter_AddRefs(parent));
NS_IF_ADDREF(*_retval); if (!parent) {
// Nowhere else to go; we're done. Restore our state.
while (lastChildCallsToMake--) {
nsCOMPtr<nsIDOMNode> dummy;
LastChild(getter_AddRefs(dummy));
}
NS_ASSERTION(mCurrentNode == origCurrentNode,
"Didn't go back to the right node?");
*_retval = nsnull;
return NS_OK;
}
++lastChildCallsToMake;
}
NS_NOTREACHED("how did we get here?");
return NS_OK; return NS_OK;
} }

View File

@ -52,7 +52,8 @@ struct DeepTreeStackItem
{ {
nsCOMPtr<nsIDOMNode> node; nsCOMPtr<nsIDOMNode> node;
nsCOMPtr<nsIDOMNodeList> kids; nsCOMPtr<nsIDOMNodeList> kids;
PRUint32 lastIndex; PRUint32 lastIndex; // Index one bigger than the index of whatever
// kid we're currently at in |kids|.
}; };
//////////////////////////////////////////////////// ////////////////////////////////////////////////////

View File

@ -47,6 +47,7 @@ include $(topsrcdir)/config/rules.mk
_TEST_FILES =\ _TEST_FILES =\
test_bug462787.html \ test_bug462787.html \
test_bug462789.html \ test_bug462789.html \
test_bug522601.xhtml \
$(NULL) $(NULL)
libs:: $(_TEST_FILES) libs:: $(_TEST_FILES)

View File

@ -0,0 +1,181 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=522601
-->
<head>
<title>Test for Bug 522601</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<bindings xmlns="http://www.mozilla.org/xbl">
<binding id="testBinding">
<content><div><children/></div><children includes="span"/></content>
</binding>
</bindings>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=522601">Mozilla Bug 522601</a>
<p id="display" style="-moz-binding: url(#testBinding)">
<span id="s">This is some text</span>
More text
<b id="b">Even more <i id="i1">Italic</i>text<i id="i2">And more italic</i></b></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
<![CDATA[
/** Test for Bug 522601 **/
SimpleTest.waitForExplicitFinish();
function testFunc(walker, func, expectedNode, str) {
var oldCurrent = walker.currentNode;
var newNode = walker[func]();
is(newNode, expectedNode, "Unexpected node after " + str);
is(walker.currentNode, newNode ? newNode : oldCurrent,
"Unexpected current node after " + str);
}
addLoadEvent(function() {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var walkerNonAnon =
Components.classes["@mozilla.org/inspector/deep-tree-walker;1"]
.createInstance(Components.interfaces.inIDeepTreeWalker);
walkerNonAnon.init($("display"), NodeFilter.SHOW_ALL);
walkerNonAnon.showAnonymousContent = false;
is(walkerNonAnon.currentNode, $("display"), "Unexpected non-anon root");
testFunc(walkerNonAnon, "nextNode", $("s").previousSibling,
"step to some text");
testFunc(walkerNonAnon, "nextNode", $("s"), "step to span");
testFunc(walkerNonAnon, "nextNode", $("s").firstChild, "step to span text");
testFunc(walkerNonAnon, "nextNode", $("s").nextSibling, "step to more text");
testFunc(walkerNonAnon, "nextNode", $("b"), "step to bold");
testFunc(walkerNonAnon, "nextNode", $("b").firstChild, "step to bold text");
testFunc(walkerNonAnon, "nextNode", $("i1"), "step to first italic");
testFunc(walkerNonAnon, "nextNode", $("i1").firstChild,
"step to first italic text");
testFunc(walkerNonAnon, "nextNode", $("i1").nextSibling,
"step to more bold text");
testFunc(walkerNonAnon, "nextNode", $("i2"), "step to second italic");
testFunc(walkerNonAnon, "nextNode", $("i2").firstChild,
"step to second italic text");
testFunc(walkerNonAnon, "nextNode", null, "step past end");
testFunc(walkerNonAnon, "parentNode", $("i2"), "step up to second italic");
testFunc(walkerNonAnon, "parentNode", $("b"), "step up to bold");
testFunc(walkerNonAnon, "nextNode", $("b").firstChild, "step to bold text again");
testFunc(walkerNonAnon, "parentNode", $("b"), "step up to bold again");
testFunc(walkerNonAnon, "parentNode", $("display"), "step up to display");
testFunc(walkerNonAnon, "parentNode", null, "step up past root");
testFunc(walkerNonAnon, "firstChild", $("s").previousSibling,
"step firstChild to display first child");
testFunc(walkerNonAnon, "nextSibling", $("s"),
"step nextSibling to span");
testFunc(walkerNonAnon, "nextSibling", $("s").nextSibling,
"step nextSibling to more text");
testFunc(walkerNonAnon, "nextSibling", $("b"), "step nextSibling to bold");
testFunc(walkerNonAnon, "nextSibling", null, "step nextSibling past end");
testFunc(walkerNonAnon, "previousSibling", $("s").nextSibling,
"step previousSibling to more text");
testFunc(walkerNonAnon, "previousSibling", $("s"),
"step previousSibling to span");
testFunc(walkerNonAnon, "previousSibling", $("s").previousSibling,
"step previousSibling to display first child");
testFunc(walkerNonAnon, "previousSibling", null,
"step previousSibling past end");
// Move the walker over to the end
while (walkerNonAnon.nextNode()) { /* do nothing */ }
is(walkerNonAnon.currentNode, $("i2").firstChild, "unexpected last node");
testFunc(walkerNonAnon, "previousNode", $("i2"), "step back to second italic");
testFunc(walkerNonAnon, "previousNode", $("i1").nextSibling,
"step back to more bold text");
testFunc(walkerNonAnon, "previousNode", $("i1").firstChild,
"step back to first italic text");
testFunc(walkerNonAnon, "previousNode", $("i1"), "step back to first italic");
testFunc(walkerNonAnon, "previousNode", $("b").firstChild,
"step back to bold text");
testFunc(walkerNonAnon, "previousNode", $("b"), "step back to bold");
testFunc(walkerNonAnon, "previousNode", $("s").nextSibling, "step back to more text");
testFunc(walkerNonAnon, "previousNode", $("s").firstChild, "step back to span text");
testFunc(walkerNonAnon, "previousNode", $("s"), "step back to span");
testFunc(walkerNonAnon, "previousNode", $("s").previousSibling,
"step back to some text");
testFunc(walkerNonAnon, "previousNode", $("display"),
"step back to root");
testFunc(walkerNonAnon, "previousNode", null,
"step back past root");
var anonDiv = document.getAnonymousNodes($("display"))[0];
var walkerAnon =
Components.classes["@mozilla.org/inspector/deep-tree-walker;1"]
.createInstance(Components.interfaces.inIDeepTreeWalker);
walkerAnon.showAnonymousContent = true;
walkerAnon.init($("display"), NodeFilter.SHOW_ALL);
is(walkerAnon.currentNode, $("display"), "Unexpected anon root");
testFunc(walkerAnon, "nextNode", anonDiv,
"step to anonymous div");
testFunc(walkerAnon, "nextNode", $("s").previousSibling,
"step to some text (anon)");
testFunc(walkerAnon, "nextNode", $("s").nextSibling, "step to more text (anon)");
testFunc(walkerAnon, "nextNode", $("b"), "step to bold (anon)");
testFunc(walkerAnon, "nextNode", $("b").firstChild, "step to bold text (anon)");
testFunc(walkerAnon, "nextNode", $("i1"), "step to first italic (anon)");
testFunc(walkerAnon, "nextNode", $("i1").firstChild,
"step to first italic text (anon)");
testFunc(walkerAnon, "nextNode", $("i1").nextSibling,
"step to more bold text (anon)");
testFunc(walkerAnon, "nextNode", $("i2"), "step to second italic (anon)");
testFunc(walkerAnon, "nextNode", $("i2").firstChild,
"step to second italic text (anon)");
testFunc(walkerAnon, "nextNode", $("s"), "step to span (anon)");
testFunc(walkerAnon, "nextNode", $("s").firstChild, "step to span text (anon)");
testFunc(walkerAnon, "nextNode", null, "step past end (anon)");
testFunc(walkerAnon, "parentNode", $("s"), "step up to span (anon)");
testFunc(walkerAnon, "parentNode", $("display"), "step up to display (anon)");
testFunc(walkerAnon, "nextNode", anonDiv, "step to anonymous div again");
testFunc(walkerAnon, "parentNode", $("display"),
"step up to display again (anon)");
testFunc(walkerAnon, "parentNode", null, "step up past root (anon)");
testFunc(walkerAnon, "firstChild", anonDiv,
"step firstChild to display first child (anon)");
testFunc(walkerAnon, "nextSibling", $("s"),
"step nextSibling to span (anon)");
testFunc(walkerAnon, "nextSibling", null, "step nextSibling past end (anon)");
testFunc(walkerAnon, "previousSibling", anonDiv,
"step previousSibling to anonymous div");
testFunc(walkerAnon, "previousSibling", null, "step previousSibling past end (anon)");
// Move the walker over to the end
while (walkerAnon.nextNode()) { /* do nothing */ }
testFunc(walkerAnon, "previousNode", $("s"), "step back to span (anon)");
testFunc(walkerAnon, "previousNode", $("i2").firstChild,
"step back to second italic text (anon)");
testFunc(walkerAnon, "previousNode", $("i2"), "step back to second italic (anon)");
testFunc(walkerAnon, "previousNode", $("i1").nextSibling,
"step back to more bold text (anon)");
testFunc(walkerAnon, "previousNode", $("i1").firstChild,
"step back to first italic text (anon)");
testFunc(walkerAnon, "previousNode", $("i1"), "step back to first italic (anon)");
testFunc(walkerAnon, "previousNode", $("b").firstChild, "step back to bold text (anon)");
testFunc(walkerAnon, "previousNode", $("b"), "step back to bold (anon)");
testFunc(walkerAnon, "previousNode", $("s").nextSibling, "step back to more text (anon)");
testFunc(walkerAnon, "previousNode", $("s").previousSibling,
"step back to some text (anon)");
testFunc(walkerAnon, "previousNode", anonDiv,
"step back to anonymous div");
testFunc(walkerAnon, "previousNode", $("display"), "step back to root (anon)");
testFunc(walkerAnon, "previousNode", null, "step back past root (anon)");
SimpleTest.finish();
});
]]>
</script>
</pre>
</body>
</html>