[7ea138]: accessible / src / generic / ImageAccessible.cpp  Maximize  Restore  History

Download this file

227 lines (182 with data), 6.1 kB

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

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks