From 65ca41c6c4820c9c7ec555698b9be59d1e9a0cd2 Mon Sep 17 00:00:00 2001 From: "peterv%netscape.com" Date: Tue, 2 Oct 2001 10:02:07 +0000 Subject: [PATCH] Fix for bug 97687 ([serializer]cs/gecko: copying using keyboard shortcuts>paste>source pasted {Country=US}) r=heikki, sr=jst. --- content/base/public/nsIDocumentEncoder.h | 6 +++- content/base/src/nsDocumentEncoder.cpp | 33 ++++++++++++++++++++++ content/base/src/nsPlainTextSerializer.cpp | 21 ++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/content/base/public/nsIDocumentEncoder.h b/content/base/public/nsIDocumentEncoder.h index 7aea67dc9bc..503601aab9e 100644 --- a/content/base/public/nsIDocumentEncoder.h +++ b/content/base/public/nsIDocumentEncoder.h @@ -148,7 +148,11 @@ public: // CR, LF, or CRLF. If neither of these flags is set, then we // will use platform line breaks. OutputCRLineBreak = 512, - OutputLFLineBreak = 1024 + OutputLFLineBreak = 1024, + + // Output the content of noscript elements (only for serializing + // to plaintext). + OutputNoScriptContent = 2048 }; /** diff --git a/content/base/src/nsDocumentEncoder.cpp b/content/base/src/nsDocumentEncoder.cpp index b2d572cb64b..13ed185fbd9 100644 --- a/content/base/src/nsDocumentEncoder.cpp +++ b/content/base/src/nsDocumentEncoder.cpp @@ -71,6 +71,9 @@ #include "nsISupportsArray.h" #include "nsIParserService.h" #include "nsParserCIID.h" +#include "nsIScriptContext.h" +#include "nsIScriptGlobalObject.h" +#include "nsIScriptSecurityManager.h" static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); @@ -195,6 +198,33 @@ nsDocumentEncoder::~nsDocumentEncoder() { } +static PRBool +IsScriptEnabled(nsIDocument *aDoc) +{ + NS_ENSURE_TRUE(aDoc, PR_TRUE); + + nsCOMPtr sm(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID)); + NS_ENSURE_TRUE(sm, PR_TRUE); + + nsCOMPtr principal; + aDoc->GetPrincipal(getter_AddRefs(principal)); + NS_ENSURE_TRUE(principal, PR_TRUE); + + nsCOMPtr globalObject; + aDoc->GetScriptGlobalObject(getter_AddRefs(globalObject)); + + nsCOMPtr scriptContext; + globalObject->GetContext(getter_AddRefs(scriptContext)); + NS_ENSURE_TRUE(scriptContext, PR_TRUE); + + JSContext* cx = (JSContext *) scriptContext->GetNativeContext(); + NS_ENSURE_TRUE(cx, PR_TRUE); + + PRBool enabled = PR_TRUE; + sm->CanExecuteScripts(cx, principal, &enabled); + return enabled; +} + NS_IMETHODIMP nsDocumentEncoder::Init(nsIDocument* aDocument, const nsAReadableString& aMimeType, @@ -1049,6 +1079,9 @@ nsHTMLCopyEncoder::Init(nsIDocument* aDocument, // (see related bugs #57296, #41924, #58646, #32768) mFlags = aFlags | OutputAbsoluteLinks; + if (!IsScriptEnabled(mDocument)) + mFlags |= OutputNoScriptContent; + nsresult rv; mParserService = do_GetService(kParserServiceCID, &rv); return rv; diff --git a/content/base/src/nsPlainTextSerializer.cpp b/content/base/src/nsPlainTextSerializer.cpp index ee557ef870c..fd08238fe6f 100644 --- a/content/base/src/nsPlainTextSerializer.cpp +++ b/content/base/src/nsPlainTextSerializer.cpp @@ -226,6 +226,11 @@ nsPlainTextSerializer::AppendText(nsIDOMText* aText, PRInt32 aEndOffset, nsAWritableString& aStr) { + if ((mTagStack[mTagStackIndex-1] == eHTMLTag_noscript) && + !(mFlags & nsIDocumentEncoder::OutputNoScriptContent)) { + return NS_OK; + } + NS_ENSURE_ARG(aText); nsresult rv = NS_OK; @@ -388,6 +393,11 @@ nsPlainTextSerializer::CloseContainer(const nsIParserNode& aNode) NS_IMETHODIMP nsPlainTextSerializer::AddLeaf(const nsIParserNode& aNode) { + if ((mTagStack[mTagStackIndex-1] == eHTMLTag_noscript) && + !(mFlags & nsIDocumentEncoder::OutputNoScriptContent)) { + return NS_OK; + } + eHTMLTags type = (eHTMLTags)aNode.GetNodeType(); const nsAReadableString& text = aNode.GetText(); @@ -494,6 +504,11 @@ nsPlainTextSerializer::DoOpenContainer(PRInt32 aTag) { eHTMLTags type = (eHTMLTags)aTag; + if ((mTagStack[mTagStackIndex-1] == eHTMLTag_noscript) && + !(mFlags & nsIDocumentEncoder::OutputNoScriptContent)) { + return NS_OK; + } + if (mTagStackIndex < TagStackSize) { mTagStack[mTagStackIndex++] = type; } @@ -722,6 +737,12 @@ nsPlainTextSerializer::DoCloseContainer(PRInt32 aTag) { eHTMLTags type = (eHTMLTags)aTag; + if ((mTagStack[mTagStackIndex-1] == eHTMLTag_noscript) && + !(mFlags & nsIDocumentEncoder::OutputNoScriptContent) && + (type != eHTMLTag_noscript)) { + return NS_OK; + } + if (mTagStackIndex > 0) { --mTagStackIndex; }