Skip to content

Commit

Permalink
Bug 479093 - Text sent to services includes body of <script> tags. r=…
Browse files Browse the repository at this point in the history
…smaug, r=josh, sr=roc
  • Loading branch information
Tom Dyas committed Mar 18, 2009
1 parent 8bb593e commit 03b91d6
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 65 deletions.
6 changes: 6 additions & 0 deletions content/base/public/nsCopySupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class nsCopySupport
// before[copy,cut,paste] and [copy,cut,paste] events will fire on.
static nsresult GetClipboardEventTarget(nsISelection *aSel,
nsIDOMNode **aEventTarget);

// Get the selection as a transferable. Similar to HTMLCopy except does
// not deal with the clipboard.
static nsresult GetTransferableForSelection(nsISelection * aSelection,
nsIDocument * aDocument,
nsITransferable ** aTransferable);
};

#endif
96 changes: 61 additions & 35 deletions content/base/src/nsCopySupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,23 @@ static nsresult AppendString(nsITransferable *aTransferable,
static nsresult AppendDOMNode(nsITransferable *aTransferable,
nsIDOMNode *aDOMNode);

nsresult nsCopySupport::HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16 aClipboardID)
// Helper used for HTMLCopy and GetTransferableForSelection since both routines
// share common code.
static nsresult
SelectionCopyHelper(nsISelection *aSel, nsIDocument *aDoc,
PRBool doPutOnClipboard, PRInt16 aClipboardID,
nsITransferable ** aTransferable)
{
// Clear the output parameter for the transferable, if provided.
if (aTransferable) {
*aTransferable = nsnull;
}

nsresult rv = NS_OK;

PRBool bIsPlainTextContext = PR_FALSE;

rv = IsPlainTextContext(aSel, aDoc, &bIsPlainTextContext);
rv = nsCopySupport::IsPlainTextContext(aSel, aDoc, &bIsPlainTextContext);
if (NS_FAILED(rv))
return rv;

Expand All @@ -114,6 +124,7 @@ nsresult nsCopySupport::HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16
rv = docEncoder->Init(domDoc, mimeType, flags);
if (NS_FAILED(rv))
return rv;

rv = docEncoder->SetSelection(aSel);
if (NS_FAILED(rv))
return rv;
Expand Down Expand Up @@ -160,42 +171,40 @@ nsresult nsCopySupport::HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16
}

// Get the Clipboard
nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID, &rv));
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIClipboard> clipboard;
if (doPutOnClipboard) {
clipboard = do_GetService(kCClipboardCID, &rv);
if (NS_FAILED(rv))
return rv;
}

