nsiproxy: Introduce IOCTL_NSIPROXY_WINE_GET_ALL_PARAMETERS.
[wine.git] / dlls / mshtml / htmlelemcol.c
blobf4722d9672920001cd0aa9191e0513f48fdc8b18
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>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 typedef struct {
36 DispatchEx dispex;
37 IHTMLElementCollection IHTMLElementCollection_iface;
39 HTMLElement **elems;
40 DWORD len;
42 LONG ref;
43 } HTMLElementCollection;
45 typedef struct {
46 IEnumVARIANT IEnumVARIANT_iface;
48 LONG ref;
50 ULONG iter;
51 HTMLElementCollection *col;
52 } HTMLElementCollectionEnum;
54 typedef struct {
55 HTMLElement **buf;
56 DWORD len;
57 DWORD size;
58 } elem_vector_t;
60 /* FIXME: Handle it better way */
61 static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
63 return CONTAINING_RECORD(iface, HTMLElement, node);
66 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement**,DWORD,compat_mode_t);
68 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
70 if(buf->len == buf->size) {
71 buf->size <<= 1;
72 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement*));
75 buf->buf[buf->len++] = elem;
78 static void elem_vector_normalize(elem_vector_t *buf)
80 if(!buf->len) {
81 heap_free(buf->buf);
82 buf->buf = NULL;
83 }else if(buf->size > buf->len) {
84 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement*));
87 buf->size = buf->len;
90 static inline BOOL is_elem_node(nsIDOMNode *node)
92 UINT16 type=0;
94 nsIDOMNode_GetNodeType(node, &type);
96 return type == ELEMENT_NODE || type == COMMENT_NODE;
99 static inline HTMLElementCollectionEnum *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
101 return CONTAINING_RECORD(iface, HTMLElementCollectionEnum, IEnumVARIANT_iface);
104 static HRESULT WINAPI HTMLElementCollectionEnum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
106 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
108 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
110 if(IsEqualGUID(riid, &IID_IUnknown)) {
111 *ppv = &This->IEnumVARIANT_iface;
112 }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
113 *ppv = &This->IEnumVARIANT_iface;
114 }else {
115 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
116 *ppv = NULL;
117 return E_NOINTERFACE;
120 IUnknown_AddRef((IUnknown*)*ppv);
121 return S_OK;
124 static ULONG WINAPI HTMLElementCollectionEnum_AddRef(IEnumVARIANT *iface)
126 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
127 LONG ref = InterlockedIncrement(&This->ref);
129 TRACE("(%p) ref=%d\n", This, ref);
131 return ref;
134 static ULONG WINAPI HTMLElementCollectionEnum_Release(IEnumVARIANT *iface)
136 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
137 LONG ref = InterlockedDecrement(&This->ref);
139 TRACE("(%p) ref=%d\n", This, ref);
141 if(!ref) {
142 IHTMLElementCollection_Release(&This->col->IHTMLElementCollection_iface);
143 heap_free(This);
146 return ref;
149 static HRESULT WINAPI HTMLElementCollectionEnum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
151 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
152 ULONG fetched = 0;
154 TRACE("(%p)->(%d %p %p)\n", This, celt, rgVar, pCeltFetched);
156 while(This->iter+fetched < This->col->len && fetched < celt) {
157 V_VT(rgVar+fetched) = VT_DISPATCH;
158 V_DISPATCH(rgVar+fetched) = (IDispatch*)&This->col->elems[This->iter+fetched]->IHTMLElement_iface;
159 IDispatch_AddRef(V_DISPATCH(rgVar+fetched));
160 fetched++;
163 This->iter += fetched;
164 if(pCeltFetched)
165 *pCeltFetched = fetched;
166 return fetched == celt ? S_OK : S_FALSE;
169 static HRESULT WINAPI HTMLElementCollectionEnum_Skip(IEnumVARIANT *iface, ULONG celt)
171 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
173 TRACE("(%p)->(%d)\n", This, celt);
175 if(This->iter + celt > This->col->len) {
176 This->iter = This->col->len;
177 return S_FALSE;
180 This->iter += celt;
181 return S_OK;
184 static HRESULT WINAPI HTMLElementCollectionEnum_Reset(IEnumVARIANT *iface)
186 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
188 TRACE("(%p)->()\n", This);
190 This->iter = 0;
191 return S_OK;
194 static HRESULT WINAPI HTMLElementCollectionEnum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
196 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
197 FIXME("(%p)->(%p)\n", This, ppEnum);
198 return E_NOTIMPL;
201 static const IEnumVARIANTVtbl HTMLElementCollectionEnumVtbl = {
202 HTMLElementCollectionEnum_QueryInterface,
203 HTMLElementCollectionEnum_AddRef,
204 HTMLElementCollectionEnum_Release,
205 HTMLElementCollectionEnum_Next,
206 HTMLElementCollectionEnum_Skip,
207 HTMLElementCollectionEnum_Reset,
208 HTMLElementCollectionEnum_Clone
211 static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
213 return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
216 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
217 REFIID riid, void **ppv)
219 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
221 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
223 if(IsEqualGUID(&IID_IUnknown, riid)) {
224 *ppv = &This->IHTMLElementCollection_iface;
225 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
226 *ppv = &This->IHTMLElementCollection_iface;
227 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
228 return *ppv ? S_OK : E_NOINTERFACE;
229 }else {
230 *ppv = NULL;
231 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
232 return E_NOINTERFACE;
235 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
236 return S_OK;
239 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
241 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
242 LONG ref = InterlockedIncrement(&This->ref);
244 TRACE("(%p) ref=%d\n", This, ref);
246 return ref;
249 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
251 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
252 LONG ref = InterlockedDecrement(&This->ref);
254 TRACE("(%p) ref=%d\n", This, ref);
256 if(!ref) {
257 unsigned i;
259 for(i=0; i < This->len; i++)
260 node_release(&This->elems[i]->node);
261 heap_free(This->elems);
263 release_dispex(&This->dispex);
264 heap_free(This);
267 return ref;
270 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
271 UINT *pctinfo)
273 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
274 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
277 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
278 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
280 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
281 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
284 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
285 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
287 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
288 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
289 lcid, rgDispId);
292 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
293 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
294 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
296 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
297 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
298 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
301 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
302 BSTR *String)
304 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
305 FIXME("(%p)->(%p)\n", This, String);
306 return E_NOTIMPL;
309 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
310 LONG v)
312 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
313 FIXME("(%p)->(%d)\n", This, v);
314 return E_NOTIMPL;
317 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
318 LONG *p)
320 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
322 TRACE("(%p)->(%p)\n", This, p);
324 *p = This->len;
325 return S_OK;
328 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
329 IUnknown **p)
331 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
332 HTMLElementCollectionEnum *ret;
334 TRACE("(%p)->(%p)\n", This, p);
336 ret = heap_alloc(sizeof(*ret));
337 if(!ret)
338 return E_OUTOFMEMORY;
340 ret->IEnumVARIANT_iface.lpVtbl = &HTMLElementCollectionEnumVtbl;
341 ret->ref = 1;
342 ret->iter = 0;
344 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
345 ret->col = This;
347 *p = (IUnknown*)&ret->IEnumVARIANT_iface;
348 return S_OK;
351 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
353 BSTR elem_id;
354 HRESULT hres;
356 hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
357 if(FAILED(hres)){
358 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
359 return FALSE;
362 if(elem_id && !wcscmp(elem_id, name)) {
363 SysFreeString(elem_id);
364 return TRUE;
367 SysFreeString(elem_id);
368 return FALSE;
371 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
373 const PRUnichar *str;
374 nsAString nsstr;
375 BOOL ret = FALSE;
376 nsresult nsres;
378 if(!elem->dom_element)
379 return FALSE;
381 nsAString_Init(&nsstr, NULL);
382 nsIDOMElement_GetId(elem->dom_element, &nsstr);
383 nsAString_GetData(&nsstr, &str);
384 if(!wcsicmp(str, name)) {
385 nsAString_Finish(&nsstr);
386 return TRUE;
389 nsres = get_elem_attr_value(elem->dom_element, L"name", &nsstr, &str);
390 if(NS_SUCCEEDED(nsres)) {
391 ret = !wcsicmp(str, name);
392 nsAString_Finish(&nsstr);
395 return ret;
398 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
400 if(idx < This->len) {
401 *ret = (IDispatch*)&This->elems[idx]->node.event_target.dispex.IDispatchEx_iface;
402 IDispatch_AddRef(*ret);
405 return S_OK;
408 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
409 VARIANT name, VARIANT index, IDispatch **pdisp)
411 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
412 HRESULT hres = S_OK;
414 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
416 *pdisp = NULL;
418 switch(V_VT(&name)) {
419 case VT_I4:
420 case VT_INT:
421 if(V_I4(&name) < 0)
422 return dispex_compat_mode(&This->dispex) < COMPAT_MODE_IE9 ? E_INVALIDARG : S_OK;
423 hres = get_item_idx(This, V_I4(&name), pdisp);
424 break;
426 case VT_UI4:
427 case VT_UINT:
428 hres = get_item_idx(This, V_UINT(&name), pdisp);
429 break;
431 case VT_BSTR: {
432 DWORD i;
434 if(V_VT(&index) == VT_I4) {
435 LONG idx = V_I4(&index);
437 if(idx < 0)
438 return E_INVALIDARG;
440 for(i=0; i<This->len; i++) {
441 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
442 break;
445 if(i != This->len) {
446 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
447 IDispatch_AddRef(*pdisp);
449 }else {
450 elem_vector_t buf = {NULL, 0, 8};
452 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
454 for(i=0; i<This->len; i++) {
455 if(is_elem_name(This->elems[i], V_BSTR(&name))) {
456 node_addref(&This->elems[i]->node);
457 elem_vector_add(&buf, This->elems[i]);
461 if(buf.len > 1) {
462 elem_vector_normalize(&buf);
463 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len,
464 dispex_compat_mode(&This->dispex));
465 }else {
466 if(buf.len == 1) {
467 /* Already AddRef-ed */
468 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
471 heap_free(buf.buf);
474 break;
477 default:
478 FIXME("Unsupported name %s\n", debugstr_variant(&name));
479 hres = E_NOTIMPL;
482 if(SUCCEEDED(hres))
483 TRACE("returning %p\n", *pdisp);
484 return hres;
487 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
488 VARIANT tagName, IDispatch **pdisp)
490 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
491 DWORD i;
492 nsAString tag_str;
493 const PRUnichar *tag;
494 elem_vector_t buf = {NULL, 0, 8};
496 if(V_VT(&tagName) != VT_BSTR) {
497 WARN("Invalid arg\n");
498 return DISP_E_MEMBERNOTFOUND;
501 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
503 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
505 nsAString_Init(&tag_str, NULL);
507 for(i=0; i<This->len; i++) {
508 if(!This->elems[i]->dom_element)
509 continue;
511 nsIDOMElement_GetTagName(This->elems[i]->dom_element, &tag_str);
512 nsAString_GetData(&tag_str, &tag);
514 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
515 V_BSTR(&tagName), -1) == CSTR_EQUAL) {
516 node_addref(&This->elems[i]->node);
517 elem_vector_add(&buf, This->elems[i]);
521 nsAString_Finish(&tag_str);
522 elem_vector_normalize(&buf);
524 TRACE("found %d tags\n", buf.len);
526 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len,
527 dispex_compat_mode(&This->dispex));
528 return S_OK;
531 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
532 HTMLElementCollection_QueryInterface,
533 HTMLElementCollection_AddRef,
534 HTMLElementCollection_Release,
535 HTMLElementCollection_GetTypeInfoCount,
536 HTMLElementCollection_GetTypeInfo,
537 HTMLElementCollection_GetIDsOfNames,
538 HTMLElementCollection_Invoke,
539 HTMLElementCollection_toString,
540 HTMLElementCollection_put_length,
541 HTMLElementCollection_get_length,
542 HTMLElementCollection_get__newEnum,
543 HTMLElementCollection_item,
544 HTMLElementCollection_tags
547 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
549 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
552 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
554 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
556 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
557 WCHAR *ptr;
558 DWORD idx=0;
560 if(!*name)
561 return DISP_E_UNKNOWNNAME;
563 for(ptr = name; *ptr && is_digit(*ptr); ptr++)
564 idx = idx*10 + (*ptr-'0');
566 if(*ptr) {
567 /* the name contains alpha characters, so search by name & id */
568 for(idx = 0; idx < This->len; ++idx) {
569 if(is_elem_id(This->elems[idx], name) ||
570 is_elem_name(This->elems[idx], name))
571 break;
575 if(idx >= This->len)
576 return DISP_E_UNKNOWNNAME;
578 *dispid = DISPID_ELEMCOL_0 + idx;
579 TRACE("ret %x\n", *dispid);
580 return S_OK;
583 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
584 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
586 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
587 DWORD idx;
589 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
591 idx = id - DISPID_ELEMCOL_0;
592 if(idx >= This->len)
593 return DISP_E_UNKNOWNNAME;
595 switch(flags) {
596 case DISPATCH_PROPERTYGET:
597 V_VT(res) = VT_DISPATCH;
598 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
599 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
600 break;
601 default:
602 FIXME("unimplemented flags %x\n", flags);
603 return E_NOTIMPL;
606 return S_OK;
609 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
610 NULL,
611 HTMLElementCollection_get_dispid,
612 HTMLElementCollection_invoke,
613 NULL
616 static const tid_t HTMLElementCollection_iface_tids[] = {
617 IHTMLElementCollection_tid,
621 static dispex_static_data_t HTMLElementCollection_dispex = {
622 &HTMLElementColection_dispex_vtbl,
623 DispHTMLElementCollection_tid,
624 HTMLElementCollection_iface_tids
627 static void create_all_list(HTMLDOMNode *elem, elem_vector_t *buf)
629 nsIDOMNodeList *nsnode_list;
630 nsIDOMNode *iter;
631 UINT32 list_len = 0, i;
632 nsresult nsres;
633 HRESULT hres;
635 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
636 if(NS_FAILED(nsres)) {
637 ERR("GetChildNodes failed: %08x\n", nsres);
638 return;
641 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
642 if(!list_len)
643 return;
645 for(i=0; i<list_len; i++) {
646 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
647 if(NS_FAILED(nsres)) {
648 ERR("Item failed: %08x\n", nsres);
649 continue;
652 if(is_elem_node(iter)) {
653 HTMLDOMNode *node;
655 hres = get_node(iter, TRUE, &node);
656 if(FAILED(hres)) {
657 FIXME("get_node failed: %08x\n", hres);
658 continue;
661 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
662 create_all_list(node, buf);
667 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
669 elem_vector_t buf = {NULL, 0, 8};
671 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
673 if(include_root) {
674 node_addref(node);
675 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
677 create_all_list(node, &buf);
678 elem_vector_normalize(&buf);
680 return HTMLElementCollection_Create(buf.buf, buf.len,
681 dispex_compat_mode(&node->event_target.dispex));
684 IHTMLElementCollection *create_collection_from_nodelist(nsIDOMNodeList *nslist, compat_mode_t compat_mode)
686 UINT32 length = 0, i;
687 HTMLDOMNode *node;
688 elem_vector_t buf;
689 HRESULT hres;
691 nsIDOMNodeList_GetLength(nslist, &length);
693 buf.len = 0;
694 buf.size = length;
695 if(length) {
696 nsIDOMNode *nsnode;
698 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
700 for(i=0; i<length; i++) {
701 nsIDOMNodeList_Item(nslist, i, &nsnode);
702 if(is_elem_node(nsnode)) {
703 hres = get_node(nsnode, TRUE, &node);
704 if(FAILED(hres))
705 continue;
706 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
708 nsIDOMNode_Release(nsnode);
711 elem_vector_normalize(&buf);
712 }else {
713 buf.buf = NULL;
716 return HTMLElementCollection_Create(buf.buf, buf.len, compat_mode);
719 IHTMLElementCollection *create_collection_from_htmlcol(nsIDOMHTMLCollection *nscol, compat_mode_t compat_mode)
721 UINT32 length = 0, i;
722 elem_vector_t buf;
723 HTMLDOMNode *node;
724 HRESULT hres = S_OK;
726 if(nscol)
727 nsIDOMHTMLCollection_GetLength(nscol, &length);
729 buf.len = buf.size = length;
730 if(buf.len) {
731 nsIDOMNode *nsnode;
733 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
735 for(i=0; i<length; i++) {
736 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
737 hres = get_node(nsnode, TRUE, &node);
738 nsIDOMNode_Release(nsnode);
739 if(FAILED(hres))
740 break;
741 buf.buf[i] = elem_from_HTMLDOMNode(node);
743 }else {
744 buf.buf = NULL;
747 if(FAILED(hres)) {
748 heap_free(buf.buf);
749 return NULL;
752 return HTMLElementCollection_Create(buf.buf, buf.len, compat_mode);
755 HRESULT get_elem_source_index(HTMLElement *elem, LONG *ret)
757 elem_vector_t buf = {NULL, 0, 8};
758 nsIDOMNode *parent_node, *iter;
759 UINT16 parent_type;
760 HTMLDOMNode *node;
761 int i;
762 nsresult nsres;
763 HRESULT hres;
765 iter = elem->node.nsnode;
766 nsIDOMNode_AddRef(iter);
768 /* Find document or document fragment parent. */
769 while(1) {
770 nsres = nsIDOMNode_GetParentNode(iter, &parent_node);
771 nsIDOMNode_Release(iter);
772 assert(nsres == NS_OK);
773 if(!parent_node)
774 break;
776 nsres = nsIDOMNode_GetNodeType(parent_node, &parent_type);
777 assert(nsres == NS_OK);
779 if(parent_type != ELEMENT_NODE) {
780 if(parent_type != DOCUMENT_NODE && parent_type != DOCUMENT_FRAGMENT_NODE)
781 FIXME("Unexpected parent_type %d\n", parent_type);
782 break;
785 iter = parent_node;
788 if(!parent_node) {
789 *ret = -1;
790 return S_OK;
793 hres = get_node(parent_node, TRUE, &node);
794 nsIDOMNode_Release(parent_node);
795 if(FAILED(hres))
796 return hres;
798 /* Create all children collection and find the element in it.
799 * This could be optimized if we ever find the reason. */
800 buf.buf = heap_alloc(buf.size*sizeof(*buf.buf));
801 if(!buf.buf) {
802 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
803 return E_OUTOFMEMORY;
806 create_all_list(node, &buf);
808 for(i=0; i < buf.len; i++) {
809 if(buf.buf[i] == elem)
810 break;
812 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
813 heap_free(buf.buf);
814 if(i == buf.len) {
815 FIXME("The element is not in parent's child list?\n");
816 return E_UNEXPECTED;
819 *ret = i;
820 return S_OK;
823 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len,
824 compat_mode_t compat_mode)
826 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
828 if (!ret)
829 return NULL;
831 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
832 ret->ref = 1;
833 ret->elems = elems;
834 ret->len = len;
836 init_dispatch(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
837 &HTMLElementCollection_dispex, compat_mode);
839 TRACE("ret=%p len=%d\n", ret, len);
841 return &ret->IHTMLElementCollection_iface;