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