Bug 1056545 part.3 ns*EditorEventListener should conform to current coding rules r=ehsan

This commit is contained in:
Masayuki Nakano 2014-08-26 09:57:46 +09:00
parent d0e3b33749
commit f53995a29b
2 changed files with 97 additions and 95 deletions

View File

@ -91,9 +91,10 @@ DoCommandCallback(Command aCommand, void* aData)
} }
} }
nsEditorEventListener::nsEditorEventListener() : nsEditorEventListener::nsEditorEventListener()
mEditor(nullptr), mCommitText(false), : mEditor(nullptr)
mInTransaction(false) , mCommitText(false)
, mInTransaction(false)
#ifdef HANDLE_NATIVE_TEXT_DIRECTION_SWITCH #ifdef HANDLE_NATIVE_TEXT_DIRECTION_SWITCH
, mHaveBidiKeyboards(false) , mHaveBidiKeyboards(false)
, mShouldSwitchTextDirection(false) , mShouldSwitchTextDirection(false)
@ -447,8 +448,9 @@ bool IsCtrlShiftPressed(bool& isRTL)
// To ignore the keys checked in 1, we set their status to 0 before // To ignore the keys checked in 1, we set their status to 0 before
// checking the key status. // checking the key status.
const int kKeyDownMask = 0x80; const int kKeyDownMask = 0x80;
if ((keystate[VK_CONTROL] & kKeyDownMask) == 0) if ((keystate[VK_CONTROL] & kKeyDownMask) == 0) {
return false; return false;
}
if (keystate[VK_RSHIFT] & kKeyDownMask) { if (keystate[VK_RSHIFT] & kKeyDownMask) {
keystate[VK_RSHIFT] = 0; keystate[VK_RSHIFT] = 0;
@ -471,8 +473,9 @@ bool IsCtrlShiftPressed(bool& isRTL)
keystate[VK_RCONTROL] = 0; keystate[VK_RCONTROL] = 0;
keystate[VK_LCONTROL] = 0; keystate[VK_LCONTROL] = 0;
for (int i = 0; i <= VK_PACKET; ++i) { for (int i = 0; i <= VK_PACKET; ++i) {
if (keystate[i] & kKeyDownMask) if (keystate[i] & kKeyDownMask) {
return false; return false;
}
} }
return true; return true;
} }
@ -624,10 +627,8 @@ nsEditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent)
int16_t button = -1; int16_t button = -1;
aMouseEvent->GetButton(&button); aMouseEvent->GetButton(&button);
// middle-mouse click (paste); // middle-mouse click (paste);
if (button == 1) if (button == 1) {
{ if (Preferences::GetBool("middlemouse.paste", false)) {
if (Preferences::GetBool("middlemouse.paste", false))
{
// Set the selection to the point under the mouse cursor: // Set the selection to the point under the mouse cursor:
nsCOMPtr<nsIDOMNode> parent; nsCOMPtr<nsIDOMNode> parent;
if (NS_FAILED(aMouseEvent->GetRangeParent(getter_AddRefs(parent)))) { if (NS_FAILED(aMouseEvent->GetRangeParent(getter_AddRefs(parent)))) {
@ -639,8 +640,9 @@ nsEditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent)
} }
nsCOMPtr<nsISelection> selection; nsCOMPtr<nsISelection> selection;
if (NS_SUCCEEDED(mEditor->GetSelection(getter_AddRefs(selection)))) if (NS_SUCCEEDED(mEditor->GetSelection(getter_AddRefs(selection)))) {
(void)selection->Collapse(parent, offset); selection->Collapse(parent, offset);
}
// If the ctrl key is pressed, we'll do paste as quotation. // If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially. // Would've used the alt key, but the kde wmgr treats alt-middle specially.
@ -648,8 +650,9 @@ nsEditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent)
aMouseEvent->GetCtrlKey(&ctrlKey); aMouseEvent->GetCtrlKey(&ctrlKey);
nsCOMPtr<nsIEditorMailSupport> mailEditor; nsCOMPtr<nsIEditorMailSupport> mailEditor;
if (ctrlKey) if (ctrlKey) {
mailEditor = do_QueryObject(mEditor); mailEditor = do_QueryObject(mEditor);
}
int32_t clipboard = nsIClipboard::kGlobalClipboard; int32_t clipboard = nsIClipboard::kGlobalClipboard;
nsCOMPtr<nsIClipboard> clipboardService = nsCOMPtr<nsIClipboard> clipboardService =
@ -662,10 +665,11 @@ nsEditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent)
} }
} }
if (mailEditor) if (mailEditor) {
mailEditor->PasteAsQuotation(clipboard); mailEditor->PasteAsQuotation(clipboard);
else } else {
mEditor->Paste(clipboard); mEditor->Paste(clipboard);
}
// Prevent the event from propagating up to be possibly handled // Prevent the event from propagating up to be possibly handled
// again by the containing window: // again by the containing window:
@ -753,17 +757,14 @@ nsEditorEventListener::DragOver(nsIDOMDragEvent* aDragEvent)
mCaret->SetVisible(true); mCaret->SetVisible(true);
mCaret->SetCaretPosition(parent, offset); mCaret->SetCaretPosition(parent, offset);
} }
} } else {
else
{
if (!IsFileControlTextBox()) { if (!IsFileControlTextBox()) {
// This is needed when dropping on an input, to prevent the editor for // This is needed when dropping on an input, to prevent the editor for
// the editable parent from receiving the event. // the editable parent from receiving the event.
aDragEvent->StopPropagation(); aDragEvent->StopPropagation();
} }
if (mCaret) if (mCaret) {
{
mCaret->SetVisible(false); mCaret->SetVisible(false);
} }
} }
@ -774,13 +775,11 @@ nsEditorEventListener::DragOver(nsIDOMDragEvent* aDragEvent)
void void
nsEditorEventListener::CleanupDragDropCaret() nsEditorEventListener::CleanupDragDropCaret()
{ {
if (mCaret) if (mCaret) {
{
mCaret->SetVisible(false); // hide it, so that it turns off its timer mCaret->SetVisible(false); // hide it, so that it turns off its timer
nsCOMPtr<nsIPresShell> presShell = GetPresShell(); nsCOMPtr<nsIPresShell> presShell = GetPresShell();
if (presShell) if (presShell) {
{
nsCOMPtr<nsISelectionController> selCon(do_QueryInterface(presShell)); nsCOMPtr<nsISelectionController> selCon(do_QueryInterface(presShell));
if (selCon) { if (selCon) {
selCon->SetCaretEnabled(false); selCon->SetCaretEnabled(false);
@ -869,8 +868,9 @@ nsEditorEventListener::CanDrop(nsIDOMDragEvent* aEvent)
// is the same as the drag source. // is the same as the drag source.
nsCOMPtr<nsIDOMNode> sourceNode; nsCOMPtr<nsIDOMNode> sourceNode;
dataTransfer->GetMozSourceNode(getter_AddRefs(sourceNode)); dataTransfer->GetMozSourceNode(getter_AddRefs(sourceNode));
if (!sourceNode) if (!sourceNode) {
return true; return true;
}
// There is a source node, so compare the source documents and this document. // There is a source node, so compare the source documents and this document.
// Disallow drops on the same document. // Disallow drops on the same document.
@ -881,18 +881,22 @@ nsEditorEventListener::CanDrop(nsIDOMDragEvent* aEvent)
nsCOMPtr<nsIDOMDocument> sourceDoc; nsCOMPtr<nsIDOMDocument> sourceDoc;
nsresult rv = sourceNode->GetOwnerDocument(getter_AddRefs(sourceDoc)); nsresult rv = sourceNode->GetOwnerDocument(getter_AddRefs(sourceDoc));
NS_ENSURE_SUCCESS(rv, false); NS_ENSURE_SUCCESS(rv, false);
if (domdoc == sourceDoc) // source and dest are the same document; disallow drops within the selection
{ // source and dest are the same document; disallow drops within the selection
if (domdoc == sourceDoc) {
nsCOMPtr<nsISelection> selection; nsCOMPtr<nsISelection> selection;
rv = mEditor->GetSelection(getter_AddRefs(selection)); rv = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(rv) || !selection) if (NS_FAILED(rv) || !selection) {
return false; return false;
}
// Don't bother if collapsed - can always drop // Don't bother if collapsed - can always drop
if (!selection->Collapsed()) { if (!selection->Collapsed()) {
nsCOMPtr<nsIDOMNode> parent; nsCOMPtr<nsIDOMNode> parent;
rv = aEvent->GetRangeParent(getter_AddRefs(parent)); rv = aEvent->GetRangeParent(getter_AddRefs(parent));
if (NS_FAILED(rv) || !parent) return false; if (NS_FAILED(rv) || !parent) {
return false;
}
int32_t offset = 0; int32_t offset = 0;
rv = aEvent->GetRangeOffset(&offset); rv = aEvent->GetRangeOffset(&offset);
@ -902,17 +906,20 @@ nsEditorEventListener::CanDrop(nsIDOMDragEvent* aEvent)
rv = selection->GetRangeCount(&rangeCount); rv = selection->GetRangeCount(&rangeCount);
NS_ENSURE_SUCCESS(rv, false); NS_ENSURE_SUCCESS(rv, false);
for (int32_t i = 0; i < rangeCount; i++) for (int32_t i = 0; i < rangeCount; i++) {
{
nsCOMPtr<nsIDOMRange> range; nsCOMPtr<nsIDOMRange> range;
rv = selection->GetRangeAt(i, getter_AddRefs(range)); rv = selection->GetRangeAt(i, getter_AddRefs(range));
if (NS_FAILED(rv) || !range) if (NS_FAILED(rv) || !range) {
continue; //don't bail yet, iterate through them all // Don't bail yet, iterate through them all
continue;
}
bool inRange = true; bool inRange = true;
(void)range->IsPointInRange(parent, offset, &inRange); range->IsPointInRange(parent, offset, &inRange);
if (inRange) if (inRange) {
return false; //okay, now you can bail, we are over the orginal selection // Okay, now you can bail, we are over the orginal selection
return false;
}
} }
} }
} }
@ -983,8 +990,9 @@ nsEditorEventListener::Focus(nsIDOMEvent* aEvent)
nsCOMPtr<nsIDOMElement> element; nsCOMPtr<nsIDOMElement> element;
fm->GetFocusedElement(getter_AddRefs(element)); fm->GetFocusedElement(getter_AddRefs(element));
if (!SameCOMIdentity(element, target)) if (!SameCOMIdentity(element, target)) {
return NS_OK; return NS_OK;
}
} }
} }
@ -1010,21 +1018,22 @@ nsEditorEventListener::Blur(nsIDOMEvent* aEvent)
nsCOMPtr<nsIDOMElement> element; nsCOMPtr<nsIDOMElement> element;
fm->GetFocusedElement(getter_AddRefs(element)); fm->GetFocusedElement(getter_AddRefs(element));
if (element) if (element) {
return NS_OK; return NS_OK;
}
mEditor->FinalizeSelection(); mEditor->FinalizeSelection();
return NS_OK; return NS_OK;
} }
void void
nsEditorEventListener::SpellCheckIfNeeded() { nsEditorEventListener::SpellCheckIfNeeded()
{
// If the spell check skip flag is still enabled from creation time, // If the spell check skip flag is still enabled from creation time,
// disable it because focused editors are allowed to spell check. // disable it because focused editors are allowed to spell check.
uint32_t currentFlags = 0; uint32_t currentFlags = 0;
mEditor->GetFlags(&currentFlags); mEditor->GetFlags(&currentFlags);
if(currentFlags & nsIPlaintextEditor::eEditorSkipSpellCheck) if(currentFlags & nsIPlaintextEditor::eEditorSkipSpellCheck) {
{
currentFlags ^= nsIPlaintextEditor::eEditorSkipSpellCheck; currentFlags ^= nsIPlaintextEditor::eEditorSkipSpellCheck;
mEditor->SetFlags(currentFlags); mEditor->SetFlags(currentFlags);
} }

View File

@ -34,7 +34,8 @@ nsresult
nsHTMLEditorEventListener::Connect(nsEditor* aEditor) nsHTMLEditorEventListener::Connect(nsEditor* aEditor)
{ {
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryObject(aEditor); nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryObject(aEditor);
nsCOMPtr<nsIHTMLInlineTableEditor> htmlInlineTableEditor = do_QueryObject(aEditor); nsCOMPtr<nsIHTMLInlineTableEditor> htmlInlineTableEditor =
do_QueryObject(aEditor);
NS_PRECONDITION(htmlEditor && htmlInlineTableEditor, NS_PRECONDITION(htmlEditor && htmlInlineTableEditor,
"Set nsHTMLEditor or its sub class"); "Set nsHTMLEditor or its sub class");
return nsEditorEventListener::Connect(aEditor); return nsEditorEventListener::Connect(aEditor);
@ -56,8 +57,8 @@ nsHTMLEditorEventListener::MouseUp(nsIDOMMouseEvent* aMouseEvent)
nsHTMLEditor* htmlEditor = GetHTMLEditor(); nsHTMLEditor* htmlEditor = GetHTMLEditor();
nsCOMPtr<nsIDOMEventTarget> target; nsCOMPtr<nsIDOMEventTarget> target;
nsresult res = aMouseEvent->GetTarget(getter_AddRefs(target)); nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target));
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(target, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(target, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target); nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
@ -77,21 +78,22 @@ nsHTMLEditorEventListener::MouseDown(nsIDOMMouseEvent* aMouseEvent)
nsHTMLEditor* htmlEditor = GetHTMLEditor(); nsHTMLEditor* htmlEditor = GetHTMLEditor();
// Detect only "context menu" click // Detect only "context menu" click
//XXX This should be easier to do! // XXX This should be easier to do!
// But eDOMEvents_contextmenu and NS_CONTEXTMENU is not exposed in any event interface :-( // But eDOMEvents_contextmenu and NS_CONTEXTMENU is not exposed in any event
// interface :-(
int16_t buttonNumber; int16_t buttonNumber;
nsresult res = aMouseEvent->GetButton(&buttonNumber); nsresult rv = aMouseEvent->GetButton(&buttonNumber);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
bool isContextClick = buttonNumber == 2; bool isContextClick = buttonNumber == 2;
int32_t clickCount; int32_t clickCount;
res = aMouseEvent->GetDetail(&clickCount); rv = aMouseEvent->GetDetail(&clickCount);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMEventTarget> target; nsCOMPtr<nsIDOMEventTarget> target;
res = aMouseEvent->GetExplicitOriginalTarget(getter_AddRefs(target)); rv = aMouseEvent->GetExplicitOriginalTarget(getter_AddRefs(target));
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(target, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(target, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target); nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
@ -100,86 +102,80 @@ nsHTMLEditorEventListener::MouseDown(nsIDOMMouseEvent* aMouseEvent)
return NS_OK; return NS_OK;
} }
if (isContextClick || (buttonNumber == 0 && clickCount == 2)) if (isContextClick || (buttonNumber == 0 && clickCount == 2)) {
{
nsCOMPtr<nsISelection> selection; nsCOMPtr<nsISelection> selection;
mEditor->GetSelection(getter_AddRefs(selection)); mEditor->GetSelection(getter_AddRefs(selection));
NS_ENSURE_TRUE(selection, NS_OK); NS_ENSURE_TRUE(selection, NS_OK);
// Get location of mouse within target node // Get location of mouse within target node
nsCOMPtr<nsIDOMNode> parent; nsCOMPtr<nsIDOMNode> parent;
res = aMouseEvent->GetRangeParent(getter_AddRefs(parent)); rv = aMouseEvent->GetRangeParent(getter_AddRefs(parent));
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE); NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
int32_t offset = 0; int32_t offset = 0;
res = aMouseEvent->GetRangeOffset(&offset); rv = aMouseEvent->GetRangeOffset(&offset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
// Detect if mouse point is within current selection for context click // Detect if mouse point is within current selection for context click
bool nodeIsInSelection = false; bool nodeIsInSelection = false;
if (isContextClick && !selection->Collapsed()) { if (isContextClick && !selection->Collapsed()) {
int32_t rangeCount; int32_t rangeCount;
res = selection->GetRangeCount(&rangeCount); rv = selection->GetRangeCount(&rangeCount);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
for (int32_t i = 0; i < rangeCount; i++) { for (int32_t i = 0; i < rangeCount; i++) {
nsCOMPtr<nsIDOMRange> range; nsCOMPtr<nsIDOMRange> range;
res = selection->GetRangeAt(i, getter_AddRefs(range)); rv = selection->GetRangeAt(i, getter_AddRefs(range));
if (NS_FAILED(res) || !range) if (NS_FAILED(rv) || !range) {
continue;//don't bail yet, iterate through them all // Don't bail yet, iterate through them all
continue;
}
res = range->IsPointInRange(parent, offset, &nodeIsInSelection); range->IsPointInRange(parent, offset, &nodeIsInSelection);
// Done when we find a range that we are in // Done when we find a range that we are in
if (nodeIsInSelection) if (nodeIsInSelection) {
break; break;
}
} }
} }
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(target); nsCOMPtr<nsIDOMNode> node = do_QueryInterface(target);
if (node && !nodeIsInSelection) if (node && !nodeIsInSelection) {
{ if (!element) {
if (!element) if (isContextClick) {
{
if (isContextClick)
{
// Set the selection to the point under the mouse cursor: // Set the selection to the point under the mouse cursor:
selection->Collapse(parent, offset); selection->Collapse(parent, offset);
} } else {
else
{
// Get enclosing link if in text so we can select the link // Get enclosing link if in text so we can select the link
nsCOMPtr<nsIDOMElement> linkElement; nsCOMPtr<nsIDOMElement> linkElement;
res = htmlEditor->GetElementOrParentByTagName(NS_LITERAL_STRING("href"), node, getter_AddRefs(linkElement)); rv = htmlEditor->GetElementOrParentByTagName(
NS_ENSURE_SUCCESS(res, res); NS_LITERAL_STRING("href"), node,
if (linkElement) getter_AddRefs(linkElement));
NS_ENSURE_SUCCESS(rv, rv);
if (linkElement) {
element = linkElement; element = linkElement;
}
} }
} }
// Select entire element clicked on if NOT within an existing selection // Select entire element clicked on if NOT within an existing selection
// and not the entire body, or table-related elements // and not the entire body, or table-related elements
if (element) if (element) {
{
nsCOMPtr<nsIDOMNode> selectAllNode = nsCOMPtr<nsIDOMNode> selectAllNode =
htmlEditor->FindUserSelectAllNode(element); htmlEditor->FindUserSelectAllNode(element);
if (selectAllNode) if (selectAllNode) {
{
nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(selectAllNode); nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(selectAllNode);
if (newElement) if (newElement) {
{
node = selectAllNode; node = selectAllNode;
element = newElement; element = newElement;
} }
} }
if (isContextClick && !nsHTMLEditUtils::IsImage(node)) if (isContextClick && !nsHTMLEditUtils::IsImage(node)) {
{
selection->Collapse(parent, offset); selection->Collapse(parent, offset);
} } else {
else
{
htmlEditor->SelectElement(element); htmlEditor->SelectElement(element);
} }
} }
@ -190,14 +186,11 @@ nsHTMLEditorEventListener::MouseDown(nsIDOMMouseEvent* aMouseEvent)
// Prevent bubbling if we changed selection or // Prevent bubbling if we changed selection or
// for all context clicks // for all context clicks
if (element || isContextClick) if (element || isContextClick) {
{
aMouseEvent->PreventDefault(); aMouseEvent->PreventDefault();
return NS_OK; return NS_OK;
} }
} } else if (!isContextClick && buttonNumber == 0 && clickCount == 1) {
else if (!isContextClick && buttonNumber == 0 && clickCount == 1)
{
// if the target element is an image, we have to display resizers // if the target element is an image, we have to display resizers
int32_t clientX, clientY; int32_t clientX, clientY;
aMouseEvent->GetClientX(&clientX); aMouseEvent->GetClientX(&clientX);
@ -214,8 +207,8 @@ nsHTMLEditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent)
NS_ENSURE_TRUE(aMouseEvent, NS_OK); NS_ENSURE_TRUE(aMouseEvent, NS_OK);
nsCOMPtr<nsIDOMEventTarget> target; nsCOMPtr<nsIDOMEventTarget> target;
nsresult res = aMouseEvent->GetTarget(getter_AddRefs(target)); nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target));
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(target, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(target, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target); nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);