JSHTMLAllCollectionCustom.cpp [plain text]
#include "config.h"
#include "JSHTMLAllCollection.h"
#include "HTMLAllCollection.h"
#include "JSDOMBinding.h"
#include "JSNode.h"
#include "JSNodeList.h"
#include "Node.h"
#include "StaticNodeList.h"
#include <runtime/JSCJSValue.h>
#include <wtf/Vector.h>
#include <wtf/text/AtomicString.h>
using namespace JSC;
namespace WebCore {
static JSValue getNamedItems(ExecState* exec, JSHTMLAllCollection* collection, PropertyName propertyName)
{
Vector<RefPtr<Node> > namedItems;
collection->impl()->namedItems(propertyNameToAtomicString(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 = jsCast<JSHTMLAllCollection*>(exec->callee());
HTMLAllCollection* collection = static_cast<HTMLAllCollection*>(jsCollection->impl());
if (exec->argumentCount() == 1) {
String string = exec->argument(0).toString(exec)->value(exec);
unsigned index = toUInt32FromStringImpl(string.impl());
if (index != PropertyName::NotAnIndex)
return JSValue::encode(toJS(exec, jsCollection->globalObject(), collection->item(index)));
return JSValue::encode(getNamedItems(exec, jsCollection, Identifier(exec, string)));
}
String string = exec->argument(0).toString(exec)->value(exec);
unsigned index = toUInt32FromStringImpl(exec->argument(1).toWTFString(exec).impl());
if (index != PropertyName::NotAnIndex) {
if (Node* node = collection->namedItemWithIndex(string, index))
return JSValue::encode(toJS(exec, jsCollection->globalObject(), node));
}
return JSValue::encode(jsUndefined());
}
CallType JSHTMLAllCollection::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callHTMLAllCollection;
return CallTypeHost;
}
bool JSHTMLAllCollection::canGetItemsForName(ExecState*, HTMLAllCollection* collection, PropertyName propertyName)
{
return collection->hasNamedItem(propertyNameToAtomicString(propertyName));
}
JSValue JSHTMLAllCollection::nameGetter(ExecState* exec, JSValue slotBase, PropertyName propertyName)
{
JSHTMLAllCollection* thisObj = jsCast<JSHTMLAllCollection*>(asObject(slotBase));
return getNamedItems(exec, thisObj, propertyName);
}
JSValue JSHTMLAllCollection::item(ExecState* exec)
{
uint32_t index = toUInt32FromStringImpl(exec->argument(0).toString(exec)->value(exec).impl());
if (index != PropertyName::NotAnIndex)
return toJS(exec, globalObject(), impl()->item(index));
return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)->value(exec)));
}
JSValue JSHTMLAllCollection::namedItem(ExecState* exec)
{
JSValue value = getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec)->value(exec)));
return value.isUndefined() ? jsNull() : value;
}
}