#include "config.h"
#include "RadioNodeList.h"
#include "Element.h"
#include "HTMLFormElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLObjectElement.h"
#include "NodeRareData.h"
namespace WebCore {
using namespace HTMLNames;
RadioNodeList::RadioNodeList(Node* rootNode, const AtomicString& name)
: LiveNodeList(rootNode, RadioNodeListType, InvalidateForFormControls, rootNode->hasTagName(formTag) ? NodeListIsRootedAtDocument : NodeListIsRootedAtNode)
, m_name(name)
{
}
RadioNodeList::~RadioNodeList()
{
ownerNode()->nodeLists()->removeCacheWithAtomicName(this, RadioNodeListType, m_name);
}
static inline HTMLInputElement* toRadioButtonInputElement(Node* node)
{
ASSERT(node->isElementNode());
HTMLInputElement* inputElement = node->toInputElement();
if (!inputElement || !inputElement->isRadioButton() || inputElement->value().isEmpty())
return 0;
return inputElement;
}
String RadioNodeList::value() const
{
for (unsigned i = 0; i < length(); ++i) {
Node* node = item(i);
const HTMLInputElement* inputElement = toRadioButtonInputElement(node);
if (!inputElement || !inputElement->checked())
continue;
return inputElement->value();
}
return String();
}
void RadioNodeList::setValue(const String& value)
{
for (unsigned i = 0; i < length(); ++i) {
Node* node = item(i);
HTMLInputElement* inputElement = toRadioButtonInputElement(node);
if (!inputElement || inputElement->value() != value)
continue;
inputElement->setChecked(true);
return;
}
}
bool RadioNodeList::checkElementMatchesRadioNodeListFilter(Element* testElement) const
{
ASSERT(testElement->hasTagName(objectTag) || testElement->isFormControlElement());
if (ownerNode()->hasTagName(formTag)) {
HTMLFormElement* formElement = 0;
if (testElement->hasTagName(objectTag))
formElement = static_cast<HTMLObjectElement*>(testElement)->form();
else
formElement = static_cast<HTMLFormControlElement*>(testElement)->form();
if (!formElement || formElement != ownerNode())
return false;
}
return testElement->getIdAttribute() == m_name || testElement->getNameAttribute() == m_name;
}
bool RadioNodeList::nodeMatches(Element* testElement) const
{
if (!testElement->hasTagName(objectTag) && !testElement->isFormControlElement())
return false;
if (HTMLInputElement* inputElement = testElement->toInputElement()) {
if (inputElement->isImageButton())
return false;
}
return checkElementMatchesRadioNodeListFilter(testElement);
}
}