Bug 1686495 [wpt PR 27132] - Add tests for proposed WebDriver Shadow DOM support...
[gecko.git] / editor / libeditor / TypeInState.h
blob5eb12bb84624f4b7427f829c1bed7d9676951cec
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_TypeInState_h
7 #define mozilla_TypeInState_h
9 #include "mozilla/EditorDOMPoint.h"
10 #include "mozilla/EventForwards.h"
11 #include "mozilla/UniquePtr.h"
12 #include "nsCOMPtr.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsGkAtoms.h"
15 #include "nsISupportsImpl.h"
16 #include "nsString.h"
17 #include "nsTArray.h"
18 #include "nscore.h"
20 // Workaround for windows headers
21 #ifdef SetProp
22 # undef SetProp
23 #endif
25 class nsAtom;
26 class nsINode;
28 namespace mozilla {
29 class HTMLEditor;
30 namespace dom {
31 class MouseEvent;
32 class Selection;
33 } // namespace dom
35 struct PropItem {
36 nsAtom* tag;
37 nsAtom* attr;
38 nsString value;
40 PropItem() : tag(nullptr), attr(nullptr) { MOZ_COUNT_CTOR(PropItem); }
41 PropItem(nsAtom* aTag, nsAtom* aAttr, const nsAString& aValue)
42 : tag(aTag),
43 attr(aAttr != nsGkAtoms::_empty ? aAttr : nullptr),
44 value(aValue) {
45 MOZ_COUNT_CTOR(PropItem);
47 MOZ_COUNTED_DTOR(PropItem)
50 class StyleCache final {
51 public:
52 StyleCache() = delete;
53 StyleCache(nsStaticAtom* aTag, nsStaticAtom* aAttribute,
54 const nsAString& aValue)
55 : mTag(aTag), mAttribute(aAttribute), mValue(aValue) {
56 MOZ_ASSERT(mTag);
59 nsStaticAtom* Tag() const { return mTag; }
60 nsStaticAtom* GetAttribute() const { return mAttribute; }
61 const nsString& Value() const { return mValue; }
63 private:
64 nsStaticAtom* mTag;
65 nsStaticAtom* mAttribute;
66 nsString mValue;
69 class MOZ_STACK_CLASS AutoStyleCacheArray final
70 : public AutoTArray<StyleCache, 19> {
71 public:
72 index_type IndexOf(const nsStaticAtom* aTag,
73 const nsStaticAtom* aAttribute) const {
74 for (index_type index = 0; index < Length(); ++index) {
75 const StyleCache& styleCache = ElementAt(index);
76 if (styleCache.Tag() == aTag && styleCache.GetAttribute() == aAttribute) {
77 return index;
80 return NoIndex;
84 class TypeInState final {
85 public:
86 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TypeInState)
87 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(TypeInState)
89 TypeInState();
90 void Reset();
92 nsresult UpdateSelState(dom::Selection* aSelection);
94 /**
95 * PreHandleMouseEvent() is called when `HTMLEditorEventListener` receives
96 * "mousedown" and "mouseup" events. Note that `aMouseDownOrUpEvent` may not
97 * be acceptable event for the `HTMLEditor`, but this is called even in
98 * the case because the event may cause a following `OnSelectionChange()`
99 * call.
101 void PreHandleMouseEvent(const dom::MouseEvent& aMouseDownOrUpEvent);
103 void PreHandleSelectionChangeCommand(Command aCommand);
104 void PostHandleSelectionChangeCommand(const HTMLEditor& aHTMLEditor,
105 Command aCommand);
107 void OnSelectionChange(const HTMLEditor& aHTMLEditor, int16_t aReason);
109 void SetProp(nsAtom* aProp, nsAtom* aAttr, const nsAString& aValue);
111 void ClearAllProps();
112 void ClearProp(nsAtom* aProp, nsAtom* aAttr);
115 * TakeClearProperty() hands back next property item on the clear list.
116 * Caller assumes ownership of PropItem and must delete it.
118 UniquePtr<PropItem> TakeClearProperty();
121 * TakeSetProperty() hands back next property item on the set list.
122 * Caller assumes ownership of PropItem and must delete it.
124 UniquePtr<PropItem> TakeSetProperty();
127 * TakeRelativeFontSize() hands back relative font value, which is then
128 * cleared out.
130 int32_t TakeRelativeFontSize();
132 void GetTypingState(bool& isSet, bool& theSetting, nsAtom* aProp,
133 nsAtom* aAttr = nullptr, nsString* outValue = nullptr);
135 static bool FindPropInList(nsAtom* aProp, nsAtom* aAttr, nsAString* outValue,
136 const nsTArray<PropItem*>& aList,
137 int32_t& outIndex);
139 protected:
140 virtual ~TypeInState();
142 void RemovePropFromSetList(nsAtom* aProp, nsAtom* aAttr);
143 void RemovePropFromClearedList(nsAtom* aProp, nsAtom* aAttr);
144 bool IsPropSet(nsAtom* aProp, nsAtom* aAttr, nsAString* outValue);
145 bool IsPropSet(nsAtom* aProp, nsAtom* aAttr, nsAString* outValue,
146 int32_t& outIndex);
147 bool IsPropCleared(nsAtom* aProp, nsAtom* aAttr);
148 bool IsPropCleared(nsAtom* aProp, nsAtom* aAttr, int32_t& outIndex);
150 bool IsLinkStyleSet() const {
151 int32_t unusedIndex = -1;
152 return FindPropInList(nsGkAtoms::a, nullptr, nullptr, mSetArray,
153 unusedIndex);
155 bool IsExplicitlyLinkStyleCleared() const {
156 int32_t unusedIndex = -1;
157 return FindPropInList(nsGkAtoms::a, nullptr, nullptr, mClearedArray,
158 unusedIndex);
160 bool IsOnlyLinkStyleCleared() const {
161 return mClearedArray.Length() == 1 && IsExplicitlyLinkStyleCleared();
163 bool AreAllStylesCleared() const {
164 int32_t unusedIndex = -1;
165 return FindPropInList(nullptr, nullptr, nullptr, mClearedArray,
166 unusedIndex);
168 bool AreSomeStylesSet() const { return !mSetArray.IsEmpty(); }
169 bool AreSomeStylesCleared() const { return !mClearedArray.IsEmpty(); }
171 nsTArray<PropItem*> mSetArray;
172 nsTArray<PropItem*> mClearedArray;
173 EditorDOMPoint mLastSelectionPoint;
174 int32_t mRelativeFontSize;
175 Command mLastSelectionCommand;
176 bool mMouseDownFiredInLinkElement;
177 bool mMouseUpFiredInLinkElement;
180 } // namespace mozilla
182 #endif // #ifndef mozilla_TypeInState_h