htmlelemcol.c 23.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2006-2008 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
20
#include <assert.h>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"

#include "wine/debug.h"

#include "mshtml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

typedef struct {
    DispatchEx dispex;
37
    IHTMLElementCollection IHTMLElementCollection_iface;
38 39 40 41 42 43 44

    HTMLElement **elems;
    DWORD len;

    LONG ref;
} HTMLElementCollection;

45 46 47 48 49 50 51 52 53
typedef struct {
    IEnumVARIANT IEnumVARIANT_iface;

    LONG ref;

    ULONG iter;
    HTMLElementCollection *col;
} HTMLElementCollectionEnum;

54 55 56 57 58 59
typedef struct {
    HTMLElement **buf;
    DWORD len;
    DWORD size;
} elem_vector_t;

60 61 62 63 64 65
/* FIXME: Handle it better way */
static inline HTMLElement *elem_from_HTMLDOMNode(HTMLDOMNode *iface)
{
    return CONTAINING_RECORD(iface, HTMLElement, node);
}

66
static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement**,DWORD,compat_mode_t);
67

68 69 70 71
static void elem_vector_add(elem_vector_t *buf, HTMLElement *elem)
{
    if(buf->len == buf->size) {
        buf->size <<= 1;
72
        buf->buf = heap_realloc(buf->buf, buf->size*sizeof(HTMLElement*));
73 74 75 76 77 78 79 80 81 82 83
    }

    buf->buf[buf->len++] = elem;
}

static void elem_vector_normalize(elem_vector_t *buf)
{
    if(!buf->len) {
        heap_free(buf->buf);
        buf->buf = NULL;
    }else if(buf->size > buf->len) {
84
        buf->buf = heap_realloc(buf->buf, buf->len*sizeof(HTMLElement*));
85 86 87 88 89 90 91
    }

    buf->size = buf->len;
}

static inline BOOL is_elem_node(nsIDOMNode *node)
{
92
    UINT16 type=0;
93 94 95 96 97 98

    nsIDOMNode_GetNodeType(node, &type);

    return type == ELEMENT_NODE || type == COMMENT_NODE;
}

99 100 101 102 103 104 105 106 107
static inline HTMLElementCollectionEnum *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
{
    return CONTAINING_RECORD(iface, HTMLElementCollectionEnum, IEnumVARIANT_iface);
}

static HRESULT WINAPI HTMLElementCollectionEnum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);

108 109
    TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);

110 111 112 113 114
    if(IsEqualGUID(riid, &IID_IUnknown)) {
        *ppv = &This->IEnumVARIANT_iface;
    }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
        *ppv = &This->IEnumVARIANT_iface;
    }else {
115
        FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI HTMLElementCollectionEnum_AddRef(IEnumVARIANT *iface)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI HTMLElementCollectionEnum_Release(IEnumVARIANT *iface)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    if(!ref) {
        IHTMLElementCollection_Release(&This->col->IHTMLElementCollection_iface);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI HTMLElementCollectionEnum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
    ULONG fetched = 0;

    TRACE("(%p)->(%d %p %p)\n", This, celt, rgVar, pCeltFetched);

    while(This->iter+fetched < This->col->len && fetched < celt) {
        V_VT(rgVar+fetched) = VT_DISPATCH;
        V_DISPATCH(rgVar+fetched) = (IDispatch*)&This->col->elems[This->iter+fetched]->IHTMLElement_iface;
        IDispatch_AddRef(V_DISPATCH(rgVar+fetched));
        fetched++;
    }

    This->iter += fetched;
164 165
    if(pCeltFetched)
        *pCeltFetched = fetched;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    return fetched == celt ? S_OK : S_FALSE;
}

static HRESULT WINAPI HTMLElementCollectionEnum_Skip(IEnumVARIANT *iface, ULONG celt)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);

    TRACE("(%p)->(%d)\n", This, celt);

    if(This->iter + celt > This->col->len) {
        This->iter = This->col->len;
        return S_FALSE;
    }

    This->iter += celt;
    return S_OK;
}

