1/*
2 * Copyright (C) 2007, 2008 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#include "config.h"
21#include "HTMLFormControlsCollection.h"
22
23
24#include "HTMLAllCollection.h"
25#include "JSDOMBinding.h"
26#include "JSHTMLCollection.h"
27#include "JSHTMLFormControlsCollection.h"
28#include "JSNode.h"
29#include "JSNodeList.h"
30#include "JSRadioNodeList.h"
31#include "Node.h"
32#include "RadioNodeList.h"
33#include <runtime/IdentifierInlines.h>
34#include <wtf/Vector.h>
35#include <wtf/text/AtomicString.h>
36
37using namespace JSC;
38
39namespace WebCore {
40
41static JSValue getNamedItems(ExecState* exec, JSHTMLFormControlsCollection* collection, PropertyName propertyName)
42{
43    Vector<Ref<Element>> namedItems;
44    const AtomicString& name = propertyNameToAtomicString(propertyName);
45    collection->impl().namedItems(name, namedItems);
46
47    if (namedItems.isEmpty())
48        return jsUndefined();
49    if (namedItems.size() == 1)
50        return toJS(exec, collection->globalObject(), &namedItems[0].get());
51
52    ASSERT(collection->impl().type() == FormControls);
53    return toJS(exec, collection->globalObject(), collection->impl().ownerNode().radioNodeList(name).get());
54}
55
56bool JSHTMLFormControlsCollection::canGetItemsForName(ExecState*, HTMLFormControlsCollection* collection, PropertyName propertyName)
57{
58    return collection->hasNamedItem(propertyNameToAtomicString(propertyName));
59}
60
61EncodedJSValue JSHTMLFormControlsCollection::nameGetter(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName propertyName)
62{
63    JSHTMLFormControlsCollection* thisObj = jsCast<JSHTMLFormControlsCollection*>(slotBase);
64    return JSValue::encode(getNamedItems(exec, thisObj, propertyName));
65}
66
67JSValue JSHTMLFormControlsCollection::namedItem(ExecState* exec)
68{
69    JSValue value = getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)->value(exec)));
70    return value.isUndefined() ? jsNull() : value;
71}
72
73} // namespace WebCore
74