Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLTextAreaElement.cpp
1 /*
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)
8  *
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.
13  *
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.
18  *
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.
23  *
24  */
25
26 #include "config.h"
27 #include "core/html/HTMLTextAreaElement.h"
28
29 #include "CSSValueKeywords.h"
30 #include "HTMLNames.h"
31 #include "bindings/v8/ExceptionState.h"
32 #include "bindings/v8/ExceptionStatePlaceholder.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/events/ThreadLocalEventNames.h"
43 #include "core/html/FormDataList.h"
44 #include "core/html/forms/FormController.h"
45 #include "core/html/shadow/ShadowElementNames.h"
46 #include "core/html/shadow/TextControlInnerElements.h"
47 #include "core/frame/Frame.h"
48 #include "core/rendering/RenderTextControlMultiLine.h"
49 #include "platform/text/PlatformLocale.h"
50 #include "wtf/StdLibExtras.h"
51 #include "wtf/text/StringBuilder.h"
52
53 namespace WebCore {
54
55 using namespace HTMLNames;
56
57 static const int defaultRows = 2;
58 static const int defaultCols = 20;
59
60 // On submission, LF characters are converted into CRLF.
61 // This function returns number of characters considering this.
62 static unsigned numberOfLineBreaks(const String& text)
63 {
64     unsigned length = text.length();
65     unsigned count = 0;
66     for (unsigned i = 0; i < length; i++) {
67         if (text[i] == '\n')
68             count++;
69     }
70     return count;
71 }
72
73 static inline unsigned computeLengthForSubmission(const String& text)
74 {
75     return text.length() + numberOfLineBreaks(text);
76 }
77
78 HTMLTextAreaElement::HTMLTextAreaElement(Document& document, HTMLFormElement* form)
79     : HTMLTextFormControlElement(textareaTag, document, form)
80     , m_rows(defaultRows)
81     , m_cols(defaultCols)
82     , m_wrap(SoftWrap)
83     , m_isDirty(false)
84 {
85     setFormControlValueMatchesRenderer(true);
86     ScriptWrappable::init(this);
87 }
88
89 PassRefPtr<HTMLTextAreaElement> HTMLTextAreaElement::create(Document& document, HTMLFormElement* form)
90 {
91     RefPtr<HTMLTextAreaElement> textArea = adoptRef(new HTMLTextAreaElement(document, form));
92     textArea->ensureUserAgentShadowRoot();
93     return textArea.release();
94 }
95
96 void HTMLTextAreaElement::didAddUserAgentShadowRoot(ShadowRoot& root)
97 {
98     root.appendChild(TextControlInnerTextElement::create(document()));
99 }
100
101 const AtomicString& HTMLTextAreaElement::formControlType() const
102 {
103     DEFINE_STATIC_LOCAL(const AtomicString, textarea, ("textarea", AtomicString::ConstructFromLiteral));
104     return textarea;
105 }
106
107 FormControlState HTMLTextAreaElement::saveFormControlState() const
108 {
109     return m_isDirty ? FormControlState(value()) : FormControlState();
110 }
111
112 void HTMLTextAreaElement::restoreFormControlState(const FormControlState& state)
113 {
114     setValue(state[0]);
115 }
116
117 void HTMLTextAreaElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
118 {
119     HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
120     setLastChangeWasNotUserEdit();
121     if (m_isDirty)
122         setInnerTextValue(value());
123     else
124         setNonDirtyValue(defaultValue());
125 }
126
127 bool HTMLTextAreaElement::isPresentationAttribute(const QualifiedName& name) const
128 {
129     if (name == alignAttr) {
130         // Don't map 'align' attribute.  This matches what Firefox, Opera and IE do.
131         // See http://bugs.webkit.org/show_bug.cgi?id=7075
132         return false;
133     }
134
135     if (name == wrapAttr)
136         return true;
137     return HTMLTextFormControlElement::isPresentationAttribute(name);
138 }
139
140 void HTMLTextAreaElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
141 {
142     if (name == wrapAttr) {
143         if (shouldWrapText()) {
144             addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePreWrap);
145             addPropertyToPresentationAttributeStyle(style, CSSPropertyWordWrap, CSSValueBreakWord);
146         } else {
147             addPropertyToPresentationAttributeStyle(style, CSSPropertyWhiteSpace, CSSValuePre);
148             addPropertyToPresentationAttributeStyle(style, CSSPropertyWordWrap, CSSValueNormal);
149         }
150     } else
151         HTMLTextFormControlElement::collectStyleForPresentationAttribute(name, value, style);
152 }
153
154 void HTMLTextAreaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
155 {
156     if (name == rowsAttr) {
157         int rows = value.toInt();
158         if (rows <= 0)
159             rows = defaultRows;
160         if (m_rows != rows) {
161             m_rows = rows;
162             if (renderer())
163                 renderer()->setNeedsLayoutAndPrefWidthsRecalc();
164         }
165     } else if (name == colsAttr) {
166         int cols = value.toInt();
167         if (cols <= 0)
168             cols = defaultCols;
169         if (m_cols != cols) {
170             m_cols = cols;
171             if (renderer())
172                 renderer()->setNeedsLayoutAndPrefWidthsRecalc();
173         }
174     } else if (name == wrapAttr) {
175         // The virtual/physical values were a Netscape extension of HTML 3.0, now deprecated.
176         // The soft/hard /off values are a recommendation for HTML 4 extension by IE and NS 4.
177         WrapMethod wrap;
178         if (equalIgnoringCase(value, "physical") || equalIgnoringCase(value, "hard") || equalIgnoringCase(value, "on"))
179             wrap = HardWrap;
180         else if (equalIgnoringCase(value, "off"))
181             wrap = NoWrap;
182         else
183             wrap = SoftWrap;
184         if (wrap != m_wrap) {
185             m_wrap = wrap;
186             if (renderer())
187                 renderer()->setNeedsLayoutAndPrefWidthsRecalc();
188         }
189     } else if (name == accesskeyAttr) {
190         // ignore for the moment
191     } else if (name == maxlengthAttr)
192         setNeedsValidityCheck();
193     else
194         HTMLTextFormControlElement::parseAttribute(name, value);
195 }
196
197 RenderObject* HTMLTextAreaElement::createRenderer(RenderStyle*)
198 {
199     return new RenderTextControlMultiLine(this);
200 }
201
202 bool HTMLTextAreaElement::appendFormData(FormDataList& encoding, bool)
203 {
204     if (name().isEmpty())
205         return false;
206
207     document().updateLayout();
208
209     const String& text = (m_wrap == HardWrap) ? valueWithHardLineBreaks() : value();
210     encoding.appendData(name(), text);
211
212     const AtomicString& dirnameAttrValue = fastGetAttribute(dirnameAttr);
213     if (!dirnameAttrValue.isNull())
214         encoding.appendData(dirnameAttrValue, directionForFormData());
215     return true;
216 }
217
218 void HTMLTextAreaElement::resetImpl()
219 {
220     setNonDirtyValue(defaultValue());
221 }
222
223 bool HTMLTextAreaElement::hasCustomFocusLogic() const
224 {
225     return true;
226 }
227
228 bool HTMLTextAreaElement::isKeyboardFocusable() const
229 {
230     // If a given text area can be focused at all, then it will always be keyboard focusable.
231     return isFocusable();
232 }
233
234 bool HTMLTextAreaElement::shouldShowFocusRingOnMouseFocus() const
235 {
236     return true;
237 }
238
239 void HTMLTextAreaElement::updateFocusAppearance(bool restorePreviousSelection)
240 {
241     if (!restorePreviousSelection || !hasCachedSelection()) {
242         // If this is the first focus, set a caret at the beginning of the text.
243         // This matches some browsers' behavior; see bug 11746 Comment #15.
244         // http://bugs.webkit.org/show_bug.cgi?id=11746#c15
245         setSelectionRange(0, 0);
246     } else
247         restoreCachedSelection();
248
249     if (document().frame())
250         document().frame()->selection().revealSelection();
251 }
252
253 void HTMLTextAreaElement::defaultEventHandler(Event* event)
254 {
255     if (renderer() && (event->isMouseEvent() || event->isDragEvent() || event->hasInterface(EventNames::WheelEvent) || event->type() == EventTypeNames::blur))
256         forwardEvent(event);
257     else if (renderer() && event->isBeforeTextInsertedEvent())
258         handleBeforeTextInsertedEvent(static_cast<BeforeTextInsertedEvent*>(event));
259
260     HTMLTextFormControlElement::defaultEventHandler(event);
261 }
262
263 void HTMLTextAreaElement::handleFocusEvent(Element*, FocusType)
264 {
265     if (Frame* frame = document().frame())
266         frame->spellChecker().didBeginEditing(this);
267 }
268
269 void HTMLTextAreaElement::subtreeHasChanged()
270 {
271     setChangedSinceLastFormControlChangeEvent(true);
272     setFormControlValueMatchesRenderer(false);
273     setNeedsValidityCheck();
274
275     if (!focused())
276         return;
277
278     // When typing in a textarea, childrenChanged is not called, so we need to force the directionality check.
279     calculateAndAdjustDirectionality();
280 }
281
282 void HTMLTextAreaElement::handleBeforeTextInsertedEvent(BeforeTextInsertedEvent* event) const
283 {
284     ASSERT(event);
285     ASSERT(renderer());
286     int signedMaxLength = maxLength();
287     if (signedMaxLength < 0)
288         return;
289     unsigned unsignedMaxLength = static_cast<unsigned>(signedMaxLength);
290
291     const String& currentValue = innerTextValue();
292     unsigned currentLength = computeLengthForSubmission(currentValue);
293     if (currentLength + computeLengthForSubmission(event->text()) < unsignedMaxLength)
294         return;
295
296     // selectionLength represents the selection length of this text field to be
297     // removed by this insertion.
298     // If the text field has no focus, we don't need to take account of the
299     // selection length. The selection is the source of text drag-and-drop in
300     // that case, and nothing in the text field will be removed.
301     unsigned selectionLength = focused() ? computeLengthForSubmission(plainText(document().frame()->selection().selection().toNormalizedRange().get())) : 0;
302     ASSERT(currentLength >= selectionLength);
303     unsigned baseLength = currentLength - selectionLength;
304     unsigned appendableLength = unsignedMaxLength > baseLength ? unsignedMaxLength - baseLength : 0;
305     event->setText(sanitizeUserInputValue(event->text(), appendableLength));
306 }
307
308 String HTMLTextAreaElement::sanitizeUserInputValue(const String& proposedValue, unsigned maxLength)
309 {
310     if (maxLength > 0 && U16_IS_LEAD(proposedValue[maxLength - 1]))
311         --maxLength;
312     return proposedValue.left(maxLength);
313 }
314
315 void HTMLTextAreaElement::updateValue() const
316 {
317     if (formControlValueMatchesRenderer())
318         return;
319
320     ASSERT(renderer());
321     m_value = innerTextValue();
322     const_cast<HTMLTextAreaElement*>(this)->setFormControlValueMatchesRenderer(true);
323     const_cast<HTMLTextAreaElement*>(this)->notifyFormStateChanged();
324     m_isDirty = true;
325     const_cast<HTMLTextAreaElement*>(this)->updatePlaceholderVisibility(false);
326 }
327
328 String HTMLTextAreaElement::value() const
329 {
330     updateValue();
331     return m_value;
332 }
333
334 void HTMLTextAreaElement::setValue(const String& value)
335 {
336     setValueCommon(value);
337     m_isDirty = true;
338     setNeedsValidityCheck();
339 }
340
341 void HTMLTextAreaElement::setNonDirtyValue(const String& value)
342 {
343     setValueCommon(value);
344     m_isDirty = false;
345     setNeedsValidityCheck();
346 }
347
348 void HTMLTextAreaElement::setValueCommon(const String& newValue)
349 {
350     // Code elsewhere normalizes line endings added by the user via the keyboard or pasting.
351     // We normalize line endings coming from JavaScript here.
352     String normalizedValue = newValue.isNull() ? "" : newValue;
353     normalizedValue.replace("\r\n", "\n");
354     normalizedValue.replace('\r', '\n');
355
356     // Return early because we don't want to move the caret or trigger other side effects
357     // when the value isn't changing. This matches Firefox behavior, at least.
358     if (normalizedValue == value())
359         return;
360
361     m_value = normalizedValue;
362     setInnerTextValue(m_value);
363     setLastChangeWasNotUserEdit();
364     updatePlaceholderVisibility(false);
365     setNeedsStyleRecalc(SubtreeStyleChange);
366     setFormControlValueMatchesRenderer(true);
367     m_suggestedValue = String();
368
369     // Set the caret to the end of the text value.
370     if (document().focusedElement() == this) {
371         unsigned endOfString = m_value.length();
372         setSelectionRange(endOfString, endOfString);
373     }
374
375     notifyFormStateChanged();
376     setTextAsOfLastFormControlChangeEvent(normalizedValue);
377 }
378
379 String HTMLTextAreaElement::defaultValue() const
380 {
381     StringBuilder value;
382
383     // Since there may be comments, ignore nodes other than text nodes.
384     for (Node* n = firstChild(); n; n = n->nextSibling()) {
385         if (n->isTextNode())
386             value.append(toText(n)->data());
387     }
388
389     return value.toString();
390 }
391
392 void HTMLTextAreaElement::setDefaultValue(const String& defaultValue)
393 {
394     RefPtr<Node> protectFromMutationEvents(this);
395
396     // To preserve comments, remove only the text nodes, then add a single text node.
397     Vector<RefPtr<Node> > textNodes;
398     for (Node* n = firstChild(); n; n = n->nextSibling()) {
399         if (n->isTextNode())
400             textNodes.append(n);
401     }
402     size_t size = textNodes.size();
403     for (size_t i = 0; i < size; ++i)
404         removeChild(textNodes[i].get(), IGNORE_EXCEPTION);
405
406     // Normalize line endings.
407     String value = defaultValue;
408     value.replace("\r\n", "\n");
409     value.replace('\r', '\n');
410
411     insertBefore(document().createTextNode(value), firstChild(), IGNORE_EXCEPTION);
412
413     if (!m_isDirty)
414         setNonDirtyValue(value);
415 }
416
417 int HTMLTextAreaElement::maxLength() const
418 {
419     bool ok;
420     int value = getAttribute(maxlengthAttr).string().toInt(&ok);
421     return ok && value >= 0 ? value : -1;
422 }
423
424 void HTMLTextAreaElement::setMaxLength(int newValue, ExceptionState& exceptionState)
425 {
426     if (newValue < 0)
427         exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0.");
428     else
429         setIntegralAttribute(maxlengthAttr, newValue);
430 }
431
432 String HTMLTextAreaElement::suggestedValue() const
433 {
434     return m_suggestedValue;
435 }
436
437 void HTMLTextAreaElement::setSuggestedValue(const String& value)
438 {
439     m_suggestedValue = value;
440     setInnerTextValue(m_suggestedValue);
441     updatePlaceholderVisibility(false);
442     setNeedsStyleRecalc(SubtreeStyleChange);
443     setFormControlValueMatchesRenderer(true);
444 }
445
446 String HTMLTextAreaElement::validationMessage() const
447 {
448     if (!willValidate())
449         return String();
450
451     if (customError())
452         return customValidationMessage();
453
454     if (valueMissing())
455         return locale().queryString(blink::WebLocalizedString::ValidationValueMissing);
456
457     if (tooLong())
458         return locale().validationMessageTooLongText(computeLengthForSubmission(value()), maxLength());
459
460     return String();
461 }
462
463 bool HTMLTextAreaElement::valueMissing() const
464 {
465     return willValidate() && valueMissing(value());
466 }
467
468 bool HTMLTextAreaElement::tooLong() const
469 {
470     return willValidate() && tooLong(value(), CheckDirtyFlag);
471 }
472
473 bool HTMLTextAreaElement::tooLong(const String& value, NeedsToCheckDirtyFlag check) const
474 {
475     // Return false for the default value or value set by script even if it is
476     // longer than maxLength.
477     if (check == CheckDirtyFlag && !lastChangeWasUserEdit())
478         return false;
479
480     int max = maxLength();
481     if (max < 0)
482         return false;
483     return computeLengthForSubmission(value) > static_cast<unsigned>(max);
484 }
485
486 bool HTMLTextAreaElement::isValidValue(const String& candidate) const
487 {
488     return !valueMissing(candidate) && !tooLong(candidate, IgnoreDirtyFlag);
489 }
490
491 void HTMLTextAreaElement::accessKeyAction(bool)
492 {
493     focus();
494 }
495
496 void HTMLTextAreaElement::setCols(int cols)
497 {
498     setIntegralAttribute(colsAttr, cols);
499 }
500
501 void HTMLTextAreaElement::setRows(int rows)
502 {
503     setIntegralAttribute(rowsAttr, rows);
504 }
505
506 bool HTMLTextAreaElement::shouldUseInputMethod()
507 {
508     return true;
509 }
510
511 bool HTMLTextAreaElement::matchesReadOnlyPseudoClass() const
512 {
513     return isReadOnly();
514 }
515
516 bool HTMLTextAreaElement::matchesReadWritePseudoClass() const
517 {
518     return !isReadOnly();
519 }
520
521 void HTMLTextAreaElement::updatePlaceholderText()
522 {
523     HTMLElement* placeholder = placeholderElement();
524     String placeholderText = strippedPlaceholder();
525     if (placeholderText.isEmpty()) {
526         if (placeholder)
527             userAgentShadowRoot()->removeChild(placeholder);
528         return;
529     }
530     if (!placeholder) {
531         RefPtr<HTMLDivElement> newElement = HTMLDivElement::create(document());
532         placeholder = newElement.get();
533         placeholder->setShadowPseudoId(AtomicString("-webkit-input-placeholder", AtomicString::ConstructFromLiteral));
534         placeholder->setAttribute(idAttr, ShadowElementNames::placeholder());
535         userAgentShadowRoot()->insertBefore(placeholder, innerTextElement()->nextSibling());
536     }
537     placeholder->setTextContent(placeholderText);
538 }
539
540 bool HTMLTextAreaElement::isInteractiveContent() const
541 {
542     return true;
543 }
544
545 bool HTMLTextAreaElement::supportsAutofocus() const
546 {
547     return true;
548 }
549
550 }