1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/src/base/TextUpdater.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_a11y_TextUpdater_h__ 1.10 +#define mozilla_a11y_TextUpdater_h__ 1.11 + 1.12 +#include "AccEvent.h" 1.13 +#include "HyperTextAccessible.h" 1.14 + 1.15 +namespace mozilla { 1.16 +namespace a11y { 1.17 + 1.18 +/** 1.19 + * Used to find a difference between old and new text and fire text change 1.20 + * events. 1.21 + */ 1.22 +class TextUpdater 1.23 +{ 1.24 +public: 1.25 + /** 1.26 + * Start text of the text leaf update. 1.27 + */ 1.28 + static void Run(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf, 1.29 + const nsAString& aNewText); 1.30 + 1.31 +private: 1.32 + TextUpdater(DocAccessible* aDocument, TextLeafAccessible* aTextLeaf) : 1.33 + mDocument(aDocument), mTextLeaf(aTextLeaf), mHyperText(nullptr), 1.34 + mTextOffset(-1) { } 1.35 + 1.36 + ~TextUpdater() 1.37 + { mDocument = nullptr; mTextLeaf = nullptr; mHyperText = nullptr; } 1.38 + 1.39 + /** 1.40 + * Update text of the text leaf accessible, fire text change and value change 1.41 + * (if applicable) events for its container hypertext accessible. 1.42 + */ 1.43 + void DoUpdate(const nsAString& aNewText, const nsAString& aOldText, 1.44 + uint32_t aSkipStart); 1.45 + 1.46 +private: 1.47 + TextUpdater(); 1.48 + TextUpdater(const TextUpdater&); 1.49 + TextUpdater& operator=(const TextUpdater&); 1.50 + 1.51 + /** 1.52 + * Fire text change events based on difference between strings. 1.53 + */ 1.54 + void ComputeTextChangeEvents(const nsAString& aStr1, 1.55 + const nsAString& aStr2, 1.56 + uint32_t* aEntries, 1.57 + nsTArray<nsRefPtr<AccEvent> >& aEvents); 1.58 + 1.59 + /** 1.60 + * Helper to create text change events for inserted text. 1.61 + */ 1.62 + inline void FireInsertEvent(const nsAString& aText, uint32_t aAddlOffset, 1.63 + nsTArray<nsRefPtr<AccEvent> >& aEvents) 1.64 + { 1.65 + nsRefPtr<AccEvent> event = 1.66 + new AccTextChangeEvent(mHyperText, mTextOffset + aAddlOffset, 1.67 + aText, true); 1.68 + aEvents.AppendElement(event); 1.69 + } 1.70 + 1.71 + /** 1.72 + * Helper to create text change events for removed text. 1.73 + */ 1.74 + inline void FireDeleteEvent(const nsAString& aText, uint32_t aAddlOffset, 1.75 + nsTArray<nsRefPtr<AccEvent> >& aEvents) 1.76 + { 1.77 + nsRefPtr<AccEvent> event = 1.78 + new AccTextChangeEvent(mHyperText, mTextOffset + aAddlOffset, 1.79 + aText, false); 1.80 + aEvents.AppendElement(event); 1.81 + } 1.82 + 1.83 + /** 1.84 + * The constant used to skip string difference calculation in case of long 1.85 + * strings. 1.86 + */ 1.87 + const static uint32_t kMaxStrLen = 1 << 6; 1.88 + 1.89 +private: 1.90 + DocAccessible* mDocument; 1.91 + TextLeafAccessible* mTextLeaf; 1.92 + HyperTextAccessible* mHyperText; 1.93 + int32_t mTextOffset; 1.94 +}; 1.95 + 1.96 +} // namespace a11y 1.97 +} // namespace mozilla 1.98 + 1.99 +#endif