po: Update Russian translation.
[wine/multimedia.git] / dlls / mshtml / htmlelemcol.c
blobc87485e61d6b4d7bf180d3e9b26e1f7fbf5591e5
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 IUnknown *ref_unk;
39 HTMLElement **elems;
40 DWORD len;
42 LONG ref;
43 } HTMLElementCollection;
45 typedef struct {
46 HTMLElement **buf;
47 DWORD len;
48 DWORD size;
49 } elem_vector_t;
51 /* FIXME: Handle it better way */
52 static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
54 return CONTAINING_RECORD(iface, HTMLElement, node);
57 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
58 HTMLElement **elems, DWORD len);
60 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
62 if(buf->len == buf->size) {
63 buf->size <<= 1;
64 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement**));
67 buf->buf[buf->len++] = elem;
70 static void elem_vector_normalize(elem_vector_t *buf)
72 if(!buf->len) {
73 heap_free(buf->buf);
74 buf->buf = NULL;
75 }else if(buf->size > buf->len) {
76 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement**));
79 buf->size = buf->len;
82 static inline BOOL is_elem_node(nsIDOMNode *node)
84 PRUint16 type=0;
86 nsIDOMNode_GetNodeType(node, &type);
88 return type == ELEMENT_NODE || type == COMMENT_NODE;
91 static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
93 return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
96 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
97 REFIID riid, void **ppv)
99 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
101 *ppv = NULL;
103 if(IsEqualGUID(&IID_IUnknown, riid)) {
104 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
105 *ppv = &This->IHTMLElementCollection_iface;
106 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
107 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This, ppv);
108 *ppv = &This->IHTMLElementCollection_iface;
109 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
110 return *ppv ? S_OK : E_NOINTERFACE;
113 if(*ppv) {
114 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
115 return S_OK;
118 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
119 return E_NOINTERFACE;
122 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
124 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
125 LONG ref = InterlockedIncrement(&This->ref);
127 TRACE("(%p) ref=%d\n", This, ref);
129 return ref;
132 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
134 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
135 LONG ref = InterlockedDecrement(&This->ref);
137 TRACE("(%p) ref=%d\n", This, ref);
139 if(!ref) {
140 IUnknown_Release(This->ref_unk);
141 release_dispex(&This->dispex);
142 heap_free(This->elems);
143 heap_free(This);
146 return ref;
149 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
150 UINT *pctinfo)
152 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
153 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
156 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
157 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
159 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
160 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
163 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
164 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
166 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
167 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
168 lcid, rgDispId);
171 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
172 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
173 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
175 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
176 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
177 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
180 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
181 BSTR *String)
183 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
184 FIXME("(%p)->(%p)\n", This, String);
185 return E_NOTIMPL;
188 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
189 LONG v)
191 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
192 FIXME("(%p)->(%d)\n", This, v);
193 return E_NOTIMPL;
196 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
197 LONG *p)
199 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
201 TRACE("(%p)->(%p)\n", This, p);
203 *p = This->len;
204 return S_OK;
207 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
208 IUnknown **p)
210 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
211 FIXME("(%p)->(%p)\n", This, p);
212 return E_NOTIMPL;
215 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
217 BSTR elem_id;
218 HRESULT hres;
220 hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
221 if(FAILED(hres)){
222 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
223 return FALSE;
226 if(elem_id && !strcmpW(elem_id, name)) {
227 SysFreeString(elem_id);
228 return TRUE;
231 SysFreeString(elem_id);
232 return FALSE;
235 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
237 const PRUnichar *str;
238 nsAString nsstr, nsname;
239 BOOL ret = FALSE;
240 nsresult nsres;
242 static const PRUnichar nameW[] = {'n','a','m','e',0};
244 if(!elem->nselem)
245 return FALSE;
247 nsAString_Init(&nsstr, NULL);
248 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
249 nsAString_GetData(&nsstr, &str);
250 if(!strcmpiW(str, name)) {
251 nsAString_Finish(&nsstr);
252 return TRUE;
255 nsAString_InitDepend(&nsname, nameW);
256 nsres = nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
257 nsAString_Finish(&nsname);
258 if(NS_SUCCEEDED(nsres)) {
259 nsAString_GetData(&nsstr, &str);
260 ret = !strcmpiW(str, name);
263 nsAString_Finish(&nsstr);
264 return ret;
267 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
269 if(idx < This->len) {
270 *ret = (IDispatch*)This->elems[idx];
271 IDispatch_AddRef(*ret);
274 return S_OK;
277 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
278 VARIANT name, VARIANT index, IDispatch **pdisp)
280 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
281 HRESULT hres = S_OK;
283 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
285 *pdisp = NULL;
287 switch(V_VT(&name)) {
288 case VT_I4:
289 if(V_I4(&name) < 0)
290 return E_INVALIDARG;
291 hres = get_item_idx(This, V_I4(&name), pdisp);
292 break;
294 case VT_UINT:
295 hres = get_item_idx(This, V_UINT(&name), pdisp);
296 break;
298 case VT_BSTR: {
299 DWORD i;
301 if(V_VT(&index) == VT_I4) {
302 LONG idx = V_I4(&index);
304 if(idx < 0)
305 return E_INVALIDARG;
307 for(i=0; i<This->len; i++) {
308 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
309 break;
312 if(i != This->len) {
313 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
314 IDispatch_AddRef(*pdisp);
316 }else {
317 elem_vector_t buf = {NULL, 0, 8};
319 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
321 for(i=0; i<This->len; i++) {
322 if(is_elem_name(This->elems[i], V_BSTR(&name)))
323 elem_vector_add(&buf, This->elems[i]);
326 if(buf.len > 1) {
327 elem_vector_normalize(&buf);
328 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
329 }else {
330 if(buf.len == 1) {
331 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
332 IDispatch_AddRef(*pdisp);
335 heap_free(buf.buf);
338 break;
341 default:
342 FIXME("Unsupported name %s\n", debugstr_variant(&name));
343 hres = E_NOTIMPL;
346 if(SUCCEEDED(hres))
347 TRACE("returning %p\n", *pdisp);
348 return hres;
351 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
352 VARIANT tagName, IDispatch **pdisp)
354 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
355 DWORD i;
356 nsAString tag_str;
357 const PRUnichar *tag;
358 elem_vector_t buf = {NULL, 0, 8};
360 if(V_VT(&tagName) != VT_BSTR) {
361 WARN("Invalid arg\n");
362 return DISP_E_MEMBERNOTFOUND;
365 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
367 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
369 nsAString_Init(&tag_str, NULL);
371 for(i=0; i<This->len; i++) {
372 if(!This->elems[i]->nselem)
373 continue;
375 nsIDOMElement_GetTagName(This->elems[i]->nselem, &tag_str);
376 nsAString_GetData(&tag_str, &tag);
378 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
379 V_BSTR(&tagName), -1) == CSTR_EQUAL)
380 elem_vector_add(&buf, This->elems[i]);
383 nsAString_Finish(&tag_str);
384 elem_vector_normalize(&buf);
386 TRACE("fount %d tags\n", buf.len);
388 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
389 return S_OK;
392 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
393 HTMLElementCollection_QueryInterface,
394 HTMLElementCollection_AddRef,
395 HTMLElementCollection_Release,
396 HTMLElementCollection_GetTypeInfoCount,
397 HTMLElementCollection_GetTypeInfo,
398 HTMLElementCollection_GetIDsOfNames,
399 HTMLElementCollection_Invoke,
400 HTMLElementCollection_toString,
401 HTMLElementCollection_put_length,
402 HTMLElementCollection_get_length,
403 HTMLElementCollection_get__newEnum,
404 HTMLElementCollection_item,
405 HTMLElementCollection_tags
408 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
410 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
413 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
415 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
417 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
418 WCHAR *ptr;
419 DWORD idx=0;
421 if(!*name)
422 return DISP_E_UNKNOWNNAME;
424 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
425 idx = idx*10 + (*ptr-'0');
427 if(*ptr) {
428 /* the name contains alpha characters, so search by name & id */
429 for(idx = 0; idx < This->len; ++idx) {
430 if(is_elem_id(This->elems[idx], name) ||
431 is_elem_name(This->elems[idx], name))
432 break;
436 if(idx >= This->len)
437 return DISP_E_UNKNOWNNAME;
439 *dispid = DISPID_ELEMCOL_0 + idx;
440 TRACE("ret %x\n", *dispid);
441 return S_OK;
444 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
445 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
447 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
448 DWORD idx;
450 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
452 idx = id - DISPID_ELEMCOL_0;
453 if(idx >= This->len)
454 return DISP_E_UNKNOWNNAME;
456 switch(flags) {
457 case DISPATCH_PROPERTYGET:
458 V_VT(res) = VT_DISPATCH;
459 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
460 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
461 break;
462 default:
463 FIXME("unimplemented flags %x\n", flags);
464 return E_NOTIMPL;
467 return S_OK;
470 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
471 NULL,
472 HTMLElementCollection_get_dispid,
473 HTMLElementCollection_invoke
476 static const tid_t HTMLElementCollection_iface_tids[] = {
477 IHTMLElementCollection_tid,
481 static dispex_static_data_t HTMLElementCollection_dispex = {
482 &HTMLElementColection_dispex_vtbl,
483 DispHTMLElementCollection_tid,
484 NULL,
485 HTMLElementCollection_iface_tids
488 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
490 nsIDOMNodeList *nsnode_list;
491 nsIDOMNode *iter;
492 PRUint32 list_len = 0, i;
493 nsresult nsres;
494 HRESULT hres;
496 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
497 if(NS_FAILED(nsres)) {
498 ERR("GetChildNodes failed: %08x\n", nsres);
499 return;
502 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
503 if(!list_len)
504 return;
506 for(i=0; i<list_len; i++) {
507 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
508 if(NS_FAILED(nsres)) {
509 ERR("Item failed: %08x\n", nsres);
510 continue;
513 if(is_elem_node(iter)) {
514 HTMLDOMNode *node;
516 hres = get_node(doc, iter, TRUE, &node);
517 if(FAILED(hres)) {
518 FIXME("get_node failed: %08x\n", hres);
519 continue;
522 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
523 create_all_list(doc, node, buf);
528 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
530 elem_vector_t buf = {NULL, 0, 8};
532 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement**));
534 if(include_root)
535 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
536 create_all_list(node->doc, node, &buf);
537 elem_vector_normalize(&buf);
539 return HTMLElementCollection_Create((IUnknown*)&node->IHTMLDOMNode_iface, buf.buf, buf.len);
542 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMNodeList *nslist)
544 PRUint32 length = 0, i;
545 HTMLDOMNode *node;
546 elem_vector_t buf;
547 HRESULT hres;
549 nsIDOMNodeList_GetLength(nslist, &length);
551 buf.len = 0;
552 buf.size = length;
553 if(length) {
554 nsIDOMNode *nsnode;
556 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
558 for(i=0; i<length; i++) {
559 nsIDOMNodeList_Item(nslist, i, &nsnode);
560 if(is_elem_node(nsnode)) {
561 hres = get_node(doc, nsnode, TRUE, &node);
562 if(FAILED(hres))
563 continue;
564 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
566 nsIDOMNode_Release(nsnode);
569 elem_vector_normalize(&buf);
570 }else {
571 buf.buf = NULL;
574 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
577 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
579 PRUint32 length = 0, i;
580 elem_vector_t buf;
581 HTMLDOMNode *node;
582 HRESULT hres = S_OK;
584 nsIDOMHTMLCollection_GetLength(nscol, &length);
586 buf.len = buf.size = length;
587 if(buf.len) {
588 nsIDOMNode *nsnode;
590 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
592 for(i=0; i<length; i++) {
593 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
594 hres = get_node(doc, nsnode, TRUE, &node);
595 nsIDOMNode_Release(nsnode);
596 if(FAILED(hres))
597 break;
598 buf.buf[i] = elem_from_HTMLDOMNode(node);
600 }else {
601 buf.buf = NULL;
604 if(FAILED(hres)) {
605 heap_free(buf.buf);
606 return NULL;
609 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
612 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
613 HTMLElement **elems, DWORD len)
615 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
617 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
618 ret->ref = 1;
619 ret->elems = elems;
620 ret->len = len;
622 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
623 &HTMLElementCollection_dispex);
625 IUnknown_AddRef(ref_unk);
626 ret->ref_unk = ref_unk;
628 TRACE("ret=%p len=%d\n", ret, len);
630 return &ret->IHTMLElementCollection_iface;