mshtml: Properly release DispatchEx data.
[wine/multimedia.git] / dlls / mshtml / htmlelemcol.c
blob63fcb80544a061678bc35c825d5a22d4dafe36eb
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_name(HTMLElement *elem, LPCWSTR name)
210 const PRUnichar *str;
211 nsAString nsstr, nsname;
212 BOOL ret = FALSE;
213 nsresult nsres;
215 static const PRUnichar nameW[] = {'n','a','m','e',0};
217 if(!elem->nselem)
218 return FALSE;
220 nsAString_Init(&nsstr, NULL);
221 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
222 nsAString_GetData(&nsstr, &str);
223 if(!strcmpiW(str, name)) {
224 nsAString_Finish(&nsstr);
225 return TRUE;
228 nsAString_Init(&nsname, nameW);
229 nsres = nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
230 nsAString_Finish(&nsname);
231 if(NS_SUCCEEDED(nsres)) {
232 nsAString_GetData(&nsstr, &str);
233 ret = !strcmpiW(str, name);
236 nsAString_Finish(&nsstr);
237 return ret;
240 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
242 if(idx < This->len) {
243 *ret = (IDispatch*)This->elems[idx];
244 IDispatch_AddRef(*ret);
247 return S_OK;
250 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
251 VARIANT name, VARIANT index, IDispatch **pdisp)
253 HTMLElementCollection *This = ELEMCOL_THIS(iface);
254 HRESULT hres = S_OK;
256 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
258 *pdisp = NULL;
260 switch(V_VT(&name)) {
261 case VT_I4:
262 if(V_I4(&name) < 0)
263 return E_INVALIDARG;
264 hres = get_item_idx(This, V_I4(&name), pdisp);
265 break;
267 case VT_UINT:
268 hres = get_item_idx(This, V_UINT(&name), pdisp);
269 break;
271 case VT_BSTR: {
272 DWORD i;
274 if(V_VT(&index) == VT_I4) {
275 LONG idx = V_I4(&index);
277 if(idx < 0)
278 return E_INVALIDARG;
280 for(i=0; i<This->len; i++) {
281 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
282 break;
285 if(i != This->len) {
286 *pdisp = (IDispatch*)HTMLELEM(This->elems[i]);
287 IDispatch_AddRef(*pdisp);
289 }else {
290 elem_vector_t buf = {NULL, 0, 8};
292 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
294 for(i=0; i<This->len; i++) {
295 if(is_elem_name(This->elems[i], V_BSTR(&name)))
296 elem_vector_add(&buf, This->elems[i]);
299 if(buf.len > 1) {
300 elem_vector_normalize(&buf);
301 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
302 }else {
303 if(buf.len == 1) {
304 *pdisp = (IDispatch*)HTMLELEM(buf.buf[0]);
305 IDispatch_AddRef(*pdisp);
308 heap_free(buf.buf);
311 break;
314 default:
315 FIXME("Unsupported name %s\n", debugstr_variant(&name));
316 hres = E_NOTIMPL;
319 if(SUCCEEDED(hres))
320 TRACE("returning %p\n", *pdisp);
321 return hres;
324 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
325 VARIANT tagName, IDispatch **pdisp)
327 HTMLElementCollection *This = ELEMCOL_THIS(iface);
328 DWORD i;
329 nsAString tag_str;
330 const PRUnichar *tag;
331 elem_vector_t buf = {NULL, 0, 8};
333 if(V_VT(&tagName) != VT_BSTR) {
334 WARN("Invalid arg\n");
335 return DISP_E_MEMBERNOTFOUND;
338 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
340 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
342 nsAString_Init(&tag_str, NULL);
344 for(i=0; i<This->len; i++) {
345 if(!This->elems[i]->nselem)
346 continue;
348 nsIDOMElement_GetTagName(This->elems[i]->nselem, &tag_str);
349 nsAString_GetData(&tag_str, &tag);
351 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
352 V_BSTR(&tagName), -1) == CSTR_EQUAL)
353 elem_vector_add(&buf, This->elems[i]);
356 nsAString_Finish(&tag_str);
357 elem_vector_normalize(&buf);
359 TRACE("fount %d tags\n", buf.len);
361 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
362 return S_OK;
365 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
367 static HRESULT HTMLElementCollection_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
369 HTMLElementCollection *This = ELEMCOL_THIS(iface);
370 WCHAR *ptr;
371 DWORD idx=0;
373 if(!*name)
374 return DISP_E_UNKNOWNNAME;
376 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
377 idx = idx*10 + (*ptr-'0');
379 if(*ptr || idx >= This->len)
380 return DISP_E_UNKNOWNNAME;
382 *dispid = DISPID_ELEMCOL_0 + idx;
383 TRACE("ret %x\n", *dispid);
384 return S_OK;
387 static HRESULT HTMLElementCollection_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
388 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
390 HTMLElementCollection *This = ELEMCOL_THIS(iface);
391 DWORD idx;
393 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
395 idx = id - DISPID_ELEMCOL_0;
396 if(idx >= This->len)
397 return DISP_E_UNKNOWNNAME;
399 switch(flags) {
400 case INVOKE_PROPERTYGET:
401 V_VT(res) = VT_DISPATCH;
402 V_DISPATCH(res) = (IDispatch*)HTMLELEM(This->elems[idx]);
403 IHTMLElement_AddRef(HTMLELEM(This->elems[idx]));
404 break;
405 default:
406 FIXME("unimplemented flags %x\n", flags);
407 return E_NOTIMPL;
410 return S_OK;
413 #undef ELEMCOL_THIS
415 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
416 HTMLElementCollection_QueryInterface,
417 HTMLElementCollection_AddRef,
418 HTMLElementCollection_Release,
419 HTMLElementCollection_GetTypeInfoCount,
420 HTMLElementCollection_GetTypeInfo,
421 HTMLElementCollection_GetIDsOfNames,
422 HTMLElementCollection_Invoke,
423 HTMLElementCollection_toString,
424 HTMLElementCollection_put_length,
425 HTMLElementCollection_get_length,
426 HTMLElementCollection_get__newEnum,
427 HTMLElementCollection_item,
428 HTMLElementCollection_tags
431 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
432 NULL,
433 HTMLElementCollection_get_dispid,
434 HTMLElementCollection_invoke
437 static const tid_t HTMLElementCollection_iface_tids[] = {
438 IHTMLElementCollection_tid,
441 static dispex_static_data_t HTMLElementCollection_dispex = {
442 &HTMLElementColection_dispex_vtbl,
443 DispHTMLElementCollection_tid,
444 NULL,
445 HTMLElementCollection_iface_tids
448 static void create_all_list(HTMLDocument *doc, HTMLDOMNode *elem, elem_vector_t *buf)
450 nsIDOMNodeList *nsnode_list;
451 nsIDOMNode *iter;
452 PRUint32 list_len = 0, i;
453 nsresult nsres;
455 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
456 if(NS_FAILED(nsres)) {
457 ERR("GetChildNodes failed: %08x\n", nsres);
458 return;
461 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
462 if(!list_len)
463 return;
465 for(i=0; i<list_len; i++) {
466 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
467 if(NS_FAILED(nsres)) {
468 ERR("Item failed: %08x\n", nsres);
469 continue;
472 if(is_elem_node(iter)) {
473 HTMLDOMNode *node = get_node(doc, iter, TRUE);
475 elem_vector_add(buf, HTMLELEM_NODE_THIS(node));
476 create_all_list(doc, node, buf);
481 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
483 elem_vector_t buf = {NULL, 0, 8};
485 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement**));
487 if(include_root)
488 elem_vector_add(&buf, HTMLELEM_NODE_THIS(node));
489 create_all_list(node->doc, node, &buf);
490 elem_vector_normalize(&buf);
492 return HTMLElementCollection_Create((IUnknown*)HTMLDOMNODE(node), buf.buf, buf.len);
495 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocument *doc, IUnknown *unk, nsIDOMNodeList *nslist)
497 PRUint32 length = 0, i;
498 elem_vector_t buf;
500 nsIDOMNodeList_GetLength(nslist, &length);
502 buf.len = 0;
503 buf.size = length;
504 if(length) {
505 nsIDOMNode *nsnode;
507 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
509 for(i=0; i<length; i++) {
510 nsIDOMNodeList_Item(nslist, i, &nsnode);
511 if(is_elem_node(nsnode))
512 buf.buf[buf.len++] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
513 nsIDOMNode_Release(nsnode);
516 elem_vector_normalize(&buf);
517 }else {
518 buf.buf = NULL;
521 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
524 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocument *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
526 PRUint32 length = 0, i;
527 elem_vector_t buf;
529 nsIDOMHTMLCollection_GetLength(nscol, &length);
531 buf.len = buf.size = length;
532 if(buf.len) {
533 nsIDOMNode *nsnode;
535 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
537 for(i=0; i<length; i++) {
538 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
539 buf.buf[i] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
540 nsIDOMNode_Release(nsnode);
542 }else {
543 buf.buf = NULL;
546 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
549 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
550 HTMLElement **elems, DWORD len)
552 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
554 ret->lpHTMLElementCollectionVtbl = &HTMLElementCollectionVtbl;
555 ret->ref = 1;
556 ret->elems = elems;
557 ret->len = len;
559 init_dispex(&ret->dispex, (IUnknown*)HTMLELEMCOL(ret), &HTMLElementCollection_dispex);
561 IUnknown_AddRef(ref_unk);
562 ret->ref_unk = ref_unk;
564 TRACE("ret=%p len=%d\n", ret, len);
566 return HTMLELEMCOL(ret);