htmlelem.c 78.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2006 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
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18 19 20 21 22 23 24 25 26
 */


#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
27
#include "winreg.h"
28
#include "ole2.h"
29
#include "shlwapi.h"
30 31 32 33

#include "wine/debug.h"

#include "mshtml_private.h"
34
#include "htmlevent.h"
35
#include "htmlstyle.h"
36

37 38
WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

39 40 41 42 43
static const WCHAR aW[]        = {'A',0};
static const WCHAR bodyW[]     = {'B','O','D','Y',0};
static const WCHAR embedW[]    = {'E','M','B','E','D',0};
static const WCHAR formW[]     = {'F','O','R','M',0};
static const WCHAR frameW[]    = {'F','R','A','M','E',0};
44
static const WCHAR headW[]     = {'H','E','A','D',0};
45 46 47
static const WCHAR iframeW[]   = {'I','F','R','A','M','E',0};
static const WCHAR imgW[]      = {'I','M','G',0};
static const WCHAR inputW[]    = {'I','N','P','U','T',0};
48
static const WCHAR metaW[]     = {'M','E','T','A',0};
49 50 51 52 53 54
static const WCHAR objectW[]   = {'O','B','J','E','C','T',0};
static const WCHAR optionW[]   = {'O','P','T','I','O','N',0};
static const WCHAR scriptW[]   = {'S','C','R','I','P','T',0};
static const WCHAR selectW[]   = {'S','E','L','E','C','T',0};
static const WCHAR styleW[]    = {'S','T','Y','L','E',0};
static const WCHAR tableW[]    = {'T','A','B','L','E',0};
55
static const WCHAR tdW[]       = {'T','D',0};
56
static const WCHAR textareaW[] = {'T','E','X','T','A','R','E','A',0};
57
static const WCHAR title_tagW[]= {'T','I','T','L','E',0};
58 59 60 61
static const WCHAR trW[]       = {'T','R',0};

typedef struct {
    const WCHAR *name;
62
    HRESULT (*constructor)(HTMLDocumentNode*,nsIDOMHTMLElement*,HTMLElement**);
63 64 65 66 67 68 69 70
} tag_desc_t;

static const tag_desc_t tag_descs[] = {
    {aW,         HTMLAnchorElement_Create},
    {bodyW,      HTMLBodyElement_Create},
    {embedW,     HTMLEmbedElement_Create},
    {formW,      HTMLFormElement_Create},
    {frameW,     HTMLFrameElement_Create},
71
    {headW,      HTMLHeadElement_Create},
72 73 74
    {iframeW,    HTMLIFrame_Create},
    {imgW,       HTMLImgElement_Create},
    {inputW,     HTMLInputElement_Create},
75
    {metaW,      HTMLMetaElement_Create},
76 77 78 79 80 81
    {objectW,    HTMLObjectElement_Create},
    {optionW,    HTMLOptionElement_Create},
    {scriptW,    HTMLScriptElement_Create},
    {selectW,    HTMLSelectElement_Create},
    {styleW,     HTMLStyleElement_Create},
    {tableW,     HTMLTable_Create},
82
    {tdW,        HTMLTableCell_Create},
83
    {textareaW,  HTMLTextAreaElement_Create},
84
    {title_tagW, HTMLTitleElement_Create},
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    {trW,        HTMLTableRow_Create}
};

static const tag_desc_t *get_tag_desc(const WCHAR *tag_name)
{
    DWORD min=0, max=sizeof(tag_descs)/sizeof(*tag_descs)-1, i;
    int r;

    while(min <= max) {
        i = (min+max)/2;
        r = strcmpW(tag_name, tag_descs[i].name);
        if(!r)
            return tag_descs+i;

        if(r < 0)
            max = i-1;
        else
            min = i+1;
    }

    return NULL;
}

