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
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 const IHTMLElementCollectionVtbl *lpHTMLElementCollectionVtbl;
43 } HTMLElementCollection;
45 #define HTMLELEMCOL(x) ((IHTMLElementCollection*) &(x)->lpHTMLElementCollectionVtbl)
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) {
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)
71 }else if(buf->size > buf->len) {
72 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement**));
78 static inline BOOL is_elem_node(nsIDOMNode *node)
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);
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;
108 IHTMLElementCollection_AddRef(HTMLELEMCOL(This));
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);
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);
134 IUnknown_Release(This->ref_unk);
135 release_dispex(&This->dispex);
136 heap_free(This->elems);
143 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
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,
176 HTMLElementCollection *This = ELEMCOL_THIS(iface);
177 FIXME("(%p)->(%p)\n", This, String);
181 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
184 HTMLElementCollection *This = ELEMCOL_THIS(iface);
185 FIXME("(%p)->(%d)\n", This, v);
189 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
192 HTMLElementCollection *This = ELEMCOL_THIS(iface);
194 TRACE("(%p)->(%p)\n", This, p);
200 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
203 HTMLElementCollection *This = ELEMCOL_THIS(iface);
204 FIXME("(%p)->(%p)\n", This, p);
208 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
210 const PRUnichar *str;
211 nsAString nsstr, nsname;
215 static const PRUnichar nameW[] = {'n','a','m','e',0};
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);
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);
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);
250 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
251 VARIANT name, VARIANT index, IDispatch **pdisp)
253 HTMLElementCollection *This = ELEMCOL_THIS(iface);
256 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
260 switch(V_VT(&name)) {
264 hres = get_item_idx(This, V_I4(&name), pdisp);
268 hres = get_item_idx(This, V_UINT(&name), pdisp);
274 if(V_VT(&index) == VT_I4) {
275 LONG idx = V_I4(&index);
280 for(i=0; i<This->len; i++) {
281 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
286 *pdisp = (IDispatch*)HTMLELEM(This->elems[i]);
287 IDispatch_AddRef(*pdisp);
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]);
300 elem_vector_normalize(&buf);
301 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
304 *pdisp = (IDispatch*)HTMLELEM(buf.buf[0]);
305 IDispatch_AddRef(*pdisp);
315 FIXME("Unsupported name %s\n", debugstr_variant(&name));
320 TRACE("returning %p\n", *pdisp);
324 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
325 VARIANT tagName, IDispatch **pdisp)
327 HTMLElementCollection *This = ELEMCOL_THIS(iface);
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)
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);
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);
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);
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);
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;
397 return DISP_E_UNKNOWNNAME;
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]));
406 FIXME("unimplemented flags %x\n", flags);
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 = {
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,
445 HTMLElementCollection_iface_tids
448 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
450 nsIDOMNodeList *nsnode_list;
452 PRUint32 list_len = 0, i;
455 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
456 if(NS_FAILED(nsres)) {
457 ERR("GetChildNodes failed: %08x\n", nsres);
461 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
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);
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**));
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(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMNodeList *nslist)
497 PRUint32 length = 0, i;
500 nsIDOMNodeList_GetLength(nslist, &length);
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);
521 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
524 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
526 PRUint32 length = 0, i;
529 nsIDOMHTMLCollection_GetLength(nscol, &length);
531 buf.len = buf.size = length;
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);
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;
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);