2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 #include "core/html/HTMLTextAreaElement.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
31 #include "core/CSSValueKeywords.h"
32 #include "core/HTMLNames.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ExceptionCode.h"
35 #include "core/dom/Text.h"
36 #include "core/dom/shadow/ShadowRoot.h"
37 #include "core/editing/FrameSelection.h"
38 #include "core/editing/SpellChecker.h"
39 #include "core/editing/TextIterator.h"
40 #include "core/events/BeforeTextInsertedEvent.h"
41 #include "core/events/Event.h"
42 #include "core/frame/FrameHost.h"
43 #include "core/frame/LocalFrame.h"
44 #include "core/html/FormDataList.h"
45 #include "core/html/forms/FormController.h"
46 #include "core/html/shadow/ShadowElementNames.h"
47 #include "core/html/shadow/TextControlInnerElements.h"
48 #include "core/page/Chrome.h"
49 #include "core/page/ChromeClient.h"
50 #include "core/rendering/RenderTextControlMultiLine.h"
51 #include "platform/text/PlatformLocale.h"
52 #include "wtf/StdLibExtras.h"
53 #include "wtf/text/StringBuilder.h"
57 using namespace HTMLNames;
59 static const int defaultRows = 2;
60 static const int defaultCols = 20;
62 // On submission, LF characters are converted into CRLF.
63 // This function returns number of characters considering this.
64 static unsigned numberOfLineBreaks(const String& text)
66 unsigned length = text.length();
68 for (unsigned i = 0; i < length; i++) {
75 static inline unsigned computeLengthForSubmission(const String& text)
77 return text.length() + numberOfLineBreaks(text);
80 HTMLTextAreaElement::HTMLTextAreaElement(Document& document, HTMLFormElement* form)
81 : HTMLTextFormControlElement(textareaTag, document, form)
86 , m_valueIsUpToDate(true)
90 PassRefPtrWillBeRawPtr<HTMLTextAreaElement> HTMLTextAreaElement::create(Document& document, HTMLFormElement* form)
92 RefPtrWillBeRawPtr<HTMLTextAreaElement> textArea = adoptRefWillBeNoop(new HTMLTextAreaElement(document, form));
93 textArea->ensureUserAgentShadowRoot();
94 return textArea.release();
97 void HTMLTextAreaElement::didAddUserAgentShadowRoot(ShadowRoot& root)
99 root.appendChild(TextControlInnerEditorElement::create(document()));
102 const AtomicString& HTMLTextAreaElement::formControlType() const
104 DEFINE_STATIC_LOCAL(const AtomicString, textarea, ("textarea", AtomicString::ConstructFromLiteral));
108 FormControlState HTMLTextAreaElement::saveFormControlState() const
110 return m_isDirty ? FormControlState(value()) : FormControlState();
113 void HTMLTextAreaElement::restoreFormControlState(const FormControlState& state)
118 void HTMLTextAreaElement::childrenChanged(const ChildrenChange& change)
120 HTMLElement::childrenChanged(change);
121 setLastChangeWasNotUserEdit();
123 setInnerEditorValue(value());
125 setNonDirtyValue(defaultValue());
128 bool HTMLTextAreaElement::isPresentationAttribute(const QualifiedName& name) const
130 if (name == alignAttr) {
131 // Don't map 'align' attribute. This matches what Firefox, Opera and IE do.
132 // See http://bugs.webkit.org/show_bug.cgi?id=7075
136 if (name == wrapAttr)
138 return HTMLTextFormControlElement::isPresentationAttribute(name);
141 void HTMLTextAreaElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
143 if (name == wrapAttr) {
144 if (shouldWrapText()) {
145 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePreWrap);
146 addPropertyToPresentationAttributeStyle(style, CSSPropertyWordWrap, CSSValueBreakWord);
148 addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre);
149 addPropertyToPresentationAttributeStyle(style, CSSPropertyWordWrap, CSSValueNormal);
152 HTMLTextFormControlElement::collectStyleForPresentationAttribute(name, value, style);
155 void HTMLTextAreaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
157 if (name == rowsAttr) {
158 int rows = value.toInt();
161 if (m_rows != rows) {
164 renderer()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
166 } else if (name == colsAttr) {
167 int cols = value.toInt();
170 if (m_cols != cols) {
173 renderer()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
175 } else if (name == wrapAttr) {
176 // The virtual/physical values were a Netscape extension of HTML 3.0, now deprecated.
177 // The soft/hard /off values are a recommendation for HTML 4 extension by IE and NS 4.
179 if (equalIgnoringCase(value, "physical") || equalIgnoringCase(value, "hard") || equalIgnoringCase(value, "on"))
181 else if (equalIgnoringCase(value, "off"))
185 if (wrap != m_wrap) {
188 renderer()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
190 } else if (name == accesskeyAttr) {
191 // ignore for the moment
192 } else if (name == maxlengthAttr)
193 setNeedsValidityCheck();
195 HTMLTextFormControlElement::parseAttribute(name, value);
198 RenderObject* HTMLTextAreaElement::createRenderer(RenderStyle*)
200 return new RenderTextControlMultiLine(this);
203 bool HTMLTextAreaElement::appendFormData(FormDataList& encoding, bool)
205 if (name().isEmpty())
208 document().updateLayout();
210 const String& text = (m_wrap == HardWrap) ? valueWithHardLineBreaks() : value();
211 encoding.appendData(name(), text);
213 const AtomicString& dirnameAttrValue = fastGetAttribute(dirnameAttr);
214 if (!dirnameAttrValue.isNull())
215 encoding.appendData(dirnameAttrValue, directionForFormData());
219 void HTMLTextAreaElement::resetImpl()
221 setNonDirtyValue(defaultValue());
224 bool HTMLTextAreaElement::hasCustomFocusLogic() const
229 bool HTMLTextAreaElement::isKeyboardFocusable() const
231 // If a given text area can be focused at all, then it will always be keyboard focusable.
232 return isFocusable();
235 bool HTMLTextAreaElement::shouldShowFocusRingOnMouseFocus() const
240 void HTMLTextAreaElement::updateFocusAppearance(bool restorePreviousSelection)
242 if (!restorePreviousSelection)
243 setSelectionRange(0, 0);
245 restoreCachedSelection();
247 if (document().frame())
248 document().frame()->selection().revealSelection();
251 void HTMLTextAreaElement::defaultEventHandler(Event* event)
253 if (renderer() && (event->isMouseEvent() || event->isDragEvent() || event->hasInterface(EventNames::WheelEvent) || event->type() == EventTypeNames::blur))
255 else if (renderer() && event->isBeforeTextInsertedEvent())
256 handleBeforeTextInsertedEvent(static_cast<BeforeTextInsertedEvent*>(event));
258 HTMLTextFormControlElement::defaultEventHandler(event);
261 void HTMLTextAreaElement::handleFocusEvent(Element*, FocusType)
263 if (LocalFrame* frame = document().frame())
264 frame->spellChecker().didBeginEditing(this);
267 void HTMLTextAreaElement::subtreeHasChanged()
269 setChangedSinceLastFormControlChangeEvent(true);
270 m_valueIsUpToDate = false;
271 setNeedsValidityCheck();
272 setAutofilled(false);
277 // When typing in a textarea, childrenChanged is not called, so we need to force the directionality check.
278 calculateAndAdjustDirectionality();
280 ASSERT(document().isActive());
281 document().frameHost()->chrome().client().didChangeValueInTextField(*this);
284 void HTMLTextAreaElement::handleBeforeTextInsertedEvent(BeforeTextInsertedEvent* event) const
288 int signedMaxLength = maxLength();
289 if (signedMaxLength < 0)
291 unsigned unsignedMaxLength = static_cast<unsigned>(signedMaxLength);
293 const String& currentValue = innerEditorValue();
294 unsigned currentLength = computeLengthForSubmission(currentValue);
295 if (currentLength + computeLengthForSubmission(event->text()) < unsignedMaxLength)
298 // selectionLength represents the selection length of this text field to be
299 // removed by this insertion.
300 // If the text field has no focus, we don't need to take account of the
301 // selection length. The selection is the source of text drag-and-drop in
302 // that case, and nothing in the text field will be removed.
303 unsigned selectionLength = 0;
306 document().frame()->selection().selection().toNormalizedPositions(start, end);
307 selectionLength = computeLengthForSubmission(plainText(start, end));
309 ASSERT(currentLength >= selectionLength);
310 unsigned baseLength = currentLength - selectionLength;
311 unsigned appendableLength = unsignedMaxLength > baseLength ? unsignedMaxLength - baseLength : 0;
312 event->setText(sanitizeUserInputValue(event->text(), appendableLength));
315 String HTMLTextAreaElement::sanitizeUserInputValue(const String& proposedValue, unsigned maxLength)
317 if (maxLength > 0 && U16_IS_LEAD(proposedValue[maxLength - 1]))
319 return proposedValue.left(maxLength);
322 void HTMLTextAreaElement::updateValue() const
324 if (m_valueIsUpToDate)
327 m_value = innerEditorValue();
328 const_cast<HTMLTextAreaElement*>(this)->m_valueIsUpToDate = true;
329 const_cast<HTMLTextAreaElement*>(this)->notifyFormStateChanged();
331 const_cast<HTMLTextAreaElement*>(this)->updatePlaceholderVisibility(false);
334 String HTMLTextAreaElement::value() const
340 void HTMLTextAreaElement::setValue(const String& value, TextFieldEventBehavior eventBehavior)
342 RefPtrWillBeRawPtr<HTMLTextAreaElement> protector(this);
343 setValueCommon(value, eventBehavior);
345 if (document().focusedElement() == this)
346 document().frameHost()->chrome().client().didUpdateTextOfFocusedElementByNonUserInput();
349 void HTMLTextAreaElement::setNonDirtyValue(const String& value)
351 setValueCommon(value, DispatchNoEvent, ChangeSelection);
355 void HTMLTextAreaElement::setValueCommon(const String& newValue, TextFieldEventBehavior eventBehavior, SelectionOption selectionOption)
357 // Code elsewhere normalizes line endings added by the user via the keyboard or pasting.
358 // We normalize line endings coming from JavaScript here.
359 String normalizedValue = newValue.isNull() ? "" : newValue;
360 normalizedValue.replace("\r\n", "\n");
361 normalizedValue.replace('\r', '\n');
363 // Return early because we don't want to trigger other side effects
364 // when the value isn't changing.
365 // FIXME: Simple early return doesn't match the Firefox ever.
366 // Remove these lines.
367 if (normalizedValue == value()) {
368 if (selectionOption == ChangeSelection) {
369 setNeedsValidityCheck();
370 if (isFinishedParsingChildren()) {
371 // Set the caret to the end of the text value except for initialize.
372 unsigned endOfString = m_value.length();
373 setSelectionRange(endOfString, endOfString, SelectionHasNoDirection, NotChangeSelection);
379 m_value = normalizedValue;
380 setInnerEditorValue(m_value);
381 if (eventBehavior == DispatchNoEvent)
382 setLastChangeWasNotUserEdit();
383 updatePlaceholderVisibility(false);
384 setNeedsStyleRecalc(SubtreeStyleChange);
385 m_suggestedValue = String();
386 setNeedsValidityCheck();
387 if (isFinishedParsingChildren()) {
388 // Set the caret to the end of the text value except for initialize.
389 unsigned endOfString = m_value.length();
390 setSelectionRange(endOfString, endOfString, SelectionHasNoDirection, NotChangeSelection);
393 notifyFormStateChanged();
394 if (eventBehavior == DispatchNoEvent) {
395 setTextAsOfLastFormControlChangeEvent(normalizedValue);
397 if (eventBehavior == DispatchInputAndChangeEvent)
398 dispatchFormControlInputEvent();
399 dispatchFormControlChangeEvent();
403 void HTMLTextAreaElement::setInnerEditorValue(const String& value)
405 HTMLTextFormControlElement::setInnerEditorValue(value);
406 m_valueIsUpToDate = true;
409 String HTMLTextAreaElement::defaultValue() const
413 // Since there may be comments, ignore nodes other than text nodes.
414 for (Node* n = firstChild(); n; n = n->nextSibling()) {
416 value.append(toText(n)->data());
419 return value.toString();
422 void HTMLTextAreaElement::setDefaultValue(const String& defaultValue)
424 RefPtrWillBeRawPtr<Node> protectFromMutationEvents(this);
426 // To preserve comments, remove only the text nodes, then add a single text node.
427 WillBeHeapVector<RefPtrWillBeMember<Node> > textNodes;
428 for (Node* n = firstChild(); n; n = n->nextSibling()) {
432 size_t size = textNodes.size();
433 for (size_t i = 0; i < size; ++i)
434 removeChild(textNodes[i].get(), IGNORE_EXCEPTION);
436 // Normalize line endings.
437 String value = defaultValue;
438 value.replace("\r\n", "\n");
439 value.replace('\r', '\n');
441 insertBefore(document().createTextNode(value), firstChild(), IGNORE_EXCEPTION);
444 setNonDirtyValue(value);
447 int HTMLTextAreaElement::maxLength() const
450 int value = getAttribute(maxlengthAttr).toInt(&ok);
451 return ok && value >= 0 ? value : -1;
454 void HTMLTextAreaElement::setMaxLength(int newValue, ExceptionState& exceptionState)
457 exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0.");
459 setIntegralAttribute(maxlengthAttr, newValue);
462 String HTMLTextAreaElement::suggestedValue() const
464 return m_suggestedValue;
467 void HTMLTextAreaElement::setSuggestedValue(const String& value)
469 m_suggestedValue = value;
472 setInnerEditorValue(m_suggestedValue);
474 setInnerEditorValue(m_value);
475 updatePlaceholderVisibility(false);
476 setNeedsStyleRecalc(SubtreeStyleChange);
479 String HTMLTextAreaElement::validationMessage() const
485 return customValidationMessage();
488 return locale().queryString(blink::WebLocalizedString::ValidationValueMissing);
491 return locale().validationMessageTooLongText(computeLengthForSubmission(value()), maxLength());
496 bool HTMLTextAreaElement::valueMissing() const
498 // We should not call value() for performance.
499 return willValidate() && valueMissing(0);
502 bool HTMLTextAreaElement::valueMissing(const String* value) const
504 return isRequiredFormControl() && !isDisabledOrReadOnly() && (value ? *value : this->value()).isEmpty();
507 bool HTMLTextAreaElement::tooLong() const
509 // We should not call value() for performance.
510 return willValidate() && tooLong(0, CheckDirtyFlag);
513 bool HTMLTextAreaElement::tooLong(const String* value, NeedsToCheckDirtyFlag check) const
515 // Return false for the default value or value set by script even if it is
516 // longer than maxLength.
517 if (check == CheckDirtyFlag && !lastChangeWasUserEdit())
520 int max = maxLength();
523 return computeLengthForSubmission(value ? *value : this->value()) > static_cast<unsigned>(max);
526 bool HTMLTextAreaElement::isValidValue(const String& candidate) const
528 return !valueMissing(&candidate) && !tooLong(&candidate, IgnoreDirtyFlag);
531 void HTMLTextAreaElement::accessKeyAction(bool)
536 void HTMLTextAreaElement::setCols(int cols)
538 setIntegralAttribute(colsAttr, cols);
541 void HTMLTextAreaElement::setRows(int rows)
543 setIntegralAttribute(rowsAttr, rows);
546 bool HTMLTextAreaElement::matchesReadOnlyPseudoClass() const
551 bool HTMLTextAreaElement::matchesReadWritePseudoClass() const
553 return !isReadOnly();
556 void HTMLTextAreaElement::updatePlaceholderText()
558 HTMLElement* placeholder = placeholderElement();
559 const AtomicString& placeholderText = fastGetAttribute(placeholderAttr);
560 if (placeholderText.isEmpty()) {
562 userAgentShadowRoot()->removeChild(placeholder);
566 RefPtrWillBeRawPtr<HTMLDivElement> newElement = HTMLDivElement::create(document());
567 placeholder = newElement.get();
568 placeholder->setShadowPseudoId(AtomicString("-webkit-input-placeholder", AtomicString::ConstructFromLiteral));
569 placeholder->setAttribute(idAttr, ShadowElementNames::placeholder());
570 userAgentShadowRoot()->insertBefore(placeholder, innerEditorElement()->nextSibling());
572 placeholder->setTextContent(placeholderText);
575 bool HTMLTextAreaElement::isInteractiveContent() const
580 bool HTMLTextAreaElement::supportsAutofocus() const