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