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
;
43 } HTMLElementCollection
;
51 /* FIXME: Handle it better way */
52 static inline HTMLElement
*elem_from_HTMLDOMNode(HTMLDOMNode
*iface
)
54 return CONTAINING_RECORD(iface
, HTMLElement
, node
);
57 static IHTMLElementCollection
*HTMLElementCollection_Create(IUnknown
*ref_unk
,
58 HTMLElement
**elems
, DWORD len
);
60 static void elem_vector_add(elem_vector_t
*buf
, HTMLElement
*elem
)
62 if(buf
->len
== buf
->size
) {
64 buf
->buf
= heap_realloc(buf
->buf
, buf
->size
*sizeof(HTMLElement
**));
67 buf
->buf
[buf
->len
++] = elem
;
70 static void elem_vector_normalize(elem_vector_t
*buf
)
75 }else if(buf
->size
> buf
->len
) {
76 buf
->buf
= heap_realloc(buf
->buf
, buf
->len
*sizeof(HTMLElement
**));
82 static inline BOOL
is_elem_node(nsIDOMNode
*node
)
86 nsIDOMNode_GetNodeType(node
, &type
);
88 return type
== ELEMENT_NODE
|| type
== COMMENT_NODE
;
91 static inline HTMLElementCollection
*impl_from_IHTMLElementCollection(IHTMLElementCollection
*iface
)
93 return CONTAINING_RECORD(iface
, HTMLElementCollection
, IHTMLElementCollection_iface
);
96 static HRESULT WINAPI
HTMLElementCollection_QueryInterface(IHTMLElementCollection
*iface
,
97 REFIID riid
, void **ppv
)
99 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
103 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
104 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
105 *ppv
= &This
->IHTMLElementCollection_iface
;
106 }else if(IsEqualGUID(&IID_IHTMLElementCollection
, riid
)) {
107 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This
, ppv
);
108 *ppv
= &This
->IHTMLElementCollection_iface
;
109 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
110 return *ppv
? S_OK
: E_NOINTERFACE
;
114 IHTMLElementCollection_AddRef(&This
->IHTMLElementCollection_iface
);
118 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
119 return E_NOINTERFACE
;
122 static ULONG WINAPI
HTMLElementCollection_AddRef(IHTMLElementCollection
*iface
)
124 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
125 LONG ref
= InterlockedIncrement(&This
->ref
);
127 TRACE("(%p) ref=%d\n", This
, ref
);
132 static ULONG WINAPI
HTMLElementCollection_Release(IHTMLElementCollection
*iface
)
134 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
135 LONG ref
= InterlockedDecrement(&This
->ref
);
137 TRACE("(%p) ref=%d\n", This
, ref
);
140 IUnknown_Release(This
->ref_unk
);
141 release_dispex(&This
->dispex
);
142 heap_free(This
->elems
);
149 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection
*iface
,
152 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
153 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
156 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfo(IHTMLElementCollection
*iface
,
157 UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
159 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
160 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
163 static HRESULT WINAPI
HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection
*iface
,
164 REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
166 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
167 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
171 static HRESULT WINAPI
HTMLElementCollection_Invoke(IHTMLElementCollection
*iface
,
172 DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
173 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
175 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
176 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
177 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
180 static HRESULT WINAPI
HTMLElementCollection_toString(IHTMLElementCollection
*iface
,
183 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
184 FIXME("(%p)->(%p)\n", This
, String
);
188 static HRESULT WINAPI
HTMLElementCollection_put_length(IHTMLElementCollection
*iface
,
191 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
192 FIXME("(%p)->(%d)\n", This
, v
);
196 static HRESULT WINAPI
HTMLElementCollection_get_length(IHTMLElementCollection
*iface
,
199 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
201 TRACE("(%p)->(%p)\n", This
, p
);
207 static HRESULT WINAPI
HTMLElementCollection_get__newEnum(IHTMLElementCollection
*iface
,
210 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
211 FIXME("(%p)->(%p)\n", This
, p
);
215 static BOOL
is_elem_id(HTMLElement
*elem
, LPCWSTR name
)
220 hres
= IHTMLElement_get_id(&elem
->IHTMLElement_iface
, &elem_id
);
222 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres
);
226 if(elem_id
&& !strcmpW(elem_id
, name
)) {
227 SysFreeString(elem_id
);
231 SysFreeString(elem_id
);
235 static BOOL
is_elem_name(HTMLElement
*elem
, LPCWSTR name
)
237 const PRUnichar
*str
;
238 nsAString nsstr
, nsname
;
242 static const PRUnichar nameW
[] = {'n','a','m','e',0};
247 nsAString_Init(&nsstr
, NULL
);
248 nsIDOMHTMLElement_GetId(elem
->nselem
, &nsstr
);
249 nsAString_GetData(&nsstr
, &str
);
250 if(!strcmpiW(str
, name
)) {
251 nsAString_Finish(&nsstr
);
255 nsAString_InitDepend(&nsname
, nameW
);
256 nsres
= nsIDOMHTMLElement_GetAttribute(elem
->nselem
, &nsname
, &nsstr
);
257 nsAString_Finish(&nsname
);
258 if(NS_SUCCEEDED(nsres
)) {
259 nsAString_GetData(&nsstr
, &str
);
260 ret
= !strcmpiW(str
, name
);
263 nsAString_Finish(&nsstr
);
267 static HRESULT
get_item_idx(HTMLElementCollection
*This
, UINT idx
, IDispatch
**ret
)
269 if(idx
< This
->len
) {
270 *ret
= (IDispatch
*)This
->elems
[idx
];
271 IDispatch_AddRef(*ret
);
277 static HRESULT WINAPI
HTMLElementCollection_item(IHTMLElementCollection
*iface
,
278 VARIANT name
, VARIANT index
, IDispatch
**pdisp
)
280 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
283 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_variant(&name
), debugstr_variant(&index
), pdisp
);
287 switch(V_VT(&name
)) {
291 hres
= get_item_idx(This
, V_I4(&name
), pdisp
);
295 hres
= get_item_idx(This
, V_UINT(&name
), pdisp
);
301 if(V_VT(&index
) == VT_I4
) {
302 LONG idx
= V_I4(&index
);
307 for(i
=0; i
<This
->len
; i
++) {
308 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)) && !idx
--)
313 *pdisp
= (IDispatch
*)&This
->elems
[i
]->IHTMLElement_iface
;
314 IDispatch_AddRef(*pdisp
);
317 elem_vector_t buf
= {NULL
, 0, 8};
319 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
321 for(i
=0; i
<This
->len
; i
++) {
322 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)))
323 elem_vector_add(&buf
, This
->elems
[i
]);
327 elem_vector_normalize(&buf
);
328 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(This
->ref_unk
, buf
.buf
, buf
.len
);
331 *pdisp
= (IDispatch
*)&buf
.buf
[0]->IHTMLElement_iface
;
332 IDispatch_AddRef(*pdisp
);
342 FIXME("Unsupported name %s\n", debugstr_variant(&name
));
347 TRACE("returning %p\n", *pdisp
);
351 static HRESULT WINAPI
HTMLElementCollection_tags(IHTMLElementCollection
*iface
,
352 VARIANT tagName
, IDispatch
**pdisp
)
354 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
357 const PRUnichar
*tag
;
358 elem_vector_t buf
= {NULL
, 0, 8};
360 if(V_VT(&tagName
) != VT_BSTR
) {
361 WARN("Invalid arg\n");
362 return DISP_E_MEMBERNOTFOUND
;
365 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(V_BSTR(&tagName
)), pdisp
);
367 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
369 nsAString_Init(&tag_str
, NULL
);
371 for(i
=0; i
<This
->len
; i
++) {
372 if(!This
->elems
[i
]->nselem
)
375 nsIDOMElement_GetTagName(This
->elems
[i
]->nselem
, &tag_str
);
376 nsAString_GetData(&tag_str
, &tag
);
378 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, tag
, -1,
379 V_BSTR(&tagName
), -1) == CSTR_EQUAL
)
380 elem_vector_add(&buf
, This
->elems
[i
]);
383 nsAString_Finish(&tag_str
);
384 elem_vector_normalize(&buf
);
386 TRACE("fount %d tags\n", buf
.len
);
388 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(This
->ref_unk
, buf
.buf
, buf
.len
);
392 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl
= {
393 HTMLElementCollection_QueryInterface
,
394 HTMLElementCollection_AddRef
,
395 HTMLElementCollection_Release
,
396 HTMLElementCollection_GetTypeInfoCount
,
397 HTMLElementCollection_GetTypeInfo
,
398 HTMLElementCollection_GetIDsOfNames
,
399 HTMLElementCollection_Invoke
,
400 HTMLElementCollection_toString
,
401 HTMLElementCollection_put_length
,
402 HTMLElementCollection_get_length
,
403 HTMLElementCollection_get__newEnum
,
404 HTMLElementCollection_item
,
405 HTMLElementCollection_tags
408 static inline HTMLElementCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
410 return CONTAINING_RECORD(iface
, HTMLElementCollection
, dispex
);
413 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
415 static HRESULT
HTMLElementCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
417 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
422 return DISP_E_UNKNOWNNAME
;
424 for(ptr
= name
; *ptr
&& isdigitW(*ptr
); ptr
++)
425 idx
= idx
*10 + (*ptr
-'0');
428 /* the name contains alpha characters, so search by name & id */
429 for(idx
= 0; idx
< This
->len
; ++idx
) {
430 if(is_elem_id(This
->elems
[idx
], name
) ||
431 is_elem_name(This
->elems
[idx
], name
))
437 return DISP_E_UNKNOWNNAME
;
439 *dispid
= DISPID_ELEMCOL_0
+ idx
;
440 TRACE("ret %x\n", *dispid
);
444 static HRESULT
HTMLElementCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
445 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
447 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
450 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
452 idx
= id
- DISPID_ELEMCOL_0
;
454 return DISP_E_UNKNOWNNAME
;
457 case DISPATCH_PROPERTYGET
:
458 V_VT(res
) = VT_DISPATCH
;
459 V_DISPATCH(res
) = (IDispatch
*)&This
->elems
[idx
]->IHTMLElement_iface
;
460 IHTMLElement_AddRef(&This
->elems
[idx
]->IHTMLElement_iface
);
463 FIXME("unimplemented flags %x\n", flags
);
470 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl
= {
472 HTMLElementCollection_get_dispid
,
473 HTMLElementCollection_invoke
476 static const tid_t HTMLElementCollection_iface_tids
[] = {
477 IHTMLElementCollection_tid
,
481 static dispex_static_data_t HTMLElementCollection_dispex
= {
482 &HTMLElementColection_dispex_vtbl
,
483 DispHTMLElementCollection_tid
,
485 HTMLElementCollection_iface_tids
488 static void create_all_list(HTMLDocumentNode
*doc
, HTMLDOMNode
*elem
, elem_vector_t
*buf
)
490 nsIDOMNodeList
*nsnode_list
;
492 PRUint32 list_len
= 0, i
;
496 nsres
= nsIDOMNode_GetChildNodes(elem
->nsnode
, &nsnode_list
);
497 if(NS_FAILED(nsres
)) {
498 ERR("GetChildNodes failed: %08x\n", nsres
);
502 nsIDOMNodeList_GetLength(nsnode_list
, &list_len
);
506 for(i
=0; i
<list_len
; i
++) {
507 nsres
= nsIDOMNodeList_Item(nsnode_list
, i
, &iter
);
508 if(NS_FAILED(nsres
)) {
509 ERR("Item failed: %08x\n", nsres
);
513 if(is_elem_node(iter
)) {
516 hres
= get_node(doc
, iter
, TRUE
, &node
);
518 FIXME("get_node failed: %08x\n", hres
);
522 elem_vector_add(buf
, elem_from_HTMLDOMNode(node
));
523 create_all_list(doc
, node
, buf
);
528 IHTMLElementCollection
*create_all_collection(HTMLDOMNode
*node
, BOOL include_root
)
530 elem_vector_t buf
= {NULL
, 0, 8};
532 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
**));
535 elem_vector_add(&buf
, elem_from_HTMLDOMNode(node
));
536 create_all_list(node
->doc
, node
, &buf
);
537 elem_vector_normalize(&buf
);
539 return HTMLElementCollection_Create((IUnknown
*)&node
->IHTMLDOMNode_iface
, buf
.buf
, buf
.len
);
542 IHTMLElementCollection
*create_collection_from_nodelist(HTMLDocumentNode
*doc
, IUnknown
*unk
, nsIDOMNodeList
*nslist
)
544 PRUint32 length
= 0, i
;
549 nsIDOMNodeList_GetLength(nslist
, &length
);
556 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
558 for(i
=0; i
<length
; i
++) {
559 nsIDOMNodeList_Item(nslist
, i
, &nsnode
);
560 if(is_elem_node(nsnode
)) {
561 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
564 buf
.buf
[buf
.len
++] = elem_from_HTMLDOMNode(node
);
566 nsIDOMNode_Release(nsnode
);
569 elem_vector_normalize(&buf
);
574 return HTMLElementCollection_Create(unk
, buf
.buf
, buf
.len
);
577 IHTMLElementCollection
*create_collection_from_htmlcol(HTMLDocumentNode
*doc
, IUnknown
*unk
, nsIDOMHTMLCollection
*nscol
)
579 PRUint32 length
= 0, i
;
584 nsIDOMHTMLCollection_GetLength(nscol
, &length
);
586 buf
.len
= buf
.size
= length
;
590 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
592 for(i
=0; i
<length
; i
++) {
593 nsIDOMHTMLCollection_Item(nscol
, i
, &nsnode
);
594 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
595 nsIDOMNode_Release(nsnode
);
598 buf
.buf
[i
] = elem_from_HTMLDOMNode(node
);
609 return HTMLElementCollection_Create(unk
, buf
.buf
, buf
.len
);
612 static IHTMLElementCollection
*HTMLElementCollection_Create(IUnknown
*ref_unk
,
613 HTMLElement
**elems
, DWORD len
)
615 HTMLElementCollection
*ret
= heap_alloc_zero(sizeof(HTMLElementCollection
));
617 ret
->IHTMLElementCollection_iface
.lpVtbl
= &HTMLElementCollectionVtbl
;
622 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLElementCollection_iface
,
623 &HTMLElementCollection_dispex
);
625 IUnknown_AddRef(ref_unk
);
626 ret
->ref_unk
= ref_unk
;
628 TRACE("ret=%p len=%d\n", ret
, len
);
630 return &ret
->IHTMLElementCollection_iface
;