mshtml: Added IHTMLButtonElement::name tests.
[wine.git] / dlls / mshtml / htmlelemcol.c
blob102a438d20828853bdc9e97b8c345c62c9babaa5
1 /*
2 * Copyright 2006-2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 typedef struct {
35 DispatchEx dispex;
36 IHTMLElementCollection IHTMLElementCollection_iface;
38 HTMLElement **elems;
39 DWORD len;
41 LONG ref;
42 } HTMLElementCollection;
44 typedef struct {
45 HTMLElement **buf;
46 DWORD len;
47 DWORD size;
48 } elem_vector_t;
50 /* FIXME: Handle it better way */
51 static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
53 return CONTAINING_RECORD(iface, HTMLElement, node);
56 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len);
58 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
60 if(buf->len == buf->size) {
61 buf->size <<= 1;
62 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement*));
65 buf->buf[buf->len++] = elem;
68 static void elem_vector_normalize(elem_vector_t *buf)
70 if(!buf->len) {
71 heap_free(buf->buf);
72 buf->buf = NULL;
73 }else if(buf->size > buf->len) {
74 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement*));
77 buf->size = buf->len;
80 static inline BOOL is_elem_node(nsIDOMNode *node)
82 UINT16 type=0;
84 nsIDOMNode_GetNodeType(node, &type);
86 return type == ELEMENT_NODE || type == COMMENT_NODE;
89 static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
91 return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
94 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
95 REFIID riid, void **ppv)
97 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
99 *ppv = NULL;
101 if(IsEqualGUID(&IID_IUnknown, riid)) {
102 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
103 *ppv = &This->IHTMLElementCollection_iface;
104 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
105 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This, ppv);
106 *ppv = &This->IHTMLElementCollection_iface;
107 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
108 return *ppv ? S_OK : E_NOINTERFACE;
111 if(*ppv) {
112 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
113 return S_OK;
116 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
117 return E_NOINTERFACE;
120 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
122 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
123 LONG ref = InterlockedIncrement(&This->ref);
125 TRACE("(%p) ref=%d\n", This, ref);
127 return ref;
130 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
132 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
133 LONG ref = InterlockedDecrement(&This->ref);
135 TRACE("(%p) ref=%d\n", This, ref);
137 if(!ref) {
138 unsigned i;
140 for(i=0; i < This->len; i++)
141 node_release(&This->elems[i]->node);
142 heap_free(This->elems);
144 release_dispex(&This->dispex);
145 heap_free(This);
148 return ref;
151 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
152 UINT *pctinfo)
154 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
155 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
158 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
159 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
161 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
162 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
165 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
166 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
168 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
169 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
170 lcid, rgDispId);
173 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
174 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
175 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
177 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
178 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
179 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
182 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
183 BSTR *String)
185 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
186 FIXME("(%p)->(%p)\n", This, String);
187 return E_NOTIMPL;
190 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
191 LONG v)
193 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
194 FIXME("(%p)->(%d)\n", This, v);
195 return E_NOTIMPL;
198 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
199 LONG *p)
201 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
203 TRACE("(%p)->(%p)\n", This, p);
205 *p = This->len;
206 return S_OK;
209 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
210 IUnknown **p)
212 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
213 FIXME("(%p)->(%p)\n", This, p);
214 return E_NOTIMPL;
217 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
219 BSTR elem_id;
220 HRESULT hres;
222 hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
223 if(FAILED(hres)){
224 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
225 return FALSE;
228 if(elem_id && !strcmpW(elem_id, name)) {
229 SysFreeString(elem_id);
230 return TRUE;
233 SysFreeString(elem_id);
234 return FALSE;
237 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
239 const PRUnichar *str;
240 nsAString nsstr, nsname;
241 BOOL ret = FALSE;
242 nsresult nsres;
244 static const PRUnichar nameW[] = {'n','a','m','e',0};
246 if(!elem->nselem)
247 return FALSE;
249 nsAString_Init(&nsstr, NULL);
250 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
251 nsAString_GetData(&nsstr, &str);
252 if(!strcmpiW(str, name)) {
253 nsAString_Finish(&nsstr);
254 return TRUE;
257 nsAString_InitDepend(&nsname, nameW);
258 nsres = nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
259 nsAString_Finish(&nsname);
260 if(NS_SUCCEEDED(nsres)) {
261 nsAString_GetData(&nsstr, &str);
262 ret = !strcmpiW(str, name);
265 nsAString_Finish(&nsstr);
266 return ret;
269 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
271 if(idx < This->len) {
272 *ret = (IDispatch*)This->elems[idx];
273 IDispatch_AddRef(*ret);
276 return S_OK;
279 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
280 VARIANT name, VARIANT index, IDispatch **pdisp)
282 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
283 HRESULT hres = S_OK;
285 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
287 *pdisp = NULL;
289 switch(V_VT(&name)) {
290 case VT_I4:
291 if(V_I4(&name) < 0)
292 return E_INVALIDARG;
293 hres = get_item_idx(This, V_I4(&name), pdisp);
294 break;
296 case VT_UINT:
297 hres = get_item_idx(This, V_UINT(&name), pdisp);
298 break;
300 case VT_BSTR: {
301 DWORD i;
303 if(V_VT(&index) == VT_I4) {
304 LONG idx = V_I4(&index);
306 if(idx < 0)
307 return E_INVALIDARG;
309 for(i=0; i<This->len; i++) {
310 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
311 break;
314 if(i != This->len) {
315 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
316 IDispatch_AddRef(*pdisp);
318 }else {
319 elem_vector_t buf = {NULL, 0, 8};
321 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
323 for(i=0; i<This->len; i++) {
324 if(is_elem_name(This->elems[i], V_BSTR(&name))) {
325 node_addref(&This->elems[i]->node);
326 elem_vector_add(&buf, This->elems[i]);
330 if(buf.len > 1) {
331 elem_vector_normalize(&buf);
332 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
333 }else {
334 if(buf.len == 1) {
335 /* Already AddRef-ed */
336 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
339 heap_free(buf.buf);
342 break;
345 default:
346 FIXME("Unsupported name %s\n", debugstr_variant(&name));
347 hres = E_NOTIMPL;
350 if(SUCCEEDED(hres))
351 TRACE("returning %p\n", *pdisp);
352 return hres;
355 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
356 VARIANT tagName, IDispatch **pdisp)
358 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
359 DWORD i;
360 nsAString tag_str;
361 const PRUnichar *tag;
362 elem_vector_t buf = {NULL, 0, 8};
364 if(V_VT(&tagName) != VT_BSTR) {
365 WARN("Invalid arg\n");
366 return DISP_E_MEMBERNOTFOUND;
369 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
371 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
373 nsAString_Init(&tag_str, NULL);
375 for(i=0; i<This->len; i++) {
376 if(!This->elems[i]->nselem)
377 continue;
379 nsIDOMHTMLElement_GetTagName(This->elems[i]->nselem, &tag_str);
380 nsAString_GetData(&tag_str, &tag);
382 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
383 V_BSTR(&tagName), -1) == CSTR_EQUAL) {
384 node_addref(&This->elems[i]->node);
385 elem_vector_add(&buf, This->elems[i]);
389 nsAString_Finish(&tag_str);
390 elem_vector_normalize(&buf);
392 TRACE("fount %d tags\n", buf.len);
394 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
395 return S_OK;
398 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
399 HTMLElementCollection_QueryInterface,
400 HTMLElementCollection_AddRef,
401 HTMLElementCollection_Release,
402 HTMLElementCollection_GetTypeInfoCount,
403 HTMLElementCollection_GetTypeInfo,
404 HTMLElementCollection_GetIDsOfNames,
405 HTMLElementCollection_Invoke,
406 HTMLElementCollection_toString,
407 HTMLElementCollection_put_length,
408 HTMLElementCollection_get_length,
409 HTMLElementCollection_get__newEnum,
410 HTMLElementCollection_item,
411 HTMLElementCollection_tags
414 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
416 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
419 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
421 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
423 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
424 WCHAR *ptr;
425 DWORD idx=0;
427 if(!*name)
428 return DISP_E_UNKNOWNNAME;
430 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
431 idx = idx*10 + (*ptr-'0');
433 if(*ptr) {
434 /* the name contains alpha characters, so search by name & id */
435 for(idx = 0; idx < This->len; ++idx) {
436 if(is_elem_id(This->elems[idx], name) ||
437 is_elem_name(This->elems[idx], name))
438 break;
442 if(idx >= This->len)
443 return DISP_E_UNKNOWNNAME;
445 *dispid = DISPID_ELEMCOL_0 + idx;
446 TRACE("ret %x\n", *dispid);
447 return S_OK;
450 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
451 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
453 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
454 DWORD idx;
456 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
458 idx = id - DISPID_ELEMCOL_0;
459 if(idx >= This->len)
460 return DISP_E_UNKNOWNNAME;
462 switch(flags) {
463 case DISPATCH_PROPERTYGET:
464 V_VT(res) = VT_DISPATCH;
465 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
466 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
467 break;
468 default:
469 FIXME("unimplemented flags %x\n", flags);
470 return E_NOTIMPL;
473 return S_OK;
476 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
477 NULL,
478 HTMLElementCollection_get_dispid,
479 HTMLElementCollection_invoke,
480 NULL
483 static const tid_t HTMLElementCollection_iface_tids[] = {
484 IHTMLElementCollection_tid,
488 static dispex_static_data_t HTMLElementCollection_dispex = {
489 &HTMLElementColection_dispex_vtbl,
490 DispHTMLElementCollection_tid,
491 NULL,
492 HTMLElementCollection_iface_tids
495 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
497 nsIDOMNodeList *nsnode_list;
498 nsIDOMNode *iter;
499 UINT32 list_len = 0, i;
500 nsresult nsres;
501 HRESULT hres;
503 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
504 if(NS_FAILED(nsres)) {
505 ERR("GetChildNodes failed: %08x\n", nsres);
506 return;
509 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
510 if(!list_len)
511 return;
513 for(i=0; i<list_len; i++) {
514 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
515 if(NS_FAILED(nsres)) {
516 ERR("Item failed: %08x\n", nsres);
517 continue;
520 if(is_elem_node(iter)) {
521 HTMLDOMNode *node;
523 hres = get_node(doc, iter, TRUE, &node);
524 if(FAILED(hres)) {
525 FIXME("get_node failed: %08x\n", hres);
526 continue;
529 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
530 create_all_list(doc, node, buf);
535 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
537 elem_vector_t buf = {NULL, 0, 8};
539 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
541 if(include_root) {
542 node_addref(node);
543 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
545 create_all_list(node->doc, node, &buf);
546 elem_vector_normalize(&buf);
548 return HTMLElementCollection_Create(buf.buf, buf.len);
551 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
553 UINT32 length = 0, i;
554 HTMLDOMNode *node;
555 elem_vector_t buf;
556 HRESULT hres;
558 nsIDOMNodeList_GetLength(nslist, &length);
560 buf.len = 0;
561 buf.size = length;
562 if(length) {
563 nsIDOMNode *nsnode;
565 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
567 for(i=0; i<length; i++) {
568 nsIDOMNodeList_Item(nslist, i, &nsnode);
569 if(is_elem_node(nsnode)) {
570 hres = get_node(doc, nsnode, TRUE, &node);
571 if(FAILED(hres))
572 continue;
573 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
575 nsIDOMNode_Release(nsnode);
578 elem_vector_normalize(&buf);
579 }else {
580 buf.buf = NULL;
583 return HTMLElementCollection_Create(buf.buf, buf.len);
586 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, nsIDOMHTMLCollection *nscol)
588 UINT32 length = 0, i;
589 elem_vector_t buf;
590 HTMLDOMNode *node;
591 HRESULT hres = S_OK;
593 nsIDOMHTMLCollection_GetLength(nscol, &length);
595 buf.len = buf.size = length;
596 if(buf.len) {
597 nsIDOMNode *nsnode;
599 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
601 for(i=0; i<length; i++) {
602 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
603 hres = get_node(doc, nsnode, TRUE, &node);
604 nsIDOMNode_Release(nsnode);
605 if(FAILED(hres))
606 break;
607 buf.buf[i] = elem_from_HTMLDOMNode(node);
609 }else {
610 buf.buf = NULL;
613 if(FAILED(hres)) {
614 heap_free(buf.buf);
615 return NULL;
618 return HTMLElementCollection_Create(buf.buf, buf.len);
621 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len)
623 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
625 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
626 ret->ref = 1;
627 ret->elems = elems;
628 ret->len = len;
630 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
631 &HTMLElementCollection_dispex);
633 TRACE("ret=%p len=%d\n", ret, len);
635 return &ret->IHTMLElementCollection_iface;