/src/mozilla-central/editor/libeditor/EditorUtils.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/EditorUtils.h" |
7 | | |
8 | | #include "mozilla/EditorDOMPoint.h" |
9 | | #include "mozilla/OwningNonNull.h" |
10 | | #include "mozilla/dom/Selection.h" |
11 | | #include "nsComponentManagerUtils.h" |
12 | | #include "nsError.h" |
13 | | #include "nsIContent.h" |
14 | | #include "nsIContentIterator.h" |
15 | | #include "nsIDocShell.h" |
16 | | #include "nsIDocument.h" |
17 | | #include "nsIInterfaceRequestorUtils.h" |
18 | | #include "nsINode.h" |
19 | | |
20 | | class nsISupports; |
21 | | class nsRange; |
22 | | |
23 | | namespace mozilla { |
24 | | |
25 | | using namespace dom; |
26 | | |
27 | | /****************************************************************************** |
28 | | * AutoSelectionRestorer |
29 | | *****************************************************************************/ |
30 | | |
31 | | AutoSelectionRestorer::AutoSelectionRestorer( |
32 | | Selection* aSelection, |
33 | | EditorBase* aEditorBase |
34 | | MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) |
35 | | : mEditorBase(nullptr) |
36 | 0 | { |
37 | 0 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
38 | 0 | if (NS_WARN_IF(!aSelection) || NS_WARN_IF(!aEditorBase)) { |
39 | 0 | return; |
40 | 0 | } |
41 | 0 | if (aEditorBase->ArePreservingSelection()) { |
42 | 0 | // We already have initialized mSavedSel, so this must be nested call. |
43 | 0 | return; |
44 | 0 | } |
45 | 0 | mSelection = aSelection; |
46 | 0 | mEditorBase = aEditorBase; |
47 | 0 | mEditorBase->PreserveSelectionAcrossActions(mSelection); |
48 | 0 | } |
49 | | |
50 | | AutoSelectionRestorer::~AutoSelectionRestorer() |
51 | 0 | { |
52 | 0 | NS_ASSERTION(!mSelection || mEditorBase, |
53 | 0 | "mEditorBase should be non-null when mSelection is"); |
54 | 0 | // mSelection will be null if this was nested call. |
55 | 0 | if (mSelection && mEditorBase->ArePreservingSelection()) { |
56 | 0 | mEditorBase->RestorePreservedSelection(mSelection); |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | void |
61 | | AutoSelectionRestorer::Abort() |
62 | 0 | { |
63 | 0 | NS_ASSERTION(!mSelection || mEditorBase, |
64 | 0 | "mEditorBase should be non-null when mSelection is"); |
65 | 0 | if (mSelection) { |
66 | 0 | mEditorBase->StopPreservingSelection(); |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | /****************************************************************************** |
71 | | * some helper classes for iterating the dom tree |
72 | | *****************************************************************************/ |
73 | | |
74 | | DOMIterator::DOMIterator(nsINode& aNode MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) |
75 | 0 | { |
76 | 0 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
77 | 0 | mIter = NS_NewContentIterator(); |
78 | 0 | DebugOnly<nsresult> rv = mIter->Init(&aNode); |
79 | 0 | MOZ_ASSERT(NS_SUCCEEDED(rv)); |
80 | 0 | } |
81 | | |
82 | | nsresult |
83 | | DOMIterator::Init(nsRange& aRange) |
84 | 0 | { |
85 | 0 | mIter = NS_NewContentIterator(); |
86 | 0 | return mIter->Init(&aRange); |
87 | 0 | } |
88 | | |
89 | | DOMIterator::DOMIterator(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) |
90 | 0 | { |
91 | 0 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
92 | 0 | } |
93 | | |
94 | | DOMIterator::~DOMIterator() |
95 | 0 | { |
96 | 0 | } |
97 | | |
98 | | void |
99 | | DOMIterator::AppendList(const BoolDomIterFunctor& functor, |
100 | | nsTArray<OwningNonNull<nsINode>>& arrayOfNodes) const |
101 | 0 | { |
102 | 0 | // Iterate through dom and build list |
103 | 0 | for (; !mIter->IsDone(); mIter->Next()) { |
104 | 0 | nsCOMPtr<nsINode> node = mIter->GetCurrentNode(); |
105 | 0 |
|
106 | 0 | if (functor(node)) { |
107 | 0 | arrayOfNodes.AppendElement(*node); |
108 | 0 | } |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | DOMSubtreeIterator::DOMSubtreeIterator( |
113 | | MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) |
114 | | : DOMIterator(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT) |
115 | 0 | { |
116 | 0 | } |
117 | | |
118 | | nsresult |
119 | | DOMSubtreeIterator::Init(nsRange& aRange) |
120 | 0 | { |
121 | 0 | mIter = NS_NewContentSubtreeIterator(); |
122 | 0 | return mIter->Init(&aRange); |
123 | 0 | } |
124 | | |
125 | | DOMSubtreeIterator::~DOMSubtreeIterator() |
126 | | { |
127 | | } |
128 | | |
129 | | /****************************************************************************** |
130 | | * some general purpose editor utils |
131 | | *****************************************************************************/ |
132 | | |
133 | | bool |
134 | | EditorUtils::IsDescendantOf(const nsINode& aNode, |
135 | | const nsINode& aParent, |
136 | | EditorRawDOMPoint* aOutPoint /* = nullptr */) |
137 | 0 | { |
138 | 0 | if (aOutPoint) { |
139 | 0 | aOutPoint->Clear(); |
140 | 0 | } |
141 | 0 |
|
142 | 0 | if (&aNode == &aParent) { |
143 | 0 | return false; |
144 | 0 | } |
145 | 0 | |
146 | 0 | for (const nsINode* node = &aNode; node; node = node->GetParentNode()) { |
147 | 0 | if (node->GetParentNode() == &aParent) { |
148 | 0 | if (aOutPoint) { |
149 | 0 | MOZ_ASSERT(node->IsContent()); |
150 | 0 | aOutPoint->Set(node->AsContent()); |
151 | 0 | } |
152 | 0 | return true; |
153 | 0 | } |
154 | 0 | } |
155 | 0 |
|
156 | 0 | return false; |
157 | 0 | } |
158 | | |
159 | | bool |
160 | | EditorUtils::IsDescendantOf(const nsINode& aNode, |
161 | | const nsINode& aParent, |
162 | | EditorDOMPoint* aOutPoint) |
163 | 0 | { |
164 | 0 | MOZ_ASSERT(aOutPoint); |
165 | 0 | aOutPoint->Clear(); |
166 | 0 | if (&aNode == &aParent) { |
167 | 0 | return false; |
168 | 0 | } |
169 | 0 | |
170 | 0 | for (const nsINode* node = &aNode; node; node = node->GetParentNode()) { |
171 | 0 | if (node->GetParentNode() == &aParent) { |
172 | 0 | MOZ_ASSERT(node->IsContent()); |
173 | 0 | aOutPoint->Set(node->AsContent()); |
174 | 0 | return true; |
175 | 0 | } |
176 | 0 | } |
177 | 0 |
|
178 | 0 | return false; |
179 | 0 | } |
180 | | |
181 | | } // namespace mozilla |