From 64bcbdf66ea4b9be81802da5a50ec2680c52dfd0 Mon Sep 17 00:00:00 2001 From: "akkana@netscape.com" Date: Fri, 30 Apr 1999 18:03:39 +0000 Subject: [PATCH] 5734: put back working copy from version 1.17, which got accidentally stomped in a cvs merge. a=cyeh,kostello --- editor/base/nsHTMLEditor.cpp | 74 ++++++++++++++++---------- editor/libeditor/html/nsHTMLEditor.cpp | 74 ++++++++++++++++---------- 2 files changed, 94 insertions(+), 54 deletions(-) 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!