static HRESULT WINAPI HTMLElementCollectionEnum_Reset(IEnumVARIANT *iface)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);

    TRACE("(%p)->()\n", This);

    This->iter = 0;
    return S_OK;
}

static HRESULT WINAPI HTMLElementCollectionEnum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
{
    HTMLElementCollectionEnum *This = impl_from_IEnumVARIANT(iface);
    FIXME("(%p)->(%p)\n", This, ppEnum);
    return E_NOTIMPL;
}

static const IEnumVARIANTVtbl HTMLElementCollectionEnumVtbl = {
    HTMLElementCollectionEnum_QueryInterface,
    HTMLElementCollectionEnum_AddRef,
    HTMLElementCollectionEnum_Release,
    HTMLElementCollectionEnum_Next,
    HTMLElementCollectionEnum_Skip,
    HTMLElementCollectionEnum_Reset,
    HTMLElementCollectionEnum_Clone
};

211 212 213 214
static inline HTMLElementCollection *impl_from_IHTMLElementCollection(IHTMLElementCollection *iface)
{
    return CONTAINING_RECORD(iface, HTMLElementCollection, IHTMLElementCollection_iface);
}
215 216 217 218

static HRESULT WINAPI HTMLElementCollection_QueryInterface(IHTMLElementCollection *iface,
                                                           REFIID riid, void **ppv)
{
219
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
220

221 222
    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 228
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
229 230 231 232
    }else {
        *ppv = NULL;
        FIXME("Unsupported iface %s\n", debugstr_mshtml_guid(riid));
        return E_NOINTERFACE;
233 234
    }

235 236
    IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
    return S_OK;
237 238 239 240
}

