Bug 1788251 [wpt PR 35638] - Prerender2: allow referrer policy mismatches, a=testonly
[gecko.git] / accessible / base / TextUpdater.h
blob87905cf082f86cccd9b54d5bfc1141a8fac1e660
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 mozilla_a11y_TextUpdater_h__
7 #define mozilla_a11y_TextUpdater_h__
9 #include "AccEvent.h"
10 #include "HyperTextAccessible.h"
12 namespace mozilla {
13 namespace a11y {
15 /**
16 * Used to find a difference between old and new text and fire text change
17 * events.
19 class TextUpdater {
20 public:
21 /**
22 * Start text of the text leaf update.
24 static void Run(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf,
25 const nsAString& aNewText);
27 private:
28 TextUpdater(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf)
29 : mDocument(aDocument),
30 mTextLeaf(aTextLeaf),
31 mHyperText(nullptr),
32 mTextOffset(-1) {}
34 ~TextUpdater() {
35 mDocument = nullptr;
36 mTextLeaf = nullptr;
37 mHyperText = nullptr;
40 /**
41 * Update text of the text leaf accessible, fire text change and value change
42 * (if applicable) events for its container hypertext accessible.
44 void DoUpdate(const nsAString& aNewText, const nsAString& aOldText,
45 uint32_t aSkipStart);
47 private:
48 TextUpdater();
49 TextUpdater(const TextUpdater&);
50 TextUpdater& operator=(const TextUpdater&);
52 /**
53 * Fire text change events based on difference between strings.
55 void ComputeTextChangeEvents(const nsAString& aStr1, const nsAString& aStr2,
56 uint32_t* aEntries,
57 nsTArray<RefPtr<AccEvent> >& aEvents);
59 /**
60 * Helper to create text change events for inserted text.
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);
69 /**
70 * Helper to create text change events for removed text.
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);
79 /**
80 * The constant used to skip string difference calculation in case of long
81 * strings.
83 const static uint32_t kMaxStrLen = 1 << 6;
85 private:
86 DocAccessible* mDocument;
87 TextLeafAccessible* mTextLeaf;
88 HyperTextAccessible* mHyperText;
89 int32_t mTextOffset;
92 } // namespace a11y
93 } // namespace mozilla
95 #endif