JSNamedNodesCollection.cpp [plain text]
#include "config.h"
#include "JSNamedNodesCollection.h"
#include "AtomicString.h"
#include "NamedAttrMap.h"
#include "Node.h"
#include "kjs_dom.h"
namespace WebCore {
using namespace KJS;
const ClassInfo JSNamedNodesCollection::info = { "Collection", 0, 0 };
JSNamedNodesCollection::JSNamedNodesCollection(KJS::JSObject* prototype, const Vector<RefPtr<Node> >& nodes)
: KJS::DOMObject(prototype)
, m_nodes(nodes)
{
}
JSValue* JSNamedNodesCollection::lengthGetter(ExecState* exec, JSObject* originalObject, const Identifier& propertyName, const PropertySlot& slot)
{
JSNamedNodesCollection* thisObj = static_cast<JSNamedNodesCollection*>(slot.slotBase());
return jsNumber(thisObj->m_nodes.size());
}
JSValue* JSNamedNodesCollection::indexGetter(ExecState* exec, JSObject* originalObject, const Identifier& propertyName, const PropertySlot& slot)
{
JSNamedNodesCollection *thisObj = static_cast<JSNamedNodesCollection*>(slot.slotBase());
return toJS(exec, thisObj->m_nodes[slot.index()].get());
}
bool JSNamedNodesCollection::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
if (propertyName == exec->propertyNames().length) {
slot.setCustom(this, lengthGetter);
return true;
}
bool ok;
unsigned index = propertyName.toUInt32(&ok);
if (ok && index < m_nodes.size()) {
slot.setCustomIndex(this, index, indexGetter);
return true;
}
AtomicString atomicPropertyName = propertyName;
for (unsigned i = 0; i < m_nodes.size(); i++) {
Node* node = m_nodes[i].get();
if (node->hasAttributes() && node->attributes()->id() == atomicPropertyName) {
slot.setCustomIndex(this, i, indexGetter);
return true;
}
}
return DOMObject::getOwnPropertySlot(exec, propertyName, slot);
}
}