JSHTMLAllCollectionCustom.cpp [plain text]
#include "config.h"
#include "JSHTMLAllCollection.h"
#include "HTMLAllCollection.h"
#include "JSDOMBinding.h"
#include "JSHTMLAllCollection.h"
#include "JSNode.h"
#include "JSNodeList.h"
#include "Node.h"
#include "StaticNodeList.h"
#include <runtime/JSValue.h>
#include <wtf/Vector.h>
#include <wtf/text/AtomicString.h>
using namespace JSC;
namespace WebCore {
static JSValue getNamedItems(ExecState* exec, JSHTMLAllCollection* collection, const Identifier& propertyName)
{
Vector<RefPtr<Node> > namedItems;
collection->impl()->namedItems(identifierToAtomicString(propertyName), namedItems);
if (namedItems.isEmpty())
return jsUndefined();
if (namedItems.size() == 1)
return toJS(exec, collection->globalObject(), namedItems[0].get());
return toJS(exec, collection->globalObject(), StaticNodeList::adopt(namedItems).get());
}
static EncodedJSValue JSC_HOST_CALL callHTMLAllCollection(ExecState* exec)
{
if (exec->argumentCount() < 1)
return JSValue::encode(jsUndefined());
JSHTMLAllCollection* jsCollection = static_cast<JSHTMLAllCollection*>(exec->callee());
HTMLAllCollection* collection = static_cast<HTMLAllCollection*>(jsCollection->impl());
if (exec->argumentCount() == 1) {
bool ok;
UString string = exec->argument(0).toString(exec);
unsigned index = Identifier::toUInt32(string, ok);
if (ok)
return JSValue::encode(toJS(exec, jsCollection->globalObject(), collection->item(index)));
return JSValue::encode(getNamedItems(exec, jsCollection, Identifier(exec, string)));
}
bool ok;
UString string = exec->argument(0).toString(exec);
unsigned index = Identifier::toUInt32(exec->argument(1).toString(exec), ok);
if (ok) {
String pstr = ustringToString(string);
Node* node = collection->namedItem(pstr);
while (node) {
if (!index)
return JSValue::encode(toJS(exec, jsCollection->globalObject(), node));
node = collection->nextNamedItem(pstr);
--index;
}
}
return JSValue::encode(jsUndefined());
}
CallType JSHTMLAllCollection::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callHTMLAllCollection;
return CallTypeHost;
}
bool JSHTMLAllCollection::canGetItemsForName(ExecState*, HTMLAllCollection* collection, const Identifier& propertyName)
{
Vector<RefPtr<Node> > namedItems;
collection->namedItems(identifierToAtomicString(propertyName), namedItems);
return !namedItems.isEmpty();
}
JSValue JSHTMLAllCollection::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
{
JSHTMLAllCollection* thisObj = static_cast<JSHTMLAllCollection*>(asObject(slotBase));
return getNamedItems(exec, thisObj, propertyName);
}
JSValue JSHTMLAllCollection::item(ExecState* exec)
{
bool ok;
uint32_t index = Identifier::toUInt32(exec->argument(0).toString(exec), ok);
if (ok)
return toJS(exec, globalObject(), impl()->item(index));
return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)));
}
JSValue JSHTMLAllCollection::namedItem(ExecState* exec)
{
return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)));
}
}