@@ -165,47 +165,78 @@ ElementDeletionObserver::NodeWillBeDestroyed(const nsINode* aNode)
165
165
// child of aParentNode. If aIsCreatedHidden is true, the class
166
166
// "hidden" is added to the created element. If aAnonClass is not
167
167
// the empty string, it becomes the value of the attribute "_moz_anonclass"
168
- nsresult
168
+ NS_IMETHODIMP
169
169
HTMLEditor::CreateAnonymousElement (const nsAString& aTag,
170
170
nsIDOMNode* aParentNode,
171
171
const nsAString& aAnonClass,
172
172
bool aIsCreatedHidden,
173
173
nsIDOMElement** aReturn)
174
174
{
175
- NS_ENSURE_ARG_POINTER (aParentNode);
176
175
NS_ENSURE_ARG_POINTER (aReturn);
177
176
*aReturn = nullptr ;
178
177
179
- nsCOMPtr<nsIContent> parentContent ( do_QueryInterface (aParentNode) );
180
- NS_ENSURE_TRUE (parentContent, NS_OK);
178
+ nsCOMPtr<nsIAtom> atom = NS_Atomize(aTag);
179
+ RefPtr<Element> element =
180
+ CreateAnonymousElement (atom, aParentNode, aAnonClass, aIsCreatedHidden);
181
+ if (NS_WARN_IF(!element)) {
182
+ return NS_ERROR_FAILURE;
183
+ }
184
+ nsCOMPtr<nsIDOMElement> newElement =
185
+ static_cast <nsIDOMElement*>(GetAsDOMNode (element));
186
+ newElement.forget (aReturn);
187
+ return NS_OK;
188
+ }
189
+
190
+ already_AddRefed<Element>
191
+ HTMLEditor::CreateAnonymousElement (nsIAtom* aTag,
192
+ nsIDOMNode* aParentNode,
193
+ const nsAString& aAnonClass,
194
+ bool aIsCreatedHidden)
195
+ {
196
+ if (NS_WARN_IF(!aParentNode)) {
197
+ return nullptr ;
198
+ }
199
+
200
+ nsCOMPtr<nsIContent> parentContent = do_QueryInterface (aParentNode);
201
+ if (NS_WARN_IF(!parentContent)) {
202
+ return nullptr ;
203
+ }
181
204
182
205
nsCOMPtr<nsIDocument> doc = GetDocument ();
183
- NS_ENSURE_TRUE (doc, NS_ERROR_NULL_POINTER);
206
+ if (NS_WARN_IF(!doc)) {
207
+ return nullptr ;
208
+ }
184
209
185
210
// Get the pres shell
186
211
nsCOMPtr<nsIPresShell> ps = GetPresShell ();
187
- NS_ENSURE_TRUE (ps, NS_ERROR_NOT_INITIALIZED);
212
+ if (NS_WARN_IF(!ps)) {
213
+ return nullptr ;
214
+ }
188
215
189
216
// Create a new node through the element factory
190
- nsCOMPtr<nsIAtom> tagAtom = NS_Atomize(aTag);
191
- nsCOMPtr<Element> newContent = CreateHTMLContent (tagAtom);
192
- NS_ENSURE_STATE (newContent);
193
-
194
- nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface (newContent);
195
- NS_ENSURE_TRUE (newElement, NS_ERROR_FAILURE);
217
+ RefPtr<Element> newContent = CreateHTMLContent (aTag);
218
+ if (NS_WARN_IF(!newContent)) {
219
+ return nullptr ;
220
+ }
196
221
197
222
// add the "hidden" class if needed
198
223
if (aIsCreatedHidden) {
199
- nsresult rv = newElement->SetAttribute (NS_LITERAL_STRING(" class" ),
200
- NS_LITERAL_STRING (" hidden" ));
201
- NS_ENSURE_SUCCESS (rv, rv);
224
+ nsresult rv =
225
+ newContent->SetAttr (kNameSpaceID_None , nsGkAtoms::_class,
226
+ NS_LITERAL_STRING (" hidden" ), true);
227
+ if (NS_WARN_IF(NS_FAILED(rv))) {
228
+ return nullptr ;
229
+ }
202
230
}
203
231
204
232
// add an _moz_anonclass attribute if needed
205
233
if (!aAnonClass.IsEmpty ()) {
206
- nsresult rv = newElement->SetAttribute (NS_LITERAL_STRING(" _moz_anonclass" ),
207
- aAnonClass);
208
- NS_ENSURE_SUCCESS (rv, rv);
234
+ nsresult rv =
235
+ newContent->SetAttr (kNameSpaceID_None , nsGkAtoms::_moz_anonclass,
236
+ aAnonClass, true );
237
+ if (NS_WARN_IF(NS_FAILED(rv))) {
238
+ return nullptr ;
239
+ }
209
240
}
210
241
211
242
{
@@ -217,7 +248,7 @@ HTMLEditor::CreateAnonymousElement(const nsAString& aTag,
217
248
newContent->BindToTree (doc, parentContent, parentContent, true );
218
249
if (NS_FAILED(rv)) {
219
250
newContent->UnbindFromTree ();
220
- return rv ;
251
+ return nullptr ;
221
252
}
222
253
}
223
254
@@ -239,8 +270,7 @@ HTMLEditor::CreateAnonymousElement(const nsAString& aTag,
239
270
// display the element
240
271
ps->RecreateFramesFor (newContent);
241
272
242
- newElement.forget (aReturn);
243
- return NS_OK;
273
+ return newContent.forget ();
244
274
}
245
275
246
276
// Removes event listener and calls DeleteRefToAnonymousNode.
0 commit comments