mshtml: Store current IHTMLEventObj in DOMEvent and create it in fire_event_obj if...
[wine.git] / dlls / mshtml / htmlelemcol.c
blob8407e024203e199575e480e44a0847a1916c8a02
1 /*
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 typedef struct {
36 DispatchEx dispex;
37 IHTMLElementCollection IHTMLElementCollection_iface;
39 HTMLElement **elems;
40 DWORD len;
42 LONG ref;
43 } HTMLElementCollection;
45 typedef struct {
46 IEnumVARIANT IEnumVARIANT_iface;
48 LONG ref;
50 ULONG iter;
51 HTMLElementCollection *col;
52 } HTMLElementCollectionEnum;
54 typedef struct {
55 HTMLElement **buf;
56 DWORD len;
57 DWORD size;
58 } elem_vector_t;
60 /* FIXME: Handle it better way */
61 static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
63 return CONTAINING_RECORD(iface, HTMLElement, node);
66 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len);
68 static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
70 if(buf->len == buf->size) {
71 buf->size <<= 1;
72 buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement*));
75 buf->buf[buf->len++] = elem;
78 static void elem_vector_normalize(elem_vector_t *buf)
80 if(!buf->len) {
81 heap_free(buf->buf);
82 buf->buf = NULL;
83 }else if(buf->size > buf->len) {
84 buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement*));
87 buf->size = buf->len;
90 static inline BOOL is_elem_node(nsIDOMNode *node)
92 UINT16 type=0;
94 nsIDOMNode_GetNodeType(node, &type);
96 return type == ELEMENT_NODE || type == COMMENT_NODE;
99 static inline HTMLElementCollectionEnum *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
101 return CONTAINING_RECORD(iface, HTMLElementCollectionEnum, IEnumVARIANT_iface);
104 static HRESULT WINAPI HTMLElementCollectionEnum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
106 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
108 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
110 if(IsEqualGUID(riid, &IID_IUnknown)) {
111 *ppv = &This->IEnumVARIANT_iface;
112 }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
113 *ppv = &This->IEnumVARIANT_iface;
114 }else {
115 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
116 *ppv = NULL;
117 return E_NOINTERFACE;
120 IUnknown_AddRef((IUnknown*)*ppv);
121 return S_OK;
124 static ULONG WINAPI HTMLElementCollectionEnum_AddRef(IEnumVARIANT *iface)
126 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
127 LONG ref = InterlockedIncrement(&This->ref);
129 TRACE("(%p) ref=%d\n", This, ref);
131 return ref;
134 static ULONG WINAPI HTMLElementCollectionEnum_Release(IEnumVARIANT *iface)
136 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
137 LONG ref = InterlockedDecrement(&This->ref);
139 TRACE("(%p) ref=%d\n", This, ref);
141 if(!ref) {
142 IHTMLElementCollection_Release(&This->col->IHTMLElementCollection_iface);
143 heap_free(This);
146 return ref;
149 static HRESULT WINAPI HTMLElementCollectionEnum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
151 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
152 ULONG fetched = 0;
154 TRACE("(%p)->(%d %p %p)\n", This, celt, rgVar, pCeltFetched);
156 while(This->iter+fetched < This->col->len && fetched < celt) {
157 V_VT(rgVar+fetched) = VT_DISPATCH;
158 V_DISPATCH(rgVar+fetched) = (IDispatch*)&This->col->elems[This->iter+fetched]->IHTMLElement_iface;
159 IDispatch_AddRef(V_DISPATCH(rgVar+fetched));
160 fetched++;
163 This->iter += fetched;
164 if(pCeltFetched)
165 *pCeltFetched = fetched;
166 return fetched == celt ? S_OK : S_FALSE;
169 static HRESULT WINAPI HTMLElementCollectionEnum_Skip(IEnumVARIANT *iface, ULONG celt)
171 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
173 TRACE("(%p)->(%d)\n", This, celt);
175 if(This->iter + celt > This->col->len) {
176 This->iter = This->col->len;
177 return S_FALSE;
180 This->iter += celt;
181 return S_OK;
184 static HRESULT WINAPI HTMLElementCollectionEnum_Reset(IEnumVARIANT *iface)
186 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
188 TRACE("(%p)->()\n", This);
190 This->iter = 0;
191 return S_OK;
194 static HRESULT WINAPI HTMLElementCollectionEnum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
196 HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
197 FIXME("(%p)->(%p)\n", This, ppEnum);
198 return E_NOTIMPL;
201 static const IEnumVARIANTVtbl HTMLElementCollectionEnumVtbl = {
202 HTMLElementCollectionEnum_QueryInterface,
203 HTMLElementCollectionEnum_AddRef,
204 HTMLElementCollectionEnum_Release,
205 HTMLElementCollectionEnum_Next,
206 HTMLElementCollectionEnum_Skip,
207 HTMLElementCollectionEnum_Reset,
208 HTMLElementCollectionEnum_Clone
211 static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
213 return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
216 static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
217 REFIID riid, void **ppv)
219 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
221 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
223 if(IsEqualGUID(&IID_IUnknown, riid)) {
224 *ppv = &This->IHTMLElementCollection_iface;
225 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
226 *ppv = &This->IHTMLElementCollection_iface;
227 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
228 return *ppv ? S_OK : E_NOINTERFACE;
229 }else {
230 *ppv = NULL;
231 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
232 return E_NOINTERFACE;
235 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
236 return S_OK;
239 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
241 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
242 LONG ref = InterlockedIncrement(&This->ref);
244 TRACE("(%p) ref=%d\n", This, ref);
246 return ref;
249 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
251 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
252 LONG ref = InterlockedDecrement(&This->ref);
254 TRACE("(%p) ref=%d\n", This, ref);
256 if(!ref) {
257 unsigned i;
259 for(i=0; i < This->len; i++)
260 node_release(&This->elems[i]->node);
261 heap_free(This->elems);
263 release_dispex(&This->dispex);
264 heap_free(This);
267 return ref;
270 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
271 UINT *pctinfo)
273 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
274 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
277 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
278 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
280 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
281 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
284 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
285 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
287 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
288 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
289 lcid, rgDispId);
292 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
293 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
294 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
296 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
297 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
298 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
301 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
302 BSTR *String)
304 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
305 FIXME("(%p)->(%p)\n", This, String);
306 return E_NOTIMPL;
309 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
310 LONG v)
312 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
313 FIXME("(%p)->(%d)\n", This, v);
314 return E_NOTIMPL;
317 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
318 LONG *p)
320 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
322 TRACE("(%p)->(%p)\n", This, p);
324 *p = This->len;
325 return S_OK;
328 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
329 IUnknown **p)
331 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
332 HTMLElementCollectionEnum *ret;
334 TRACE("(%p)->(%p)\n", This, p);
336 ret = heap_alloc(sizeof(*ret));
337 if(!ret)
338 return E_OUTOFMEMORY;
340 ret->IEnumVARIANT_iface.lpVtbl = &HTMLElementCollectionEnumVtbl;
341 ret->ref = 1;
342 ret->iter = 0;
344 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
345 ret->col = This;
347 *p = (IUnknown*)&ret->IEnumVARIANT_iface;
348 return S_OK;
351 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
353 BSTR elem_id;
354 HRESULT hres;
356 hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
357 if(FAILED(hres)){
358 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
359 return FALSE;
362 if(elem_id && !strcmpW(elem_id, name)) {
363 SysFreeString(elem_id);
364 return TRUE;
367 SysFreeString(elem_id);
368 return FALSE;
371 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
373 const PRUnichar *str;
374 nsAString nsstr;
375 BOOL ret = FALSE;
376 nsresult nsres;
378 static const PRUnichar nameW[] = {'n','a','m','e',0};
380 if(!elem->nselem)
381 return FALSE;
383 nsAString_Init(&nsstr, NULL);
384 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
385 nsAString_GetData(&nsstr, &str);
386 if(!strcmpiW(str, name)) {
387 nsAString_Finish(&nsstr);
388 return TRUE;
391 nsres = get_elem_attr_value(elem->nselem, nameW, &nsstr, &str);
392 if(NS_SUCCEEDED(nsres)) {
393 ret = !strcmpiW(str, name);
394 nsAString_Finish(&nsstr);
397 return ret;
400 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
402 if(idx < This->len) {
403 *ret = (IDispatch*)&This->elems[idx]->node.event_target.dispex.IDispatchEx_iface;
404 IDispatch_AddRef(*ret);
407 return S_OK;
410 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
411 VARIANT name, VARIANT index, IDispatch **pdisp)
413 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
414 HRESULT hres = S_OK;
416 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
418 *pdisp = NULL;
420 switch(V_VT(&name)) {
421 case VT_I4:
422 case VT_INT:
423 if(V_I4(&name) < 0)
424 return E_INVALIDARG;
425 hres = get_item_idx(This, V_I4(&name), pdisp);
426 break;
428 case VT_UI4:
429 case VT_UINT:
430 hres = get_item_idx(This, V_UINT(&name), pdisp);
431 break;
433 case VT_BSTR: {
434 DWORD i;
436 if(V_VT(&index) == VT_I4) {
437 LONG idx = V_I4(&index);
439 if(idx < 0)
440 return E_INVALIDARG;
442 for(i=0; i<This->len; i++) {
443 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
444 break;
447 if(i != This->len) {
448 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
449 IDispatch_AddRef(*pdisp);
451 }else {
452 elem_vector_t buf = {NULL, 0, 8};
454 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
456 for(i=0; i<This->len; i++) {
457 if(is_elem_name(This->elems[i], V_BSTR(&name))) {
458 node_addref(&This->elems[i]->node);
459 elem_vector_add(&buf, This->elems[i]);
463 if(buf.len > 1) {
464 elem_vector_normalize(&buf);
465 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
466 }else {
467 if(buf.len == 1) {
468 /* Already AddRef-ed */
469 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
472 heap_free(buf.buf);
475 break;
478 default:
479 FIXME("Unsupported name %s\n", debugstr_variant(&name));
480 hres = E_NOTIMPL;
483 if(SUCCEEDED(hres))
484 TRACE("returning %p\n", *pdisp);
485 return hres;
488 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
489 VARIANT tagName, IDispatch **pdisp)
491 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
492 DWORD i;
493 nsAString tag_str;
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]->nselem)
510 continue;
512 nsIDOMHTMLElement_GetTagName(This->elems[i]->nselem, &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("fount %d tags\n", buf.len);
527 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
528 return S_OK;
531 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
532 HTMLElementCollection_QueryInterface,
533 HTMLElementCollection_AddRef,
534 HTMLElementCollection_Release,
535 HTMLElementCollection_GetTypeInfoCount,
536 HTMLElementCollection_GetTypeInfo,
537 HTMLElementCollection_GetIDsOfNames,
538 HTMLElementCollection_Invoke,
539 HTMLElementCollection_toString,
540 HTMLElementCollection_put_length,
541 HTMLElementCollection_get_length,
542 HTMLElementCollection_get__newEnum,
543 HTMLElementCollection_item,
544 HTMLElementCollection_tags
547 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
549 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
552 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
554 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
556 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
557 WCHAR *ptr;
558 DWORD idx=0;
560 if(!*name)
561 return DISP_E_UNKNOWNNAME;
563 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
564 idx = idx*10 + (*ptr-'0');
566 if(*ptr) {
567 /* the name contains alpha characters, so search by name & id */
568 for(idx = 0; idx < This->len; ++idx) {
569 if(is_elem_id(This->elems[idx], name) ||
570 is_elem_name(This->elems[idx], name))
571 break;
575 if(idx >= This->len)
576 return DISP_E_UNKNOWNNAME;
578 *dispid = DISPID_ELEMCOL_0 + idx;
579 TRACE("ret %x\n", *dispid);
580 return S_OK;
583 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
584 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
586 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
587 DWORD idx;
589 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
591 idx = id - DISPID_ELEMCOL_0;
592 if(idx >= This->len)
593 return DISP_E_UNKNOWNNAME;
595 switch(flags) {
596 case DISPATCH_PROPERTYGET:
597 V_VT(res) = VT_DISPATCH;
598 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
599 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
600 break;
601 default:
602 FIXME("unimplemented flags %x\n", flags);
603 return E_NOTIMPL;
606 return S_OK;
609 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
610 NULL,
611 HTMLElementCollection_get_dispid,
612 HTMLElementCollection_invoke,
613 NULL
616 static const tid_t HTMLElementCollection_iface_tids[] = {
617 IHTMLElementCollection_tid,
621 static dispex_static_data_t HTMLElementCollection_dispex = {
622 &HTMLElementColection_dispex_vtbl,
623 DispHTMLElementCollection_tid,
624 HTMLElementCollection_iface_tids
627 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
629 nsIDOMNodeList *nsnode_list;
630 nsIDOMNode *iter;
631 UINT32 list_len = 0, i;
632 nsresult nsres;
633 HRESULT hres;
635 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
636 if(NS_FAILED(nsres)) {
637 ERR("GetChildNodes failed: %08x\n", nsres);
638 return;
641 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
642 if(!list_len)
643 return;
645 for(i=0; i<list_len; i++) {
646 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
647 if(NS_FAILED(nsres)) {
648 ERR("Item failed: %08x\n", nsres);
649 continue;
652 if(is_elem_node(iter)) {
653 HTMLDOMNode *node;
655 hres = get_node(doc, iter, TRUE, &node);
656 if(FAILED(hres)) {
657 FIXME("get_node failed: %08x\n", hres);
658 continue;
661 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
662 create_all_list(doc, node, buf);
667 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
669 elem_vector_t buf = {NULL, 0, 8};
671 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
673 if(include_root) {
674 node_addref(node);
675 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
677 create_all_list(node->doc, node, &buf);
678 elem_vector_normalize(&buf);
680 return HTMLElementCollection_Create(buf.buf, buf.len);
683 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
685 UINT32 length = 0, i;
686 HTMLDOMNode *node;
687 elem_vector_t buf;
688 HRESULT hres;
690 nsIDOMNodeList_GetLength(nslist, &length);
692 buf.len = 0;
693 buf.size = length;
694 if(length) {
695 nsIDOMNode *nsnode;
697 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
699 for(i=0; i<length; i++) {
700 nsIDOMNodeList_Item(nslist, i, &nsnode);
701 if(is_elem_node(nsnode)) {
702 hres = get_node(doc, nsnode, TRUE, &node);
703 if(FAILED(hres))
704 continue;
705 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
707 nsIDOMNode_Release(nsnode);
710 elem_vector_normalize(&buf);
711 }else {
712 buf.buf = NULL;
715 return HTMLElementCollection_Create(buf.buf, buf.len);
718 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, nsIDOMHTMLCollection *nscol)
720 UINT32 length = 0, i;
721 elem_vector_t buf;
722 HTMLDOMNode *node;
723 HRESULT hres = S_OK;
725 if(nscol)
726 nsIDOMHTMLCollection_GetLength(nscol, &length);
728 buf.len = buf.size = length;
729 if(buf.len) {
730 nsIDOMNode *nsnode;
732 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
734 for(i=0; i<length; i++) {
735 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
736 hres = get_node(doc, nsnode, TRUE, &node);
737 nsIDOMNode_Release(nsnode);
738 if(FAILED(hres))
739 break;
740 buf.buf[i] = elem_from_HTMLDOMNode(node);
742 }else {
743 buf.buf = NULL;
746 if(FAILED(hres)) {
747 heap_free(buf.buf);
748 return NULL;
751 return HTMLElementCollection_Create(buf.buf, buf.len);
754 HRESULT get_elem_source_index(HTMLElement *elem, LONG *ret)
756 elem_vector_t buf = {NULL, 0, 8};
757 nsIDOMNode *parent_node, *iter;
758 UINT16 parent_type;
759 HTMLDOMNode *node;
760 int i;
761 nsresult nsres;
762 HRESULT hres;
764 iter = elem->node.nsnode;
765 nsIDOMNode_AddRef(iter);
767 /* Find document or document fragment parent. */
768 while(1) {
769 nsres = nsIDOMNode_GetParentNode(iter, &parent_node);
770 nsIDOMNode_Release(iter);
771 assert(nsres == NS_OK);
772 if(!parent_node)
773 break;
775 nsres = nsIDOMNode_GetNodeType(parent_node, &parent_type);
776 assert(nsres == NS_OK);
778 if(parent_type != ELEMENT_NODE) {
779 if(parent_type != DOCUMENT_NODE && parent_type != DOCUMENT_FRAGMENT_NODE)
780 FIXME("Unexpected parent_type %d\n", parent_type);
781 break;
784 iter = parent_node;
787 if(!parent_node) {
788 *ret = -1;
789 return S_OK;
792 hres = get_node(elem->node.doc, parent_node, TRUE, &node);
793 nsIDOMNode_Release(parent_node);
794 if(FAILED(hres))
795 return hres;
798 /* Create all children collection and find the element in it.
799 * This could be optimized if we ever find the reason. */
800 buf.buf = heap_alloc(buf.size*sizeof(*buf.buf));
801 if(!buf.buf) {
802 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
803 return E_OUTOFMEMORY;
806 create_all_list(elem->node.doc, node, &buf);
808 for(i=0; i < buf.len; i++) {
809 if(buf.buf[i] == elem)
810 break;
812 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
813 heap_free(buf.buf);
814 if(i == buf.len) {
815 FIXME("The element is not in parent's child list?\n");
816 return E_UNEXPECTED;
819 *ret = i;
820 return S_OK;
823 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len)
825 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
827 if (!ret)
828 return NULL;
830 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
831 ret->ref = 1;
832 ret->elems = elems;
833 ret->len = len;
835 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
836 &HTMLElementCollection_dispex);
838 TRACE("ret=%p len=%d\n", ret, len);
840 return &ret->IHTMLElementCollection_iface;