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

Unified Diff: third_party/WebKit/Source/core/html/forms/RadioButtonGroupScope.cpp

Issue 1632493002: Precisely account for required buttons in a radio group. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix oilpan compilation Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/fast/forms/radio/radio-group-remove-required-expected.txt ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/html/forms/RadioButtonGroupScope.cpp
diff --git a/third_party/WebKit/Source/core/html/forms/RadioButtonGroupScope.cpp b/third_party/WebKit/Source/core/html/forms/RadioButtonGroupScope.cpp
index de3c04c523c20dee85f25a5e4359ca70ca2b6f3e..a2e773acd8b1a2f3a44108223396f51129e9cddf 100644
--- a/third_party/WebKit/Source/core/html/forms/RadioButtonGroupScope.cpp
+++ b/third_party/WebKit/Source/core/html/forms/RadioButtonGroupScope.cpp
@@ -22,7 +22,7 @@
#include "core/InputTypeNames.h"
#include "core/html/HTMLInputElement.h"
-#include "wtf/HashSet.h"
+#include "wtf/HashMap.h"
namespace blink {
@@ -47,7 +47,18 @@ private:
bool isValid() const;
void setCheckedButton(HTMLInputElement*);
- WillBeHeapHashSet<RawPtrWillBeMember<HTMLInputElement>> m_members;
+ // The map records the 'required' state of each (button) element.
+ using Members = WillBeHeapHashMap<RawPtrWillBeMember<HTMLInputElement>, bool>;
+
+#if ENABLE(OILPAN)
+ using MemberKeyValue = WTF::KeyValuePair<Member<HTMLInputElement>, bool>;
+#else
+ using MemberKeyValue = WTF::KeyValuePair<HTMLInputElement*, bool>;
+#endif
+
+ void updateRequiredButton(MemberKeyValue&, bool isRequired);
+
+ Members m_members;
RawPtrWillBeMember<HTMLInputElement> m_checkedButton;
size_t m_requiredCount;
};
@@ -78,14 +89,28 @@ void RadioButtonGroup::setCheckedButton(HTMLInputElement* button)
oldCheckedButton->setChecked(false);
}
+void RadioButtonGroup::updateRequiredButton(MemberKeyValue& it, bool isRequired)
+{
+ if (it.value == isRequired)
+ return;
+
+ it.value = isRequired;
+ if (isRequired) {
+ m_requiredCount++;
+ } else {
+ ASSERT(m_requiredCount);
+ m_requiredCount--;
+ }
+}
+
void RadioButtonGroup::add(HTMLInputElement* button)
{
ASSERT(button->type() == InputTypeNames::radio);
- if (!m_members.add(button).isNewEntry)
+ auto addResult = m_members.add(button, false);
+ if (!addResult.isNewEntry)
return;
bool groupWasValid = isValid();
- if (button->isRequired())
- ++m_requiredCount;
+ updateRequiredButton(*addResult.storedValue, button->isRequired());
if (button->checked())
setCheckedButton(button);
@@ -112,7 +137,8 @@ void RadioButtonGroup::updateCheckedState(HTMLInputElement* button)
}
if (wasValid != isValid())
setNeedsValidityCheckForAllButtons();
- for (HTMLInputElement* const inputElement : m_members) {
+ for (auto& member : m_members) {
+ HTMLInputElement* const inputElement = member.key;
inputElement->pseudoStateChanged(CSSSelector::PseudoIndeterminate);
}
}
@@ -120,14 +146,12 @@ void RadioButtonGroup::updateCheckedState(HTMLInputElement* button)
void RadioButtonGroup::requiredAttributeChanged(HTMLInputElement* button)
{
ASSERT(button->type() == InputTypeNames::radio);
- ASSERT(m_members.contains(button));
+ auto it = m_members.find(button);
+ ASSERT(it != m_members.end());
bool wasValid = isValid();
- if (button->isRequired()) {
- ++m_requiredCount;
- } else {
- ASSERT(m_requiredCount);
- --m_requiredCount;
- }
+ // Synchronize the 'required' flag for the button, along with
+ // updating the overall count.
+ updateRequiredButton(*it, button->isRequired());
if (wasValid != isValid())
setNeedsValidityCheckForAllButtons();
}
@@ -135,15 +159,13 @@ void RadioButtonGroup::requiredAttributeChanged(HTMLInputElement* button)
void RadioButtonGroup::remove(HTMLInputElement* button)
{
ASSERT(button->type() == InputTypeNames::radio);
- WillBeHeapHashSet<RawPtrWillBeMember<HTMLInputElement>>::iterator it = m_members.find(button);
+ auto it = m_members.find(button);
if (it == m_members.end())
return;
bool wasValid = isValid();
+ ASSERT(it->value == button->isRequired());
+ updateRequiredButton(*it, false);
m_members.remove(it);
- if (button->isRequired()) {
- ASSERT(m_requiredCount);
- --m_requiredCount;
- }
if (m_checkedButton == button)
m_checkedButton = nullptr;
@@ -162,7 +184,8 @@ void RadioButtonGroup::remove(HTMLInputElement* button)
void RadioButtonGroup::setNeedsValidityCheckForAllButtons()
{
- for (HTMLInputElement* const button : m_members) {
+ for (auto& element : m_members) {
+ HTMLInputElement* const button = element.key;
ASSERT(button->type() == InputTypeNames::radio);
button->setNeedsValidityCheck();
}
« no previous file with comments | « third_party/WebKit/LayoutTests/fast/forms/radio/radio-group-remove-required-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698