Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / dom / CheckedRadioButtons.cpp
blob9883f5843e3c63696e5d6a09ecafa75c0b116960
1 /*
2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "config.h"
22 #include "CheckedRadioButtons.h"
24 #include "HTMLInputElement.h"
26 namespace WebCore {
28 void CheckedRadioButtons::addButton(HTMLFormControlElement* element)
30 // We only want to add radio buttons.
31 if (!element->isRadioButton())
32 return;
34 // Without a name, there is no group.
35 if (element->name().isEmpty())
36 return;
38 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(element);
40 // We only track checked buttons.
41 if (!inputElement->checked())
42 return;
44 if (!m_nameToCheckedRadioButtonMap)
45 m_nameToCheckedRadioButtonMap.set(new NameToInputMap);
47 pair<NameToInputMap::iterator, bool> result = m_nameToCheckedRadioButtonMap->add(element->name().impl(), inputElement);
48 if (result.second)
49 return;
51 HTMLInputElement* oldCheckedButton = result.first->second;
52 if (oldCheckedButton == inputElement)
53 return;
55 result.first->second = inputElement;
56 oldCheckedButton->setChecked(false);
59 HTMLInputElement* CheckedRadioButtons::checkedButtonForGroup(const AtomicString& name) const
61 if (!m_nameToCheckedRadioButtonMap)
62 return 0;
64 return m_nameToCheckedRadioButtonMap->get(name.impl());
67 void CheckedRadioButtons::removeButton(HTMLFormControlElement* element)
69 if (element->name().isEmpty() || !m_nameToCheckedRadioButtonMap)
70 return;
72 NameToInputMap::iterator it = m_nameToCheckedRadioButtonMap->find(element->name().impl());
73 if (it == m_nameToCheckedRadioButtonMap->end() || it->second != element)
74 return;
76 InputElement* inputElement = toInputElement(element);
77 ASSERT_UNUSED(inputElement, inputElement);
78 ASSERT(inputElement->isChecked());
79 ASSERT(element->isRadioButton());
81 m_nameToCheckedRadioButtonMap->remove(it);
82 if (m_nameToCheckedRadioButtonMap->isEmpty())
83 m_nameToCheckedRadioButtonMap.clear();
86 } // namespace