[gdi32]
[reactos.git] / reactos / dll / win32 / 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 release_dispex(&This->dispex);
136 heap_free(This->elems);
137 heap_free(This);
138 }
139
140 return ref;
141 }
142
143 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
144 UINT *pctinfo)
145 {
146 HTMLElementCollection *This = ELEMCOL_THIS(iface);
147 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
148 }
149
150 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
151 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
152 {
153 HTMLElementCollection *This = ELEMCOL_THIS(iface);
154 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
155 }
156
157 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
158 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
159 {
160 HTMLElementCollection *This = ELEMCOL_THIS(iface);
161 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
162 }
163
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)
167 {
168 HTMLElementCollection *This = ELEMCOL_THIS(iface);
169 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
170 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
171 }
172
173 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
174 BSTR *String)
175 {
176 HTMLElementCollection *This = ELEMCOL_THIS(iface);
177 FIXME("(%p)->(%p)\n", This, String);
178 return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
182 LONG v)
183 {
184 HTMLElementCollection *This = ELEMCOL_THIS(iface);
185 FIXME("(%p)->(%d)\n", This, v);
186 return E_NOTIMPL;
187 }
188
189 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
190 LONG *p)
191 {
192 HTMLElementCollection *This = ELEMCOL_THIS(iface);
193
194 TRACE("(%p)->(%p)\n", This, p);
195
196 *p = This->len;
197 return S_OK;
198 }
199
200 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
201 IUnknown **p)
202 {
203 HTMLElementCollection *This = ELEMCOL_THIS(iface);
204 FIXME("(%p)->(%p)\n", This, p);
205 return E_NOTIMPL;
206 }
207
208 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
209 {
210 const PRUnichar *str;
211 nsAString nsstr, nsname;
212 BOOL ret = FALSE;
213 nsresult nsres;
214
215 static const PRUnichar nameW[] = {'n','a','m','e',0};
216
217 if(!elem->nselem)
218 return FALSE;
219
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;
226 }
227
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);
234 }
235
236 nsAString_Finish(&nsstr);
237 return ret;
238 }
239
240 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
241 {
242 if(idx < This->len) {
243 *ret = (IDispatch*)This->elems[idx];
244 IDispatch_AddRef(*ret);
245 }
246
247 return S_OK;
248 }
249
250 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
251 VARIANT name, VARIANT index, IDispatch **pdisp)
252 {
253 HTMLElementCollection *This = ELEMCOL_THIS(iface);
254 HRESULT hres = S_OK;
255
256 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
257
258 *pdisp = NULL;
259
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;
266
267 case VT_UINT:
268 hres = get_item_idx(This, V_UINT(&name), pdisp);
269 break;
270
271 case VT_BSTR: {
272 DWORD i;
273
274 if(V_VT(&index) == VT_I4) {
275 LONG idx = V_I4(&index);
276
277 if(idx < 0)
278 return E_INVALIDARG;
279
280 for(i=0; i<This->len; i++) {
281 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
282 break;
283 }
284
285 if(i != This->len) {
286 *pdisp = (IDispatch*)HTMLELEM(This->elems[i]);
287 IDispatch_AddRef(*pdisp);
288 }
289 }else {
290 elem_vector_t buf = {NULL, 0, 8};
291
292 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
293
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]);
297 }
298
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);
306 }
307
308 heap_free(buf.buf);
309 }
310 }
311 break;
312 }
313
314 default:
315 FIXME("Unsupported name %s\n", debugstr_variant(&name));
316 hres = E_NOTIMPL;
317 }
318
319 if(SUCCEEDED(hres))
320 TRACE("returning %p\n", *pdisp);
321 return hres;
322 }
323
324 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
325 VARIANT tagName, IDispatch **pdisp)
326 {
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};
332
333 if(V_VT(&tagName) != VT_BSTR) {
334 WARN("Invalid arg\n");
335 return DISP_E_MEMBERNOTFOUND;
336 }
337
338 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
339
340 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
341
342 nsAString_Init(&tag_str, NULL);
343
344 for(i=0; i<This->len; i++) {
345 if(!This->elems[i]->nselem)
346 continue;
347
348 nsIDOMElement_GetTagName(This->elems[i]->nselem, &tag_str);
349 nsAString_GetData(&tag_str, &tag);
350
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]);
354 }
355
356 nsAString_Finish(&tag_str);
357 elem_vector_normalize(&buf);
358
359 TRACE("fount %d tags\n", buf.len);
360
361 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
362 return S_OK;
363 }
364
365 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
366
367 static HRESULT HTMLElementCollection_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
368 {
369 HTMLElementCollection *This = ELEMCOL_THIS(iface);
370 WCHAR *ptr;
371 DWORD idx=0;
372
373 if(!*name)
374 return DISP_E_UNKNOWNNAME;
375
376 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
377 idx = idx*10 + (*ptr-'0');
378
379 if(*ptr || idx >= This->len)
380 return DISP_E_UNKNOWNNAME;
381
382 *dispid = DISPID_ELEMCOL_0 + idx;
383 TRACE("ret %x\n", *dispid);
384 return S_OK;
385 }
386
387 static HRESULT HTMLElementCollection_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
388 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
389 {
390 HTMLElementCollection *This = ELEMCOL_THIS(iface);
391 DWORD idx;
392
393 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
394
395 idx = id - DISPID_ELEMCOL_0;
396 if(idx >= This->len)
397 return DISP_E_UNKNOWNNAME;
398
399 switch(flags) {
400 case DISPATCH_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;
408 }
409
410 return S_OK;
411 }
412
413 #undef ELEMCOL_THIS
414
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
429 };
430
431 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
432 NULL,
433 HTMLElementCollection_get_dispid,
434 HTMLElementCollection_invoke
435 };
436
437 static const tid_t HTMLElementCollection_iface_tids[] = {
438 IHTMLElementCollection_tid,
439 0
440 };
441 static dispex_static_data_t HTMLElementCollection_dispex = {
442 &HTMLElementColection_dispex_vtbl,
443 DispHTMLElementCollection_tid,
444 NULL,
445 HTMLElementCollection_iface_tids
446 };
447
448 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
449 {
450 nsIDOMNodeList *nsnode_list;
451 nsIDOMNode *iter;
452 PRUint32 list_len = 0, i;
453 nsresult nsres;
454
455 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
456 if(NS_FAILED(nsres)) {
457 ERR("GetChildNodes failed: %08x\n", nsres);
458 return;
459 }
460
461 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
462 if(!list_len)
463 return;
464
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;
470 }
471
472 if(is_elem_node(iter)) {
473 HTMLDOMNode *node = get_node(doc, iter, TRUE);
474
475 elem_vector_add(buf, HTMLELEM_NODE_THIS(node));
476 create_all_list(doc, node, buf);
477 }
478 }
479 }
480
481 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
482 {
483 elem_vector_t buf = {NULL, 0, 8};
484
485 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement**));
486
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);
491
492 return HTMLElementCollection_Create((IUnknown*)HTMLDOMNODE(node), buf.buf, buf.len);
493 }
494
495 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMNodeList *nslist)
496 {
497 PRUint32 length = 0, i;
498 elem_vector_t buf;
499
500 nsIDOMNodeList_GetLength(nslist, &length);
501
502 buf.len = 0;
503 buf.size = length;
504 if(length) {
505 nsIDOMNode *nsnode;
506
507 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
508
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);
514 }
515
516 elem_vector_normalize(&buf);
517 }else {
518 buf.buf = NULL;
519 }
520
521 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
522 }
523
524 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
525 {
526 PRUint32 length = 0, i;
527 elem_vector_t buf;
528
529 nsIDOMHTMLCollection_GetLength(nscol, &length);
530
531 buf.len = buf.size = length;
532 if(buf.len) {
533 nsIDOMNode *nsnode;
534
535 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
536
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);
541 }
542 }else {
543 buf.buf = NULL;
544 }
545
546 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
547 }
548
549 static IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
550 HTMLElement **elems, DWORD len)
551 {
552 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
553
554 ret->lpHTMLElementCollectionVtbl = &HTMLElementCollectionVtbl;
555 ret->ref = 1;
556 ret->elems = elems;
557 ret->len = len;
558
559 init_dispex(&ret->dispex, (IUnknown*)HTMLELEMCOL(ret), &HTMLElementCollection_dispex);
560
561 IUnknown_AddRef(ref_unk);
562 ret->ref_unk = ref_unk;
563
564 TRACE("ret=%p len=%d\n", ret, len);
565
566 return HTMLELEMCOL(ret);
567 }