void RadioButtonGroup::remove(HTMLInputElement* button)
{
    ASSERT(button->isRadioButton());
    HashSet<HTMLInputElement*>::iterator it = m_members.find(button);
    if (it == m_members.end())
        return;
    bool wasValid = isValid();
    m_members.remove(it);
    if (button->isRequired()) {
        ASSERT(m_requiredCount);
        --m_requiredCount;
    }
    if (m_checkedButton == button)
        m_checkedButton = nullptr;

    if (m_members.isEmpty()) {
        ASSERT(!m_requiredCount);
        ASSERT(!m_checkedButton);
    } else if (wasValid != isValid())
        setNeedsValidityCheckForAllButtons();
    if (!wasValid) {
        // A radio button not in a group is always valid. We need to make it
        // valid only if the group was invalid.
        button->setNeedsValidityCheck();
    }
}
void RadioButtonGroup::remove(HTMLInputElement* button) {
  DCHECK_EQ(button->type(), InputTypeNames::radio);
  auto it = m_members.find(button);
  if (it == m_members.end())
    return;
  bool wasValid = isValid();
  DCHECK_EQ(it->value, button->isRequired());
  updateRequiredButton(*it, false);
  m_members.remove(it);
  if (m_checkedButton == button)
    m_checkedButton = nullptr;

  if (m_members.isEmpty()) {
    DCHECK(!m_requiredCount);
    DCHECK(!m_checkedButton);
  } else if (wasValid != isValid()) {
    setNeedsValidityCheckForAllButtons();
  }
  if (!wasValid) {
    // A radio button not in a group is always valid. We need to make it
    // valid only if the group was invalid.
    button->setNeedsValidityCheck();
  }

  // Send notification to update AX attributes for AXObjects which radiobutton
  // group has.
  if (!m_members.isEmpty()) {
    HTMLInputElement* input = m_members.begin()->key;
    if (AXObjectCache* cache = input->document().existingAXObjectCache())
      cache->radiobuttonRemovedFromGroup(input);
  }
}
void RadioButtonGroup::requiredAttributeChanged(HTMLInputElement* button) {
  DCHECK_EQ(button->type(), InputTypeNames::radio);
  auto it = m_members.find(button);
  DCHECK_NE(it, m_members.end());
  bool wasValid = isValid();
  // Synchronize the 'required' flag for the button, along with
  // updating the overall count.
  updateRequiredButton(*it, button->isRequired());
  if (wasValid != isValid())
    setNeedsValidityCheckForAllButtons();
}
void RadioButtonGroup::updateCheckedState(HTMLInputElement* button)
{
    ASSERT(button->isRadioButton());
    ASSERT(m_members.contains(button));
    bool wasValid = isValid();
    if (button->checked())
        setCheckedButton(button);
    else {
        if (m_checkedButton == button)
            m_checkedButton = 0;
    }
    if (wasValid != isValid())
        setNeedsValidityCheckForAllButtons();
}
void RadioButtonGroup::requiredAttributeChanged(HTMLInputElement* button)
{
    ASSERT(button->isRadioButton());
    ASSERT(m_members.contains(button));
    bool wasValid = isValid();
    if (button->isRequired())
        ++m_requiredCount;
    else {
        ASSERT(m_requiredCount);
        --m_requiredCount;
    }
    if (wasValid != isValid())
        setNeedsValidityCheckForAllButtons();
}
void RadioButtonGroup::updateCheckedState(HTMLInputElement* button) {
  DCHECK_EQ(button->type(), InputTypeNames::radio);
  DCHECK(m_members.contains(button));
  bool wasValid = isValid();
  if (button->checked()) {
    setCheckedButton(button);
  } else {
    if (m_checkedButton == button)
      m_checkedButton = nullptr;
  }
  if (wasValid != isValid())
    setNeedsValidityCheckForAllButtons();
  for (auto& member : m_members) {
    HTMLInputElement* const inputElement = member.key;
    inputElement->pseudoStateChanged(CSSSelector::PseudoIndeterminate);
  }
}
void RadioButtonGroup::add(HTMLInputElement* button) {
  DCHECK_EQ(button->type(), InputTypeNames::radio);
  auto addResult = m_members.add(button, false);
  if (!addResult.isNewEntry)
    return;
  bool groupWasValid = isValid();
  updateRequiredButton(*addResult.storedValue, button->isRequired());
  if (button->checked())
    setCheckedButton(button);

  bool groupIsValid = isValid();
  if (groupWasValid != groupIsValid) {
    setNeedsValidityCheckForAllButtons();
  } else if (!groupIsValid) {
    // A radio button not in a group is always valid. We need to make it
    // invalid only if the group is invalid.
    button->setNeedsValidityCheck();
  }
}
void RadioButtonGroup::add(HTMLInputElement* button)
{
    ASSERT(button->isRadioButton());
    if (!m_members.add(button).isNewEntry)
        return;
    bool groupWasValid = isValid();
    if (button->isRequired())
        ++m_requiredCount;
    if (button->checked())
        setCheckedButton(button);

    bool groupIsValid = isValid();
    if (groupWasValid != groupIsValid)
        setNeedsValidityCheckForAllButtons();
    else if (!groupIsValid) {
        // A radio button not in a group is always valid. We need to make it
        // invalid only if the group is invalid.
        button->setNeedsValidityCheck();
    }
}