108
HRESULT replace_node_by_html(nsIDOMHTMLDocument *nsdoc, nsIDOMNode *nsnode, const WCHAR *html)
109 110 111 112 113 114 115 116 117
{
    nsIDOMDocumentFragment *nsfragment;
    nsIDOMNSRange *nsrange;
    nsIDOMNode *nsparent;
    nsIDOMRange *range;
    nsAString html_str;
    nsresult nsres;
    HRESULT hres = S_OK;

118
    nsres = nsIDOMHTMLDocument_CreateRange(nsdoc, &range);
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
    if(NS_FAILED(nsres)) {
        ERR("CreateRange failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMRange_QueryInterface(range, &IID_nsIDOMNSRange, (void**)&nsrange);
    nsIDOMRange_Release(range);
    if(NS_FAILED(nsres)) {
        ERR("Could not get nsIDOMNSRange: %08x\n", nsres);
        return E_FAIL;
    }

    nsAString_InitDepend(&html_str, html);
    nsIDOMNSRange_CreateContextualFragment(nsrange, &html_str, &nsfragment);
    nsIDOMNSRange_Release(nsrange);
    nsAString_Finish(&html_str);
    if(NS_FAILED(nsres)) {
        ERR("CreateContextualFragment failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMNode_GetParentNode(nsnode, &nsparent);
    if(NS_SUCCEEDED(nsres) && nsparent) {
        nsIDOMNode *nstmp;

        nsres = nsIDOMNode_ReplaceChild(nsparent, (nsIDOMNode*)nsfragment, nsnode, &nstmp);
        nsIDOMNode_Release(nsparent);
        if(NS_FAILED(nsres)) {
            ERR("ReplaceChild failed: %08x\n", nsres);
            hres = E_FAIL;
        }else if(nstmp) {
            nsIDOMNode_Release(nstmp);
        }
    }else {
        ERR("GetParentNode failed: %08x\n", nsres);
        hres = E_FAIL;
    }

    nsIDOMDocumentFragment_Release(nsfragment);
    return hres;
}

161 162 163
typedef struct
{
    DispatchEx dispex;
164
    IHTMLFiltersCollection IHTMLFiltersCollection_iface;
165 166 167 168

    LONG ref;
} HTMLFiltersCollection;

169 170 171 172
static inline HTMLFiltersCollection *impl_from_IHTMLFiltersCollection(IHTMLFiltersCollection *iface)
{
    return CONTAINING_RECORD(iface, HTMLFiltersCollection, IHTMLFiltersCollection_iface);
}
173

174
static IHTMLFiltersCollection *HTMLFiltersCollection_Create(void);
175

176 177 178 179
static inline HTMLElement *impl_from_IHTMLElement(IHTMLElement *iface)
{
    return CONTAINING_RECORD(iface, HTMLElement, IHTMLElement_iface);
}
180

181 182 183 184 185 186
HRESULT create_nselem(HTMLDocumentNode *doc, const WCHAR *tag, nsIDOMHTMLElement **ret)
{
    nsIDOMElement *nselem;
    nsAString tag_str;
    nsresult nsres;

187
    if(!doc->nsdoc) {
188 189 190 191
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

192
    nsAString_InitDepend(&tag_str, tag);
193
    nsres = nsIDOMDocument_CreateElement(doc->nsdoc, &tag_str, &nselem);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    nsAString_Finish(&tag_str);
    if(NS_FAILED(nsres)) {
        ERR("CreateElement failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLElement, (void**)ret);
    nsIDOMElement_Release(nselem);
    if(NS_FAILED(nsres)) {
        ERR("Could not get nsIDOMHTMLElement iface: %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
}

210 211 212
static HRESULT WINAPI HTMLElement_QueryInterface(IHTMLElement *iface,
                                                 REFIID riid, void **ppv)
{
213
    HTMLElement *This = impl_from_IHTMLElement(iface);
214

215
    return IHTMLDOMNode_QueryInterface(&This->node.IHTMLDOMNode_iface, riid, ppv);
216 217 218 219
}

static ULONG WINAPI HTMLElement_AddRef(IHTMLElement *iface)
{
220
    HTMLElement *This = impl_from_IHTMLElement(iface);
221

222
    return IHTMLDOMNode_AddRef(&This->node.IHTMLDOMNode_iface);
223 224 225 226
}

static ULONG WINAPI HTMLElement_Release(IHTMLElement *iface)
{
227
    HTMLElement *This = impl_from_IHTMLElement(iface);
228

229
    return IHTMLDOMNode_Release(&This->node.IHTMLDOMNode_iface);
230 231 232 233
}

static HRESULT WINAPI HTMLElement_GetTypeInfoCount(IHTMLElement *iface, UINT *pctinfo)
{
234
    HTMLElement *This = impl_from_IHTMLElement(iface);
235
    return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
236 237 238 239 240
}

static HRESULT WINAPI HTMLElement_GetTypeInfo(IHTMLElement *iface, UINT iTInfo,
                                              LCID lcid, ITypeInfo **ppTInfo)
{
241
    HTMLElement *This = impl_from_IHTMLElement(iface);
242
    return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
243 244 245 246 247 248
}

static HRESULT WINAPI HTMLElement_GetIDsOfNames(IHTMLElement *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
249
    HTMLElement *This = impl_from_IHTMLElement(iface);
250 251
    return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
252 253 254 255 256 257
}

static HRESULT WINAPI HTMLElement_Invoke(IHTMLElement *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
258
    HTMLElement *This = impl_from_IHTMLElement(iface);
259
    return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
260
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
261 262 263 264 265
}

static HRESULT WINAPI HTMLElement_setAttribute(IHTMLElement *iface, BSTR strAttributeName,
                                               VARIANT AttributeValue, LONG lFlags)
{
266
    HTMLElement *This = impl_from_IHTMLElement(iface);
267
    HRESULT hres;
268 269 270
    DISPID dispid, dispidNamed = DISPID_PROPERTYPUT;
    DISPPARAMS dispParams;
    EXCEPINFO excep;
271

272
    TRACE("(%p)->(%s %s %08x)\n", This, debugstr_w(strAttributeName), debugstr_variant(&AttributeValue), lFlags);
273

274
    hres = IDispatchEx_GetDispID(&This->node.dispex.IDispatchEx_iface, strAttributeName,
275 276
            fdexNameCaseInsensitive | fdexNameEnsure, &dispid);
    if(FAILED(hres))
277 278
        return hres;

279 280 281 282
    dispParams.cArgs = 1;
    dispParams.cNamedArgs = 1;
    dispParams.rgdispidNamedArgs = &dispidNamed;
    dispParams.rgvarg = &AttributeValue;
283

284 285
    hres = IDispatchEx_InvokeEx(&This->node.dispex.IDispatchEx_iface, dispid,
            LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &dispParams, NULL, &excep, NULL);
286
    return hres;
287 288 289 290 291
}

static HRESULT WINAPI HTMLElement_getAttribute(IHTMLElement *iface, BSTR strAttributeName,
                                               LONG lFlags, VARIANT *AttributeValue)
{
292
    HTMLElement *This = impl_from_IHTMLElement(iface);
293 294 295 296
    DISPID dispid;
    HRESULT hres;
    DISPPARAMS dispParams = {NULL, NULL, 0, 0};
    EXCEPINFO excep;
297

298
    TRACE("(%p)->(%s %08x %p)\n", This, debugstr_w(strAttributeName), lFlags, AttributeValue);
299

300
    hres = IDispatchEx_GetDispID(&This->node.dispex.IDispatchEx_iface, strAttributeName,
301 302
            fdexNameCaseInsensitive, &dispid);
    if(hres == DISP_E_UNKNOWNNAME) {
303 304
        V_VT(AttributeValue) = VT_NULL;
        return S_OK;
305 306
    }

307 308 309
    if(FAILED(hres)) {
        V_VT(AttributeValue) = VT_NULL;
        return hres;
310 311
    }

312 313
    hres = IDispatchEx_InvokeEx(&This->node.dispex.IDispatchEx_iface, dispid, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYGET, &dispParams, AttributeValue, &excep, NULL);
314 315

    return hres;
316 317 318 319 320
}

static HRESULT WINAPI HTMLElement_removeAttribute(IHTMLElement *iface, BSTR strAttributeName,
                                                  LONG lFlags, VARIANT_BOOL *pfSuccess)
{
321
    HTMLElement *This = impl_from_IHTMLElement(iface);
322 323 324 325

    TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(strAttributeName), lFlags, pfSuccess);

    return remove_prop(&This->node.dispex, strAttributeName, pfSuccess);
326 327 328 329
}

static HRESULT WINAPI HTMLElement_put_className(IHTMLElement *iface, BSTR v)
{
330
    HTMLElement *This = impl_from_IHTMLElement(iface);
331 332 333 334 335 336 337 338 339 340
    nsAString classname_str;
    nsresult nsres;

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

    if(!This->nselem) {
        FIXME("NULL nselem\n");
        return E_NOTIMPL;
    }

341
    nsAString_InitDepend(&classname_str, v);
342 343 344 345 346 347
    nsres = nsIDOMHTMLElement_SetClassName(This->nselem, &classname_str);
    nsAString_Finish(&classname_str);
    if(NS_FAILED(nsres))
        ERR("SetClassName failed: %08x\n", nsres);

    return S_OK;
348 349 350 351
}

static HRESULT WINAPI HTMLElement_get_className(IHTMLElement *iface, BSTR *p)
{
352
    HTMLElement *This = impl_from_IHTMLElement(iface);
353 354 355 356 357
    nsAString class_str;
    nsresult nsres;

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

358 359 360 361 362
    if(!This->nselem) {
        FIXME("NULL nselem\n");
        return E_NOTIMPL;
    }

363 364
    nsAString_Init(&class_str, NULL);
    nsres = nsIDOMHTMLElement_GetClassName(This->nselem, &class_str);
365
    return return_nsstr(nsres, &class_str, p);
366 367 368 369
}

static HRESULT WINAPI HTMLElement_put_id(IHTMLElement *iface, BSTR v)
{
370
    HTMLElement *This = impl_from_IHTMLElement(iface);
371 372 373 374 375 376 377 378 379 380
    nsAString id_str;
    nsresult nsres;

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

    if(!This->nselem) {
        FIXME("nselem == NULL\n");
        return S_OK;
    }

381
    nsAString_InitDepend(&id_str, v);
382 383 384 385 386 387
    nsres = nsIDOMHTMLElement_SetId(This->nselem, &id_str);
    nsAString_Finish(&id_str);
    if(NS_FAILED(nsres))
        ERR("SetId failed: %08x\n", nsres);

    return S_OK;
388 389 390 391
}

static HRESULT WINAPI HTMLElement_get_id(IHTMLElement *iface, BSTR *p)
{
392
    HTMLElement *This = impl_from_IHTMLElement(iface);
393 394 395 396 397
    nsAString id_str;
    nsresult nsres;

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

398 399
    if(!This->nselem) {
        *p = NULL;
400
        return S_OK;
401
    }
402 403 404

    nsAString_Init(&id_str, NULL);
    nsres = nsIDOMHTMLElement_GetId(This->nselem, &id_str);
405
    return return_nsstr(nsres, &id_str, p);
406 407 408 409
}

static HRESULT WINAPI HTMLElement_get_tagName(IHTMLElement *iface, BSTR *p)
{
410
    HTMLElement *This = impl_from_IHTMLElement(iface);
411 412 413 414 415
    nsAString tag_str;
    nsresult nsres;

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

416 417 418 419 420 421
    if(!This->nselem) {
        static const WCHAR comment_tagW[] = {'!',0};

        WARN("NULL nselem, assuming comment\n");

        *p = SysAllocString(comment_tagW);
422
        return *p ? S_OK : E_OUTOFMEMORY;
423 424
    }

425 426
    nsAString_Init(&tag_str, NULL);
    nsres = nsIDOMHTMLElement_GetTagName(This->nselem, &tag_str);
427
    return return_nsstr(nsres, &tag_str, p);
428 429 430 431
}

static HRESULT WINAPI HTMLElement_get_parentElement(IHTMLElement *iface, IHTMLElement **p)
{
432
    HTMLElement *This = impl_from_IHTMLElement(iface);
433 434 435 436 437
    IHTMLDOMNode *node;
    HRESULT hres;

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

438
    hres = IHTMLDOMNode_get_parentNode(&This->node.IHTMLDOMNode_iface, &node);
439 440 441 442 443 444 445 446 447
    if(FAILED(hres))
        return hres;

    hres = IHTMLDOMNode_QueryInterface(node, &IID_IHTMLElement, (void**)p);
    IHTMLDOMNode_Release(node);
    if(FAILED(hres))
        *p = NULL;

    return S_OK;
448 449 450 451
}

static HRESULT WINAPI HTMLElement_get_style(IHTMLElement *iface, IHTMLStyle **p)
{
452
    HTMLElement *This = impl_from_IHTMLElement(iface);
453 454 455

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

456 457 458 459 460
    if(!This->style) {
        nsIDOMElementCSSInlineStyle *nselemstyle;
        nsIDOMCSSStyleDeclaration *nsstyle;
        nsresult nsres;
        HRESULT hres;
461

462 463 464 465
        if(!This->nselem) {
            FIXME("NULL nselem\n");
            return E_NOTIMPL;
        }
466

467 468 469
        nsres = nsIDOMHTMLElement_QueryInterface(This->nselem, &IID_nsIDOMElementCSSInlineStyle,
                (void**)&nselemstyle);
        if(NS_FAILED(nsres)) {
470
            ERR("Could not get nsIDOMCSSStyleDeclaration interface: %08x\n", nsres);
471 472
            return E_FAIL;
        }
473

474 475 476 477 478 479 480
        nsres = nsIDOMElementCSSInlineStyle_GetStyle(nselemstyle, &nsstyle);
        nsIDOMElementCSSInlineStyle_Release(nselemstyle);
        if(NS_FAILED(nsres)) {
            ERR("GetStyle failed: %08x\n", nsres);
            return E_FAIL;
        }

481
        hres = HTMLStyle_Create(This, nsstyle, &This->style);
482 483 484 485
        nsIDOMCSSStyleDeclaration_Release(nsstyle);
        if(FAILED(hres))
            return hres;
    }
486

487 488
    *p = &This->style->IHTMLStyle_iface;
    IHTMLStyle_AddRef(*p);
489
    return S_OK;
490 491 492 493
}

static HRESULT WINAPI HTMLElement_put_onhelp(IHTMLElement *iface, VARIANT v)
{
494
    HTMLElement *This = impl_from_IHTMLElement(iface);
495
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
496 497 498 499 500
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onhelp(IHTMLElement *iface, VARIANT *p)
{
501
    HTMLElement *This = impl_from_IHTMLElement(iface);
502 503 504 505 506 507
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onclick(IHTMLElement *iface, VARIANT v)
{
508
    HTMLElement *This = impl_from_IHTMLElement(iface);
509

510
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
511 512

    return set_node_event(&This->node, EVENTID_CLICK, &v);
513 514 515 516
}

static HRESULT WINAPI HTMLElement_get_onclick(IHTMLElement *iface, VARIANT *p)
{
517
    HTMLElement *This = impl_from_IHTMLElement(iface);
518 519 520 521

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

    return get_node_event(&This->node, EVENTID_CLICK, p);
522 523 524 525
}

static HRESULT WINAPI HTMLElement_put_ondblclick(IHTMLElement *iface, VARIANT v)
{
526
    HTMLElement *This = impl_from_IHTMLElement(iface);
527 528 529 530

    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_node_event(&This->node, EVENTID_DBLCLICK, &v);
531 532 533 534
}

static HRESULT WINAPI HTMLElement_get_ondblclick(IHTMLElement *iface, VARIANT *p)
{
535
    HTMLElement *This = impl_from_IHTMLElement(iface);
536 537 538 539

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

    return get_node_event(&This->node, EVENTID_DBLCLICK, p);
540 541 542 543
}

static HRESULT WINAPI HTMLElement_put_onkeydown(IHTMLElement *iface, VARIANT v)
{
544
    HTMLElement *This = impl_from_IHTMLElement(iface);
545 546 547 548

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_node_event(&This->node, EVENTID_KEYDOWN, &v);
549 550 551 552
}

static HRESULT WINAPI HTMLElement_get_onkeydown(IHTMLElement *iface, VARIANT *p)
{
553
    HTMLElement *This = impl_from_IHTMLElement(iface);
554 555 556 557

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

    return get_node_event(&This->node, EVENTID_KEYDOWN, p);
558 559 560 561
}

static HRESULT WINAPI HTMLElement_put_onkeyup(IHTMLElement *iface, VARIANT v)
{
562
    HTMLElement *This = impl_from_IHTMLElement(iface);
563

564
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
565 566

    return set_node_event(&This->node, EVENTID_KEYUP, &v);
567 568 569 570
}

static HRESULT WINAPI HTMLElement_get_onkeyup(IHTMLElement *iface, VARIANT *p)
{
571
    HTMLElement *This = impl_from_IHTMLElement(iface);
572 573 574 575 576 577
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onkeypress(IHTMLElement *iface, VARIANT v)
{
578
    HTMLElement *This = impl_from_IHTMLElement(iface);
579
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
580 581 582 583 584
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onkeypress(IHTMLElement *iface, VARIANT *p)
{
585
    HTMLElement *This = impl_from_IHTMLElement(iface);
586 587 588 589 590 591
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onmouseout(IHTMLElement *iface, VARIANT v)
{
592
    HTMLElement *This = impl_from_IHTMLElement(iface);
593 594 595 596

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_node_event(&This->node, EVENTID_MOUSEOUT, &v);
597 598 599 600
}

static HRESULT WINAPI HTMLElement_get_onmouseout(IHTMLElement *iface, VARIANT *p)
{
601
    HTMLElement *This = impl_from_IHTMLElement(iface);
602 603 604 605

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

    return get_node_event(&This->node, EVENTID_MOUSEOUT, p);
606 607 608 609
}

static HRESULT WINAPI HTMLElement_put_onmouseover(IHTMLElement *iface, VARIANT v)
{
610
    HTMLElement *This = impl_from_IHTMLElement(iface);
611

612
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
613 614

    return set_node_event(&This->node, EVENTID_MOUSEOVER, &v);
615 616 617 618
}

static HRESULT WINAPI HTMLElement_get_onmouseover(IHTMLElement *iface, VARIANT *p)
{
619
    HTMLElement *This = impl_from_IHTMLElement(iface);
620 621 622 623

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

    return get_node_event(&This->node, EVENTID_MOUSEOVER, p);
624 625 626 627
}

static HRESULT WINAPI HTMLElement_put_onmousemove(IHTMLElement *iface, VARIANT v)
{
628
    HTMLElement *This = impl_from_IHTMLElement(iface);
629

630
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
631 632

    return set_node_event(&This->node, EVENTID_MOUSEMOVE, &v);
633 634 635 636
}

static HRESULT WINAPI HTMLElement_get_onmousemove(IHTMLElement *iface, VARIANT *p)
{
637
    HTMLElement *This = impl_from_IHTMLElement(iface);
638 639 640 641

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

    return get_node_event(&This->node, EVENTID_MOUSEMOVE, p);
642 643 644 645
}

static HRESULT WINAPI HTMLElement_put_onmousedown(IHTMLElement *iface, VARIANT v)
{
646
    HTMLElement *This = impl_from_IHTMLElement(iface);
647

648
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
649 650

    return set_node_event(&This->node, EVENTID_MOUSEDOWN, &v);
651 652 653 654
}

static HRESULT WINAPI HTMLElement_get_onmousedown(IHTMLElement *iface, VARIANT *p)
{
655
    HTMLElement *This = impl_from_IHTMLElement(iface);
656 657 658 659

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

    return get_node_event(&This->node, EVENTID_MOUSEDOWN, p);
660 661 662 663
}

static HRESULT WINAPI HTMLElement_put_onmouseup(IHTMLElement *iface, VARIANT v)
{
664
    HTMLElement *This = impl_from_IHTMLElement(iface);
665 666 667 668

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_node_event(&This->node, EVENTID_MOUSEUP, &v);
669 670 671 672
}

static HRESULT WINAPI HTMLElement_get_onmouseup(IHTMLElement *iface, VARIANT *p)
{
673
    HTMLElement *This = impl_from_IHTMLElement(iface);
674 675 676 677

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

    return get_node_event(&This->node, EVENTID_MOUSEUP, p);
678 679 680 681
}

static HRESULT WINAPI HTMLElement_get_document(IHTMLElement *iface, IDispatch **p)
{
682
    HTMLElement *This = impl_from_IHTMLElement(iface);
683 684 685 686 687 688

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

    if(!p)
        return E_POINTER;

689 690 691
    if(This->node.vtbl->get_document)
        return This->node.vtbl->get_document(&This->node, p);

692
    *p = (IDispatch*)&This->node.doc->basedoc.IHTMLDocument2_iface;
693 694
    IDispatch_AddRef(*p);
    return S_OK;
695 696
}

697 698
static const WCHAR titleW[] = {'t','i','t','l','e',0};

699 700
static HRESULT WINAPI HTMLElement_put_title(IHTMLElement *iface, BSTR v)
{
701
    HTMLElement *This = impl_from_IHTMLElement(iface);
702 703 704 705 706
    nsAString title_str;
    nsresult nsres;

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

707 708 709 710 711 712 713 714 715 716 717 718 719 720
    if(!This->nselem) {
        VARIANT *var;
        HRESULT hres;

        hres = dispex_get_dprop_ref(&This->node.dispex, titleW, TRUE, &var);
        if(FAILED(hres))
            return hres;

        VariantClear(var);
        V_VT(var) = VT_BSTR;
        V_BSTR(var) = v ? SysAllocString(v) : NULL;
        return S_OK;
    }

721
    nsAString_InitDepend(&title_str, v);
722 723 724 725 726 727
    nsres = nsIDOMHTMLElement_SetTitle(This->nselem, &title_str);
    nsAString_Finish(&title_str);
    if(NS_FAILED(nsres))
        ERR("SetTitle failed: %08x\n", nsres);

    return S_OK;
728 729 730 731
}

static HRESULT WINAPI HTMLElement_get_title(IHTMLElement *iface, BSTR *p)
{
732
    HTMLElement *This = impl_from_IHTMLElement(iface);
733 734 735 736 737
    nsAString title_str;
    nsresult nsres;

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

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    if(!This->nselem) {
        VARIANT *var;
        HRESULT hres;

        hres = dispex_get_dprop_ref(&This->node.dispex, titleW, FALSE, &var);
        if(hres == DISP_E_UNKNOWNNAME) {
            *p = NULL;
        }else if(V_VT(var) != VT_BSTR) {
            FIXME("title = %s\n", debugstr_variant(var));
            return E_FAIL;
        }else {
            *p = V_BSTR(var) ? SysAllocString(V_BSTR(var)) : NULL;
        }

        return S_OK;
    }

755 756
    nsAString_Init(&title_str, NULL);
    nsres = nsIDOMHTMLElement_GetTitle(This->nselem, &title_str);
757
    return return_nsstr(nsres, &title_str, p);
758 759 760 761
}

static HRESULT WINAPI HTMLElement_put_language(IHTMLElement *iface, BSTR v)
{
762
    HTMLElement *This = impl_from_IHTMLElement(iface);
763 764 765 766 767 768
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_language(IHTMLElement *iface, BSTR *p)
{
769
    HTMLElement *This = impl_from_IHTMLElement(iface);
770 771 772 773 774 775
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onselectstart(IHTMLElement *iface, VARIANT v)
{
776
    HTMLElement *This = impl_from_IHTMLElement(iface);
777 778 779 780

    TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));

    return set_node_event(&This->node, EVENTID_SELECTSTART, &v);
781 782 783 784
}

static HRESULT WINAPI HTMLElement_get_onselectstart(IHTMLElement *iface, VARIANT *p)
{
785
    HTMLElement *This = impl_from_IHTMLElement(iface);
786 787 788 789

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

    return get_node_event(&This->node, EVENTID_SELECTSTART, p);
790 791 792 793
}

static HRESULT WINAPI HTMLElement_scrollIntoView(IHTMLElement *iface, VARIANT varargStart)
{
794
    HTMLElement *This = impl_from_IHTMLElement(iface);
795
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&varargStart));
796 797 798 799 800 801
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_contains(IHTMLElement *iface, IHTMLElement *pChild,
                                           VARIANT_BOOL *pfResult)
{
802
    HTMLElement *This = impl_from_IHTMLElement(iface);
803 804 805 806
    FIXME("(%p)->(%p %p)\n", This, pChild, pfResult);
    return E_NOTIMPL;
}

807
static HRESULT WINAPI HTMLElement_get_sourceIndex(IHTMLElement *iface, LONG *p)
808
{
809
    HTMLElement *This = impl_from_IHTMLElement(iface);
810 811 812 813 814 815
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_recordNumber(IHTMLElement *iface, VARIANT *p)
{
816
    HTMLElement *This = impl_from_IHTMLElement(iface);
817 818 819 820 821 822
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_lang(IHTMLElement *iface, BSTR v)
{
823
    HTMLElement *This = impl_from_IHTMLElement(iface);
824 825 826 827 828 829
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_lang(IHTMLElement *iface, BSTR *p)
{
830
    HTMLElement *This = impl_from_IHTMLElement(iface);
831 832 833 834
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

835
static HRESULT WINAPI HTMLElement_get_offsetLeft(IHTMLElement *iface, LONG *p)
836
{
837
    HTMLElement *This = impl_from_IHTMLElement(iface);
838 839 840 841 842
    PRInt32 off_left = 0;
    nsresult nsres;

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

843
    nsres = nsIDOMHTMLElement_GetOffsetLeft(This->nselem, &off_left);
844 845 846 847 848 849 850
    if(NS_FAILED(nsres)) {
        ERR("GetOffsetLeft failed: %08x\n", nsres);
        return E_FAIL;
    }

    *p = off_left;
    return S_OK;
851 852
}

853
static HRESULT WINAPI HTMLElement_get_offsetTop(IHTMLElement *iface, LONG *p)
854
{
855
    HTMLElement *This = impl_from_IHTMLElement(iface);
856 857 858 859 860
    PRInt32 top = 0;
    nsresult nsres;

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

861
    nsres = nsIDOMHTMLElement_GetOffsetTop(This->nselem, &top);
862 863 864 865 866 867 868
    if(NS_FAILED(nsres)) {
        ERR("GetOffsetTop failed: %08x\n", nsres);
        return E_FAIL;
    }

    *p = top;
    return S_OK;
869 870
}

871
static HRESULT WINAPI HTMLElement_get_offsetWidth(IHTMLElement *iface, LONG *p)
872
{
873
    HTMLElement *This = impl_from_IHTMLElement(iface);
874 875 876 877 878
    PRInt32 offset = 0;
    nsresult nsres;

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

879
    nsres = nsIDOMHTMLElement_GetOffsetWidth(This->nselem, &offset);
880 881 882 883 884 885 886
    if(NS_FAILED(nsres)) {
        ERR("GetOffsetWidth failed: %08x\n", nsres);
        return E_FAIL;
    }

    *p = offset;
    return S_OK;
887 888
}

889
static HRESULT WINAPI HTMLElement_get_offsetHeight(IHTMLElement *iface, LONG *p)
890
{
891
    HTMLElement *This = impl_from_IHTMLElement(iface);
892 893 894 895 896
    PRInt32 offset = 0;
    nsresult nsres;

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

897
    nsres = nsIDOMHTMLElement_GetOffsetHeight(This->nselem, &offset);
898 899 900 901 902 903 904
    if(NS_FAILED(nsres)) {
        ERR("GetOffsetHeight failed: %08x\n", nsres);
        return E_FAIL;
    }

    *p = offset;
    return S_OK;
905 906 907 908
}

static HRESULT WINAPI HTMLElement_get_offsetParent(IHTMLElement *iface, IHTMLElement **p)
{
909
    HTMLElement *This = impl_from_IHTMLElement(iface);
910 911 912 913 914 915
    nsIDOMElement *nsparent;
    nsresult nsres;
    HRESULT hres;

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

916
    nsres = nsIDOMHTMLElement_GetOffsetParent(This->nselem, &nsparent);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
    if(NS_FAILED(nsres)) {
        ERR("GetOffsetParent failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nsparent) {
        HTMLDOMNode *node;

        hres = get_node(This->node.doc, (nsIDOMNode*)nsparent, TRUE, &node);
        nsIDOMElement_Release(nsparent);
        if(FAILED(hres))
            return hres;

        hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
    }else {
        *p = NULL;
        hres = S_OK;
    }

    return hres;
937 938 939 940
}

static HRESULT WINAPI HTMLElement_put_innerHTML(IHTMLElement *iface, BSTR v)
{
941
    HTMLElement *This = impl_from_IHTMLElement(iface);
942 943 944 945 946
    nsAString html_str;
    nsresult nsres;

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

947 948 949 950 951
    if(!This->nselem) {
        FIXME("NULL nselem\n");
        return E_NOTIMPL;
    }

952
    nsAString_InitDepend(&html_str, v);
953
    nsres = nsIDOMHTMLElement_SetInnerHTML(This->nselem, &html_str);
954 955 956 957 958 959 960
    nsAString_Finish(&html_str);
    if(NS_FAILED(nsres)) {
        FIXME("SetInnerHtml failed %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
961 962 963 964
}

static HRESULT WINAPI HTMLElement_get_innerHTML(IHTMLElement *iface, BSTR *p)
{
965
    HTMLElement *This = impl_from_IHTMLElement(iface);
966 967 968 969 970 971 972 973 974 975 976
    nsAString html_str;
    nsresult nsres;

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

    if(!This->nselem) {
        FIXME("NULL nselem\n");
        return E_NOTIMPL;
    }

    nsAString_Init(&html_str, NULL);
977
    nsres = nsIDOMHTMLElement_GetInnerHTML(This->nselem, &html_str);
978
    return return_nsstr(nsres, &html_str, p);
979 980 981 982
}

static HRESULT WINAPI HTMLElement_put_innerText(IHTMLElement *iface, BSTR v)
{
983
    HTMLElement *This = impl_from_IHTMLElement(iface);
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    nsIDOMNode *nschild, *tmp;
    nsIDOMText *text_node;
    nsAString text_str;
    nsresult nsres;

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

    while(1) {
        nsres = nsIDOMHTMLElement_GetLastChild(This->nselem, &nschild);
        if(NS_FAILED(nsres)) {
            ERR("GetLastChild failed: %08x\n", nsres);
            return E_FAIL;
        }
        if(!nschild)
            break;

        nsres = nsIDOMHTMLElement_RemoveChild(This->nselem, nschild, &tmp);
        nsIDOMNode_Release(nschild);
        if(NS_FAILED(nsres)) {
            ERR("RemoveChild failed: %08x\n", nsres);
            return E_FAIL;
        }
        nsIDOMNode_Release(tmp);
    }

1009
    nsAString_InitDepend(&text_str, v);
1010
    nsres = nsIDOMHTMLDocument_CreateTextNode(This->node.doc->nsdoc, &text_str, &text_node);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    nsAString_Finish(&text_str);
    if(NS_FAILED(nsres)) {
        ERR("CreateTextNode failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMHTMLElement_AppendChild(This->nselem, (nsIDOMNode*)text_node, &tmp);
    if(NS_FAILED(nsres)) {
        ERR("AppendChild failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsIDOMNode_Release(tmp);
    return S_OK;
1025 1026 1027 1028
}

static HRESULT WINAPI HTMLElement_get_innerText(IHTMLElement *iface, BSTR *p)
{
1029
    HTMLElement *This = impl_from_IHTMLElement(iface);
1030 1031 1032 1033

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

    return get_node_text(&This->node, p);
1034 1035 1036 1037
}

static HRESULT WINAPI HTMLElement_put_outerHTML(IHTMLElement *iface, BSTR v)
{
1038
    HTMLElement *This = impl_from_IHTMLElement(iface);
1039 1040 1041

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

1042
    return replace_node_by_html(This->node.doc->nsdoc, This->node.nsnode, v);
1043 1044 1045 1046
}

static HRESULT WINAPI HTMLElement_get_outerHTML(IHTMLElement *iface, BSTR *p)
{
1047
    HTMLElement *This = impl_from_IHTMLElement(iface);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
    nsAString html_str;
    HRESULT hres;

    WARN("(%p)->(%p) semi-stub\n", This, p);

    nsAString_Init(&html_str, NULL);
    hres = nsnode_to_nsstring(This->node.nsnode, &html_str);
    if(SUCCEEDED(hres)) {
        const PRUnichar *html;

        nsAString_GetData(&html_str, &html);
        *p = SysAllocString(html);
        if(!*p)
            hres = E_OUTOFMEMORY;
    }

    nsAString_Finish(&html_str);

    TRACE("ret %s\n", debugstr_w(*p));
1067
    return hres;
1068 1069 1070 1071
}

static HRESULT WINAPI HTMLElement_put_outerText(IHTMLElement *iface, BSTR v)
{
1072
    HTMLElement *This = impl_from_IHTMLElement(iface);
1073 1074 1075 1076 1077 1078
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_outerText(IHTMLElement *iface, BSTR *p)
{
1079
    HTMLElement *This = impl_from_IHTMLElement(iface);
1080 1081 1082 1083
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
static HRESULT HTMLElement_InsertAdjacentNode(HTMLElement *This, BSTR where, nsIDOMNode *nsnode)
{
    static const WCHAR wszBeforeBegin[] = {'b','e','f','o','r','e','B','e','g','i','n',0};
    static const WCHAR wszAfterBegin[] = {'a','f','t','e','r','B','e','g','i','n',0};
    static const WCHAR wszBeforeEnd[] = {'b','e','f','o','r','e','E','n','d',0};
    static const WCHAR wszAfterEnd[] = {'a','f','t','e','r','E','n','d',0};
    nsresult nsres;

    if (!strcmpiW(where, wszBeforeBegin))
    {
        nsIDOMNode *unused;
        nsIDOMNode *parent;
1096
        nsres = nsIDOMNode_GetParentNode(This->node.nsnode, &parent);
1097
        if (!parent) return E_INVALIDARG;
1098
        nsres = nsIDOMNode_InsertBefore(parent, nsnode, This->node.nsnode, &unused);
1099 1100 1101 1102 1103 1104 1105
        if (unused) nsIDOMNode_Release(unused);
        nsIDOMNode_Release(parent);
    }
    else if (!strcmpiW(where, wszAfterBegin))
    {
        nsIDOMNode *unused;
        nsIDOMNode *first_child;
1106 1107
        nsIDOMNode_GetFirstChild(This->node.nsnode, &first_child);
        nsres = nsIDOMNode_InsertBefore(This->node.nsnode, nsnode, first_child, &unused);
1108 1109 1110 1111 1112 1113
        if (unused) nsIDOMNode_Release(unused);
        if (first_child) nsIDOMNode_Release(first_child);
    }
    else if (!strcmpiW(where, wszBeforeEnd))
    {
        nsIDOMNode *unused;
1114
        nsres = nsIDOMNode_AppendChild(This->node.nsnode, nsnode, &unused);
1115 1116 1117 1118 1119 1120 1121
        if (unused) nsIDOMNode_Release(unused);
    }
    else if (!strcmpiW(where, wszAfterEnd))
    {
        nsIDOMNode *unused;
        nsIDOMNode *next_sibling;
        nsIDOMNode *parent;
1122
        nsIDOMNode_GetParentNode(This->node.nsnode, &parent);
1123 1124
        if (!parent) return E_INVALIDARG;

1125
        nsIDOMNode_GetNextSibling(This->node.nsnode, &next_sibling);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
        if (next_sibling)
        {
            nsres = nsIDOMNode_InsertBefore(parent, nsnode, next_sibling, &unused);
            nsIDOMNode_Release(next_sibling);
        }
        else
            nsres = nsIDOMNode_AppendChild(parent, nsnode, &unused);
        nsIDOMNode_Release(parent);
        if (unused) nsIDOMNode_Release(unused);
    }
    else
    {
        ERR("invalid where: %s\n", debugstr_w(where));
        return E_INVALIDARG;
    }

    if (NS_FAILED(nsres))
        return E_FAIL;
    else
        return S_OK;
}

1148 1149 1150
static HRESULT WINAPI HTMLElement_insertAdjacentHTML(IHTMLElement *iface, BSTR where,
                                                     BSTR html)
{
1151
    HTMLElement *This = impl_from_IHTMLElement(iface);
1152 1153 1154 1155
    nsIDOMRange *range;
    nsIDOMNSRange *nsrange;
    nsIDOMNode *nsnode;
    nsAString ns_html;
1156
    nsresult nsres;
1157 1158 1159 1160
    HRESULT hr;

    TRACE("(%p)->(%s %s)\n", This, debugstr_w(where), debugstr_w(html));

1161
    if(!This->node.doc->nsdoc) {
1162 1163
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
1164 1165
    }

1166
    nsres = nsIDOMHTMLDocument_CreateRange(This->node.doc->nsdoc, &range);
1167 1168 1169 1170 1171 1172
    if(NS_FAILED(nsres))
    {
        ERR("CreateRange failed: %08x\n", nsres);
        return E_FAIL;
    }

1173
    nsIDOMRange_SetStartBefore(range, This->node.nsnode);
1174 1175 1176 1177 1178 1179 1180 1181 1182

    nsIDOMRange_QueryInterface(range, &IID_nsIDOMNSRange, (void **)&nsrange);
    nsIDOMRange_Release(range);
    if(NS_FAILED(nsres))
    {
        ERR("getting nsIDOMNSRange failed: %08x\n", nsres);
        return E_FAIL;
    }

1183
    nsAString_InitDepend(&ns_html, html);
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

    nsres = nsIDOMNSRange_CreateContextualFragment(nsrange, &ns_html, (nsIDOMDocumentFragment **)&nsnode);
    nsIDOMNSRange_Release(nsrange);
    nsAString_Finish(&ns_html);

    if(NS_FAILED(nsres) || !nsnode)
    {
        ERR("CreateTextNode failed: %08x\n", nsres);
        return E_FAIL;
    }

    hr = HTMLElement_InsertAdjacentNode(This, where, nsnode);
    nsIDOMNode_Release(nsnode);

    return hr;
1199 1200 1201 1202 1203
}

static HRESULT WINAPI HTMLElement_insertAdjacentText(IHTMLElement *iface, BSTR where,
                                                     BSTR text)
{
1204
    HTMLElement *This = impl_from_IHTMLElement(iface);
1205 1206
    nsIDOMNode *nsnode;
    nsAString ns_text;
1207
    nsresult nsres;
1208 1209 1210 1211
    HRESULT hr;

    TRACE("(%p)->(%s %s)\n", This, debugstr_w(where), debugstr_w(text));

1212
    if(!This->node.doc->nsdoc) {
1213 1214
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
1215 1216 1217
    }


1218
    nsAString_InitDepend(&ns_text, text);
1219
    nsres = nsIDOMDocument_CreateTextNode(This->node.doc->nsdoc, &ns_text, (nsIDOMText **)&nsnode);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    nsAString_Finish(&ns_text);

    if(NS_FAILED(nsres) || !nsnode)
    {
        ERR("CreateTextNode failed: %08x\n", nsres);
        return E_FAIL;
    }

    hr = HTMLElement_InsertAdjacentNode(This, where, nsnode);
    nsIDOMNode_Release(nsnode);

    return hr;
1232 1233 1234 1235
}

static HRESULT WINAPI HTMLElement_get_parentTextEdit(IHTMLElement *iface, IHTMLElement **p)
{
1236
    HTMLElement *This = impl_from_IHTMLElement(iface);
1237 1238 1239 1240 1241 1242
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_isTextEdit(IHTMLElement *iface, VARIANT_BOOL *p)
{
1243
    HTMLElement *This = impl_from_IHTMLElement(iface);
1244 1245 1246 1247 1248 1249
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_click(IHTMLElement *iface)
{
1250
    HTMLElement *This = impl_from_IHTMLElement(iface);
1251 1252 1253

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

1254
    return call_fire_event(&This->node, EVENTID_CLICK);
1255 1256 1257 1258 1259
}

static HRESULT WINAPI HTMLElement_get_filters(IHTMLElement *iface,
                                              IHTMLFiltersCollection **p)
{
1260
    HTMLElement *This = impl_from_IHTMLElement(iface);
1261 1262 1263 1264 1265 1266 1267 1268
    TRACE("(%p)->(%p)\n", This, p);

    if(!p)
        return E_POINTER;

    *p = HTMLFiltersCollection_Create();

    return S_OK;
1269 1270 1271 1272
}

static HRESULT WINAPI HTMLElement_put_ondragstart(IHTMLElement *iface, VARIANT v)
{
1273
    HTMLElement *This = impl_from_IHTMLElement(iface);
1274
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1275 1276 1277 1278 1279
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_ondragstart(IHTMLElement *iface, VARIANT *p)
{
1280
    HTMLElement *This = impl_from_IHTMLElement(iface);
1281 1282 1283 1284 1285 1286
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_toString(IHTMLElement *iface, BSTR *String)
{
1287
    HTMLElement *This = impl_from_IHTMLElement(iface);
1288 1289 1290 1291 1292 1293
    FIXME("(%p)->(%p)\n", This, String);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onbeforeupdate(IHTMLElement *iface, VARIANT v)
{
1294
    HTMLElement *This = impl_from_IHTMLElement(iface);
1295
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1296 1297 1298 1299 1300
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onbeforeupdate(IHTMLElement *iface, VARIANT *p)
{
1301
    HTMLElement *This = impl_from_IHTMLElement(iface);
1302 1303 1304 1305 1306 1307
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onafterupdate(IHTMLElement *iface, VARIANT v)
{
1308
    HTMLElement *This = impl_from_IHTMLElement(iface);
1309
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1310 1311 1312 1313 1314
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onafterupdate(IHTMLElement *iface, VARIANT *p)
{
1315
    HTMLElement *This = impl_from_IHTMLElement(iface);
1316 1317 1318 1319 1320 1321
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onerrorupdate(IHTMLElement *iface, VARIANT v)
{
1322
    HTMLElement *This = impl_from_IHTMLElement(iface);
1323
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1324 1325 1326 1327 1328
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onerrorupdate(IHTMLElement *iface, VARIANT *p)
{
1329
    HTMLElement *This = impl_from_IHTMLElement(iface);
1330 1331 1332 1333 1334 1335
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onrowexit(IHTMLElement *iface, VARIANT v)
{
1336
    HTMLElement *This = impl_from_IHTMLElement(iface);
1337
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1338 1339 1340 1341 1342
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onrowexit(IHTMLElement *iface, VARIANT *p)
{
1343
    HTMLElement *This = impl_from_IHTMLElement(iface);
1344 1345 1346 1347 1348 1349
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onrowenter(IHTMLElement *iface, VARIANT v)
{
1350
    HTMLElement *This = impl_from_IHTMLElement(iface);
1351
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1352 1353 1354 1355 1356
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onrowenter(IHTMLElement *iface, VARIANT *p)
{
1357
    HTMLElement *This = impl_from_IHTMLElement(iface);
1358 1359 1360 1361 1362 1363
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_ondatasetchanged(IHTMLElement *iface, VARIANT v)
{
1364
    HTMLElement *This = impl_from_IHTMLElement(iface);
1365
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1366 1367 1368 1369 1370
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_ondatasetchanged(IHTMLElement *iface, VARIANT *p)
{
1371
    HTMLElement *This = impl_from_IHTMLElement(iface);
1372 1373 1374 1375 1376 1377
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_ondataavailable(IHTMLElement *iface, VARIANT v)
{
1378
    HTMLElement *This = impl_from_IHTMLElement(iface);
1379
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1380 1381 1382 1383 1384
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_ondataavailable(IHTMLElement *iface, VARIANT *p)
{
1385
    HTMLElement *This = impl_from_IHTMLElement(iface);
1386 1387 1388 1389 1390 1391
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_ondatasetcomplete(IHTMLElement *iface, VARIANT v)
{
1392
    HTMLElement *This = impl_from_IHTMLElement(iface);
1393
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1394 1395 1396 1397 1398
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_ondatasetcomplete(IHTMLElement *iface, VARIANT *p)
{
1399
    HTMLElement *This = impl_from_IHTMLElement(iface);
1400 1401 1402 1403 1404 1405
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_put_onfilterchange(IHTMLElement *iface, VARIANT v)
{
1406
    HTMLElement *This = impl_from_IHTMLElement(iface);
1407
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1408 1409 1410 1411 1412
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLElement_get_onfilterchange(IHTMLElement *iface, VARIANT *p)
{
1413
    HTMLElement *This = impl_from_IHTMLElement(iface);
1414 1415 1416 1417
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

1418
static HRESULT WINAPI HTMLElement_get_children(IHTMLElement *iface, IDispatch **p)
1419
{
1420
    HTMLElement *This = impl_from_IHTMLElement(iface);
1421 1422 1423
    nsIDOMNodeList *nsnode_list;
    nsresult nsres;

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

    nsres = nsIDOMNode_GetChildNodes(This->node.nsnode, &nsnode_list);
1427 1428
    if(NS_FAILED(nsres)) {
        ERR("GetChildNodes failed: %08x\n", nsres);
1429
        return E_FAIL;
1430 1431
    }

1432 1433
    *p = (IDispatch*)create_collection_from_nodelist(This->node.doc,
            (IUnknown*)&This->IHTMLElement_iface, nsnode_list);
1434

1435
    nsIDOMNodeList_Release(nsnode_list);
1436
    return S_OK;
1437 1438 1439 1440
}

static HRESULT WINAPI HTMLElement_get_all(IHTMLElement *iface, IDispatch **p)
{
1441
    HTMLElement *This = impl_from_IHTMLElement(iface);
1442 1443 1444

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

1445
    *p = (IDispatch*)create_all_collection(&This->node, FALSE);
1446
    return S_OK;
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
}

static const IHTMLElementVtbl HTMLElementVtbl = {
    HTMLElement_QueryInterface,
    HTMLElement_AddRef,
    HTMLElement_Release,
    HTMLElement_GetTypeInfoCount,
    HTMLElement_GetTypeInfo,
    HTMLElement_GetIDsOfNames,
    HTMLElement_Invoke,
    HTMLElement_setAttribute,
    HTMLElement_getAttribute,
    HTMLElement_removeAttribute,
    HTMLElement_put_className,
    HTMLElement_get_className,
    HTMLElement_put_id,
    HTMLElement_get_id,
    HTMLElement_get_tagName,
    HTMLElement_get_parentElement,
    HTMLElement_get_style,
    HTMLElement_put_onhelp,
    HTMLElement_get_onhelp,
    HTMLElement_put_onclick,
    HTMLElement_get_onclick,
    HTMLElement_put_ondblclick,
    HTMLElement_get_ondblclick,
    HTMLElement_put_onkeydown,
    HTMLElement_get_onkeydown,
    HTMLElement_put_onkeyup,
    HTMLElement_get_onkeyup,
    HTMLElement_put_onkeypress,
    HTMLElement_get_onkeypress,
    HTMLElement_put_onmouseout,
    HTMLElement_get_onmouseout,
    HTMLElement_put_onmouseover,
    HTMLElement_get_onmouseover,
    HTMLElement_put_onmousemove,
    HTMLElement_get_onmousemove,
    HTMLElement_put_onmousedown,
    HTMLElement_get_onmousedown,
    HTMLElement_put_onmouseup,
    HTMLElement_get_onmouseup,
    HTMLElement_get_document,
    HTMLElement_put_title,
    HTMLElement_get_title,
    HTMLElement_put_language,
    HTMLElement_get_language,
    HTMLElement_put_onselectstart,
    HTMLElement_get_onselectstart,
    HTMLElement_scrollIntoView,
    HTMLElement_contains,
    HTMLElement_get_sourceIndex,
    HTMLElement_get_recordNumber,
    HTMLElement_put_lang,
    HTMLElement_get_lang,
    HTMLElement_get_offsetLeft,
    HTMLElement_get_offsetTop,
    HTMLElement_get_offsetWidth,
    HTMLElement_get_offsetHeight,
    HTMLElement_get_offsetParent,
    HTMLElement_put_innerHTML,
    HTMLElement_get_innerHTML,
    HTMLElement_put_innerText,
    HTMLElement_get_innerText,
    HTMLElement_put_outerHTML,
    HTMLElement_get_outerHTML,
    HTMLElement_put_outerText,
    HTMLElement_get_outerText,
    HTMLElement_insertAdjacentHTML,
    HTMLElement_insertAdjacentText,
    HTMLElement_get_parentTextEdit,
    HTMLElement_get_isTextEdit,
    HTMLElement_click,
    HTMLElement_get_filters,
    HTMLElement_put_ondragstart,
    HTMLElement_get_ondragstart,
    HTMLElement_toString,
    HTMLElement_put_onbeforeupdate,
    HTMLElement_get_onbeforeupdate,
    HTMLElement_put_onafterupdate,
    HTMLElement_get_onafterupdate,
    HTMLElement_put_onerrorupdate,
    HTMLElement_get_onerrorupdate,
    HTMLElement_put_onrowexit,
    HTMLElement_get_onrowexit,
    HTMLElement_put_onrowenter,
    HTMLElement_get_onrowenter,
    HTMLElement_put_ondatasetchanged,
    HTMLElement_get_ondatasetchanged,
    HTMLElement_put_ondataavailable,
    HTMLElement_get_ondataavailable,
    HTMLElement_put_ondatasetcomplete,
    HTMLElement_get_ondatasetcomplete,
    HTMLElement_put_onfilterchange,
    HTMLElement_get_onfilterchange,
    HTMLElement_get_children,
    HTMLElement_get_all
};

1546 1547 1548 1549 1550
HTMLElement *unsafe_impl_from_IHTMLElement(IHTMLElement *iface)
{
    return iface->lpVtbl == &HTMLElementVtbl ? impl_from_IHTMLElement(iface) : NULL;
}

1551 1552 1553 1554 1555
static inline HTMLElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
{
    return CONTAINING_RECORD(iface, HTMLElement, node);
}

1556
HRESULT HTMLElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
1557
{
1558
    HTMLElement *This = impl_from_HTMLDOMNode(iface);
1559

1560 1561 1562 1563
    *ppv =  NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1564
        *ppv = &This->IHTMLElement_iface;
1565 1566
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1567
        *ppv = &This->IHTMLElement_iface;
1568 1569
    }else if(IsEqualGUID(&IID_IHTMLElement, riid)) {
        TRACE("(%p)->(IID_IHTMLElement %p)\n", This, ppv);
1570
        *ppv = &This->IHTMLElement_iface;
1571 1572
    }else if(IsEqualGUID(&IID_IHTMLElement2, riid)) {
        TRACE("(%p)->(IID_IHTMLElement2 %p)\n", This, ppv);
1573
        *ppv = &This->IHTMLElement2_iface;
1574 1575
    }else if(IsEqualGUID(&IID_IHTMLElement3, riid)) {
        TRACE("(%p)->(IID_IHTMLElement3 %p)\n", This, ppv);
1576
        *ppv = &This->IHTMLElement3_iface;
1577 1578 1579
    }else if(IsEqualGUID(&IID_IHTMLElement4, riid)) {
        TRACE("(%p)->(IID_IHTMLElement4 %p)\n", This, ppv);
        *ppv = &This->IHTMLElement4_iface;
1580 1581
    }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
        TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1582
        *ppv = &This->cp_container.IConnectionPointContainer_iface;
1583 1584 1585
    }

    if(*ppv) {
1586
        IHTMLElement_AddRef(&This->IHTMLElement_iface);
1587 1588 1589
        return S_OK;
    }

1590
    return HTMLDOMNode_QI(&This->node, riid, ppv);
1591 1592
}

1593
void HTMLElement_destructor(HTMLDOMNode *iface)
1594
{
1595
    HTMLElement *This = impl_from_HTMLDOMNode(iface);
1596

1597 1598
    ConnectionPointContainer_Destroy(&This->cp_container);

1599 1600
    if(This->nselem)
        nsIDOMHTMLElement_Release(This->nselem);
1601 1602
    if(This->style) {
        This->style->elem = NULL;
1603
        IHTMLStyle_Release(&This->style->IHTMLStyle_iface);
1604
    }
1605 1606 1607 1608 1609 1610 1611 1612 1613
    if(This->attrs) {
        HTMLDOMAttribute *attr;

        LIST_FOR_EACH_ENTRY(attr, &This->attrs->attrs, HTMLDOMAttribute, entry)
            attr->elem = NULL;

        This->attrs->elem = NULL;
        IHTMLAttributeCollection_Release(&This->attrs->IHTMLAttributeCollection_iface);
    }
1614

1615 1616
    heap_free(This->filter);

1617
    HTMLDOMNode_destructor(&This->node);
1618 1619
}

1620 1621
HRESULT HTMLElement_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
{
1622
    HTMLElement *This = impl_from_HTMLDOMNode(iface);
1623
    HTMLElement *new_elem;
1624
    HRESULT hres;
1625

1626 1627 1628
    hres = HTMLElement_Create(This->node.doc, nsnode, FALSE, &new_elem);
    if(FAILED(hres))
        return hres;
1629

1630 1631 1632 1633 1634 1635 1636 1637
    if(This->filter) {
        new_elem->filter = heap_strdupW(This->filter);
        if(!new_elem->filter) {
            IHTMLElement_Release(&This->IHTMLElement_iface);
            return E_OUTOFMEMORY;
        }
    }

1638
    IHTMLElement_AddRef(&new_elem->IHTMLElement_iface);
1639 1640 1641 1642
    *ret = &new_elem->node;
    return S_OK;
}

1643
static const NodeImplVtbl HTMLElementImplVtbl = {
1644
    HTMLElement_QI,
1645
    HTMLElement_destructor,
1646 1647
    HTMLElement_clone,
    HTMLElement_get_attr_col
1648 1649
};

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
static inline HTMLElement *impl_from_DispatchEx(DispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLElement, node.dispex);
}

static HRESULT HTMLElement_get_dispid(DispatchEx *dispex, BSTR name,
        DWORD grfdex, DISPID *pid)
{
    HTMLElement *This = impl_from_DispatchEx(dispex);

    if(This->node.vtbl->get_dispid)
        return This->node.vtbl->get_dispid(&This->node, name, grfdex, pid);

    return DISP_E_UNKNOWNNAME;
}

static HRESULT HTMLElement_invoke(DispatchEx *dispex, DISPID id, LCID lcid,
        WORD flags, DISPPARAMS *params, VARIANT *res, EXCEPINFO *ei,
        IServiceProvider *caller)
{
    HTMLElement *This = impl_from_DispatchEx(dispex);

    if(This->node.vtbl->invoke)
        return This->node.vtbl->invoke(&This->node, id, lcid, flags,
                params, res, ei, caller);

    ERR("(%p): element has no invoke method\n", This);
    return E_NOTIMPL;
}

1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
static HRESULT HTMLElement_populate_props(DispatchEx *dispex)
{
    HTMLElement *This = impl_from_DispatchEx(dispex);
    nsIDOMNamedNodeMap *attrs;
    nsIDOMNode *node;
    nsAString nsstr;
    const PRUnichar *str;
    BSTR name;
    VARIANT value;
    unsigned i;
    PRUint32 len;
    DISPID id;
    nsresult nsres;
    HRESULT hres;

    if(!This->nselem)
        return S_FALSE;

    nsres = nsIDOMHTMLElement_GetAttributes(This->nselem, &attrs);
    if(NS_FAILED(nsres))
        return E_FAIL;

    nsres = nsIDOMNamedNodeMap_GetLength(attrs, &len);
    if(NS_FAILED(nsres)) {
        nsIDOMNamedNodeMap_Release(attrs);
        return E_FAIL;
    }

    nsAString_Init(&nsstr, NULL);
    for(i=0; i<len; i++) {
        nsres = nsIDOMNamedNodeMap_Item(attrs, i, &node);
        if(NS_FAILED(nsres))
            continue;

        nsres = nsIDOMNode_GetNodeName(node, &nsstr);
        if(NS_FAILED(nsres)) {
            nsIDOMNode_Release(node);
            continue;
        }

        nsAString_GetData(&nsstr, &str);
        name = SysAllocString(str);
        if(!name) {
            nsIDOMNode_Release(node);
            continue;
        }

        hres = IDispatchEx_GetDispID(&dispex->IDispatchEx_iface, name, fdexNameCaseInsensitive, &id);
        if(hres != DISP_E_UNKNOWNNAME) {
            nsIDOMNode_Release(node);
            SysFreeString(name);
            continue;
        }

        nsres = nsIDOMNode_GetNodeValue(node, &nsstr);
        nsIDOMNode_Release(node);
        if(NS_FAILED(nsres)) {
            SysFreeString(name);
            continue;
        }

        nsAString_GetData(&nsstr, &str);
        V_VT(&value) = VT_BSTR;
        if(*str) {
            V_BSTR(&value) = SysAllocString(str);
            if(!V_BSTR(&value)) {
                SysFreeString(name);
                continue;
            }
        } else
            V_BSTR(&value) = NULL;

        IHTMLElement_setAttribute(&This->IHTMLElement_iface, name, value, 0);
        SysFreeString(name);
        VariantClear(&value);
    }
    nsAString_Finish(&nsstr);

    nsIDOMNamedNodeMap_Release(attrs);
    return S_OK;
}

1762
static const tid_t HTMLElement_iface_tids[] = {
1763
    HTMLELEMENT_TIDS,
1764 1765
    0
};
1766

1767
static dispex_static_data_vtbl_t HTMLElement_dispex_vtbl = {
1768
    NULL,
1769
    HTMLElement_get_dispid,
1770 1771
    HTMLElement_invoke,
    HTMLElement_populate_props
1772 1773 1774 1775
};

static dispex_static_data_t HTMLElement_dispex = {
    &HTMLElement_dispex_vtbl,
1776 1777
    DispHTMLUnknownElement_tid,
    NULL,
1778
    HTMLElement_iface_tids
1779 1780
};

1781
void HTMLElement_Init(HTMLElement *This, HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, dispex_static_data_t *dispex_data)
1782
{
1783
    This->IHTMLElement_iface.lpVtbl = &HTMLElementVtbl;
1784 1785

    HTMLElement2_Init(This);
1786
    HTMLElement3_Init(This);
1787

1788 1789
    if(dispex_data && !dispex_data->vtbl)
        dispex_data->vtbl = &HTMLElement_dispex_vtbl;
1790 1791
    init_dispex(&This->node.dispex, (IUnknown*)&This->IHTMLElement_iface,
            dispex_data ? dispex_data : &HTMLElement_dispex);
1792 1793 1794 1795 1796 1797 1798

    if(nselem)
        nsIDOMHTMLElement_AddRef(nselem);
    This->nselem = nselem;

    HTMLDOMNode_Init(doc, &This->node, (nsIDOMNode*)nselem);

1799
    ConnectionPointContainer_Init(&This->cp_container, (IUnknown*)&This->IHTMLElement_iface);
1800 1801
}

1802
HRESULT HTMLElement_Create(HTMLDocumentNode *doc, nsIDOMNode *nsnode, BOOL use_generic, HTMLElement **ret)
1803
{
1804
    nsIDOMHTMLElement *nselem;
1805 1806
    nsAString class_name_str;
    const PRUnichar *class_name;
1807
    const tag_desc_t *tag;
1808
    HTMLElement *elem;
1809
    nsresult nsres;
1810
    HRESULT hres;
1811

1812
    nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMHTMLElement, (void**)&nselem);
1813
    if(NS_FAILED(nsres))
1814
        return E_FAIL;
1815 1816

    nsAString_Init(&class_name_str, NULL);
1817
    nsIDOMHTMLElement_GetTagName(nselem, &class_name_str);
1818

1819
    nsAString_GetData(&class_name_str, &class_name);
1820

1821 1822
    tag = get_tag_desc(class_name);
    if(tag) {
1823
        hres = tag->constructor(doc, nselem, &elem);
1824
    }else if(use_generic) {
1825
        hres = HTMLGenericElement_Create(doc, nselem, &elem);
1826
    }else {
1827 1828 1829 1830 1831 1832 1833 1834
        elem = heap_alloc_zero(sizeof(HTMLElement));
        if(elem) {
            HTMLElement_Init(elem, doc, nselem, &HTMLElement_dispex);
            elem->node.vtbl = &HTMLElementImplVtbl;
            hres = S_OK;
        }else {
            hres = E_OUTOFMEMORY;
        }
1835 1836
    }

1837
    TRACE("%s ret %p\n", debugstr_w(class_name), elem);
1838

1839
    nsIDOMElement_Release(nselem);
1840
    nsAString_Finish(&class_name_str);
1841 1842
    if(FAILED(hres))
        return hres;
1843

1844 1845
    *ret = elem;
    return S_OK;
1846
}
1847

1848
/* interface IHTMLFiltersCollection */
1849 1850
static HRESULT WINAPI HTMLFiltersCollection_QueryInterface(IHTMLFiltersCollection *iface, REFIID riid, void **ppv)
{
1851
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1852 1853 1854 1855 1856

    TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1857
        *ppv = &This->IHTMLFiltersCollection_iface;
1858 1859
    }else if(IsEqualGUID(&IID_IHTMLFiltersCollection, riid)) {
        TRACE("(%p)->(IID_IHTMLFiltersCollection %p)\n", This, ppv);
1860
        *ppv = &This->IHTMLFiltersCollection_iface;
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
    }

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

    FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI HTMLFiltersCollection_AddRef(IHTMLFiltersCollection *iface)
{
1876
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1877 1878 1879 1880 1881 1882 1883 1884 1885
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI HTMLFiltersCollection_Release(IHTMLFiltersCollection *iface)
{
1886
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref)
    {
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI HTMLFiltersCollection_GetTypeInfoCount(IHTMLFiltersCollection *iface, UINT *pctinfo)
{
1901
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1902
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1903 1904 1905 1906 1907
}

static HRESULT WINAPI HTMLFiltersCollection_GetTypeInfo(IHTMLFiltersCollection *iface,
                                    UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
1908
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1909
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1910 1911 1912 1913 1914 1915
}

static HRESULT WINAPI HTMLFiltersCollection_GetIDsOfNames(IHTMLFiltersCollection *iface,
                                    REFIID riid, LPOLESTR *rgszNames, UINT cNames,
                                    LCID lcid, DISPID *rgDispId)
{
1916
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1917 1918
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
1919 1920 1921 1922 1923 1924
}

static HRESULT WINAPI HTMLFiltersCollection_Invoke(IHTMLFiltersCollection *iface, DISPID dispIdMember, REFIID riid,
                                    LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
                                    EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
1925
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1926
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
1927 1928 1929 1930 1931
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI HTMLFiltersCollection_get_length(IHTMLFiltersCollection *iface, LONG *p)
{
1932
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1933

1934 1935
    if(!p)
        return E_POINTER;
1936

1937 1938
    FIXME("(%p)->(%p) Always returning 0\n", This, p);
    *p = 0;
1939 1940 1941 1942 1943 1944

    return S_OK;
}

static HRESULT WINAPI HTMLFiltersCollection_get__newEnum(IHTMLFiltersCollection *iface, IUnknown **p)
{
1945
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1946 1947 1948 1949 1950 1951
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLFiltersCollection_item(IHTMLFiltersCollection *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
{
1952
    HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
    FIXME("(%p)->(%p, %p)\n", This, pvarIndex, pvarResult);
    return E_NOTIMPL;
}

static const IHTMLFiltersCollectionVtbl HTMLFiltersCollectionVtbl = {
    HTMLFiltersCollection_QueryInterface,
    HTMLFiltersCollection_AddRef,
    HTMLFiltersCollection_Release,
    HTMLFiltersCollection_GetTypeInfoCount,
    HTMLFiltersCollection_GetTypeInfo,
    HTMLFiltersCollection_GetIDsOfNames,
    HTMLFiltersCollection_Invoke,
    HTMLFiltersCollection_get_length,
    HTMLFiltersCollection_get__newEnum,
    HTMLFiltersCollection_item
};

1970
static HRESULT HTMLFiltersCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
{
    WCHAR *ptr;
    int idx = 0;

    for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
        idx = idx*10 + (*ptr-'0');
    if(*ptr)
        return DISP_E_UNKNOWNNAME;

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

1985
static HRESULT HTMLFiltersCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
1986 1987
        VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
{
1988
    TRACE("(%p)->(%x %x %x %p %p %p)\n", dispex, id, lcid, flags, params, res, ei);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000

    V_VT(res) = VT_DISPATCH;
    V_DISPATCH(res) = NULL;

    FIXME("always returning NULL\n");

    return S_OK;
}

static const dispex_static_data_vtbl_t HTMLFiltersCollection_dispex_vtbl = {
    NULL,
    HTMLFiltersCollection_get_dispid,
2001 2002
    HTMLFiltersCollection_invoke,
    NULL
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
};

static const tid_t HTMLFiltersCollection_iface_tids[] = {
    IHTMLFiltersCollection_tid,
    0
};
static dispex_static_data_t HTMLFiltersCollection_dispex = {
    &HTMLFiltersCollection_dispex_vtbl,
    IHTMLFiltersCollection_tid,
    NULL,
    HTMLFiltersCollection_iface_tids
};

2016
static IHTMLFiltersCollection *HTMLFiltersCollection_Create(void)
2017 2018 2019
{
    HTMLFiltersCollection *ret = heap_alloc(sizeof(HTMLFiltersCollection));

2020
    ret->IHTMLFiltersCollection_iface.lpVtbl = &HTMLFiltersCollectionVtbl;
2021 2022
    ret->ref = 1;

2023 2024
    init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLFiltersCollection_iface,
            &HTMLFiltersCollection_dispex);
2025

2026
    return &ret->IHTMLFiltersCollection_iface;
2027
}
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047

/* interface IHTMLAttributeCollection */
static inline HTMLAttributeCollection *impl_from_IHTMLAttributeCollection(IHTMLAttributeCollection *iface)
{
    return CONTAINING_RECORD(iface, HTMLAttributeCollection, IHTMLAttributeCollection_iface);
}

static HRESULT WINAPI HTMLAttributeCollection_QueryInterface(IHTMLAttributeCollection *iface, REFIID riid, void **ppv)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IHTMLAttributeCollection_iface;
    }else if(IsEqualGUID(&IID_IHTMLAttributeCollection, riid)) {
        TRACE("(%p)->(IID_IHTMLAttributeCollection %p)\n", This, ppv);
        *ppv = &This->IHTMLAttributeCollection_iface;
    }else if(IsEqualGUID(&IID_IHTMLAttributeCollection2, riid)) {
2048 2049
        TRACE("(%p)->(IID_IHTMLAttributeCollection2 %p)\n", This, ppv);
        *ppv = &This->IHTMLAttributeCollection2_iface;
2050
    }else if(IsEqualGUID(&IID_IHTMLAttributeCollection3, riid)) {
2051 2052
        TRACE("(%p)->(IID_IHTMLAttributeCollection3 %p)\n", This, ppv);
        *ppv = &This->IHTMLAttributeCollection3_iface;
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
    }

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

    WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI HTMLAttributeCollection_AddRef(IHTMLAttributeCollection *iface)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI HTMLAttributeCollection_Release(IHTMLAttributeCollection *iface)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
2084 2085 2086 2087 2088 2089 2090 2091
        while(!list_empty(&This->attrs)) {
            HTMLDOMAttribute *attr = LIST_ENTRY(list_head(&This->attrs), HTMLDOMAttribute, entry);

            list_remove(&attr->entry);
            attr->elem = NULL;
            IHTMLDOMAttribute_Release(&attr->IHTMLDOMAttribute_iface);
        }

2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI HTMLAttributeCollection_GetTypeInfoCount(IHTMLAttributeCollection *iface, UINT *pctinfo)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
}

static HRESULT WINAPI HTMLAttributeCollection_GetTypeInfo(IHTMLAttributeCollection *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
}

static HRESULT WINAPI HTMLAttributeCollection_GetIDsOfNames(IHTMLAttributeCollection *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
}

static HRESULT WINAPI HTMLAttributeCollection_Invoke(IHTMLAttributeCollection *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
        VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

2128
static HRESULT get_attr_dispid_by_idx(HTMLAttributeCollection *This, LONG *idx, DISPID *dispid)
2129
{
2130
    IDispatchEx *dispex = &This->elem->node.dispex.IDispatchEx_iface;
2131 2132
    DISPID id = DISPID_STARTENUM;
    LONG len = -1;
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
    HRESULT hres;

    FIXME("filter non-enumerable attributes out\n");

    while(1) {
        hres = IDispatchEx_GetNextDispID(dispex, fdexEnumAll, id, &id);
        if(FAILED(hres))
            return hres;
        else if(hres == S_FALSE)
            break;

        len++;
2145
        if(len == *idx)
2146 2147 2148
            break;
    }

2149 2150 2151 2152
    if(dispid) {
        *dispid = id;
        return *idx==len ? S_OK : DISP_E_UNKNOWNNAME;
    }
2153

2154
    *idx = len+1;
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
    return S_OK;
}

static inline HRESULT get_attr_dispid_by_name(HTMLAttributeCollection *This, BSTR name, DISPID *id)
{
    HRESULT hres;

    if(name[0]>='0' && name[0]<='9') {
        WCHAR *end_ptr;
        LONG idx;

        idx = strtoulW(name, &end_ptr, 10);
        if(!*end_ptr) {
2168
            hres = get_attr_dispid_by_idx(This, &idx, id);
2169 2170 2171 2172 2173
            if(SUCCEEDED(hres))
                return hres;
        }
    }

2174 2175 2176 2177 2178
    if(!This->elem) {
        WARN("NULL elem\n");
        return E_UNEXPECTED;
    }

2179 2180 2181 2182 2183
    hres = IDispatchEx_GetDispID(&This->elem->node.dispex.IDispatchEx_iface,
            name, fdexNameCaseInsensitive, id);
    return hres;
}

2184
static inline HRESULT get_domattr(HTMLAttributeCollection *This, DISPID id, LONG *list_pos, HTMLDOMAttribute **attr)
2185 2186
{
    HTMLDOMAttribute *iter;
2187
    LONG pos = 0;
2188 2189 2190
    HRESULT hres;

    *attr = NULL;
2191
    LIST_FOR_EACH_ENTRY(iter, &This->attrs, HTMLDOMAttribute, entry) {
2192 2193 2194 2195
        if(iter->dispid == id) {
            *attr = iter;
            break;
        }
2196
        pos++;
2197 2198 2199
    }

    if(!*attr) {
2200 2201 2202 2203 2204
        if(!This->elem) {
            WARN("NULL elem\n");
            return E_UNEXPECTED;
        }

2205
        pos++;
2206
        hres = HTMLDOMAttribute_Create(This->elem, id, attr);
2207 2208 2209 2210 2211
        if(FAILED(hres))
            return hres;
    }

    IHTMLDOMAttribute_AddRef(&(*attr)->IHTMLDOMAttribute_iface);
2212 2213
    if(list_pos)
        *list_pos = pos;
2214 2215 2216
    return S_OK;
}

2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
static HRESULT WINAPI HTMLAttributeCollection_get_length(IHTMLAttributeCollection *iface, LONG *p)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    HRESULT hres;

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

    *p = -1;
    hres = get_attr_dispid_by_idx(This, p, NULL);
    return hres;
}

static HRESULT WINAPI HTMLAttributeCollection__newEnum(IHTMLAttributeCollection *iface, IUnknown **p)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
    FIXME("(%p)->(%p)\n", This, p);
    return E_NOTIMPL;
}

2236 2237 2238
static HRESULT WINAPI HTMLAttributeCollection_item(IHTMLAttributeCollection *iface, VARIANT *name, IDispatch **ppItem)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2239 2240 2241 2242 2243 2244 2245 2246
    HTMLDOMAttribute *attr;
    DISPID id;
    HRESULT hres;

    TRACE("(%p)->(%s %p)\n", This, debugstr_variant(name), ppItem);

    switch(V_VT(name)) {
    case VT_I4:
2247
        hres = get_attr_dispid_by_idx(This, &V_I4(name), &id);
2248 2249 2250 2251 2252
        break;
    case VT_BSTR:
        hres = get_attr_dispid_by_name(This, V_BSTR(name), &id);
        break;
    default:
2253
        FIXME("unsupported name %s\n", debugstr_variant(name));
2254 2255 2256 2257 2258 2259 2260
        hres = E_NOTIMPL;
    }
    if(hres == DISP_E_UNKNOWNNAME)
        return E_INVALIDARG;
    if(FAILED(hres))
        return hres;

2261
    hres = get_domattr(This, id, NULL, &attr);
2262 2263 2264 2265 2266
    if(FAILED(hres))
        return hres;

    *ppItem = (IDispatch*)&attr->IHTMLDOMAttribute_iface;
    return S_OK;
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
}

static const IHTMLAttributeCollectionVtbl HTMLAttributeCollectionVtbl = {
    HTMLAttributeCollection_QueryInterface,
    HTMLAttributeCollection_AddRef,
    HTMLAttributeCollection_Release,
    HTMLAttributeCollection_GetTypeInfoCount,
    HTMLAttributeCollection_GetTypeInfo,
    HTMLAttributeCollection_GetIDsOfNames,
    HTMLAttributeCollection_Invoke,
    HTMLAttributeCollection_get_length,
    HTMLAttributeCollection__newEnum,
    HTMLAttributeCollection_item
};

2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
static inline HTMLAttributeCollection *impl_from_IHTMLAttributeCollection2(IHTMLAttributeCollection2 *iface)
{
    return CONTAINING_RECORD(iface, HTMLAttributeCollection, IHTMLAttributeCollection2_iface);
}

static HRESULT WINAPI HTMLAttributeCollection2_QueryInterface(IHTMLAttributeCollection2 *iface, REFIID riid, void **ppv)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IHTMLAttributeCollection_QueryInterface(&This->IHTMLAttributeCollection_iface, riid, ppv);
}

static ULONG WINAPI HTMLAttributeCollection2_AddRef(IHTMLAttributeCollection2 *iface)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IHTMLAttributeCollection_AddRef(&This->IHTMLAttributeCollection_iface);
}

static ULONG WINAPI HTMLAttributeCollection2_Release(IHTMLAttributeCollection2 *iface)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IHTMLAttributeCollection_Release(&This->IHTMLAttributeCollection_iface);
}

static HRESULT WINAPI HTMLAttributeCollection2_GetTypeInfoCount(IHTMLAttributeCollection2 *iface, UINT *pctinfo)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
}

static HRESULT WINAPI HTMLAttributeCollection2_GetTypeInfo(IHTMLAttributeCollection2 *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
}

static HRESULT WINAPI HTMLAttributeCollection2_GetIDsOfNames(IHTMLAttributeCollection2 *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
}

static HRESULT WINAPI HTMLAttributeCollection2_Invoke(IHTMLAttributeCollection2 *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
        VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI HTMLAttributeCollection2_getNamedItem(IHTMLAttributeCollection2 *iface, BSTR bstrName,
        IHTMLDOMAttribute **newretNode)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
    HTMLDOMAttribute *attr;
    DISPID id;
    HRESULT hres;

    TRACE("(%p)->(%s %p)\n", This, debugstr_w(bstrName), newretNode);

    hres = get_attr_dispid_by_name(This, bstrName, &id);
    if(hres == DISP_E_UNKNOWNNAME) {
        *newretNode = NULL;
        return S_OK;
    } else if(FAILED(hres)) {
        return hres;
    }

2353
    hres = get_domattr(This, id, NULL, &attr);
2354 2355 2356 2357 2358
    if(FAILED(hres))
        return hres;

    *newretNode = &attr->IHTMLDOMAttribute_iface;
    return S_OK;
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
}

static HRESULT WINAPI HTMLAttributeCollection2_setNamedItem(IHTMLAttributeCollection2 *iface,
        IHTMLDOMAttribute *ppNode, IHTMLDOMAttribute **newretNode)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    FIXME("(%p)->(%p %p)\n", This, ppNode, newretNode);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLAttributeCollection2_removeNamedItem(IHTMLAttributeCollection2 *iface,
        BSTR bstrName, IHTMLDOMAttribute **newretNode)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(bstrName), newretNode);
    return E_NOTIMPL;
}

static const IHTMLAttributeCollection2Vtbl HTMLAttributeCollection2Vtbl = {
    HTMLAttributeCollection2_QueryInterface,
    HTMLAttributeCollection2_AddRef,
    HTMLAttributeCollection2_Release,
    HTMLAttributeCollection2_GetTypeInfoCount,
    HTMLAttributeCollection2_GetTypeInfo,
    HTMLAttributeCollection2_GetIDsOfNames,
    HTMLAttributeCollection2_Invoke,
    HTMLAttributeCollection2_getNamedItem,
    HTMLAttributeCollection2_setNamedItem,
    HTMLAttributeCollection2_removeNamedItem
};

2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
static inline HTMLAttributeCollection *impl_from_IHTMLAttributeCollection3(IHTMLAttributeCollection3 *iface)
{
    return CONTAINING_RECORD(iface, HTMLAttributeCollection, IHTMLAttributeCollection3_iface);
}

static HRESULT WINAPI HTMLAttributeCollection3_QueryInterface(IHTMLAttributeCollection3 *iface, REFIID riid, void **ppv)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IHTMLAttributeCollection_QueryInterface(&This->IHTMLAttributeCollection_iface, riid, ppv);
}

static ULONG WINAPI HTMLAttributeCollection3_AddRef(IHTMLAttributeCollection3 *iface)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IHTMLAttributeCollection_AddRef(&This->IHTMLAttributeCollection_iface);
}

static ULONG WINAPI HTMLAttributeCollection3_Release(IHTMLAttributeCollection3 *iface)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IHTMLAttributeCollection_Release(&This->IHTMLAttributeCollection_iface);
}

static HRESULT WINAPI HTMLAttributeCollection3_GetTypeInfoCount(IHTMLAttributeCollection3 *iface, UINT *pctinfo)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
}

static HRESULT WINAPI HTMLAttributeCollection3_GetTypeInfo(IHTMLAttributeCollection3 *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
}

static HRESULT WINAPI HTMLAttributeCollection3_GetIDsOfNames(IHTMLAttributeCollection3 *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
            lcid, rgDispId);
}

static HRESULT WINAPI HTMLAttributeCollection3_Invoke(IHTMLAttributeCollection3 *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
        VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI HTMLAttributeCollection3_getNamedItem(IHTMLAttributeCollection3 *iface, BSTR bstrName,
        IHTMLDOMAttribute **ppNodeOut)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2447
    return IHTMLAttributeCollection2_getNamedItem(&This->IHTMLAttributeCollection2_iface, bstrName, ppNodeOut);
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
}

static HRESULT WINAPI HTMLAttributeCollection3_setNamedItem(IHTMLAttributeCollection3 *iface,
        IHTMLDOMAttribute *pNodeIn, IHTMLDOMAttribute **ppNodeOut)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    FIXME("(%p)->(%p %p)\n", This, pNodeIn, ppNodeOut);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLAttributeCollection3_removeNamedItem(IHTMLAttributeCollection3 *iface,
        BSTR bstrName, IHTMLDOMAttribute **ppNodeOut)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(bstrName), ppNodeOut);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLAttributeCollection3_item(IHTMLAttributeCollection3 *iface, LONG index, IHTMLDOMAttribute **ppNodeOut)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2469 2470 2471 2472 2473 2474
    HTMLDOMAttribute *attr;
    DISPID id;
    HRESULT hres;

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

2475
    hres = get_attr_dispid_by_idx(This, &index, &id);
2476 2477 2478 2479 2480
    if(hres == DISP_E_UNKNOWNNAME)
        return E_INVALIDARG;
    if(FAILED(hres))
        return hres;

2481
    hres = get_domattr(This, id, NULL, &attr);
2482 2483 2484 2485 2486
    if(FAILED(hres))
        return hres;

    *ppNodeOut = &attr->IHTMLDOMAttribute_iface;
    return S_OK;
2487 2488 2489 2490 2491
}

static HRESULT WINAPI HTMLAttributeCollection3_get_length(IHTMLAttributeCollection3 *iface, LONG *p)
{
    HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2492
    return IHTMLAttributeCollection_get_length(&This->IHTMLAttributeCollection_iface, p);
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
}

static const IHTMLAttributeCollection3Vtbl HTMLAttributeCollection3Vtbl = {
    HTMLAttributeCollection3_QueryInterface,
    HTMLAttributeCollection3_AddRef,
    HTMLAttributeCollection3_Release,
    HTMLAttributeCollection3_GetTypeInfoCount,
    HTMLAttributeCollection3_GetTypeInfo,
    HTMLAttributeCollection3_GetIDsOfNames,
    HTMLAttributeCollection3_Invoke,
    HTMLAttributeCollection3_getNamedItem,
    HTMLAttributeCollection3_setNamedItem,
    HTMLAttributeCollection3_removeNamedItem,
    HTMLAttributeCollection3_item,
    HTMLAttributeCollection3_get_length
};

2510 2511 2512 2513 2514 2515 2516 2517
static inline HTMLAttributeCollection *HTMLAttributeCollection_from_DispatchEx(DispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLAttributeCollection, dispex);
}

static HRESULT HTMLAttributeCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
{
    HTMLAttributeCollection *This = HTMLAttributeCollection_from_DispatchEx(dispex);
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
    HTMLDOMAttribute *attr;
    LONG pos;
    HRESULT hres;

    TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(name), flags, dispid);

    hres = get_attr_dispid_by_name(This, name, dispid);
    if(FAILED(hres))
        return hres;

    hres = get_domattr(This, *dispid, &pos, &attr);
    if(FAILED(hres))
        return hres;
    IHTMLDOMAttribute_Release(&attr->IHTMLDOMAttribute_iface);

    *dispid = MSHTML_DISPID_CUSTOM_MIN+pos;
    return S_OK;
2535 2536
}

2537 2538
static HRESULT HTMLAttributeCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid,
        WORD flags, DISPPARAMS *params, VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2539 2540
{
    HTMLAttributeCollection *This = HTMLAttributeCollection_from_DispatchEx(dispex);
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566

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

    switch(flags) {
    case DISPATCH_PROPERTYGET: {
        HTMLDOMAttribute *iter;

        id = id-MSHTML_DISPID_CUSTOM_MIN+1;

        LIST_FOR_EACH_ENTRY(iter, &This->attrs, HTMLDOMAttribute, entry) {
            if(!(--id))
                break;
        }
        if(id)
            return E_INVALIDARG;

        IHTMLDOMAttribute_AddRef(&iter->IHTMLDOMAttribute_iface);
        V_VT(res) = VT_DISPATCH;
        V_DISPATCH(res) = (IDispatch*)&iter->IHTMLDOMAttribute_iface;
        return S_OK;
    }

    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
}

static const dispex_static_data_vtbl_t HTMLAttributeCollection_dispex_vtbl = {
    NULL,
    HTMLAttributeCollection_get_dispid,
    HTMLAttributeCollection_invoke,
    NULL
};

static const tid_t HTMLAttributeCollection_iface_tids[] = {
    IHTMLAttributeCollection_tid,
    IHTMLAttributeCollection2_tid,
    IHTMLAttributeCollection3_tid,
    0
};

static dispex_static_data_t HTMLAttributeCollection_dispex = {
    &HTMLAttributeCollection_dispex_vtbl,
    DispHTMLAttributeCollection_tid,
    NULL,
    HTMLAttributeCollection_iface_tids
};

HRESULT HTMLElement_get_attr_col(HTMLDOMNode *iface, HTMLAttributeCollection **ac)
{
    HTMLElement *This = impl_from_HTMLDOMNode(iface);

2594 2595 2596 2597 2598
    if(This->attrs) {
        IHTMLAttributeCollection_AddRef(&This->attrs->IHTMLAttributeCollection_iface);
        *ac = This->attrs;
        return S_OK;
    }
2599

2600 2601 2602
    This->attrs = heap_alloc_zero(sizeof(HTMLAttributeCollection));
    if(!This->attrs)
        return E_OUTOFMEMORY;
2603

2604 2605 2606 2607
    This->attrs->IHTMLAttributeCollection_iface.lpVtbl = &HTMLAttributeCollectionVtbl;
    This->attrs->IHTMLAttributeCollection2_iface.lpVtbl = &HTMLAttributeCollection2Vtbl;
    This->attrs->IHTMLAttributeCollection3_iface.lpVtbl = &HTMLAttributeCollection3Vtbl;
    This->attrs->ref = 2;
2608

2609 2610 2611
    This->attrs->elem = This;
    list_init(&This->attrs->attrs);
    init_dispex(&This->attrs->dispex, (IUnknown*)&This->attrs->IHTMLAttributeCollection_iface,
2612 2613
            &HTMLAttributeCollection_dispex);

2614
    *ac = This->attrs;
2615 2616
    return S_OK;
}