Bug 1443902 - Reinitilize selection after destroying nsIEditingSession. r=masayuki

When setting contenteditable to false, editing session destroys HTMLEditor.
Destroying HTMLEditor means that selection visibility is reset by
FinalizeSelection.

So after calling TearDownEditorOnWindow on nsHTMLDocument, we should initialize
selection visibility if current focus is text control that has editor.

MozReview-Commit-ID: 4V8kZtOtKO3

--HG--
extra : rebase_source : 9d90c12b3c93e4dfd95095ce29a26e5fdd83f952
This commit is contained in:
Makoto Kato 2018-07-09 16:53:47 +09:00
parent 1609fd1ef0
commit 16910a6e95
12 changed files with 181 additions and 0 deletions

View File

@ -2339,6 +2339,20 @@ nsHTMLDocument::TurnEditingOff()
mEditingState = eOff;
// Editor resets selection since it is being destroyed. But if focus is
// still into editable control, we have to initialize selection again.
nsFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
Element* element = fm->GetFocusedElement();
nsCOMPtr<nsITextControlElement> txtCtrl = do_QueryInterface(element);
if (txtCtrl) {
RefPtr<TextEditor> textEditor = txtCtrl->GetTextEditor();
if (textEditor) {
textEditor->ReinitializeSelection(*element);
}
}
}
return NS_OK;
}

View File

@ -4799,6 +4799,23 @@ EditorBase::FinalizeSelection()
return NS_OK;
}
void
EditorBase::ReinitializeSelection(Element& aElement)
{
if (NS_WARN_IF(Destroyed())) {
return;
}
OnFocus(&aElement);
nsPresContext* context = GetPresContext();
if (NS_WARN_IF(!context)) {
return;
}
nsCOMPtr<nsIContent> focusedContent = GetFocusedContentForIME();
IMEStateManager::OnFocusInEditor(context, focusedContent, *this);
}
Element*
EditorBase::GetEditorRoot()
{

View File

@ -672,6 +672,14 @@ public:
*/
void SyncRealTimeSpell();
/**
* This method re-initializes the selection and caret state that are for
* current editor state. When editor session is destroyed, it always reset
* selection state even if this has no focus. So if destroying editor,
* we have to call this method for focused editor to set selection state.
*/
void ReinitializeSelection(Element& aElement);
protected: // May be called by friends.
/****************************************************************************
* Some classes like TextEditRules, HTMLEditRules, WSRunObject which are

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(4, 4);
}
</script>
</head>
<body onload="init()">
<textarea id=t1 contenteditable=true>ABCD</textarea>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(4, 4);
document.getElementById("t1").setAttribute("contentEditable", "false");
}
</script>
</head>
<body onload="init()">
<textarea id=t1 contenteditable=true>ABCD</textarea>
</body>
</html>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(4, 4);
}
</script>
</head>
<body onload="init()">
<div id="d1">
<input type="text" id=t1 value="ABCD">
</div>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(4, 4);
document.getElementById("d1").setAttribute("contentEditable", "false");
}
</script>
</head>
<body onload="init()">
<div contenteditable=true id="d1">
<input type="text" id=t1 value="ABCD">
</div>
</body>
</html>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(0, 1);
}
</script>
</head>
<body onload="init()">
<div>
<input type="text" id=t1 value="ABCD" readonly>
</div>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(0, 1);
document.getElementById("d1").setAttribute("contentEditable", "false");
}
</script>
</head>
<body onload="init()">
<div contenteditable=true id="d1">
<input type="text" id=t1 value="ABCD" readonly>
</div>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(4, 4);
}
</script>
</head>
<body onload="init()">
<div id="d1">
<input type="text">
</div>
<input type="text" id=t1 value="ABCD">
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<script>
function init()
{
document.getElementById("t1").focus();
document.getElementById("t1").setSelectionRange(4, 4);
document.getElementById("d1").setAttribute("contentEditable", "false");
}
</script>
</head>
<body onload="init()">
<div contenteditable=true id="d1">
<input type="text">
</div>
<input type="text" id=t1 value="ABCD">
</body>
</html>

View File

@ -135,3 +135,7 @@ needs-focus == spellcheck-contenteditable-focused-reframe.html spellcheck-conten
needs-focus == 969773.html 969773-ref.html
fuzzy-if(skiaContent,1,220) == 997805.html 997805-ref.html
fuzzy-if(skiaContent,1,220) skip-if(verify&&OSX) == 1088158.html 1088158-ref.html
needs-focus == 1443902-1.html 1443902-1-ref.html
needs-focus == 1443902-2.html 1443902-2-ref.html
needs-focus == 1443902-3.html 1443902-3-ref.html
needs-focus == 1443902-4.html 1443902-4-ref.html