d3d9: Remove old debug messages from the tests.
[wine/multimedia.git] / dlls / mshtml / htmlelemcol.c
blob9c0fedde8d00eb6d4954c392c5b95d9a745b1f2f
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 const IHTMLElementCollectionVtbl *lpHTMLElementCollectionVtbl;
38 IUnknown *ref_unk;
39 HTMLElement **elems;
40 DWORD len;
42 LONG ref;
43 } HTMLElementCollection;
45 #define HTMLELEMCOL(x) ((IHTMLElementCollection*) &(x)->lpHTMLElementCollectionVtbl)
47 typedef struct {
48 HTMLElement **buf;
49 DWORD len;
50 DWORD size;
51 } elem_vector_t;
53 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
54 HTMLElement **elems, DWORD len);
56 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
58 if(buf->len == buf->size) {
59 buf->size <<= 1;
60 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement**));
63 buf->buf[buf->len++] = elem;
66 static void elem_vector_normalize(elem_vector_t *buf)
68 if(!buf->len) {
69 heap_free(buf->buf);
70 buf->buf = NULL;
71 }else if(buf->size > buf->len) {
72 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement**));
75 buf->size = buf->len;
78 static inline BOOL is_elem_node(nsIDOMNode *node)
80 PRUint16 type=0;
82 nsIDOMNode_GetNodeType(node, &type);
84 return type == ELEMENT_NODE || type == COMMENT_NODE;
87 #define ELEMCOL_THIS(iface) DEFINE_THIS(HTMLElementCollection, HTMLElementCollection, iface)
88 #define HTMLELEM_NODE_THIS(iface) DEFINE_THIS2(HTMLElement, node, iface)
90 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
91 REFIID riid, void **ppv)
93 HTMLElementCollection *This = ELEMCOL_THIS(iface);
95 *ppv = NULL;
97 if(IsEqualGUID(&IID_IUnknown, riid)) {
98 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
99 *ppv = HTMLELEMCOL(This);
100 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
101 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This, ppv);
102 *ppv = HTMLELEMCOL(This);
103 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
104 return *ppv ? S_OK : E_NOINTERFACE;
107 if(*ppv) {
108 IHTMLElementCollection_AddRef(HTMLELEMCOL(This));
109 return S_OK;
112 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
113 return E_NOINTERFACE;
116 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
118 HTMLElementCollection *This = ELEMCOL_THIS(iface);
119 LONG ref = InterlockedIncrement(&This->ref);
121 TRACE("(%p) ref=%d\n", This, ref);
123 return ref;
126 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
128 HTMLElementCollection *This = ELEMCOL_THIS(iface);
129 LONG ref = InterlockedDecrement(&This->ref);
131 TRACE("(%p) ref=%d\n", This, ref);
133 if(!ref) {
134 IUnknown_Release(This->ref_unk);
135 release_dispex(&This->dispex);
136 heap_free(This->elems);
137 heap_free(This);
140 return ref;
143 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
144 UINT *pctinfo)
146 HTMLElementCollection *This = ELEMCOL_THIS(iface);
147 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
150 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
151 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
153 HTMLElementCollection *This = ELEMCOL_THIS(iface);
154 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
157 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
158 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
160 HTMLElementCollection *This = ELEMCOL_THIS(iface);
161 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
164 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
165 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
166 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
168 HTMLElementCollection *This = ELEMCOL_THIS(iface);
169 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
170 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
173 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
174 BSTR *String)
176 HTMLElementCollection *This = ELEMCOL_THIS(iface);
177 FIXME("(%p)->(%p)\n", This, String);
178 return E_NOTIMPL;
181 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
182 LONG v)
184 HTMLElementCollection *This = ELEMCOL_THIS(iface);
185 FIXME("(%p)->(%d)\n", This, v);
186 return E_NOTIMPL;
189 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
190 LONG *p)
192 HTMLElementCollection *This = ELEMCOL_THIS(iface);
194 TRACE("(%p)->(%p)\n", This, p);
196 *p = This->len;
197 return S_OK;
200 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
201 IUnknown **p)
203 HTMLElementCollection *This = ELEMCOL_THIS(iface);
204 FIXME("(%p)->(%p)\n", This, p);
205 return E_NOTIMPL;
208 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
210 BSTR elem_id;
211 HRESULT hres;
213 hres = IHTMLElement_get_id(HTMLELEM(elem), &elem_id);
214 if(FAILED(hres)){
215 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
216 return FALSE;
219 if(elem_id && !strcmpW(elem_id, name)) {
220 SysFreeString(elem_id);
221 return TRUE;
224 SysFreeString(elem_id);
225 return FALSE;
228 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
230 const PRUnichar *str;
231 nsAString nsstr, nsname;
232 BOOL ret = FALSE;
233 nsresult nsres;
235 static const PRUnichar nameW[] = {'n','a','m','e',0};
237 if(!elem->nselem)
238 return FALSE;
240 nsAString_Init(&nsstr, NULL);
241 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
242 nsAString_GetData(&nsstr, &str);
243 if(!strcmpiW(str, name)) {
244 nsAString_Finish(&nsstr);
245 return TRUE;
248 nsAString_InitDepend(&nsname, nameW);
249 nsres = nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
250 nsAString_Finish(&nsname);
251 if(NS_SUCCEEDED(nsres)) {
252 nsAString_GetData(&nsstr, &str);
253 ret = !strcmpiW(str, name);
256 nsAString_Finish(&nsstr);
257 return ret;
260 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
262 if(idx < This->len) {
263 *ret = (IDispatch*)This->elems[idx];
264 IDispatch_AddRef(*ret);
267 return S_OK;
270 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
271 VARIANT name, VARIANT index, IDispatch **pdisp)
273 HTMLElementCollection *This = ELEMCOL_THIS(iface);
274 HRESULT hres = S_OK;
276 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
278 *pdisp = NULL;
280 switch(V_VT(&name)) {
281 case VT_I4:
282 if(V_I4(&name) < 0)
283 return E_INVALIDARG;
284 hres = get_item_idx(This, V_I4(&name), pdisp);
285 break;
287 case VT_UINT:
288 hres = get_item_idx(This, V_UINT(&name), pdisp);
289 break;
291 case VT_BSTR: {
292 DWORD i;
294 if(V_VT(&index) == VT_I4) {
295 LONG idx = V_I4(&index);
297 if(idx < 0)
298 return E_INVALIDARG;
300 for(i=0; i<This->len; i++) {
301 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
302 break;
305 if(i != This->len) {
306 *pdisp = (IDispatch*)HTMLELEM(This->elems[i]);
307 IDispatch_AddRef(*pdisp);
309 }else {
310 elem_vector_t buf = {NULL, 0, 8};
312 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
314 for(i=0; i<This->len; i++) {
315 if(is_elem_name(This->elems[i], V_BSTR(&name)))
316 elem_vector_add(&buf, This->elems[i]);
319 if(buf.len > 1) {
320 elem_vector_normalize(&buf);
321 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
322 }else {
323 if(buf.len == 1) {
324 *pdisp = (IDispatch*)HTMLELEM(buf.buf[0]);
325 IDispatch_AddRef(*pdisp);
328 heap_free(buf.buf);
331 break;
334 default:
335 FIXME("Unsupported name %s\n", debugstr_variant(&name));
336 hres = E_NOTIMPL;
339 if(SUCCEEDED(hres))
340 TRACE("returning %p\n", *pdisp);
341 return hres;
344 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
345 VARIANT tagName, IDispatch **pdisp)
347 HTMLElementCollection *This = ELEMCOL_THIS(iface);
348 DWORD i;
349 nsAString tag_str;
350 const PRUnichar *tag;
351 elem_vector_t buf = {NULL, 0, 8};
353 if(V_VT(&tagName) != VT_BSTR) {
354 WARN("Invalid arg\n");
355 return DISP_E_MEMBERNOTFOUND;
358 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
360 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
362 nsAString_Init(&tag_str, NULL);
364 for(i=0; i<This->len; i++) {
365 if(!This->elems[i]->nselem)
366 continue;
368 nsIDOMElement_GetTagName(This->elems[i]->nselem, &tag_str);
369 nsAString_GetData(&tag_str, &tag);
371 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
372 V_BSTR(&tagName), -1) == CSTR_EQUAL)
373 elem_vector_add(&buf, This->elems[i]);
376 nsAString_Finish(&tag_str);
377 elem_vector_normalize(&buf);
379 TRACE("fount %d tags\n", buf.len);
381 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
382 return S_OK;
385 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
387 static HRESULT HTMLElementCollection_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
389 HTMLElementCollection *This = ELEMCOL_THIS(iface);
390 WCHAR *ptr;
391 DWORD idx=0;
393 if(!*name)
394 return DISP_E_UNKNOWNNAME;
396 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
397 idx = idx*10 + (*ptr-'0');
399 if(*ptr) {
400 /* the name contains alpha characters, so search by name & id */
401 for(idx = 0; idx < This->len; ++idx) {
402 if(is_elem_id(This->elems[idx], name) ||
403 is_elem_name(This->elems[idx], name))
404 break;
408 if(idx >= This->len)
409 return DISP_E_UNKNOWNNAME;
411 *dispid = DISPID_ELEMCOL_0 + idx;
412 TRACE("ret %x\n", *dispid);
413 return S_OK;
416 static HRESULT HTMLElementCollection_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
417 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
419 HTMLElementCollection *This = ELEMCOL_THIS(iface);
420 DWORD idx;
422 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
424 idx = id - DISPID_ELEMCOL_0;
425 if(idx >= This->len)
426 return DISP_E_UNKNOWNNAME;
428 switch(flags) {
429 case DISPATCH_PROPERTYGET:
430 V_VT(res) = VT_DISPATCH;
431 V_DISPATCH(res) = (IDispatch*)HTMLELEM(This->elems[idx]);
432 IHTMLElement_AddRef(HTMLELEM(This->elems[idx]));
433 break;
434 default:
435 FIXME("unimplemented flags %x\n", flags);
436 return E_NOTIMPL;
439 return S_OK;
442 #undef ELEMCOL_THIS
444 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
445 HTMLElementCollection_QueryInterface,
446 HTMLElementCollection_AddRef,
447 HTMLElementCollection_Release,
448 HTMLElementCollection_GetTypeInfoCount,
449 HTMLElementCollection_GetTypeInfo,
450 HTMLElementCollection_GetIDsOfNames,
451 HTMLElementCollection_Invoke,
452 HTMLElementCollection_toString,
453 HTMLElementCollection_put_length,
454 HTMLElementCollection_get_length,
455 HTMLElementCollection_get__newEnum,
456 HTMLElementCollection_item,
457 HTMLElementCollection_tags
460 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
461 NULL,
462 HTMLElementCollection_get_dispid,
463 HTMLElementCollection_invoke
466 static const tid_t HTMLElementCollection_iface_tids[] = {
467 IHTMLElementCollection_tid,
470 static dispex_static_data_t HTMLElementCollection_dispex = {
471 &HTMLElementColection_dispex_vtbl,
472 DispHTMLElementCollection_tid,
473 NULL,
474 HTMLElementCollection_iface_tids
477 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
479 nsIDOMNodeList *nsnode_list;
480 nsIDOMNode *iter;
481 PRUint32 list_len = 0, i;
482 nsresult nsres;
484 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
485 if(NS_FAILED(nsres)) {
486 ERR("GetChildNodes failed: %08x\n", nsres);
487 return;
490 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
491 if(!list_len)
492 return;
494 for(i=0; i<list_len; i++) {
495 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
496 if(NS_FAILED(nsres)) {
497 ERR("Item failed: %08x\n", nsres);
498 continue;
501 if(is_elem_node(iter)) {
502 HTMLDOMNode *node = get_node(doc, iter, TRUE);
504 elem_vector_add(buf, HTMLELEM_NODE_THIS(node));
505 create_all_list(doc, node, buf);
510 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
512 elem_vector_t buf = {NULL, 0, 8};
514 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement**));
516 if(include_root)
517 elem_vector_add(&buf, HTMLELEM_NODE_THIS(node));
518 create_all_list(node->doc, node, &buf);
519 elem_vector_normalize(&buf);
521 return HTMLElementCollection_Create((IUnknown*)HTMLDOMNODE(node), buf.buf, buf.len);
524 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMNodeList *nslist)
526 PRUint32 length = 0, i;
527 elem_vector_t buf;
529 nsIDOMNodeList_GetLength(nslist, &length);
531 buf.len = 0;
532 buf.size = length;
533 if(length) {
534 nsIDOMNode *nsnode;
536 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
538 for(i=0; i<length; i++) {
539 nsIDOMNodeList_Item(nslist, i, &nsnode);
540 if(is_elem_node(nsnode))
541 buf.buf[buf.len++] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
542 nsIDOMNode_Release(nsnode);
545 elem_vector_normalize(&buf);
546 }else {
547 buf.buf = NULL;
550 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
553 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
555 PRUint32 length = 0, i;
556 elem_vector_t buf;
558 nsIDOMHTMLCollection_GetLength(nscol, &length);
560 buf.len = buf.size = length;
561 if(buf.len) {
562 nsIDOMNode *nsnode;
564 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
566 for(i=0; i<length; i++) {
567 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
568 buf.buf[i] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
569 nsIDOMNode_Release(nsnode);
571 }else {
572 buf.buf = NULL;
575 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
578 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
579 HTMLElement **elems, DWORD len)
581 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
583 ret->lpHTMLElementCollectionVtbl = &HTMLElementCollectionVtbl;
584 ret->ref = 1;
585 ret->elems = elems;
586 ret->len = len;
588 init_dispex(&ret->dispex, (IUnknown*)HTMLELEMCOL(ret), &HTMLElementCollection_dispex);
590 IUnknown_AddRef(ref_unk);
591 ret->ref_unk = ref_unk;
593 TRACE("ret=%p len=%d\n", ret, len);
595 return HTMLELEMCOL(ret);