static ULONG WINAPI HTMLElementCollection_AddRef(IHTMLElementCollection *iface)
{
241
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
242 243 244 245 246 247 248 249 250
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI HTMLElementCollection_Release(IHTMLElementCollection *iface)
{
251
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
252 253 254 255 256
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    if(!ref) {
257 258 259 260
        unsigned i;

        for(i=0; i < This->len; i++)
            node_release(&This->elems[i]->node);
261
        heap_free(This->elems);
262 263

        release_dispex(&This->dispex);
264 265 266 267 268 269 270 271 272
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI HTMLElementCollection_GetTypeInfoCount(IHTMLElementCollection *iface,
                                                             UINT *pctinfo)
{
273
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
274
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
275 276 277 278 279
}

static HRESULT WINAPI HTMLElementCollection_GetTypeInfo(IHTMLElementCollection *iface,
        UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
280
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
281
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
282 283 284 285 286
}

static HRESULT WINAPI HTMLElementCollection_GetIDsOfNames(IHTMLElementCollection *iface,
        REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
287
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
288 289
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
290 291 292 293 294 295
}

static HRESULT WINAPI HTMLElementCollection_Invoke(IHTMLElementCollection *iface,
        DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
        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 299 300 301 302 303
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI HTMLElementCollection_toString(IHTMLElementCollection *iface,
                                                     BSTR *String)
{
304
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
305 306 307 308 309
    FIXME("(%p)->(%p)\n", This, String);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElementCollection_put_length(IHTMLElementCollection *iface,
310
                                                       LONG v)
311
{
312
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
313
    FIXME("(%p)->(%d)\n", This, v);
314 315 316 317
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElementCollection_get_length(IHTMLElementCollection *iface,
318
                                                       LONG *p)
319
{
320
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
321 322 323 324 325 326 327 328 329 330

    TRACE("(%p)->(%p)\n", This, p);

    *p = This->len;
    return S_OK;
}

static HRESULT WINAPI HTMLElementCollection_get__newEnum(IHTMLElementCollection *iface,
                                                         IUnknown **p)
{
331
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    HTMLElementCollectionEnum *ret;

    TRACE("(%p)->(%p)\n", This, p);

    ret = heap_alloc(sizeof(*ret));
    if(!ret)
        return E_OUTOFMEMORY;

    ret->IEnumVARIANT_iface.lpVtbl = &HTMLElementCollectionEnumVtbl;
    ret->ref = 1;
    ret->iter = 0;

    IHTMLElementCollection_AddRef(&This->IHTMLElementCollection_iface);
    ret->col = This;

    *p = (IUnknown*)&ret->IEnumVARIANT_iface;
    return S_OK;
349 350
}

351 352 353 354 355
static BOOL is_elem_id(HTMLElement *elem, LPCWSTR name)
{
    BSTR elem_id;
    HRESULT hres;

356
    hres = IHTMLElement_get_id(&elem->IHTMLElement_iface, &elem_id);
357 358 359 360 361
    if(FAILED(hres)){
        WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
        return FALSE;
    }

362
    if(elem_id && !wcscmp(elem_id, name)) {
363 364 365 366 367 368 369 370
        SysFreeString(elem_id);
        return TRUE;
    }

    SysFreeString(elem_id);
    return FALSE;
}

371 372 373
static BOOL is_elem_name(HTMLElement *elem, LPCWSTR name)
{
    const PRUnichar *str;
374
    nsAString nsstr;
375 376 377
    BOOL ret = FALSE;
    nsresult nsres;

378
    if(!elem->dom_element)
379 380 381
        return FALSE;

    nsAString_Init(&nsstr, NULL);
382
    nsIDOMElement_GetId(elem->dom_element, &nsstr);
383
    nsAString_GetData(&nsstr, &str);
384
    if(!wcsicmp(str, name)) {
385 386 387 388
        nsAString_Finish(&nsstr);
        return TRUE;
    }

389
    nsres = get_elem_attr_value(elem->dom_element, L"name", &nsstr, &str);
390
    if(NS_SUCCEEDED(nsres)) {
391
        ret = !wcsicmp(str, name);
392
        nsAString_Finish(&nsstr);
393 394 395 396 397
    }

    return ret;
}

398 399 400
static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
{
    if(idx < This->len) {
401
        *ret = (IDispatch*)&This->elems[idx]->node.event_target.dispex.IDispatchEx_iface;
402 403 404 405 406 407
        IDispatch_AddRef(*ret);
    }

    return S_OK;
}

408 409 410
static HRESULT WINAPI HTMLElementCollection_item(IHTMLElementCollection *iface,
        VARIANT name, VARIANT index, IDispatch **pdisp)
{
411
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
412
    HRESULT hres = S_OK;
413

414
    TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
415 416 417

    *pdisp = NULL;

418 419
    switch(V_VT(&name)) {
    case VT_I4:
420
    case VT_INT:
421
        if(V_I4(&name) < 0)
422
            return dispex_compat_mode(&This->dispex) < COMPAT_MODE_IE9 ? E_INVALIDARG : S_OK;
423 424
        hres = get_item_idx(This, V_I4(&name), pdisp);
        break;
425

426
    case VT_UI4:
427 428 429
    case VT_UINT:
        hres = get_item_idx(This, V_UINT(&name), pdisp);
        break;
430

431
    case VT_BSTR: {
432 433 434 435 436 437 438 439 440 441 442 443 444 445
        DWORD i;

        if(V_VT(&index) == VT_I4) {
            LONG idx = V_I4(&index);

            if(idx < 0)
                return E_INVALIDARG;

            for(i=0; i<This->len; i++) {
                if(is_elem_name(This->elems[i], V_BSTR(&name)) && !idx--)
                    break;
            }

            if(i != This->len) {
446
                *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
447 448 449 450 451 452 453 454
                IDispatch_AddRef(*pdisp);
            }
        }else {
            elem_vector_t buf = {NULL, 0, 8};

            buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));

            for(i=0; i<This->len; i++) {
455 456
                if(is_elem_name(This->elems[i], V_BSTR(&name))) {
                    node_addref(&This->elems[i]->node);
457
                    elem_vector_add(&buf, This->elems[i]);
458
                }
459 460 461 462
            }

            if(buf.len > 1) {
                elem_vector_normalize(&buf);
463 464
                *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len,
                                                                  dispex_compat_mode(&This->dispex));
465 466
            }else {
                if(buf.len == 1) {
467
                    /* Already AddRef-ed */
468
                    *pdisp = (IDispatch*)&buf.buf[0]->IHTMLElement_iface;
469 470 471 472 473
                }

                heap_free(buf.buf);
            }
        }
474 475 476 477 478 479
        break;
    }

    default:
        FIXME("Unsupported name %s\n", debugstr_variant(&name));
        hres = E_NOTIMPL;
480 481
    }

482 483 484
    if(SUCCEEDED(hres))
        TRACE("returning %p\n", *pdisp);
    return hres;
485 486 487 488 489
}

static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
                                                 VARIANT tagName, IDispatch **pdisp)
{
490
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    DWORD i;
    nsAString tag_str;
    const PRUnichar *tag;
    elem_vector_t buf = {NULL, 0, 8};

    if(V_VT(&tagName) != VT_BSTR) {
        WARN("Invalid arg\n");
        return DISP_E_MEMBERNOTFOUND;
    }

    TRACE("(%p)->(%s %p)\n", This, debugstr_w(V_BSTR(&tagName)), pdisp);

    buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));

    nsAString_Init(&tag_str, NULL);

    for(i=0; i<This->len; i++) {
508
        if(!This->elems[i]->dom_element)
509 510
            continue;

511
        nsIDOMElement_GetTagName(This->elems[i]->dom_element, &tag_str);
512 513 514
        nsAString_GetData(&tag_str, &tag);

        if(CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, tag, -1,
515 516
                          V_BSTR(&tagName), -1) == CSTR_EQUAL) {
            node_addref(&This->elems[i]->node);
517
            elem_vector_add(&buf, This->elems[i]);
518
        }
519 520 521 522 523
    }

    nsAString_Finish(&tag_str);
    elem_vector_normalize(&buf);

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
524
    TRACE("found %d tags\n", buf.len);
