dwrite: Implement CreateFontFaceFromHdc().
[wine.git] / dlls / mshtml / htmlelemcol.c
blob1542926c330e575d5ccf748563f80b346d8842a0
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 if(IsEqualGUID(&IID_IUnknown, riid)) {
222 *ppv = &This->IHTMLElementCollection_iface;
223 }else if(IsEqualGUID(&IID_IHTMLElementCollection, riid)) {
224 *ppv = &This->IHTMLElementCollection_iface;
225 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
226 return *ppv ? S_OK : E_NOINTERFACE;
227 }else {
228 *ppv = NULL;
229 FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
230 return E_NOINTERFACE;
233 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
234 return S_OK;
237 static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
239 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
240 LONG ref = InterlockedIncrement(&This->ref);
242 TRACE("(%p) ref=%d\n", This, ref);
244 return ref;
247 static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
249 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
250 LONG ref = InterlockedDecrement(&This->ref);
252 TRACE("(%p) ref=%d\n", This, ref);
254 if(!ref) {
255 unsigned i;
257 for(i=0; i < This->len; i++)
258 node_release(&This->elems[i]->node);
259 heap_free(This->elems);
261 release_dispex(&This->dispex);
262 heap_free(This);
265 return ref;
268 static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
269 UINT *pctinfo)
271 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
272 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
275 static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
276 UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
278 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
279 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
282 static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
283 REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
285 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
286 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
287 lcid, rgDispId);
290 static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
291 DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
292 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
294 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
295 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
296 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
299 static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
300 BSTR *String)
302 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
303 FIXME("(%p)->(%p)\n", This, String);
304 return E_NOTIMPL;
307 static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
308 LONG v)
310 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
311 FIXME("(%p)->(%d)\n", This, v);
312 return E_NOTIMPL;
315 static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
316 LONG *p)
318 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
320 TRACE("(%p)->(%p)\n", This, p);
322 *p = This->len;
323 return S_OK;
326 static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
327 IUnknown **p)
329 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
330 HTMLElementCollectionEnum *ret;
332 TRACE("(%p)->(%p)\n", This, p);
334 ret = heap_alloc(sizeof(*ret));
335 if(!ret)
336 return E_OUTOFMEMORY;
338 ret->IEnumVARIANT_iface.lpVtbl = &HTMLElementCollectionEnumVtbl;
339 ret->ref = 1;
340 ret->iter = 0;
342 IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
343 ret->col = This;
345 *p = (IUnknown*)&ret->IEnumVARIANT_iface;
346 return S_OK;
349 static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
351 BSTR elem_id;
352 HRESULT hres;
354 hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
355 if(FAILED(hres)){
356 WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
357 return FALSE;
360 if(elem_id && !strcmpW(elem_id, name)) {
361 SysFreeString(elem_id);
362 return TRUE;
365 SysFreeString(elem_id);
366 return FALSE;
369 static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
371 const PRUnichar *str;
372 nsAString nsstr;
373 BOOL ret = FALSE;
374 nsresult nsres;
376 static const PRUnichar nameW[] = {'n','a','m','e',0};
378 if(!elem->nselem)
379 return FALSE;
381 nsAString_Init(&nsstr, NULL);
382 nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
383 nsAString_GetData(&nsstr, &str);
384 if(!strcmpiW(str, name)) {
385 nsAString_Finish(&nsstr);
386 return TRUE;
389 nsres = get_elem_attr_value(elem->nselem, nameW, &nsstr, &str);
390 if(NS_SUCCEEDED(nsres)) {
391 ret = !strcmpiW(str, name);
392 nsAString_Finish(&nsstr);
395 return ret;
398 static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
400 if(idx < This->len) {
401 *ret = (IDispatch*)This->elems[idx];
402 IDispatch_AddRef(*ret);
405 return S_OK;
408 static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
409 VARIANT name, VARIANT index, IDispatch **pdisp)
411 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
412 HRESULT hres = S_OK;
414 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
416 *pdisp = NULL;
418 switch(V_VT(&name)) {
419 case VT_I4:
420 if(V_I4(&name) < 0)
421 return E_INVALIDARG;
422 hres = get_item_idx(This, V_I4(&name), pdisp);
423 break;
425 case VT_UINT:
426 hres = get_item_idx(This, V_UINT(&name), pdisp);
427 break;
429 case VT_BSTR: {
430 DWORD i;
432 if(V_VT(&index) == VT_I4) {
433 LONG idx = V_I4(&index);
435 if(idx < 0)
436 return E_INVALIDARG;
438 for(i=0; i<This->len; i++) {
439 if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
440 break;
443 if(i != This->len) {
444 *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
445 IDispatch_AddRef(*pdisp);
447 }else {
448 elem_vector_t buf = {NULL, 0, 8};
450 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
452 for(i=0; i<This->len; i++) {
453 if(is_elem_name(This->elems[i], V_BSTR(&name))) {
454 node_addref(&This->elems[i]->node);
455 elem_vector_add(&buf, This->elems[i]);
459 if(buf.len > 1) {
460 elem_vector_normalize(&buf);
461 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
462 }else {
463 if(buf.len == 1) {
464 /* Already AddRef-ed */
465 *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
468 heap_free(buf.buf);
471 break;
474 default:
475 FIXME("Unsupported name %s\n", debugstr_variant(&name));
476 hres = E_NOTIMPL;
479 if(SUCCEEDED(hres))
480 TRACE("returning %p\n", *pdisp);
481 return hres;
484 static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
485 VARIANT tagName, IDispatch **pdisp)
487 HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
488 DWORD i;
489 nsAString tag_str;
490 const PRUnichar *tag;
491 elem_vector_t buf = {NULL, 0, 8};
493 if(V_VT(&tagName) != VT_BSTR) {
494 WARN("Invalid arg\n");
495 return DISP_E_MEMBERNOTFOUND;
498 TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);
500 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
502 nsAString_Init(&tag_str, NULL);
504 for(i=0; i<This->len; i++) {
505 if(!This->elems[i]->nselem)
506 continue;
508 nsIDOMHTMLElement_GetTagName(This->elems[i]->nselem, &tag_str);
509 nsAString_GetData(&tag_str, &tag);
511 if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
512 V_BSTR(&tagName), -1) == CSTR_EQUAL) {
513 node_addref(&This->elems[i]->node);
514 elem_vector_add(&buf, This->elems[i]);
518 nsAString_Finish(&tag_str);
519 elem_vector_normalize(&buf);
521 TRACE("fount %d tags\n", buf.len);
523 *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
524 return S_OK;
527 static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
528 HTMLElementCollection_QueryInterface,
529 HTMLElementCollection_AddRef,
530 HTMLElementCollection_Release,
531 HTMLElementCollection_GetTypeInfoCount,
532 HTMLElementCollection_GetTypeInfo,
533 HTMLElementCollection_GetIDsOfNames,
534 HTMLElementCollection_Invoke,
535 HTMLElementCollection_toString,
536 HTMLElementCollection_put_length,
537 HTMLElementCollection_get_length,
538 HTMLElementCollection_get__newEnum,
539 HTMLElementCollection_item,
540 HTMLElementCollection_tags
543 static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
545 return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
548 #define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN
550 static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
552 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
553 WCHAR *ptr;
554 DWORD idx=0;
556 if(!*name)
557 return DISP_E_UNKNOWNNAME;
559 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
560 idx = idx*10 + (*ptr-'0');
562 if(*ptr) {
563 /* the name contains alpha characters, so search by name & id */
564 for(idx = 0; idx < This->len; ++idx) {
565 if(is_elem_id(This->elems[idx], name) ||
566 is_elem_name(This->elems[idx], name))
567 break;
571 if(idx >= This->len)
572 return DISP_E_UNKNOWNNAME;
574 *dispid = DISPID_ELEMCOL_0 + idx;
575 TRACE("ret %x\n", *dispid);
576 return S_OK;
579 static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
580 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
582 HTMLElementCollection *This = impl_from_DispatchEx(dispex);
583 DWORD idx;
585 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
587 idx = id - DISPID_ELEMCOL_0;
588 if(idx >= This->len)
589 return DISP_E_UNKNOWNNAME;
591 switch(flags) {
592 case DISPATCH_PROPERTYGET:
593 V_VT(res) = VT_DISPATCH;
594 V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
595 IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
596 break;
597 default:
598 FIXME("unimplemented flags %x\n", flags);
599 return E_NOTIMPL;
602 return S_OK;
605 static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
606 NULL,
607 HTMLElementCollection_get_dispid,
608 HTMLElementCollection_invoke,
609 NULL
612 static const tid_t HTMLElementCollection_iface_tids[] = {
613 IHTMLElementCollection_tid,
617 static dispex_static_data_t HTMLElementCollection_dispex = {
618 &HTMLElementColection_dispex_vtbl,
619 DispHTMLElementCollection_tid,
620 NULL,
621 HTMLElementCollection_iface_tids
624 static void create_all_list(HTMLDocumentNode *doc, HTMLDOMNode *elem, elem_vector_t *buf)
626 nsIDOMNodeList *nsnode_list;
627 nsIDOMNode *iter;
628 UINT32 list_len = 0, i;
629 nsresult nsres;
630 HRESULT hres;
632 nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
633 if(NS_FAILED(nsres)) {
634 ERR("GetChildNodes failed: %08x\n", nsres);
635 return;
638 nsIDOMNodeList_GetLength(nsnode_list, &list_len);
639 if(!list_len)
640 return;
642 for(i=0; i<list_len; i++) {
643 nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
644 if(NS_FAILED(nsres)) {
645 ERR("Item failed: %08x\n", nsres);
646 continue;
649 if(is_elem_node(iter)) {
650 HTMLDOMNode *node;
652 hres = get_node(doc, iter, TRUE, &node);
653 if(FAILED(hres)) {
654 FIXME("get_node failed: %08x\n", hres);
655 continue;
658 elem_vector_add(buf, elem_from_HTMLDOMNode(node));
659 create_all_list(doc, node, buf);
664 IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
666 elem_vector_t buf = {NULL, 0, 8};
668 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
670 if(include_root) {
671 node_addref(node);
672 elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
674 create_all_list(node->doc, node, &buf);
675 elem_vector_normalize(&buf);
677 return HTMLElementCollection_Create(buf.buf, buf.len);
680 IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
682 UINT32 length = 0, i;
683 HTMLDOMNode *node;
684 elem_vector_t buf;
685 HRESULT hres;
687 nsIDOMNodeList_GetLength(nslist, &length);
689 buf.len = 0;
690 buf.size = length;
691 if(length) {
692 nsIDOMNode *nsnode;
694 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
696 for(i=0; i<length; i++) {
697 nsIDOMNodeList_Item(nslist, i, &nsnode);
698 if(is_elem_node(nsnode)) {
699 hres = get_node(doc, nsnode, TRUE, &node);
700 if(FAILED(hres))
701 continue;
702 buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
704 nsIDOMNode_Release(nsnode);
707 elem_vector_normalize(&buf);
708 }else {
709 buf.buf = NULL;
712 return HTMLElementCollection_Create(buf.buf, buf.len);
715 IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, nsIDOMHTMLCollection *nscol)
717 UINT32 length = 0, i;
718 elem_vector_t buf;
719 HTMLDOMNode *node;
720 HRESULT hres = S_OK;
722 nsIDOMHTMLCollection_GetLength(nscol, &length);
724 buf.len = buf.size = length;
725 if(buf.len) {
726 nsIDOMNode *nsnode;
728 buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
730 for(i=0; i<length; i++) {
731 nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
732 hres = get_node(doc, nsnode, TRUE, &node);
733 nsIDOMNode_Release(nsnode);
734 if(FAILED(hres))
735 break;
736 buf.buf[i] = elem_from_HTMLDOMNode(node);
738 }else {
739 buf.buf = NULL;
742 if(FAILED(hres)) {
743 heap_free(buf.buf);
744 return NULL;
747 return HTMLElementCollection_Create(buf.buf, buf.len);
750 HRESULT get_elem_source_index(HTMLElement *elem, LONG *ret)
752 elem_vector_t buf = {NULL, 0, 8};
753 nsIDOMNode *parent_node, *iter;
754 UINT16 parent_type;
755 HTMLDOMNode *node;
756 int i;
757 nsresult nsres;
758 HRESULT hres;
760 iter = elem->node.nsnode;
761 nsIDOMNode_AddRef(iter);
763 /* Find document or document fragment parent. */
764 while(1) {
765 nsres = nsIDOMNode_GetParentNode(iter, &parent_node);
766 nsIDOMNode_Release(iter);
767 assert(nsres == NS_OK);
768 if(!parent_node)
769 break;
771 nsres = nsIDOMNode_GetNodeType(parent_node, &parent_type);
772 assert(nsres == NS_OK);
774 if(parent_type != ELEMENT_NODE) {
775 if(parent_type != DOCUMENT_NODE && parent_type != DOCUMENT_FRAGMENT_NODE)
776 FIXME("Unexpected parent_type %d\n", parent_type);
777 break;
780 iter = parent_node;
783 if(!parent_node) {
784 *ret = -1;
785 return S_OK;
788 hres = get_node(elem->node.doc, parent_node, TRUE, &node);
789 nsIDOMNode_Release(parent_node);
790 if(FAILED(hres))
791 return hres;
794 /* Create all children collection and find the element in it.
795 * This could be optimized if we ever find the reason. */
796 buf.buf = heap_alloc(buf.size*sizeof(*buf.buf));
797 if(!buf.buf) {
798 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
799 return E_OUTOFMEMORY;
802 create_all_list(elem->node.doc, node, &buf);
804 for(i=0; i < buf.len; i++) {
805 if(buf.buf[i] == elem)
806 break;
808 IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
809 heap_free(buf.buf);
810 if(i == buf.len) {
811 FIXME("The element is not in parent's child list?\n");
812 return E_UNEXPECTED;
815 *ret = i;
816 return S_OK;
819 static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len)
821 HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));
823 ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
824 ret->ref = 1;
825 ret->elems = elems;
826 ret->len = len;
828 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
829 &HTMLElementCollection_dispex);
831 TRACE("ret=%p len=%d\n", ret, len);
833 return &ret->IHTMLElementCollection_iface;