ole32: Initial implementation of transaction locking.
[wine/multimedia.git] / dlls / mshtml / htmlelemcol.c
blob5819d4441506a96d5d2c20c965c818dbc4493e7f
1 /*
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 typedef struct {
35 DispatchEx dispex;
36 IHTMLElementCollection IHTMLElementCollection_iface;
38 HTMLElement **elems;
39 DWORD len;
41 LONG ref;
42 } HTMLElementCollection;
44 typedef struct {
45 IEnumVARIANT IEnumVARIANT_iface;
47 LONG ref;
49 ULONG iter;
50 HTMLElementCollection *col;
51 } HTMLElementCollectionEnum;
53 typedef struct {
54 HTMLElement **buf;
55 DWORD len;
56 DWORD size;
57 } elem_vector_t;
59 /* FIXME: Handle it better way */
60 static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
62 return CONTAINING_RECORD(iface, HTMLElement, node);
65 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len);
67 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
69 if(buf->len == buf->size) {
70 buf->size <<= 1;
71 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement*));
74 buf->buf[buf->len++] = elem;
77 static void elem_vector_normalize(elem_vector_t *buf)
79 if(!buf->len) {
80 heap_free(buf->buf);
81 buf->buf = NULL;
82 }else if(buf->size > buf->len) {
83 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement*));
86 buf->size = buf->len;
89 static inline BOOL is_elem_node(nsIDOMNode *node)
91 UINT16 type=0;
93 nsIDOMNode_GetNodeType(node, &type);
95 return type == ELEMENT_NODE || type == COMMENT_NODE;
98 static inline HTMLElementCollectionEnum *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
100 return CONTAINING_RECORD(iface, HTMLElementCollectionEnum, IEnumVARIANT_iface);
103 static HRESULT WINAPI HTMLElementCollectionEnum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
105 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
107 if(IsEqualGUID(riid, &IID_IUnknown)) {
108 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
109 *ppv = &This->IEnumVARIANT_iface;
110 }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
111 TRACE("(%p)->(IID_IEnumVARIANT %p)\n", This, ppv);
112 *ppv = &This->IEnumVARIANT_iface;
113 }else {
114 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
115 *ppv = NULL;
116 return E_NOINTERFACE;
119 IUnknown_AddRef((IUnknown*)*ppv);
120 return S_OK;
123 static ULONG WINAPI HTMLElementCollectionEnum_AddRef(IEnumVARIANT *iface)
125 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
126 LONG ref = InterlockedIncrement(&This->ref);
128 TRACE("(%p) ref=%d\n", This, ref);
130 return ref;
133 static ULONG WINAPI HTMLElementCollectionEnum_Release(IEnumVARIANT *iface)
135 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
136 LONG ref = InterlockedDecrement(&This->ref);
138 TRACE("(%p) ref=%d\n", This, ref);
140 if(!ref) {
141 IHTMLElementCollection_Release(&This->col->IHTMLElementCollection_iface);
142 heap_free(This);
145 return ref;
148 static HRESULT WINAPI HTMLElementCollectionEnum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
150 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
151 ULONG fetched = 0;
153 TRACE("(%p)->(%d %p %p)\n", This, celt, rgVar, pCeltFetched);
155 while(This->iter+fetched < This->col->len && fetched < celt) {
156 V_VT(rgVar+fetched) = VT_DISPATCH;
157 V_DISPATCH(rgVar+fetched) = (IDispatch*)&This->col->elems[This->iter+fetched]->IHTMLElement_iface;
158 IDispatch_AddRef(V_DISPATCH(rgVar+fetched));
159 fetched++;
162 This->iter += fetched;
163 if(pCeltFetched)
164 *pCeltFetched = fetched;
165 return fetched == celt ? S_OK : S_FALSE;
168 static HRESULT WINAPI HTMLElementCollectionEnum_Skip(IEnumVARIANT *iface, ULONG celt)
170 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
172 TRACE("(%p)->(%d)\n", This, celt);
174 if(This->iter + celt > This->col->len) {
175 This->iter = This->col->len;
176 return S_FALSE;
179 This->iter += celt;
180 return S_OK;
183 static HRESULT WINAPI HTMLElementCollectionEnum_Reset(IEnumVARIANT *iface)
185 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
187 TRACE("(%p)->()\n", This);
189 This->iter = 0;
190 return S_OK;
193 static HRESULT WINAPI HTMLElementCollectionEnum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
195 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
196 FIXME("(%p)->(%p)\n", This, ppEnum);
197 return E_NOTIMPL;
200 static const IEnumVARIANTVtbl HTMLElementCollectionEnumVtbl = {
201 HTMLElementCollectionEnum_QueryInterface,
202 HTMLElementCollectionEnum_AddRef,
203 HTMLElementCollectionEnum_Release,
204 HTMLElementCollectionEnum_Next,
205 HTMLElementCollectionEnum_Skip,
206 HTMLElementCollectionEnum_Reset,
207 HTMLElementCollectionEnum_Clone
210 static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
212 return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
215 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
216 REFIID riid, void **ppv)
218 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
220 *ppv = NULL;
222 if(IsEqualGUID(&IID_IUnknown, riid)) {
223 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
224 *ppv = &This->IHTMLElementCollection_iface;
225 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
226 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This, ppv);
227 *ppv = &This->IHTMLElementCollection_iface;
228 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
229 return *ppv ? S_OK : E_NOINTERFACE;
232 if(*ppv) {
233 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
234 return S_OK;
237 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
238 return E_NOINTERFACE;
241 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
243 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
244 LONG ref = InterlockedIncrement(&This->ref);
246 TRACE("(%p) ref=%d\n", This, ref);
248 return ref;
251 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
253 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
254 LONG ref = InterlockedDecrement(&This->ref);
256 TRACE("(%p) ref=%d\n", This, ref);
258 if(!ref) {
259 unsigned i;
261 for(i=0; i < This->len; i++)
262 node_release(&This->elems[i]->node);
263 heap_free(This->elems);
265 release_dispex(&This->dispex);
266 heap_free(This);
269 return ref;
272 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
273 UINT *pctinfo)
275 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
276 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
279 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
280 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
282 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
283 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
286 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
287 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
289 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
290 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
291 lcid, rgDispId);
294 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
295 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
296 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
298 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
299 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
300 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
303 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
304 BSTR *String)
306 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
307 FIXME("(%p)->(%p)\n", This, String);
308 return E_NOTIMPL;
311 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
312 LONG v)
314 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
315 FIXME("(%p)->(%d)\n", This, v);
316 return E_NOTIMPL;
319 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
320 LONG *p)
322 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
324 TRACE("(%p)->(%p)\n", This, p);
326 *p = This->len;
327 return S_OK;
330 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
331 IUnknown **p)
333 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
334 HTMLElementCollectionEnum *ret;
336 TRACE("(%p)->(%p)\n", This, p);
338 ret = heap_alloc(sizeof(*ret));
339 if(!ret)
340 return E_OUTOFMEMORY;
342 ret->IEnumVARIANT_iface.lpVtbl = &HTMLElementCollectionEnumVtbl;
343 ret->ref = 1;
344 ret->iter = 0;
346 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
347 ret->col = This;
349 *p = (IUnknown*)&ret->IEnumVARIANT_iface;
350 return S_OK;
353 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
355 BSTR elem_id;
356 HRESULT hres;
358 hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
359 if(FAILED(hres)){
360 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
361 return FALSE;
364 if(elem_id && !strcmpW(elem_id, name)) {
365 SysFreeString(elem_id);
366 return TRUE;
369 SysFreeString(elem_id);
370 return FALSE;
373 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
375 const PRUnichar *str;
376 nsAString nsstr, nsname;
377 BOOL ret = FALSE;
378 nsresult nsres;
380 static const PRUnichar nameW[] = {'n','a','m','e',0};
382 if(!elem->nselem)
383 return FALSE;
385 nsAString_Init(&nsstr, NULL);
386 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
387 nsAString_GetData(&nsstr, &str);
388 if(!strcmpiW(str, name)) {
389 nsAString_Finish(&nsstr);
390 return TRUE;
393 nsAString_InitDepend(&nsname, nameW);
394 nsres = nsIDOMHTMLElement_GetAttribute(elem->nselem, &nsname, &nsstr);
395 nsAString_Finish(&nsname);
396 if(NS_SUCCEEDED(nsres)) {
397 nsAString_GetData(&nsstr, &str);
398 ret = !strcmpiW(str, name);
401 nsAString_Finish(&nsstr);
402 return ret;
405 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
407 if(idx < This->len) {
408 *ret = (IDispatch*)This->elems[idx];
409 IDispatch_AddRef(*ret);
412 return S_OK;
415 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
416 VARIANT name, VARIANT index, IDispatch **pdisp)
418 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
419 HRESULT hres = S_OK;
421 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
423 *pdisp = NULL;
425 switch(V_VT(&name)) {
426 case VT_I4:
427 if(V_I4(&name) < 0)
428 return E_INVALIDARG;
429 hres = get_item_idx(This, V_I4(&name), pdisp);
430 break;
432 case VT_UINT:
433 hres = get_item_idx(This, V_UINT(&name), pdisp);
434 break;
436 case VT_BSTR: {
437 DWORD i;
439 if(V_VT(&index) == VT_I4) {
440 LONG idx = V_I4(&index);
442 if(idx < 0)
443 return E_INVALIDARG;
445 for(i=0; i<This->len; i++) {
446 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
447 break;
450 if(i != This->len) {
451 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
452 IDispatch_AddRef(*pdisp);
454 }else {
455 elem_vector_t buf = {NULL, 0, 8};
457 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
459 for(i=0; i<This->len; i++) {
460 if(is_elem_name(This->elems[i], V_BSTR(&name))) {
461 node_addref(&This->elems[i]->node);
462 elem_vector_add(&buf, This->elems[i]);
466 if(buf.len > 1) {
467 elem_vector_normalize(&buf);
468 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
469 }else {
470 if(buf.len == 1) {
471 /* Already AddRef-ed */
472 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
475 heap_free(buf.buf);
478 break;
481 default:
482 FIXME("Unsupported name %s\n", debugstr_variant(&name));
483 hres = E_NOTIMPL;
486 if(SUCCEEDED(hres))
487 TRACE("returning %p\n", *pdisp);
488 return hres;
491 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
492 VARIANT tagName, IDispatch **pdisp)
494 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
495 DWORD i;
496 nsAString tag_str;
497 const PRUnichar *tag;
498 elem_vector_t buf = {NULL, 0, 8};
500 if(V_VT(&tagName) != VT_BSTR) {
501 WARN("Invalid arg\n");
502 return DISP_E_MEMBERNOTFOUND;
505 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
507 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
509 nsAString_Init(&tag_str, NULL);
511 for(i=0; i<This->len; i++) {
512 if(!This->elems[i]->nselem)
513 continue;
515 nsIDOMHTMLElement_GetTagName(This->elems[i]->nselem, &tag_str);
516 nsAString_GetData(&tag_str, &tag);
518 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
519 V_BSTR(&tagName), -1) == CSTR_EQUAL) {
520 node_addref(&This->elems[i]->node);
521 elem_vector_add(&buf, This->elems[i]);
525 nsAString_Finish(&tag_str);
526 elem_vector_normalize(&buf);
528 TRACE("fount %d tags\n", buf.len);
530 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
531 return S_OK;
534 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
535 HTMLElementCollection_QueryInterface,
536 HTMLElementCollection_AddRef,
537 HTMLElementCollection_Release,
538 HTMLElementCollection_GetTypeInfoCount,
539 HTMLElementCollection_GetTypeInfo,
540 HTMLElementCollection_GetIDsOfNames,
541 HTMLElementCollection_Invoke,
542 HTMLElementCollection_toString,
543 HTMLElementCollection_put_length,
544 HTMLElementCollection_get_length,
545 HTMLElementCollection_get__newEnum,
546 HTMLElementCollection_item,
547 HTMLElementCollection_tags
550 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
552 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
555 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
557 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
559 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
560 WCHAR *ptr;
561 DWORD idx=0;
563 if(!*name)
564 return DISP_E_UNKNOWNNAME;
566 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
567 idx = idx*10 + (*ptr-'0');
569 if(*ptr) {
570 /* the name contains alpha characters, so search by name & id */
571 for(idx = 0; idx < This->len; ++idx) {
572 if(is_elem_id(This->elems[idx], name) ||
573 is_elem_name(This->elems[idx], name))
574 break;
578 if(idx >= This->len)
579 return DISP_E_UNKNOWNNAME;
581 *dispid = DISPID_ELEMCOL_0 + idx;
582 TRACE("ret %x\n", *dispid);
583 return S_OK;
586 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
587 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
589 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
590 DWORD idx;
592 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
594 idx = id - DISPID_ELEMCOL_0;
595 if(idx >= This->len)
596 return DISP_E_UNKNOWNNAME;
598 switch(flags) {
599 case DISPATCH_PROPERTYGET:
600 V_VT(res) = VT_DISPATCH;
601 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
602 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
603 break;
604 default:
605 FIXME("unimplemented flags %x\n", flags);
606 return E_NOTIMPL;
609 return S_OK;
612 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
613 NULL,
614 HTMLElementCollection_get_dispid,
615 HTMLElementCollection_invoke,
616 NULL
619 static const tid_t HTMLElementCollection_iface_tids[] = {
620 IHTMLElementCollection_tid,
624 static dispex_static_data_t HTMLElementCollection_dispex = {
625 &HTMLElementColection_dispex_vtbl,
626 DispHTMLElementCollection_tid,
627 NULL,
628 HTMLElementCollection_iface_tids
631 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
633 nsIDOMNodeList *nsnode_list;
634 nsIDOMNode *iter;
635 UINT32 list_len = 0, i;
636 nsresult nsres;
637 HRESULT hres;
639 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
640 if(NS_FAILED(nsres)) {
641 ERR("GetChildNodes failed: %08x\n", nsres);
642 return;
645 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
646 if(!list_len)
647 return;
649 for(i=0; i<list_len; i++) {
650 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
651 if(NS_FAILED(nsres)) {
652 ERR("Item failed: %08x\n", nsres);
653 continue;
656 if(is_elem_node(iter)) {
657 HTMLDOMNode *node;
659 hres = get_node(doc, iter, TRUE, &node);
660 if(FAILED(hres)) {
661 FIXME("get_node failed: %08x\n", hres);
662 continue;
665 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
666 create_all_list(doc, node, buf);
671 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
673 elem_vector_t buf = {NULL, 0, 8};
675 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
677 if(include_root) {
678 node_addref(node);
679 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
681 create_all_list(node->doc, node, &buf);
682 elem_vector_normalize(&buf);
684 return HTMLElementCollection_Create(buf.buf, buf.len);
687 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
689 UINT32 length = 0, i;
690 HTMLDOMNode *node;
691 elem_vector_t buf;
692 HRESULT hres;
694 nsIDOMNodeList_GetLength(nslist, &length);
696 buf.len = 0;
697 buf.size = length;
698 if(length) {
699 nsIDOMNode *nsnode;
701 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
703 for(i=0; i<length; i++) {
704 nsIDOMNodeList_Item(nslist, i, &nsnode);
705 if(is_elem_node(nsnode)) {
706 hres = get_node(doc, nsnode, TRUE, &node);
707 if(FAILED(hres))
708 continue;
709 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
711 nsIDOMNode_Release(nsnode);
714 elem_vector_normalize(&buf);
715 }else {
716 buf.buf = NULL;
719 return HTMLElementCollection_Create(buf.buf, buf.len);
722 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, nsIDOMHTMLCollection *nscol)
724 UINT32 length = 0, i;
725 elem_vector_t buf;
726 HTMLDOMNode *node;
727 HRESULT hres = S_OK;
729 nsIDOMHTMLCollection_GetLength(nscol, &length);
731 buf.len = buf.size = length;
732 if(buf.len) {
733 nsIDOMNode *nsnode;
735 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
737 for(i=0; i<length; i++) {
738 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
739 hres = get_node(doc, nsnode, TRUE, &node);
740 nsIDOMNode_Release(nsnode);
741 if(FAILED(hres))
742 break;
743 buf.buf[i] = elem_from_HTMLDOMNode(node);
745 }else {
746 buf.buf = NULL;
749 if(FAILED(hres)) {
750 heap_free(buf.buf);
751 return NULL;
754 return HTMLElementCollection_Create(buf.buf, buf.len);
757 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len)
759 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
761 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
762 ret->ref = 1;
763 ret->elems = elems;
764 ret->len = len;
766 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
767 &HTMLElementCollection_dispex);
769 TRACE("ret=%p len=%d\n", ret, len);
771 return &ret->IHTMLElementCollection_iface;