if ( clipboard )
{
if ((doPutOnClipboard && clipboard) || aTransferable != nsnull) {
// Create a transferable for putting data on the Clipboard
nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID);
if ( trans )
{
if (bIsHTMLCopy)
{
if (trans) {
if (bIsHTMLCopy) {
// set up the data converter
trans->SetConverter(htmlConverter);

if (!buffer.IsEmpty())
{
if (!buffer.IsEmpty()) {
// Add the html DataFlavor to the transferable
rv = AppendString(trans, buffer, kHTMLMime);
NS_ENSURE_SUCCESS(rv, rv);
}
{
// Add the htmlcontext DataFlavor to the transferable
// Even if parents is empty string, this flavor should
// be attached to the transferable
rv = AppendString(trans, parents, kHTMLContext);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!info.IsEmpty())
{

// Add the htmlcontext DataFlavor to the transferable
// Even if parents is empty string, this flavor should
// be attached to the transferable
rv = AppendString(trans, parents, kHTMLContext);
NS_ENSURE_SUCCESS(rv, rv);

if (!info.IsEmpty()) {
// Add the htmlinfo DataFlavor to the transferable
rv = AppendString(trans, info, kHTMLInfo);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!plaintextBuffer.IsEmpty())
{

if (!plaintextBuffer.IsEmpty()) {
// unicode text
// Add the unicode DataFlavor to the transferable
// If we didn't have this, then nsDataObj::GetData matches text/unicode against
Expand Down Expand Up @@ -223,28 +232,45 @@ nsresult nsCopySupport::HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16
NS_ENSURE_SUCCESS(rv, rv);
}
}
}
else
{
if (!textBuffer.IsEmpty())
{
// Add the unicode DataFlavor to the transferable
} else {
if (!textBuffer.IsEmpty()) {
// Add the unicode DataFlavor to the transferable
rv = AppendString(trans, textBuffer, kUnicodeMime);
NS_ENSURE_SUCCESS(rv, rv);
}
}

PRBool doPutOnClipboard = PR_TRUE;
DoHooks(aDoc, trans, &doPutOnClipboard);
if (doPutOnClipboard && clipboard) {
PRBool actuallyPutOnClipboard = PR_TRUE;
nsCopySupport::DoHooks(aDoc, trans, &actuallyPutOnClipboard);

// put the transferable on the clipboard
if (doPutOnClipboard)
clipboard->SetData(trans, nsnull, aClipboardID);
// put the transferable on the clipboard
if (actuallyPutOnClipboard)
clipboard->SetData(trans, nsnull, aClipboardID);
}

// Return the transferable to the caller if requested.
if (aTransferable != nsnull) {
trans.swap(*aTransferable);
}
}
}
return rv;
}

nsresult nsCopySupport::HTMLCopy(nsISelection *aSel, nsIDocument *aDoc, PRInt16 aClipboardID)
{
return SelectionCopyHelper(aSel, aDoc, PR_TRUE, aClipboardID, nsnull);
}

nsresult
nsCopySupport::GetTransferableForSelection(nsISelection * aSel,
nsIDocument * aDoc,
nsITransferable ** aTransferable)
{
return SelectionCopyHelper(aSel, aDoc, PR_FALSE, 0, aTransferable);
}

nsresult nsCopySupport::DoHooks(nsIDocument *aDoc, nsITransferable *aTrans,
PRBool *aDoPutOnClipboard)
{
Expand Down
43 changes: 42 additions & 1 deletion content/events/src/nsContentEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "nsRange.h"
#include "nsGUIEvent.h"
#include "nsCaret.h"
#include "nsCopySupport.h"
#include "nsFrameSelection.h"
#include "nsIFrame.h"
#include "nsIView.h"
Expand Down Expand Up @@ -92,6 +93,12 @@ nsContentEventHandler::Init(nsQueryContentEvent* aEvent)
NS_ASSERTION(mSelection,
"GetSelectionForCopy succeeded, but the result is null");

PRBool isCollapsed;
rv = mSelection->GetIsCollapsed(&isCollapsed);
if (NS_FAILED(rv))
return NS_ERROR_NOT_AVAILABLE;
aEvent->mReply.mHasSelection = !isCollapsed;

nsCOMPtr<nsIDOMRange> firstRange;
rv = mSelection->GetRangeAt(0, getter_AddRefs(firstRange));
// This shell doesn't support selection.
Expand All @@ -111,7 +118,6 @@ nsContentEventHandler::Init(nsQueryContentEvent* aEvent)
rv = mPresShell->GetCaret(getter_AddRefs(caret));
NS_ENSURE_SUCCESS(rv, rv);
NS_ASSERTION(caret, "GetCaret succeeded, but the result is null");
PRBool isCollapsed;
nsRect r;
nsIView* view = nsnull;
rv = caret->GetCaretCoordinates(nsCaret::eRenderingViewCoordinates,
Expand Down Expand Up @@ -684,6 +690,41 @@ nsContentEventHandler::OnQueryCaretRect(nsQueryContentEvent* aEvent)
return NS_OK;
}

nsresult
nsContentEventHandler::OnQueryContentState(nsQueryContentEvent * aEvent)
{
nsresult rv = Init(aEvent);
if (NS_FAILED(rv))
return rv;

aEvent->mSucceeded = PR_TRUE;

return NS_OK;
}

nsresult
nsContentEventHandler::OnQuerySelectionAsTransferable(nsQueryContentEvent* aEvent)
{
nsresult rv = Init(aEvent);
if (NS_FAILED(rv))
return rv;

if (!aEvent->mReply.mHasSelection) {
aEvent->mSucceeded = PR_TRUE;
aEvent->mReply.mTransferable = nsnull;
return NS_OK;
}

nsCOMPtr<nsIDocument> doc = mPresShell->GetDocument();
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);

rv = nsCopySupport::GetTransferableForSelection(mSelection, doc, getter_AddRefs(aEvent->mReply.mTransferable));
NS_ENSURE_SUCCESS(rv, rv);

aEvent->mSucceeded = PR_TRUE;
return NS_OK;
}

nsresult
nsContentEventHandler::GetFlatTextOffsetOfRange(nsIContent* aRootContent,
nsINode* aNode,
Expand Down
4 changes: 4 additions & 0 deletions content/events/src/nsContentEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class NS_STACK_CLASS nsContentEventHandler {
nsresult OnQueryTextRect(nsQueryContentEvent* aEvent);
// NS_QUERY_EDITOR_RECT event handler
nsresult OnQueryEditorRect(nsQueryContentEvent* aEvent);
// NS_QUERY_CONTENT_STATE event handler
nsresult OnQueryContentState(nsQueryContentEvent* aEvent);
// NS_QUERY_SELECTION_AS_TRANSFERABLE event handler
nsresult OnQuerySelectionAsTransferable(nsQueryContentEvent* aEvent);

// NS_SELECTION_* event
nsresult OnSelectionEvent(nsSelectionEvent* aEvent);
Expand Down
12 changes: 12 additions & 0 deletions content/events/src/nsEventStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,18 @@ nsEventStateManager::PreHandleEvent(nsPresContext* aPresContext,
handler.OnQueryEditorRect((nsQueryContentEvent*)aEvent);
}
break;
case NS_QUERY_CONTENT_STATE:
{
nsContentEventHandler handler(mPresContext);
handler.OnQueryContentState(static_cast<nsQueryContentEvent*>(aEvent));
}
break;
case NS_QUERY_SELECTION_AS_TRANSFERABLE:
{
nsContentEventHandler handler(mPresContext);
handler.OnQuerySelectionAsTransferable(static_cast<nsQueryContentEvent*>(aEvent));
}
break;
case NS_SELECTION_SET:
{
nsContentEventHandler handler(mPresContext);
Expand Down
9 changes: 9 additions & 0 deletions widget/public/nsGUIEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "nsIWidget.h"
#include "nsTArray.h"
#include "nsTraceRefcnt.h"
#include "nsITransferable.h"

class nsIRenderingContext;
class nsIRegion;
Expand Down Expand Up @@ -362,6 +363,11 @@ class nsHashKey;
// Query for the bounding rect of the current focused frame. Result is relative
// to top level widget coordinates
#define NS_QUERY_EDITOR_RECT (NS_QUERY_CONTENT_EVENT_START + 5)
// Query for the current state of the content. The particular members of
// mReply that are set for each query content event will be valid on success.
#define NS_QUERY_CONTENT_STATE (NS_QUERY_CONTENT_EVENT_START + 6)
// Query for the selection in the form of a nsITransferable.
#define NS_QUERY_SELECTION_AS_TRANSFERABLE (NS_QUERY_CONTENT_EVENT_START + 7)

// Video events
#ifdef MOZ_MEDIA
Expand Down Expand Up @@ -1012,6 +1018,9 @@ class nsQueryContentEvent : public nsGUIEvent
// The return widget has the caret. This is set at all query events.
nsIWidget* mFocusedWidget;
PRPackedBool mReversed; // true if selection is reversed (end < start)
PRPackedBool mHasSelection; // true if the selection exists
// used by NS_QUERY_SELECTION_AS_TRANSFERABLE
nsCOMPtr<nsITransferable> mTransferable;
} mReply;
};

Expand Down
Loading

0 comments on commit 03b91d6

Please sign in to comment.