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_Init(&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
);
385 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
387 static HRESULT
HTMLElementCollection_get_dispid(IUnknown
*iface
, BSTR name
, DWORD flags
, DISPID
*dispid
)
389 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
394 return DISP_E_UNKNOWNNAME
;
396 for(ptr
= name
; *ptr
&& isdigitW(*ptr
); ptr
++)
397 idx
= idx
*10 + (*ptr
-'0');
400 /* the name contains alpha characters, so search by name & id */
401 for(idx
= 0; idx
< This
->len
; ++idx
) {
402 if(is_elem_id(This
->elems
[idx
], name
) ||
403 is_elem_name(This
->elems
[idx
], name
))
409 return DISP_E_UNKNOWNNAME
;
411 *dispid
= DISPID_ELEMCOL_0
+ idx
;
412 TRACE("ret %x\n", *dispid
);
416 static HRESULT
HTMLElementCollection_invoke(IUnknown
*iface
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
417 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
419 HTMLElementCollection
*This
= ELEMCOL_THIS(iface
);
422 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
424 idx
= id
- DISPID_ELEMCOL_0
;
426 return DISP_E_UNKNOWNNAME
;
429 case DISPATCH_PROPERTYGET
:
430 V_VT(res
) = VT_DISPATCH
;
431 V_DISPATCH(res
) = (IDispatch
*)HTMLELEM(This
->elems
[idx
]);
432 IHTMLElement_AddRef(HTMLELEM(This
->elems
[idx
]));
435 FIXME("unimplemented flags %x\n", flags
);
444 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl
= {
445 HTMLElementCollection_QueryInterface
,
446 HTMLElementCollection_AddRef
,
447 HTMLElementCollection_Release
,
448 HTMLElementCollection_GetTypeInfoCount
,
449 HTMLElementCollection_GetTypeInfo
,
450 HTMLElementCollection_GetIDsOfNames
,
451 HTMLElementCollection_Invoke
,
452 HTMLElementCollection_toString
,
453 HTMLElementCollection_put_length
,
454 HTMLElementCollection_get_length
,
455 HTMLElementCollection_get__newEnum
,
456 HTMLElementCollection_item
,
457 HTMLElementCollection_tags
460 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl
= {
462 HTMLElementCollection_get_dispid
,
463 HTMLElementCollection_invoke
466 static const tid_t HTMLElementCollection_iface_tids
[] = {
467 IHTMLElementCollection_tid
,
470 static dispex_static_data_t HTMLElementCollection_dispex
= {
471 &HTMLElementColection_dispex_vtbl
,
472 DispHTMLElementCollection_tid
,
474 HTMLElementCollection_iface_tids
477 static void create_all_list(HTMLDocumentNode
*doc
, HTMLDOMNode
*elem
, elem_vector_t
*buf
)
479 nsIDOMNodeList
*nsnode_list
;
481 PRUint32 list_len
= 0, i
;
484 nsres
= nsIDOMNode_GetChildNodes(elem
->nsnode
, &nsnode_list
);
485 if(NS_FAILED(nsres
)) {
486 ERR("GetChildNodes failed: %08x\n", nsres
);
490 nsIDOMNodeList_GetLength(nsnode_list
, &list_len
);
494 for(i
=0; i
<list_len
; i
++) {
495 nsres
= nsIDOMNodeList_Item(nsnode_list
, i
, &iter
);
496 if(NS_FAILED(nsres
)) {
497 ERR("Item failed: %08x\n", nsres
);
501 if(is_elem_node(iter
)) {
502 HTMLDOMNode
*node
= get_node(doc
, iter
, TRUE
);
504 elem_vector_add(buf
, HTMLELEM_NODE_THIS(node
));
505 create_all_list(doc
, node
, buf
);
510 IHTMLElementCollection
*create_all_collection(HTMLDOMNode
*node
, BOOL include_root
)
512 elem_vector_t buf
= {NULL
, 0, 8};
514 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
**));
517 elem_vector_add(&buf
, HTMLELEM_NODE_THIS(node
));
518 create_all_list(node
->doc
, node
, &buf
);
519 elem_vector_normalize(&buf
);
521 return HTMLElementCollection_Create((IUnknown
*)HTMLDOMNODE(node
), buf
.buf
, buf
.len
);
524 IHTMLElementCollection
*create_collection_from_nodelist(HTMLDocumentNode
*doc
, IUnknown
*unk
, nsIDOMNodeList
*nslist
)
526 PRUint32 length
= 0, i
;
529 nsIDOMNodeList_GetLength(nslist
, &length
);
536 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
538 for(i
=0; i
<length
; i
++) {
539 nsIDOMNodeList_Item(nslist
, i
, &nsnode
);
540 if(is_elem_node(nsnode
))
541 buf
.buf
[buf
.len
++] = HTMLELEM_NODE_THIS(get_node(doc
, nsnode
, TRUE
));
542 nsIDOMNode_Release(nsnode
);
545 elem_vector_normalize(&buf
);
550 return HTMLElementCollection_Create(unk
, buf
.buf
, buf
.len
);
553 IHTMLElementCollection
*create_collection_from_htmlcol(HTMLDocumentNode
*doc
, IUnknown
*unk
, nsIDOMHTMLCollection
*nscol
)
555 PRUint32 length
= 0, i
;
558 nsIDOMHTMLCollection_GetLength(nscol
, &length
);
560 buf
.len
= buf
.size
= length
;
564 buf
.buf
= heap_alloc(buf
.size
*sizeof(HTMLElement
*));
566 for(i
=0; i
<length
; i
++) {
567 nsIDOMHTMLCollection_Item(nscol
, i
, &nsnode
);
568 buf
.buf
[i
] = HTMLELEM_NODE_THIS(get_node(doc
, nsnode
, TRUE
));
569 nsIDOMNode_Release(nsnode
);
575 return HTMLElementCollection_Create(unk
, buf
.buf
, buf
.len
);
578 static IHTMLElementCollection
*HTMLElementCollection_Create(IUnknown
*ref_unk
,
579 HTMLElement
**elems
, DWORD len
)
581 HTMLElementCollection
*ret
= heap_alloc_zero(sizeof(HTMLElementCollection
));
583 ret
->lpHTMLElementCollectionVtbl
= &HTMLElementCollectionVtbl
;
588 init_dispex(&ret
->dispex
, (IUnknown
*)HTMLELEMCOL(ret
), &HTMLElementCollection_dispex
);
590 IUnknown_AddRef(ref_unk
);
591 ret
->ref_unk
= ref_unk
;
593 TRACE("ret=%p len=%d\n", ret
, len
);
595 return HTMLELEMCOL(ret
);