weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 29 | #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.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 35 | #include "StaticNodeList.h" |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 36 | #include <runtime/JSValue.h> |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 37 | #include <wtf/Vector.h> |
barraclough@apple.com | bbb3cd4 | 2010-08-10 17:45:41 +0000 | [diff] [blame] | 38 | #include <wtf/text/AtomicString.h> |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace JSC; |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 44 | static JSValue getNamedItems(ExecState* exec, JSHTMLAllCollection* collection, const Identifier& propertyName) |
| 45 | { |
| 46 | Vector<RefPtr<Node> > namedItems; |
barraclough@apple.com | 2b9b597 | 2010-04-17 00:10:39 +0000 | [diff] [blame] | 47 | collection->impl()->namedItems(identifierToAtomicString(propertyName), namedItems); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 48 | |
| 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.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 62 | static EncodedJSValue JSC_HOST_CALL callHTMLAllCollection(ExecState* exec) |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 63 | { |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 64 | if (exec->argumentCount() < 1) |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 65 | return JSValue::encode(jsUndefined()); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 66 | |
| 67 | // Do not use thisObj here. It can be the JSHTMLDocument, in the document.forms(i) case. |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 68 | JSHTMLAllCollection* jsCollection = static_cast<JSHTMLAllCollection*>(exec->callee()); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 69 | HTMLAllCollection* collection = static_cast<HTMLAllCollection*>(jsCollection->impl()); |
| 70 | |
| 71 | // Also, do we need the TypeError test here ? |
| 72 | |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 73 | if (exec->argumentCount() == 1) { |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 74 | // Support for document.all(<index>) etc. |
| 75 | bool ok; |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 76 | UString string = exec->argument(0).toString(exec); |
barraclough@apple.com | 794f461 | 2010-08-18 07:41:22 +0000 | [diff] [blame] | 77 | unsigned index = Identifier::toUInt32(string, ok); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 78 | if (ok) |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 79 | return JSValue::encode(toJS(exec, jsCollection->globalObject(), collection->item(index))); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 80 | |
| 81 | // Support for document.images('<name>') etc. |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 82 | return JSValue::encode(getNamedItems(exec, jsCollection, Identifier(exec, string))); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // The second arg, if set, is the index of the item we want |
| 86 | bool ok; |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 87 | UString string = exec->argument(0).toString(exec); |
barraclough@apple.com | 794f461 | 2010-08-18 07:41:22 +0000 | [diff] [blame] | 88 | unsigned index = Identifier::toUInt32(exec->argument(1).toString(exec), ok); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 89 | if (ok) { |
paroga@webkit.org | e2df052 | 2011-07-25 01:27:05 +0000 | [diff] [blame] | 90 | AtomicString pstr = ustringToAtomicString(string); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 91 | Node* node = collection->namedItem(pstr); |
| 92 | while (node) { |
| 93 | if (!index) |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 94 | return JSValue::encode(toJS(exec, jsCollection->globalObject(), node)); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 95 | node = collection->nextNamedItem(pstr); |
| 96 | --index; |
| 97 | } |
| 98 | } |
| 99 | |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 100 | return JSValue::encode(jsUndefined()); |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | CallType JSHTMLAllCollection::getCallData(CallData& callData) |
| 104 | { |
| 105 | callData.native.function = callHTMLAllCollection; |
| 106 | return CallTypeHost; |
| 107 | } |
| 108 | |
| 109 | bool JSHTMLAllCollection::canGetItemsForName(ExecState*, HTMLAllCollection* collection, const Identifier& propertyName) |
| 110 | { |
| 111 | Vector<RefPtr<Node> > namedItems; |
barraclough@apple.com | 2b9b597 | 2010-04-17 00:10:39 +0000 | [diff] [blame] | 112 | collection->namedItems(identifierToAtomicString(propertyName), namedItems); |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 113 | return !namedItems.isEmpty(); |
| 114 | } |
| 115 | |
oliver@apple.com | 467b779 | 2010-03-02 08:20:48 +0000 | [diff] [blame] | 116 | JSValue JSHTMLAllCollection::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName) |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 117 | { |
oliver@apple.com | 467b779 | 2010-03-02 08:20:48 +0000 | [diff] [blame] | 118 | JSHTMLAllCollection* thisObj = static_cast<JSHTMLAllCollection*>(asObject(slotBase)); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 119 | return getNamedItems(exec, thisObj, propertyName); |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 122 | JSValue JSHTMLAllCollection::item(ExecState* exec) |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 123 | { |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 124 | bool ok; |
barraclough@apple.com | 794f461 | 2010-08-18 07:41:22 +0000 | [diff] [blame] | 125 | uint32_t index = Identifier::toUInt32(exec->argument(0).toString(exec), ok); |
weinig@apple.com | a11f543 | 2009-10-26 20:04:22 +0000 | [diff] [blame] | 126 | if (ok) |
| 127 | return toJS(exec, globalObject(), impl()->item(index)); |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 128 | return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec))); |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 129 | } |
| 130 | |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 131 | JSValue JSHTMLAllCollection::namedItem(ExecState* exec) |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 132 | { |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 133 | return getNamedItems(exec, this, Identifier(exec, exec->argument(0).toString(exec))); |
weinig@apple.com | 963305c | 2009-10-23 21:12:03 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace WebCore |