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 const IHTMLElementCollectionVtbl
*lpHTMLElementCollectionVtbl
;
43 } HTMLElementCollection
;
45 #define HTMLELEMCOL(x) ((IHTMLElementCollection*) &(x)->lpHTMLElementCollectionVtbl)
53 static IHTMLElementCollection
*HTMLElementCollection_Create(IUnknown
*ref_unk
,
54 HTMLElement
**elems
, DWORD len
);
56 static void elem_vector_add(elem_vector_t
*buf
, HTMLElement
*elem
)
58 if(buf
->len
== buf
->size
) {
60 buf
->buf
= heap_realloc(buf
->buf
, buf
->size
*sizeof(HTMLElement
**));
63 buf
->buf
[buf
->len
++] = elem
;
66 static void elem_vector_normalize(elem_vector_t
*buf
)
71 }else if(buf
->size
> buf
->len
) {
72 buf
->buf
= heap_realloc(buf
->buf
, buf
->len
*sizeof(HTMLElement
**));
78 static inline BOOL
is_elem_node(nsIDOMNode
*node
)
82 nsIDOMNode_GetNodeType(node
, &type
);
84 return type
== ELEMENT_NODE
|| type
== COMMENT_NODE
;
87 #define ELEMCOL_THIS(iface) DEFINE_THIS(HTMLElementCollection, HTMLElementCollection, iface)
88 #define HTMLELEM_NODE_THIS(iface) DEFINE_THIS2(HTMLElement, node, iface)
90 static HRESULT WINAPI
HTMLElementCollection_QueryInterface(IHTMLElementCollection
*iface
,
91 REFIID riid
, void **ppv
)
93 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
97 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
98 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
99 *ppv
= HTMLELEMCOL(This
);
100 }else if(IsEqualGUID(&IID_IHTMLElementCollection
, riid
)) {
101 TRACE("(%p)->(IID_IHTMLElementCollection %p)\n", This
, ppv
);
102 *ppv
= HTMLELEMCOL(This
);
103 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
104 return *ppv
? S_OK
: E_NOINTERFACE
;
108 IHTMLElementCollection_AddRef(HTMLELEMCOL(This
));
112 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
113 return E_NOINTERFACE
;
116 static ULONG WINAPI
HTMLElementCollection_AddRef(IHTMLElementCollection
*iface
)
118 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
119 LONG ref
= InterlockedIncrement(&This
->ref
);
121 TRACE("(%p) ref=%d\n", This
, ref
);
126 static ULONG WINAPI
HTMLElementCollection_Release(IHTMLElementCollection
*iface
)
128 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
129 LONG ref
= InterlockedDecrement(&This
->ref
);
131 TRACE("(%p) ref=%d\n", This
, ref
);
134 IUnknown_Release(This
->ref_unk
);
135 release_dispex(&This
->dispex
);
136 heap_free(This
->elems
);
143 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection
*iface
,
146 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
147 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This
->dispex
), pctinfo
);
150 static HRESULT WINAPI
HTMLElementCollection_GetTypeInfo(IHTMLElementCollection
*iface
,
151 UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
153 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
154 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This
->dispex
), iTInfo
, lcid
, ppTInfo
);
157 static HRESULT WINAPI
HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection
*iface
,
158 REFIID riid
, LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
160 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
161 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This
->dispex
), riid
, rgszNames
, cNames
, lcid
, rgDispId
);
164 static HRESULT WINAPI
HTMLElementCollection_Invoke(IHTMLElementCollection
*iface
,
165 DISPID dispIdMember
, REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
166 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
168 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
169 return IDispatchEx_Invoke(DISPATCHEX(&This
->dispex
), dispIdMember
, riid
, lcid
,
170 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
173 static HRESULT WINAPI
HTMLElementCollection_toString(IHTMLElementCollection
*iface
,
176 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
177 FIXME("(%p)->(%p)\n", This
, String
);
181 static HRESULT WINAPI
HTMLElementCollection_put_length(IHTMLElementCollection
*iface
,
184 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
185 FIXME("(%p)->(%d)\n", This
, v
);
189 static HRESULT WINAPI
HTMLElementCollection_get_length(IHTMLElementCollection
*iface
,
192 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
194 TRACE("(%p)->(%p)\n", This
, p
);
200 static HRESULT WINAPI
HTMLElementCollection_get__newEnum(IHTMLElementCollection
*iface
,
203 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
204 FIXME("(%p)->(%p)\n", This
, p
);
208 static BOOL
is_elem_id(HTMLElement
*elem
, LPCWSTR name
)
213 hres
= IHTMLElement_get_id(HTMLELEM(elem
), &elem_id
);
215 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres
);
219 if(elem_id
&& !strcmpW(elem_id
, name
)) {
220 SysFreeString(elem_id
);
224 SysFreeString(elem_id
);
228 static BOOL
is_elem_name(HTMLElement
*elem
, LPCWSTR name
)
230 const PRUnichar
*str
;
231 nsAString nsstr
, nsname
;
235 static const PRUnichar nameW
[] = {'n','a','m','e',0};
240 nsAString_Init(&nsstr
, NULL
);
241 nsIDOMHTMLElement_GetId(elem
->nselem
, &nsstr
);
242 nsAString_GetData(&nsstr
, &str
);
243 if(!strcmpiW(str
, name
)) {
244 nsAString_Finish(&nsstr
);
248 nsAString_InitDepend(&nsname
, nameW
);
249 nsres
= nsIDOMHTMLElement_GetAttribute(elem
->nselem
, &nsname
, &nsstr
);
250 nsAString_Finish(&nsname
);
251 if(NS_SUCCEEDED(nsres
)) {
252 nsAString_GetData(&nsstr
, &str
);
253 ret
= !strcmpiW(str
, name
);
256 nsAString_Finish(&nsstr
);
260 static HRESULT
get_item_idx(HTMLElementCollection
*This
, UINT idx
, IDispatch
**ret
)
262 if(idx
< This
->len
) {
263 *ret
= (IDispatch
*)This
->elems
[idx
];
264 IDispatch_AddRef(*ret
);
270 static HRESULT WINAPI
HTMLElementCollection_item(IHTMLElementCollection
*iface
,
271 VARIANT name
, VARIANT index
, IDispatch
**pdisp
)
273 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
276 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_variant(&name
), debugstr_variant(&index
), pdisp
);
280 switch(V_VT(&name
)) {
284 hres
= get_item_idx(This
, V_I4(&name
), pdisp
);
288 hres
= get_item_idx(This
, V_UINT(&name
), pdisp
);
294 if(V_VT(&index
) == VT_I4
) {
295 LONG idx
= V_I4(&index
);
300 for(i
=0; i
<This
->len
; i
++) {
301 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)) && !idx
--)
306 *pdisp
= (IDispatch
*)HTMLELEM(This
->elems
[i
]);
307 IDispatch_AddRef(*pdisp
);
310 elem_vector_t buf
= {NULL
, 0, 8};
312 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
314 for(i
=0; i
<This
->len
; i
++) {
315 if(is_elem_name(This
->elems
[i
], V_BSTR(&name
)))
316 elem_vector_add(&buf
, This
->elems
[i
]);
320 elem_vector_normalize(&buf
);
321 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(This
->ref_unk
, buf
.buf
, buf
.len
);
324 *pdisp
= (IDispatch
*)HTMLELEM(buf
.buf
[0]);
325 IDispatch_AddRef(*pdisp
);
335 FIXME("Unsupported name %s\n", debugstr_variant(&name
));
340 TRACE("returning %p\n", *pdisp
);
344 static HRESULT WINAPI
HTMLElementCollection_tags(IHTMLElementCollection
*iface
,
345 VARIANT tagName
, IDispatch
**pdisp
)
347 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
350 const PRUnichar
*tag
;
351 elem_vector_t buf
= {NULL
, 0, 8};
353 if(V_VT(&tagName
) != VT_BSTR
) {
354 WARN("Invalid arg\n");
355 return DISP_E_MEMBERNOTFOUND
;
358 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(V_BSTR(&tagName
)), pdisp
);
360 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
362 nsAString_Init(&tag_str
, NULL
);
364 for(i
=0; i
<This
->len
; i
++) {
365 if(!This
->elems
[i
]->nselem
)
368 nsIDOMElement_GetTagName(This
->elems
[i
]->nselem
, &tag_str
);
369 nsAString_GetData(&tag_str
, &tag
);
371 if(CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
, tag
, -1,
372 V_BSTR(&tagName
), -1) == CSTR_EQUAL
)
373 elem_vector_add(&buf
, This
->elems
[i
]);
376 nsAString_Finish(&tag_str
);
377 elem_vector_normalize(&buf
);
379 TRACE("fount %d tags\n", buf
.len
);
381 *pdisp
= (IDispatch
*)HTMLElementCollection_Create(This
->ref_unk
, buf
.buf
, buf
.len
);
387 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl
= {
388 HTMLElementCollection_QueryInterface
,
389 HTMLElementCollection_AddRef
,
390 HTMLElementCollection_Release
,
391 HTMLElementCollection_GetTypeInfoCount
,
392 HTMLElementCollection_GetTypeInfo
,
393 HTMLElementCollection_GetIDsOfNames
,
394 HTMLElementCollection_Invoke
,
395 HTMLElementCollection_toString
,
396 HTMLElementCollection_put_length
,
397 HTMLElementCollection_get_length
,
398 HTMLElementCollection_get__newEnum
,
399 HTMLElementCollection_item
,
400 HTMLElementCollection_tags
403 static inline HTMLElementCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
405 return CONTAINING_RECORD(iface
, HTMLElementCollection
, dispex
);
408 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
410 static HRESULT
HTMLElementCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
412 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
417 return DISP_E_UNKNOWNNAME
;
419 for(ptr
= name
; *ptr
&& isdigitW(*ptr
); ptr
++)
420 idx
= idx
*10 + (*ptr
-'0');
423 /* the name contains alpha characters, so search by name & id */
424 for(idx
= 0; idx
< This
->len
; ++idx
) {
425 if(is_elem_id(This
->elems
[idx
], name
) ||
426 is_elem_name(This
->elems
[idx
], name
))
432 return DISP_E_UNKNOWNNAME
;
434 *dispid
= DISPID_ELEMCOL_0
+ idx
;
435 TRACE("ret %x\n", *dispid
);
439 static HRESULT
HTMLElementCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
440 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
442 HTMLElementCollection
*This
= impl_from_DispatchEx(dispex
);
445 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
447 idx
= id
- DISPID_ELEMCOL_0
;
449 return DISP_E_UNKNOWNNAME
;
452 case DISPATCH_PROPERTYGET
:
453 V_VT(res
) = VT_DISPATCH
;
454 V_DISPATCH(res
) = (IDispatch
*)HTMLELEM(This
->elems
[idx
]);
455 IHTMLElement_AddRef(HTMLELEM(This
->elems
[idx
]));
458 FIXME("unimplemented flags %x\n", flags
);
465 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl
= {
467 HTMLElementCollection_get_dispid
,
468 HTMLElementCollection_invoke
471 static const tid_t HTMLElementCollection_iface_tids
[] = {
472 IHTMLElementCollection_tid
,
476 static dispex_static_data_t HTMLElementCollection_dispex
= {
477 &HTMLElementColection_dispex_vtbl
,
478 DispHTMLElementCollection_tid
,
480 HTMLElementCollection_iface_tids
483 static void create_all_list(HTMLDocumentNode
*doc
, HTMLDOMNode
*elem
, elem_vector_t
*buf
)
485 nsIDOMNodeList
*nsnode_list
;
487 PRUint32 list_len
= 0, i
;
491 nsres
= nsIDOMNode_GetChildNodes(elem
->nsnode
, &nsnode_list
);
492 if(NS_FAILED(nsres
)) {
493 ERR("GetChildNodes failed: %08x\n", nsres
);
497 nsIDOMNodeList_GetLength(nsnode_list
, &list_len
);
501 for(i
=0; i
<list_len
; i
++) {
502 nsres
= nsIDOMNodeList_Item(nsnode_list
, i
, &iter
);
503 if(NS_FAILED(nsres
)) {
504 ERR("Item failed: %08x\n", nsres
);
508 if(is_elem_node(iter
)) {
511 hres
= get_node(doc
, iter
, TRUE
, &node
);
513 FIXME("get_node failed: %08x\n", hres
);
517 elem_vector_add(buf
, HTMLELEM_NODE_THIS(node
));
518 create_all_list(doc
, node
, buf
);
523 IHTMLElementCollection
*create_all_collection(HTMLDOMNode
*node
, BOOL include_root
)
525 elem_vector_t buf
= {NULL
, 0, 8};
527 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
**));
530 elem_vector_add(&buf
, HTMLELEM_NODE_THIS(node
));
531 create_all_list(node
->doc
, node
, &buf
);
532 elem_vector_normalize(&buf
);
534 return HTMLElementCollection_Create((IUnknown
*)HTMLDOMNODE(node
), buf
.buf
, buf
.len
);
537 IHTMLElementCollection
*create_collection_from_nodelist(HTMLDocumentNode
*doc
, IUnknown
*unk
, nsIDOMNodeList
*nslist
)
539 PRUint32 length
= 0, i
;
544 nsIDOMNodeList_GetLength(nslist
, &length
);
551 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
553 for(i
=0; i
<length
; i
++) {
554 nsIDOMNodeList_Item(nslist
, i
, &nsnode
);
555 if(is_elem_node(nsnode
)) {
556 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
559 buf
.buf
[buf
.len
++] = HTMLELEM_NODE_THIS(node
);
561 nsIDOMNode_Release(nsnode
);
564 elem_vector_normalize(&buf
);
569 return HTMLElementCollection_Create(unk
, buf
.buf
, buf
.len
);
572 IHTMLElementCollection
*create_collection_from_htmlcol(HTMLDocumentNode
*doc
, IUnknown
*unk
, nsIDOMHTMLCollection
*nscol
)
574 PRUint32 length
= 0, i
;
579 nsIDOMHTMLCollection_GetLength(nscol
, &length
);
581 buf
.len
= buf
.size
= length
;
585 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
587 for(i
=0; i
<length
; i
++) {
588 nsIDOMHTMLCollection_Item(nscol
, i
, &nsnode
);
589 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
590 nsIDOMNode_Release(nsnode
);
593 buf
.buf
[i
] = HTMLELEM_NODE_THIS(node
);
604 return HTMLElementCollection_Create(unk
, buf
.buf
, buf
.len
);
607 static IHTMLElementCollection
*HTMLElementCollection_Create(IUnknown
*ref_unk
,
608 HTMLElement
**elems
, DWORD len
)
610 HTMLElementCollection
*ret
= heap_alloc_zero(sizeof(HTMLElementCollection
));
612 ret
->lpHTMLElementCollectionVtbl
= &HTMLElementCollectionVtbl
;
617 init_dispex(&ret
->dispex
, (IUnknown
*)HTMLELEMCOL(ret
), &HTMLElementCollection_dispex
);
619 IUnknown_AddRef(ref_unk
);
620 ret
->ref_unk
= ref_unk
;
622 TRACE("ret=%p len=%d\n", ret
, len
);
624 return HTMLELEMCOL(ret
);