|
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 "Chrome.h" |
|
33 #include "ChromeClient.h" |
|
34 #include "Document.h" |
|
35 #include "Event.h" |
|
36 #include "EventNames.h" |
|
37 #include "ExceptionCode.h" |
|
38 #include "FocusController.h" |
|
39 #include "FormDataList.h" |
|
40 #include "Frame.h" |
|
41 #include "HTMLNames.h" |
|
42 #include "InputElement.h" |
|
43 #include "Page.h" |
|
44 #include "RenderStyle.h" |
|
45 #include "RenderTextControlMultiLine.h" |
|
46 #include "ScriptEventListener.h" |
|
47 #include "Text.h" |
|
48 #include "TextIterator.h" |
|
49 #include "VisibleSelection.h" |
|
50 #include <wtf/StdLibExtras.h> |
|
51 |
|
52 namespace WebCore { |
|
53 |
|
54 using namespace HTMLNames; |
|
55 |
|
56 static const int defaultRows = 2; |
|
57 static const int defaultCols = 20; |
|
58 |
|
59 static inline void notifyFormStateChanged(const HTMLTextAreaElement* element) |
|
60 { |
|
61 Frame* frame = element->document()->frame(); |
|
62 if (!frame) |
|
63 return; |
|
64 frame->page()->chrome()->client()->formStateDidChange(element); |
|
65 } |
|
66 |
|
67 HTMLTextAreaElement::HTMLTextAreaElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form) |
|
68 : HTMLTextFormControlElement(tagName, document, form) |
|
69 , m_rows(defaultRows) |
|
70 , m_cols(defaultCols) |
|
71 , m_wrap(SoftWrap) |
|
72 , m_cachedSelectionStart(-1) |
|
73 , m_cachedSelectionEnd(-1) |
|
74 , m_isDirty(false) |
|
75 { |
|
76 ASSERT(hasTagName(textareaTag)); |
|
77 setFormControlValueMatchesRenderer(true); |
|
78 } |
|
79 |
|
80 PassRefPtr<HTMLTextAreaElement> HTMLTextAreaElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form) |
|
81 { |
|
82 return adoptRef(new HTMLTextAreaElement(tagName, document, form)); |
|
83 } |
|
84 |
|
85 const AtomicString& HTMLTextAreaElement::formControlType() const |
|
86 { |
|
87 DEFINE_STATIC_LOCAL(const AtomicString, textarea, ("textarea")); |
|
88 return textarea; |
|
89 } |
|
90 |
|
91 bool HTMLTextAreaElement::saveFormControlState(String& result) const |
|
92 { |
|
93 String currentValue = value(); |
|
94 if (currentValue == defaultValue()) |
|
95 return false; |
|
96 result = currentValue; |
|
97 return true; |
|
98 } |
|
99 |
|
100 void HTMLTextAreaElement::restoreFormControlState(const String& state) |
|
101 { |
|
102 setValue(state); |
|
103 } |
|
104 |
|
105 void HTMLTextAreaElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) |
|
106 { |
|
107 setNonDirtyValue(defaultValue()); |
|
108 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); |
|
109 } |
|
110 |
|
111 void HTMLTextAreaElement::parseMappedAttribute(Attribute* attr) |
|
112 { |
|
113 if (attr->name() == rowsAttr) { |
|
114 int rows = attr->value().toInt(); |
|
115 if (rows <= 0) |
|
116 rows = defaultRows; |
|
117 if (m_rows != rows) { |
|
118 m_rows = rows; |
|
119 if (renderer()) |
|
120 renderer()->setNeedsLayoutAndPrefWidthsRecalc(); |
|
121 } |
|
122 } else if (attr->name() == colsAttr) { |
|
123 int cols = attr->value().toInt(); |
|
124 if (cols <= 0) |
|
125 cols = defaultCols; |
|
126 if (m_cols != cols) { |
|
127 m_cols = cols; |
|
128 if (renderer()) |
|
129 renderer()->setNeedsLayoutAndPrefWidthsRecalc(); |
|
130 } |
|
131 } else if (attr->name() == wrapAttr) { |
|
132 // The virtual/physical values were a Netscape extension of HTML 3.0, now deprecated. |
|
133 // The soft/hard /off values are a recommendation for HTML 4 extension by IE and NS 4. |
|
134 WrapMethod wrap; |
|
135 if (equalIgnoringCase(attr->value(), "physical") || equalIgnoringCase(attr->value(), "hard") || equalIgnoringCase(attr->value(), "on")) |
|
136 wrap = HardWrap; |
|
137 else if (equalIgnoringCase(attr->value(), "off")) |
|
138 wrap = NoWrap; |
|
139 else |
|
140 wrap = SoftWrap; |
|
141 if (wrap != m_wrap) { |
|
142 m_wrap = wrap; |
|
143 |
|
144 if (shouldWrapText()) { |
|
145 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValuePreWrap); |
|
146 addCSSProperty(attr, CSSPropertyWordWrap, CSSValueBreakWord); |
|
147 } else { |
|
148 addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValuePre); |
|
149 addCSSProperty(attr, CSSPropertyWordWrap, CSSValueNormal); |
|
150 } |
|
151 |
|
152 if (renderer()) |
|
153 renderer()->setNeedsLayoutAndPrefWidthsRecalc(); |
|
154 } |
|
155 } else if (attr->name() == accesskeyAttr) { |
|
156 // ignore for the moment |
|
157 } else if (attr->name() == alignAttr) { |
|
158 // Don't map 'align' attribute. This matches what Firefox, Opera and IE do. |
|
159 // See http://bugs.webkit.org/show_bug.cgi?id=7075 |
|
160 } else if (attr->name() == maxlengthAttr) |
|
161 setNeedsValidityCheck(); |
|
162 else |
|
163 HTMLTextFormControlElement::parseMappedAttribute(attr); |
|
164 } |
|
165 |
|
166 RenderObject* HTMLTextAreaElement::createRenderer(RenderArena* arena, RenderStyle*) |
|
167 { |
|
168 return new (arena) RenderTextControlMultiLine(this, placeholderShouldBeVisible()); |
|
169 } |
|
170 |
|
171 bool HTMLTextAreaElement::appendFormData(FormDataList& encoding, bool) |
|
172 { |
|
173 if (name().isEmpty()) |
|
174 return false; |
|
175 |
|
176 document()->updateLayout(); |
|
177 |
|
178 // FIXME: It's not acceptable to ignore the HardWrap setting when there is no renderer. |
|
179 // While we have no evidence this has ever been a practical problem, it would be best to fix it some day. |
|
180 RenderTextControl* control = toRenderTextControl(renderer()); |
|
181 const String& text = (m_wrap == HardWrap && control) ? control->textWithHardLineBreaks() : value(); |
|
182 encoding.appendData(name(), text); |
|
183 return true; |
|
184 } |
|
185 |
|
186 void HTMLTextAreaElement::reset() |
|
187 { |
|
188 setNonDirtyValue(defaultValue()); |
|
189 } |
|
190 |
|
191 bool HTMLTextAreaElement::isKeyboardFocusable(KeyboardEvent*) const |
|
192 { |
|
193 // If a given text area can be focused at all, then it will always be keyboard focusable. |
|
194 return isFocusable(); |
|
195 } |
|
196 |
|
197 bool HTMLTextAreaElement::isMouseFocusable() const |
|
198 { |
|
199 return isFocusable(); |
|
200 } |
|
201 |
|
202 void HTMLTextAreaElement::updateFocusAppearance(bool restorePreviousSelection) |
|
203 { |
|
204 ASSERT(renderer()); |
|
205 ASSERT(!document()->childNeedsAndNotInStyleRecalc()); |
|
206 |
|
207 if (!restorePreviousSelection || m_cachedSelectionStart < 0) { |
|
208 #if ENABLE(ON_FIRST_TEXTAREA_FOCUS_SELECT_ALL) |
|
209 // Devices with trackballs or d-pads may focus on a textarea in route |
|
210 // to another focusable node. By selecting all text, the next movement |
|
211 // can more readily be interpreted as moving to the next node. |
|
212 select(); |
|
213 #else |
|
214 // If this is the first focus, set a caret at the beginning of the text. |
|
215 // This matches some browsers' behavior; see bug 11746 Comment #15. |
|
216 // http://bugs.webkit.org/show_bug.cgi?id=11746#c15 |
|
217 setSelectionRange(0, 0); |
|
218 #endif |
|
219 } else { |
|
220 // Restore the cached selection. This matches other browsers' behavior. |
|
221 setSelectionRange(m_cachedSelectionStart, m_cachedSelectionEnd); |
|
222 } |
|
223 |
|
224 if (document()->frame()) |
|
225 document()->frame()->revealSelection(); |
|
226 } |
|
227 |
|
228 void HTMLTextAreaElement::defaultEventHandler(Event* event) |
|
229 { |
|
230 if (renderer() && (event->isMouseEvent() || event->isDragEvent() || event->isWheelEvent() || event->type() == eventNames().blurEvent)) |
|
231 toRenderTextControlMultiLine(renderer())->forwardEvent(event); |
|
232 else if (renderer() && event->isBeforeTextInsertedEvent()) |
|
233 handleBeforeTextInsertedEvent(static_cast<BeforeTextInsertedEvent*>(event)); |
|
234 |
|
235 HTMLFormControlElementWithState::defaultEventHandler(event); |
|
236 } |
|
237 |
|
238 void HTMLTextAreaElement::handleBeforeTextInsertedEvent(BeforeTextInsertedEvent* event) const |
|
239 { |
|
240 ASSERT(event); |
|
241 ASSERT(renderer()); |
|
242 int signedMaxLength = maxLength(); |
|
243 if (signedMaxLength < 0) |
|
244 return; |
|
245 unsigned unsignedMaxLength = static_cast<unsigned>(signedMaxLength); |
|
246 |
|
247 unsigned currentLength = numGraphemeClusters(toRenderTextControl(renderer())->text()); |
|
248 // selectionLength represents the selection length of this text field to be |
|
249 // removed by this insertion. |
|
250 // If the text field has no focus, we don't need to take account of the |
|
251 // selection length. The selection is the source of text drag-and-drop in |
|
252 // that case, and nothing in the text field will be removed. |
|
253 unsigned selectionLength = focused() ? numGraphemeClusters(plainText(document()->frame()->selection()->selection().toNormalizedRange().get())) : 0; |
|
254 ASSERT(currentLength >= selectionLength); |
|
255 unsigned baseLength = currentLength - selectionLength; |
|
256 unsigned appendableLength = unsignedMaxLength > baseLength ? unsignedMaxLength - baseLength : 0; |
|
257 event->setText(sanitizeUserInputValue(event->text(), appendableLength)); |
|
258 } |
|
259 |
|
260 String HTMLTextAreaElement::sanitizeUserInputValue(const String& proposedValue, unsigned maxLength) |
|
261 { |
|
262 return proposedValue.left(numCharactersInGraphemeClusters(proposedValue, maxLength)); |
|
263 } |
|
264 |
|
265 void HTMLTextAreaElement::rendererWillBeDestroyed() |
|
266 { |
|
267 updateValue(); |
|
268 } |
|
269 |
|
270 void HTMLTextAreaElement::updateValue() const |
|
271 { |
|
272 if (formControlValueMatchesRenderer()) |
|
273 return; |
|
274 |
|
275 ASSERT(renderer()); |
|
276 m_value = toRenderTextControl(renderer())->text(); |
|
277 const_cast<HTMLTextAreaElement*>(this)->setFormControlValueMatchesRenderer(true); |
|
278 notifyFormStateChanged(this); |
|
279 m_isDirty = true; |
|
280 } |
|
281 |
|
282 String HTMLTextAreaElement::value() const |
|
283 { |
|
284 updateValue(); |
|
285 return m_value; |
|
286 } |
|
287 |
|
288 void HTMLTextAreaElement::setValue(const String& value) |
|
289 { |
|
290 setNonDirtyValue(value); |
|
291 m_isDirty = true; |
|
292 } |
|
293 |
|
294 void HTMLTextAreaElement::setNonDirtyValue(const String& value) |
|
295 { |
|
296 // Code elsewhere normalizes line endings added by the user via the keyboard or pasting. |
|
297 // We normalize line endings coming from JavaScript here. |
|
298 String normalizedValue = value.isNull() ? "" : value; |
|
299 normalizedValue.replace("\r\n", "\n"); |
|
300 normalizedValue.replace('\r', '\n'); |
|
301 |
|
302 // Return early because we don't want to move the caret or trigger other side effects |
|
303 // when the value isn't changing. This matches Firefox behavior, at least. |
|
304 if (normalizedValue == this->value()) |
|
305 return; |
|
306 |
|
307 m_value = normalizedValue; |
|
308 updatePlaceholderVisibility(false); |
|
309 setNeedsStyleRecalc(); |
|
310 setNeedsValidityCheck(); |
|
311 m_isDirty = false; |
|
312 setFormControlValueMatchesRenderer(true); |
|
313 |
|
314 // Set the caret to the end of the text value. |
|
315 if (document()->focusedNode() == this) { |
|
316 unsigned endOfString = m_value.length(); |
|
317 setSelectionRange(endOfString, endOfString); |
|
318 } |
|
319 |
|
320 notifyFormStateChanged(this); |
|
321 } |
|
322 |
|
323 String HTMLTextAreaElement::defaultValue() const |
|
324 { |
|
325 String value = ""; |
|
326 |
|
327 // Since there may be comments, ignore nodes other than text nodes. |
|
328 for (Node* n = firstChild(); n; n = n->nextSibling()) { |
|
329 if (n->isTextNode()) |
|
330 value += static_cast<Text*>(n)->data(); |
|
331 } |
|
332 |
|
333 UChar firstCharacter = value[0]; |
|
334 if (firstCharacter == '\r' && value[1] == '\n') |
|
335 value.remove(0, 2); |
|
336 else if (firstCharacter == '\r' || firstCharacter == '\n') |
|
337 value.remove(0, 1); |
|
338 |
|
339 return value; |
|
340 } |
|
341 |
|
342 void HTMLTextAreaElement::setDefaultValue(const String& defaultValue) |
|
343 { |
|
344 // To preserve comments, remove only the text nodes, then add a single text node. |
|
345 |
|
346 Vector<RefPtr<Node> > textNodes; |
|
347 for (Node* n = firstChild(); n; n = n->nextSibling()) { |
|
348 if (n->isTextNode()) |
|
349 textNodes.append(n); |
|
350 } |
|
351 ExceptionCode ec; |
|
352 size_t size = textNodes.size(); |
|
353 for (size_t i = 0; i < size; ++i) |
|
354 removeChild(textNodes[i].get(), ec); |
|
355 |
|
356 // Normalize line endings. |
|
357 // Add an extra line break if the string starts with one, since |
|
358 // the code to read default values from the DOM strips the leading one. |
|
359 String value = defaultValue; |
|
360 value.replace("\r\n", "\n"); |
|
361 value.replace('\r', '\n'); |
|
362 if (value[0] == '\n') |
|
363 value = "\n" + value; |
|
364 |
|
365 insertBefore(document()->createTextNode(value), firstChild(), ec); |
|
366 |
|
367 setNonDirtyValue(value); |
|
368 } |
|
369 |
|
370 int HTMLTextAreaElement::maxLength() const |
|
371 { |
|
372 bool ok; |
|
373 int value = getAttribute(maxlengthAttr).string().toInt(&ok); |
|
374 return ok && value >= 0 ? value : -1; |
|
375 } |
|
376 |
|
377 void HTMLTextAreaElement::setMaxLength(int newValue, ExceptionCode& ec) |
|
378 { |
|
379 if (newValue < 0) |
|
380 ec = INDEX_SIZE_ERR; |
|
381 else |
|
382 setAttribute(maxlengthAttr, String::number(newValue)); |
|
383 } |
|
384 |
|
385 bool HTMLTextAreaElement::tooLong() const |
|
386 { |
|
387 // Return false for the default value even if it is longer than maxLength. |
|
388 if (!m_isDirty) |
|
389 return false; |
|
390 |
|
391 int max = maxLength(); |
|
392 if (max < 0) |
|
393 return false; |
|
394 return numGraphemeClusters(value()) > static_cast<unsigned>(max); |
|
395 } |
|
396 |
|
397 void HTMLTextAreaElement::accessKeyAction(bool) |
|
398 { |
|
399 focus(); |
|
400 } |
|
401 |
|
402 void HTMLTextAreaElement::setCols(int cols) |
|
403 { |
|
404 setAttribute(colsAttr, String::number(cols)); |
|
405 } |
|
406 |
|
407 void HTMLTextAreaElement::setRows(int rows) |
|
408 { |
|
409 setAttribute(rowsAttr, String::number(rows)); |
|
410 } |
|
411 |
|
412 bool HTMLTextAreaElement::shouldUseInputMethod() const |
|
413 { |
|
414 return true; |
|
415 } |
|
416 |
|
417 } // namespace |