mshtml: Implement OmNavigator_get_appName.
[wine] / dlls / mshtml / htmlelemcol.c
1 /*
2  * Copyright 2006-2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29
30 #include "mshtml_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 typedef struct {
35     DispatchEx dispex;
36     const IHTMLElementCollectionVtbl *lpHTMLElementCollectionVtbl;
37
38     IUnknown *ref_unk;
39     HTMLElement **elems;
40     DWORD len;
41
42     LONG ref;
43 } HTMLElementCollection;
44
45 #define HTMLELEMCOL(x)  ((IHTMLElementCollection*) &(x)->lpHTMLElementCollectionVtbl)
46
47 typedef struct {
48     HTMLElement **buf;
49     DWORD len;
50     DWORD size;
51 } elem_vector_t;
52
53 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
54                                                             HTMLElement **elems, DWORD len);
55
56 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
57 {
58     if(buf->len == buf->size) {
59         buf->size <<= 1;
60         buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement**));
61     }
62
63     buf->buf[buf->len++] = elem;
64 }
65
66 static void elem_vector_normalize(elem_vector_t *buf)
67 {
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**));
73     }
74
75     buf->size = buf->len;
76 }
77
78 static inline BOOL is_elem_node(nsIDOMNode *node)
79 {
80     PRUint16 type=0;
81
82     nsIDOMNode_GetNodeType(node, &type);
83
84     return type == ELEMENT_NODE || type == COMMENT_NODE;
85 }
86
87 #define ELEMCOL_THIS(iface) DEFINE_THIS(HTMLElementCollection, HTMLElementCollection, iface)
88 #define HTMLELEM_NODE_THIS(iface) DEFINE_THIS2(HTMLElement, node, iface)
89
90 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
91                                                            REFIID riid, void **ppv)
92 {
93     HTMLElementCollection *This = ELEMCOL_THIS(iface);
94
95     *ppv = NULL;
96
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;
105     }
106
107     if(*ppv) {
108         IHTMLElementCollection_AddRef(HTMLELEMCOL(This));
109         return S_OK;
110     }
111
112     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
113     return E_NOINTERFACE;
114 }
115
116 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
117 {
118     HTMLElementCollection *This = ELEMCOL_THIS(iface);
119     LONG ref = InterlockedIncrement(&This->ref);
120
121     TRACE("(%p) ref=%d\n", This, ref);
122
123     return ref;
124 }
125
126 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
127 {
128     HTMLElementCollection *This = ELEMCOL_THIS(iface);
129     LONG ref = InterlockedDecrement(&This->ref);
130
131     TRACE("(%p) ref=%d\n", This, ref);
132
133     if(!ref) {
134         IUnknown_Release(This->ref_unk);
135         heap_free(This->elems);
136         heap_free(This);
137     }
138
139     return ref;
140 }
141
142 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
143                                                              UINT *pctinfo)
144 {
145     HTMLElementCollection *This = ELEMCOL_THIS(iface);
146     return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
147 }
148
149 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
150         UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
151 {
152     HTMLElementCollection *This = ELEMCOL_THIS(iface);
153     return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
154 }
155
156 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
157         REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
158 {
159     HTMLElementCollection *This = ELEMCOL_THIS(iface);
160     return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
161 }
162
163 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
164         DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
165         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
166 {
167     HTMLElementCollection *This = ELEMCOL_THIS(iface);
168     return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
169             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
170 }
171
172 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
173                                                      BSTR *String)
174 {
175     HTMLElementCollection *This = ELEMCOL_THIS(iface);
176     FIXME("(%p)->(%p)\n", This, String);
177     return E_NOTIMPL;
178 }
179
180 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
181                                                        LONG v)
182 {
183     HTMLElementCollection *This = ELEMCOL_THIS(iface);
184     FIXME("(%p)->(%d)\n", This, v);
185     return E_NOTIMPL;
186 }
187
188 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
189                                                        LONG *p)
190 {
191     HTMLElementCollection *This = ELEMCOL_THIS(iface);
192
193     TRACE("(%p)->(%p)\n", This, p);
194
195     *p = This->len;
196     return S_OK;
197 }
198
199 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
200                                                          IUnknown **p)
201 {
202     HTMLElementCollection *This = ELEMCOL_THIS(iface);
203     FIXME("(%p)->(%p)\n", This, p);
204     return E_NOTIMPL;
205 }
206
207 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
208 {
209     const PRUnichar *str;
210     nsAString nsstr, nsname;
211     BOOL ret = FALSE;
212     nsresult nsres;
213
214     static const PRUnichar nameW[] = {'n','a','m','e',0};
215
216     if(!elem->nselem)
217         return FALSE;
218
219     nsAString_Init(&nsstr, NULL);
220     nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
221     nsAString_GetData(&nsstr, &str);
222     if(!strcmpiW(str, name)) {
223         nsAString_Finish(&nsstr);
224         return TRUE;
225     }
226
227     nsAString_Init(&nsname, nameW);
228     nsres =  nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
229     nsAString_Finish(&nsname);
230     if(NS_SUCCEEDED(nsres)) {
231         nsAString_GetData(&nsstr, &str);
232         ret = !strcmpiW(str, name);
233     }
234
235     nsAString_Finish(&nsstr);
236     return ret;
237 }
238
239 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
240 {
241     if(idx < This->len) {
242         *ret = (IDispatch*)This->elems[idx];
243         IDispatch_AddRef(*ret);
244     }
245
246     return S_OK;
247 }
248
249 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
250         VARIANT name, VARIANT index, IDispatch **pdisp)
251 {
252     HTMLElementCollection *This = ELEMCOL_THIS(iface);
253     HRESULT hres = S_OK;
254
255     TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
256
257     *pdisp = NULL;
258
259     switch(V_VT(&name)) {
260     case VT_I4:
261         if(V_I4(&name) < 0)
262             return E_INVALIDARG;
263         hres = get_item_idx(This, V_I4(&name), pdisp);
264         break;
265
266     case VT_UINT:
267         hres = get_item_idx(This, V_UINT(&name), pdisp);
268         break;
269
270     case VT_BSTR: {
271         DWORD i;
272
273         if(V_VT(&index) == VT_I4) {
274             LONG idx = V_I4(&index);
275
276             if(idx < 0)
277                 return E_INVALIDARG;
278
279             for(i=0; i<This->len; i++) {
280                 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
281                     break;
282             }
283
284             if(i != This->len) {
285                 *pdisp = (IDispatch*)HTMLELEM(This->elems[i]);
286                 IDispatch_AddRef(*pdisp);
287             }
288         }else {
289             elem_vector_t buf = {NULL, 0, 8};
290
291             buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
292
293             for(i=0; i<This->len; i++) {
294                 if(is_elem_name(This->elems[i], V_BSTR(&name)))
295                     elem_vector_add(&buf, This->elems[i]);
296             }
297
298             if(buf.len > 1) {
299                 elem_vector_normalize(&buf);
300                 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
301             }else {
302                 if(buf.len == 1) {
303                     *pdisp = (IDispatch*)HTMLELEM(buf.buf[0]);
304                     IDispatch_AddRef(*pdisp);
305                 }
306
307                 heap_free(buf.buf);
308             }
309         }
310         break;
311     }
312
313     default:
314         FIXME("Unsupported name %s\n", debugstr_variant(&name));
315         hres = E_NOTIMPL;
316     }
317
318     if(SUCCEEDED(hres))
319         TRACE("returning %p\n", *pdisp);
320     return hres;
321 }
322
323 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
324                                                  VARIANT tagName, IDispatch **pdisp)
325 {
326     HTMLElementCollection *This = ELEMCOL_THIS(iface);
327     DWORD i;
328     nsAString tag_str;
329     const PRUnichar *tag;
330     elem_vector_t buf = {NULL, 0, 8};
331
332     if(V_VT(&tagName) != VT_BSTR) {
333         WARN("Invalid arg\n");
334         return DISP_E_MEMBERNOTFOUND;
335     }
336
337     TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
338
339     buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
340
341     nsAString_Init(&tag_str, NULL);
342
343     for(i=0; i<This->len; i++) {
344         if(!This->elems[i]->nselem)
345             continue;
346
347         nsIDOMElement_GetTagName(This->elems[i]->nselem, &tag_str);
348         nsAString_GetData(&tag_str, &tag);
349
350         if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
351                           V_BSTR(&tagName), -1) == CSTR_EQUAL)
352             elem_vector_add(&buf, This->elems[i]);
353     }
354
355     nsAString_Finish(&tag_str);
356     elem_vector_normalize(&buf);
357
358     TRACE("fount %d tags\n", buf.len);
359
360     *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
361     return S_OK;
362 }
363
364 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
365
366 static HRESULT HTMLElementCollection_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
367 {
368     HTMLElementCollection *This = ELEMCOL_THIS(iface);
369     WCHAR *ptr;
370     DWORD idx=0;
371
372     if(!*name)
373         return DISP_E_UNKNOWNNAME;
374
375     for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
376         idx = idx*10 + (*ptr-'0');
377
378     if(*ptr || idx >= This->len)
379         return DISP_E_UNKNOWNNAME;
380
381     *dispid = DISPID_ELEMCOL_0 + idx;
382     TRACE("ret %x\n", *dispid);
383     return S_OK;
384 }
385
386 static HRESULT HTMLElementCollection_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
387         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
388 {
389     HTMLElementCollection *This = ELEMCOL_THIS(iface);
390     DWORD idx;
391
392     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
393
394     idx = id - DISPID_ELEMCOL_0;
395     if(idx >= This->len)
396         return DISP_E_UNKNOWNNAME;
397
398     switch(flags) {
399     case INVOKE_PROPERTYGET:
400         V_VT(res) = VT_DISPATCH;
401         V_DISPATCH(res) = (IDispatch*)HTMLELEM(This->elems[idx]);
402         IHTMLElement_AddRef(HTMLELEM(This->elems[idx]));
403         break;
404     default:
405         FIXME("unimplemented flags %x\n", flags);
406         return E_NOTIMPL;
407     }
408
409     return S_OK;
410 }
411
412 #undef ELEMCOL_THIS
413
414 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
415     HTMLElementCollection_QueryInterface,
416     HTMLElementCollection_AddRef,
417     HTMLElementCollection_Release,
418     HTMLElementCollection_GetTypeInfoCount,
419     HTMLElementCollection_GetTypeInfo,
420     HTMLElementCollection_GetIDsOfNames,
421     HTMLElementCollection_Invoke,
422     HTMLElementCollection_toString,
423     HTMLElementCollection_put_length,
424     HTMLElementCollection_get_length,
425     HTMLElementCollection_get__newEnum,
426     HTMLElementCollection_item,
427     HTMLElementCollection_tags
428 };
429
430 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
431     HTMLElementCollection_get_dispid,
432     HTMLElementCollection_invoke
433 };
434
435 static const tid_t HTMLElementCollection_iface_tids[] = {
436     IHTMLElementCollection_tid,
437     0
438 };
439 static dispex_static_data_t HTMLElementCollection_dispex = {
440     &HTMLElementColection_dispex_vtbl,
441     DispHTMLElementCollection_tid,
442     NULL,
443     HTMLElementCollection_iface_tids
444 };
445
446 static void create_all_list(HTMLDocument *doc, HTMLDOMNode *elem, elem_vector_t *buf)
447 {
448     nsIDOMNodeList *nsnode_list;
449     nsIDOMNode *iter;
450     PRUint32 list_len = 0, i;
451     nsresult nsres;
452
453     nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
454     if(NS_FAILED(nsres)) {
455         ERR("GetChildNodes failed: %08x\n", nsres);
456         return;
457     }
458
459     nsIDOMNodeList_GetLength(nsnode_list, &list_len);
460     if(!list_len)
461         return;
462
463     for(i=0; i<list_len; i++) {
464         nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
465         if(NS_FAILED(nsres)) {
466             ERR("Item failed: %08x\n", nsres);
467             continue;
468         }
469
470         if(is_elem_node(iter)) {
471             HTMLDOMNode *node = get_node(doc, iter, TRUE);
472
473             elem_vector_add(buf, HTMLELEM_NODE_THIS(node));
474             create_all_list(doc, node, buf);
475         }
476     }
477 }
478
479 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
480 {
481     elem_vector_t buf = {NULL, 0, 8};
482
483     buf.buf = heap_alloc(buf.size*sizeof(HTMLElement**));
484
485     if(include_root)
486         elem_vector_add(&buf, HTMLELEM_NODE_THIS(node));
487     create_all_list(node->doc, node, &buf);
488     elem_vector_normalize(&buf);
489
490     return HTMLElementCollection_Create((IUnknown*)HTMLDOMNODE(node), buf.buf, buf.len);
491 }
492
493 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocument *doc, IUnknown *unk, nsIDOMNodeList *nslist)
494 {
495     PRUint32 length = 0, i;
496     elem_vector_t buf;
497
498     nsIDOMNodeList_GetLength(nslist, &length);
499
500     buf.len = 0;
501     buf.size = length;
502     if(length) {
503         nsIDOMNode *nsnode;
504
505         buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
506
507         for(i=0; i<length; i++) {
508             nsIDOMNodeList_Item(nslist, i, &nsnode);
509             if(is_elem_node(nsnode))
510                 buf.buf[buf.len++] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
511             nsIDOMNode_Release(nsnode);
512         }
513
514         elem_vector_normalize(&buf);
515     }else {
516         buf.buf = NULL;
517     }
518
519     return HTMLElementCollection_Create(unk, buf.buf, buf.len);
520 }
521
522 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocument *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
523 {
524     PRUint32 length = 0, i;
525     elem_vector_t buf;
526
527     nsIDOMHTMLCollection_GetLength(nscol, &length);
528
529     buf.len = buf.size = length;
530     if(buf.len) {
531         nsIDOMNode *nsnode;
532
533         buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
534
535         for(i=0; i<length; i++) {
536             nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
537             buf.buf[i] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
538             nsIDOMNode_Release(nsnode);
539         }
540     }else {
541         buf.buf = NULL;
542     }
543
544     return HTMLElementCollection_Create(unk, buf.buf, buf.len);
545 }
546
547 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
548             HTMLElement **elems, DWORD len)
549 {
550     HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
551
552     ret->lpHTMLElementCollectionVtbl = &HTMLElementCollectionVtbl;
553     ret->ref = 1;
554     ret->elems = elems;
555     ret->len = len;
556
557     init_dispex(&ret->dispex, (IUnknown*)HTMLELEMCOL(ret), &HTMLElementCollection_dispex);
558
559     IUnknown_AddRef(ref_unk);
560     ret->ref_unk = ref_unk;
561
562     TRACE("ret=%p len=%d\n", ret, len);
563
564     return HTMLELEMCOL(ret);
565 }