htmlelemcol.c 23.2 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 **elems, DWORD len);
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 362 363 364 365 366 367 368 369 370
    if(FAILED(hres)){
        WARN("IHTMLElement_get_id failed: 0x%08x\n", hres);
        return FALSE;
    }

    if(elem_id && !strcmpW(elem_id, name)) {
        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 378 379 380 381 382 383 384 385 386 387 388 389 390
    BOOL ret = FALSE;
    nsresult nsres;

    static const PRUnichar nameW[] = {'n','a','m','e',0};

    if(!elem->nselem)
        return FALSE;

    nsAString_Init(&nsstr, NULL);
    nsIDOMHTMLElement_GetId(elem->nselem, &nsstr);
    nsAString_GetData(&nsstr, &str);
    if(!strcmpiW(str, name)) {
        nsAString_Finish(&nsstr);
        return TRUE;
    }

391
    nsres = get_elem_attr_value(elem->nselem, nameW, &nsstr, &str);
392 393
    if(NS_SUCCEEDED(nsres)) {
        ret = !strcmpiW(str, name);
394
        nsAString_Finish(&nsstr);
395 396 397 398 399
    }

    return ret;
}

400 401 402 403 404 405 406 407 408 409
static HRESULT get_item_idx(HTMLElementCollection *This, UINT idx, IDispatch **ret)
{
    if(idx < This->len) {
        *ret = (IDispatch*)This->elems[idx];
        IDispatch_AddRef(*ret);
    }

    return S_OK;
}

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

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

    *pdisp = NULL;

420 421
    switch(V_VT(&name)) {
    case VT_I4:
422
    case VT_INT:
423 424
        if(V_I4(&name) < 0)
            return E_INVALIDARG;
425 426
        hres = get_item_idx(This, V_I4(&name), pdisp);
        break;
427

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

433
    case VT_BSTR: {
434 435 436 437 438 439 440 441 442 443 444 445 446 447
        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) {
448
                *pdisp = (IDispatch*)&This->elems[i]->IHTMLElement_iface;
449 450 451 452 453 454 455 456
                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++) {
457 458
                if(is_elem_name(This->elems[i], V_BSTR(&name))) {
                    node_addref(&This->elems[i]->node);
459
                    elem_vector_add(&buf, This->elems[i]);
460
                }
461 462 463 464
            }

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

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

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

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

static HRESULT WINAPI HTMLElementCollection_tags(IHTMLElementCollection *iface,
                                                 VARIANT tagName, IDispatch **pdisp)
{
491
    HTMLElementCollection *This = impl_from_IHTMLElementCollection(iface);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    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++) {
        if(!This->elems[i]->nselem)
            continue;

512
        nsIDOMHTMLElement_GetTagName(This->elems[i]->nselem, &tag_str);
513 514 515
        nsAString_GetData(&tag_str, &tag);

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

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

    TRACE("fount %d tags\n", buf.len);

527
    *pdisp = (IDispatch*)HTMLElementCollection_Create(buf.buf, buf.len);
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 564 565
    for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
        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 627
static dispex_static_data_t HTMLElementCollection_dispex = {
    &HTMLElementColection_dispex_vtbl,
    DispHTMLElementCollection_tid,
    NULL,
    HTMLElementCollection_iface_tids
};

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

    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)) {
654 655 656 657 658 659 660
            HTMLDOMNode *node;

            hres = get_node(doc, iter, TRUE, &node);
            if(FAILED(hres)) {
                FIXME("get_node failed: %08x\n", hres);
                continue;
            }
661

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

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

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

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

681
    return HTMLElementCollection_Create(buf.buf, buf.len);
682 683
}

684
IHTMLElementCollection *create_collection_from_nodelist(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
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 703 704 705
            if(is_elem_node(nsnode)) {
                hres = get_node(doc, nsnode, TRUE, &node);
                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);
717 718
}

719
IHTMLElementCollection *create_collection_from_htmlcol(HTMLDocumentNode *doc, nsIDOMHTMLCollection *nscol)
720
{
721
    UINT32 length = 0, i;
722
    elem_vector_t buf;
723 724
    HTMLDOMNode *node;
    HRESULT hres = S_OK;
725 726 727 728 729 730 731 732 733 734 735

    nsIDOMHTMLCollection_GetLength(nscol, &length);

    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);
736
            hres = get_node(doc, nsnode, TRUE, &node);
737
            nsIDOMNode_Release(nsnode);
738 739
            if(FAILED(hres))
                break;
740
            buf.buf[i] = elem_from_HTMLDOMNode(node);
741 742 743 744 745
        }
    }else {
        buf.buf = NULL;
    }

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

751
    return HTMLElementCollection_Create(buf.buf, buf.len);
752 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 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
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;
    }

    hres = get_node(elem->node.doc, parent_node, TRUE, &node);
    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;
    }

    create_all_list(elem->node.doc, node, &buf);

    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
static IHTMLElementCollection *HTMLElementCollection_Create(HTMLElement **elems, DWORD len)
824 825 826
{
    HTMLElementCollection *ret = heap_alloc_zero(sizeof(HTMLElementCollection));

827 828 829
    if (!ret)
        return NULL;

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

835 836
    init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLElementCollection_iface,
            &HTMLElementCollection_dispex);
837 838 839

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

840
    return &ret->IHTMLElementCollection_iface;
841
}