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 IHTMLElementCollection IHTMLElementCollection_iface
;
40 } HTMLElementCollection
;
43 IEnumVARIANT IEnumVARIANT_iface
;
48 HTMLElementCollection
*col
;
49 } HTMLElementCollectionEnum
;
57 /* FIXME: Handle it better way */
58 static inline HTMLElement
*elem_from_HTMLDOMNode(HTMLDOMNode
*iface
)
60 return CONTAINING_RECORD(iface
, HTMLElement
, node
);
63 static IHTMLElementCollection
*HTMLElementCollection_Create(HTMLElement
**,DWORD
,compat_mode_t
);
65 static void elem_vector_add(elem_vector_t
*buf
, HTMLElement
*elem
)
67 if(buf
->len
== buf
->size
) {
69 buf
->buf
= realloc(buf
->buf
, buf
->size
* sizeof(HTMLElement
*));
72 buf
->buf
[buf
->len
++] = elem
;
75 static void elem_vector_normalize(elem_vector_t
*buf
)
80 }else if(buf
->size
> buf
->len
) {
81 buf
->buf
= realloc(buf
->buf
, buf
->len
*sizeof(HTMLElement
*));
87 static inline BOOL
is_elem_node(nsIDOMNode
*node
)
91 nsIDOMNode_GetNodeType(node
, &type
);
93 return type
== ELEMENT_NODE
|| type
== COMMENT_NODE
;
96 static inline HTMLElementCollectionEnum
*impl_from_IEnumVARIANT(IEnumVARIANT
*iface
)
98 return CONTAINING_RECORD(iface
, HTMLElementCollectionEnum
, IEnumVARIANT_iface
);
101 static HRESULT WINAPI
HTMLElementCollectionEnum_QueryInterface(IEnumVARIANT
*iface
, REFIID riid
, void **ppv
)
103 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
105 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
107 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
108 *ppv
= &This
->IEnumVARIANT_iface
;
109 }else if(IsEqualGUID(riid
, &IID_IEnumVARIANT
)) {
110 *ppv
= &This
->IEnumVARIANT_iface
;
112 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid
));
114 return E_NOINTERFACE
;
117 IUnknown_AddRef((IUnknown
*)*ppv
);
121 static ULONG WINAPI
HTMLElementCollectionEnum_AddRef(IEnumVARIANT
*iface
)
123 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
124 LONG ref
= InterlockedIncrement(&This
->ref
);
126 TRACE("(%p) ref=%ld\n", This
, ref
);
131 static ULONG WINAPI
HTMLElementCollectionEnum_Release(IEnumVARIANT
*iface
)
133 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
134 LONG ref
= InterlockedDecrement(&This
->ref
);
136 TRACE("(%p) ref=%ld\n", This
, ref
);
139 IHTMLElementCollection_Release(&This
->col
->IHTMLElementCollection_iface
);
146 static HRESULT WINAPI
HTMLElementCollectionEnum_Next(IEnumVARIANT
*iface
, ULONG celt
, VARIANT
*rgVar
, ULONG
*pCeltFetched
)
148 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
151 TRACE("(%p)->(%ld %p %p)\n", This
, celt
, rgVar
, pCeltFetched
);
153 while(This
->iter
+fetched
< This
->col
->len
&& fetched
< celt
) {
154 V_VT(rgVar
+fetched
) = VT_DISPATCH
;
155 V_DISPATCH(rgVar
+fetched
) = (IDispatch
*)&This
->col
->elems
[This
->iter
+fetched
]->IHTMLElement_iface
;
156 IDispatch_AddRef(V_DISPATCH(rgVar
+fetched
));
160 This
->iter
+= fetched
;
162 *pCeltFetched
= fetched
;
163 return fetched
== celt
? S_OK
: S_FALSE
;
166 static HRESULT WINAPI
HTMLElementCollectionEnum_Skip(IEnumVARIANT
*iface
, ULONG celt
)
168 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
170 TRACE("(%p)->(%ld)\n", This
, celt
);
172 if(This
->iter
+ celt
> This
->col
->len
) {
173 This
->iter
= This
->col
->len
;
181 static HRESULT WINAPI
HTMLElementCollectionEnum_Reset(IEnumVARIANT
*iface
)
183 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
185 TRACE("(%p)->()\n", This
);
191 static HRESULT WINAPI
HTMLElementCollectionEnum_Clone(IEnumVARIANT
*iface
, IEnumVARIANT
**ppEnum
)
193 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
194 FIXME("(%p)->(%p)\n", This
, ppEnum
);
198 static const IEnumVARIANTVtbl HTMLElementCollectionEnumVtbl
= {
199 HTMLElementCollectionEnum_QueryInterface
,
200 HTMLElementCollectionEnum_AddRef
,
201 HTMLElementCollectionEnum_Release
,
202 HTMLElementCollectionEnum_Next
,
203 HTMLElementCollectionEnum_Skip
,
204 HTMLElementCollectionEnum_Reset
,
205 HTMLElementCollectionEnum_Clone
208 static inline HTMLElementCollection
*impl_from_IHTMLElementCollection(IHTMLElementCollection
*iface
)
210 return CONTAINING_RECORD(iface
, HTMLElementCollection
, IHTMLElementCollection_iface
);
213 static HRESULT WINAPI
HTMLElementCollection_QueryInterface(IHTMLElementCollection
*iface
,
214 REFIID riid
, void **ppv
)
216 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
217 return IDispatchEx_QueryInterface(&This
->dispex
.IDispatchEx_iface
, riid
, ppv
);
220 static ULONG WINAPI
HTMLElementCollection_AddRef(IHTMLElementCollection
*iface
)
222 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
223 return IDispatchEx_AddRef(&This
->dispex
.IDispatchEx_iface
);
226 static ULONG WINAPI
HTMLElementCollection_Release(IHTMLElementCollection
*iface
)
228 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
229 return IDispatchEx_Release(&This
->dispex
.IDispatchEx_iface
);
232 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection
*iface
,
235 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
236 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
239 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfo(IHTMLElementCollection
*iface
,
240 UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
242 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
243 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
246 static HRESULT WINAPI
HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection
*iface
,
247 REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
249 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
250 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
254 static HRESULT WINAPI
HTMLElementCollection_Invoke(IHTMLElementCollection
*iface
,
255 DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
256 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
258 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
259 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
260 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
263 static HRESULT WINAPI
HTMLElementCollection_toString(IHTMLElementCollection
*iface
,
266 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
268 TRACE("(%p)->(%p)\n", This
, String
);
270 return dispex_to_string(&This
->dispex
, String
);
273 static HRESULT WINAPI
HTMLElementCollection_put_length(IHTMLElementCollection
*iface
,
276 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
277 FIXME("(%p)->(%ld)\n", This
, v
);
281 static HRESULT WINAPI
HTMLElementCollection_get_length(IHTMLElementCollection
*iface
,
284 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
286 TRACE("(%p)->(%p)\n", This
, p
);
292 static HRESULT WINAPI
HTMLElementCollection_get__newEnum(IHTMLElementCollection
*iface
,
295 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
296 HTMLElementCollectionEnum
*ret
;
298 TRACE("(%p)->(%p)\n", This
, p
);
300 ret
= malloc(sizeof(*ret
));
302 return E_OUTOFMEMORY
;
304 ret
->IEnumVARIANT_iface
.lpVtbl
= &HTMLElementCollectionEnumVtbl
;
308 IHTMLElementCollection_AddRef(&This
->IHTMLElementCollection_iface
);
311 *p
= (IUnknown
*)&ret
->IEnumVARIANT_iface
;
315 static BOOL
is_elem_id(HTMLElement
*elem
, LPCWSTR name
)
320 hres
= IHTMLElement_get_id(&elem
->IHTMLElement_iface
, &elem_id
);
322 WARN("IHTMLElement_get_id failed: 0x%08lx\n", hres
);
326 if(elem_id
&& !wcscmp(elem_id
, name
)) {
327 SysFreeString(elem_id
);
331 SysFreeString(elem_id
);
335 static BOOL
is_elem_name(HTMLElement
*elem
, LPCWSTR name
)
337 const PRUnichar
*str
;
342 if(!elem
->dom_element
)
345 nsAString_Init(&nsstr
, NULL
);
346 nsIDOMElement_GetId(elem
->dom_element
, &nsstr
);
347 nsAString_GetData(&nsstr
, &str
);
348 if(!wcsicmp(str
, name
)) {
349 nsAString_Finish(&nsstr
);
353 nsres
= get_elem_attr_value(elem
->dom_element
, L
"name", &nsstr
, &str
);
354 if(NS_SUCCEEDED(nsres
)) {
355 ret
= !wcsicmp(str
, name
);
356 nsAString_Finish(&nsstr
);
362 static HRESULT
get_item_idx(HTMLElementCollection
*This
, UINT idx
, IDispatch
**ret
)
364 if(idx
< This
->len
) {
365 *ret
= (IDispatch
*)&This
->elems
[idx
]->node
.event_target
.dispex
.IDispatchEx_iface
;
366 IDispatch_AddRef(*ret
);
372 static HRESULT WINAPI
HTMLElementCollection_item(IHTMLElementCollection
*iface
,
373 VARIANT name
, VARIANT index
, IDispatch
**pdisp
)
375 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
378 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_variant(&name
), debugstr_variant(&index
), pdisp
);
382 switch(V_VT(&name
)) {
386 return dispex_compat_mode(&This
->dispex
) < COMPAT_MODE_IE9
? E_INVALIDARG
: S_OK
;
387 hres
= get_item_idx(This
, V_I4(&name
), pdisp
);
392 hres
= get_item_idx(This
, V_UINT(&name
), pdisp
);
398 if(V_VT(&index
) == VT_I4
) {
399 LONG idx
= V_I4(&index
);
404 for(i
=0; i
<This
->len
; i
++) {
405 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)) && !idx
--)
410 *pdisp
= (IDispatch
*)&This
->elems
[i
]->IHTMLElement_iface
;
411 IDispatch_AddRef(*pdisp
);
414 elem_vector_t buf
= {NULL
, 0, 8};
416 buf
.buf
= malloc(buf
.size
* sizeof(HTMLElement
*));
418 for(i
=0; i
<This
->len
; i
++) {
419 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
))) {
420 node_addref(&This
->elems
[i
]->node
);
421 elem_vector_add(&buf
, This
->elems
[i
]);
426 elem_vector_normalize(&buf
);
427 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(buf
.buf
, buf
.len
,
428 dispex_compat_mode(&This
->dispex
));
431 /* Already AddRef-ed */
432 *pdisp
= (IDispatch
*)&buf
.buf
[0]->IHTMLElement_iface
;
442 FIXME("Unsupported name %s\n", debugstr_variant(&name
));
447 TRACE("returning %p\n", *pdisp
);
451 static HRESULT WINAPI
HTMLElementCollection_tags(IHTMLElementCollection
*iface
,
452 VARIANT tagName
, IDispatch
**pdisp
)
454 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
457 const PRUnichar
*tag
;
458 elem_vector_t buf
= {NULL
, 0, 8};
460 if(V_VT(&tagName
) != VT_BSTR
) {
461 WARN("Invalid arg\n");
462 return DISP_E_MEMBERNOTFOUND
;
465 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(V_BSTR(&tagName
)), pdisp
);
467 buf
.buf
= malloc(buf
.size
* sizeof(HTMLElement
*));
469 nsAString_Init(&tag_str
, NULL
);
471 for(i
=0; i
<This
->len
; i
++) {
472 if(!This
->elems
[i
]->dom_element
)
475 nsIDOMElement_GetTagName(This
->elems
[i
]->dom_element
, &tag_str
);
476 nsAString_GetData(&tag_str
, &tag
);
478 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, tag
, -1,
479 V_BSTR(&tagName
), -1) == CSTR_EQUAL
) {
480 node_addref(&This
->elems
[i
]->node
);
481 elem_vector_add(&buf
, This
->elems
[i
]);
485 nsAString_Finish(&tag_str
);
486 elem_vector_normalize(&buf
);
488 TRACE("found %ld tags\n", buf
.len
);
490 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(buf
.buf
, buf
.len
,
491 dispex_compat_mode(&This
->dispex
));
495 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl
= {
496 HTMLElementCollection_QueryInterface
,
497 HTMLElementCollection_AddRef
,
498 HTMLElementCollection_Release
,
499 HTMLElementCollection_GetTypeInfoCount
,
500 HTMLElementCollection_GetTypeInfo
,
501 HTMLElementCollection_GetIDsOfNames
,
502 HTMLElementCollection_Invoke
,
503 HTMLElementCollection_toString
,
504 HTMLElementCollection_put_length
,
505 HTMLElementCollection_get_length
,
506 HTMLElementCollection_get__newEnum
,
507 HTMLElementCollection_item
,
508 HTMLElementCollection_tags
511 static inline HTMLElementCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
513 return CONTAINING_RECORD(iface
, HTMLElementCollection
, dispex
);
516 static void *HTMLElementCollection_query_interface(DispatchEx
*dispex
, REFIID riid
)
518 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
520 if(IsEqualGUID(&IID_IHTMLElementCollection
, riid
))
521 return &This
->IHTMLElementCollection_iface
;
526 static void HTMLElementCollection_traverse(DispatchEx
*dispex
, nsCycleCollectionTraversalCallback
*cb
)
528 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
531 for(i
= 0; i
< This
->len
; i
++)
532 note_cc_edge((nsISupports
*)&This
->elems
[i
]->node
.IHTMLDOMNode_iface
, "elem", cb
);
535 static void HTMLElementCollection_unlink(DispatchEx
*dispex
)
537 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
538 unsigned i
, len
= This
->len
;
542 for(i
= 0; i
< len
; i
++)
543 node_release(&This
->elems
[i
]->node
);
548 static void HTMLElementCollection_destructor(DispatchEx
*dispex
)
550 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
554 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
556 static HRESULT
HTMLElementCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
558 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
563 return DISP_E_UNKNOWNNAME
;
565 for(ptr
= name
; *ptr
&& is_digit(*ptr
); ptr
++)
566 idx
= idx
*10 + (*ptr
-'0');
569 /* the name contains alpha characters, so search by name & id */
570 for(idx
= 0; idx
< This
->len
; ++idx
) {
571 if(is_elem_id(This
->elems
[idx
], name
) ||
572 is_elem_name(This
->elems
[idx
], name
))
578 return DISP_E_UNKNOWNNAME
;
580 *dispid
= DISPID_ELEMCOL_0
+ idx
;
581 TRACE("ret %lx\n", *dispid
);
585 static HRESULT
HTMLElementCollection_get_name(DispatchEx
*dispex
, DISPID id
, BSTR
*name
)
587 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
588 DWORD idx
= id
- MSHTML_DISPID_CUSTOM_MIN
;
593 return DISP_E_MEMBERNOTFOUND
;
595 len
= swprintf(buf
, ARRAY_SIZE(buf
), L
"%u", idx
);
596 return (*name
= SysAllocStringLen(buf
, len
)) ? S_OK
: E_OUTOFMEMORY
;
599 static HRESULT
HTMLElementCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
600 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
602 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
605 TRACE("(%p)->(%lx %lx %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
607 idx
= id
- DISPID_ELEMCOL_0
;
609 return DISP_E_MEMBERNOTFOUND
;
612 case DISPATCH_PROPERTYGET
:
613 V_VT(res
) = VT_DISPATCH
;
614 V_DISPATCH(res
) = (IDispatch
*)&This
->elems
[idx
]->IHTMLElement_iface
;
615 IHTMLElement_AddRef(&This
->elems
[idx
]->IHTMLElement_iface
);
618 FIXME("unimplemented flags %x\n", flags
);
625 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl
= {
626 .query_interface
= HTMLElementCollection_query_interface
,
627 .destructor
= HTMLElementCollection_destructor
,
628 .traverse
= HTMLElementCollection_traverse
,
629 .unlink
= HTMLElementCollection_unlink
,
630 .get_dispid
= HTMLElementCollection_get_dispid
,
631 .get_name
= HTMLElementCollection_get_name
,
632 .invoke
= HTMLElementCollection_invoke
,
635 static const tid_t HTMLElementCollection_iface_tids
[] = {
636 IHTMLElementCollection_tid
,
640 static dispex_static_data_t HTMLElementCollection_dispex
= {
642 &HTMLElementColection_dispex_vtbl
,
643 DispHTMLElementCollection_tid
,
644 HTMLElementCollection_iface_tids
647 static void create_all_list(HTMLDOMNode
*elem
, elem_vector_t
*buf
)
649 nsIDOMNodeList
*nsnode_list
;
651 UINT32 list_len
= 0, i
;
655 nsres
= nsIDOMNode_GetChildNodes(elem
->nsnode
, &nsnode_list
);
656 if(NS_FAILED(nsres
)) {
657 ERR("GetChildNodes failed: %08lx\n", nsres
);
661 nsIDOMNodeList_GetLength(nsnode_list
, &list_len
);
663 for(i
=0; i
<list_len
; i
++) {
664 nsres
= nsIDOMNodeList_Item(nsnode_list
, i
, &iter
);
665 if(NS_FAILED(nsres
)) {
666 ERR("Item failed: %08lx\n", nsres
);
670 if(is_elem_node(iter
)) {
673 hres
= get_node(iter
, TRUE
, &node
);
675 FIXME("get_node failed: %08lx\n", hres
);
676 nsIDOMNode_Release(iter
);
680 elem_vector_add(buf
, elem_from_HTMLDOMNode(node
));
681 create_all_list(node
, buf
);
684 nsIDOMNode_Release(iter
);
687 nsIDOMNodeList_Release(nsnode_list
);
690 IHTMLElementCollection
*create_all_collection(HTMLDOMNode
*node
, BOOL include_root
)
692 elem_vector_t buf
= {NULL
, 0, 8};
694 buf
.buf
= malloc(buf
.size
* sizeof(HTMLElement
*));
698 elem_vector_add(&buf
, elem_from_HTMLDOMNode(node
));
700 create_all_list(node
, &buf
);
701 elem_vector_normalize(&buf
);
703 return HTMLElementCollection_Create(buf
.buf
, buf
.len
,
704 dispex_compat_mode(&node
->event_target
.dispex
));
707 IHTMLElementCollection
*create_collection_from_nodelist(nsIDOMNodeList
*nslist
, compat_mode_t compat_mode
)
709 UINT32 length
= 0, i
;
714 nsIDOMNodeList_GetLength(nslist
, &length
);
721 buf
.buf
= malloc(buf
.size
* sizeof(HTMLElement
*));
723 for(i
=0; i
<length
; i
++) {
724 nsIDOMNodeList_Item(nslist
, i
, &nsnode
);
725 if(is_elem_node(nsnode
)) {
726 hres
= get_node(nsnode
, TRUE
, &node
);
729 buf
.buf
[buf
.len
++] = elem_from_HTMLDOMNode(node
);
731 nsIDOMNode_Release(nsnode
);
734 elem_vector_normalize(&buf
);
739 return HTMLElementCollection_Create(buf
.buf
, buf
.len
, compat_mode
);
742 IHTMLElementCollection
*create_collection_from_htmlcol(nsIDOMHTMLCollection
*nscol
, compat_mode_t compat_mode
)
744 UINT32 length
= 0, i
;
750 nsIDOMHTMLCollection_GetLength(nscol
, &length
);
752 buf
.len
= buf
.size
= length
;
756 buf
.buf
= malloc(buf
.size
* sizeof(HTMLElement
*));
758 for(i
=0; i
<length
; i
++) {
759 nsIDOMHTMLCollection_Item(nscol
, i
, &nsnode
);
760 hres
= get_node(nsnode
, TRUE
, &node
);
761 nsIDOMNode_Release(nsnode
);
764 buf
.buf
[i
] = elem_from_HTMLDOMNode(node
);
775 return HTMLElementCollection_Create(buf
.buf
, buf
.len
, compat_mode
);
778 HRESULT
get_elem_source_index(HTMLElement
*elem
, LONG
*ret
)
780 elem_vector_t buf
= {NULL
, 0, 8};
781 nsIDOMNode
*parent_node
, *iter
;
788 iter
= elem
->node
.nsnode
;
789 nsIDOMNode_AddRef(iter
);
791 /* Find document or document fragment parent. */
793 nsres
= nsIDOMNode_GetParentNode(iter
, &parent_node
);
794 nsIDOMNode_Release(iter
);
795 assert(nsres
== NS_OK
);
799 nsres
= nsIDOMNode_GetNodeType(parent_node
, &parent_type
);
800 assert(nsres
== NS_OK
);
802 if(parent_type
!= ELEMENT_NODE
) {
803 if(parent_type
!= DOCUMENT_NODE
&& parent_type
!= DOCUMENT_FRAGMENT_NODE
)
804 FIXME("Unexpected parent_type %d\n", parent_type
);
816 hres
= get_node(parent_node
, TRUE
, &node
);
817 nsIDOMNode_Release(parent_node
);
821 /* Create all children collection and find the element in it.
822 * This could be optimized if we ever find the reason. */
823 buf
.buf
= malloc(buf
.size
* sizeof(*buf
.buf
));
825 IHTMLDOMNode_Release(&node
->IHTMLDOMNode_iface
);
826 return E_OUTOFMEMORY
;
829 create_all_list(node
, &buf
);
831 for(i
=0; i
< buf
.len
; i
++) {
832 if(buf
.buf
[i
] == elem
)
835 IHTMLDOMNode_Release(&node
->IHTMLDOMNode_iface
);
837 for(j
= 0; j
< buf
.len
; j
++)
838 IHTMLDOMNode_Release(&buf
.buf
[j
]->node
.IHTMLDOMNode_iface
);
842 FIXME("The element is not in parent's child list?\n");
850 static IHTMLElementCollection
*HTMLElementCollection_Create(HTMLElement
**elems
, DWORD len
,
851 compat_mode_t compat_mode
)
853 HTMLElementCollection
*ret
= calloc(1, sizeof(HTMLElementCollection
));
858 ret
->IHTMLElementCollection_iface
.lpVtbl
= &HTMLElementCollectionVtbl
;
862 init_dispatch(&ret
->dispex
, &HTMLElementCollection_dispex
, compat_mode
);
864 TRACE("ret=%p len=%ld\n", ret
, len
);
866 return &ret
->IHTMLElementCollection_iface
;