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 void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
55 if(buf->len == buf->size) {
57 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement**));
60 buf->buf[buf->len++] = elem;
63 static void elem_vector_normalize(elem_vector_t *buf)
68 }else if(buf->size > buf->len) {
69 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement**));
75 static inline BOOL is_elem_node(nsIDOMNode *node)
79 nsIDOMNode_GetNodeType(node, &type);
81 return type == ELEMENT_NODE || type == COMMENT_NODE;
84 #define ELEMCOL_THIS(iface) DEFINE_THIS(HTMLElementCollection, HTMLElementCollection, iface)
85 #define HTMLELEM_NODE_THIS(iface) DEFINE_THIS2(HTMLElement, node, iface)
87 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
88 REFIID riid, void **ppv)
90 HTMLElementCollection *This = ELEMCOL_THIS(iface);
94 if(IsEqualGUID(&IID_IUnknown, riid)) {
95 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
96 *ppv = HTMLELEMCOL(This);
97 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
98 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This, ppv);
99 *ppv = HTMLELEMCOL(This);
100 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
101 return *ppv ? S_OK : E_NOINTERFACE;
105 IHTMLElementCollection_AddRef(HTMLELEMCOL(This));
109 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
110 return E_NOINTERFACE;
113 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
115 HTMLElementCollection *This = ELEMCOL_THIS(iface);
116 LONG ref = InterlockedIncrement(&This->ref);
118 TRACE("(%p) ref=%d\n", This, ref);
123 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
125 HTMLElementCollection *This = ELEMCOL_THIS(iface);
126 LONG ref = InterlockedDecrement(&This->ref);
128 TRACE("(%p) ref=%d\n", This, ref);
131 IUnknown_Release(This->ref_unk);
132 heap_free(This->elems);
139 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
142 HTMLElementCollection *This = ELEMCOL_THIS(iface);
143 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
146 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
147 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
149 HTMLElementCollection *This = ELEMCOL_THIS(iface);
150 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
153 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
154 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
156 HTMLElementCollection *This = ELEMCOL_THIS(iface);
157 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
160 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
161 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
162 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
164 HTMLElementCollection *This = ELEMCOL_THIS(iface);
165 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
166 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
169 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
172 HTMLElementCollection *This = ELEMCOL_THIS(iface);
173 FIXME("(%p)->(%p)\n", This, String);
177 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
180 HTMLElementCollection *This = ELEMCOL_THIS(iface);
181 FIXME("(%p)->(%ld)\n", This, v);
185 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
188 HTMLElementCollection *This = ELEMCOL_THIS(iface);
190 TRACE("(%p)->(%p)\n", This, p);
196 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
199 HTMLElementCollection *This = ELEMCOL_THIS(iface);
200 FIXME("(%p)->(%p)\n", This, p);
204 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
206 const PRUnichar *str;
207 nsAString nsstr, nsname;
211 static const PRUnichar nameW[] = {'n','a','m','e',0};
216 nsAString_Init(&nsstr, NULL);
217 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
218 nsAString_GetData(&nsstr, &str);
219 if(!strcmpiW(str, name)) {
220 nsAString_Finish(&nsstr);
224 nsAString_Init(&nsname, nameW);
225 nsres = nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
226 nsAString_Finish(&nsname);
227 if(NS_SUCCEEDED(nsres)) {
228 nsAString_GetData(&nsstr, &str);
229 ret = !strcmpiW(str, name);
232 nsAString_Finish(&nsstr);
236 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
237 VARIANT name, VARIANT index, IDispatch **pdisp)
239 HTMLElementCollection *This = ELEMCOL_THIS(iface);
241 TRACE("(%p)->(v(%d) v(%d) %p)\n", This, V_VT(&name), V_VT(&index), pdisp);
245 if(V_VT(&name) == VT_I4) {
246 TRACE("name is VT_I4: %d\n", V_I4(&name));
250 if(V_I4(&name) >= This->len)
253 *pdisp = (IDispatch*)This->elems[V_I4(&name)];
254 IDispatch_AddRef(*pdisp);
255 TRACE("Returning pdisp=%p\n", pdisp);
259 if(V_VT(&name) == VT_BSTR) {
262 TRACE("name is VT_BSTR: %s\n", debugstr_w(V_BSTR(&name)));
264 if(V_VT(&index) == VT_I4) {
265 LONG idx = V_I4(&index);
267 TRACE("index = %d\n", idx);
272 for(i=0; i<This->len; i++) {
273 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
278 *pdisp = (IDispatch*)HTMLELEM(This->elems[i]);
279 IDispatch_AddRef(*pdisp);
284 elem_vector_t buf = {NULL, 0, 8};
286 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
288 for(i=0; i<This->len; i++) {
289 if(is_elem_name(This->elems[i], V_BSTR(&name)))
290 elem_vector_add(&buf, This->elems[i]);
294 elem_vector_normalize(&buf);
295 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
298 *pdisp = (IDispatch*)HTMLELEM(buf.buf[0]);
299 IDispatch_AddRef(*pdisp);
309 FIXME("unsupported arguments\n");
313 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
314 VARIANT tagName, IDispatch **pdisp)
316 HTMLElementCollection *This = ELEMCOL_THIS(iface);
319 const PRUnichar *tag;
320 elem_vector_t buf = {NULL, 0, 8};
322 if(V_VT(&tagName) != VT_BSTR) {
323 WARN("Invalid arg\n");
324 return DISP_E_MEMBERNOTFOUND;
327 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
329 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
331 nsAString_Init(&tag_str, NULL);
333 for(i=0; i<This->len; i++) {
334 if(!This->elems[i]->nselem)
337 nsIDOMElement_GetTagName(This->elems[i]->nselem, &tag_str);
338 nsAString_GetData(&tag_str, &tag);
340 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
341 V_BSTR(&tagName), -1) == CSTR_EQUAL)
342 elem_vector_add(&buf, This->elems[i]);
345 nsAString_Finish(&tag_str);
346 elem_vector_normalize(&buf);
348 TRACE("fount %d tags\n", buf.len);
350 *pdisp = (IDispatch*)HTMLElementCollection_Create(This->ref_unk, buf.buf, buf.len);
354 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
356 static HRESULT HTMLElementCollection_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
358 HTMLElementCollection *This = ELEMCOL_THIS(iface);
363 return DISP_E_UNKNOWNNAME;
365 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
366 idx = idx*10 + (*ptr-'0');
368 if(*ptr || idx >= This->len)
369 return DISP_E_UNKNOWNNAME;
371 *dispid = DISPID_ELEMCOL_0 + idx;
372 TRACE("ret %x\n", *dispid);
376 static HRESULT HTMLElementCollection_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
377 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
379 HTMLElementCollection *This = ELEMCOL_THIS(iface);
382 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
384 idx = id - DISPID_ELEMCOL_0;
386 return DISP_E_UNKNOWNNAME;
389 case INVOKE_PROPERTYGET:
390 V_VT(res) = VT_DISPATCH;
391 V_DISPATCH(res) = (IDispatch*)HTMLELEM(This->elems[idx]);
392 IHTMLElement_AddRef(HTMLELEM(This->elems[idx]));
395 FIXME("unimplemented flags %x\n", flags);
404 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
405 HTMLElementCollection_QueryInterface,
406 HTMLElementCollection_AddRef,
407 HTMLElementCollection_Release,
408 HTMLElementCollection_GetTypeInfoCount,
409 HTMLElementCollection_GetTypeInfo,
410 HTMLElementCollection_GetIDsOfNames,
411 HTMLElementCollection_Invoke,
412 HTMLElementCollection_toString,
413 HTMLElementCollection_put_length,
414 HTMLElementCollection_get_length,
415 HTMLElementCollection_get__newEnum,
416 HTMLElementCollection_item,
417 HTMLElementCollection_tags
420 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
421 HTMLElementCollection_get_dispid,
422 HTMLElementCollection_invoke
425 static const tid_t HTMLElementCollection_iface_tids[] = {
426 IHTMLElementCollection_tid,
429 static dispex_static_data_t HTMLElementCollection_dispex = {
430 &HTMLElementColection_dispex_vtbl,
431 DispHTMLElementCollection_tid,
433 HTMLElementCollection_iface_tids
436 static void create_all_list(HTMLDocument *doc, HTMLDOMNode *elem, elem_vector_t *buf)
438 nsIDOMNodeList *nsnode_list;
440 PRUint32 list_len = 0, i;
443 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
444 if(NS_FAILED(nsres)) {
445 ERR("GetChildNodes failed: %08x\n", nsres);
449 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
453 for(i=0; i<list_len; i++) {
454 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
455 if(NS_FAILED(nsres)) {
456 ERR("Item failed: %08x\n", nsres);
460 if(is_elem_node(iter)) {
461 HTMLDOMNode *node = get_node(doc, iter, TRUE);
463 elem_vector_add(buf, HTMLELEM_NODE_THIS(node));
464 create_all_list(doc, node, buf);
469 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
471 elem_vector_t buf = {NULL, 0, 8};
473 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement**));
476 elem_vector_add(&buf, HTMLELEM_NODE_THIS(node));
477 create_all_list(node->doc, node, &buf);
478 elem_vector_normalize(&buf);
480 return HTMLElementCollection_Create((IUnknown*)HTMLDOMNODE(node), buf.buf, buf.len);
483 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocument *doc, IUnknown *unk, nsIDOMNodeList *nslist)
485 PRUint32 length = 0, i;
488 nsIDOMNodeList_GetLength(nslist, &length);
495 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
497 for(i=0; i<length; i++) {
498 nsIDOMNodeList_Item(nslist, i, &nsnode);
499 if(is_elem_node(nsnode))
500 buf.buf[buf.len++] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
501 nsIDOMNode_Release(nsnode);
504 elem_vector_normalize(&buf);
509 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
512 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocument *doc, IUnknown *unk, nsIDOMHTMLCollection *nscol)
514 PRUint32 length = 0, i;
517 nsIDOMHTMLCollection_GetLength(nscol, &length);
519 buf.len = buf.size = length;
523 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
525 for(i=0; i<length; i++) {
526 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
527 buf.buf[i] = HTMLELEM_NODE_THIS(get_node(doc, nsnode, TRUE));
528 nsIDOMNode_Release(nsnode);
534 return HTMLElementCollection_Create(unk, buf.buf, buf.len);
537 IHTMLElementCollection *HTMLElementCollection_Create(IUnknown *ref_unk,
538 HTMLElement **elems, DWORD len)
540 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
542 ret->lpHTMLElementCollectionVtbl = &HTMLElementCollectionVtbl;
547 init_dispex(&ret->dispex, (IUnknown*)HTMLELEMCOL(ret), &HTMLElementCollection_dispex);
549 IUnknown_AddRef(ref_unk);
550 ret->ref_unk = ref_unk;
552 TRACE("ret=%p len=%d\n", ret, len);
554 return HTMLELEMCOL(ret);