TextRange.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef mozilla_a11y_TextRange_h__
  6. #define mozilla_a11y_TextRange_h__
  7. #include "mozilla/Move.h"
  8. #include "nsCaseTreatment.h"
  9. #include "nsRect.h"
  10. #include "nsTArray.h"
  11. class nsIVariant;
  12. namespace mozilla {
  13. namespace a11y {
  14. class Accessible;
  15. class HyperTextAccessible;
  16. /**
  17. * A text point (hyper text + offset), represents a boundary of text range.
  18. */
  19. struct TextPoint final
  20. {
  21. TextPoint(HyperTextAccessible* aContainer, int32_t aOffset) :
  22. mContainer(aContainer), mOffset(aOffset) { }
  23. TextPoint(const TextPoint& aPoint) :
  24. mContainer(aPoint.mContainer), mOffset(aPoint.mOffset) { }
  25. HyperTextAccessible* mContainer;
  26. int32_t mOffset;
  27. bool operator ==(const TextPoint& aPoint) const
  28. { return mContainer == aPoint.mContainer && mOffset == aPoint.mOffset; }
  29. bool operator <(const TextPoint& aPoint) const;
  30. };
  31. /**
  32. * Represents a text range within the text control or document.
  33. */
  34. class TextRange final
  35. {
  36. public:
  37. TextRange(HyperTextAccessible* aRoot,
  38. HyperTextAccessible* aStartContainer, int32_t aStartOffset,
  39. HyperTextAccessible* aEndContainer, int32_t aEndOffset);
  40. TextRange() {}
  41. TextRange(TextRange&& aRange) :
  42. mRoot(mozilla::Move(aRange.mRoot)),
  43. mStartContainer(mozilla::Move(aRange.mStartContainer)),
  44. mEndContainer(mozilla::Move(aRange.mEndContainer)),
  45. mStartOffset(aRange.mStartOffset), mEndOffset(aRange.mEndOffset) {}
  46. TextRange& operator= (TextRange&& aRange)
  47. {
  48. mRoot = mozilla::Move(aRange.mRoot);
  49. mStartContainer = mozilla::Move(aRange.mStartContainer);
  50. mEndContainer = mozilla::Move(aRange.mEndContainer);
  51. mStartOffset = aRange.mStartOffset;
  52. mEndOffset = aRange.mEndOffset;
  53. return *this;
  54. }
  55. HyperTextAccessible* StartContainer() const { return mStartContainer; }
  56. int32_t StartOffset() const { return mStartOffset; }
  57. HyperTextAccessible* EndContainer() const { return mEndContainer; }
  58. int32_t EndOffset() const { return mEndOffset; }
  59. bool operator ==(const TextRange& aRange) const
  60. {
  61. return mStartContainer == aRange.mStartContainer &&
  62. mStartOffset == aRange.mStartOffset &&
  63. mEndContainer == aRange.mEndContainer && mEndOffset == aRange.mEndOffset;
  64. }
  65. TextPoint StartPoint() const { return TextPoint(mStartContainer, mStartOffset); }
  66. TextPoint EndPoint() const { return TextPoint(mEndContainer, mEndOffset); }
  67. /**
  68. * Return a container containing both start and end points.
  69. */
  70. Accessible* Container() const;
  71. /**
  72. * Return a list of embedded objects enclosed by the text range (includes
  73. * partially overlapped objects).
  74. */
  75. void EmbeddedChildren(nsTArray<Accessible*>* aChildren) const;
  76. /**
  77. * Return text enclosed by the range.
  78. */
  79. void Text(nsAString& aText) const;
  80. /**
  81. * Return list of bounding rects of the text range by lines.
  82. */
  83. void Bounds(nsTArray<nsIntRect> aRects) const;
  84. enum ETextUnit {
  85. eFormat,
  86. eWord,
  87. eLine,
  88. eParagraph,
  89. ePage,
  90. eDocument
  91. };
  92. /**
  93. * Move the range or its points on specified amount of given units.
  94. */
  95. void Move(ETextUnit aUnit, int32_t aCount)
  96. {
  97. MoveEnd(aUnit, aCount);
  98. MoveStart(aUnit, aCount);
  99. }
  100. void MoveStart(ETextUnit aUnit, int32_t aCount)
  101. {
  102. MoveInternal(aUnit, aCount, *mStartContainer, mStartOffset,
  103. mEndContainer, mEndOffset);
  104. }
  105. void MoveEnd(ETextUnit aUnit, int32_t aCount)
  106. { MoveInternal(aUnit, aCount, *mEndContainer, mEndOffset); }
  107. /**
  108. * Move the range points to the closest unit boundaries.
  109. */
  110. void Normalize(ETextUnit aUnit);
  111. /**
  112. * Crops the range if it overlaps the given accessible element boundaries,
  113. * returns true if the range was cropped successfully.
  114. */
  115. bool Crop(Accessible* aContainer);
  116. enum EDirection {
  117. eBackward,
  118. eForward
  119. };
  120. /**
  121. * Return range enclosing the found text.
  122. */
  123. void FindText(const nsAString& aText, EDirection aDirection,
  124. nsCaseTreatment aCaseSensitive, TextRange* aFoundRange) const;
  125. enum EAttr {
  126. eAnimationStyleAttr,
  127. eAnnotationObjectsAttr,
  128. eAnnotationTypesAttr,
  129. eBackgroundColorAttr,
  130. eBulletStyleAttr,
  131. eCapStyleAttr,
  132. eCaretBidiModeAttr,
  133. eCaretPositionAttr,
  134. eCultureAttr,
  135. eFontNameAttr,
  136. eFontSizeAttr,
  137. eFontWeightAttr,
  138. eForegroundColorAttr,
  139. eHorizontalTextAlignmentAttr,
  140. eIndentationFirstLineAttr,
  141. eIndentationLeadingAttr,
  142. eIndentationTrailingAttr,
  143. eIsActiveAttr,
  144. eIsHiddenAttr,
  145. eIsItalicAttr,
  146. eIsReadOnlyAttr,
  147. eIsSubscriptAttr,
  148. eIsSuperscriptAttr,
  149. eLinkAttr,
  150. eMarginBottomAttr,
  151. eMarginLeadingAttr,
  152. eMarginTopAttr,
  153. eMarginTrailingAttr,
  154. eOutlineStylesAttr,
  155. eOverlineColorAttr,
  156. eOverlineStyleAttr,
  157. eSelectionActiveEndAttr,
  158. eStrikethroughColorAttr,
  159. eStrikethroughStyleAttr,
  160. eStyleIdAttr,
  161. eStyleNameAttr,
  162. eTabsAttr,
  163. eTextFlowDirectionsAttr,
  164. eUnderlineColorAttr,
  165. eUnderlineStyleAttr
  166. };
  167. /**
  168. * Return range enclosing text having requested attribute.
  169. */
  170. void FindAttr(EAttr aAttr, nsIVariant* aValue, EDirection aDirection,
  171. TextRange* aFoundRange) const;
  172. /**
  173. * Add/remove the text range from selection.
  174. */
  175. void AddToSelection() const;
  176. void RemoveFromSelection() const;
  177. void Select() const;
  178. /**
  179. * Scroll the text range into view.
  180. */
  181. enum EHowToAlign {
  182. eAlignToTop,
  183. eAlignToBottom
  184. };
  185. void ScrollIntoView(EHowToAlign aHow) const;
  186. /**
  187. * Return true if this TextRange object represents an actual range of text.
  188. */
  189. bool IsValid() const { return mRoot; }
  190. void SetStartPoint(HyperTextAccessible* aContainer, int32_t aOffset)
  191. { mStartContainer = aContainer; mStartOffset = aOffset; }
  192. void SetEndPoint(HyperTextAccessible* aContainer, int32_t aOffset)
  193. { mStartContainer = aContainer; mStartOffset = aOffset; }
  194. private:
  195. TextRange(const TextRange& aRange) = delete;
  196. TextRange& operator=(const TextRange& aRange) = delete;
  197. friend class HyperTextAccessible;
  198. friend class xpcAccessibleTextRange;
  199. void Set(HyperTextAccessible* aRoot,
  200. HyperTextAccessible* aStartContainer, int32_t aStartOffset,
  201. HyperTextAccessible* aEndContainer, int32_t aEndOffset);
  202. /**
  203. * Text() method helper.
  204. * @param aText [in,out] calculated text
  205. * @param aCurrent [in] currently traversed node
  206. * @param aStartIntlOffset [in] start offset if current node is a text node
  207. * @return true if calculation is not finished yet
  208. */
  209. bool TextInternal(nsAString& aText, Accessible* aCurrent,
  210. uint32_t aStartIntlOffset) const;
  211. void MoveInternal(ETextUnit aUnit, int32_t aCount,
  212. HyperTextAccessible& aContainer, int32_t aOffset,
  213. HyperTextAccessible* aStopContainer = nullptr,
  214. int32_t aStopOffset = 0);
  215. /**
  216. * A helper method returning a common parent for two given accessible
  217. * elements.
  218. */
  219. Accessible* CommonParent(Accessible* aAcc1, Accessible* aAcc2,
  220. nsTArray<Accessible*>* aParents1, uint32_t* aPos1,
  221. nsTArray<Accessible*>* aParents2, uint32_t* aPos2) const;
  222. RefPtr<HyperTextAccessible> mRoot;
  223. RefPtr<HyperTextAccessible> mStartContainer;
  224. RefPtr<HyperTextAccessible> mEndContainer;
  225. int32_t mStartOffset;
  226. int32_t mEndOffset;
  227. };
  228. } // namespace a11y
  229. } // namespace mozilla
  230. #endif