/src/mozilla-central/accessible/base/TextAttrs.h
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 | | #ifndef nsTextAttrs_h_ |
7 | | #define nsTextAttrs_h_ |
8 | | |
9 | | #include "mozilla/FontPropertyTypes.h" |
10 | | #include "nsCOMPtr.h" |
11 | | #include "nsColor.h" |
12 | | #include "nsString.h" |
13 | | #include "nsStyleConsts.h" |
14 | | |
15 | | class nsIFrame; |
16 | | class nsIPersistentProperties; |
17 | | class nsIContent; |
18 | | class nsDeviceContext; |
19 | | |
20 | | namespace mozilla { |
21 | | namespace a11y { |
22 | | |
23 | | class Accessible; |
24 | | class HyperTextAccessible; |
25 | | |
26 | | /** |
27 | | * Used to expose text attributes for the hyper text accessible (see |
28 | | * HyperTextAccessible class). |
29 | | * |
30 | | * @note "invalid: spelling" text attribute is implemented entirely in |
31 | | * HyperTextAccessible class. |
32 | | */ |
33 | | class TextAttrsMgr |
34 | | { |
35 | | public: |
36 | | /** |
37 | | * Constructor. Used to expose default text attributes. |
38 | | */ |
39 | | explicit TextAttrsMgr(HyperTextAccessible* aHyperTextAcc) : |
40 | | mOffsetAcc(nullptr), mHyperTextAcc(aHyperTextAcc), |
41 | 0 | mOffsetAccIdx(-1), mIncludeDefAttrs(true) { } |
42 | | |
43 | | /** |
44 | | * Constructor. Used to expose text attributes at the given offset. |
45 | | * |
46 | | * @param aHyperTextAcc [in] hyper text accessible text attributes are |
47 | | * calculated for |
48 | | * @param aIncludeDefAttrs [optional] indicates whether default text |
49 | | * attributes should be included into list of exposed |
50 | | * text attributes |
51 | | * @param oOffsetAcc [optional] offset an accessible the text attributes |
52 | | * should be calculated for |
53 | | * @param oOffsetAccIdx [optional] index in parent of offset accessible |
54 | | */ |
55 | | TextAttrsMgr(HyperTextAccessible* aHyperTextAcc, |
56 | | bool aIncludeDefAttrs, |
57 | | Accessible* aOffsetAcc, |
58 | | int32_t aOffsetAccIdx) : |
59 | | mOffsetAcc(aOffsetAcc), mHyperTextAcc(aHyperTextAcc), |
60 | 0 | mOffsetAccIdx(aOffsetAccIdx), mIncludeDefAttrs(aIncludeDefAttrs) { } |
61 | | |
62 | | /* |
63 | | * Return text attributes and hyper text offsets where these attributes are |
64 | | * applied. Offsets are calculated in the case of non default attributes. |
65 | | * |
66 | | * @note In the case of default attributes pointers on hyper text offsets |
67 | | * must be skipped. |
68 | | * |
69 | | * @param aAttributes [in, out] text attributes list |
70 | | * @param aStartHTOffset [out, optional] start hyper text offset |
71 | | * @param aEndHTOffset [out, optional] end hyper text offset |
72 | | */ |
73 | | void GetAttributes(nsIPersistentProperties* aAttributes, |
74 | | uint32_t* aStartHTOffset = nullptr, |
75 | | uint32_t* aEndHTOffset = nullptr); |
76 | | |
77 | | protected: |
78 | | /** |
79 | | * Calculates range (start and end offsets) of text where the text attributes |
80 | | * are stretched. New offsets may be smaller if one of text attributes changes |
81 | | * its value before or after the given offsets. |
82 | | * |
83 | | * @param aTextAttrArray [in] text attributes array |
84 | | * @param aAttrArrayLen [in] text attributes array length |
85 | | * @param aStartHTOffset [in, out] the start offset |
86 | | * @param aEndHTOffset [in, out] the end offset |
87 | | */ |
88 | | class TextAttr; |
89 | | void GetRange(TextAttr* aAttrArray[], uint32_t aAttrArrayLen, |
90 | | uint32_t* aStartOffset, uint32_t* aEndOffset); |
91 | | |
92 | | private: |
93 | | Accessible* mOffsetAcc; |
94 | | HyperTextAccessible* mHyperTextAcc; |
95 | | int32_t mOffsetAccIdx; |
96 | | bool mIncludeDefAttrs; |
97 | | |
98 | | protected: |
99 | | |
100 | | /** |
101 | | * Interface class of text attribute class implementations. |
102 | | */ |
103 | | class TextAttr |
104 | | { |
105 | | public: |
106 | | /** |
107 | | * Expose the text attribute to the given attribute set. |
108 | | * |
109 | | * @param aAttributes [in] the given attribute set |
110 | | * @param aIncludeDefAttrValue [in] if true then attribute is exposed even |
111 | | * if its value is the same as default one |
112 | | */ |
113 | | virtual void Expose(nsIPersistentProperties* aAttributes, |
114 | | bool aIncludeDefAttrValue) = 0; |
115 | | |
116 | | /** |
117 | | * Return true if the text attribute value on the given element equals with |
118 | | * predefined attribute value. |
119 | | */ |
120 | | virtual bool Equal(Accessible* aAccessible) = 0; |
121 | | }; |
122 | | |
123 | | |
124 | | /** |
125 | | * Base class to work with text attributes. See derived classes below. |
126 | | */ |
127 | | template<class T> |
128 | | class TTextAttr : public TextAttr |
129 | | { |
130 | | public: |
131 | 0 | explicit TTextAttr(bool aGetRootValue) : mGetRootValue(aGetRootValue) {} Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<nsTString<char16_t> >::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<unsigned int>::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<int>::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::FontSlantStyle>::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::FontWeight>::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<bool>::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::a11y::TextAttrsMgr::TextDecorValue>::TTextAttr(bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::a11y::TextAttrsMgr::TextPosValue>::TTextAttr(bool) |
132 | | |
133 | | // TextAttr |
134 | | virtual void Expose(nsIPersistentProperties* aAttributes, |
135 | | bool aIncludeDefAttrValue) override |
136 | 0 | { |
137 | 0 | if (mGetRootValue) { |
138 | 0 | if (mIsRootDefined) |
139 | 0 | ExposeValue(aAttributes, mRootNativeValue); |
140 | 0 | return; |
141 | 0 | } |
142 | 0 |
|
143 | 0 | if (mIsDefined) { |
144 | 0 | if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue) |
145 | 0 | ExposeValue(aAttributes, mNativeValue); |
146 | 0 | return; |
147 | 0 | } |
148 | 0 |
|
149 | 0 | if (aIncludeDefAttrValue && mIsRootDefined) |
150 | 0 | ExposeValue(aAttributes, mRootNativeValue); |
151 | 0 | } Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<unsigned int>::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<nsTString<char16_t> >::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<int>::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::FontSlantStyle>::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::FontWeight>::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<bool>::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::a11y::TextAttrsMgr::TextDecorValue>::Expose(nsIPersistentProperties*, bool) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::a11y::TextAttrsMgr::TextPosValue>::Expose(nsIPersistentProperties*, bool) |
152 | | |
153 | | virtual bool Equal(Accessible* aAccessible) override |
154 | 0 | { |
155 | 0 | T nativeValue; |
156 | 0 | bool isDefined = GetValueFor(aAccessible, &nativeValue); |
157 | 0 |
|
158 | 0 | if (!mIsDefined && !isDefined) |
159 | 0 | return true; |
160 | 0 | |
161 | 0 | if (mIsDefined && isDefined) |
162 | 0 | return nativeValue == mNativeValue; |
163 | 0 | |
164 | 0 | if (mIsDefined) |
165 | 0 | return mNativeValue == mRootNativeValue; |
166 | 0 | |
167 | 0 | return nativeValue == mRootNativeValue; |
168 | 0 | } Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<unsigned int>::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<nsTString<char16_t> >::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<int>::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::FontSlantStyle>::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::FontWeight>::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<bool>::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::a11y::TextAttrsMgr::TextDecorValue>::Equal(mozilla::a11y::Accessible*) Unexecuted instantiation: mozilla::a11y::TextAttrsMgr::TTextAttr<mozilla::a11y::TextAttrsMgr::TextPosValue>::Equal(mozilla::a11y::Accessible*) |
169 | | |
170 | | protected: |
171 | | |
172 | | // Expose the text attribute with the given value to attribute set. |
173 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
174 | | const T& aValue) = 0; |
175 | | |
176 | | // Return native value for the given DOM element. |
177 | | virtual bool GetValueFor(Accessible* aAccessible, T* aValue) = 0; |
178 | | |
179 | | // Indicates if root value should be exposed. |
180 | | bool mGetRootValue; |
181 | | |
182 | | // Native value and flag indicating if the value is defined (initialized in |
183 | | // derived classes). Note, undefined native value means it is inherited |
184 | | // from root. |
185 | | MOZ_INIT_OUTSIDE_CTOR T mNativeValue; |
186 | | MOZ_INIT_OUTSIDE_CTOR bool mIsDefined; |
187 | | |
188 | | // Native root value and flag indicating if the value is defined (initialized |
189 | | // in derived classes). |
190 | | MOZ_INIT_OUTSIDE_CTOR T mRootNativeValue; |
191 | | MOZ_INIT_OUTSIDE_CTOR bool mIsRootDefined; |
192 | | }; |
193 | | |
194 | | |
195 | | /** |
196 | | * Class is used for the work with 'language' text attribute. |
197 | | */ |
198 | | class LangTextAttr : public TTextAttr<nsString> |
199 | | { |
200 | | public: |
201 | | LangTextAttr(HyperTextAccessible* aRoot, nsIContent* aRootElm, |
202 | | nsIContent* aElm); |
203 | | virtual ~LangTextAttr(); |
204 | | |
205 | | protected: |
206 | | |
207 | | // TextAttr |
208 | | virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue) override; |
209 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
210 | | const nsString& aValue) override; |
211 | | |
212 | | private: |
213 | | nsCOMPtr<nsIContent> mRootContent; |
214 | | }; |
215 | | |
216 | | |
217 | | /** |
218 | | * Class is used for the 'invalid' text attribute. Note, it calculated |
219 | | * the attribute from aria-invalid attribute only; invalid:spelling attribute |
220 | | * calculated from misspelled text in the editor is managed by |
221 | | * HyperTextAccessible and applied on top of the value from aria-invalid. |
222 | | */ |
223 | | class InvalidTextAttr : public TTextAttr<uint32_t> |
224 | | { |
225 | | public: |
226 | | InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm); |
227 | 0 | virtual ~InvalidTextAttr() { }; |
228 | | |
229 | | protected: |
230 | | |
231 | | enum { |
232 | | eFalse, |
233 | | eGrammar, |
234 | | eSpelling, |
235 | | eTrue |
236 | | }; |
237 | | |
238 | | // TextAttr |
239 | | virtual bool GetValueFor(Accessible* aAccessible, uint32_t* aValue) override; |
240 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
241 | | const uint32_t& aValue) override; |
242 | | |
243 | | private: |
244 | | bool GetValue(nsIContent* aElm, uint32_t* aValue); |
245 | | nsIContent* mRootElm; |
246 | | }; |
247 | | |
248 | | |
249 | | /** |
250 | | * Class is used for the work with 'background-color' text attribute. |
251 | | */ |
252 | | class BGColorTextAttr : public TTextAttr<nscolor> |
253 | | { |
254 | | public: |
255 | | BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
256 | 0 | virtual ~BGColorTextAttr() { } |
257 | | |
258 | | protected: |
259 | | |
260 | | // TextAttr |
261 | | virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue) |
262 | | override; |
263 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
264 | | const nscolor& aValue) override; |
265 | | |
266 | | private: |
267 | | bool GetColor(nsIFrame* aFrame, nscolor* aColor); |
268 | | nsIFrame* mRootFrame; |
269 | | }; |
270 | | |
271 | | |
272 | | /** |
273 | | * Class is used for the work with 'color' text attribute. |
274 | | */ |
275 | | class ColorTextAttr : public TTextAttr<nscolor> |
276 | | { |
277 | | public: |
278 | | ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
279 | 0 | virtual ~ColorTextAttr() { } |
280 | | |
281 | | protected: |
282 | | |
283 | | // TTextAttr |
284 | | virtual bool GetValueFor(Accessible* aAccessible, nscolor* aValue) |
285 | | override; |
286 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
287 | | const nscolor& aValue) override; |
288 | | }; |
289 | | |
290 | | |
291 | | /** |
292 | | * Class is used for the work with "font-family" text attribute. |
293 | | */ |
294 | | class FontFamilyTextAttr : public TTextAttr<nsString> |
295 | | { |
296 | | public: |
297 | | FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
298 | 0 | virtual ~FontFamilyTextAttr() { } |
299 | | |
300 | | protected: |
301 | | |
302 | | // TTextAttr |
303 | | virtual bool GetValueFor(Accessible* aAccessible, nsString* aValue) |
304 | | override; |
305 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
306 | | const nsString& aValue) override; |
307 | | |
308 | | private: |
309 | | |
310 | | bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily); |
311 | | }; |
312 | | |
313 | | |
314 | | /** |
315 | | * Class is used for the work with "font-size" text attribute. |
316 | | */ |
317 | | class FontSizeTextAttr : public TTextAttr<nscoord> |
318 | | { |
319 | | public: |
320 | | FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
321 | 0 | virtual ~FontSizeTextAttr() { } |
322 | | |
323 | | protected: |
324 | | |
325 | | // TTextAttr |
326 | | virtual bool GetValueFor(Accessible* aAccessible, nscoord* aValue) |
327 | | override; |
328 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
329 | | const nscoord& aValue) override; |
330 | | |
331 | | private: |
332 | | nsDeviceContext* mDC; |
333 | | }; |
334 | | |
335 | | |
336 | | /** |
337 | | * Class is used for the work with "font-style" text attribute. |
338 | | */ |
339 | | class FontStyleTextAttr : public TTextAttr<mozilla::FontSlantStyle> |
340 | | { |
341 | | public: |
342 | | FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
343 | 0 | virtual ~FontStyleTextAttr() { } |
344 | | |
345 | | protected: |
346 | | |
347 | | // TTextAttr |
348 | | virtual bool GetValueFor(Accessible* aContent, mozilla::FontSlantStyle* aValue) |
349 | | override; |
350 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
351 | | const mozilla::FontSlantStyle& aValue) override; |
352 | | }; |
353 | | |
354 | | |
355 | | /** |
356 | | * Class is used for the work with "font-weight" text attribute. |
357 | | */ |
358 | | class FontWeightTextAttr : public TTextAttr<mozilla::FontWeight> |
359 | | { |
360 | | public: |
361 | | FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
362 | 0 | virtual ~FontWeightTextAttr() { } |
363 | | |
364 | | protected: |
365 | | |
366 | | // TTextAttr |
367 | | virtual bool GetValueFor(Accessible* aAccessible, |
368 | | mozilla::FontWeight* aValue) |
369 | | override; |
370 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
371 | | const mozilla::FontWeight& aValue) override; |
372 | | |
373 | | private: |
374 | | mozilla::FontWeight GetFontWeight(nsIFrame* aFrame); |
375 | | }; |
376 | | |
377 | | /** |
378 | | * Class is used for the work with 'auto-generated' text attribute. |
379 | | */ |
380 | | class AutoGeneratedTextAttr : public TTextAttr<bool> |
381 | | { |
382 | | public: |
383 | | AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc, |
384 | | Accessible* aAccessible); |
385 | 0 | virtual ~AutoGeneratedTextAttr() { } |
386 | | |
387 | | protected: |
388 | | // TextAttr |
389 | | virtual bool GetValueFor(Accessible* aAccessible, bool* aValue) |
390 | | override; |
391 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
392 | | const bool& aValue) override; |
393 | | }; |
394 | | |
395 | | |
396 | | /** |
397 | | * TextDecorTextAttr class is used for the work with |
398 | | * "text-line-through-style", "text-line-through-color", |
399 | | * "text-underline-style" and "text-underline-color" text attributes. |
400 | | */ |
401 | | |
402 | | class TextDecorValue |
403 | | { |
404 | | public: |
405 | | TextDecorValue() : |
406 | | mColor{0}, mLine{NS_STYLE_TEXT_DECORATION_LINE_NONE}, |
407 | 0 | mStyle{NS_STYLE_TEXT_DECORATION_STYLE_NONE} { } |
408 | | explicit TextDecorValue(nsIFrame* aFrame); |
409 | | |
410 | 0 | nscolor Color() const { return mColor; } |
411 | 0 | uint8_t Style() const { return mStyle; } |
412 | | |
413 | | bool IsDefined() const |
414 | 0 | { return IsUnderline() || IsLineThrough(); } |
415 | | bool IsUnderline() const |
416 | 0 | { return mLine & NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE; } |
417 | | bool IsLineThrough() const |
418 | 0 | { return mLine & NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH; } |
419 | | |
420 | | bool operator ==(const TextDecorValue& aValue) |
421 | 0 | { |
422 | 0 | return mColor == aValue.mColor && mLine == aValue.mLine && |
423 | 0 | mStyle == aValue.mStyle; |
424 | 0 | } |
425 | | bool operator !=(const TextDecorValue& aValue) |
426 | 0 | { return !(*this == aValue); } |
427 | | |
428 | | private: |
429 | | nscolor mColor; |
430 | | uint8_t mLine; |
431 | | uint8_t mStyle; |
432 | | }; |
433 | | |
434 | | class TextDecorTextAttr : public TTextAttr<TextDecorValue> |
435 | | { |
436 | | public: |
437 | | TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
438 | 0 | virtual ~TextDecorTextAttr() { } |
439 | | |
440 | | protected: |
441 | | |
442 | | // TextAttr |
443 | | virtual bool GetValueFor(Accessible* aAccessible, TextDecorValue* aValue) |
444 | | override; |
445 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
446 | | const TextDecorValue& aValue) override; |
447 | | }; |
448 | | |
449 | | /** |
450 | | * Class is used for the work with "text-position" text attribute. |
451 | | */ |
452 | | |
453 | | enum TextPosValue { |
454 | | eTextPosNone = 0, |
455 | | eTextPosBaseline, |
456 | | eTextPosSub, |
457 | | eTextPosSuper |
458 | | }; |
459 | | |
460 | | class TextPosTextAttr : public TTextAttr<TextPosValue> |
461 | | { |
462 | | public: |
463 | | TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame); |
464 | 0 | virtual ~TextPosTextAttr() { } |
465 | | |
466 | | protected: |
467 | | |
468 | | // TextAttr |
469 | | virtual bool GetValueFor(Accessible* aAccessible, TextPosValue* aValue) |
470 | | override; |
471 | | virtual void ExposeValue(nsIPersistentProperties* aAttributes, |
472 | | const TextPosValue& aValue) override; |
473 | | |
474 | | private: |
475 | | TextPosValue GetTextPosValue(nsIFrame* aFrame) const; |
476 | | }; |
477 | | |
478 | | }; // TextAttrMgr |
479 | | |
480 | | } // namespace a11y |
481 | | } // namespace mozilla |
482 | | |
483 | | #endif |