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
;
50 /* FIXME: Handle it better way */
51 static inline HTMLElement
*elem_from_HTMLDOMNode(HTMLDOMNode
*iface
)
53 return CONTAINING_RECORD(iface
, HTMLElement
, node
);
56 static IHTMLElementCollection
*HTMLElementCollection_Create(HTMLElement
**elems
, DWORD len
);
58 static void elem_vector_add(elem_vector_t
*buf
, HTMLElement
*elem
)
60 if(buf
->len
== buf
->size
) {
62 buf
->buf
= heap_realloc(buf
->buf
, buf
->size
*sizeof(HTMLElement
*));
65 buf
->buf
[buf
->len
++] = elem
;
68 static void elem_vector_normalize(elem_vector_t
*buf
)
73 }else if(buf
->size
> buf
->len
) {
74 buf
->buf
= heap_realloc(buf
->buf
, buf
->len
*sizeof(HTMLElement
*));
80 static inline BOOL
is_elem_node(nsIDOMNode
*node
)
84 nsIDOMNode_GetNodeType(node
, &type
);
86 return type
== ELEMENT_NODE
|| type
== COMMENT_NODE
;
89 static inline HTMLElementCollection
*impl_from_IHTMLElementCollection(IHTMLElementCollection
*iface
)
91 return CONTAINING_RECORD(iface
, HTMLElementCollection
, IHTMLElementCollection_iface
);
94 static HRESULT WINAPI
HTMLElementCollection_QueryInterface(IHTMLElementCollection
*iface
,
95 REFIID riid
, void **ppv
)
97 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
101 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
102 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
103 *ppv
= &This
->IHTMLElementCollection_iface
;
104 }else if(IsEqualGUID(&IID_IHTMLElementCollection
, riid
)) {
105 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This
, ppv
);
106 *ppv
= &This
->IHTMLElementCollection_iface
;
107 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
108 return *ppv
? S_OK
: E_NOINTERFACE
;
112 IHTMLElementCollection_AddRef(&This
->IHTMLElementCollection_iface
);
116 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
117 return E_NOINTERFACE
;
120 static ULONG WINAPI
HTMLElementCollection_AddRef(IHTMLElementCollection
*iface
)
122 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
123 LONG ref
= InterlockedIncrement(&This
->ref
);
125 TRACE("(%p) ref=%d\n", This
, ref
);
130 static ULONG WINAPI
HTMLElementCollection_Release(IHTMLElementCollection
*iface
)
132 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
133 LONG ref
= InterlockedDecrement(&This
->ref
);
135 TRACE("(%p) ref=%d\n", This
, ref
);
140 for(i
=0; i
< This
->len
; i
++)
141 node_release(&This
->elems
[i
]->node
);
142 heap_free(This
->elems
);
144 release_dispex(&This
->dispex
);
151 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection
*iface
,
154 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
155 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
158 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfo(IHTMLElementCollection
*iface
,
159 UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
161 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
162 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
165 static HRESULT WINAPI
HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection
*iface
,
166 REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
168 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
169 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
173 static HRESULT WINAPI
HTMLElementCollection_Invoke(IHTMLElementCollection
*iface
,
174 DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
175 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
177 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
178 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
179 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
182 static HRESULT WINAPI
HTMLElementCollection_toString(IHTMLElementCollection
*iface
,
185 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
186 FIXME("(%p)->(%p)\n", This
, String
);
190 static HRESULT WINAPI
HTMLElementCollection_put_length(IHTMLElementCollection
*iface
,
193 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
194 FIXME("(%p)->(%d)\n", This
, v
);
198 static HRESULT WINAPI
HTMLElementCollection_get_length(IHTMLElementCollection
*iface
,
201 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
203 TRACE("(%p)->(%p)\n", This
, p
);
209 static HRESULT WINAPI
HTMLElementCollection_get__newEnum(IHTMLElementCollection
*iface
,
212 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
213 FIXME("(%p)->(%p)\n", This
, p
);
217 static BOOL
is_elem_id(HTMLElement
*elem
, LPCWSTR name
)
222 hres
= IHTMLElement_get_id(&elem
->IHTMLElement_iface
, &elem_id
);
224 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres
);
228 if(elem_id
&& !strcmpW(elem_id
, name
)) {
229 SysFreeString(elem_id
);
233 SysFreeString(elem_id
);
237 static BOOL
is_elem_name(HTMLElement
*elem
, LPCWSTR name
)
239 const PRUnichar
*str
;
240 nsAString nsstr
, nsname
;
244 static const PRUnichar nameW
[] = {'n','a','m','e',0};
249 nsAString_Init(&nsstr
, NULL
);
250 nsIDOMHTMLElement_GetId(elem
->nselem
, &nsstr
);
251 nsAString_GetData(&nsstr
, &str
);
252 if(!strcmpiW(str
, name
)) {
253 nsAString_Finish(&nsstr
);
257 nsAString_InitDepend(&nsname
, nameW
);
258 nsres
= nsIDOMHTMLElement_GetAttribute(elem
->nselem
, &nsname
, &nsstr
);
259 nsAString_Finish(&nsname
);
260 if(NS_SUCCEEDED(nsres
)) {
261 nsAString_GetData(&nsstr
, &str
);
262 ret
= !strcmpiW(str
, name
);
265 nsAString_Finish(&nsstr
);
269 static HRESULT
get_item_idx(HTMLElementCollection
*This
, UINT idx
, IDispatch
**ret
)
271 if(idx
< This
->len
) {
272 *ret
= (IDispatch
*)This
->elems
[idx
];
273 IDispatch_AddRef(*ret
);
279 static HRESULT WINAPI
HTMLElementCollection_item(IHTMLElementCollection
*iface
,
280 VARIANT name
, VARIANT index
, IDispatch
**pdisp
)
282 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
285 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_variant(&name
), debugstr_variant(&index
), pdisp
);
289 switch(V_VT(&name
)) {
293 hres
= get_item_idx(This
, V_I4(&name
), pdisp
);
297 hres
= get_item_idx(This
, V_UINT(&name
), pdisp
);
303 if(V_VT(&index
) == VT_I4
) {
304 LONG idx
= V_I4(&index
);
309 for(i
=0; i
<This
->len
; i
++) {
310 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)) && !idx
--)
315 *pdisp
= (IDispatch
*)&This
->elems
[i
]->IHTMLElement_iface
;
316 IDispatch_AddRef(*pdisp
);
319 elem_vector_t buf
= {NULL
, 0, 8};
321 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
323 for(i
=0; i
<This
->len
; i
++) {
324 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
))) {
325 node_addref(&This
->elems
[i
]->node
);
326 elem_vector_add(&buf
, This
->elems
[i
]);
331 elem_vector_normalize(&buf
);
332 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(buf
.buf
, buf
.len
);
335 /* Already AddRef-ed */
336 *pdisp
= (IDispatch
*)&buf
.buf
[0]->IHTMLElement_iface
;
346 FIXME("Unsupported name %s\n", debugstr_variant(&name
));
351 TRACE("returning %p\n", *pdisp
);
355 static HRESULT WINAPI
HTMLElementCollection_tags(IHTMLElementCollection
*iface
,
356 VARIANT tagName
, IDispatch
**pdisp
)
358 HTMLElementCollection
*This
= impl_from_IHTMLElementCollection(iface
);
361 const PRUnichar
*tag
;
362 elem_vector_t buf
= {NULL
, 0, 8};
364 if(V_VT(&tagName
) != VT_BSTR
) {
365 WARN("Invalid arg\n");
366 return DISP_E_MEMBERNOTFOUND
;
369 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(V_BSTR(&tagName
)), pdisp
);
371 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
373 nsAString_Init(&tag_str
, NULL
);
375 for(i
=0; i
<This
->len
; i
++) {
376 if(!This
->elems
[i
]->nselem
)
379 nsIDOMHTMLElement_GetTagName(This
->elems
[i
]->nselem
, &tag_str
);
380 nsAString_GetData(&tag_str
, &tag
);
382 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, tag
, -1,
383 V_BSTR(&tagName
), -1) == CSTR_EQUAL
) {
384 node_addref(&This
->elems
[i
]->node
);
385 elem_vector_add(&buf
, This
->elems
[i
]);
389 nsAString_Finish(&tag_str
);
390 elem_vector_normalize(&buf
);
392 TRACE("fount %d tags\n", buf
.len
);
394 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(buf
.buf
, buf
.len
);
398 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl
= {
399 HTMLElementCollection_QueryInterface
,
400 HTMLElementCollection_AddRef
,
401 HTMLElementCollection_Release
,
402 HTMLElementCollection_GetTypeInfoCount
,
403 HTMLElementCollection_GetTypeInfo
,
404 HTMLElementCollection_GetIDsOfNames
,
405 HTMLElementCollection_Invoke
,
406 HTMLElementCollection_toString
,
407 HTMLElementCollection_put_length
,
408 HTMLElementCollection_get_length
,
409 HTMLElementCollection_get__newEnum
,
410 HTMLElementCollection_item
,
411 HTMLElementCollection_tags
414 static inline HTMLElementCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
416 return CONTAINING_RECORD(iface
, HTMLElementCollection
, dispex
);
419 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
421 static HRESULT
HTMLElementCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
423 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
428 return DISP_E_UNKNOWNNAME
;
430 for(ptr
= name
; *ptr
&& isdigitW(*ptr
); ptr
++)
431 idx
= idx
*10 + (*ptr
-'0');
434 /* the name contains alpha characters, so search by name & id */
435 for(idx
= 0; idx
< This
->len
; ++idx
) {
436 if(is_elem_id(This
->elems
[idx
], name
) ||
437 is_elem_name(This
->elems
[idx
], name
))
443 return DISP_E_UNKNOWNNAME
;
445 *dispid
= DISPID_ELEMCOL_0
+ idx
;
446 TRACE("ret %x\n", *dispid
);
450 static HRESULT
HTMLElementCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
451 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
453 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
456 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
458 idx
= id
- DISPID_ELEMCOL_0
;
460 return DISP_E_UNKNOWNNAME
;
463 case DISPATCH_PROPERTYGET
:
464 V_VT(res
) = VT_DISPATCH
;
465 V_DISPATCH(res
) = (IDispatch
*)&This
->elems
[idx
]->IHTMLElement_iface
;
466 IHTMLElement_AddRef(&This
->elems
[idx
]->IHTMLElement_iface
);
469 FIXME("unimplemented flags %x\n", flags
);
476 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl
= {
478 HTMLElementCollection_get_dispid
,
479 HTMLElementCollection_invoke
,
483 static const tid_t HTMLElementCollection_iface_tids
[] = {
484 IHTMLElementCollection_tid
,
488 static dispex_static_data_t HTMLElementCollection_dispex
= {
489 &HTMLElementColection_dispex_vtbl
,
490 DispHTMLElementCollection_tid
,
492 HTMLElementCollection_iface_tids
495 static void create_all_list(HTMLDocumentNode
*doc
, HTMLDOMNode
*elem
, elem_vector_t
*buf
)
497 nsIDOMNodeList
*nsnode_list
;
499 UINT32 list_len
= 0, i
;
503 nsres
= nsIDOMNode_GetChildNodes(elem
->nsnode
, &nsnode_list
);
504 if(NS_FAILED(nsres
)) {
505 ERR("GetChildNodes failed: %08x\n", nsres
);
509 nsIDOMNodeList_GetLength(nsnode_list
, &list_len
);
513 for(i
=0; i
<list_len
; i
++) {
514 nsres
= nsIDOMNodeList_Item(nsnode_list
, i
, &iter
);
515 if(NS_FAILED(nsres
)) {
516 ERR("Item failed: %08x\n", nsres
);
520 if(is_elem_node(iter
)) {
523 hres
= get_node(doc
, iter
, TRUE
, &node
);
525 FIXME("get_node failed: %08x\n", hres
);
529 elem_vector_add(buf
, elem_from_HTMLDOMNode(node
));
530 create_all_list(doc
, node
, buf
);
535 IHTMLElementCollection
*create_all_collection(HTMLDOMNode
*node
, BOOL include_root
)
537 elem_vector_t buf
= {NULL
, 0, 8};
539 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
543 elem_vector_add(&buf
, elem_from_HTMLDOMNode(node
));
545 create_all_list(node
->doc
, node
, &buf
);
546 elem_vector_normalize(&buf
);
548 return HTMLElementCollection_Create(buf
.buf
, buf
.len
);
551 IHTMLElementCollection
*create_collection_from_nodelist(HTMLDocumentNode
*doc
, nsIDOMNodeList
*nslist
)
553 UINT32 length
= 0, i
;
558 nsIDOMNodeList_GetLength(nslist
, &length
);
565 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
567 for(i
=0; i
<length
; i
++) {
568 nsIDOMNodeList_Item(nslist
, i
, &nsnode
);
569 if(is_elem_node(nsnode
)) {
570 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
573 buf
.buf
[buf
.len
++] = elem_from_HTMLDOMNode(node
);
575 nsIDOMNode_Release(nsnode
);
578 elem_vector_normalize(&buf
);
583 return HTMLElementCollection_Create(buf
.buf
, buf
.len
);
586 IHTMLElementCollection
*create_collection_from_htmlcol(HTMLDocumentNode
*doc
, nsIDOMHTMLCollection
*nscol
)
588 UINT32 length
= 0, i
;
593 nsIDOMHTMLCollection_GetLength(nscol
, &length
);
595 buf
.len
= buf
.size
= length
;
599 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
601 for(i
=0; i
<length
; i
++) {
602 nsIDOMHTMLCollection_Item(nscol
, i
, &nsnode
);
603 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
604 nsIDOMNode_Release(nsnode
);
607 buf
.buf
[i
] = elem_from_HTMLDOMNode(node
);
618 return HTMLElementCollection_Create(buf
.buf
, buf
.len
);
621 static IHTMLElementCollection
*HTMLElementCollection_Create(HTMLElement
**elems
, DWORD len
)
623 HTMLElementCollection
*ret
= heap_alloc_zero(sizeof(HTMLElementCollection
));
625 ret
->IHTMLElementCollection_iface
.lpVtbl
= &HTMLElementCollectionVtbl
;
630 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLElementCollection_iface
,
631 &HTMLElementCollection_dispex
);
633 TRACE("ret=%p len=%d\n", ret
, len
);
635 return &ret
->IHTMLElementCollection_iface
;