/src/mozilla-central/accessible/generic/ImageAccessible.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "ImageAccessible.h" |
7 | | |
8 | | #include "nsAccUtils.h" |
9 | | #include "Role.h" |
10 | | #include "AccIterator.h" |
11 | | #include "States.h" |
12 | | |
13 | | #include "imgIContainer.h" |
14 | | #include "imgIRequest.h" |
15 | | #include "nsGenericHTMLElement.h" |
16 | | #include "nsIDocument.h" |
17 | | #include "nsIImageLoadingContent.h" |
18 | | #include "nsIPresShell.h" |
19 | | #include "nsIServiceManager.h" |
20 | | #include "nsIPersistentProperties2.h" |
21 | | #include "nsPIDOMWindow.h" |
22 | | #include "nsIURI.h" |
23 | | |
24 | | using namespace mozilla::a11y; |
25 | | |
26 | | //////////////////////////////////////////////////////////////////////////////// |
27 | | // ImageAccessible |
28 | | //////////////////////////////////////////////////////////////////////////////// |
29 | | |
30 | | ImageAccessible:: |
31 | | ImageAccessible(nsIContent* aContent, DocAccessible* aDoc) : |
32 | | LinkableAccessible(aContent, aDoc) |
33 | 0 | { |
34 | 0 | mType = eImageType; |
35 | 0 | } |
36 | | |
37 | | ImageAccessible::~ImageAccessible() |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | //////////////////////////////////////////////////////////////////////////////// |
42 | | // Accessible public |
43 | | |
44 | | uint64_t |
45 | | ImageAccessible::NativeState() const |
46 | 0 | { |
47 | 0 | // The state is a bitfield, get our inherited state, then logically OR it with |
48 | 0 | // states::ANIMATED if this is an animated image. |
49 | 0 |
|
50 | 0 | uint64_t state = LinkableAccessible::NativeState(); |
51 | 0 |
|
52 | 0 | nsCOMPtr<nsIImageLoadingContent> content(do_QueryInterface(mContent)); |
53 | 0 | nsCOMPtr<imgIRequest> imageRequest; |
54 | 0 |
|
55 | 0 | if (content) |
56 | 0 | content->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, |
57 | 0 | getter_AddRefs(imageRequest)); |
58 | 0 |
|
59 | 0 | nsCOMPtr<imgIContainer> imgContainer; |
60 | 0 | if (imageRequest) |
61 | 0 | imageRequest->GetImage(getter_AddRefs(imgContainer)); |
62 | 0 |
|
63 | 0 | if (imgContainer) { |
64 | 0 | bool animated = false; |
65 | 0 | imgContainer->GetAnimated(&animated); |
66 | 0 | if (animated) |
67 | 0 | state |= states::ANIMATED; |
68 | 0 | } |
69 | 0 |
|
70 | 0 | return state; |
71 | 0 | } |
72 | | |
73 | | ENameValueFlag |
74 | | ImageAccessible::NativeName(nsString& aName) const |
75 | 0 | { |
76 | 0 | bool hasAltAttrib = |
77 | 0 | mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::alt, aName); |
78 | 0 | if (!aName.IsEmpty()) |
79 | 0 | return eNameOK; |
80 | 0 | |
81 | 0 | ENameValueFlag nameFlag = Accessible::NativeName(aName); |
82 | 0 | if (!aName.IsEmpty()) |
83 | 0 | return nameFlag; |
84 | 0 | |
85 | 0 | // No accessible name but empty 'alt' attribute is present. If further name |
86 | 0 | // computation algorithm doesn't provide non empty name then it means |
87 | 0 | // an empty 'alt' attribute was used to indicate a decorative image (see |
88 | 0 | // Accessible::Name() method for details). |
89 | 0 | return hasAltAttrib ? eNoNameOnPurpose : eNameOK; |
90 | 0 | } |
91 | | |
92 | | role |
93 | | ImageAccessible::NativeRole() const |
94 | 0 | { |
95 | 0 | return roles::GRAPHIC; |
96 | 0 | } |
97 | | |
98 | | //////////////////////////////////////////////////////////////////////////////// |
99 | | // Accessible |
100 | | |
101 | | uint8_t |
102 | | ImageAccessible::ActionCount() const |
103 | 0 | { |
104 | 0 | uint8_t actionCount = LinkableAccessible::ActionCount(); |
105 | 0 | return HasLongDesc() ? actionCount + 1 : actionCount; |
106 | 0 | } |
107 | | |
108 | | void |
109 | | ImageAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) |
110 | 0 | { |
111 | 0 | aName.Truncate(); |
112 | 0 | if (IsLongDescIndex(aIndex) && HasLongDesc()) |
113 | 0 | aName.AssignLiteral("showlongdesc"); |
114 | 0 | else |
115 | 0 | LinkableAccessible::ActionNameAt(aIndex, aName); |
116 | 0 | } |
117 | | |
118 | | bool |
119 | | ImageAccessible::DoAction(uint8_t aIndex) const |
120 | 0 | { |
121 | 0 | // Get the long description uri and open in a new window. |
122 | 0 | if (!IsLongDescIndex(aIndex)) |
123 | 0 | return LinkableAccessible::DoAction(aIndex); |
124 | 0 | |
125 | 0 | nsCOMPtr<nsIURI> uri = GetLongDescURI(); |
126 | 0 | if (!uri) |
127 | 0 | return false; |
128 | 0 | |
129 | 0 | nsAutoCString utf8spec; |
130 | 0 | uri->GetSpec(utf8spec); |
131 | 0 | NS_ConvertUTF8toUTF16 spec(utf8spec); |
132 | 0 |
|
133 | 0 | nsIDocument* document = mContent->OwnerDoc(); |
134 | 0 | nsCOMPtr<nsPIDOMWindowOuter> piWindow = document->GetWindow(); |
135 | 0 | if (!piWindow) |
136 | 0 | return false; |
137 | 0 | |
138 | 0 | nsCOMPtr<nsPIDOMWindowOuter> tmp; |
139 | 0 | return NS_SUCCEEDED(piWindow->Open(spec, EmptyString(), EmptyString(), |
140 | 0 | /* aLoadInfo = */ nullptr, |
141 | 0 | /* aForceNoOpener = */ false, |
142 | 0 | getter_AddRefs(tmp))); |
143 | 0 | } |
144 | | |
145 | | //////////////////////////////////////////////////////////////////////////////// |
146 | | // ImageAccessible |
147 | | |
148 | | nsIntPoint |
149 | | ImageAccessible::Position(uint32_t aCoordType) |
150 | 0 | { |
151 | 0 | nsIntPoint point = Bounds().TopLeft(); |
152 | 0 | nsAccUtils::ConvertScreenCoordsTo(&point.x, &point.y, aCoordType, this); |
153 | 0 | return point; |
154 | 0 | } |
155 | | |
156 | | nsIntSize |
157 | | ImageAccessible::Size() |
158 | 0 | { |
159 | 0 | return Bounds().Size(); |
160 | 0 | } |
161 | | |
162 | | // Accessible |
163 | | already_AddRefed<nsIPersistentProperties> |
164 | | ImageAccessible::NativeAttributes() |
165 | 0 | { |
166 | 0 | nsCOMPtr<nsIPersistentProperties> attributes = |
167 | 0 | LinkableAccessible::NativeAttributes(); |
168 | 0 |
|
169 | 0 | nsAutoString src; |
170 | 0 | mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::src, src); |
171 | 0 | if (!src.IsEmpty()) |
172 | 0 | nsAccUtils::SetAccAttr(attributes, nsGkAtoms::src, src); |
173 | 0 |
|
174 | 0 | return attributes.forget(); |
175 | 0 | } |
176 | | |
177 | | //////////////////////////////////////////////////////////////////////////////// |
178 | | // Private methods |
179 | | |
180 | | already_AddRefed<nsIURI> |
181 | | ImageAccessible::GetLongDescURI() const |
182 | 0 | { |
183 | 0 | if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::longdesc)) { |
184 | 0 | // To check if longdesc contains an invalid url. |
185 | 0 | nsAutoString longdesc; |
186 | 0 | mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::longdesc, longdesc); |
187 | 0 | if (longdesc.FindChar(' ') != -1 || longdesc.FindChar('\t') != -1 || |
188 | 0 | longdesc.FindChar('\r') != -1 || longdesc.FindChar('\n') != -1) { |
189 | 0 | return nullptr; |
190 | 0 | } |
191 | 0 | nsCOMPtr<nsIURI> baseURI = mContent->GetBaseURI(); |
192 | 0 | nsCOMPtr<nsIURI> uri; |
193 | 0 | nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri), longdesc, |
194 | 0 | mContent->OwnerDoc(), baseURI); |
195 | 0 | return uri.forget(); |
196 | 0 | } |
197 | 0 | |
198 | 0 | DocAccessible* document = Document(); |
199 | 0 | if (document) { |
200 | 0 | IDRefsIterator iter(document, mContent, nsGkAtoms::aria_describedby); |
201 | 0 | while (nsIContent* target = iter.NextElem()) { |
202 | 0 | if ((target->IsHTMLElement(nsGkAtoms::a) || |
203 | 0 | target->IsHTMLElement(nsGkAtoms::area)) && |
204 | 0 | target->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::href)) { |
205 | 0 | nsGenericHTMLElement* element = |
206 | 0 | nsGenericHTMLElement::FromNode(target); |
207 | 0 |
|
208 | 0 | nsCOMPtr<nsIURI> uri; |
209 | 0 | element->GetURIAttr(nsGkAtoms::href, nullptr, getter_AddRefs(uri)); |
210 | 0 | return uri.forget(); |
211 | 0 | } |
212 | 0 | } |
213 | 0 | } |
214 | 0 |
|
215 | 0 | return nullptr; |
216 | 0 | } |
217 | | |
218 | | bool |
219 | | ImageAccessible::IsLongDescIndex(uint8_t aIndex) const |
220 | 0 | { |
221 | 0 | return aIndex == LinkableAccessible::ActionCount(); |
222 | 0 | } |
223 | | |