525

526 527
    *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len,
                                                      dispex_compat_mode(&This->dispex));
528 529 530
    return S_OK;
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static const IHTMLElementCollectionVtbl HTMLElementCollectionVtbl = {
    HTMLElementCollection_QueryInterface,
    HTMLElementCollection_AddRef,
    HTMLElementCollection_Release,
    HTMLElementCollection_GetTypeInfoCount,
    HTMLElementCollection_GetTypeInfo,
    HTMLElementCollection_GetIDsOfNames,
    HTMLElementCollection_Invoke,
    HTMLElementCollection_toString,
    HTMLElementCollection_put_length,
    HTMLElementCollection_get_length,
    HTMLElementCollection_get__newEnum,
    HTMLElementCollection_item,
    HTMLElementCollection_tags
};

static inline HTMLElementCollection *impl_from_DispatchEx(DispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLElementCollection, dispex);
}

552 553
#define DISPID_ELEMCOL_0 MSHTML_DISPID_CUSTOM_MIN

554
static HRESULT HTMLElementCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
555
{
556
    HTMLElementCollection *This = impl_from_DispatchEx(dispex);
557 558 559
    WCHAR *ptr;
    DWORD idx=0;

560 561 562
    if(!*name)
        return DISP_E_UNKNOWNNAME;

563
    for(ptr = name; *ptr && is_digit(*ptr); ptr++)
564 565
        idx = idx*10 + (*ptr-'0');

566 567 568 569 570 571 572 573 574 575
    if(*ptr) {
        /* the name contains alpha characters, so search by name & id */
        for(idx = 0; idx < This->len; ++idx) {
            if(is_elem_id(This->elems[idx], name) ||
                    is_elem_name(This->elems[idx], name))
                break;
        }
    }

    if(idx >= This->len)
576 577 578 579 580 581 582
        return DISP_E_UNKNOWNNAME;

    *dispid = DISPID_ELEMCOL_0 + idx;
    TRACE("ret %x\n", *dispid);
    return S_OK;
}

