Bug 1867925 - Mark some storage-access-api tests as intermittent after wpt-sync....
[gecko.git] / accessible / base / TextAttrs.h
blob031e11427395f1c831eeb8795f27fb17ebeaa637
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/. */
6 #ifndef nsTextAttrs_h_
7 #define nsTextAttrs_h_
9 #include "mozilla/FontPropertyTypes.h"
10 #include "nsCOMPtr.h"
11 #include "nsColor.h"
12 #include "nsString.h"
13 #include "nsStyleConsts.h"
15 class nsIFrame;
16 class nsIContent;
17 class nsDeviceContext;
19 namespace mozilla {
20 namespace a11y {
22 class AccAttributes;
23 class LocalAccessible;
24 class HyperTextAccessible;
26 /**
27 * Used to expose text attributes for the hyper text accessible (see
28 * HyperTextAccessible class).
30 * @note "invalid: spelling" text attribute is implemented entirely in
31 * HyperTextAccessible class.
33 class TextAttrsMgr {
34 public:
35 /**
36 * Constructor. Used to expose default text attributes.
38 explicit TextAttrsMgr(HyperTextAccessible* aHyperTextAcc)
39 : mOffsetAcc(nullptr),
40 mHyperTextAcc(aHyperTextAcc),
41 mOffsetAccIdx(-1),
42 mIncludeDefAttrs(true) {}
44 /**
45 * Constructor. Used to expose text attributes at the given offset.
47 * @param aHyperTextAcc [in] hyper text accessible text attributes are
48 * calculated for
49 * @param aIncludeDefAttrs [optional] indicates whether default text
50 * attributes should be included into list of exposed
51 * text attributes
52 * @param oOffsetAcc [optional] offset an accessible the text attributes
53 * should be calculated for
54 * @param oOffsetAccIdx [optional] index in parent of offset accessible
56 TextAttrsMgr(HyperTextAccessible* aHyperTextAcc, bool aIncludeDefAttrs,
57 LocalAccessible* aOffsetAcc, int32_t aOffsetAccIdx)
58 : mOffsetAcc(aOffsetAcc),
59 mHyperTextAcc(aHyperTextAcc),
60 mOffsetAccIdx(aOffsetAccIdx),
61 mIncludeDefAttrs(aIncludeDefAttrs) {}
64 * Return text attributes and hyper text offsets where these attributes are
65 * applied. Offsets are calculated in the case of non default attributes.
67 * @note In the case of default attributes pointers on hyper text offsets
68 * must be skipped.
70 * @param aAttributes [in, out] text attributes list
71 * @param aStartHTOffset [out, optional] start hyper text offset
72 * @param aEndHTOffset [out, optional] end hyper text offset
74 void GetAttributes(AccAttributes* aAttributes,
75 uint32_t* aStartHTOffset = nullptr,
76 uint32_t* aEndHTOffset = nullptr);
78 protected:
79 /**
80 * Calculates range (start and end offsets) of text where the text attributes
81 * are stretched. New offsets may be smaller if one of text attributes changes
82 * its value before or after the given offsets.
84 * @param aTextAttrArray [in] text attributes array
85 * @param aAttrArrayLen [in] text attributes array length
86 * @param aStartHTOffset [in, out] the start offset
87 * @param aEndHTOffset [in, out] the end offset
89 class TextAttr;
90 void GetRange(TextAttr* aAttrArray[], uint32_t aAttrArrayLen,
91 uint32_t* aStartOffset, uint32_t* aEndOffset);
93 private:
94 LocalAccessible* mOffsetAcc;
95 HyperTextAccessible* mHyperTextAcc;
96 int32_t mOffsetAccIdx;
97 bool mIncludeDefAttrs;
99 protected:
101 * Interface class of text attribute class implementations.
103 class TextAttr {
104 public:
106 * Expose the text attribute to the given attribute set.
108 * @param aAttributes [in] the given attribute set
109 * @param aIncludeDefAttrValue [in] if true then attribute is exposed even
110 * if its value is the same as default one
112 virtual void Expose(AccAttributes* aAttributes,
113 bool aIncludeDefAttrValue) = 0;
116 * Return true if the text attribute value on the given element equals with
117 * predefined attribute value.
119 virtual bool Equal(LocalAccessible* aAccessible) = 0;
123 * Base class to work with text attributes. See derived classes below.
125 template <class T>
126 class TTextAttr : public TextAttr {
127 public:
128 explicit TTextAttr(bool aGetRootValue) : mGetRootValue(aGetRootValue) {}
130 // TextAttr
131 virtual void Expose(AccAttributes* aAttributes,
132 bool aIncludeDefAttrValue) override {
133 if (mGetRootValue) {
134 if (mIsRootDefined) ExposeValue(aAttributes, mRootNativeValue);
135 return;
138 if (mIsDefined) {
139 if (aIncludeDefAttrValue || mRootNativeValue != mNativeValue) {
140 ExposeValue(aAttributes, mNativeValue);
142 return;
145 if (aIncludeDefAttrValue && mIsRootDefined) {
146 ExposeValue(aAttributes, mRootNativeValue);
150 virtual bool Equal(LocalAccessible* aAccessible) override {
151 T nativeValue;
152 bool isDefined = GetValueFor(aAccessible, &nativeValue);
154 if (!mIsDefined && !isDefined) return true;
156 if (mIsDefined && isDefined) return nativeValue == mNativeValue;
158 if (mIsDefined) return mNativeValue == mRootNativeValue;
160 return nativeValue == mRootNativeValue;
163 protected:
164 // Expose the text attribute with the given value to attribute set.
165 virtual void ExposeValue(AccAttributes* aAttributes, const T& aValue) = 0;
167 // Return native value for the given DOM element.
168 virtual bool GetValueFor(LocalAccessible* aAccessible, T* aValue) = 0;
170 // Indicates if root value should be exposed.
171 bool mGetRootValue;
173 // Native value and flag indicating if the value is defined (initialized in
174 // derived classes). Note, undefined native value means it is inherited
175 // from root.
176 MOZ_INIT_OUTSIDE_CTOR T mNativeValue;
177 MOZ_INIT_OUTSIDE_CTOR bool mIsDefined;
179 // Native root value and flag indicating if the value is defined
180 // (initialized in derived classes).
181 MOZ_INIT_OUTSIDE_CTOR T mRootNativeValue;
182 MOZ_INIT_OUTSIDE_CTOR bool mIsRootDefined;
186 * Class is used for the work with 'language' text attribute.
188 class LangTextAttr : public TTextAttr<nsString> {
189 public:
190 LangTextAttr(HyperTextAccessible* aRoot, nsIContent* aRootElm,
191 nsIContent* aElm);
192 virtual ~LangTextAttr();
194 protected:
195 // TextAttr
196 virtual bool GetValueFor(LocalAccessible* aAccessible,
197 nsString* aValue) override;
198 virtual void ExposeValue(AccAttributes* aAttributes,
199 const nsString& aValue) override;
201 private:
202 nsCOMPtr<nsIContent> mRootContent;
206 * Class is used for the 'invalid' text attribute. Note, it calculated
207 * the attribute from aria-invalid attribute only; invalid:spelling attribute
208 * calculated from misspelled text in the editor is managed by
209 * HyperTextAccessible and applied on top of the value from aria-invalid.
211 class InvalidTextAttr : public TTextAttr<uint32_t> {
212 public:
213 InvalidTextAttr(nsIContent* aRootElm, nsIContent* aElm);
214 virtual ~InvalidTextAttr(){};
216 protected:
217 enum { eFalse, eGrammar, eSpelling, eTrue };
219 // TextAttr
220 virtual bool GetValueFor(LocalAccessible* aAccessible,
221 uint32_t* aValue) override;
222 virtual void ExposeValue(AccAttributes* aAttributes,
223 const uint32_t& aValue) override;
225 private:
226 bool GetValue(nsIContent* aElm, uint32_t* aValue);
227 nsIContent* mRootElm;
231 * Class is used for the work with 'background-color' text attribute.
233 class BGColorTextAttr : public TTextAttr<nscolor> {
234 public:
235 BGColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
236 virtual ~BGColorTextAttr() {}
238 protected:
239 // TextAttr
240 virtual bool GetValueFor(LocalAccessible* aAccessible,
241 nscolor* aValue) override;
242 virtual void ExposeValue(AccAttributes* aAttributes,
243 const nscolor& aValue) override;
245 private:
246 bool GetColor(nsIFrame* aFrame, nscolor* aColor);
247 nsIFrame* mRootFrame;
251 * Class is used for the work with 'color' text attribute.
253 class ColorTextAttr : public TTextAttr<nscolor> {
254 public:
255 ColorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
256 virtual ~ColorTextAttr() {}
258 protected:
259 // TTextAttr
260 virtual bool GetValueFor(LocalAccessible* aAccessible,
261 nscolor* aValue) override;
262 virtual void ExposeValue(AccAttributes* aAttributes,
263 const nscolor& aValue) override;
267 * Class is used for the work with "font-family" text attribute.
269 class FontFamilyTextAttr : public TTextAttr<nsString> {
270 public:
271 FontFamilyTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
272 virtual ~FontFamilyTextAttr() {}
274 protected:
275 // TTextAttr
276 virtual bool GetValueFor(LocalAccessible* aAccessible,
277 nsString* aValue) override;
278 virtual void ExposeValue(AccAttributes* aAttributes,
279 const nsString& aValue) override;
281 private:
282 bool GetFontFamily(nsIFrame* aFrame, nsString& aFamily);
286 * Class is used for the work with "font-size" text attribute.
288 class FontSizeTextAttr : public TTextAttr<nscoord> {
289 public:
290 FontSizeTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
291 virtual ~FontSizeTextAttr() {}
293 protected:
294 // TTextAttr
295 virtual bool GetValueFor(LocalAccessible* aAccessible,
296 nscoord* aValue) override;
297 virtual void ExposeValue(AccAttributes* aAttributes,
298 const nscoord& aValue) override;
300 private:
301 nsDeviceContext* mDC;
305 * Class is used for the work with "font-style" text attribute.
307 class FontStyleTextAttr : public TTextAttr<mozilla::FontSlantStyle> {
308 public:
309 FontStyleTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
310 virtual ~FontStyleTextAttr() {}
312 protected:
313 // TTextAttr
314 virtual bool GetValueFor(LocalAccessible* aContent,
315 mozilla::FontSlantStyle* aValue) override;
316 virtual void ExposeValue(AccAttributes* aAttributes,
317 const mozilla::FontSlantStyle& aValue) override;
321 * Class is used for the work with "font-weight" text attribute.
323 class FontWeightTextAttr : public TTextAttr<mozilla::FontWeight> {
324 public:
325 FontWeightTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
326 virtual ~FontWeightTextAttr() {}
328 protected:
329 // TTextAttr
330 virtual bool GetValueFor(LocalAccessible* aAccessible,
331 mozilla::FontWeight* aValue) override;
332 virtual void ExposeValue(AccAttributes* aAttributes,
333 const mozilla::FontWeight& aValue) override;
335 private:
336 mozilla::FontWeight GetFontWeight(nsIFrame* aFrame);
340 * Class is used for the work with 'auto-generated' text attribute.
342 class AutoGeneratedTextAttr : public TTextAttr<bool> {
343 public:
344 AutoGeneratedTextAttr(HyperTextAccessible* aHyperTextAcc,
345 LocalAccessible* aAccessible);
346 virtual ~AutoGeneratedTextAttr() {}
348 protected:
349 // TextAttr
350 virtual bool GetValueFor(LocalAccessible* aAccessible,
351 bool* aValue) override;
352 virtual void ExposeValue(AccAttributes* aAttributes,
353 const bool& aValue) override;
357 * TextDecorTextAttr class is used for the work with
358 * "text-line-through-style", "text-line-through-color",
359 * "text-underline-style" and "text-underline-color" text attributes.
362 class TextDecorValue {
363 public:
364 TextDecorValue()
365 : mColor{0},
366 mLine{StyleTextDecorationLine::NONE},
367 mStyle{StyleTextDecorationStyle::None} {}
368 explicit TextDecorValue(nsIFrame* aFrame);
370 nscolor Color() const { return mColor; }
371 mozilla::StyleTextDecorationStyle Style() const { return mStyle; }
373 bool IsDefined() const { return IsUnderline() || IsLineThrough(); }
374 bool IsUnderline() const {
375 return bool(mLine & mozilla::StyleTextDecorationLine::UNDERLINE);
377 bool IsLineThrough() const {
378 return bool(mLine & mozilla::StyleTextDecorationLine::LINE_THROUGH);
381 bool operator==(const TextDecorValue& aValue) const {
382 return mColor == aValue.mColor && mLine == aValue.mLine &&
383 mStyle == aValue.mStyle;
385 bool operator!=(const TextDecorValue& aValue) const {
386 return !(*this == aValue);
389 private:
390 nscolor mColor;
391 mozilla::StyleTextDecorationLine mLine;
392 mozilla::StyleTextDecorationStyle mStyle;
395 class TextDecorTextAttr : public TTextAttr<TextDecorValue> {
396 public:
397 TextDecorTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame);
398 virtual ~TextDecorTextAttr() {}
400 protected:
401 // TextAttr
402 virtual bool GetValueFor(LocalAccessible* aAccessible,
403 TextDecorValue* aValue) override;
404 virtual void ExposeValue(AccAttributes* aAttributes,
405 const TextDecorValue& aValue) override;
409 * Class is used for the work with "text-position" text attribute.
412 enum TextPosValue { eTextPosBaseline, eTextPosSub, eTextPosSuper };
414 class TextPosTextAttr : public TTextAttr<Maybe<TextPosValue>> {
415 public:
416 TextPosTextAttr(nsIFrame* aRootFrame, nsIFrame* aFrame,
417 nsIContent* aRootElm, nsIContent* aElm);
418 virtual ~TextPosTextAttr() {}
420 protected:
421 // TextAttr
422 virtual bool GetValueFor(LocalAccessible* aAccessible,
423 Maybe<TextPosValue>* aValue) override;
424 virtual void ExposeValue(AccAttributes* aAttributes,
425 const Maybe<TextPosValue>& aValue) override;
427 private:
428 Maybe<TextPosValue> GetAriaTextPosValue(nsIContent* aElm) const;
429 Maybe<TextPosValue> GetAriaTextPosValue(nsIContent* aElm,
430 nsIFrame*& ariaFrame) const;
431 Maybe<TextPosValue> GetLayoutTextPosValue(nsIFrame* aFrame) const;
432 nsIContent* mRootElm;
435 }; // TextAttrMgr
437 } // namespace a11y
438 } // namespace mozilla
440 #endif