Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: third_party/WebKit/Source/core/html/HTMLInputElement.cpp

Issue 2462983002: INPUT element: code cleanup (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
6 * rights reserved. 6 * rights reserved.
7 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) 7 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
8 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org) 8 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
9 * Copyright (C) 2010 Google Inc. All rights reserved. 9 * Copyright (C) 2010 Google Inc. All rights reserved.
10 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. 10 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved.
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 DCHECK(m_parsingInProgress); 382 DCHECK(m_parsingInProgress);
383 DCHECK(!m_inputType); 383 DCHECK(!m_inputType);
384 DCHECK(!m_inputTypeView); 384 DCHECK(!m_inputTypeView);
385 385
386 const AtomicString& newTypeName = 386 const AtomicString& newTypeName =
387 InputType::normalizeTypeName(fastGetAttribute(typeAttr)); 387 InputType::normalizeTypeName(fastGetAttribute(typeAttr));
388 m_inputType = InputType::create(*this, newTypeName); 388 m_inputType = InputType::create(*this, newTypeName);
389 m_inputTypeView = m_inputType->createView(); 389 m_inputTypeView = m_inputType->createView();
390 String defaultValue = fastGetAttribute(valueAttr); 390 String defaultValue = fastGetAttribute(valueAttr);
391 if (m_inputType->valueMode() == ValueMode::kValue) 391 if (m_inputType->valueMode() == ValueMode::kValue)
392 m_valueIfDirty = sanitizeValue(defaultValue); 392 m_nonAttributeValue = sanitizeValue(defaultValue);
393 ensureUserAgentShadowRoot(); 393 ensureUserAgentShadowRoot();
394 394
395 setNeedsWillValidateCheck(); 395 setNeedsWillValidateCheck();
396 396
397 if (!defaultValue.isNull()) 397 if (!defaultValue.isNull())
398 m_inputType->warnIfValueIsInvalid(defaultValue); 398 m_inputType->warnIfValueIsInvalid(defaultValue);
399 399
400 m_inputTypeView->updateView(); 400 m_inputTypeView->updateView();
401 setTextAsOfLastFormControlChangeEvent(value()); 401 setTextAsOfLastFormControlChangeEvent(value());
402 setChangedSinceLastFormControlChangeEvent(false); 402 setChangedSinceLastFormControlChangeEvent(false);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 // 434 //
435 // 1. If the previous state of the element's type attribute put the value IDL 435 // 1. If the previous state of the element's type attribute put the value IDL
436 // attribute in the value mode, and the element's value is not the empty 436 // attribute in the value mode, and the element's value is not the empty
437 // string, and the new state of the element's type attribute puts the value 437 // string, and the new state of the element's type attribute puts the value
438 // IDL attribute in either the default mode or the default/on mode, then set 438 // IDL attribute in either the default mode or the default/on mode, then set
439 // the element's value content attribute to the element's value. 439 // the element's value content attribute to the element's value.
440 if (oldValueMode == ValueMode::kValue && 440 if (oldValueMode == ValueMode::kValue &&
441 (newValueMode == ValueMode::kDefault || 441 (newValueMode == ValueMode::kDefault ||
442 newValueMode == ValueMode::kDefaultOn)) { 442 newValueMode == ValueMode::kDefaultOn)) {
443 if (hasDirtyValue()) 443 if (hasDirtyValue())
444 setAttribute(valueAttr, AtomicString(m_valueIfDirty)); 444 setAttribute(valueAttr, AtomicString(m_nonAttributeValue));
445 m_valueIfDirty = String(); 445 m_nonAttributeValue = String();
446 m_hasDirtyValue = false; 446 m_hasDirtyValue = false;
447 } 447 }
448 // 2. Otherwise, if the previous state of the element's type attribute put the 448 // 2. Otherwise, if the previous state of the element's type attribute put the
449 // value IDL attribute in any mode other than the value mode, and the new 449 // value IDL attribute in any mode other than the value mode, and the new
450 // state of the element's type attribute puts the value IDL attribute in the 450 // state of the element's type attribute puts the value IDL attribute in the
451 // value mode, then set the value of the element to the value of the value 451 // value mode, then set the value of the element to the value of the value
452 // content attribute, if there is one, or the empty string otherwise, and then 452 // content attribute, if there is one, or the empty string otherwise, and then
453 // set the control's dirty value flag to false. 453 // set the control's dirty value flag to false.
454 else if (oldValueMode != ValueMode::kValue && 454 else if (oldValueMode != ValueMode::kValue &&
455 newValueMode == ValueMode::kValue) { 455 newValueMode == ValueMode::kValue) {
456 AtomicString valueString = fastGetAttribute(valueAttr); 456 AtomicString valueString = fastGetAttribute(valueAttr);
457 m_inputType->warnIfValueIsInvalid(valueString); 457 m_inputType->warnIfValueIsInvalid(valueString);
458 m_valueIfDirty = sanitizeValue(valueString); 458 m_nonAttributeValue = sanitizeValue(valueString);
459 m_hasDirtyValue = false; 459 m_hasDirtyValue = false;
460 } 460 }
461 // 3. Otherwise, if the previous state of the element's type attribute put the 461 // 3. Otherwise, if the previous state of the element's type attribute put the
462 // value IDL attribute in any mode other than the filename mode, and the new 462 // value IDL attribute in any mode other than the filename mode, and the new
463 // state of the element's type attribute puts the value IDL attribute in the 463 // state of the element's type attribute puts the value IDL attribute in the
464 // filename mode, then set the value of the element to the empty string. 464 // filename mode, then set the value of the element to the empty string.
465 else if (oldValueMode != ValueMode::kFilename && 465 else if (oldValueMode != ValueMode::kFilename &&
466 newValueMode == ValueMode::kFilename) { 466 newValueMode == ValueMode::kFilename) {
467 m_valueIfDirty = String(); 467 m_nonAttributeValue = String();
468 m_hasDirtyValue = false; 468 m_hasDirtyValue = false;
469 469
470 } else { 470 } else {
471 // ValueMode wasn't changed, or kDefault <-> kDefaultOn. 471 // ValueMode wasn't changed, or kDefault <-> kDefaultOn.
472 if (!hasDirtyValue()) { 472 if (!hasDirtyValue()) {
473 String defaultValue = fastGetAttribute(valueAttr); 473 String defaultValue = fastGetAttribute(valueAttr);
474 if (!defaultValue.isNull()) 474 if (!defaultValue.isNull())
475 m_inputType->warnIfValueIsInvalid(defaultValue); 475 m_inputType->warnIfValueIsInvalid(defaultValue);
476 } 476 }
477 477
478 if (newValueMode == ValueMode::kValue) { 478 if (newValueMode == ValueMode::kValue) {
479 String newValue = sanitizeValue(m_valueIfDirty); 479 String newValue = sanitizeValue(m_nonAttributeValue);
480 if (!equalIgnoringNullity(newValue, m_valueIfDirty)) { 480 if (!equalIgnoringNullity(newValue, m_nonAttributeValue)) {
481 if (hasDirtyValue()) 481 if (hasDirtyValue())
482 setValue(newValue); 482 setValue(newValue);
483 else 483 else
484 setNonDirtyValue(newValue); 484 setNonDirtyValue(newValue);
485 } 485 }
486 } 486 }
487 } 487 }
488 488
489 m_needsToUpdateViewValue = true; 489 m_needsToUpdateViewValue = true;
490 m_inputTypeView->updateView(); 490 m_inputTypeView->updateView();
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 else 721 else
722 m_autocomplete = On; 722 m_autocomplete = On;
723 } 723 }
724 } else if (name == typeAttr) { 724 } else if (name == typeAttr) {
725 updateType(); 725 updateType();
726 } else if (name == valueAttr) { 726 } else if (name == valueAttr) {
727 // We only need to setChanged if the form is looking at the default value 727 // We only need to setChanged if the form is looking at the default value
728 // right now. 728 // right now.
729 if (!hasDirtyValue()) { 729 if (!hasDirtyValue()) {
730 if (m_inputType->valueMode() == ValueMode::kValue) { 730 if (m_inputType->valueMode() == ValueMode::kValue) {
731 m_valueIfDirty = sanitizeValue(value); 731 m_nonAttributeValue = sanitizeValue(value);
732 setTextAsOfLastFormControlChangeEvent(m_valueIfDirty); 732 setTextAsOfLastFormControlChangeEvent(m_nonAttributeValue);
733 } 733 }
734 updatePlaceholderVisibility(); 734 updatePlaceholderVisibility();
735 setNeedsStyleRecalc( 735 setNeedsStyleRecalc(
736 SubtreeStyleChange, 736 SubtreeStyleChange,
737 StyleChangeReasonForTracing::fromAttribute(valueAttr)); 737 StyleChangeReasonForTracing::fromAttribute(valueAttr));
738 } 738 }
739 m_needsToUpdateViewValue = true; 739 m_needsToUpdateViewValue = true;
740 setNeedsValidityCheck(); 740 setNeedsValidityCheck();
741 m_valueAttributeWasUpdatedAfterParsing = !m_parsingInProgress; 741 m_valueAttributeWasUpdatedAfterParsing = !m_parsingInProgress;
742 m_inputType->warnIfValueIsInvalidAndElementIsVisible(value); 742 m_inputType->warnIfValueIsInvalidAndElementIsVisible(value);
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 bool HTMLInputElement::sizeShouldIncludeDecoration(int& preferredSize) const { 989 bool HTMLInputElement::sizeShouldIncludeDecoration(int& preferredSize) const {
990 return m_inputTypeView->sizeShouldIncludeDecoration(defaultSize, 990 return m_inputTypeView->sizeShouldIncludeDecoration(defaultSize,
991 preferredSize); 991 preferredSize);
992 } 992 }
993 993
994 void HTMLInputElement::copyNonAttributePropertiesFromElement( 994 void HTMLInputElement::copyNonAttributePropertiesFromElement(
995 const Element& source) { 995 const Element& source) {
996 const HTMLInputElement& sourceElement = 996 const HTMLInputElement& sourceElement =
997 static_cast<const HTMLInputElement&>(source); 997 static_cast<const HTMLInputElement&>(source);
998 998
999 m_valueIfDirty = sourceElement.m_valueIfDirty; 999 m_nonAttributeValue = sourceElement.m_nonAttributeValue;
1000 m_hasDirtyValue = sourceElement.m_hasDirtyValue; 1000 m_hasDirtyValue = sourceElement.m_hasDirtyValue;
1001 setChecked(sourceElement.m_isChecked); 1001 setChecked(sourceElement.m_isChecked);
1002 m_dirtyCheckedness = sourceElement.m_dirtyCheckedness; 1002 m_dirtyCheckedness = sourceElement.m_dirtyCheckedness;
1003 m_isIndeterminate = sourceElement.m_isIndeterminate; 1003 m_isIndeterminate = sourceElement.m_isIndeterminate;
1004 m_inputType->copyNonAttributeProperties(sourceElement); 1004 m_inputType->copyNonAttributeProperties(sourceElement);
1005 1005
1006 HTMLTextFormControlElement::copyNonAttributePropertiesFromElement(source); 1006 HTMLTextFormControlElement::copyNonAttributePropertiesFromElement(source);
1007 1007
1008 m_needsToUpdateViewValue = true; 1008 m_needsToUpdateViewValue = true;
1009 m_inputTypeView->updateView(); 1009 m_inputTypeView->updateView();
1010 } 1010 }
1011 1011
1012 String HTMLInputElement::value() const { 1012 String HTMLInputElement::value() const {
1013 switch (m_inputType->valueMode()) { 1013 switch (m_inputType->valueMode()) {
1014 case ValueMode::kFilename: { 1014 case ValueMode::kFilename:
1015 String value; 1015 return m_inputType->valueInFilenameValueMode();
1016 // TODO(tkent): The bool return value of getTypeSpecificValue() doesn't
1017 // make sense. FileInputType::getTypeSpecificValue() always returns true.
1018 bool result = m_inputType->getTypeSpecificValue(value);
1019 DCHECK(result);
1020 return value;
1021 }
1022 case ValueMode::kDefault: 1016 case ValueMode::kDefault:
1023 return fastGetAttribute(valueAttr); 1017 return fastGetAttribute(valueAttr);
1024 case ValueMode::kDefaultOn: { 1018 case ValueMode::kDefaultOn: {
1025 AtomicString valueString = fastGetAttribute(valueAttr); 1019 AtomicString valueString = fastGetAttribute(valueAttr);
1026 return valueString.isNull() ? "on" : valueString; 1020 return valueString.isNull() ? "on" : valueString;
1027 } 1021 }
1028 case ValueMode::kValue: 1022 case ValueMode::kValue:
1029 return m_valueIfDirty; 1023 return m_nonAttributeValue;
1030 } 1024 }
1031 NOTREACHED(); 1025 NOTREACHED();
1032 return emptyString(); 1026 return emptyString();
1033 } 1027 }
1034 1028
1035 String HTMLInputElement::valueWithDefault() const { 1029 String HTMLInputElement::valueOrDefaultLabel() const {
1036 String value = this->value(); 1030 String value = this->value();
1037 if (!value.isNull()) 1031 if (!value.isNull())
1038 return value; 1032 return value;
1039 1033 return m_inputType->defaultLabel();
1040 return m_inputType->defaultValue();
1041 } 1034 }
1042 1035
1043 void HTMLInputElement::setValueForUser(const String& value) { 1036 void HTMLInputElement::setValueForUser(const String& value) {
1044 // Call setValue and make it send a change event. 1037 // Call setValue and make it send a change event.
1045 setValue(value, DispatchChangeEvent); 1038 setValue(value, DispatchChangeEvent);
1046 } 1039 }
1047 1040
1048 const String& HTMLInputElement::suggestedValue() const { 1041 const String& HTMLInputElement::suggestedValue() const {
1049 return m_suggestedValue; 1042 return m_suggestedValue;
1050 } 1043 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 1105
1113 if (!valueChanged) 1106 if (!valueChanged)
1114 return; 1107 return;
1115 1108
1116 notifyFormStateChanged(); 1109 notifyFormStateChanged();
1117 } 1110 }
1118 1111
1119 void HTMLInputElement::setNonAttributeValue(const String& sanitizedValue) { 1112 void HTMLInputElement::setNonAttributeValue(const String& sanitizedValue) {
1120 // This is a common code for ValueMode::kValue. 1113 // This is a common code for ValueMode::kValue.
1121 DCHECK_EQ(m_inputType->valueMode(), ValueMode::kValue); 1114 DCHECK_EQ(m_inputType->valueMode(), ValueMode::kValue);
1122 m_valueIfDirty = sanitizedValue; 1115 m_nonAttributeValue = sanitizedValue;
1123 m_hasDirtyValue = true; 1116 m_hasDirtyValue = true;
1124 setNeedsValidityCheck(); 1117 setNeedsValidityCheck();
1125 if (m_inputType->isSteppable()) { 1118 if (m_inputType->isSteppable()) {
1126 pseudoStateChanged(CSSSelector::PseudoInRange); 1119 pseudoStateChanged(CSSSelector::PseudoInRange);
1127 pseudoStateChanged(CSSSelector::PseudoOutOfRange); 1120 pseudoStateChanged(CSSSelector::PseudoOutOfRange);
1128 } 1121 }
1129 if (document().focusedElement() == this) 1122 if (document().focusedElement() == this)
1130 document() 1123 document()
1131 .frameHost() 1124 .frameHost()
1132 ->chromeClient() 1125 ->chromeClient()
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1178 // File upload controls will never use this. 1171 // File upload controls will never use this.
1179 DCHECK_NE(type(), InputTypeNames::file); 1172 DCHECK_NE(type(), InputTypeNames::file);
1180 1173
1181 m_suggestedValue = String(); 1174 m_suggestedValue = String();
1182 1175
1183 // Renderer and our event handler are responsible for sanitizing values. 1176 // Renderer and our event handler are responsible for sanitizing values.
1184 DCHECK(value == m_inputType->sanitizeUserInputValue(value) || 1177 DCHECK(value == m_inputType->sanitizeUserInputValue(value) ||
1185 m_inputType->sanitizeUserInputValue(value).isEmpty()); 1178 m_inputType->sanitizeUserInputValue(value).isEmpty());
1186 1179
1187 DCHECK(!value.isNull()); 1180 DCHECK(!value.isNull());
1188 m_valueIfDirty = value; 1181 m_nonAttributeValue = value;
1189 m_hasDirtyValue = true; 1182 m_hasDirtyValue = true;
1190 m_needsToUpdateViewValue = false; 1183 m_needsToUpdateViewValue = false;
1191 1184
1192 // Input event is fired by the Node::defaultEventHandler for editable 1185 // Input event is fired by the Node::defaultEventHandler for editable
1193 // controls. 1186 // controls.
1194 if (!isTextField()) 1187 if (!isTextField())
1195 dispatchInputEvent(); 1188 dispatchInputEvent();
1196 notifyFormStateChanged(); 1189 notifyFormStateChanged();
1197 1190
1198 setNeedsValidityCheck(); 1191 setNeedsValidityCheck();
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after
1958 1951
1959 bool HTMLInputElement::hasFallbackContent() const { 1952 bool HTMLInputElement::hasFallbackContent() const {
1960 return m_inputTypeView->hasFallbackContent(); 1953 return m_inputTypeView->hasFallbackContent();
1961 } 1954 }
1962 1955
1963 void HTMLInputElement::setFilesFromPaths(const Vector<String>& paths) { 1956 void HTMLInputElement::setFilesFromPaths(const Vector<String>& paths) {
1964 return m_inputType->setFilesFromPaths(paths); 1957 return m_inputType->setFilesFromPaths(paths);
1965 } 1958 }
1966 1959
1967 } // namespace blink 1960 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698