583
static HRESULT HTMLElementCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
584 585
        VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
{
586
    HTMLElementCollection *This = impl_from_DispatchEx(dispex);
587 588 589 590 591 592 593 594 595
    DWORD idx;

    TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);

    idx = id - DISPID_ELEMCOL_0;
    if(idx >= This->len)
        return DISP_E_UNKNOWNNAME;

    switch(flags) {
596
    case DISPATCH_PROPERTYGET:
597
        V_VT(res) = VT_DISPATCH;
598 599
        V_DISPATCH(res) = (IDispatch*)&This->elems[idx]->IHTMLElement_iface;
        IHTMLElement_AddRef(&This->elems[idx]->IHTMLElement_iface);
600 601 602 603 604 605 606 607 608 609
        break;
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
}

static const dispex_static_data_vtbl_t HTMLElementColection_dispex_vtbl = {
610
    NULL,
611
    HTMLElementCollection_get_dispid,
612 613
    HTMLElementCollection_invoke,
    NULL
614 615 616 617 618 619
};

static const tid_t HTMLElementCollection_iface_tids[] = {
    IHTMLElementCollection_tid,
    0
};
620

621 622 623 624 625 626
static dispex_static_data_t HTMLElementCollection_dispex = {
    &HTMLElementColection_dispex_vtbl,
    DispHTMLElementCollection_tid,
    HTMLElementCollection_iface_tids
};

627
static void create_all_list(HTMLDOMNode *elem, elem_vector_t *buf)
628 629 630
{
    nsIDOMNodeList *nsnode_list;
    nsIDOMNode *iter;
631
    UINT32 list_len = 0, i;
632
    nsresult nsres;
633
    HRESULT hres;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

    nsres = nsIDOMNode_GetChildNodes(elem->nsnode, &nsnode_list);
    if(NS_FAILED(nsres)) {
        ERR("GetChildNodes failed: %08x\n", nsres);
        return;
    }

    nsIDOMNodeList_GetLength(nsnode_list, &list_len);
    if(!list_len)
        return;

    for(i=0; i<list_len; i++) {
        nsres = nsIDOMNodeList_Item(nsnode_list, i, &iter);
        if(NS_FAILED(nsres)) {
            ERR("Item failed: %08x\n", nsres);
            continue;
        }

        if(is_elem_node(iter)) {
653 654
            HTMLDOMNode *node;

655
            hres = get_node(iter, TRUE, &node);
656 657 658 659
            if(FAILED(hres)) {
                FIXME("get_node failed: %08x\n", hres);
                continue;
            }
660

661
            elem_vector_add(buf, elem_from_HTMLDOMNode(node));
662
            create_all_list(node, buf);
663 664 665 666
        }
    }
}

667
IHTMLElementCollection *create_all_collection(HTMLDOMNode *node, BOOL include_root)
668 669 670
{
    elem_vector_t buf = {NULL, 0, 8};

671
    buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));
672

673 674
    if(include_root) {
        node_addref(node);
675
        elem_vector_add(&buf, elem_from_HTMLDOMNode(node));
676
    }
677
    create_all_list(node, &buf);
678 679
    elem_vector_normalize(&buf);

680 681
    return HTMLElementCollection_Create(buf.buf, buf.len,
                                        dispex_compat_mode(&node->event_target.dispex));
682 683
}

684
IHTMLElementCollection *create_collection_from_nodelist(nsIDOMNodeList *nslist, compat_mode_t compat_mode)
685
{
686
    UINT32 length = 0, i;
687
    HTMLDOMNode *node;
688
    elem_vector_t buf;
689
    HRESULT hres;
690 691 692

    nsIDOMNodeList_GetLength(nslist, &length);

693 694 695
    buf.len = 0;
    buf.size = length;
    if(length) {
696 697 698 699 700 701
        nsIDOMNode *nsnode;

        buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));

        for(i=0; i<length; i++) {
            nsIDOMNodeList_Item(nslist, i, &nsnode);
702
            if(is_elem_node(nsnode)) {
703
                hres = get_node(nsnode, TRUE, &node);
704 705
                if(FAILED(hres))
                    continue;
706
                buf.buf[buf.len++] = elem_from_HTMLDOMNode(node);
707
            }
708 709
            nsIDOMNode_Release(nsnode);
        }
710 711

        elem_vector_normalize(&buf);
712 713 714 715
    }else {
        buf.buf = NULL;
    }

