blob: 3793fe55afddd6ad7dc91c111640ad7d832e191c [file] [log] [blame]
weinig@apple.com963305c2009-10-23 21:12:03 +00001/*
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSHTMLAllCollection.h"
28
weinig@apple.com963305c2009-10-23 21:12:03 +000029#include "HTMLAllCollection.h"
30#include "JSDOMBinding.h"
31#include "JSHTMLAllCollection.h"
32#include "JSNode.h"
33#include "JSNodeList.h"
34#include "Node.h"
weinig@apple.coma11f5432009-10-26 20:04:22 +000035#include "StaticNodeList.h"
barraclough@apple.com99ff3432010-06-03 20:00:18 +000036#include <runtime/JSValue.h>
weinig@apple.coma11f5432009-10-26 20:04:22 +000037#include <wtf/Vector.h>
barraclough@apple.combbb3cd42010-08-10 17:45:41 +000038#include <wtf/text/AtomicString.h>
weinig@apple.com963305c2009-10-23 21:12:03 +000039
40using namespace JSC;
41
42namespace WebCore {
43
weinig@apple.coma11f5432009-10-26 20:04:22 +000044static JSValue getNamedItems(ExecState* exec, JSHTMLAllCollection* collection, const Identifier& propertyName)
45{
46 Vector<RefPtr<Node> > namedItems;
barraclough@apple.com2b9b5972010-04-17 00:10:39 +000047 collection->impl()->namedItems(identifierToAtomicString(propertyName), namedItems);
weinig@apple.coma11f5432009-10-26 20:04:22 +000048
49 if (namedItems.isEmpty())
50 return jsUndefined();
51 if (namedItems.size() == 1)
52 return toJS(exec, collection->globalObject(), namedItems[0].get());
53
54 // FIXME: HTML5 specifies that this should be a DynamicNodeList.
55 // FIXME: HTML5 specifies that non-HTMLOptionsCollection collections should return
56 // the first matching item instead of a NodeList.
57 return toJS(exec, collection->globalObject(), StaticNodeList::adopt(namedItems).get());
58}
59
60// HTMLCollections are strange objects, they support both get and call,
61// so that document.forms.item(0) and document.forms(0) both work.
barraclough@apple.com99ff3432010-06-03 20:00:18 +000062static EncodedJSValue JSC_HOST_CALL callHTMLAllCollection(ExecState* exec)
weinig@apple.com963305c2009-10-23 21:12:03 +000063{
ggaren@apple.comfea29f12010-05-29 06:33:05 +000064 if (exec->argumentCount() < 1)
barraclough@apple.com99ff3432010-06-03 20:00:18 +000065 return JSValue::encode(jsUndefined());
weinig@apple.coma11f5432009-10-26 20:04:22 +000066
67 // Do not use thisObj here. It can be the JSHTMLDocument, in the document.forms(i) case.
ggaren@apple.comfea29f12010-05-29 06:33:05 +000068 JSHTMLAllCollection* jsCollection = static_cast<JSHTMLAllCollection*>(exec->callee());
weinig@apple.coma11f5432009-10-26 20:04:22 +000069 HTMLAllCollection* collection = static_cast<HTMLAllCollection*>(jsCollection->impl());
70
71 // Also, do we need the TypeError test here ?
72
ggaren@apple.comfea29f12010-05-29 06:33:05 +000073 if (exec->argumentCount() == 1) {
weinig@apple.coma11f5432009-10-26 20:04:22 +000074 // Support for document.all(<index>) etc.
75 bool ok;
ggaren@apple.comfea29f12010-05-29 06:33:05 +000076 UString string = exec->argument(0).toString(exec);
barraclough@apple.com794f4612010-08-18 07:41:22 +000077 unsigned index = Identifier::toUInt32(string, ok);
weinig@apple.coma11f5432009-10-26 20:04:22 +000078 if (ok)
barraclough@apple.com99ff3432010-06-03 20:00:18 +000079 return JSValue::encode(toJS(exec, jsCollection->globalObject(), collection->item(index)));
weinig@apple.coma11f5432009-10-26 20:04:22 +000080
81 // Support for document.images('<name>') etc.
barraclough@apple.com99ff3432010-06-03 20:00:18 +000082 return JSValue::encode(getNamedItems(exec, jsCollection, Identifier(exec, string)));
weinig@apple.coma11f5432009-10-26 20:04:22 +000083 }
84
85 // The second arg, if set, is the index of the item we want
86 bool ok;
ggaren@apple.comfea29f12010-05-29 06:33:05 +000087 UString string = exec->argument(0).toString(exec);
barraclough@apple.com794f4612010-08-18 07:41:22 +000088 unsigned index = Identifier::toUInt32(exec->argument(1).toString(exec), ok);
weinig@apple.coma11f5432009-10-26 20:04:22 +000089 if (ok) {
paroga@webkit.orge2df0522011-07-25 01:27:05 +000090 AtomicString pstr = ustringToAtomicString(string);
weinig@apple.coma11f5432009-10-26 20:04:22 +000091 Node* node = collection->namedItem(pstr);
92 while (node) {
93 if (!index)
barraclough@apple.com99ff3432010-06-03 20:00:18 +000094 return JSValue::encode(toJS(exec, jsCollection->globalObject(), node));
weinig@apple.coma11f5432009-10-26 20:04:22 +000095 node = collection->nextNamedItem(pstr);
96 --index;
97 }
98 }
99
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000100 return JSValue::encode(jsUndefined());
weinig@apple.com963305c2009-10-23 21:12:03 +0000101}
102
103CallType JSHTMLAllCollection::getCallData(CallData& callData)
104{
105 callData.native.function = callHTMLAllCollection;
106 return CallTypeHost;
107}
108
109bool JSHTMLAllCollection::canGetItemsForName(ExecState*, HTMLAllCollection* collection, const Identifier& propertyName)
110{
111 Vector<RefPtr<Node> > namedItems;
barraclough@apple.com2b9b5972010-04-17 00:10:39 +0000112 collection->namedItems(identifierToAtomicString(propertyName), namedItems);
weinig@apple.com963305c2009-10-23 21:12:03 +0000113 return !namedItems.isEmpty();
114}
115
oliver@apple.com467b7792010-03-02 08:20:48 +0000116JSValue JSHTMLAllCollection::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
weinig@apple.com963305c2009-10-23 21:12:03 +0000117{
oliver@apple.com467b7792010-03-02 08:20:48 +0000118 JSHTMLAllCollection* thisObj = static_cast<JSHTMLAllCollection*>(asObject(slotBase));
weinig@apple.coma11f5432009-10-26 20:04:22 +0000119 return getNamedItems(exec, thisObj, propertyName);
weinig@apple.com963305c2009-10-23 21:12:03 +0000120}
121
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000122JSValue JSHTMLAllCollection::item(ExecState* exec)
weinig@apple.com963305c2009-10-23 21:12:03 +0000123{
weinig@apple.coma11f5432009-10-26 20:04:22 +0000124 bool ok;
barraclough@apple.com794f4612010-08-18 07:41:22 +0000125 uint32_t index = Identifier::toUInt32(exec->argument(0).toString(exec), ok);
weinig@apple.coma11f5432009-10-26 20:04:22 +0000126 if (ok)
127 return toJS(exec, globalObject(), impl()->item(index));
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000128 return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)));
weinig@apple.com963305c2009-10-23 21:12:03 +0000129}
130
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000131JSValue JSHTMLAllCollection::namedItem(ExecState* exec)
weinig@apple.com963305c2009-10-23 21:12:03 +0000132{
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000133 return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)));
weinig@apple.com963305c2009-10-23 21:12:03 +0000134}
135
136} // namespace WebCore