mshtml: Added noscript tag handling tests.
[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     IHTMLElementCollection IHTMLElementCollection_iface;
37
38     HTMLElement **elems;
39     DWORD len;
40
41     LONG ref;
42 } HTMLElementCollection;
43
44 typedef struct {
45     HTMLElement **buf;
46     DWORD len;
47     DWORD size;
48 } elem_vector_t;
49
50 /* FIXME: Handle it better way */
51 static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
52 {
53     return CONTAINING_RECORD(iface, HTMLElement, node);
54 }
55
56 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len);
57
58 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
59 {
60     if(buf->len == buf->size) {
61         buf->size <<= 1;
62         buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement*));
63     }
64
65     buf->buf[buf->len++] = elem;
66 }
67
68 static void elem_vector_normalize(elem_vector_t *buf)
69 {
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*));
75     }
76
77     buf->size = buf->len;
78 }
79
80 static inline BOOL is_elem_node(nsIDOMNode *node)
81 {
82     PRUint16 type=0;
83
84     nsIDOMNode_GetNodeType(node, &type);
85
86     return type == ELEMENT_NODE || type == COMMENT_NODE;
87 }
88
89 static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
90 {
91     return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
92 }
93
94 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
95                                                            REFIID riid, void **ppv)
96 {
97     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
98
99     *ppv = NULL;
100
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;
109     }
110
111     if(*ppv) {
112         IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
113         return S_OK;
114     }
115
116     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
117     return E_NOINTERFACE;
118 }
119
120 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
121 {
122     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
123     LONG ref = InterlockedIncrement(&This->ref);
124
125     TRACE("(%p) ref=%d\n", This, ref);
126
127     return ref;
128 }
129
130 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
131 {
132     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
133     LONG ref = InterlockedDecrement(&This->ref);
134
135     TRACE("(%p) ref=%d\n", This, ref);
136
137     if(!ref) {
138         unsigned i;
139
140         for(i=0; i < This->len; i++)
141             node_release(&This->elems[i]->node);
142         heap_free(This->elems);
143
144         release_dispex(&This->dispex);
145         heap_free(This);
146     }
147
148     return ref;
149 }
150
151 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
152                                                              UINT *pctinfo)
153 {
154     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
155     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
156 }
157
158 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
159         UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
160 {
161     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
162     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
163 }
164
165 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
166         REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
167 {
168     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
169     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
170             lcid, rgDispId);
171 }
172
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)
176 {
177     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
178     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
179             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
180 }
181
182 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
183                                                      BSTR *String)
184 {
185     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
186     FIXME("(%p)->(%p)\n", This, String);
187     return E_NOTIMPL;
188 }
189
190 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
191                                                        LONG v)
192 {
193     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
194     FIXME("(%p)->(%d)\n", This, v);
195     return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
199                                                        LONG *p)
200 {
201     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
202
203     TRACE("(%p)->(%p)\n", This, p);
204
205     *p = This->len;
206     return S_OK;
207 }
208
209 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
210                                                          IUnknown **p)
211 {
212     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
213     FIXME("(%p)->(%p)\n", This, p);
214     return E_NOTIMPL;
215 }
216
217 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
218 {
219     BSTR elem_id;
220     HRESULT hres;
221
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;
226     }
227
228     if(elem_id && !strcmpW(elem_id, name)) {
229         SysFreeString(elem_id);
230         return TRUE;
231     }
232
233     SysFreeString(elem_id);
234     return FALSE;
235 }
236
237 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
238 {
239     const PRUnichar *str;
240     nsAString nsstr, nsname;
241     BOOL ret = FALSE;
242     nsresult nsres;
243
244     static const PRUnichar nameW[] = {'n','a','m','e',0};
245
246     if(!elem->nselem)
247         return FALSE;
248
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;
255     }
256
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);
263     }
264
265     nsAString_Finish(&nsstr);
266     return ret;
267 }
268
269 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
270 {
271     if(idx < This->len) {
272         *ret = (IDispatch*)This->elems[idx];
273         IDispatch_AddRef(*ret);
274     }
275
276     return S_OK;
277 }
278
279 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
280         VARIANT name, VARIANT index, IDispatch **pdisp)
281 {
282     HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
283     HRESULT hres = S_OK;
284
285     TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
286
287     *pdisp = NULL;
288
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;
295
296     case VT_UINT:
297         hres = get_item_idx(This, V_UINT(&name), pdisp);
298         break;
299
300     case VT_BSTR: {
301         DWORD i;
302
303         if(V_VT(&index) == VT_I4) {
304             LONG idx = V_I4(&index);
305
306             if(idx < 0)
307                 return E_INVALIDARG;
308
309             for(i=0; i<This->len; i++) {
310                 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
311                     break;
312             }
313
314             if(i != This->len) {
315                 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
316                 IDispatch_AddRef(*pdisp);
317             }
318         }else {
319             elem_vector_t buf = {NULL, 0, 8};
320
321             buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
322
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]);
327                 }
328             }
329
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;
337                 }
338
339                 heap_free(buf.buf);
340             }
341         }
342         break;
343     }
344
345     default:
346         FIXME("Unsupported name %s\n", debugstr_variant(&name));
347         hres = E_NOTIMPL;
348     }
349
350     if(SUCCEEDED(hres))
351         TRACE("returning %p\n", *pdisp);
352     return hres;
353 }
354
355 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
356                                                  VARIANT tagName, IDispatch **pdisp)
357 {
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};
363
364     if(V_VT(&tagName) != VT_BSTR) {
365         WARN("Invalid arg\n");
366         return DISP_E_MEMBERNOTFOUND;
367     }
368
369     TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
370
371     buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
372
373     nsAString_Init(&tag_str, NULL);
374
375     for(i=0; i<This->len; i++) {
376         if(!This->elems[i]->nselem)
377             continue;
378
379         nsIDOMHTMLElement_GetTagName(This->elems[i]->nselem, &tag_str);
380         nsAString_GetData(&tag_str, &tag);
381
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]);
386         }
387     }
388
389     nsAString_Finish(&tag_str);
390     elem_vector_normalize(&buf);
391
392     TRACE("fount %d tags\n", buf.len);
393
394     *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
395     return S_OK;
396 }
397
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
412 };
413
414 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
415 {
416     return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
417 }
418
419 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
420
421 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
422 {
423     HTMLElementCollection *This = impl_from_DispatchEx(dispex);
424     WCHAR *ptr;
425     DWORD idx=0;
426
427     if(!*name)
428         return DISP_E_UNKNOWNNAME;
429
430     for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
431         idx = idx*10 + (*ptr-'0');
432
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;
439         }
440     }
441
442     if(idx >= This->len)
443         return DISP_E_UNKNOWNNAME;
444
445     *dispid = DISPID_ELEMCOL_0 + idx;
446     TRACE("ret %x\n", *dispid);
447     return S_OK;
448 }
449
450 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
451         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
452 {
453     HTMLElementCollection *This = impl_from_DispatchEx(dispex);
454     DWORD idx;
455
456     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
457
458     idx = id - DISPID_ELEMCOL_0;
459     if(idx >= This->len)
460         return DISP_E_UNKNOWNNAME;
461
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;
471     }
472
473     return S_OK;
474 }
475
476 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
477     NULL,
478     HTMLElementCollection_get_dispid,
479     HTMLElementCollection_invoke,
480     NULL
481 };
482
483 static const tid_t HTMLElementCollection_iface_tids[] = {
484     IHTMLElementCollection_tid,
485     0
486 };
487
488 static dispex_static_data_t HTMLElementCollection_dispex = {
489     &HTMLElementColection_dispex_vtbl,
490     DispHTMLElementCollection_tid,
491     NULL,
492     HTMLElementCollection_iface_tids
493 };
494
495 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
496 {
497     nsIDOMNodeList *nsnode_list;
498     nsIDOMNode *iter;
499     PRUint32 list_len = 0, i;
500     nsresult nsres;
501     HRESULT hres;
502
503     nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
504     if(NS_FAILED(nsres)) {
505         ERR("GetChildNodes failed: %08x\n", nsres);
506         return;
507     }
508
509     nsIDOMNodeList_GetLength(nsnode_list, &list_len);
510     if(!list_len)
511         return;
512
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;
518         }
519
520         if(is_elem_node(iter)) {
521             HTMLDOMNode *node;
522
523             hres = get_node(doc, iter, TRUE, &node);
524             if(FAILED(hres)) {
525                 FIXME("get_node failed: %08x\n", hres);
526                 continue;
527             }
528
529             elem_vector_add(buf, elem_from_HTMLDOMNode(node));
530             create_all_list(doc, node, buf);
531         }
532     }
533 }
534
535 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
536 {
537     elem_vector_t buf = {NULL, 0, 8};
538
539     buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
540
541     if(include_root) {
542         node_addref(node);
543         elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
544     }
545     create_all_list(node->doc, node, &buf);
546     elem_vector_normalize(&buf);
547
548     return HTMLElementCollection_Create(buf.buf, buf.len);
549 }
550
551 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
552 {
553     PRUint32 length = 0, i;
554     HTMLDOMNode *node;
555     elem_vector_t buf;
556     HRESULT hres;
557
558     nsIDOMNodeList_GetLength(nslist, &length);
559
560     buf.len = 0;
561     buf.size = length;
562     if(length) {
563         nsIDOMNode *nsnode;
564
565         buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
566
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);
574             }
575             nsIDOMNode_Release(nsnode);
576         }
577
578         elem_vector_normalize(&buf);
579     }else {
580         buf.buf = NULL;
581     }
582
583     return HTMLElementCollection_Create(buf.buf, buf.len);
584 }
585
586 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, nsIDOMHTMLCollection *nscol)
587 {
588     PRUint32 length = 0, i;
589     elem_vector_t buf;
590     HTMLDOMNode *node;
591     HRESULT hres = S_OK;
592
593     nsIDOMHTMLCollection_GetLength(nscol, &length);
594
595     buf.len = buf.size = length;
596     if(buf.len) {
597         nsIDOMNode *nsnode;
598
599         buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
600
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);
608         }
609     }else {
610         buf.buf = NULL;
611     }
612
613     if(FAILED(hres)) {
614         heap_free(buf.buf);
615         return NULL;
616     }
617
618     return HTMLElementCollection_Create(buf.buf, buf.len);
619 }
620
621 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len)
622 {
623     HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
624
625     ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
626     ret->ref = 1;
627     ret->elems = elems;
628     ret->len = len;
629
630     init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
631             &HTMLElementCollection_dispex);
632
633     TRACE("ret=%p len=%d\n", ret, len);
634
635     return &ret->IHTMLElementCollection_iface;
636 }