Skip to content

Commit

Permalink
Bug 1283915 - Preserve input selection properties after type change. …
Browse files Browse the repository at this point in the history
…r=smaug

MozReview-Commit-ID: 7xJKc3vIpTY
  • Loading branch information
daliacoss committed Jul 27, 2016
1 parent f6e9881 commit 4d5b39d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dom/html/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions dom/html/nsTextEditorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions dom/html/nsTextEditorState.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class nsTextEditorState : public mozilla::SupportsWeakPtr<nsTextEditorState> {

bool IsSelectionCached() const;
SelectionProperties& GetSelectionProperties();
void SetSelectionProperties(SelectionProperties& aProps);
void WillInitEagerly() { mSelectionRestoreEagerInit = true; }
bool HasNeverInitializedBefore() const { return !mEverInited; }

Expand Down
1 change: 1 addition & 0 deletions dom/html/test/forms/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
70 changes: 70 additions & 0 deletions dom/html/test/forms/test_bug1283915.html
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 4d5b39d

Please sign in to comment.