Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / accessible / src / base / nsAccessibleTreeWalker.cpp
blobf2a9770c37554e94fb8f9400a88266a47a45ade2
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Original Author: Aaron Leventhal (aaronl@netscape.com)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsAccessibleTreeWalker.h"
40 #include "nsAccessibilityAtoms.h"
41 #include "nsAccessNode.h"
42 #include "nsIServiceManager.h"
43 #include "nsIContent.h"
44 #include "nsIDOMXULElement.h"
45 #include "nsIPresShell.h"
46 #include "nsIFrame.h"
47 #include "nsWeakReference.h"
49 nsAccessibleTreeWalker::nsAccessibleTreeWalker(nsIWeakReference* aPresShell, nsIDOMNode* aNode, PRBool aWalkAnonContent):
50 mWeakShell(aPresShell),
51 mAccService(do_GetService("@mozilla.org/accessibilityService;1")),
52 mWalkAnonContent(aWalkAnonContent)
54 mState.domNode = aNode;
55 mState.prevState = nsnull;
56 mState.siblingIndex = eSiblingsUninitialized;
57 mState.siblingList = nsnull;
58 mState.isHidden = false;
59 mState.frame = nsnull;
61 MOZ_COUNT_CTOR(nsAccessibleTreeWalker);
64 nsAccessibleTreeWalker::~nsAccessibleTreeWalker()
66 // Clear state stack from memory
67 while (NS_SUCCEEDED(PopState()))
68 /* do nothing */ ;
69 MOZ_COUNT_DTOR(nsAccessibleTreeWalker);
72 void nsAccessibleTreeWalker::GetKids(nsIDOMNode *aParentNode)
74 nsCOMPtr<nsIContent> parentContent(do_QueryInterface(aParentNode));
75 if (!parentContent || !parentContent->IsNodeOfType(nsINode::eHTML)) {
76 mState.frame = nsnull; // Don't walk frames in non-HTML content, just walk the DOM.
79 PushState();
80 UpdateFrame(PR_TRUE);
82 // Walk frames? UpdateFrame() sets this when it sees anonymous frames
83 if (mState.siblingIndex == eSiblingsWalkFrames) {
84 return;
87 // Walk anonymous content? Not currently used for HTML -- anonymous content there uses frame walking
88 mState.siblingIndex = 0; // Indicates our index into the sibling list
89 if (parentContent) {
90 if (mWalkAnonContent) {
91 // Walk anonymous content
92 nsIDocument* doc = parentContent->GetOwnerDoc();
93 if (doc) {
94 // returns null if no anon nodes
95 doc->GetXBLChildNodesFor(parentContent,
96 getter_AddRefs(mState.siblingList));
99 if (!mState.siblingList) {
100 // Walk normal DOM. Just use nsIContent -- it doesn't require
101 // the mallocs that GetChildNodes() needs
102 //aParentNode->GetChildNodes(getter_AddRefs(mState.siblingList));
103 mState.parentContent = parentContent;
104 mState.domNode = do_QueryInterface(parentContent->GetChildAt(0 /* 0 == mState.siblingIndex */));
105 return;
108 else {
109 // We're on document node, that's why we could not QI to nsIContent.
110 // So, use nsIDOMNodeList method to walk content.
111 aParentNode->GetChildNodes(getter_AddRefs(mState.siblingList));
112 if (!mState.siblingList) {
113 return;
117 mState.siblingList->Item(0 /* 0 == mState.siblingIndex */, getter_AddRefs(mState.domNode));
120 NS_IMETHODIMP nsAccessibleTreeWalker::PopState()
122 nsIFrame *frameParent = mState.frame? mState.frame->GetParent(): nsnull;
123 if (mState.prevState) {
124 WalkState *toBeDeleted = mState.prevState;
125 mState = *mState.prevState; // deep copy
126 mState.isHidden = PR_FALSE; // If we were in a child, the parent wasn't hidden
127 if (!mState.frame) {
128 mState.frame = frameParent;
130 delete toBeDeleted;
131 return NS_OK;
133 ClearState();
134 mState.frame = frameParent;
135 mState.isHidden = PR_FALSE;
136 return NS_ERROR_FAILURE;
139 void nsAccessibleTreeWalker::ClearState()
141 mState.siblingList = nsnull;
142 mState.parentContent = nsnull;
143 mState.accessible = nsnull;
144 mState.domNode = nsnull;
145 mState.siblingIndex = eSiblingsUninitialized;
148 NS_IMETHODIMP nsAccessibleTreeWalker::PushState()
150 // Duplicate mState and put right before end; reset mState; make mState the new end of the stack
151 WalkState* nextToLastState= new WalkState();
152 if (!nextToLastState)
153 return NS_ERROR_OUT_OF_MEMORY;
154 *nextToLastState = mState; // Deep copy - copy contents of struct to new state that will be added to end of our stack
155 ClearState();
156 mState.prevState = nextToLastState; // Link to previous state
157 return NS_OK;
160 void nsAccessibleTreeWalker::GetNextDOMNode()
162 // Get next DOM node
163 if (mState.parentContent) {
164 mState.domNode = do_QueryInterface(mState.parentContent->GetChildAt(++mState.siblingIndex));
166 else if (mState.siblingIndex == eSiblingsWalkFrames) {
167 if (mState.frame) {
168 mState.domNode = do_QueryInterface(mState.frame->GetContent());
169 } else {
170 mState.domNode = nsnull;
173 else {
174 mState.siblingList->Item(++mState.siblingIndex, getter_AddRefs(mState.domNode));
178 NS_IMETHODIMP nsAccessibleTreeWalker::GetNextSibling()
180 // Make sure mState.prevState and mState.siblingIndex are initialized so we can walk forward
181 NS_ASSERTION(mState.prevState && mState.siblingIndex != eSiblingsUninitialized,
182 "Error - GetNextSibling() only works after a GetFirstChild(), so we must have a prevState.");
183 mState.accessible = nsnull;
185 while (PR_TRUE) {
186 // Get next frame
187 UpdateFrame(PR_FALSE);
188 GetNextDOMNode();
190 if (!mState.domNode) { // Done with current siblings
191 PopState(); // Use parent - go up in stack. Can always pop state because we have to start with a GetFirstChild().
192 if (!mState.prevState) {
193 mState.accessible = nsnull;
194 break; // Back to original accessible that we did GetFirstChild() from
197 else if ((mState.domNode != mState.prevState->domNode && GetAccessible()) ||
198 NS_SUCCEEDED(GetFirstChild())) {
199 return NS_OK; // if next is accessible, use it
202 return NS_ERROR_FAILURE;
205 NS_IMETHODIMP nsAccessibleTreeWalker::GetFirstChild()
207 mState.accessible = nsnull;
208 if (mState.isHidden || !mState.domNode) {
209 return NS_ERROR_FAILURE;
212 nsCOMPtr<nsIDOMNode> parent(mState.domNode);
213 GetKids(parent); // Side effects change our state (mState)
215 // Recursive loop: depth first search for first accessible child
216 while (mState.domNode) {
217 if ((mState.domNode != parent && GetAccessible()) || NS_SUCCEEDED(GetFirstChild()))
218 return NS_OK;
219 UpdateFrame(PR_FALSE);
220 GetNextDOMNode();
223 PopState(); // Return to previous state
224 return NS_ERROR_FAILURE;
227 void nsAccessibleTreeWalker::UpdateFrame(PRBool aTryFirstChild)
229 if (!mState.frame) {
230 return;
232 if (aTryFirstChild) {
233 nsIContent *containerContent = mState.frame->GetContent();
234 mState.frame = mState.frame->GetFirstChild(nsnull);
235 // temporary workaround for Bug 359210. We never want to walk frames.
236 // Aaron Leventhal will refix :before and :after content later without walking frames.
237 #if 0
238 if (mState.frame && mState.siblingIndex < 0) {
239 // Container frames can contain generated content frames from
240 // :before and :after style rules, so we walk their frame trees
241 // instead of content trees
242 // XXX Walking the frame tree doesn't get us Aural CSS nodes, e.g.
243 // @media screen { display: none; }
244 // Asking the style system might be better (with ProbePseudoStyleFor(),
245 // except that we need to ask only for those display types that support
246 // :before and :after (which roughly means non-replaced elements)
247 // Here's some code to see if there is an :after rule for an element
248 // nsRefPtr<nsStyleContext> pseudoContext;
249 // nsStyleContext *styleContext = primaryFrame->GetStyleContext();
250 // if (aContent) {
251 // pseudoContext = presContext->StyleSet()->
252 // ProbePseudoStyleFor(content, nsAccessibilityAtoms::after, aStyleContext);
253 mState.domNode = do_QueryInterface(mState.frame->GetContent());
254 mState.siblingIndex = eSiblingsWalkFrames;
256 #endif
257 // Special case: <input type="file">
258 // We should still need to walk frames inside the file control frame
259 // This special case may turn into a more general rule after Firefox 3,
260 // if HTML 5 controls use nsIAnonymousContentCreator
261 if (containerContent->Tag() == nsAccessibilityAtoms::input &&
262 containerContent->AttrValueIs(kNameSpaceID_None, nsAccessibilityAtoms::type,
263 NS_LITERAL_STRING("file"), eIgnoreCase) &&
264 mState.frame && mState.siblingIndex < 0) {
265 mState.domNode = do_QueryInterface(mState.frame->GetContent());
266 mState.siblingIndex = eSiblingsWalkFrames;
269 else {
270 mState.frame = mState.frame->GetNextSibling();
275 * If the DOM node's frame has an accessible or the DOMNode
276 * itself implements nsIAccessible return it.
278 PRBool nsAccessibleTreeWalker::GetAccessible()
280 if (!mAccService) {
281 return PR_FALSE;
284 mState.accessible = nsnull;
285 nsCOMPtr<nsIPresShell> presShell(do_QueryReferent(mWeakShell));
287 mAccService->GetAccessible(mState.domNode, presShell, mWeakShell,
288 &mState.frame, &mState.isHidden,
289 getter_AddRefs(mState.accessible));
290 return mState.accessible ? PR_TRUE : PR_FALSE;