Bug 1623837 - Use the last focused selection in nsDocViewerFocusListener. r=masayuki

This fixes the case where you have a focused input (or selection for <input
disabled>) in an <iframe> and move the focus outside of the iframe.

Differential Revision: https://phabricator.services.mozilla.com/D67596

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Emilio Cobos Álvarez 2020-03-23 01:31:14 +00:00
parent 4d5019198d
commit f5459056be
6 changed files with 44 additions and 7 deletions

View File

@ -13,6 +13,7 @@
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsCRT.h"
#include "nsFrameSelection.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIContent.h"
@ -3397,26 +3398,27 @@ nsresult nsDocViewerFocusListener::HandleEvent(Event* aEvent) {
RefPtr<PresShell> presShell = mDocViewer->GetPresShell();
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
int16_t selectionStatus;
presShell->GetDisplaySelection(&selectionStatus);
RefPtr<nsFrameSelection> selection =
presShell->GetLastFocusedFrameSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
auto selectionStatus = selection->GetDisplaySelection();
nsAutoString eventType;
aEvent->GetType(eventType);
if (eventType.EqualsLiteral("focus")) {
// If selection was disabled, re-enable it.
if (selectionStatus == nsISelectionController::SELECTION_DISABLED ||
selectionStatus == nsISelectionController::SELECTION_HIDDEN) {
presShell->SetDisplaySelection(nsISelectionController::SELECTION_ON);
presShell->RepaintSelection(nsISelectionController::SELECTION_NORMAL);
selection->SetDisplaySelection(nsISelectionController::SELECTION_ON);
selection->RepaintSelection(SelectionType::eNormal);
}
} else {
MOZ_ASSERT(eventType.EqualsLiteral("blur"), "Unexpected event type");
// If selection was on, disable it.
if (selectionStatus == nsISelectionController::SELECTION_ON ||
selectionStatus == nsISelectionController::SELECTION_ATTENTION) {
presShell->SetDisplaySelection(
selection->SetDisplaySelection(
nsISelectionController::SELECTION_DISABLED);
presShell->RepaintSelection(nsISelectionController::SELECTION_NORMAL);
selection->RepaintSelection(SelectionType::eNormal);
}
}

View File

@ -0,0 +1,6 @@
<!doctype html>
<input id="outside" value="abc">
<iframe srcdoc="<!doctype html>Something inside <input value='some input'>"></iframe>
<script>
document.getElementById("outside").select();
</script>

View File

@ -0,0 +1,9 @@
<!doctype html>
<input id="outside" value="abc">
<iframe srcdoc="<!doctype html>Something inside <input value='some input'>"></iframe>
<script>
onload = function() {
document.querySelector("iframe").contentDocument.querySelector("input").select();
document.getElementById("outside").select();
}
</script>

View File

@ -0,0 +1,8 @@
<!doctype html>
<input id="outside" style="position: absolute; left: 200vw;"> <!-- intentionally out of view -->
<iframe srcdoc="<!doctype html>Something inside <input value='some input'>"></iframe>
<script>
onload = function() {
document.querySelector("iframe").contentDocument.querySelector("input").select();
}
</script>

View File

@ -0,0 +1,9 @@
<!doctype html>
<input id="outside" style="position: absolute; left: 200vw;"> <!-- intentionally out of view -->
<iframe srcdoc="<!doctype html>Something inside <input value='some input'>"></iframe>
<script>
onload = function() {
document.querySelector("iframe").contentDocument.querySelector("input").select();
document.getElementById("outside").select();
}
</script>

View File

@ -52,3 +52,6 @@ fuzzy(0-7,0-4) needs-focus == rtl-selection-with-decoration.html rtl-selection-w
needs-focus == semitransparent-decoration-line.html semitransparent-decoration-line-ref.html
fuzzy-if(OSX,0-1,0-6) fuzzy-if(Android,0-188,0-39) needs-focus == writing-mode.html writing-mode-ref.html
needs-focus == 1478604.html 1478604-ref.html
needs-focus != disabled-1.html disabled-1-notref.html
needs-focus != disabled-2.html disabled-2-notref.html