Bug 918940 - Part b: Implement setRangeText for HTMLTextAreaElement. r=ehsan

This commit is contained in:
Andrew Quartey 2013-10-02 10:30:29 -04:00
parent aeba023912
commit 87bb620d84
4 changed files with 120 additions and 6 deletions

View File

@ -889,6 +889,9 @@ HTMLTextAreaElement::SetSelectionRange(uint32_t aSelectionStart,
rv = textControlFrame->SetSelectionRange(aSelectionStart, aSelectionEnd, dir);
if (NS_SUCCEEDED(rv)) {
rv = textControlFrame->ScrollSelectionIntoView();
nsRefPtr<nsAsyncDOMEvent> event =
new nsAsyncDOMEvent(this, NS_LITERAL_STRING("select"), true, false);
event->PostDOMEvent();
}
}
}
@ -898,6 +901,103 @@ HTMLTextAreaElement::SetSelectionRange(uint32_t aSelectionStart,
}
}
void
HTMLTextAreaElement::SetRangeText(const nsAString& aReplacement,
ErrorResult& aRv)
{
int32_t start, end;
aRv = GetSelectionRange(&start, &end);
if (aRv.Failed()) {
if (mState.IsSelectionCached()) {
start = mState.GetSelectionProperties().mStart;
end = mState.GetSelectionProperties().mEnd;
aRv = NS_OK;
}
}
SetRangeText(aReplacement, start, end, mozilla::dom::SelectionMode::Preserve,
aRv, start, end);
}
void
HTMLTextAreaElement::SetRangeText(const nsAString& aReplacement,
uint32_t aStart, uint32_t aEnd,
const SelectionMode& aSelectMode,
ErrorResult& aRv, int32_t aSelectionStart,
int32_t aSelectionEnd)
{
if (aStart > aEnd) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return;
}
nsAutoString value;
GetValueInternal(value, false);
uint32_t inputValueLength = value.Length();
if (aStart > inputValueLength) {
aStart = inputValueLength;
}
if (aEnd > inputValueLength) {
aEnd = inputValueLength;
}
if (aSelectionStart == -1 && aSelectionEnd == -1) {
aRv = GetSelectionRange(&aSelectionStart, &aSelectionEnd);
if (aRv.Failed()) {
if (mState.IsSelectionCached()) {
aSelectionStart = mState.GetSelectionProperties().mStart;
aSelectionEnd = mState.GetSelectionProperties().mEnd;
aRv = NS_OK;
}
}
}
if (aStart < aEnd) {
value.Replace(aStart, aEnd - aStart, aReplacement);
SetValueInternal(value, false);
}
uint32_t newEnd = aStart + aReplacement.Length();
int32_t delta = aReplacement.Length() - (aEnd - aStart);
switch (aSelectMode) {
case mozilla::dom::SelectionMode::Select:
{
aSelectionStart = aStart;
aSelectionEnd = newEnd;
}
break;
case mozilla::dom::SelectionMode::Start:
{
aSelectionStart = aSelectionEnd = aStart;
}
break;
case mozilla::dom::SelectionMode::End:
{
aSelectionStart = aSelectionEnd = newEnd;
}
break;
case mozilla::dom::SelectionMode::Preserve:
{
if ((uint32_t)aSelectionStart > aEnd)
aSelectionStart += delta;
else if ((uint32_t)aSelectionStart > aStart)
aSelectionStart = aStart;
if ((uint32_t)aSelectionEnd > aEnd)
aSelectionEnd += delta;
else if ((uint32_t)aSelectionEnd > aStart)
aSelectionEnd = newEnd;
}
break;
}
Optional<nsAString> direction;
SetSelectionRange(aSelectionStart, aSelectionEnd, direction, aRv);
}
nsresult
HTMLTextAreaElement::Reset()
{

View File

@ -17,6 +17,7 @@
#include "nsStubMutationObserver.h"
#include "nsIConstraintValidation.h"
#include "mozilla/dom/HTMLFormElement.h"
#include "mozilla/dom/HTMLInputElementBinding.h"
#include "nsGkAtoms.h"
#include "nsTextEditorState.h"
@ -214,6 +215,14 @@ public:
{
return GetBoolAttr(nsGkAtoms::required);
}
void SetRangeText(const nsAString& aReplacement, ErrorResult& aRv);
void SetRangeText(const nsAString& aReplacement, uint32_t aStart,
uint32_t aEnd, const SelectionMode& aSelectMode,
ErrorResult& aRv, int32_t aSelectionStart = -1,
int32_t aSelectionEnd = -1);
void SetRequired(bool aRequired, ErrorResult& aError)
{
SetHTMLBoolAttr(nsGkAtoms::required, aRequired, aError);

View File

@ -4,13 +4,14 @@
https://bugzilla.mozilla.org/show_bug.cgi?id=850364
-->
<head>
<title>Test for Bug 850364</title>
<title>Test for Bug 850364 && Bug 918940</title>
<script type="text/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" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=850364">Mozilla Bug 850364</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=850364">Mozilla Bug 918940</a>
<p id="display"></p>
<div id="content">
@ -20,6 +21,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=850364
<input type="url" id="input_url"></input>
<input type="tel" id="input_tel"></input>
<input type="password" id="input_password"></input>
<textarea id="input_textarea"></textarea>
<!-- "SetRangeText() non-supported types" -->
<input type="button" id="input_button"></input>
@ -31,14 +33,14 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=850364
<input type="range" id="input_range"></input>
<input type="file" id="input_file"></input>
<input type="email" id="input_email"></input>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 850364 **/
/** Tests for Bug 850364 && Bug 918940 **/
var SupportedTypes = ["text", "search", "url", "tel", "password"];
var SupportedTypes = ["text", "search", "url", "tel", "password", "textarea"];
var NonSupportedTypes = ["button", "submit", "image", "reset", "radio",
"checkbox", "range", "file", "email"];

View File

@ -63,8 +63,11 @@ interface HTMLTextAreaElement : HTMLElement {
attribute unsigned long selectionEnd;
[Throws]
attribute DOMString selectionDirection;
// void setRangeText(DOMString replacement);
// void setRangeText(DOMString replacement, unsigned long start, unsigned long end, optional SelectionMode selectionMode);
[Throws]
void setRangeText(DOMString replacement);
[Throws]
void setRangeText(DOMString replacement, unsigned long start,
unsigned long end, optional SelectionMode selectionMode = "preserve");
[Throws]
void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction);
};