HyperTextAccessible.h (8101B)
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 mozilla_a11y_HyperTextAccessible_h__ 7 #define mozilla_a11y_HyperTextAccessible_h__ 8 9 #include "AccessibleWrap.h" 10 #include "mozilla/a11y/HyperTextAccessibleBase.h" 11 #include "nsIAccessibleText.h" 12 #include "nsIAccessibleTypes.h" 13 #include "nsIFrame.h" // only for nsSelectionAmount 14 #include "nsISelectionController.h" 15 16 class nsFrameSelection; 17 class nsIFrame; 18 class nsRange; 19 class nsIWidget; 20 21 namespace mozilla { 22 class EditorBase; 23 namespace dom { 24 class Selection; 25 } 26 27 namespace a11y { 28 29 class TextLeafPoint; 30 class TextRange; 31 32 struct DOMPoint { 33 DOMPoint() : node(nullptr), idx(0) {} 34 DOMPoint(nsINode* aNode, int32_t aIdx) : node(aNode), idx(aIdx) {} 35 36 nsINode* node; 37 int32_t idx; 38 }; 39 40 /** 41 * Special Accessible that knows how contain both text and embedded objects 42 */ 43 class HyperTextAccessible : public AccessibleWrap, 44 public HyperTextAccessibleBase { 45 public: 46 HyperTextAccessible(nsIContent* aContent, DocAccessible* aDoc); 47 48 NS_INLINE_DECL_REFCOUNTING_INHERITED(HyperTextAccessible, AccessibleWrap) 49 50 // LocalAccessible 51 virtual already_AddRefed<AccAttributes> NativeAttributes() override; 52 virtual mozilla::a11y::role NativeRole() const override; 53 virtual uint64_t NativeState() const override; 54 55 virtual void Shutdown() override; 56 virtual bool RemoveChild(LocalAccessible* aAccessible) override; 57 virtual bool InsertChildAt(uint32_t aIndex, LocalAccessible* aChild) override; 58 virtual void RelocateChild(uint32_t aNewIndex, 59 LocalAccessible* aChild) override; 60 virtual Relation RelationByType(RelationType aType) const override; 61 62 // HyperTextAccessible (static helper method) 63 64 // Convert content offset to rendered text offset 65 nsresult ContentToRenderedOffset(nsIFrame* aFrame, int32_t aContentOffset, 66 uint32_t* aRenderedOffset) const; 67 68 // Convert rendered text offset to content offset 69 nsresult RenderedToContentOffset(nsIFrame* aFrame, uint32_t aRenderedOffset, 70 int32_t* aContentOffset) const; 71 72 ////////////////////////////////////////////////////////////////////////////// 73 // HyperLinkAccessible 74 75 /** 76 * Return link accessible at the given index. 77 */ 78 LocalAccessible* LinkAt(uint32_t aIndex) { 79 Accessible* child = EmbeddedChildAt(aIndex); 80 return child ? child->AsLocal() : nullptr; 81 } 82 83 ////////////////////////////////////////////////////////////////////////////// 84 // HyperTextAccessible: DOM point to text offset conversions. 85 86 /** 87 * Turn a DOM point (node and offset) into a character offset of this 88 * hypertext. Will look for closest match when the DOM node does not have 89 * an accessible object associated with it. Will return an offset for the end 90 * of the string if the node is not found. 91 * 92 * @param aNode [in] the node to look for 93 * @param aNodeOffset [in] the offset to look for 94 * if -1 just look directly for the node 95 * if >=0 and aNode is text, this represents a char 96 * offset if >=0 and aNode is not text, this represents a child node offset 97 * @param aIsEndOffset [in] if true, then this offset is not inclusive. The 98 * character indicated by the offset returned is at [offset - 1]. This means 99 * if the passed-in offset is really in a descendant, then the offset 100 * returned will come just after the relevant embedded object characer. If 101 * false, then the offset is inclusive. The character indicated by the offset 102 * returned is at [offset]. If the passed-in offset in inside a descendant, 103 * then the returned offset will be on the relevant embedded object char. 104 */ 105 uint32_t DOMPointToOffset(nsINode* aNode, int32_t aNodeOffset, 106 bool aIsEndOffset = false) const; 107 108 /** 109 * Transform the given a11y point into the offset relative this hypertext. 110 */ 111 uint32_t TransformOffset(LocalAccessible* aDescendant, uint32_t aOffset, 112 bool aIsEndOffset) const; 113 114 /** 115 * Convert the given offset into DOM point. 116 * 117 * If offset is at text leaf then DOM point is (text node, offsetInTextNode), 118 * if before embedded object then (parent node, indexInParent), if after then 119 * (parent node, indexInParent + 1). 120 */ 121 DOMPoint OffsetToDOMPoint(int32_t aOffset) const; 122 123 ////////////////////////////////////////////////////////////////////////////// 124 // TextAccessible 125 126 virtual already_AddRefed<AccAttributes> DefaultTextAttributes() override; 127 128 // HyperTextAccessibleBase provides an overload which takes an Accessible. 129 using HyperTextAccessibleBase::GetChildOffset; 130 131 virtual LocalAccessible* GetChildAtOffset(uint32_t aOffset) const override { 132 return LocalChildAt(GetChildIndexAtOffset(aOffset)); 133 } 134 135 /** 136 * Return an offset at the given point. 137 */ 138 int32_t OffsetAtPoint(int32_t aX, int32_t aY, uint32_t aCoordType) override; 139 140 /** 141 * Get/set caret offset, if no caret then -1. 142 */ 143 virtual int32_t CaretOffset() const override; 144 145 virtual std::pair<mozilla::LayoutDeviceIntRect, nsIWidget*> GetCaretRect() 146 override; 147 148 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual bool RemoveFromSelection( 149 int32_t aSelectionNum) override; 150 151 virtual void ScrollSubstringToPoint(int32_t aStartOffset, int32_t aEndOffset, 152 uint32_t aCoordinateType, int32_t aX, 153 int32_t aY) override; 154 155 virtual void SelectionRanges(nsTArray<TextRange>* aRanges) const override; 156 157 ////////////////////////////////////////////////////////////////////////////// 158 // EditableTextAccessible 159 160 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void ReplaceText( 161 const nsAString& aText) override; 162 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void InsertText( 163 const nsAString& aText, int32_t aPosition) override; 164 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void CopyText(int32_t aStartPos, 165 int32_t aEndPos) override; 166 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void CutText(int32_t aStartPos, 167 int32_t aEndPos) override; 168 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void DeleteText(int32_t aStartPos, 169 int32_t aEndPos) override; 170 MOZ_CAN_RUN_SCRIPT virtual void PasteText(int32_t aPosition) override; 171 172 /** 173 * Return the editor associated with the accessible. 174 * The result may be either TextEditor or HTMLEditor. 175 */ 176 virtual already_AddRefed<EditorBase> GetEditor() const; 177 178 /** 179 * Return DOM selection object for the accessible. 180 */ 181 dom::Selection* DOMSelection() const; 182 183 protected: 184 virtual ~HyperTextAccessible() {} 185 186 // LocalAccessible 187 virtual ENameValueFlag NativeName(nsString& aName) const override; 188 189 // HyperTextAccessible 190 191 // Selection helpers 192 193 /** 194 * Return frame selection object for the accessible. 195 */ 196 already_AddRefed<nsFrameSelection> FrameSelection() const; 197 198 // Helpers 199 200 /** 201 * Set xml-roles attributes for MathML elements. 202 * @param aAttributes 203 */ 204 void SetMathMLXMLRoles(AccAttributes* aAttributes); 205 206 // HyperTextAccessibleBase 207 virtual const Accessible* Acc() const override { return this; } 208 209 virtual nsTArray<int32_t>& GetCachedHyperTextOffsets() override { 210 return mOffsets; 211 } 212 213 private: 214 /** 215 * End text offsets array. 216 */ 217 mutable nsTArray<int32_t> mOffsets; 218 }; 219 220 //////////////////////////////////////////////////////////////////////////////// 221 // LocalAccessible downcasting method 222 223 inline HyperTextAccessible* LocalAccessible::AsHyperText() { 224 return IsHyperText() ? static_cast<HyperTextAccessible*>(this) : nullptr; 225 } 226 227 inline HyperTextAccessibleBase* LocalAccessible::AsHyperTextBase() { 228 return AsHyperText(); 229 } 230 231 } // namespace a11y 232 } // namespace mozilla 233 234 #endif