716
    return HTMLElementCollection_Create(buf.buf, buf.len, compat_mode);
717 718
}

719
IHTMLElementCollection *create_collection_from_htmlcol(nsIDOMHTMLCollection *nscol, compat_mode_t compat_mode)
720
{
721
    UINT32 length = 0, i;
722
    elem_vector_t buf;
723 724
    HTMLDOMNode *node;
    HRESULT hres = S_OK;
725

726 727
    if(nscol)
        nsIDOMHTMLCollection_GetLength(nscol, &length);
728 729 730 731 732 733 734 735 736

    buf.len = buf.size = length;
    if(buf.len) {
        nsIDOMNode *nsnode;

        buf.buf = heap_alloc(buf.size*sizeof(HTMLElement*));

        for(i=0; i<length; i++) {
            nsIDOMHTMLCollection_Item(nscol, i, &nsnode);
737
            hres = get_node(nsnode, TRUE, &node);
738
            nsIDOMNode_Release(nsnode);
739 740
            if(FAILED(hres))
                break;
741
            buf.buf[i] = elem_from_HTMLDOMNode(node);
742 743 744 745 746
        }
    }else {
        buf.buf = NULL;
    }

747 748 749 750 751
    if(FAILED(hres)) {
        heap_free(buf.buf);
        return NULL;
    }

752
    return HTMLElementCollection_Create(buf.buf, buf.len, compat_mode);
753 754
}

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
HRESULT get_elem_source_index(HTMLElement *elem, LONG *ret)
{
    elem_vector_t buf = {NULL, 0, 8};
    nsIDOMNode *parent_node, *iter;
    UINT16 parent_type;
    HTMLDOMNode *node;
    int i;
    nsresult nsres;
    HRESULT hres;

    iter = elem->node.nsnode;
    nsIDOMNode_AddRef(iter);

    /* Find document or document fragment parent. */
    while(1) {
        nsres = nsIDOMNode_GetParentNode(iter, &parent_node);
        nsIDOMNode_Release(iter);
        assert(nsres == NS_OK);
        if(!parent_node)
            break;

        nsres = nsIDOMNode_GetNodeType(parent_node, &parent_type);
        assert(nsres == NS_OK);

        if(parent_type != ELEMENT_NODE) {
            if(parent_type != DOCUMENT_NODE && parent_type != DOCUMENT_FRAGMENT_NODE)
                FIXME("Unexpected parent_type %d\n", parent_type);
            break;
        }

        iter = parent_node;
    }

    if(!parent_node) {
        *ret = -1;
        return S_OK;
    }

793
    hres = get_node(parent_node, TRUE, &node);
794 795 796 797 798 799 800 801 802 803 804 805
    nsIDOMNode_Release(parent_node);
    if(FAILED(hres))
        return hres;

    /* Create all children collection and find the element in it.
     * This could be optimized if we ever find the reason. */
    buf.buf = heap_alloc(buf.size*sizeof(*buf.buf));
    if(!buf.buf) {
        IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
        return E_OUTOFMEMORY;
    }

806
    create_all_list(node, &buf);
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

    for(i=0; i < buf.len; i++) {
        if(buf.buf[i] == elem)
            break;
    }
    IHTMLDOMNode_Release(&node->IHTMLDOMNode_iface);
    heap_free(buf.buf);
    if(i == buf.len) {
        FIXME("The element is not in parent's child list?\n");
        return E_UNEXPECTED;
    }

    *ret = i;
    return S_OK;
}

823 824
static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len,
                                                            compat_mode_t compat_mode)
825 826 827
{
    HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));

828 829 830
    if (!ret)
        return NULL;

831
    ret->IHTMLElementCollection_iface.lpVtbl = &HTMLElementCollectionVtbl;
832 833 834 835
    ret->ref = 1;
    ret->elems = elems;
    ret->len = len;

836 837
    init_dispatch(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
                  &HTMLElementCollection_dispex, compat_mode);
838 839 840

    TRACE("ret=%p len=%d\n", ret, len);

841
    return &ret->IHTMLElementCollection_iface;
842
}