Skip to content

Commit ba15108

Browse files
committed
Bug 1313986 - Part 2. Add CreateAnonymousElement with nsIAtom. r=masayuki
I would like to nsIAtom and mozilla::dom::Element version of CreateAnonymousElement to clean up code. When getting/setting attirubte, editor sometimes use string, not nsGkAtoms. We should use new mozilla::dom::Element methods. Also, we should add _moz_anonclass to atom list that uses on editor. MozReview-Commit-ID: ICaAWVPjcej --HG-- extra : rebase_source : 9585214aa678c16905250265a75b817c90246fcc
1 parent 18132c8 commit ba15108

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

dom/base/nsGkAtomList.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ GK_ATOM(moz, "_moz")
3636
GK_ATOM(mozframetype, "mozframetype")
3737
GK_ATOM(_moz_abspos, "_moz_abspos")
3838
GK_ATOM(_moz_activated, "_moz_activated")
39+
GK_ATOM(_moz_anonclass, "_moz_anonclass")
3940
GK_ATOM(_moz_resizing, "_moz_resizing")
4041
GK_ATOM(mozallowfullscreen, "mozallowfullscreen")
4142
GK_ATOM(moztype, "_moz-type")

editor/libeditor/HTMLAnonymousNodeEditor.cpp

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,47 +165,78 @@ ElementDeletionObserver::NodeWillBeDestroyed(const nsINode* aNode)
165165
// child of aParentNode. If aIsCreatedHidden is true, the class
166166
// "hidden" is added to the created element. If aAnonClass is not
167167
// the empty string, it becomes the value of the attribute "_moz_anonclass"
168-
nsresult
168+
NS_IMETHODIMP
169169
HTMLEditor::CreateAnonymousElement(const nsAString& aTag,
170170
nsIDOMNode* aParentNode,
171171
const nsAString& aAnonClass,
172172
bool aIsCreatedHidden,
173173
nsIDOMElement** aReturn)
174174
{
175-
NS_ENSURE_ARG_POINTER(aParentNode);
176175
NS_ENSURE_ARG_POINTER(aReturn);
177176
*aReturn = nullptr;
178177

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+
}
181204

182205
nsCOMPtr<nsIDocument> doc = GetDocument();
183-
NS_ENSURE_TRUE(doc, NS_ERROR_NULL_POINTER);
206+
if (NS_WARN_IF(!doc)) {
207+
return nullptr;
208+
}
184209

185210
// Get the pres shell
186211
nsCOMPtr<nsIPresShell> ps = GetPresShell();
187-
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
212+
if (NS_WARN_IF(!ps)) {
213+
return nullptr;
214+
}
188215

189216
// 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+
}
196221

197222
// add the "hidden" class if needed
198223
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+
}
202230
}
203231

204232
// add an _moz_anonclass attribute if needed
205233
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+
}
209240
}
210241

211242
{
@@ -217,7 +248,7 @@ HTMLEditor::CreateAnonymousElement(const nsAString& aTag,
217248
newContent->BindToTree(doc, parentContent, parentContent, true);
218249
if (NS_FAILED(rv)) {
219250
newContent->UnbindFromTree();
220-
return rv;
251+
return nullptr;
221252
}
222253
}
223254

@@ -239,8 +270,7 @@ HTMLEditor::CreateAnonymousElement(const nsAString& aTag,
239270
// display the element
240271
ps->RecreateFramesFor(newContent);
241272

242-
newElement.forget(aReturn);
243-
return NS_OK;
273+
return newContent.forget();
244274
}
245275

246276
// Removes event listener and calls DeleteRefToAnonymousNode.

editor/libeditor/HTMLEditor.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,25 @@ class HTMLEditor final : public TextEditor
10981098
const nsAString& aTagName, nsINode* aNode);
10991099
already_AddRefed<Element> CreateElementWithDefaults(
11001100
const nsAString& aTagName);
1101+
/**
1102+
* Returns an anonymous Element of type aTag,
1103+
* child of aParentNode. If aIsCreatedHidden is true, the class
1104+
* "hidden" is added to the created element. If aAnonClass is not
1105+
* the empty string, it becomes the value of the attribute "_moz_anonclass"
1106+
* @return a Element
1107+
* @param aTag [IN] desired type of the element to create
1108+
* @param aParentNode [IN] the parent node of the created anonymous
1109+
* element
1110+
* @param aAnonClass [IN] contents of the _moz_anonclass attribute
1111+
* @param aIsCreatedHidden [IN] a boolean specifying if the class "hidden"
1112+
* is to be added to the created anonymous
1113+
* element
1114+
*/
1115+
already_AddRefed<Element> CreateAnonymousElement(
1116+
nsIAtom* aTag,
1117+
nsIDOMNode* aParentNode,
1118+
const nsAString& aAnonClass,
1119+
bool aIsCreatedHidden);
11011120
};
11021121

11031122
} // namespace mozilla

0 commit comments

Comments
 (0)