mshtml: Fix checks for digit characters.
[wine.git] / dlls / mshtml / htmlelemcol.c
blob51cc90110f6b444d1d5a8aea4515094cdb0b4c88
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**,DWORD,compat_mode_t);
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 && !wcscmp(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->dom_element)
381 return FALSE;
383 nsAString_Init(&nsstr, NULL);
384 nsIDOMElement_GetId(elem->dom_element, &nsstr);
385 nsAString_GetData(&nsstr, &str);
386 if(!wcsicmp(str, name)) {
387 nsAString_Finish(&nsstr);
388 return TRUE;
391 nsres = get_elem_attr_value(elem->dom_element, nameW, &nsstr, &str);
392 if(NS_SUCCEEDED(nsres)) {
393 ret = !wcsicmp(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 dispex_compat_mode(&This->dispex) < COMPAT_MODE_IE9 ? E_INVALIDARG : S_OK;
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 dispex_compat_mode(&This->dispex));
467 }else {
468 if(buf.len == 1) {
469 /* Already AddRef-ed */
470 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
473 heap_free(buf.buf);
476 break;
479 default:
480 FIXME("Unsupported name %s\n", debugstr_variant(&name));
481 hres = E_NOTIMPL;
484 if(SUCCEEDED(hres))
485 TRACE("returning %p\n", *pdisp);
486 return hres;
489 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
490 VARIANT tagName, IDispatch **pdisp)
492 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
493 DWORD i;
494 nsAString tag_str;
495 const PRUnichar *tag;
496 elem_vector_t buf = {NULL, 0, 8};
498 if(V_VT(&tagName) != VT_BSTR) {
499 WARN("Invalid arg\n");
500 return DISP_E_MEMBERNOTFOUND;
503 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
505 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
507 nsAString_Init(&tag_str, NULL);
509 for(i=0; i<This->len; i++) {
510 if(!This->elems[i]->dom_element)
511 continue;
513 nsIDOMElement_GetTagName(This->elems[i]->dom_element, &tag_str);
514 nsAString_GetData(&tag_str, &tag);
516 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
517 V_BSTR(&tagName), -1) == CSTR_EQUAL) {
518 node_addref(&This->elems[i]->node);
519 elem_vector_add(&buf, This->elems[i]);
523 nsAString_Finish(&tag_str);
524 elem_vector_normalize(&buf);
526 TRACE("found %d tags\n", buf.len);
528 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len,
529 dispex_compat_mode(&This->dispex));
530 return S_OK;
533 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
534 HTMLElementCollection_QueryInterface,
535 HTMLElementCollection_AddRef,
536 HTMLElementCollection_Release,
537 HTMLElementCollection_GetTypeInfoCount,
538 HTMLElementCollection_GetTypeInfo,
539 HTMLElementCollection_GetIDsOfNames,
540 HTMLElementCollection_Invoke,
541 HTMLElementCollection_toString,
542 HTMLElementCollection_put_length,
543 HTMLElementCollection_get_length,
544 HTMLElementCollection_get__newEnum,
545 HTMLElementCollection_item,
546 HTMLElementCollection_tags
549 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
551 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
554 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
556 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
558 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
559 WCHAR *ptr;
560 DWORD idx=0;
562 if(!*name)
563 return DISP_E_UNKNOWNNAME;
565 for(ptr = name; *ptr && is_digit(*ptr); ptr++)
566 idx = idx*10 + (*ptr-'0');
568 if(*ptr) {
569 /* the name contains alpha characters, so search by name & id */
570 for(idx = 0; idx < This->len; ++idx) {
571 if(is_elem_id(This->elems[idx], name) ||
572 is_elem_name(This->elems[idx], name))
573 break;
577 if(idx >= This->len)
578 return DISP_E_UNKNOWNNAME;
580 *dispid = DISPID_ELEMCOL_0 + idx;
581 TRACE("ret %x\n", *dispid);
582 return S_OK;
585 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
586 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
588 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
589 DWORD idx;
591 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
593 idx = id - DISPID_ELEMCOL_0;
594 if(idx >= This->len)
595 return DISP_E_UNKNOWNNAME;
597 switch(flags) {
598 case DISPATCH_PROPERTYGET:
599 V_VT(res) = VT_DISPATCH;
600 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
601 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
602 break;
603 default:
604 FIXME("unimplemented flags %x\n", flags);
605 return E_NOTIMPL;
608 return S_OK;
611 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
612 NULL,
613 HTMLElementCollection_get_dispid,
614 HTMLElementCollection_invoke,
615 NULL
618 static const tid_t HTMLElementCollection_iface_tids[] = {
619 IHTMLElementCollection_tid,
623 static dispex_static_data_t HTMLElementCollection_dispex = {
624 &HTMLElementColection_dispex_vtbl,
625 DispHTMLElementCollection_tid,
626 HTMLElementCollection_iface_tids
629 static void create_all_list(HTMLDOMNode *elem, elem_vector_t *buf)
631 nsIDOMNodeList *nsnode_list;
632 nsIDOMNode *iter;
633 UINT32 list_len = 0, i;
634 nsresult nsres;
635 HRESULT hres;
637 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
638 if(NS_FAILED(nsres)) {
639 ERR("GetChildNodes failed: %08x\n", nsres);
640 return;
643 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
644 if(!list_len)
645 return;
647 for(i=0; i<list_len; i++) {
648 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
649 if(NS_FAILED(nsres)) {
650 ERR("Item failed: %08x\n", nsres);
651 continue;
654 if(is_elem_node(iter)) {
655 HTMLDOMNode *node;
657 hres = get_node(iter, TRUE, &node);
658 if(FAILED(hres)) {
659 FIXME("get_node failed: %08x\n", hres);
660 continue;
663 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
664 create_all_list(node, buf);
669 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
671 elem_vector_t buf = {NULL, 0, 8};
673 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
675 if(include_root) {
676 node_addref(node);
677 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
679 create_all_list(node, &buf);
680 elem_vector_normalize(&buf);
682 return HTMLElementCollection_Create(buf.buf, buf.len,
683 dispex_compat_mode(&node->event_target.dispex));
686 IHTMLElementCollection *create_collection_from_nodelist(nsIDOMNodeList *nslist, compat_mode_t compat_mode)
688 UINT32 length = 0, i;
689 HTMLDOMNode *node;
690 elem_vector_t buf;
691 HRESULT hres;
693 nsIDOMNodeList_GetLength(nslist, &length);
695 buf.len = 0;
696 buf.size = length;
697 if(length) {
698 nsIDOMNode *nsnode;
700 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
702 for(i=0; i<length; i++) {
703 nsIDOMNodeList_Item(nslist, i, &nsnode);
704 if(is_elem_node(nsnode)) {
705 hres = get_node(nsnode, TRUE, &node);
706 if(FAILED(hres))
707 continue;
708 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
710 nsIDOMNode_Release(nsnode);
713 elem_vector_normalize(&buf);
714 }else {
715 buf.buf = NULL;
718 return HTMLElementCollection_Create(buf.buf, buf.len, compat_mode);
721 IHTMLElementCollection *create_collection_from_htmlcol(nsIDOMHTMLCollection *nscol, compat_mode_t compat_mode)
723 UINT32 length = 0, i;
724 elem_vector_t buf;
725 HTMLDOMNode *node;
726 HRESULT hres = S_OK;
728 if(nscol)
729 nsIDOMHTMLCollection_GetLength(nscol, &length);
731 buf.len = buf.size = length;
732 if(buf.len) {
733 nsIDOMNode *nsnode;
735 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
737 for(i=0; i<length; i++) {
738 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
739 hres = get_node(nsnode, TRUE, &node);
740 nsIDOMNode_Release(nsnode);
741 if(FAILED(hres))
742 break;
743 buf.buf[i] = elem_from_HTMLDOMNode(node);
745 }else {
746 buf.buf = NULL;
749 if(FAILED(hres)) {
750 heap_free(buf.buf);
751 return NULL;
754 return HTMLElementCollection_Create(buf.buf, buf.len, compat_mode);
757 HRESULT get_elem_source_index(HTMLElement *elem, LONG *ret)
759 elem_vector_t buf = {NULL, 0, 8};
760 nsIDOMNode *parent_node, *iter;
761 UINT16 parent_type;
762 HTMLDOMNode *node;
763 int i;
764 nsresult nsres;
765 HRESULT hres;
767 iter = elem->node.nsnode;
768 nsIDOMNode_AddRef(iter);
770 /* Find document or document fragment parent. */
771 while(1) {
772 nsres = nsIDOMNode_GetParentNode(iter, &parent_node);
773 nsIDOMNode_Release(iter);
774 assert(nsres == NS_OK);
775 if(!parent_node)
776 break;
778 nsres = nsIDOMNode_GetNodeType(parent_node, &parent_type);
779 assert(nsres == NS_OK);
781 if(parent_type != ELEMENT_NODE) {
782 if(parent_type != DOCUMENT_NODE && parent_type != DOCUMENT_FRAGMENT_NODE)
783 FIXME("Unexpected parent_type %d\n", parent_type);
784 break;
787 iter = parent_node;
790 if(!parent_node) {
791 *ret = -1;
792 return S_OK;
795 hres = get_node(parent_node, TRUE, &node);
796 nsIDOMNode_Release(parent_node);
797 if(FAILED(hres))
798 return hres;
800 /* Create all children collection and find the element in it.
801 * This could be optimized if we ever find the reason. */
802 buf.buf = heap_alloc(buf.size*sizeof(*buf.buf));
803 if(!buf.buf) {
804 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
805 return E_OUTOFMEMORY;
808 create_all_list(node, &buf);
810 for(i=0; i < buf.len; i++) {
811 if(buf.buf[i] == elem)
812 break;
814 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
815 heap_free(buf.buf);
816 if(i == buf.len) {
817 FIXME("The element is not in parent's child list?\n");
818 return E_UNEXPECTED;
821 *ret = i;
822 return S_OK;
825 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len,
826 compat_mode_t compat_mode)
828 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
830 if (!ret)
831 return NULL;
833 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
834 ret->ref = 1;
835 ret->elems = elems;
836 ret->len = len;
838 init_dispex_with_compat_mode(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
839 &HTMLElementCollection_dispex, compat_mode);
841 TRACE("ret=%p len=%d\n", ret, len);
843 return &ret->IHTMLElementCollection_iface;