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

Side by Side Diff: Source/core/html/HTMLTextAreaElement.cpp

Issue 435753003: Implement minlength for <input> and <textarea>. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 4 months 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, 2010 Apple Inc. All rights reserv ed. 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserv ed.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org) 7 * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 wrap = NoWrap; 183 wrap = NoWrap;
184 else 184 else
185 wrap = SoftWrap; 185 wrap = SoftWrap;
186 if (wrap != m_wrap) { 186 if (wrap != m_wrap) {
187 m_wrap = wrap; 187 m_wrap = wrap;
188 if (renderer()) 188 if (renderer())
189 renderer()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalid ation(); 189 renderer()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalid ation();
190 } 190 }
191 } else if (name == accesskeyAttr) { 191 } else if (name == accesskeyAttr) {
192 // ignore for the moment 192 // ignore for the moment
193 } else if (name == maxlengthAttr) 193 } else if (name == maxlengthAttr) {
194 setNeedsValidityCheck(); 194 setNeedsValidityCheck();
195 else 195 } else if (name == minlengthAttr) {
196 setNeedsValidityCheck();
197 } else
196 HTMLTextFormControlElement::parseAttribute(name, value); 198 HTMLTextFormControlElement::parseAttribute(name, value);
197 } 199 }
198 200
199 RenderObject* HTMLTextAreaElement::createRenderer(RenderStyle*) 201 RenderObject* HTMLTextAreaElement::createRenderer(RenderStyle*)
200 { 202 {
201 return new RenderTextControlMultiLine(this); 203 return new RenderTextControlMultiLine(this);
202 } 204 }
203 205
204 bool HTMLTextAreaElement::appendFormData(FormDataList& encoding, bool) 206 bool HTMLTextAreaElement::appendFormData(FormDataList& encoding, bool)
205 { 207 {
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 setNonDirtyValue(value); 442 setNonDirtyValue(value);
441 } 443 }
442 444
443 int HTMLTextAreaElement::maxLength() const 445 int HTMLTextAreaElement::maxLength() const
444 { 446 {
445 bool ok; 447 bool ok;
446 int value = getAttribute(maxlengthAttr).string().toInt(&ok); 448 int value = getAttribute(maxlengthAttr).string().toInt(&ok);
447 return ok && value >= 0 ? value : -1; 449 return ok && value >= 0 ? value : -1;
448 } 450 }
449 451
452 int HTMLTextAreaElement::minLength() const
453 {
454 bool ok;
455 int value = getAttribute(minlengthAttr).string().toInt(&ok);
456 if (ok && value >= 0) {
457 int max = maxLength();
keishi 2014/08/01 03:14:54 We don't need to clamp to maxLength
Bartek Nowierski 2014/10/03 12:52:00 Done.
458 if (max >= 0 && value > max) {
459 value = max;
460 }
461 return value;
462 }
463 return -1;
464 }
465
450 void HTMLTextAreaElement::setMaxLength(int newValue, ExceptionState& exceptionSt ate) 466 void HTMLTextAreaElement::setMaxLength(int newValue, ExceptionState& exceptionSt ate)
451 { 467 {
468 int min = minLength();
452 if (newValue < 0) 469 if (newValue < 0)
453 exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0."); 470 exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0.");
471 else if (min >= 0 && newValue < min)
472 exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is less than minlength" + String::number(min) + ".");
454 else 473 else
455 setIntegralAttribute(maxlengthAttr, newValue); 474 setIntegralAttribute(maxlengthAttr, newValue);
456 } 475 }
457 476
477 void HTMLTextAreaElement::setMinLength(int newValue, ExceptionState& exceptionSt ate)
478 {
479 int max = maxLength();
480 if (newValue < 0)
481 exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is not positive or 0.");
482 else if (max >= 0 && newValue > max)
483 exceptionState.throwDOMException(IndexSizeError, "The value provided (" + String::number(newValue) + ") is more than maxlength" + String::number(max) + ".");
484 else
485 setIntegralAttribute(minlengthAttr, newValue);
486 }
487
458 String HTMLTextAreaElement::suggestedValue() const 488 String HTMLTextAreaElement::suggestedValue() const
459 { 489 {
460 return m_suggestedValue; 490 return m_suggestedValue;
461 } 491 }
462 492
463 void HTMLTextAreaElement::setSuggestedValue(const String& value) 493 void HTMLTextAreaElement::setSuggestedValue(const String& value)
464 { 494 {
465 m_suggestedValue = value; 495 m_suggestedValue = value;
466 496
467 if (!value.isNull()) 497 if (!value.isNull())
(...skipping 11 matching lines...) Expand all
479 509
480 if (customError()) 510 if (customError())
481 return customValidationMessage(); 511 return customValidationMessage();
482 512
483 if (valueMissing()) 513 if (valueMissing())
484 return locale().queryString(blink::WebLocalizedString::ValidationValueMi ssing); 514 return locale().queryString(blink::WebLocalizedString::ValidationValueMi ssing);
485 515
486 if (tooLong()) 516 if (tooLong())
487 return locale().validationMessageTooLongText(computeLengthForSubmission( value()), maxLength()); 517 return locale().validationMessageTooLongText(computeLengthForSubmission( value()), maxLength());
488 518
519 // TODO(bartekn): Add a message for tooShort().
keishi 2014/08/01 03:14:54 I think you need to land the blink side change fir
Bartek Nowierski 2014/10/03 12:52:00 I'll do it in the next patchset. Let's focus on ge
520
489 return String(); 521 return String();
490 } 522 }
491 523
492 bool HTMLTextAreaElement::valueMissing() const 524 bool HTMLTextAreaElement::valueMissing() const
493 { 525 {
494 return willValidate() && valueMissing(value()); 526 return willValidate() && valueMissing(value());
495 } 527 }
496 528
497 bool HTMLTextAreaElement::tooLong() const 529 bool HTMLTextAreaElement::tooLong() const
498 { 530 {
499 return willValidate() && tooLong(value(), CheckDirtyFlag); 531 return willValidate() && tooLong(value(), CheckDirtyFlag);
500 } 532 }
501 533
534 bool HTMLTextAreaElement::tooShort() const
535 {
536 return willValidate() && tooShort(value(), CheckDirtyFlag);
537 }
538
502 bool HTMLTextAreaElement::tooLong(const String& value, NeedsToCheckDirtyFlag che ck) const 539 bool HTMLTextAreaElement::tooLong(const String& value, NeedsToCheckDirtyFlag che ck) const
503 { 540 {
504 // Return false for the default value or value set by script even if it is 541 // Return false for the default value or value set by script even if it is
505 // longer than maxLength. 542 // longer than maxLength.
506 if (check == CheckDirtyFlag && !lastChangeWasUserEdit()) 543 if (check == CheckDirtyFlag && !lastChangeWasUserEdit())
507 return false; 544 return false;
508 545
509 int max = maxLength(); 546 int max = maxLength();
510 if (max < 0) 547 if (max < 0)
511 return false; 548 return false;
512 return computeLengthForSubmission(value) > static_cast<unsigned>(max); 549 return computeLengthForSubmission(value) > static_cast<unsigned>(max);
513 } 550 }
514 551
552 bool HTMLTextAreaElement::tooShort(const String& value, NeedsToCheckDirtyFlag ch eck) const
553 {
554 // Return false for the default value or value set by script even if it is
555 // shorter than minLength.
556 if (check == CheckDirtyFlag && !lastChangeWasUserEdit())
557 return false;
558
559 int min = minLength();
560 if (min <= 0)
561 return false;
562 // An empty string is excluded from minlength check.
563 unsigned len = computeLengthForSubmission(value);
564 return len > 0 && len < static_cast<unsigned>(min);
565 }
566
515 bool HTMLTextAreaElement::isValidValue(const String& candidate) const 567 bool HTMLTextAreaElement::isValidValue(const String& candidate) const
516 { 568 {
517 return !valueMissing(candidate) && !tooLong(candidate, IgnoreDirtyFlag); 569 return !valueMissing(candidate) && !tooLong(candidate, IgnoreDirtyFlag);
518 } 570 }
519 571
520 void HTMLTextAreaElement::accessKeyAction(bool) 572 void HTMLTextAreaElement::accessKeyAction(bool)
521 { 573 {
522 focus(); 574 focus();
523 } 575 }
524 576
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 { 617 {
566 return true; 618 return true;
567 } 619 }
568 620
569 bool HTMLTextAreaElement::supportsAutofocus() const 621 bool HTMLTextAreaElement::supportsAutofocus() const
570 { 622 {
571 return true; 623 return true;
572 } 624 }
573 625
574 } 626 }
OLDNEW
« Source/core/html/HTMLInputElement.cpp ('K') | « Source/core/html/HTMLTextAreaElement.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698