HTMLTextAreaElement.cpp [plain text]
#include "config.h"
#include "HTMLTextAreaElement.h"
#include "ChromeClient.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "FocusController.h"
#include "FormDataList.h"
#include "Frame.h"
#include "HTMLNames.h"
#include "MappedAttribute.h"
#include "Page.h"
#include "RenderStyle.h"
#include "RenderTextControlMultiLine.h"
#include "ScriptEventListener.h"
#include "Text.h"
#include "VisibleSelection.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
using namespace HTMLNames;
static const int defaultRows = 2;
static const int defaultCols = 20;
static inline void notifyFormStateChanged(const HTMLTextAreaElement* element)
{
Frame* frame = element->document()->frame();
if (!frame)
return;
frame->page()->chrome()->client()->formStateDidChange(element);
}
HTMLTextAreaElement::HTMLTextAreaElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
: HTMLFormControlElementWithState(tagName, document, form)
, m_rows(defaultRows)
, m_cols(defaultCols)
, m_wrap(SoftWrap)
, m_cachedSelectionStart(-1)
, m_cachedSelectionEnd(-1)
{
ASSERT(hasTagName(textareaTag));
setFormControlValueMatchesRenderer(true);
notifyFormStateChanged(this);
}
const AtomicString& HTMLTextAreaElement::formControlType() const
{
DEFINE_STATIC_LOCAL(const AtomicString, textarea, ("textarea"));
return textarea;
}
bool HTMLTextAreaElement::saveFormControlState(String& result) const
{
result = value();
return true;
}
void HTMLTextAreaElement::restoreFormControlState(const String& state)
{
setDefaultValue(state);
}
int HTMLTextAreaElement::selectionStart()
{
if (!renderer())
return 0;
if (document()->focusedNode() != this && m_cachedSelectionStart >= 0)
return m_cachedSelectionStart;
return toRenderTextControl(renderer())->selectionStart();
}
int HTMLTextAreaElement::selectionEnd()
{
if (!renderer())
return 0;
if (document()->focusedNode() != this && m_cachedSelectionEnd >= 0)
return m_cachedSelectionEnd;
return toRenderTextControl(renderer())->selectionEnd();
}
void HTMLTextAreaElement::setSelectionStart(int start)
{
if (!renderer())
return;
toRenderTextControl(renderer())->setSelectionStart(start);
}
void HTMLTextAreaElement::setSelectionEnd(int end)
{
if (!renderer())
return;
toRenderTextControl(renderer())->setSelectionEnd(end);
}
void HTMLTextAreaElement::select()
{
if (!renderer())
return;
toRenderTextControl(renderer())->select();
}
void HTMLTextAreaElement::setSelectionRange(int start, int end)
{
if (!renderer())
return;
toRenderTextControl(renderer())->setSelectionRange(start, end);
}
void HTMLTextAreaElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
setValue(defaultValue());
HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
}
void HTMLTextAreaElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == rowsAttr) {
int rows = attr->value().toInt();
if (rows <= 0)
rows = defaultRows;
if (m_rows != rows) {
m_rows = rows;
if (renderer())
renderer()->setNeedsLayoutAndPrefWidthsRecalc();
}
} else if (attr->name() == colsAttr) {
int cols = attr->value().toInt();
if (cols <= 0)
cols = defaultCols;
if (m_cols != cols) {
m_cols = cols;
if (renderer())
renderer()->setNeedsLayoutAndPrefWidthsRecalc();
}
} else if (attr->name() == wrapAttr) {
WrapMethod wrap;
if (equalIgnoringCase(attr->value(), "physical") || equalIgnoringCase(attr->value(), "hard") || equalIgnoringCase(attr->value(), "on"))
wrap = HardWrap;
else if (equalIgnoringCase(attr->value(), "off"))
wrap = NoWrap;
else
wrap = SoftWrap;
if (wrap != m_wrap) {
m_wrap = wrap;
if (renderer())
renderer()->setNeedsLayoutAndPrefWidthsRecalc();
}
} else if (attr->name() == accesskeyAttr) {
} else if (attr->name() == alignAttr) {
} else if (attr->name() == onfocusAttr)
setAttributeEventListener(eventNames().focusEvent, createAttributeEventListener(this, attr));
else if (attr->name() == onblurAttr)
setAttributeEventListener(eventNames().blurEvent, createAttributeEventListener(this, attr));
else if (attr->name() == onselectAttr)
setAttributeEventListener(eventNames().selectEvent, createAttributeEventListener(this, attr));
else if (attr->name() == onchangeAttr)
setAttributeEventListener(eventNames().changeEvent, createAttributeEventListener(this, attr));
else
HTMLFormControlElementWithState::parseMappedAttribute(attr);
}
RenderObject* HTMLTextAreaElement::createRenderer(RenderArena* arena, RenderStyle*)
{
return new (arena) RenderTextControlMultiLine(this);
}
bool HTMLTextAreaElement::appendFormData(FormDataList& encoding, bool)
{
if (name().isEmpty())
return false;
RenderTextControl* control = toRenderTextControl(renderer());
const String& text = (m_wrap == HardWrap && control) ? control->textWithHardLineBreaks() : value();
encoding.appendData(name(), text);
return true;
}
void HTMLTextAreaElement::reset()
{
setValue(defaultValue());
}
bool HTMLTextAreaElement::isKeyboardFocusable(KeyboardEvent*) const
{
return isFocusable();
}
bool HTMLTextAreaElement::isMouseFocusable() const
{
return isFocusable();
}
void HTMLTextAreaElement::updateFocusAppearance(bool restorePreviousSelection)
{
ASSERT(renderer());
if (!restorePreviousSelection || m_cachedSelectionStart < 0) {
#if ENABLE(ON_FIRST_TEXTAREA_FOCUS_SELECT_ALL)
select();
#else
setSelectionRange(0, 0);
#endif
} else {
setSelectionRange(m_cachedSelectionStart, m_cachedSelectionEnd);
}
if (document()->frame())
document()->frame()->revealSelection();
}
void HTMLTextAreaElement::defaultEventHandler(Event* event)
{
if (renderer() && (event->isMouseEvent() || event->isDragEvent() || event->isWheelEvent() || event->type() == eventNames().blurEvent))
static_cast<RenderTextControlMultiLine*>(renderer())->forwardEvent(event);
HTMLFormControlElementWithState::defaultEventHandler(event);
}
void HTMLTextAreaElement::rendererWillBeDestroyed()
{
updateValue();
}
void HTMLTextAreaElement::updateValue() const
{
if (formControlValueMatchesRenderer())
return;
ASSERT(renderer());
m_value = toRenderTextControl(renderer())->text();
const_cast<HTMLTextAreaElement*>(this)->setFormControlValueMatchesRenderer(true);
notifyFormStateChanged(this);
}
String HTMLTextAreaElement::value() const
{
updateValue();
return m_value;
}
void HTMLTextAreaElement::setValue(const String& value)
{
String normalizedValue = value.isNull() ? "" : value;
normalizedValue.replace("\r\n", "\n");
normalizedValue.replace('\r', '\n');
if (normalizedValue == this->value())
return;
m_value = normalizedValue;
setFormControlValueMatchesRenderer(true);
if (inDocument())
document()->updateStyleIfNeeded();
if (renderer())
renderer()->updateFromElement();
if (document()->focusedNode() == this) {
unsigned endOfString = m_value.length();
setSelectionRange(endOfString, endOfString);
}
setNeedsStyleRecalc();
notifyFormStateChanged(this);
}
String HTMLTextAreaElement::defaultValue() const
{
String value = "";
for (Node* n = firstChild(); n; n = n->nextSibling()) {
if (n->isTextNode())
value += static_cast<Text*>(n)->data();
}
UChar firstCharacter = value[0];
if (firstCharacter == '\r' && value[1] == '\n')
value.remove(0, 2);
else if (firstCharacter == '\r' || firstCharacter == '\n')
value.remove(0, 1);
return value;
}
void HTMLTextAreaElement::setDefaultValue(const String& defaultValue)
{
Vector<RefPtr<Node> > textNodes;
for (Node* n = firstChild(); n; n = n->nextSibling()) {
if (n->isTextNode())
textNodes.append(n);
}
ExceptionCode ec;
size_t size = textNodes.size();
for (size_t i = 0; i < size; ++i)
removeChild(textNodes[i].get(), ec);
String value = defaultValue;
value.replace("\r\n", "\n");
value.replace('\r', '\n');
if (value[0] == '\n')
value = "\n" + value;
insertBefore(document()->createTextNode(value), firstChild(), ec);
setValue(value);
}
void HTMLTextAreaElement::accessKeyAction(bool)
{
focus();
}
const AtomicString& HTMLTextAreaElement::accessKey() const
{
return getAttribute(accesskeyAttr);
}
void HTMLTextAreaElement::setAccessKey(const String& value)
{
setAttribute(accesskeyAttr, value);
}
void HTMLTextAreaElement::setCols(int cols)
{
setAttribute(colsAttr, String::number(cols));
}
void HTMLTextAreaElement::setRows(int rows)
{
setAttribute(rowsAttr, String::number(rows));
}
VisibleSelection HTMLTextAreaElement::selection() const
{
if (!renderer() || m_cachedSelectionStart < 0 || m_cachedSelectionEnd < 0)
return VisibleSelection();
return toRenderTextControl(renderer())->selection(m_cachedSelectionStart, m_cachedSelectionEnd);
}
bool HTMLTextAreaElement::shouldUseInputMethod() const
{
return true;
}
}