diff --git a/editor/base/nsHTMLEditor.cpp b/editor/base/nsHTMLEditor.cpp
index 5b4e81dfb4db..b73d4c4af655 100644
--- a/editor/base/nsHTMLEditor.cpp
+++ b/editor/base/nsHTMLEditor.cpp
@@ -948,39 +948,59 @@ nsHTMLEditor::InsertImage(nsString& aURL,
nsString& aAlignment)
{
nsresult res;
-
- (void)nsEditor::BeginTransaction();
-
nsCOMPtr newNode;
- nsAutoString tag("IMG");
- res = nsEditor::DeleteSelectionAndCreateNode(tag, getter_AddRefs(newNode));
- if (!NS_SUCCEEDED(res) || !newNode)
+
+ nsCOMPtrdoc;
+ res = GetDocument(getter_AddRefs(doc));
+ if (NS_SUCCEEDED(res))
{
- (void)nsEditor::EndTransaction();
+ nsAutoString tag("IMG");
+ nsCOMPtrnewElement;
+ res = doc->CreateElement(tag, getter_AddRefs(newElement));
+ if (NS_SUCCEEDED(res) && newElement)
+ {
+ newNode = do_QueryInterface(newElement);
+ nsCOMPtr image (do_QueryInterface(newNode));
+ // Set all the attributes now, before we insert into the tree:
+ if (image)
+ {
+ if (NS_SUCCEEDED(res = image->SetSrc(aURL)))
+ if (NS_SUCCEEDED(res = image->SetWidth(aWidth)))
+ if (NS_SUCCEEDED(res = image->SetHeight(aHeight)))
+ if (NS_SUCCEEDED(res = image->SetAlt(aAlt)))
+ if (NS_SUCCEEDED(res = image->SetBorder(aBorder)))
+ if (NS_SUCCEEDED(res = image->SetAlign(aAlignment)))
+ if (NS_SUCCEEDED(res = image->SetHspace(aHspace)))
+ if (NS_SUCCEEDED(res = image->SetVspace(aVspace)))
+ ;
+ }
+ }
+ }
+
+ // If any of these failed, then don't insert the new node into the tree
+ if (!NS_SUCCEEDED(res))
+ {
+#ifdef DEBUG_akkana
+ printf("Some failure creating the new image node\n");
+#endif
return res;
}
- nsCOMPtr image (do_QueryInterface(newNode));
- if (!image)
- {
-#ifdef DEBUG_akkana
- printf("Not an image element\n");
-#endif
- (void)nsEditor::EndTransaction();
- return NS_NOINTERFACE;
- }
+ //
+ // Now we're ready to insert the new image node:
+ // Starting now, don't return without ending the transaction!
+ //
+ (void)nsEditor::BeginTransaction();
- // Can't return from any of these intermediates
- // because then we won't hit the EndTransaction()
- if (NS_SUCCEEDED(res = image->SetSrc(aURL)))
- if (NS_SUCCEEDED(res = image->SetWidth(aWidth)))
- if (NS_SUCCEEDED(res = image->SetHeight(aHeight)))
- if (NS_SUCCEEDED(res = image->SetAlt(aAlt)))
- if (NS_SUCCEEDED(res = image->SetBorder(aBorder)))
- if (NS_SUCCEEDED(res = image->SetAlign(aAlignment)))
- if (NS_SUCCEEDED(res = image->SetHspace(aHspace)))
- if (NS_SUCCEEDED(res = image->SetVspace(aVspace)))
- ;
+ nsCOMPtr parentNode;
+ PRInt32 offsetOfNewNode;
+ res = nsEditor::DeleteSelectionAndPrepareToCreateNode(parentNode,
+ offsetOfNewNode);
+ if (NS_SUCCEEDED(res))
+ {
+ // and insert it into the right place in the tree:
+ res = InsertNode(newNode, parentNode, offsetOfNewNode);
+ }
(void)nsEditor::EndTransaction(); // don't return this result!
diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp
index 5b4e81dfb4db..b73d4c4af655 100644
--- a/editor/libeditor/html/nsHTMLEditor.cpp
+++ b/editor/libeditor/html/nsHTMLEditor.cpp
@@ -948,39 +948,59 @@ nsHTMLEditor::InsertImage(nsString& aURL,
nsString& aAlignment)
{
nsresult res;
-
- (void)nsEditor::BeginTransaction();
-
nsCOMPtr newNode;
- nsAutoString tag("IMG");
- res = nsEditor::DeleteSelectionAndCreateNode(tag, getter_AddRefs(newNode));
- if (!NS_SUCCEEDED(res) || !newNode)
+
+ nsCOMPtrdoc;
+ res = GetDocument(getter_AddRefs(doc));
+ if (NS_SUCCEEDED(res))
{
- (void)nsEditor::EndTransaction();
+ nsAutoString tag("IMG");
+ nsCOMPtrnewElement;
+ res = doc->CreateElement(tag, getter_AddRefs(newElement));
+ if (NS_SUCCEEDED(res) && newElement)
+ {
+ newNode = do_QueryInterface(newElement);
+ nsCOMPtr image (do_QueryInterface(newNode));
+ // Set all the attributes now, before we insert into the tree:
+ if (image)
+ {
+ if (NS_SUCCEEDED(res = image->SetSrc(aURL)))
+ if (NS_SUCCEEDED(res = image->SetWidth(aWidth)))
+ if (NS_SUCCEEDED(res = image->SetHeight(aHeight)))
+ if (NS_SUCCEEDED(res = image->SetAlt(aAlt)))
+ if (NS_SUCCEEDED(res = image->SetBorder(aBorder)))
+ if (NS_SUCCEEDED(res = image->SetAlign(aAlignment)))
+ if (NS_SUCCEEDED(res = image->SetHspace(aHspace)))
+ if (NS_SUCCEEDED(res = image->SetVspace(aVspace)))
+ ;
+ }
+ }
+ }
+
+ // If any of these failed, then don't insert the new node into the tree
+ if (!NS_SUCCEEDED(res))
+ {
+#ifdef DEBUG_akkana
+ printf("Some failure creating the new image node\n");
+#endif
return res;
}
- nsCOMPtr image (do_QueryInterface(newNode));
- if (!image)
- {
-#ifdef DEBUG_akkana
- printf("Not an image element\n");
-#endif
- (void)nsEditor::EndTransaction();
- return NS_NOINTERFACE;
- }
+ //
+ // Now we're ready to insert the new image node:
+ // Starting now, don't return without ending the transaction!
+ //
+ (void)nsEditor::BeginTransaction();
- // Can't return from any of these intermediates
- // because then we won't hit the EndTransaction()
- if (NS_SUCCEEDED(res = image->SetSrc(aURL)))
- if (NS_SUCCEEDED(res = image->SetWidth(aWidth)))
- if (NS_SUCCEEDED(res = image->SetHeight(aHeight)))
- if (NS_SUCCEEDED(res = image->SetAlt(aAlt)))
- if (NS_SUCCEEDED(res = image->SetBorder(aBorder)))
- if (NS_SUCCEEDED(res = image->SetAlign(aAlignment)))
- if (NS_SUCCEEDED(res = image->SetHspace(aHspace)))
- if (NS_SUCCEEDED(res = image->SetVspace(aVspace)))
- ;
+ nsCOMPtr parentNode;
+ PRInt32 offsetOfNewNode;
+ res = nsEditor::DeleteSelectionAndPrepareToCreateNode(parentNode,
+ offsetOfNewNode);
+ if (NS_SUCCEEDED(res))
+ {
+ // and insert it into the right place in the tree:
+ res = InsertNode(newNode, parentNode, offsetOfNewNode);
+ }
(void)nsEditor::EndTransaction(); // don't return this result!