Bug 1283915 - Preserve input selection properties after type change. r=smaug

MozReview-Commit-ID: 7xJKc3vIpTY

--HG--
extra : rebase_source : 6fa36a607efb1e8eec1cc8efbb174d622aa0281f
This commit is contained in:
Decky Coss 2016-07-27 11:04:53 -04:00
Родитель eac25e3163
Коммит 08a416fd4e
5 изменённых файлов: 95 добавлений и 0 удалений

Просмотреть файл

@ -4826,12 +4826,22 @@ HTMLInputElement::HandleTypeChange(uint8_t aNewType)
GetValue(aOldValue);
}
nsTextEditorState::SelectionProperties sp;
if (GetEditorState()) {
sp = mInputData.mState->GetSelectionProperties();
}
// We already have a copy of the value, lets free it and changes the type.
FreeData();
mType = aNewType;
if (IsSingleLineTextControl()) {
mInputData.mState = new nsTextEditorState(this);
if (!sp.IsDefault()) {
mInputData.mState->SetSelectionProperties(sp);
}
}
/**

Просмотреть файл

@ -1536,6 +1536,19 @@ nsTextEditorState::GetSelectionProperties()
return mSelectionProperties;
}
void
nsTextEditorState::SetSelectionProperties(nsTextEditorState::SelectionProperties& aProps)
{
if (mBoundFrame) {
mBoundFrame->SetSelectionRange(aProps.GetStart(),
aProps.GetEnd(),
aProps.GetDirection());
} else {
mSelectionProperties = aProps;
}
}
HTMLInputElement*
nsTextEditorState::GetParentNumberControl(nsFrame* aFrame) const
{

Просмотреть файл

@ -260,6 +260,7 @@ public:
bool IsSelectionCached() const;
SelectionProperties& GetSelectionProperties();
void SetSelectionProperties(SelectionProperties& aProps);
void WillInitEagerly() { mSelectionRestoreEagerInit = true; }
bool HasNeverInitializedBefore() const { return !mEverInited; }

Просмотреть файл

@ -5,6 +5,7 @@ support-files =
!/dom/html/test/reflect.js
[test_bug1039548.html]
[test_bug1283915.html]
[test_bug1286509.html]
skip-if = os == "android" || appname == "b2g" # up/down arrow keys not supported on android/b2g
[test_button_attributes_reflection.html]

Просмотреть файл

@ -0,0 +1,70 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1283915
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1283915</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
/** Test for Bug 1283915 **/
SimpleTest.waitForExplicitFinish();
function isCursorAtEnd(field){
is(field.selectionStart, field.value.length);
is(field.selectionEnd, field.value.length);
}
function test() {
var tField = document.getElementById("textField");
tField.focus();
synthesizeKey("a", {});
is(tField.value, "a");
isCursorAtEnd(tField);
document.body.offsetWidth; // frame must be created after type change
synthesizeKey("b", {});
is(tField.value, "ab");
isCursorAtEnd(tField);
synthesizeKey("c", {});
is(tField.value, "abc");
isCursorAtEnd(tField);
var nField = document.getElementById("numField");
nField.focus();
synthesizeKey("1", {});
is(nField.value, "1");
isCursorAtEnd(nField);
document.body.offsetWidth;
synthesizeKey("2", {});
is(nField.value, "12");
isCursorAtEnd(nField);
synthesizeKey("3", {});
is(nField.value, "123");
isCursorAtEnd(nField);
SimpleTest.finish();
}
SimpleTest.waitForFocus(test);
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1283915">Mozilla Bug 1283915</a>
<p id="display"></p>
<input id="textField" type="text" oninput="if (this.type !='password') this.type = 'password';">
<input id="numField" type="text" oninput="if (this.type !='number') this.type = 'number';">
<pre id="test">
</pre>
</body>
</html>