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

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

Issue 616443002: Implement :valid and :invalid pseudoclass for <form> (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Add layout tests, apply feedback from Keishi, add support for removing/inserting elements into the … Created 6 years, 2 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 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com) 6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 if (shouldAutofocusOnAttach(this)) 239 if (shouldAutofocusOnAttach(this))
240 document().setAutofocusElement(this); 240 document().setAutofocusElement(this);
241 } 241 }
242 242
243 void HTMLFormControlElement::didMoveToNewDocument(Document& oldDocument) 243 void HTMLFormControlElement::didMoveToNewDocument(Document& oldDocument)
244 { 244 {
245 FormAssociatedElement::didMoveToNewDocument(oldDocument); 245 FormAssociatedElement::didMoveToNewDocument(oldDocument);
246 HTMLElement::didMoveToNewDocument(oldDocument); 246 HTMLElement::didMoveToNewDocument(oldDocument);
247 } 247 }
248 248
249 void HTMLFormControlElement::setFormOwnerNeedsStyleRecalc()
keishi 2014/10/03 03:44:13 I think this should be done by the HTMLFormElement
Bartek Nowierski 2014/10/03 06:01:33 Done.
250 {
251 HTMLFormElement* form = formOwner();
252 if (form)
253 form->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracin g::createWithExtraData(StyleChangeReason::PseudoClass, StyleChangeExtraData::Inv alid));
254 }
255
249 Node::InsertionNotificationRequest HTMLFormControlElement::insertedInto(Containe rNode* insertionPoint) 256 Node::InsertionNotificationRequest HTMLFormControlElement::insertedInto(Containe rNode* insertionPoint)
250 { 257 {
251 m_ancestorDisabledState = AncestorDisabledStateUnknown; 258 m_ancestorDisabledState = AncestorDisabledStateUnknown;
252 m_dataListAncestorState = Unknown; 259 m_dataListAncestorState = Unknown;
253 setNeedsWillValidateCheck(); 260 setNeedsWillValidateCheck();
254 HTMLElement::insertedInto(insertionPoint); 261 HTMLElement::insertedInto(insertionPoint);
255 FormAssociatedElement::insertedInto(insertionPoint); 262 FormAssociatedElement::insertedInto(insertionPoint);
263
264 // Element addition can change validity of the form.
keishi 2014/10/03 03:44:13 I think you'll find that Blink is light on comment
Bartek Nowierski 2014/10/03 06:01:33 Done.
265 // Order form style recalculation (do it at the end to ensure that form
266 // owner is set).
267 setFormOwnerNeedsStyleRecalc();
268
256 return InsertionDone; 269 return InsertionDone;
257 } 270 }
258 271
259 void HTMLFormControlElement::removedFrom(ContainerNode* insertionPoint) 272 void HTMLFormControlElement::removedFrom(ContainerNode* insertionPoint)
260 { 273 {
274 // Element removal can definitely change validity of the form.
275 // Order form style recalculation (do it at the begining to ensure that
276 // form owner is still set).
277 setFormOwnerNeedsStyleRecalc();
278
261 hideVisibleValidationMessage(); 279 hideVisibleValidationMessage();
262 m_hasValidationMessage = false; 280 m_hasValidationMessage = false;
263 m_ancestorDisabledState = AncestorDisabledStateUnknown; 281 m_ancestorDisabledState = AncestorDisabledStateUnknown;
264 m_dataListAncestorState = Unknown; 282 m_dataListAncestorState = Unknown;
265 HTMLElement::removedFrom(insertionPoint); 283 HTMLElement::removedFrom(insertionPoint);
266 FormAssociatedElement::removedFrom(insertionPoint); 284 FormAssociatedElement::removedFrom(insertionPoint);
267 } 285 }
268 286
269 void HTMLFormControlElement::setChangedSinceLastFormControlChangeEvent(bool chan ged) 287 void HTMLFormControlElement::setChangedSinceLastFormControlChangeEvent(bool chan ged)
270 { 288 {
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 { 480 {
463 Page* page = document().page(); 481 Page* page = document().page();
464 if (!page) 482 if (!page)
465 return 0; 483 return 0;
466 484
467 return &page->validationMessageClient(); 485 return &page->validationMessageClient();
468 } 486 }
469 487
470 bool HTMLFormControlElement::checkValidity(WillBeHeapVector<RefPtrWillBeMember<F ormAssociatedElement> >* unhandledInvalidControls) 488 bool HTMLFormControlElement::checkValidity(WillBeHeapVector<RefPtrWillBeMember<F ormAssociatedElement> >* unhandledInvalidControls)
471 { 489 {
472 if (!willValidate() || isValidFormControlElement()) 490 if (!willValidate() || isValidElement())
473 return true; 491 return true;
474 // An event handler can deref this object. 492 // An event handler can deref this object.
475 RefPtrWillBeRawPtr<HTMLFormControlElement> protector(this); 493 RefPtrWillBeRawPtr<HTMLFormControlElement> protector(this);
476 RefPtrWillBeRawPtr<Document> originalDocument(document()); 494 RefPtrWillBeRawPtr<Document> originalDocument(document());
477 bool needsDefaultAction = dispatchEvent(Event::createCancelable(EventTypeNam es::invalid)); 495 bool needsDefaultAction = dispatchEvent(Event::createCancelable(EventTypeNam es::invalid));
478 if (needsDefaultAction && unhandledInvalidControls && inDocument() && origin alDocument == document()) 496 if (needsDefaultAction && unhandledInvalidControls && inDocument() && origin alDocument == document())
479 unhandledInvalidControls->append(this); 497 unhandledInvalidControls->append(this);
480 return false; 498 return false;
481 } 499 }
482 500
483 bool HTMLFormControlElement::isValidFormControlElement() 501 bool HTMLFormControlElement::isValidElement()
484 { 502 {
485 // If the following assertion fails, setNeedsValidityCheck() is not called 503 // If the following assertion fails, setNeedsValidityCheck() is not called
486 // correctly when something which changes validity is updated. 504 // correctly when something which changes validity is updated.
487 ASSERT(m_isValid == valid()); 505 ASSERT(m_isValid == valid());
488 return m_isValid; 506 return m_isValid;
489 } 507 }
490 508
491 void HTMLFormControlElement::setNeedsValidityCheck() 509 void HTMLFormControlElement::setNeedsValidityCheck()
492 { 510 {
493 bool newIsValid = valid(); 511 bool newIsValid = valid();
494 if (willValidate() && newIsValid != m_isValid) { 512 if (willValidate() && newIsValid != m_isValid) {
495 // Update style for pseudo classes such as :valid :invalid. 513 // Update style for pseudo classes such as :valid :invalid.
514 // A change here could've affected the style of the form itself and
515 // other form elemetns, so recaluclate the entire form's subtree.
516 setFormOwnerNeedsStyleRecalc();
517 // Then recalculate the element itself (and its subtree), because
518 // it might not be in the tree of the owner form.
496 setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::cre ateWithExtraData(StyleChangeReason::PseudoClass, StyleChangeExtraData::Invalid)) ; 519 setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::cre ateWithExtraData(StyleChangeReason::PseudoClass, StyleChangeExtraData::Invalid)) ;
497 } 520 }
498 m_isValid = newIsValid; 521 m_isValid = newIsValid;
499 522
500 // Updates only if this control already has a validation message. 523 // Updates only if this control already has a validation message.
501 if (isValidationMessageVisible()) { 524 if (isValidationMessageVisible()) {
502 // Calls updateVisibleValidationMessage() even if m_isValid is not 525 // Calls updateVisibleValidationMessage() even if m_isValid is not
503 // changed because a validation message can be changed. 526 // changed because a validation message can be changed.
504 updateVisibleValidationMessage(); 527 updateVisibleValidationMessage();
505 } 528 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 570
548 void HTMLFormControlElement::setFocus(bool flag) 571 void HTMLFormControlElement::setFocus(bool flag)
549 { 572 {
550 LabelableElement::setFocus(flag); 573 LabelableElement::setFocus(flag);
551 574
552 if (!flag && wasChangedSinceLastFormControlChangeEvent()) 575 if (!flag && wasChangedSinceLastFormControlChangeEvent())
553 dispatchFormControlChangeEvent(); 576 dispatchFormControlChangeEvent();
554 } 577 }
555 578
556 } // namespace blink 579 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698