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
;
42 } HTMLElementCollection
;
45 IEnumVARIANT IEnumVARIANT_iface
;
50 HTMLElementCollection
*col
;
51 } HTMLElementCollectionEnum
;
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
**,DWORD
,compat_mode_t
);
67 static void elem_vector_add(elem_vector_t
*buf
, HTMLElement
*elem
)
69 if(buf
->len
== buf
->size
) {
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
)
82 }else if(buf
->size
> buf
->len
) {
83 buf
->buf
= heap_realloc(buf
->buf
, buf
->len
*sizeof(HTMLElement
*));
89 static inline BOOL
is_elem_node(nsIDOMNode
*node
)
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 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
109 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
110 *ppv
= &This
->IEnumVARIANT_iface
;
111 }else if(IsEqualGUID(riid
, &IID_IEnumVARIANT
)) {
112 *ppv
= &This
->IEnumVARIANT_iface
;
114 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid
));
116 return E_NOINTERFACE
;
119 IUnknown_AddRef((IUnknown
*)*ppv
);
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=%ld\n", This
, 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=%ld\n", This
, ref
);
141 IHTMLElementCollection_Release(&This
->col
->IHTMLElementCollection_iface
);
148 static HRESULT WINAPI
HTMLElementCollectionEnum_Next(IEnumVARIANT
*iface
, ULONG celt
, VARIANT
*rgVar
, ULONG
*pCeltFetched
)
150 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
153 TRACE("(%p)->(%ld %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
));
162 This
->iter
+= fetched
;
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)->(%ld)\n", This
, celt
);
174 if(This
->iter
+ celt
> This
->col
->len
) {
175 This
->iter
= This
->col
->len
;
183 static HRESULT WINAPI
HTMLElementCollectionEnum_Reset(IEnumVARIANT
*iface
)
185 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
187 TRACE("(%p)->()\n", This
);
193 static HRESULT WINAPI
HTMLElementCollectionEnum_Clone(IEnumVARIANT
*iface
, IEnumVARIANT
**ppEnum
)
195 HTMLElementCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
196 FIXME("(%p)->(%p)\n", This
, ppEnum
);
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 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
222 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
223 *ppv
= &This
->IHTMLElementCollection_iface
;
224 }else if(IsEqualGUID(&IID_IHTMLElementCollection
, riid
)) {
225 *ppv
= &This
->IHTMLElementCollection_iface
;
226 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
227 return *ppv
? S_OK
: E_NOINTERFACE
;
230 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid
));
231 return E_NOINTERFACE
;
234 IHTMLElementCollection_AddRef(&This
->IHTMLElementCollection_iface
);
238 static ULONG WINAPI
HTMLElementCollection_AddRef(IHTMLElementCollection
*iface
)
240 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
241 LONG ref
= InterlockedIncrement(&This
->ref
);
243 TRACE("(%p) ref=%ld\n", This
, ref
);
248 static ULONG WINAPI
HTMLElementCollection_Release(IHTMLElementCollection
*iface
)
250 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
251 LONG ref
= InterlockedDecrement(&This
->ref
);
253 TRACE("(%p) ref=%ld\n", This
, ref
);
258 for(i
=0; i
< This
->len
; i
++)
259 node_release(&This
->elems
[i
]->node
);
260 heap_free(This
->elems
);
262 release_dispex(&This
->dispex
);
269 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection
*iface
,
272 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
273 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
276 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfo(IHTMLElementCollection
*iface
,
277 UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
279 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
280 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
283 static HRESULT WINAPI
HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection
*iface
,
284 REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
286 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
287 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
291 static HRESULT WINAPI
HTMLElementCollection_Invoke(IHTMLElementCollection
*iface
,
292 DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
293 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
295 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
296 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
297 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
300 static HRESULT WINAPI
HTMLElementCollection_toString(IHTMLElementCollection
*iface
,
303 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
305 TRACE("(%p)->(%p)\n", This
, String
);
307 return dispex_to_string(&This
->dispex
, String
);
310 static HRESULT WINAPI
HTMLElementCollection_put_length(IHTMLElementCollection
*iface
,
313 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
314 FIXME("(%p)->(%ld)\n", This
, v
);
318 static HRESULT WINAPI
HTMLElementCollection_get_length(IHTMLElementCollection
*iface
,
321 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
323 TRACE("(%p)->(%p)\n", This
, p
);
329 static HRESULT WINAPI
HTMLElementCollection_get__newEnum(IHTMLElementCollection
*iface
,
332 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
333 HTMLElementCollectionEnum
*ret
;
335 TRACE("(%p)->(%p)\n", This
, p
);
337 ret
= heap_alloc(sizeof(*ret
));
339 return E_OUTOFMEMORY
;
341 ret
->IEnumVARIANT_iface
.lpVtbl
= &HTMLElementCollectionEnumVtbl
;
345 IHTMLElementCollection_AddRef(&This
->IHTMLElementCollection_iface
);
348 *p
= (IUnknown
*)&ret
->IEnumVARIANT_iface
;
352 static BOOL
is_elem_id(HTMLElement
*elem
, LPCWSTR name
)
357 hres
= IHTMLElement_get_id(&elem
->IHTMLElement_iface
, &elem_id
);
359 WARN("IHTMLElement_get_id failed: 0x%08lx\n", hres
);
363 if(elem_id
&& !wcscmp(elem_id
, name
)) {
364 SysFreeString(elem_id
);
368 SysFreeString(elem_id
);
372 static BOOL
is_elem_name(HTMLElement
*elem
, LPCWSTR name
)
374 const PRUnichar
*str
;
379 if(!elem
->dom_element
)
382 nsAString_Init(&nsstr
, NULL
);
383 nsIDOMElement_GetId(elem
->dom_element
, &nsstr
);
384 nsAString_GetData(&nsstr
, &str
);
385 if(!wcsicmp(str
, name
)) {
386 nsAString_Finish(&nsstr
);
390 nsres
= get_elem_attr_value(elem
->dom_element
, L
"name", &nsstr
, &str
);
391 if(NS_SUCCEEDED(nsres
)) {
392 ret
= !wcsicmp(str
, name
);
393 nsAString_Finish(&nsstr
);
399 static HRESULT
get_item_idx(HTMLElementCollection
*This
, UINT idx
, IDispatch
**ret
)
401 if(idx
< This
->len
) {
402 *ret
= (IDispatch
*)&This
->elems
[idx
]->node
.event_target
.dispex
.IDispatchEx_iface
;
403 IDispatch_AddRef(*ret
);
409 static HRESULT WINAPI
HTMLElementCollection_item(IHTMLElementCollection
*iface
,
410 VARIANT name
, VARIANT index
, IDispatch
**pdisp
)
412 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
415 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_variant(&name
), debugstr_variant(&index
), pdisp
);
419 switch(V_VT(&name
)) {
423 return dispex_compat_mode(&This
->dispex
) < COMPAT_MODE_IE9
? E_INVALIDARG
: S_OK
;
424 hres
= get_item_idx(This
, V_I4(&name
), pdisp
);
429 hres
= get_item_idx(This
, V_UINT(&name
), pdisp
);
435 if(V_VT(&index
) == VT_I4
) {
436 LONG idx
= V_I4(&index
);
441 for(i
=0; i
<This
->len
; i
++) {
442 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)) && !idx
--)
447 *pdisp
= (IDispatch
*)&This
->elems
[i
]->IHTMLElement_iface
;
448 IDispatch_AddRef(*pdisp
);
451 elem_vector_t buf
= {NULL
, 0, 8};
453 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
455 for(i
=0; i
<This
->len
; i
++) {
456 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
))) {
457 node_addref(&This
->elems
[i
]->node
);
458 elem_vector_add(&buf
, This
->elems
[i
]);
463 elem_vector_normalize(&buf
);
464 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(buf
.buf
, buf
.len
,
465 dispex_compat_mode(&This
->dispex
));
468 /* Already AddRef-ed */
469 *pdisp
= (IDispatch
*)&buf
.buf
[0]->IHTMLElement_iface
;
479 FIXME("Unsupported name %s\n", debugstr_variant(&name
));
484 TRACE("returning %p\n", *pdisp
);
488 static HRESULT WINAPI
HTMLElementCollection_tags(IHTMLElementCollection
*iface
,
489 VARIANT tagName
, IDispatch
**pdisp
)
491 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
494 const PRUnichar
*tag
;
495 elem_vector_t buf
= {NULL
, 0, 8};
497 if(V_VT(&tagName
) != VT_BSTR
) {
498 WARN("Invalid arg\n");
499 return DISP_E_MEMBERNOTFOUND
;
502 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(V_BSTR(&tagName
)), pdisp
);
504 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
506 nsAString_Init(&tag_str
, NULL
);
508 for(i
=0; i
<This
->len
; i
++) {
509 if(!This
->elems
[i
]->dom_element
)
512 nsIDOMElement_GetTagName(This
->elems
[i
]->dom_element
, &tag_str
);
513 nsAString_GetData(&tag_str
, &tag
);
515 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, tag
, -1,
516 V_BSTR(&tagName
), -1) == CSTR_EQUAL
) {
517 node_addref(&This
->elems
[i
]->node
);
518 elem_vector_add(&buf
, This
->elems
[i
]);
522 nsAString_Finish(&tag_str
);
523 elem_vector_normalize(&buf
);
525 TRACE("found %ld tags\n", buf
.len
);
527 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(buf
.buf
, buf
.len
,
528 dispex_compat_mode(&This
->dispex
));
532 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl
= {
533 HTMLElementCollection_QueryInterface
,
534 HTMLElementCollection_AddRef
,
535 HTMLElementCollection_Release
,
536 HTMLElementCollection_GetTypeInfoCount
,
537 HTMLElementCollection_GetTypeInfo
,
538 HTMLElementCollection_GetIDsOfNames
,
539 HTMLElementCollection_Invoke
,
540 HTMLElementCollection_toString
,
541 HTMLElementCollection_put_length
,
542 HTMLElementCollection_get_length
,
543 HTMLElementCollection_get__newEnum
,
544 HTMLElementCollection_item
,
545 HTMLElementCollection_tags
548 static inline HTMLElementCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
550 return CONTAINING_RECORD(iface
, HTMLElementCollection
, dispex
);
553 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
555 static HRESULT
HTMLElementCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
557 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
562 return DISP_E_UNKNOWNNAME
;
564 for(ptr
= name
; *ptr
&& is_digit(*ptr
); ptr
++)
565 idx
= idx
*10 + (*ptr
-'0');
568 /* the name contains alpha characters, so search by name & id */
569 for(idx
= 0; idx
< This
->len
; ++idx
) {
570 if(is_elem_id(This
->elems
[idx
], name
) ||
571 is_elem_name(This
->elems
[idx
], name
))
577 return DISP_E_UNKNOWNNAME
;
579 *dispid
= DISPID_ELEMCOL_0
+ idx
;
580 TRACE("ret %lx\n", *dispid
);
584 static HRESULT
HTMLElementCollection_get_name(DispatchEx
*dispex
, DISPID id
, BSTR
*name
)
586 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
587 DWORD idx
= id
- MSHTML_DISPID_CUSTOM_MIN
;
592 return DISP_E_MEMBERNOTFOUND
;
594 len
= swprintf(buf
, ARRAY_SIZE(buf
), L
"%u", idx
);
595 return (*name
= SysAllocStringLen(buf
, len
)) ? S_OK
: E_OUTOFMEMORY
;
598 static HRESULT
HTMLElementCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
599 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
601 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
604 TRACE("(%p)->(%lx %lx %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
606 idx
= id
- DISPID_ELEMCOL_0
;
608 return DISP_E_MEMBERNOTFOUND
;
611 case DISPATCH_PROPERTYGET
:
612 V_VT(res
) = VT_DISPATCH
;
613 V_DISPATCH(res
) = (IDispatch
*)&This
->elems
[idx
]->IHTMLElement_iface
;
614 IHTMLElement_AddRef(&This
->elems
[idx
]->IHTMLElement_iface
);
617 FIXME("unimplemented flags %x\n", flags
);
624 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl
= {
626 HTMLElementCollection_get_dispid
,
627 HTMLElementCollection_get_name
,
628 HTMLElementCollection_invoke
,
632 static const tid_t HTMLElementCollection_iface_tids
[] = {
633 IHTMLElementCollection_tid
,
637 static dispex_static_data_t HTMLElementCollection_dispex
= {
639 &HTMLElementColection_dispex_vtbl
,
640 DispHTMLElementCollection_tid
,
641 HTMLElementCollection_iface_tids
644 static void create_all_list(HTMLDOMNode
*elem
, elem_vector_t
*buf
)
646 nsIDOMNodeList
*nsnode_list
;
648 UINT32 list_len
= 0, i
;
652 nsres
= nsIDOMNode_GetChildNodes(elem
->nsnode
, &nsnode_list
);
653 if(NS_FAILED(nsres
)) {
654 ERR("GetChildNodes failed: %08lx\n", nsres
);
658 nsIDOMNodeList_GetLength(nsnode_list
, &list_len
);
662 for(i
=0; i
<list_len
; i
++) {
663 nsres
= nsIDOMNodeList_Item(nsnode_list
, i
, &iter
);
664 if(NS_FAILED(nsres
)) {
665 ERR("Item failed: %08lx\n", nsres
);
669 if(is_elem_node(iter
)) {
672 hres
= get_node(iter
, TRUE
, &node
);
674 FIXME("get_node failed: %08lx\n", hres
);
678 elem_vector_add(buf
, elem_from_HTMLDOMNode(node
));
679 create_all_list(node
, buf
);
684 IHTMLElementCollection
*create_all_collection(HTMLDOMNode
*node
, BOOL include_root
)
686 elem_vector_t buf
= {NULL
, 0, 8};
688 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
692 elem_vector_add(&buf
, elem_from_HTMLDOMNode(node
));
694 create_all_list(node
, &buf
);
695 elem_vector_normalize(&buf
);
697 return HTMLElementCollection_Create(buf
.buf
, buf
.len
,
698 dispex_compat_mode(&node
->event_target
.dispex
));
701 IHTMLElementCollection
*create_collection_from_nodelist(nsIDOMNodeList
*nslist
, compat_mode_t compat_mode
)
703 UINT32 length
= 0, i
;
708 nsIDOMNodeList_GetLength(nslist
, &length
);
715 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
717 for(i
=0; i
<length
; i
++) {
718 nsIDOMNodeList_Item(nslist
, i
, &nsnode
);
719 if(is_elem_node(nsnode
)) {
720 hres
= get_node(nsnode
, TRUE
, &node
);
723 buf
.buf
[buf
.len
++] = elem_from_HTMLDOMNode(node
);
725 nsIDOMNode_Release(nsnode
);
728 elem_vector_normalize(&buf
);
733 return HTMLElementCollection_Create(buf
.buf
, buf
.len
, compat_mode
);
736 IHTMLElementCollection
*create_collection_from_htmlcol(nsIDOMHTMLCollection
*nscol
, compat_mode_t compat_mode
)
738 UINT32 length
= 0, i
;
744 nsIDOMHTMLCollection_GetLength(nscol
, &length
);
746 buf
.len
= buf
.size
= length
;
750 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
752 for(i
=0; i
<length
; i
++) {
753 nsIDOMHTMLCollection_Item(nscol
, i
, &nsnode
);
754 hres
= get_node(nsnode
, TRUE
, &node
);
755 nsIDOMNode_Release(nsnode
);
758 buf
.buf
[i
] = elem_from_HTMLDOMNode(node
);
769 return HTMLElementCollection_Create(buf
.buf
, buf
.len
, compat_mode
);
772 HRESULT
get_elem_source_index(HTMLElement
*elem
, LONG
*ret
)
774 elem_vector_t buf
= {NULL
, 0, 8};
775 nsIDOMNode
*parent_node
, *iter
;
782 iter
= elem
->node
.nsnode
;
783 nsIDOMNode_AddRef(iter
);
785 /* Find document or document fragment parent. */
787 nsres
= nsIDOMNode_GetParentNode(iter
, &parent_node
);
788 nsIDOMNode_Release(iter
);
789 assert(nsres
== NS_OK
);
793 nsres
= nsIDOMNode_GetNodeType(parent_node
, &parent_type
);
794 assert(nsres
== NS_OK
);
796 if(parent_type
!= ELEMENT_NODE
) {
797 if(parent_type
!= DOCUMENT_NODE
&& parent_type
!= DOCUMENT_FRAGMENT_NODE
)
798 FIXME("Unexpected parent_type %d\n", parent_type
);
810 hres
= get_node(parent_node
, TRUE
, &node
);
811 nsIDOMNode_Release(parent_node
);
815 /* Create all children collection and find the element in it.
816 * This could be optimized if we ever find the reason. */
817 buf
.buf
= heap_alloc(buf
.size
*sizeof(*buf
.buf
));
819 IHTMLDOMNode_Release(&node
->IHTMLDOMNode_iface
);
820 return E_OUTOFMEMORY
;
823 create_all_list(node
, &buf
);
825 for(i
=0; i
< buf
.len
; i
++) {
826 if(buf
.buf
[i
] == elem
)
829 IHTMLDOMNode_Release(&node
->IHTMLDOMNode_iface
);
832 FIXME("The element is not in parent's child list?\n");
840 static IHTMLElementCollection
*HTMLElementCollection_Create(HTMLElement
**elems
, DWORD len
,
841 compat_mode_t compat_mode
)
843 HTMLElementCollection
*ret
= heap_alloc_zero(sizeof(HTMLElementCollection
));
848 ret
->IHTMLElementCollection_iface
.lpVtbl
= &HTMLElementCollectionVtbl
;
853 init_dispatch(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLElementCollection_iface
,
854 &HTMLElementCollection_dispex
, compat_mode
);
856 TRACE("ret=%p len=%ld\n", ret
, len
);
858 return &ret
->IHTMLElementCollection_iface
;