From 45318ab7289df3e73a46b62ce93adc5f505d2f3f Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Sat, 6 Nov 2010 18:49:26 -0400 Subject: [PATCH] Bug 551704 - Part 3: Don't copy invisible BR nodes to the clipboard for editable elements, to make us more compatible with WebKit and Opera; r=bzbarsky a=blocking-betaN+ --- content/base/src/nsHTMLContentSerializer.cpp | 44 +++++++++++ editor/idl/nsIHTMLEditor.idl | 7 +- editor/libeditor/html/nsHTMLEditor.cpp | 10 +++ .../libeditor/html/tests/test_bug551704.html | 77 ++++++++++++++++++- 4 files changed, 135 insertions(+), 3 deletions(-) diff --git a/content/base/src/nsHTMLContentSerializer.cpp b/content/base/src/nsHTMLContentSerializer.cpp index 23a8b68ce346..550ddb9e31cb 100644 --- a/content/base/src/nsHTMLContentSerializer.cpp +++ b/content/base/src/nsHTMLContentSerializer.cpp @@ -68,6 +68,10 @@ #include "nsLWBrkCIID.h" #include "nsIScriptElement.h" #include "nsAttrName.h" +#include "nsIDocShell.h" +#include "nsIEditorDocShell.h" +#include "nsIEditor.h" +#include "nsIHTMLEditor.h" static const PRInt32 kLongLineLen = 128; @@ -81,6 +85,40 @@ nsresult NS_NewHTMLContentSerializer(nsIContentSerializer** aSerializer) return CallQueryInterface(it, aSerializer); } +static +PRBool +IsInvisibleBreak(nsIContent *aNode, nsIAtom *aTag) { + // xxxehsan: we should probably figure out a way to determine + // if a BR node is visible without using the editor. + if (aTag != nsGkAtoms::br || !aNode->IsEditable()) { + return PR_FALSE; + } + + // Grab the editor associated with the document + nsIDocument *doc = aNode->GetCurrentDoc(); + if (doc) { + nsPIDOMWindow *window = doc->GetWindow(); + if (window) { + nsIDocShell *docShell = window->GetDocShell(); + if (docShell) { + nsCOMPtr editorDocShell = do_QueryInterface(docShell); + if (editorDocShell) { + nsCOMPtr editor; + editorDocShell->GetEditor(getter_AddRefs(editor)); + nsCOMPtr htmlEditor = do_QueryInterface(editor); + if (htmlEditor) { + PRBool isVisible = PR_FALSE; + nsCOMPtr domNode = do_QueryInterface(aNode); + htmlEditor->BreakIsVisible(domNode, &isVisible); + return !isVisible; + } + } + } + } + } + return PR_FALSE; +} + nsHTMLContentSerializer::nsHTMLContentSerializer() { mIsHTMLSerializer = PR_TRUE; @@ -204,6 +242,12 @@ nsHTMLContentSerializer::AppendElementStart(nsIContent *aElement, } nsIAtom *name = content->Tag(); + + if ((mFlags & nsIDocumentEncoder::OutputPreformatted) && + IsInvisibleBreak(content, name)) { + return NS_OK; + } + PRBool lineBreakBeforeOpen = LineBreakBeforeOpen(content->GetNameSpaceID(), name); if ((mDoFormat || forceFormat) && !mPreLevel && !mDoRaw) { diff --git a/editor/idl/nsIHTMLEditor.idl b/editor/idl/nsIHTMLEditor.idl index 6550065e6379..a9ba99250b2f 100644 --- a/editor/idl/nsIHTMLEditor.idl +++ b/editor/idl/nsIHTMLEditor.idl @@ -51,7 +51,7 @@ interface nsIContentFilter; NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_EDITOR, 1) %} -[scriptable, uuid(afc36593-5787-4420-93d9-b2c0ccbf0cad)] +[scriptable, uuid(c964b8b0-e9e8-11df-9492-0800200c9a66)] interface nsIHTMLEditor : nsISupports { @@ -608,5 +608,10 @@ interface nsIHTMLEditor : nsISupports * @return true if CR in a paragraph creates a new paragraph */ attribute boolean returnInParagraphCreatesNewParagraph; + + /** + * Checks whether a BR node is visible to the user. + */ + boolean breakIsVisible(in nsIDOMNode aNode); }; diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp index 5dbb92fd17a8..5617dbcb7335 100644 --- a/editor/libeditor/html/nsHTMLEditor.cpp +++ b/editor/libeditor/html/nsHTMLEditor.cpp @@ -1342,6 +1342,16 @@ PRBool nsHTMLEditor::IsVisBreak(nsIDOMNode *aNode) return PR_TRUE; } +NS_IMETHODIMP +nsHTMLEditor::BreakIsVisible(nsIDOMNode *aNode, PRBool *aIsVisible) +{ + NS_ENSURE_ARG_POINTER(aNode && aIsVisible); + + *aIsVisible = IsVisBreak(aNode); + + return NS_OK; +} + NS_IMETHODIMP nsHTMLEditor::GetIsDocumentEditable(PRBool *aIsDocumentEditable) diff --git a/editor/libeditor/html/tests/test_bug551704.html b/editor/libeditor/html/tests/test_bug551704.html index 5d106f94054d..937fd27230a7 100644 --- a/editor/libeditor/html/tests/test_bug551704.html +++ b/editor/libeditor/html/tests/test_bug551704.html @@ -15,17 +15,90 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=551704

a b
+

+
a
+

+
a
+
+
a
+
+
a