Bug 1490840 - Only delete text in ReplaceText if the string is empty. r=Jamie

This commit is contained in:
Eitan Isaacson 2018-09-18 16:16:00 -04:00
parent 46b1fcfb0f
commit c712a2aaaa
3 changed files with 46 additions and 8 deletions

View File

@ -56,20 +56,18 @@ HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset)
inline void
HyperTextAccessible::ReplaceText(const nsAString& aText)
{
// We need to call DeleteText() even if there is no contents because we need
// to ensure to move focus to the editor via SetSelectionRange() called in
// DeleteText().
DeleteText(0, CharacterCount());
if (aText.Length() == 0) {
DeleteText(0, CharacterCount());
return;
}
SetSelectionRange(0, CharacterCount());
RefPtr<TextEditor> textEditor = GetEditor();
if (!textEditor) {
return;
}
// DeleteText() may cause inserting <br> element in some cases. Let's
// select all again and replace whole contents.
textEditor->SelectAll();
DebugOnly<nsresult> rv = textEditor->InsertTextAsAction(aText);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert the new text");
}

View File

@ -12,5 +12,6 @@ support-files = doc.html
[test_lineboundary.html]
[test_passwords.html]
[test_selection.html]
[test_settext_input_event.html]
[test_wordboundary.html]
[test_words.html]

View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>Test that setTextContents only sends one DOM input event</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript"
src="../common.js"></script>
<script type="application/javascript">
async function doTest() {
let input = getAccessible("input", [nsIAccessibleEditableText]);
let eventPromise = new Promise(resolve =>
document.getElementById("input").addEventListener(
"input", resolve, { once: true }));
input.setTextContents("goodbye");
let inputEvent = await eventPromise;
is(inputEvent.target.value, "goodbye", "input set to new value.");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
addA11yLoadEvent(doTest);
</script>
</head>
<body>
<a target="_blank"
title="HyperTextAccessible::ReplaceText causes two distinct DOM 'input' events"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=1490840">Mozilla Bug 1490840</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<input id="input" value="hello">
</body>
</html>