• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 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 COMPUTER, 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 COMPUTER, 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 "JSNamedNodesCollection.h"
28 
29 #include "AtomicString.h"
30 #include "Element.h"
31 #include "JSNode.h"
32 #include "NamedNodeMap.h"
33 
34 namespace WebCore {
35 
36 using namespace JSC;
37 
38 ASSERT_CLASS_FITS_IN_CELL(JSNamedNodesCollection);
39 
40 const ClassInfo JSNamedNodesCollection::s_info = { "Collection", 0, 0, 0 };
41 
42 // Such a collection is usually very short-lived, it only exists
43 // for constructs like document.forms.<name>[1],
44 // so it shouldn't be a problem that it's storing all the nodes (with the same name). (David)
JSNamedNodesCollection(ExecState * exec,JSDOMGlobalObject * globalObject,const Vector<RefPtr<Node>> & nodes)45 JSNamedNodesCollection::JSNamedNodesCollection(ExecState* exec, JSDOMGlobalObject* globalObject, const Vector<RefPtr<Node> >& nodes)
46     : DOMObjectWithGlobalPointer(getDOMStructure<JSNamedNodesCollection>(exec, globalObject), globalObject)
47     , m_nodes(new Vector<RefPtr<Node> >(nodes))
48 {
49 }
50 
lengthGetter(ExecState * exec,const Identifier &,const PropertySlot & slot)51 JSValue JSNamedNodesCollection::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
52 {
53     JSNamedNodesCollection* thisObj = static_cast<JSNamedNodesCollection*>(asObject(slot.slotBase()));
54     return jsNumber(exec, thisObj->m_nodes->size());
55 }
56 
indexGetter(ExecState * exec,const Identifier &,const PropertySlot & slot)57 JSValue JSNamedNodesCollection::indexGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
58 {
59     JSNamedNodesCollection *thisObj = static_cast<JSNamedNodesCollection*>(asObject(slot.slotBase()));
60     return toJS(exec, (*thisObj->m_nodes)[slot.index()].get());
61 }
62 
getOwnPropertySlot(ExecState * exec,const Identifier & propertyName,PropertySlot & slot)63 bool JSNamedNodesCollection::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
64 {
65     if (propertyName == exec->propertyNames().length) {
66         slot.setCustom(this, lengthGetter);
67         return true;
68     }
69 
70     bool ok;
71     unsigned index = propertyName.toUInt32(&ok);
72     if (ok && index < m_nodes->size()) {
73         slot.setCustomIndex(this, index, indexGetter);
74         return true;
75     }
76 
77     // For IE compatibility, we need to be able to look up elements in a
78     // document.formName.name result by id as well as be index.
79 
80     AtomicString atomicPropertyName = propertyName;
81     for (unsigned i = 0; i < m_nodes->size(); i++) {
82         Node* node = (*m_nodes)[i].get();
83         if (node->hasAttributes() && node->attributes()->id() == atomicPropertyName) {
84             slot.setCustomIndex(this, i, indexGetter);
85             return true;
86         }
87     }
88 
89     return DOMObjectWithGlobalPointer::getOwnPropertySlot(exec, propertyName, slot);
90 }
91 
92 } // namespace WebCore
93