1 /*
2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21 #include "config.h"
22 #include "CheckedRadioButtons.h"
23
24 #include "HTMLInputElement.h"
25
26 namespace WebCore {
27
addButton(HTMLFormControlElement * element)28 void CheckedRadioButtons::addButton(HTMLFormControlElement* element)
29 {
30 // We only want to add radio buttons.
31 if (!element->isRadioButton())
32 return;
33
34 // Without a name, there is no group.
35 if (element->name().isEmpty())
36 return;
37
38 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(element);
39
40 // We only track checked buttons.
41 if (!inputElement->checked())
42 return;
43
44 if (!m_nameToCheckedRadioButtonMap)
45 m_nameToCheckedRadioButtonMap.set(new NameToInputMap);
46
47 pair<NameToInputMap::iterator, bool> result = m_nameToCheckedRadioButtonMap->add(element->name().impl(), inputElement);
48 if (result.second)
49 return;
50
51 HTMLInputElement* oldCheckedButton = result.first->second;
52 if (oldCheckedButton == inputElement)
53 return;
54
55 result.first->second = inputElement;
56 oldCheckedButton->setChecked(false);
57 }
58
checkedButtonForGroup(const AtomicString & name) const59 HTMLInputElement* CheckedRadioButtons::checkedButtonForGroup(const AtomicString& name) const
60 {
61 if (!m_nameToCheckedRadioButtonMap)
62 return 0;
63
64 m_nameToCheckedRadioButtonMap->checkConsistency();
65
66 return m_nameToCheckedRadioButtonMap->get(name.impl());
67 }
68
removeButton(HTMLFormControlElement * element)69 void CheckedRadioButtons::removeButton(HTMLFormControlElement* element)
70 {
71 if (element->name().isEmpty() || !m_nameToCheckedRadioButtonMap)
72 return;
73
74 m_nameToCheckedRadioButtonMap->checkConsistency();
75
76 NameToInputMap::iterator it = m_nameToCheckedRadioButtonMap->find(element->name().impl());
77 if (it == m_nameToCheckedRadioButtonMap->end() || it->second != element)
78 return;
79
80 InputElement* inputElement = element->toInputElement();
81 ASSERT_UNUSED(inputElement, inputElement);
82 ASSERT(inputElement->isChecked());
83 ASSERT(element->isRadioButton());
84
85 m_nameToCheckedRadioButtonMap->remove(it);
86 if (m_nameToCheckedRadioButtonMap->isEmpty())
87 m_nameToCheckedRadioButtonMap.clear();
88 }
89
90 } // namespace
91