TextUpdater.h (2785B)
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_TextUpdater_h__ 7 #define mozilla_a11y_TextUpdater_h__ 8 9 #include "AccEvent.h" 10 #include "HyperTextAccessible.h" 11 12 namespace mozilla { 13 namespace a11y { 14 15 /** 16 * Used to find a difference between old and new text and fire text change 17 * events. 18 */ 19 class TextUpdater { 20 public: 21 /** 22 * Start text of the text leaf update. 23 */ 24 static void Run(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf, 25 const nsAString& aNewText); 26 27 private: 28 TextUpdater(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf) 29 : mDocument(aDocument), 30 mTextLeaf(aTextLeaf), 31 mHyperText(nullptr), 32 mTextOffset(-1) {} 33 34 ~TextUpdater() { 35 mDocument = nullptr; 36 mTextLeaf = nullptr; 37 mHyperText = nullptr; 38 } 39 40 /** 41 * Update text of the text leaf accessible, fire text change and value change 42 * (if applicable) events for its container hypertext accessible. 43 */ 44 void DoUpdate(const nsAString& aNewText, const nsAString& aOldText, 45 uint32_t aSkipStart); 46 47 private: 48 TextUpdater(); 49 TextUpdater(const TextUpdater&); 50 TextUpdater& operator=(const TextUpdater&); 51 52 /** 53 * Fire text change events based on difference between strings. 54 */ 55 void ComputeTextChangeEvents(const nsAString& aStr1, const nsAString& aStr2, 56 uint32_t* aEntries, 57 nsTArray<RefPtr<AccEvent> >& aEvents); 58 59 /** 60 * Helper to create text change events for inserted text. 61 */ 62 inline void FireInsertEvent(const nsAString& aText, uint32_t aAddlOffset, 63 nsTArray<RefPtr<AccEvent> >& aEvents) { 64 RefPtr<AccEvent> event = new AccTextChangeEvent( 65 mHyperText, mTextOffset + aAddlOffset, aText, true); 66 aEvents.AppendElement(event); 67 } 68 69 /** 70 * Helper to create text change events for removed text. 71 */ 72 inline void FireDeleteEvent(const nsAString& aText, uint32_t aAddlOffset, 73 nsTArray<RefPtr<AccEvent> >& aEvents) { 74 RefPtr<AccEvent> event = new AccTextChangeEvent( 75 mHyperText, mTextOffset + aAddlOffset, aText, false); 76 aEvents.AppendElement(event); 77 } 78 79 /** 80 * The constant used to skip string difference calculation in case of long 81 * strings. 82 */ 83 const static uint32_t kMaxStrLen = 1 << 6; 84 85 private: 86 DocAccessible* mDocument; 87 TextLeafAccessible* mTextLeaf; 88 HyperTextAccessible* mHyperText; 89 int32_t mTextOffset; 90 }; 91 92 } // namespace a11y 93 } // namespace mozilla 94 95 #endif