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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Aaron Leventhal (aaronl@netscape.com)
24 : * Kyle Yuan (kyle.yuan@sun.com)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsHTMLTextAccessible.h"
41 :
42 : #include "nsDocAccessible.h"
43 : #include "nsAccUtils.h"
44 : #include "nsTextEquivUtils.h"
45 : #include "Relation.h"
46 : #include "Role.h"
47 : #include "States.h"
48 :
49 : #include "nsIAccessibleRelation.h"
50 : #include "nsIFrame.h"
51 : #include "nsPresContext.h"
52 : #include "nsBlockFrame.h"
53 : #include "nsISelection.h"
54 : #include "nsISelectionController.h"
55 : #include "nsComponentManagerUtils.h"
56 :
57 : using namespace mozilla::a11y;
58 :
59 : ////////////////////////////////////////////////////////////////////////////////
60 : // nsHTMLTextAccessible
61 : ////////////////////////////////////////////////////////////////////////////////
62 :
63 0 : nsHTMLTextAccessible::
64 : nsHTMLTextAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
65 0 : nsTextAccessibleWrap(aContent, aDoc)
66 : {
67 0 : }
68 :
69 0 : NS_IMPL_ISUPPORTS_INHERITED0(nsHTMLTextAccessible, nsTextAccessible)
70 :
71 : NS_IMETHODIMP
72 0 : nsHTMLTextAccessible::GetName(nsAString& aName)
73 : {
74 : // Text node, ARIA can't be used.
75 0 : aName = mText;
76 0 : return NS_OK;
77 : }
78 :
79 : role
80 0 : nsHTMLTextAccessible::NativeRole()
81 : {
82 0 : nsIFrame *frame = GetFrame();
83 : // Don't return on null frame -- we still return a role
84 : // after accessible is shutdown/DEFUNCT
85 0 : if (frame && frame->IsGeneratedContentFrame())
86 0 : return roles::STATICTEXT;
87 :
88 0 : return nsTextAccessible::NativeRole();
89 : }
90 :
91 : PRUint64
92 0 : nsHTMLTextAccessible::NativeState()
93 : {
94 0 : PRUint64 state = nsTextAccessible::NativeState();
95 :
96 0 : nsDocAccessible* docAccessible = Document();
97 0 : if (docAccessible) {
98 0 : PRUint64 docState = docAccessible->State();
99 0 : if (0 == (docState & states::EDITABLE)) {
100 0 : state |= states::READONLY; // Links not focusable in editor
101 : }
102 : }
103 :
104 0 : return state;
105 : }
106 :
107 : nsresult
108 0 : nsHTMLTextAccessible::GetAttributesInternal(nsIPersistentProperties *aAttributes)
109 : {
110 0 : if (NativeRole() == roles::STATICTEXT) {
111 0 : nsAutoString oldValueUnused;
112 0 : aAttributes->SetStringProperty(NS_LITERAL_CSTRING("auto-generated"),
113 0 : NS_LITERAL_STRING("true"), oldValueUnused);
114 : }
115 :
116 0 : return NS_OK;
117 : }
118 :
119 :
120 : ////////////////////////////////////////////////////////////////////////////////
121 : // nsHTMLHRAccessible
122 : ////////////////////////////////////////////////////////////////////////////////
123 :
124 0 : nsHTMLHRAccessible::
125 : nsHTMLHRAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
126 0 : nsLeafAccessible(aContent, aDoc)
127 : {
128 0 : }
129 :
130 : role
131 0 : nsHTMLHRAccessible::NativeRole()
132 : {
133 0 : return roles::SEPARATOR;
134 : }
135 :
136 :
137 : ////////////////////////////////////////////////////////////////////////////////
138 : // nsHTMLBRAccessible
139 : ////////////////////////////////////////////////////////////////////////////////
140 :
141 0 : nsHTMLBRAccessible::
142 : nsHTMLBRAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
143 0 : nsLeafAccessible(aContent, aDoc)
144 : {
145 0 : }
146 :
147 : role
148 0 : nsHTMLBRAccessible::NativeRole()
149 : {
150 0 : return roles::WHITESPACE;
151 : }
152 :
153 : PRUint64
154 0 : nsHTMLBRAccessible::NativeState()
155 : {
156 0 : return states::READONLY;
157 : }
158 :
159 : nsresult
160 0 : nsHTMLBRAccessible::GetNameInternal(nsAString& aName)
161 : {
162 0 : aName = static_cast<PRUnichar>('\n'); // Newline char
163 0 : return NS_OK;
164 : }
165 :
166 : ////////////////////////////////////////////////////////////////////////////////
167 : // nsHTMLLabelAccessible
168 : ////////////////////////////////////////////////////////////////////////////////
169 :
170 0 : nsHTMLLabelAccessible::
171 : nsHTMLLabelAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
172 0 : nsHyperTextAccessibleWrap(aContent, aDoc)
173 : {
174 0 : }
175 :
176 0 : NS_IMPL_ISUPPORTS_INHERITED0(nsHTMLLabelAccessible, nsHyperTextAccessible)
177 :
178 : nsresult
179 0 : nsHTMLLabelAccessible::GetNameInternal(nsAString& aName)
180 : {
181 0 : return nsTextEquivUtils::GetNameFromSubtree(this, aName);
182 : }
183 :
184 : role
185 0 : nsHTMLLabelAccessible::NativeRole()
186 : {
187 0 : return roles::LABEL;
188 : }
189 :
190 : ////////////////////////////////////////////////////////////////////////////////
191 : // nsHTMLOuputAccessible
192 : ////////////////////////////////////////////////////////////////////////////////
193 :
194 0 : nsHTMLOutputAccessible::
195 : nsHTMLOutputAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
196 0 : nsHyperTextAccessibleWrap(aContent, aDoc)
197 : {
198 0 : }
199 :
200 0 : NS_IMPL_ISUPPORTS_INHERITED0(nsHTMLOutputAccessible, nsHyperTextAccessible)
201 :
202 : Relation
203 0 : nsHTMLOutputAccessible::RelationByType(PRUint32 aType)
204 : {
205 0 : Relation rel = nsAccessibleWrap::RelationByType(aType);
206 0 : if (aType == nsIAccessibleRelation::RELATION_CONTROLLED_BY)
207 0 : rel.AppendIter(new IDRefsIterator(mContent, nsGkAtoms::_for));
208 :
209 : return rel;
210 : }
211 :
212 : role
213 0 : nsHTMLOutputAccessible::NativeRole()
214 : {
215 0 : return roles::SECTION;
216 : }
217 :
218 : nsresult
219 0 : nsHTMLOutputAccessible::GetAttributesInternal(nsIPersistentProperties* aAttributes)
220 : {
221 0 : nsresult rv = nsAccessibleWrap::GetAttributesInternal(aAttributes);
222 0 : NS_ENSURE_SUCCESS(rv, rv);
223 :
224 : nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::live,
225 0 : NS_LITERAL_STRING("polite"));
226 :
227 0 : return NS_OK;
228 : }
229 :
230 :
231 : ////////////////////////////////////////////////////////////////////////////////
232 : // nsHTMLLIAccessible
233 : ////////////////////////////////////////////////////////////////////////////////
234 :
235 0 : nsHTMLLIAccessible::
236 : nsHTMLLIAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
237 0 : nsHyperTextAccessibleWrap(aContent, aDoc), mBullet(nsnull)
238 : {
239 0 : mFlags |= eHTMLListItemAccessible;
240 :
241 0 : nsBlockFrame* blockFrame = do_QueryFrame(GetFrame());
242 0 : if (blockFrame && blockFrame->HasBullet()) {
243 0 : mBullet = new nsHTMLListBulletAccessible(mContent, mDoc);
244 0 : if (!Document()->BindToDocument(mBullet, nsnull))
245 0 : mBullet = nsnull;
246 : }
247 0 : }
248 :
249 0 : NS_IMPL_ISUPPORTS_INHERITED0(nsHTMLLIAccessible, nsHyperTextAccessible)
250 :
251 : void
252 0 : nsHTMLLIAccessible::Shutdown()
253 : {
254 0 : mBullet = nsnull;
255 :
256 0 : nsHyperTextAccessibleWrap::Shutdown();
257 0 : }
258 :
259 : role
260 0 : nsHTMLLIAccessible::NativeRole()
261 : {
262 0 : return roles::LISTITEM;
263 : }
264 :
265 : PRUint64
266 0 : nsHTMLLIAccessible::NativeState()
267 : {
268 0 : return nsHyperTextAccessibleWrap::NativeState() | states::READONLY;
269 : }
270 :
271 0 : NS_IMETHODIMP nsHTMLLIAccessible::GetBounds(PRInt32 *x, PRInt32 *y, PRInt32 *width, PRInt32 *height)
272 : {
273 0 : nsresult rv = nsAccessibleWrap::GetBounds(x, y, width, height);
274 0 : if (NS_FAILED(rv) || !mBullet)
275 0 : return rv;
276 :
277 : PRInt32 bulletX, bulletY, bulletWidth, bulletHeight;
278 0 : rv = mBullet->GetBounds(&bulletX, &bulletY, &bulletWidth, &bulletHeight);
279 0 : NS_ENSURE_SUCCESS(rv, rv);
280 :
281 0 : *x = bulletX; // Move x coordinate of list item over to cover bullet as well
282 0 : *width += bulletWidth;
283 0 : return NS_OK;
284 : }
285 :
286 : ////////////////////////////////////////////////////////////////////////////////
287 : // nsHTMLLIAccessible: public
288 :
289 : void
290 0 : nsHTMLLIAccessible::UpdateBullet(bool aHasBullet)
291 : {
292 0 : if (aHasBullet == !!mBullet) {
293 0 : NS_NOTREACHED("Bullet and accessible are in sync already!");
294 0 : return;
295 : }
296 :
297 0 : nsDocAccessible* document = Document();
298 0 : if (aHasBullet) {
299 0 : mBullet = new nsHTMLListBulletAccessible(mContent, mDoc);
300 0 : if (document->BindToDocument(mBullet, nsnull)) {
301 0 : InsertChildAt(0, mBullet);
302 : }
303 : } else {
304 0 : RemoveChild(mBullet);
305 0 : document->UnbindFromDocument(mBullet);
306 0 : mBullet = nsnull;
307 : }
308 :
309 : // XXXtodo: fire show/hide and reorder events. That's hard to make it
310 : // right now because coalescence happens by DOM node.
311 : }
312 :
313 : ////////////////////////////////////////////////////////////////////////////////
314 : // nsHTMLLIAccessible: nsAccessible protected
315 :
316 : void
317 0 : nsHTMLLIAccessible::CacheChildren()
318 : {
319 0 : if (mBullet)
320 0 : AppendChild(mBullet);
321 :
322 : // Cache children from subtree.
323 0 : nsAccessibleWrap::CacheChildren();
324 0 : }
325 :
326 : ////////////////////////////////////////////////////////////////////////////////
327 : // nsHTMLListBulletAccessible
328 : ////////////////////////////////////////////////////////////////////////////////
329 :
330 0 : nsHTMLListBulletAccessible::
331 : nsHTMLListBulletAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
332 0 : nsLeafAccessible(aContent, aDoc)
333 : {
334 0 : }
335 :
336 : ////////////////////////////////////////////////////////////////////////////////
337 : // nsHTMLListBulletAccessible: nsAccessNode
338 :
339 : bool
340 0 : nsHTMLListBulletAccessible::IsPrimaryForNode() const
341 : {
342 0 : return false;
343 : }
344 :
345 : ////////////////////////////////////////////////////////////////////////////////
346 : // nsHTMLListBulletAccessible: nsAccessible
347 :
348 : NS_IMETHODIMP
349 0 : nsHTMLListBulletAccessible::GetName(nsAString &aName)
350 : {
351 0 : aName.Truncate();
352 :
353 0 : if (IsDefunct())
354 0 : return NS_ERROR_FAILURE;
355 :
356 : // Native anonymous content, ARIA can't be used. Get list bullet text.
357 0 : nsBlockFrame* blockFrame = do_QueryFrame(mContent->GetPrimaryFrame());
358 0 : NS_ASSERTION(blockFrame, "No frame for list item!");
359 0 : if (blockFrame) {
360 0 : blockFrame->GetBulletText(aName);
361 :
362 : // Append space otherwise bullets are jammed up against list text.
363 0 : aName.Append(' ');
364 : }
365 :
366 0 : return NS_OK;
367 : }
368 :
369 : role
370 0 : nsHTMLListBulletAccessible::NativeRole()
371 : {
372 0 : return roles::STATICTEXT;
373 : }
374 :
375 : PRUint64
376 0 : nsHTMLListBulletAccessible::NativeState()
377 : {
378 0 : PRUint64 state = nsLeafAccessible::NativeState();
379 :
380 0 : state &= ~states::FOCUSABLE;
381 0 : state |= states::READONLY;
382 0 : return state;
383 : }
384 :
385 : void
386 0 : nsHTMLListBulletAccessible::AppendTextTo(nsAString& aText, PRUint32 aStartOffset,
387 : PRUint32 aLength)
388 : {
389 0 : nsAutoString bulletText;
390 0 : nsBlockFrame* blockFrame = do_QueryFrame(mContent->GetPrimaryFrame());
391 0 : NS_ASSERTION(blockFrame, "No frame for list item!");
392 0 : if (blockFrame)
393 0 : blockFrame->GetBulletText(bulletText);
394 :
395 0 : aText.Append(Substring(bulletText, aStartOffset, aLength));
396 0 : }
397 :
398 : ////////////////////////////////////////////////////////////////////////////////
399 : // nsHTMLListAccessible
400 : ////////////////////////////////////////////////////////////////////////////////
401 :
402 0 : nsHTMLListAccessible::
403 : nsHTMLListAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
404 0 : nsHyperTextAccessibleWrap(aContent, aDoc)
405 : {
406 0 : }
407 :
408 0 : NS_IMPL_ISUPPORTS_INHERITED0(nsHTMLListAccessible, nsHyperTextAccessible)
409 :
410 : role
411 0 : nsHTMLListAccessible::NativeRole()
412 : {
413 0 : return roles::LIST;
414 : }
415 :
416 : PRUint64
417 0 : nsHTMLListAccessible::NativeState()
418 : {
419 0 : return nsHyperTextAccessibleWrap::NativeState() | states::READONLY;
420 : }
421 :
|