htmldoc.c 72.2 KB
Newer Older
1
/*
2
 * Copyright 2005-2009 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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 27 28
 */

#include "config.h"

#include <stdarg.h>
#include <stdio.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
29
#include "wininet.h"
30
#include "ole2.h"
31
#include "perhist.h"
32
#include "mshtmdid.h"
33 34 35 36

#include "wine/debug.h"

#include "mshtml_private.h"
37
#include "htmlevent.h"
38
#include "pluginhost.h"
39 40 41

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

42 43 44 45
static inline HTMLDocument *impl_from_IHTMLDocument2(IHTMLDocument2 *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocument, IHTMLDocument2_iface);
}
46

47
static HRESULT WINAPI HTMLDocument_QueryInterface(IHTMLDocument2 *iface, REFIID riid, void **ppv)
48
{
49
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
50

51
    return htmldoc_query_interface(This, riid, ppv);
52 53 54 55
}

static ULONG WINAPI HTMLDocument_AddRef(IHTMLDocument2 *iface)
{
56
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
57 58

    return htmldoc_addref(This);
59 60 61 62
}

static ULONG WINAPI HTMLDocument_Release(IHTMLDocument2 *iface)
{
63
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
64

65
    return htmldoc_release(This);
66 67 68 69
}

static HRESULT WINAPI HTMLDocument_GetTypeInfoCount(IHTMLDocument2 *iface, UINT *pctinfo)
{
70
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
71

72
    return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
73 74 75 76 77
}

static HRESULT WINAPI HTMLDocument_GetTypeInfo(IHTMLDocument2 *iface, UINT iTInfo,
                                                LCID lcid, ITypeInfo **ppTInfo)
{
78
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
79

80
    return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
81 82 83 84 85 86
}

static HRESULT WINAPI HTMLDocument_GetIDsOfNames(IHTMLDocument2 *iface, REFIID riid,
                                                LPOLESTR *rgszNames, UINT cNames,
                                                LCID lcid, DISPID *rgDispId)
{
87
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
88

89 90
    return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
            rgDispId);
91 92 93 94 95 96
}

static HRESULT WINAPI HTMLDocument_Invoke(IHTMLDocument2 *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
97
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
98

99 100
    return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
            pDispParams, pVarResult, pExcepInfo, puArgErr);
101 102 103 104
}

static HRESULT WINAPI HTMLDocument_get_Script(IHTMLDocument2 *iface, IDispatch **p)
{
105
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
106 107 108

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

109
    *p = (IDispatch*)&This->window->IHTMLWindow2_iface;
110 111
    IDispatch_AddRef(*p);
    return S_OK;
112 113 114 115
}

static HRESULT WINAPI HTMLDocument_get_all(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
116
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
117
    nsIDOMElement *nselem = NULL;
118
    HTMLDOMNode *node;
119
    nsresult nsres;
120
    HRESULT hres;
121 122 123

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

124
    if(!This->doc_node->nsdoc) {
125 126
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
127 128
    }

129
    nsres = nsIDOMHTMLDocument_GetDocumentElement(This->doc_node->nsdoc, &nselem);
130 131 132
    if(NS_FAILED(nsres)) {
        ERR("GetDocumentElement failed: %08x\n", nsres);
        return E_FAIL;
133 134
    }

135
    if(!nselem) {
136
        *p = NULL;
137
        return S_OK;
138
    }
139

140 141 142 143 144
    hres = get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE, &node);
    if(SUCCEEDED(hres))
        *p = create_all_collection(node, TRUE);
    nsIDOMElement_Release(nselem);
    return hres;
145 146 147 148
}

static HRESULT WINAPI HTMLDocument_get_body(IHTMLDocument2 *iface, IHTMLElement **p)
{
149
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
150 151
    nsIDOMHTMLElement *nsbody = NULL;
    HTMLDOMNode *node;
152
    HRESULT hres;
153 154 155

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

156 157
    if(This->doc_node->nsdoc) {
        nsresult nsres;
158

159 160 161 162 163
        nsres = nsIDOMHTMLDocument_GetBody(This->doc_node->nsdoc, &nsbody);
        if(NS_FAILED(nsres)) {
            TRACE("Could not get body: %08x\n", nsres);
            return E_UNEXPECTED;
        }
164 165
    }

166
    if(!nsbody) {
167
        *p = NULL;
168
        return S_OK;
169
    }
170

171 172 173 174 175
    hres = get_node(This->doc_node, (nsIDOMNode*)nsbody, TRUE, &node);
    nsIDOMHTMLElement_Release(nsbody);
    if(FAILED(hres))
        return hres;

176
    return IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
177 178 179 180
}

static HRESULT WINAPI HTMLDocument_get_activeElement(IHTMLDocument2 *iface, IHTMLElement **p)
{
181
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
182
    FIXME("(%p)->(%p)\n", This, p);
183 184 185 186 187
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_images(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
188
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
189 190 191 192 193 194 195 196 197 198
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

199
    if(!This->doc_node->nsdoc) {
200 201 202 203
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

204
    nsres = nsIDOMHTMLDocument_GetImages(This->doc_node->nsdoc, &nscoll);
205 206 207 208 209 210
    if(NS_FAILED(nsres)) {
        ERR("GetImages failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
211
        *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
212 213 214 215
        nsIDOMElement_Release(nscoll);
    }

    return S_OK;
216 217 218 219
}

static HRESULT WINAPI HTMLDocument_get_applets(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
220
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
221 222 223 224 225 226 227 228 229 230
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

231
    if(!This->doc_node->nsdoc) {
232 233 234 235
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

236
    nsres = nsIDOMHTMLDocument_GetApplets(This->doc_node->nsdoc, &nscoll);
237 238 239 240 241 242
    if(NS_FAILED(nsres)) {
        ERR("GetApplets failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
243
        *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
244 245 246 247
        nsIDOMElement_Release(nscoll);
    }

    return S_OK;
248 249 250 251
}

static HRESULT WINAPI HTMLDocument_get_links(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
252
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
253 254 255 256 257 258 259 260 261 262
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

263
    if(!This->doc_node->nsdoc) {
264 265 266 267
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

268
    nsres = nsIDOMHTMLDocument_GetLinks(This->doc_node->nsdoc, &nscoll);
269 270 271 272 273 274
    if(NS_FAILED(nsres)) {
        ERR("GetLinks failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
275
        *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
276 277 278 279
        nsIDOMElement_Release(nscoll);
    }

    return S_OK;
280 281 282 283
}

static HRESULT WINAPI HTMLDocument_get_forms(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
284
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
285 286 287 288 289 290 291 292 293 294
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

295
    if(!This->doc_node->nsdoc) {
296 297 298 299
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

300
    nsres = nsIDOMHTMLDocument_GetForms(This->doc_node->nsdoc, &nscoll);
301 302 303 304 305 306
    if(NS_FAILED(nsres)) {
        ERR("GetForms failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
307
        *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
308 309 310 311
        nsIDOMElement_Release(nscoll);
    }

    return S_OK;
312 313 314 315
}

static HRESULT WINAPI HTMLDocument_get_anchors(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
316
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
317 318 319 320 321 322 323 324 325 326
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

327
    if(!This->doc_node->nsdoc) {
328 329 330 331
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

332
    nsres = nsIDOMHTMLDocument_GetAnchors(This->doc_node->nsdoc, &nscoll);
333 334 335 336 337 338
    if(NS_FAILED(nsres)) {
        ERR("GetAnchors failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
339
        *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
340 341 342 343
        nsIDOMElement_Release(nscoll);
    }

    return S_OK;
344 345 346 347
}

static HRESULT WINAPI HTMLDocument_put_title(IHTMLDocument2 *iface, BSTR v)
{
348
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
349 350 351 352 353
    nsAString nsstr;
    nsresult nsres;

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

354
    if(!This->doc_node->nsdoc) {
355 356
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
357 358
    }

359
    nsAString_InitDepend(&nsstr, v);
360
    nsres = nsIDOMHTMLDocument_SetTitle(This->doc_node->nsdoc, &nsstr);
361 362 363 364 365
    nsAString_Finish(&nsstr);
    if(NS_FAILED(nsres))
        ERR("SetTitle failed: %08x\n", nsres);

    return S_OK;
366 367 368 369
}

static HRESULT WINAPI HTMLDocument_get_title(IHTMLDocument2 *iface, BSTR *p)
{
370
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
371 372 373 374 375 376
    const PRUnichar *ret;
    nsAString nsstr;
    nsresult nsres;

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

377
    if(!This->doc_node->nsdoc) {
378 379
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
380 381 382 383
    }


    nsAString_Init(&nsstr, NULL);
384
    nsres = nsIDOMHTMLDocument_GetTitle(This->doc_node->nsdoc, &nsstr);
385 386 387 388 389
    if (NS_SUCCEEDED(nsres)) {
        nsAString_GetData(&nsstr, &ret);
        *p = SysAllocString(ret);
    }
    nsAString_Finish(&nsstr);
390

391
    if(NS_FAILED(nsres)) {
392
        ERR("GetTitle failed: %08x\n", nsres);
393 394
        return E_FAIL;
    }
395 396

    return S_OK;
397 398 399 400
}

static HRESULT WINAPI HTMLDocument_get_scripts(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
401
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
402
    FIXME("(%p)->(%p)\n", This, p);
403 404 405 406 407
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_designMode(IHTMLDocument2 *iface, BSTR v)
{
408
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
409
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
410 411 412 413 414
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_designMode(IHTMLDocument2 *iface, BSTR *p)
{
415
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
416 417 418 419 420 421 422 423 424
    static WCHAR szOff[] = {'O','f','f',0};
    FIXME("(%p)->(%p) always returning Off\n", This, p);

    if(!p)
        return E_INVALIDARG;

    *p = SysAllocString(szOff);

    return S_OK;
425 426 427 428
}

static HRESULT WINAPI HTMLDocument_get_selection(IHTMLDocument2 *iface, IHTMLSelectionObject **p)
{
429
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
430 431
    nsISelection *nsselection;
    nsresult nsres;
432 433 434

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

435 436 437 438
    nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
    if(NS_FAILED(nsres)) {
        ERR("GetSelection failed: %08x\n", nsres);
        return E_FAIL;
439 440
    }

441
    return HTMLSelectionObject_Create(This->doc_node, nsselection, p);
442 443 444 445
}

static HRESULT WINAPI HTMLDocument_get_readyState(IHTMLDocument2 *iface, BSTR *p)
{
446
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

    static const WCHAR wszUninitialized[] = {'u','n','i','n','i','t','i','a','l','i','z','e','d',0};
    static const WCHAR wszLoading[] = {'l','o','a','d','i','n','g',0};
    static const WCHAR wszLoaded[] = {'l','o','a','d','e','d',0};
    static const WCHAR wszInteractive[] = {'i','n','t','e','r','a','c','t','i','v','e',0};
    static const WCHAR wszComplete[] = {'c','o','m','p','l','e','t','e',0};

    static const LPCWSTR readystate_str[] = {
        wszUninitialized,
        wszLoading,
        wszLoaded,
        wszInteractive,
        wszComplete
    };

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

    if(!p)
        return E_POINTER;

467
    *p = SysAllocString(readystate_str[This->window->readystate]);
468
    return S_OK;
469 470 471 472
}

static HRESULT WINAPI HTMLDocument_get_frames(IHTMLDocument2 *iface, IHTMLFramesCollection2 **p)
{
473
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
474
    FIXME("(%p)->(%p)\n", This, p);
475 476 477 478 479
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_embeds(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
480
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
481
    FIXME("(%p)->(%p)\n", This, p);
482 483 484 485 486
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_plugins(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
487
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
488
    FIXME("(%p)->(%p)\n", This, p);
489 490 491 492 493
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_alinkColor(IHTMLDocument2 *iface, VARIANT v)
{
494
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
495
    FIXME("(%p)\n", This);
496 497 498 499 500
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_alinkColor(IHTMLDocument2 *iface, VARIANT *p)
{
501
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
502
    FIXME("(%p)->(%p)\n", This, p);
503 504 505 506 507
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_bgColor(IHTMLDocument2 *iface, VARIANT v)
{
508
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
509
    FIXME("(%p)\n", This);
510 511 512 513 514
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_bgColor(IHTMLDocument2 *iface, VARIANT *p)
{
515
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
516
    FIXME("(%p)->(%p)\n", This, p);
517 518 519 520 521
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_fgColor(IHTMLDocument2 *iface, VARIANT v)
{
522
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
523
    FIXME("(%p)\n", This);
524 525 526 527 528
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fgColor(IHTMLDocument2 *iface, VARIANT *p)
{
529
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
530
    FIXME("(%p)->(%p)\n", This, p);
531 532 533 534 535
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_linkColor(IHTMLDocument2 *iface, VARIANT v)
{
536
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
537
    FIXME("(%p)->()\n", This);
538 539 540 541 542
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_linkColor(IHTMLDocument2 *iface, VARIANT *p)
{
543
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
544
    FIXME("(%p)->(%p)\n", This, p);
545 546 547 548 549
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_vlinkColor(IHTMLDocument2 *iface, VARIANT v)
{
550
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
551
    FIXME("(%p)\n", This);
552 553 554 555 556
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_vlinkColor(IHTMLDocument2 *iface, VARIANT *p)
{
557
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
558
    FIXME("(%p)->(%p)\n", This, p);
559 560 561 562 563
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_referrer(IHTMLDocument2 *iface, BSTR *p)
{
564
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
565

566
    FIXME("(%p)->(%p)\n", This, p);
567 568 569 570

    *p = NULL;
    return S_OK;
 }
571 572 573

static HRESULT WINAPI HTMLDocument_get_location(IHTMLDocument2 *iface, IHTMLLocation **p)
{
574
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
575 576 577

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

578 579 580 581 582
    if(!This->doc_node->nsdoc) {
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

583
    return IHTMLWindow2_get_location(&This->window->IHTMLWindow2_iface, p);
584 585 586 587
}

static HRESULT WINAPI HTMLDocument_get_lastModified(IHTMLDocument2 *iface, BSTR *p)
{
588
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
589
    FIXME("(%p)->(%p)\n", This, p);
590 591 592 593 594
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_URL(IHTMLDocument2 *iface, BSTR v)
{
595
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
596
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
597 598 599 600 601
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_URL(IHTMLDocument2 *iface, BSTR *p)
{
602
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
603 604 605 606 607 608

    static const WCHAR about_blank_url[] =
        {'a','b','o','u','t',':','b','l','a','n','k',0};

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

609
    *p = SysAllocString(This->window->url ? This->window->url : about_blank_url);
610
    return *p ? S_OK : E_OUTOFMEMORY;
611 612 613 614
}

static HRESULT WINAPI HTMLDocument_put_domain(IHTMLDocument2 *iface, BSTR v)
{
615
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
616
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
617 618 619 620 621
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_domain(IHTMLDocument2 *iface, BSTR *p)
{
622
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
623
    FIXME("(%p)->(%p)\n", This, p);
624 625 626 627 628
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_cookie(IHTMLDocument2 *iface, BSTR v)
{
629
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
630 631 632 633 634 635 636 637 638 639 640
    BOOL bret;

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

    bret = InternetSetCookieExW(This->window->url, NULL, v, 0, 0);
    if(!bret) {
        FIXME("InternetSetCookieExW failed: %u\n", GetLastError());
        return HRESULT_FROM_WIN32(GetLastError());
    }

    return S_OK;
641 642 643 644
}

static HRESULT WINAPI HTMLDocument_get_cookie(IHTMLDocument2 *iface, BSTR *p)
{
645
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    DWORD size;
    BOOL bret;

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

    size = 0;
    bret = InternetGetCookieExW(This->window->url, NULL, NULL, &size, 0, NULL);
    if(!bret) {
        switch(GetLastError()) {
        case ERROR_INSUFFICIENT_BUFFER:
            break;
        case ERROR_NO_MORE_ITEMS:
            *p = NULL;
            return S_OK;
        default:
            FIXME("InternetGetCookieExW failed: %u\n", GetLastError());
            return HRESULT_FROM_WIN32(GetLastError());
        }
    }

    if(!size) {
        *p = NULL;
        return S_OK;
    }

    *p = SysAllocStringLen(NULL, size-1);
    if(!*p)
        return E_OUTOFMEMORY;

    bret = InternetGetCookieExW(This->window->url, NULL, *p, &size, 0, NULL);
    if(!bret) {
        ERR("InternetGetCookieExW failed: %u\n", GetLastError());
        return E_FAIL;
    }

    return S_OK;
682 683 684 685
}

static HRESULT WINAPI HTMLDocument_put_expando(IHTMLDocument2 *iface, VARIANT_BOOL v)
{
686
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
687
    FIXME("(%p)->(%x)\n", This, v);
688 689 690 691 692
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_expando(IHTMLDocument2 *iface, VARIANT_BOOL *p)
{
693
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
694
    FIXME("(%p)->(%p)\n", This, p);
695 696 697 698 699
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_charset(IHTMLDocument2 *iface, BSTR v)
{
700
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
701
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
702 703 704 705 706
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_charset(IHTMLDocument2 *iface, BSTR *p)
{
707
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
708
    FIXME("(%p)->(%p)\n", This, p);
709 710 711 712 713
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_defaultCharset(IHTMLDocument2 *iface, BSTR v)
{
714
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
715
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
716 717 718 719 720
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_defaultCharset(IHTMLDocument2 *iface, BSTR *p)
{
721
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
722
    FIXME("(%p)->(%p)\n", This, p);
723 724 725 726 727
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_mimeType(IHTMLDocument2 *iface, BSTR *p)
{
728
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
729
    FIXME("(%p)->(%p)\n", This, p);
730 731 732 733 734
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fileSize(IHTMLDocument2 *iface, BSTR *p)
{
735
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
736
    FIXME("(%p)->(%p)\n", This, p);
737 738 739 740 741
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fileCreatedDate(IHTMLDocument2 *iface, BSTR *p)
{
742
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
743
    FIXME("(%p)->(%p)\n", This, p);
744 745 746 747 748
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fileModifiedDate(IHTMLDocument2 *iface, BSTR *p)
{
749
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
750
    FIXME("(%p)->(%p)\n", This, p);
751 752 753 754 755
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fileUpdatedDate(IHTMLDocument2 *iface, BSTR *p)
{
756
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
757
    FIXME("(%p)->(%p)\n", This, p);
758 759 760 761 762
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_security(IHTMLDocument2 *iface, BSTR *p)
{
763
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
764
    FIXME("(%p)->(%p)\n", This, p);
765 766 767 768 769
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_protocol(IHTMLDocument2 *iface, BSTR *p)
{
770
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
771
    FIXME("(%p)->(%p)\n", This, p);
772 773 774 775 776
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_nameProp(IHTMLDocument2 *iface, BSTR *p)
{
777
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
778
    FIXME("(%p)->(%p)\n", This, p);
779 780 781
    return E_NOTIMPL;
}

782
static HRESULT document_write(HTMLDocument *This, SAFEARRAY *psarray, BOOL ln)
783
{
784 785
    nsAString nsstr;
    VARIANT *var;
786
    ULONG i, argc;
787 788 789
    nsresult nsres;
    HRESULT hres;

790
    if(!This->doc_node->nsdoc) {
791 792 793 794
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

795 796 797
    if (!psarray)
        return S_OK;

798 799 800 801 802 803 804 805 806 807 808 809 810
    if(psarray->cDims != 1) {
        FIXME("cDims=%d\n", psarray->cDims);
        return E_INVALIDARG;
    }

    hres = SafeArrayAccessData(psarray, (void**)&var);
    if(FAILED(hres)) {
        WARN("SafeArrayAccessData failed: %08x\n", hres);
        return hres;
    }

    nsAString_Init(&nsstr, NULL);

811 812
    argc = psarray->rgsabound[0].cElements;
    for(i=0; i < argc; i++) {
813 814
        if(V_VT(var+i) == VT_BSTR) {
            nsAString_SetData(&nsstr, V_BSTR(var+i));
815
            if(!ln || i != argc-1)
816
                nsres = nsIDOMHTMLDocument_Write(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
817
            else
818
                nsres = nsIDOMHTMLDocument_Writeln(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
819 820 821 822 823 824 825 826 827 828 829
            if(NS_FAILED(nsres))
                ERR("Write failed: %08x\n", nsres);
        }else {
            FIXME("vt=%d\n", V_VT(var+i));
        }
    }

    nsAString_Finish(&nsstr);
    SafeArrayUnaccessData(psarray);

    return S_OK;
830 831
}

832 833
static HRESULT WINAPI HTMLDocument_write(IHTMLDocument2 *iface, SAFEARRAY *psarray)
{
834
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
835 836 837 838 839 840

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

    return document_write(This, psarray, FALSE);
}

841 842
static HRESULT WINAPI HTMLDocument_writeln(IHTMLDocument2 *iface, SAFEARRAY *psarray)
{
843
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
844 845 846 847

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

    return document_write(This, psarray, TRUE);
848 849 850 851 852
}

static HRESULT WINAPI HTMLDocument_open(IHTMLDocument2 *iface, BSTR url, VARIANT name,
                        VARIANT features, VARIANT replace, IDispatch **pomWindowResult)
{
853
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
854
    nsISupports *tmp;
855 856 857 858 859 860 861
    nsresult nsres;

    static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};

    TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_w(url), debugstr_variant(&name),
          debugstr_variant(&features), debugstr_variant(&replace), pomWindowResult);

862
    if(!This->doc_node->nsdoc) {
863 864 865 866 867 868 869 870
        ERR("!nsdoc\n");
        return E_NOTIMPL;
    }

    if(!url || strcmpW(url, text_htmlW) || V_VT(&name) != VT_ERROR
       || V_VT(&features) != VT_ERROR || V_VT(&replace) != VT_ERROR)
        FIXME("unsupported args\n");

871
    nsres = nsIDOMHTMLDocument_Open(This->doc_node->nsdoc, NULL, NULL, NULL, NULL, 0, &tmp);
872 873 874 875 876
    if(NS_FAILED(nsres)) {
        ERR("Open failed: %08x\n", nsres);
        return E_FAIL;
    }

877 878 879
    if(tmp)
        nsISupports_Release(tmp);

880 881
    *pomWindowResult = (IDispatch*)&This->window->IHTMLWindow2_iface;
    IHTMLWindow2_AddRef(&This->window->IHTMLWindow2_iface);
882
    return S_OK;
883 884 885 886
}

static HRESULT WINAPI HTMLDocument_close(IHTMLDocument2 *iface)
{
887
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
888 889 890 891
    nsresult nsres;

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

892
    if(!This->doc_node->nsdoc) {
893 894 895 896
        ERR("!nsdoc\n");
        return E_NOTIMPL;
    }

897
    nsres = nsIDOMHTMLDocument_Close(This->doc_node->nsdoc);
898 899 900 901 902 903
    if(NS_FAILED(nsres)) {
        ERR("Close failed: %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
904 905 906 907
}

static HRESULT WINAPI HTMLDocument_clear(IHTMLDocument2 *iface)
{
908
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
909 910 911 912
    nsresult nsres;

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

913
    nsres = nsIDOMHTMLDocument_Clear(This->doc_node->nsdoc);
914 915 916 917 918 919
    if(NS_FAILED(nsres)) {
        ERR("Clear failed: %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
920 921 922 923 924
}

static HRESULT WINAPI HTMLDocument_queryCommandSupported(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
925
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
926
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
927 928 929 930 931 932
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandEnabled(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
933
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
934
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
935 936 937 938 939 940
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandState(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
941
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
942
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
943 944 945 946 947 948
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandIndeterm(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
949
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
950
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
951 952 953 954 955 956
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandText(IHTMLDocument2 *iface, BSTR cmdID,
                                                        BSTR *pfRet)
{
957
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
958
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
959 960 961 962 963 964
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandValue(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT *pfRet)
{
965
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
966
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
967 968 969 970 971 972
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_execCommand(IHTMLDocument2 *iface, BSTR cmdID,
                                VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
{
973
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
974
    FIXME("(%p)->(%s %x %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
975 976 977 978 979 980
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_execCommandShowHelp(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
981
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
982
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
983 984 985 986
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_createElement(IHTMLDocument2 *iface, BSTR eTag,
987
                                                 IHTMLElement **newElem)
988
{
989
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
990
    nsIDOMHTMLElement *nselem;
991
    HTMLElement *elem;
992
    HRESULT hres;
993 994 995

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

996 997 998
    hres = create_nselem(This->doc_node, eTag, &nselem);
    if(FAILED(hres))
        return hres;
999

1000
    hres = HTMLElement_Create(This->doc_node, (nsIDOMNode*)nselem, TRUE, &elem);
1001
    nsIDOMHTMLElement_Release(nselem);
1002 1003
    if(FAILED(hres))
        return hres;
1004

1005 1006
    *newElem = &elem->IHTMLElement_iface;
    IHTMLElement_AddRef(&elem->IHTMLElement_iface);
1007
    return S_OK;
1008 1009 1010 1011
}

static HRESULT WINAPI HTMLDocument_put_onhelp(IHTMLDocument2 *iface, VARIANT v)
{
1012
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1013
    FIXME("(%p)\n", This);
1014 1015 1016 1017 1018
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onhelp(IHTMLDocument2 *iface, VARIANT *p)
{
1019
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1020
    FIXME("(%p)->(%p)\n", This, p);
1021 1022 1023 1024 1025
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onclick(IHTMLDocument2 *iface, VARIANT v)
{
1026
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1027 1028 1029 1030

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

    return set_doc_event(This, EVENTID_CLICK, &v);
1031 1032 1033 1034
}

static HRESULT WINAPI HTMLDocument_get_onclick(IHTMLDocument2 *iface, VARIANT *p)
{
1035
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1036 1037 1038 1039

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

    return get_doc_event(This, EVENTID_CLICK, p);
1040 1041 1042 1043
}

static HRESULT WINAPI HTMLDocument_put_ondblclick(IHTMLDocument2 *iface, VARIANT v)
{
1044
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1045
    FIXME("(%p)\n", This);
1046 1047 1048 1049 1050
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_ondblclick(IHTMLDocument2 *iface, VARIANT *p)
{
1051
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1052
    FIXME("(%p)->(%p)\n", This, p);
1053 1054 1055 1056 1057
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onkeyup(IHTMLDocument2 *iface, VARIANT v)
{
1058
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1059 1060 1061 1062

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

    return set_doc_event(This, EVENTID_KEYUP, &v);
1063 1064 1065 1066
}

static HRESULT WINAPI HTMLDocument_get_onkeyup(IHTMLDocument2 *iface, VARIANT *p)
{
1067
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1068 1069 1070 1071

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

    return get_doc_event(This, EVENTID_KEYUP, p);
1072 1073 1074 1075
}

static HRESULT WINAPI HTMLDocument_put_onkeydown(IHTMLDocument2 *iface, VARIANT v)
{
1076
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1077 1078 1079 1080

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

    return set_doc_event(This, EVENTID_KEYDOWN, &v);
1081 1082 1083 1084
}

static HRESULT WINAPI HTMLDocument_get_onkeydown(IHTMLDocument2 *iface, VARIANT *p)
{
1085
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1086 1087 1088 1089

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

    return get_doc_event(This, EVENTID_KEYDOWN, p);
1090 1091 1092 1093
}

static HRESULT WINAPI HTMLDocument_put_onkeypress(IHTMLDocument2 *iface, VARIANT v)
{
1094
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1095
    FIXME("(%p)\n", This);
1096 1097 1098 1099 1100
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onkeypress(IHTMLDocument2 *iface, VARIANT *p)
{
1101
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1102
    FIXME("(%p)->(%p)\n", This, p);
1103 1104 1105 1106 1107
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onmouseup(IHTMLDocument2 *iface, VARIANT v)
{
1108
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1109 1110 1111 1112

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

    return set_doc_event(This, EVENTID_MOUSEUP, &v);
1113 1114 1115 1116
}

static HRESULT WINAPI HTMLDocument_get_onmouseup(IHTMLDocument2 *iface, VARIANT *p)
{
1117
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1118 1119 1120 1121

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

    return get_doc_event(This, EVENTID_MOUSEUP, p);
1122 1123 1124 1125
}

static HRESULT WINAPI HTMLDocument_put_onmousedown(IHTMLDocument2 *iface, VARIANT v)
{
1126
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1127 1128 1129 1130

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

    return set_doc_event(This, EVENTID_MOUSEDOWN, &v);
1131 1132 1133 1134
}

static HRESULT WINAPI HTMLDocument_get_onmousedown(IHTMLDocument2 *iface, VARIANT *p)
{
1135
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1136 1137 1138 1139

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

    return get_doc_event(This, EVENTID_MOUSEDOWN, p);
1140 1141 1142 1143
}

static HRESULT WINAPI HTMLDocument_put_onmousemove(IHTMLDocument2 *iface, VARIANT v)
{
1144
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1145 1146 1147 1148

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

    return set_doc_event(This, EVENTID_MOUSEMOVE, &v);
1149 1150 1151 1152
}

static HRESULT WINAPI HTMLDocument_get_onmousemove(IHTMLDocument2 *iface, VARIANT *p)
{
1153
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1154 1155 1156 1157

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

    return get_doc_event(This, EVENTID_MOUSEMOVE, p);
1158 1159 1160 1161
}

static HRESULT WINAPI HTMLDocument_put_onmouseout(IHTMLDocument2 *iface, VARIANT v)
{
1162
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1163 1164 1165 1166

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

    return set_doc_event(This, EVENTID_MOUSEOUT, &v);
1167 1168 1169 1170
}

static HRESULT WINAPI HTMLDocument_get_onmouseout(IHTMLDocument2 *iface, VARIANT *p)
{
1171
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1172 1173 1174 1175

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

    return get_doc_event(This, EVENTID_MOUSEOUT, p);
1176 1177 1178 1179
}

static HRESULT WINAPI HTMLDocument_put_onmouseover(IHTMLDocument2 *iface, VARIANT v)
{
1180
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1181 1182 1183 1184

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

    return set_doc_event(This, EVENTID_MOUSEOVER, &v);
1185 1186 1187 1188
}

static HRESULT WINAPI HTMLDocument_get_onmouseover(IHTMLDocument2 *iface, VARIANT *p)
{
1189
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1190 1191 1192 1193

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

    return get_doc_event(This, EVENTID_MOUSEOVER, p);
1194 1195 1196 1197
}

static HRESULT WINAPI HTMLDocument_put_onreadystatechange(IHTMLDocument2 *iface, VARIANT v)
{
1198
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1199 1200 1201 1202

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

    return set_doc_event(This, EVENTID_READYSTATECHANGE, &v);
1203 1204 1205 1206
}

static HRESULT WINAPI HTMLDocument_get_onreadystatechange(IHTMLDocument2 *iface, VARIANT *p)
{
1207
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1208 1209 1210 1211

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

    return get_doc_event(This, EVENTID_READYSTATECHANGE, p);
1212 1213 1214 1215
}

static HRESULT WINAPI HTMLDocument_put_onafterupdate(IHTMLDocument2 *iface, VARIANT v)
{
1216
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1217
    FIXME("(%p)\n", This);
1218 1219 1220 1221 1222
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onafterupdate(IHTMLDocument2 *iface, VARIANT *p)
{
1223
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1224
    FIXME("(%p)->(%p)\n", This, p);
1225 1226 1227 1228 1229
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onrowexit(IHTMLDocument2 *iface, VARIANT v)
{
1230
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1231
    FIXME("(%p)\n", This);
1232 1233 1234 1235 1236
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onrowexit(IHTMLDocument2 *iface, VARIANT *p)
{
1237
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1238
    FIXME("(%p)->(%p)\n", This, p);
1239 1240 1241 1242 1243
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onrowenter(IHTMLDocument2 *iface, VARIANT v)
{
1244
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1245
    FIXME("(%p)\n", This);
1246 1247 1248 1249 1250
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onrowenter(IHTMLDocument2 *iface, VARIANT *p)
{
1251
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1252
    FIXME("(%p)->(%p)\n", This, p);
1253 1254 1255 1256 1257
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_ondragstart(IHTMLDocument2 *iface, VARIANT v)
{
1258
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1259 1260 1261 1262

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

    return set_doc_event(This, EVENTID_DRAGSTART, &v);
1263 1264 1265 1266
}

static HRESULT WINAPI HTMLDocument_get_ondragstart(IHTMLDocument2 *iface, VARIANT *p)
{
1267
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1268 1269 1270 1271

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

    return get_doc_event(This, EVENTID_DRAGSTART, p);
1272 1273 1274 1275
}

static HRESULT WINAPI HTMLDocument_put_onselectstart(IHTMLDocument2 *iface, VARIANT v)
{
1276
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1277 1278 1279 1280

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

    return set_doc_event(This, EVENTID_SELECTSTART, &v);
1281 1282 1283 1284
}

static HRESULT WINAPI HTMLDocument_get_onselectstart(IHTMLDocument2 *iface, VARIANT *p)
{
1285
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1286 1287 1288 1289

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

    return get_doc_event(This, EVENTID_SELECTSTART, p);
1290 1291
}

1292
static HRESULT WINAPI HTMLDocument_elementFromPoint(IHTMLDocument2 *iface, LONG x, LONG y,
1293 1294
                                                        IHTMLElement **elementHit)
{
1295
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1296 1297 1298 1299 1300 1301 1302
    nsIDOMElement *nselem;
    HTMLDOMNode *node;
    nsresult nsres;
    HRESULT hres;

    TRACE("(%p)->(%d %d %p)\n", This, x, y, elementHit);

1303
    nsres = nsIDOMHTMLDocument_ElementFromPoint(This->doc_node->nsdoc, x, y, &nselem);
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    if(NS_FAILED(nsres)) {
        ERR("ElementFromPoint failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(!nselem) {
        *elementHit = NULL;
        return S_OK;
    }

    hres = get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE, &node);
    nsIDOMElement_Release(nselem);
    if(FAILED(hres))
        return hres;

    return IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)elementHit);
1320 1321 1322 1323
}

static HRESULT WINAPI HTMLDocument_get_parentWindow(IHTMLDocument2 *iface, IHTMLWindow2 **p)
{
1324
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1325 1326 1327

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

1328
    *p = &This->window->IHTMLWindow2_iface;
1329 1330
    IHTMLWindow2_AddRef(*p);
    return S_OK;
1331 1332 1333
}

static HRESULT WINAPI HTMLDocument_get_styleSheets(IHTMLDocument2 *iface,
1334
                                                   IHTMLStyleSheetsCollection **p)
1335
{
1336
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1337 1338 1339 1340 1341 1342 1343
    nsIDOMStyleSheetList *nsstylelist;
    nsresult nsres;

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

    *p = NULL;

1344
    if(!This->doc_node->nsdoc) {
1345 1346
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
1347 1348
    }

1349
    nsres = nsIDOMHTMLDocument_GetStyleSheets(This->doc_node->nsdoc, &nsstylelist);
1350 1351 1352 1353
    if(NS_FAILED(nsres)) {
        ERR("GetStyleSheets failed: %08x\n", nsres);
        return E_FAIL;
    }
1354 1355

    *p = HTMLStyleSheetsCollection_Create(nsstylelist);
1356
    nsIDOMStyleSheetList_Release(nsstylelist);
1357 1358

    return S_OK;
1359 1360 1361 1362
}

static HRESULT WINAPI HTMLDocument_put_onbeforeupdate(IHTMLDocument2 *iface, VARIANT v)
{
1363
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1364
    FIXME("(%p)\n", This);
1365 1366 1367 1368 1369
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onbeforeupdate(IHTMLDocument2 *iface, VARIANT *p)
{
1370
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1371
    FIXME("(%p)->(%p)\n", This, p);
1372 1373 1374 1375 1376
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onerrorupdate(IHTMLDocument2 *iface, VARIANT v)
{
1377
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1378
    FIXME("(%p)\n", This);
1379 1380 1381 1382 1383
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onerrorupdate(IHTMLDocument2 *iface, VARIANT *p)
{
1384
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1385
    FIXME("(%p)->(%p)\n", This, p);
1386 1387 1388 1389 1390
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_toString(IHTMLDocument2 *iface, BSTR *String)
{
1391
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1392
    FIXME("(%p)->(%p)\n", This, String);
1393 1394 1395 1396
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_createStyleSheet(IHTMLDocument2 *iface, BSTR bstrHref,
1397
                                            LONG lIndex, IHTMLStyleSheet **ppnewStyleSheet)
1398
{
1399
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1400

1401
    FIXME("(%p)->(%s %d %p) semi-stub\n", This, debugstr_w(bstrHref), lIndex, ppnewStyleSheet);
1402

1403
    *ppnewStyleSheet = HTMLStyleSheet_Create(NULL);
1404
    return S_OK;
1405 1406
}

1407
static const IHTMLDocument2Vtbl HTMLDocumentVtbl = {
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 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
    HTMLDocument_QueryInterface,
    HTMLDocument_AddRef,
    HTMLDocument_Release,
    HTMLDocument_GetTypeInfoCount,
    HTMLDocument_GetTypeInfo,
    HTMLDocument_GetIDsOfNames,
    HTMLDocument_Invoke,
    HTMLDocument_get_Script,
    HTMLDocument_get_all,
    HTMLDocument_get_body,
    HTMLDocument_get_activeElement,
    HTMLDocument_get_images,
    HTMLDocument_get_applets,
    HTMLDocument_get_links,
    HTMLDocument_get_forms,
    HTMLDocument_get_anchors,
    HTMLDocument_put_title,
    HTMLDocument_get_title,
    HTMLDocument_get_scripts,
    HTMLDocument_put_designMode,
    HTMLDocument_get_designMode,
    HTMLDocument_get_selection,
    HTMLDocument_get_readyState,
    HTMLDocument_get_frames,
    HTMLDocument_get_embeds,
    HTMLDocument_get_plugins,
    HTMLDocument_put_alinkColor,
    HTMLDocument_get_alinkColor,
    HTMLDocument_put_bgColor,
    HTMLDocument_get_bgColor,
    HTMLDocument_put_fgColor,
    HTMLDocument_get_fgColor,
    HTMLDocument_put_linkColor,
    HTMLDocument_get_linkColor,
    HTMLDocument_put_vlinkColor,
    HTMLDocument_get_vlinkColor,
    HTMLDocument_get_referrer,
    HTMLDocument_get_location,
    HTMLDocument_get_lastModified,
    HTMLDocument_put_URL,
    HTMLDocument_get_URL,
    HTMLDocument_put_domain,
    HTMLDocument_get_domain,
    HTMLDocument_put_cookie,
    HTMLDocument_get_cookie,
    HTMLDocument_put_expando,
    HTMLDocument_get_expando,
    HTMLDocument_put_charset,
    HTMLDocument_get_charset,
    HTMLDocument_put_defaultCharset,
    HTMLDocument_get_defaultCharset,
    HTMLDocument_get_mimeType,
    HTMLDocument_get_fileSize,
    HTMLDocument_get_fileCreatedDate,
    HTMLDocument_get_fileModifiedDate,
    HTMLDocument_get_fileUpdatedDate,
    HTMLDocument_get_security,
    HTMLDocument_get_protocol,
    HTMLDocument_get_nameProp,
    HTMLDocument_write,
    HTMLDocument_writeln,
    HTMLDocument_open,
    HTMLDocument_close,
    HTMLDocument_clear,
    HTMLDocument_queryCommandSupported,
    HTMLDocument_queryCommandEnabled,
    HTMLDocument_queryCommandState,
    HTMLDocument_queryCommandIndeterm,
    HTMLDocument_queryCommandText,
    HTMLDocument_queryCommandValue,
    HTMLDocument_execCommand,
    HTMLDocument_execCommandShowHelp,
    HTMLDocument_createElement,
    HTMLDocument_put_onhelp,
    HTMLDocument_get_onhelp,
    HTMLDocument_put_onclick,
    HTMLDocument_get_onclick,
    HTMLDocument_put_ondblclick,
    HTMLDocument_get_ondblclick,
    HTMLDocument_put_onkeyup,
    HTMLDocument_get_onkeyup,
    HTMLDocument_put_onkeydown,
    HTMLDocument_get_onkeydown,
    HTMLDocument_put_onkeypress,
    HTMLDocument_get_onkeypress,
    HTMLDocument_put_onmouseup,
    HTMLDocument_get_onmouseup,
    HTMLDocument_put_onmousedown,
    HTMLDocument_get_onmousedown,
    HTMLDocument_put_onmousemove,
    HTMLDocument_get_onmousemove,
    HTMLDocument_put_onmouseout,
    HTMLDocument_get_onmouseout,
    HTMLDocument_put_onmouseover,
    HTMLDocument_get_onmouseover,
    HTMLDocument_put_onreadystatechange,
    HTMLDocument_get_onreadystatechange,
    HTMLDocument_put_onafterupdate,
    HTMLDocument_get_onafterupdate,
    HTMLDocument_put_onrowexit,
    HTMLDocument_get_onrowexit,
    HTMLDocument_put_onrowenter,
    HTMLDocument_get_onrowenter,
    HTMLDocument_put_ondragstart,
    HTMLDocument_get_ondragstart,
    HTMLDocument_put_onselectstart,
    HTMLDocument_get_onselectstart,
    HTMLDocument_elementFromPoint,
    HTMLDocument_get_parentWindow,
    HTMLDocument_get_styleSheets,
    HTMLDocument_put_onbeforeupdate,
    HTMLDocument_get_onbeforeupdate,
    HTMLDocument_put_onerrorupdate,
    HTMLDocument_get_onerrorupdate,
    HTMLDocument_toString,
    HTMLDocument_createStyleSheet
};

1526 1527
static void HTMLDocument_on_advise(IUnknown *iface, cp_static_data_t *cp)
{
1528
    HTMLDocument *This = impl_from_IHTMLDocument2((IHTMLDocument2*)iface);
1529 1530

    if(This->window)
1531
        update_cp_events(This->window, &This->doc_node->node.event_target, cp, This->doc_node->node.nsnode);
1532 1533
}

1534 1535 1536 1537
static inline HTMLDocument *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocument, ISupportErrorInfo_iface);
}
1538 1539 1540

static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **ppv)
{
1541
    HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1542
    return htmldoc_query_interface(This, riid, ppv);
1543 1544 1545 1546
}

static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
{
1547
    HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1548
    return htmldoc_addref(This);
1549 1550 1551 1552
}

static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
{
1553
    HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1554
    return htmldoc_release(This);
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
}

static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
{
    FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
    return S_FALSE;
}

static const ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
    SupportErrorInfo_QueryInterface,
    SupportErrorInfo_AddRef,
    SupportErrorInfo_Release,
    SupportErrorInfo_InterfaceSupportsErrorInfo
};

1570 1571 1572 1573
static inline HTMLDocument *impl_from_IDispatchEx(IDispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocument, IDispatchEx_iface);
}
1574

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
static HRESULT dispid_from_elem_name(HTMLDocumentNode *This, BSTR name, DISPID *dispid)
{
    nsIDOMNodeList *node_list;
    nsAString name_str;
    PRUint32 len;
    unsigned i;
    nsresult nsres;

    if(!This->nsdoc)
        return DISP_E_UNKNOWNNAME;

    nsAString_InitDepend(&name_str, name);
    nsres = nsIDOMHTMLDocument_GetElementsByName(This->nsdoc, &name_str, &node_list);
    nsAString_Finish(&name_str);
    if(NS_FAILED(nsres))
        return E_FAIL;

    nsres = nsIDOMNodeList_GetLength(node_list, &len);
    nsIDOMNodeList_Release(node_list);
    if(NS_FAILED(nsres))
        return E_FAIL;

    if(!len)
        return DISP_E_UNKNOWNNAME;

    for(i=0; i < This->elem_vars_cnt; i++) {
        if(!strcmpW(name, This->elem_vars[i])) {
            *dispid = MSHTML_DISPID_CUSTOM_MIN+i;
            return S_OK;
        }
    }

    if(This->elem_vars_cnt == This->elem_vars_size) {
        WCHAR **new_vars;

        if(This->elem_vars_size) {
            new_vars = heap_realloc(This->elem_vars, This->elem_vars_size*2*sizeof(WCHAR*));
            if(!new_vars)
                return E_OUTOFMEMORY;
            This->elem_vars_size *= 2;
        }else {
            new_vars = heap_alloc(16*sizeof(WCHAR*));
            if(!new_vars)
                return E_OUTOFMEMORY;
            This->elem_vars_size = 16;
        }

        This->elem_vars = new_vars;
    }

    This->elem_vars[This->elem_vars_cnt] = heap_strdupW(name);
    if(!This->elem_vars[This->elem_vars_cnt])
        return E_OUTOFMEMORY;

    *dispid = MSHTML_DISPID_CUSTOM_MIN+This->elem_vars_cnt++;
    return S_OK;
}

1633 1634
static HRESULT WINAPI DocDispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
{
1635
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1636

1637
    return htmldoc_query_interface(This, riid, ppv);
1638 1639 1640 1641
}

static ULONG WINAPI DocDispatchEx_AddRef(IDispatchEx *iface)
{
1642
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1643

1644
    return htmldoc_addref(This);
1645 1646 1647 1648
}

static ULONG WINAPI DocDispatchEx_Release(IDispatchEx *iface)
{
1649
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1650

1651
    return htmldoc_release(This);
1652 1653 1654 1655
}

static HRESULT WINAPI DocDispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
{
1656
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1657

1658
    return IDispatchEx_GetTypeInfoCount(This->dispex, pctinfo);
1659 1660 1661 1662 1663
}

static HRESULT WINAPI DocDispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
                                               LCID lcid, ITypeInfo **ppTInfo)
{
1664
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1665

1666
    return IDispatchEx_GetTypeInfo(This->dispex, iTInfo, lcid, ppTInfo);
1667 1668 1669 1670 1671 1672
}

static HRESULT WINAPI DocDispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
                                                 LPOLESTR *rgszNames, UINT cNames,
                                                 LCID lcid, DISPID *rgDispId)
{
1673
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1674

1675
    return IDispatchEx_GetIDsOfNames(This->dispex, riid, rgszNames, cNames, lcid, rgDispId);
1676 1677 1678 1679 1680 1681
}

static HRESULT WINAPI DocDispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
1682
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    switch(dispIdMember) {
    case DISPID_READYSTATE:
        TRACE("DISPID_READYSTATE\n");

        if(!(wFlags & DISPATCH_PROPERTYGET))
            return E_INVALIDARG;

        V_VT(pVarResult) = VT_I4;
1695
        V_I4(pVarResult) = This->window->readystate;
1696 1697 1698
        return S_OK;
    }

1699
    return IDispatchEx_Invoke(This->dispex, dispIdMember, riid, lcid, wFlags, pDispParams,
1700 1701 1702 1703 1704
                              pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI DocDispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
{
1705
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1706
    HRESULT hres;
1707

1708 1709 1710 1711 1712
    hres = IDispatchEx_GetDispID(This->dispex, bstrName, grfdex, pid);
    if(hres != DISP_E_UNKNOWNNAME)
        return hres;

    return  dispid_from_elem_name(This->doc_node, bstrName, pid);
1713 1714 1715 1716 1717
}

static HRESULT WINAPI DocDispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
        VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
{
1718
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1719

1720
    if(This->window && id == DISPID_IHTMLDOCUMENT2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT))
1721 1722
        return IDispatchEx_InvokeEx(&This->window->IDispatchEx_iface, DISPID_IHTMLWINDOW2_LOCATION,
                lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1723 1724


1725
    return IDispatchEx_InvokeEx(This->dispex, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1726 1727 1728 1729
}

static HRESULT WINAPI DocDispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
{
1730
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1731

1732
    return IDispatchEx_DeleteMemberByName(This->dispex, bstrName, grfdex);
1733 1734 1735 1736
}

static HRESULT WINAPI DocDispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
{
1737
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1738

1739
    return IDispatchEx_DeleteMemberByDispID(This->dispex, id);
1740 1741 1742 1743
}

static HRESULT WINAPI DocDispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
{
1744
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1745

1746
    return IDispatchEx_GetMemberProperties(This->dispex, id, grfdexFetch, pgrfdex);
1747 1748 1749 1750
}

static HRESULT WINAPI DocDispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
{
1751
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1752

1753
    return IDispatchEx_GetMemberName(This->dispex, id, pbstrName);
1754 1755 1756 1757
}

static HRESULT WINAPI DocDispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
{
1758
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1759

1760
    return IDispatchEx_GetNextDispID(This->dispex, grfdex, id, pid);
1761 1762 1763 1764
}

static HRESULT WINAPI DocDispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
{
1765
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1766

1767
    return IDispatchEx_GetNameSpaceParent(This->dispex, ppunk);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
}

static const IDispatchExVtbl DocDispatchExVtbl = {
    DocDispatchEx_QueryInterface,
    DocDispatchEx_AddRef,
    DocDispatchEx_Release,
    DocDispatchEx_GetTypeInfoCount,
    DocDispatchEx_GetTypeInfo,
    DocDispatchEx_GetIDsOfNames,
    DocDispatchEx_Invoke,
    DocDispatchEx_GetDispID,
    DocDispatchEx_InvokeEx,
    DocDispatchEx_DeleteMemberByName,
    DocDispatchEx_DeleteMemberByDispID,
    DocDispatchEx_GetMemberProperties,
    DocDispatchEx_GetMemberName,
    DocDispatchEx_GetNextDispID,
    DocDispatchEx_GetNameSpaceParent
};

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
static inline HTMLDocument *impl_from_IProvideClassInfo(IProvideClassInfo *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocument, IProvideClassInfo_iface);
}

static HRESULT WINAPI ProvideClassInfo_QueryInterface(IProvideClassInfo *iface,
        REFIID riid, void **ppv)
{
    HTMLDocument *This = impl_from_IProvideClassInfo(iface);
    return htmldoc_query_interface(This, riid, ppv);
}

static ULONG WINAPI ProvideClassInfo_AddRef(IProvideClassInfo *iface)
{
    HTMLDocument *This = impl_from_IProvideClassInfo(iface);
    return htmldoc_addref(This);
}

static ULONG WINAPI ProvideClassInfo_Release(IProvideClassInfo *iface)
{
    HTMLDocument *This = impl_from_IProvideClassInfo(iface);
    return htmldoc_release(This);
}

static HRESULT WINAPI ProvideClassInfo_GetClassInfo(IProvideClassInfo* iface,
        ITypeInfo **ppTI)
{
    HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1816 1817
    TRACE("(%p)->(%p)\n", This, ppTI);
    return get_htmldoc_classinfo(ppTI);
1818 1819 1820 1821 1822 1823 1824 1825 1826
}

static const IProvideClassInfoVtbl ProvideClassInfoVtbl = {
    ProvideClassInfo_QueryInterface,
    ProvideClassInfo_AddRef,
    ProvideClassInfo_Release,
    ProvideClassInfo_GetClassInfo
};

1827
static BOOL htmldoc_qi(HTMLDocument *This, REFIID riid, void **ppv)
1828 1829 1830 1831 1832
{
    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
1833
        *ppv = &This->IHTMLDocument2_iface;
1834 1835
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch, %p)\n", This, ppv);
1836
        *ppv = &This->IDispatchEx_iface;
1837 1838
    }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
        TRACE("(%p)->(IID_IDispatchEx, %p)\n", This, ppv);
1839
        *ppv = &This->IDispatchEx_iface;
1840 1841
    }else if(IsEqualGUID(&IID_IHTMLDocument, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument, %p)\n", This, ppv);
1842
        *ppv = &This->IHTMLDocument2_iface;
1843 1844
    }else if(IsEqualGUID(&IID_IHTMLDocument2, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument2, %p)\n", This, ppv);
1845
        *ppv = &This->IHTMLDocument2_iface;
1846 1847
    }else if(IsEqualGUID(&IID_IHTMLDocument3, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument3, %p)\n", This, ppv);
1848
        *ppv = &This->IHTMLDocument3_iface;
1849 1850
    }else if(IsEqualGUID(&IID_IHTMLDocument4, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument4, %p)\n", This, ppv);
1851
        *ppv = &This->IHTMLDocument4_iface;
1852 1853
    }else if(IsEqualGUID(&IID_IHTMLDocument5, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument5, %p)\n", This, ppv);
1854
        *ppv = &This->IHTMLDocument5_iface;
1855 1856
    }else if(IsEqualGUID(&IID_IHTMLDocument6, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument6, %p)\n", This, ppv);
1857
        *ppv = &This->IHTMLDocument6_iface;
1858 1859
    }else if(IsEqualGUID(&IID_IPersist, riid)) {
        TRACE("(%p)->(IID_IPersist, %p)\n", This, ppv);
1860
        *ppv = &This->IPersistFile_iface;
1861 1862
    }else if(IsEqualGUID(&IID_IPersistMoniker, riid)) {
        TRACE("(%p)->(IID_IPersistMoniker, %p)\n", This, ppv);
1863
        *ppv = &This->IPersistMoniker_iface;
1864 1865
    }else if(IsEqualGUID(&IID_IPersistFile, riid)) {
        TRACE("(%p)->(IID_IPersistFile, %p)\n", This, ppv);
1866
        *ppv = &This->IPersistFile_iface;
1867 1868
    }else if(IsEqualGUID(&IID_IMonikerProp, riid)) {
        TRACE("(%p)->(IID_IMonikerProp, %p)\n", This, ppv);
1869
        *ppv = &This->IMonikerProp_iface;
1870 1871
    }else if(IsEqualGUID(&IID_IOleObject, riid)) {
        TRACE("(%p)->(IID_IOleObject, %p)\n", This, ppv);
1872
        *ppv = &This->IOleObject_iface;
1873 1874
    }else if(IsEqualGUID(&IID_IOleDocument, riid)) {
        TRACE("(%p)->(IID_IOleDocument, %p)\n", This, ppv);
1875
        *ppv = &This->IOleDocument_iface;
1876 1877
    }else if(IsEqualGUID(&IID_IOleDocumentView, riid)) {
        TRACE("(%p)->(IID_IOleDocumentView, %p)\n", This, ppv);
1878
        *ppv = &This->IOleDocumentView_iface;
1879 1880
    }else if(IsEqualGUID(&IID_IOleInPlaceActiveObject, riid)) {
        TRACE("(%p)->(IID_IOleInPlaceActiveObject, %p)\n", This, ppv);
1881
        *ppv = &This->IOleInPlaceActiveObject_iface;
1882 1883
    }else if(IsEqualGUID(&IID_IViewObject, riid)) {
        TRACE("(%p)->(IID_IViewObject, %p)\n", This, ppv);
1884
        *ppv = &This->IViewObjectEx_iface;
1885 1886
    }else if(IsEqualGUID(&IID_IViewObject2, riid)) {
        TRACE("(%p)->(IID_IViewObject2, %p)\n", This, ppv);
1887
        *ppv = &This->IViewObjectEx_iface;
1888 1889
    }else if(IsEqualGUID(&IID_IViewObjectEx, riid)) {
        TRACE("(%p)->(IID_IViewObjectEx, %p)\n", This, ppv);
1890
        *ppv = &This->IViewObjectEx_iface;
1891 1892
    }else if(IsEqualGUID(&IID_IOleWindow, riid)) {
        TRACE("(%p)->(IID_IOleWindow, %p)\n", This, ppv);
1893
        *ppv = &This->IOleInPlaceActiveObject_iface;
1894 1895
    }else if(IsEqualGUID(&IID_IOleInPlaceObject, riid)) {
        TRACE("(%p)->(IID_IOleInPlaceObject, %p)\n", This, ppv);
1896
        *ppv = &This->IOleInPlaceObjectWindowless_iface;
1897 1898
    }else if(IsEqualGUID(&IID_IOleInPlaceObjectWindowless, riid)) {
        TRACE("(%p)->(IID_IOleInPlaceObjectWindowless, %p)\n", This, ppv);
1899
        *ppv = &This->IOleInPlaceObjectWindowless_iface;
1900 1901
    }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
        TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
1902
        *ppv = &This->IServiceProvider_iface;
1903 1904
    }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
        TRACE("(%p)->(IID_IOleCommandTarget, %p)\n", This, ppv);
1905
        *ppv = &This->IOleCommandTarget_iface;
1906 1907
    }else if(IsEqualGUID(&IID_IOleControl, riid)) {
        TRACE("(%p)->(IID_IOleControl, %p)\n", This, ppv);
1908
        *ppv = &This->IOleControl_iface;
1909 1910
    }else if(IsEqualGUID(&IID_IHlinkTarget, riid)) {
        TRACE("(%p)->(IID_IHlinkTarget, %p)\n", This, ppv);
1911
        *ppv = &This->IHlinkTarget_iface;
1912 1913
    }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
        TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1914
        *ppv = &This->cp_container.IConnectionPointContainer_iface;
1915 1916
    }else if(IsEqualGUID(&IID_IPersistStreamInit, riid)) {
        TRACE("(%p)->(IID_IPersistStreamInit %p)\n", This, ppv);
1917
        *ppv = &This->IPersistStreamInit_iface;
1918 1919
    }else if(IsEqualGUID(&DIID_DispHTMLDocument, riid)) {
        TRACE("(%p)->(DIID_DispHTMLDocument %p)\n", This, ppv);
1920
        *ppv = &This->IHTMLDocument2_iface;
1921 1922
    }else if(IsEqualGUID(&IID_ISupportErrorInfo, riid)) {
        TRACE("(%p)->(IID_ISupportErrorInfo %p)\n", This, ppv);
1923
        *ppv = &This->ISupportErrorInfo_iface;
1924 1925
    }else if(IsEqualGUID(&IID_IPersistHistory, riid)) {
        TRACE("(%p)->(IID_IPersistHistory %p)\n", This, ppv);
1926
        *ppv = &This->IPersistHistory_iface;
1927 1928
    }else if(IsEqualGUID(&CLSID_CMarkup, riid)) {
        FIXME("(%p)->(CLSID_CMarkup %p)\n", This, ppv);
1929
        *ppv = NULL;
1930 1931
    }else if(IsEqualGUID(&IID_IRunnableObject, riid)) {
        TRACE("(%p)->(IID_IRunnableObject %p) returning NULL\n", This, ppv);
1932
        *ppv = NULL;
1933 1934
    }else if(IsEqualGUID(&IID_IPersistPropertyBag, riid)) {
        TRACE("(%p)->(IID_IPersistPropertyBag %p) returning NULL\n", This, ppv);
1935
        *ppv = NULL;
1936 1937
    }else if(IsEqualGUID(&IID_IMarshal, riid)) {
        TRACE("(%p)->(IID_IMarshal %p) returning NULL\n", This, ppv);
1938
        *ppv = NULL;
1939 1940 1941
    }else if(IsEqualGUID(&IID_IExternalConnection, riid)) {
        TRACE("(%p)->(IID_IExternalConnection %p) returning NULL\n", This, ppv);
        *ppv = NULL;
1942 1943 1944
    }else if(IsEqualGUID(&IID_IStdMarshalInfo, riid)) {
        TRACE("(%p)->(IID_IStdMarshalInfo %p) returning NULL\n", This, ppv);
        *ppv = NULL;
1945 1946
    }else if(IsEqualGUID(&IID_IObjectWithSite, riid)) {
        TRACE("(%p)->(IID_IObjectWithSite %p)\n", This, ppv);
1947
        *ppv = &This->IObjectWithSite_iface;
1948 1949 1950
    }else if(IsEqualGUID(&IID_IOleContainer, riid)) {
        TRACE("(%p)->(IID_IOleContainer %p)\n", This, ppv);
        *ppv = &This->IOleContainer_iface;
1951 1952 1953
    }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
        TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
        *ppv = &This->IObjectSafety_iface;
1954 1955 1956
    }else if(IsEqualGUID(&IID_IProvideClassInfo, riid)) {
        TRACE("(%p)->(IID_IProvideClassInfo, %p)\n", This, ppv);
        *ppv = &This->IProvideClassInfo_iface;
1957
    }else {
1958
        return FALSE;
1959 1960
    }

1961 1962 1963
    if(*ppv)
        IUnknown_AddRef((IUnknown*)*ppv);
    return TRUE;
1964 1965
}

1966
static cp_static_data_t HTMLDocumentEvents_data = { HTMLDocumentEvents_tid, HTMLDocument_on_advise };
1967

1968
static void init_doc(HTMLDocument *doc, IUnknown *unk_impl, IDispatchEx *dispex)
1969
{
1970
    doc->IHTMLDocument2_iface.lpVtbl = &HTMLDocumentVtbl;
1971
    doc->IDispatchEx_iface.lpVtbl = &DocDispatchExVtbl;
1972
    doc->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
1973
    doc->IProvideClassInfo_iface.lpVtbl = &ProvideClassInfoVtbl;
1974

1975
    doc->unk_impl = unk_impl;
1976
    doc->dispex = dispex;
1977
    doc->task_magic = get_task_target_magic();
1978

1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
    HTMLDocument_HTMLDocument3_Init(doc);
    HTMLDocument_HTMLDocument5_Init(doc);
    HTMLDocument_Persist_Init(doc);
    HTMLDocument_OleCmd_Init(doc);
    HTMLDocument_OleObj_Init(doc);
    HTMLDocument_View_Init(doc);
    HTMLDocument_Window_Init(doc);
    HTMLDocument_Service_Init(doc);
    HTMLDocument_Hlink_Init(doc);

1989
    ConnectionPointContainer_Init(&doc->cp_container, (IUnknown*)&doc->IHTMLDocument2_iface);
1990
    ConnectionPoint_Init(&doc->cp_dispatch, &doc->cp_container, &IID_IDispatch, &HTMLDocumentEvents_data);
1991 1992 1993
    ConnectionPoint_Init(&doc->cp_propnotif, &doc->cp_container, &IID_IPropertyNotifySink, NULL);
    ConnectionPoint_Init(&doc->cp_htmldocevents, &doc->cp_container, &DIID_HTMLDocumentEvents, &HTMLDocumentEvents_data);
    ConnectionPoint_Init(&doc->cp_htmldocevents2, &doc->cp_container, &DIID_HTMLDocumentEvents2, NULL);
1994
}
1995

1996 1997
static void destroy_htmldoc(HTMLDocument *This)
{
1998
    remove_target_tasks(This->task_magic);
1999 2000 2001 2002

    ConnectionPointContainer_Destroy(&This->cp_container);
}

2003 2004 2005 2006
static inline HTMLDocumentNode *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocumentNode, node);
}
2007 2008 2009

static HRESULT HTMLDocumentNode_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
{
2010
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2011 2012 2013 2014

    if(htmldoc_qi(&This->basedoc, riid, ppv))
        return *ppv ? S_OK : E_NOINTERFACE;

2015 2016
    if(IsEqualGUID(&IID_IInternetHostSecurityManager, riid)) {
        TRACE("(%p)->(IID_IInternetHostSecurityManager %p)\n", This, ppv);
2017
        *ppv = &This->IInternetHostSecurityManager_iface;
2018 2019 2020 2021 2022 2023
    }else {
        return HTMLDOMNode_QI(&This->node, riid, ppv);
    }

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

2026
static void HTMLDocumentNode_destructor(HTMLDOMNode *iface)
2027
{
2028
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2029 2030 2031 2032 2033
    unsigned i;

    for(i=0; i < This->elem_vars_cnt; i++)
        heap_free(This->elem_vars[i]);
    heap_free(This->elem_vars);
2034

2035
    detach_events(This);
2036 2037
    if(This->body_event_target)
        release_event_target(This->body_event_target);
2038 2039
    if(This->nsevent_listener)
        release_nsevents(This);
2040 2041
    if(This->catmgr)
        ICatInformation_Release(This->catmgr);
2042

2043 2044 2045
    detach_selection(This);
    detach_ranges(This);
    release_nodes(This);
2046

2047 2048 2049
    while(!list_empty(&This->plugin_hosts))
        detach_plugin_host(LIST_ENTRY(list_head(&This->plugin_hosts), PluginHost, entry));

2050
    if(This->nsdoc) {
2051
        release_document_mutation(This);
2052
        nsIDOMHTMLDocument_Release(This->nsdoc);
2053
    }
2054

2055
    heap_free(This->event_vector);
2056 2057 2058
    destroy_htmldoc(&This->basedoc);
}

2059 2060
static HRESULT HTMLDocumentNode_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
{
2061
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2062 2063 2064 2065
    FIXME("%p\n", This);
    return E_NOTIMPL;
}

2066 2067
static const NodeImplVtbl HTMLDocumentNodeImplVtbl = {
    HTMLDocumentNode_QI,
2068 2069
    HTMLDocumentNode_destructor,
    HTMLDocumentNode_clone
2070 2071
};

2072 2073
static HRESULT HTMLDocumentFragment_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
{
2074
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
    HTMLDocumentNode *new_node;
    HRESULT hres;

    hres = create_document_fragment(nsnode, This->node.doc, &new_node);
    if(FAILED(hres))
        return hres;

    *ret = &new_node->node;
    return S_OK;
}

2086 2087 2088 2089 2090 2091 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 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
static inline HTMLDocumentNode *impl_from_DispatchEx(DispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocumentNode, node.dispex);
}

static HRESULT HTMLDocumentNode_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
        VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
{
    HTMLDocumentNode *This = impl_from_DispatchEx(dispex);
    nsIDOMNodeList *node_list;
    nsAString name_str;
    nsIDOMNode *nsnode;
    HTMLDOMNode *node;
    unsigned i;
    nsresult nsres;
    HRESULT hres;

    if(flags != DISPATCH_PROPERTYGET) {
        FIXME("unsupported flags %x\n", flags);
        return E_NOTIMPL;
    }

    i = id - MSHTML_DISPID_CUSTOM_MIN;

    if(!This->nsdoc || i >= This->elem_vars_cnt)
        return DISP_E_UNKNOWNNAME;

    nsAString_InitDepend(&name_str, This->elem_vars[i]);
    nsres = nsIDOMHTMLDocument_GetElementsByName(This->nsdoc, &name_str, &node_list);
    nsAString_Finish(&name_str);
    if(NS_FAILED(nsres))
        return E_FAIL;

    nsres = nsIDOMNodeList_Item(node_list, 0, &nsnode);
    nsIDOMNodeList_Release(node_list);
    if(NS_FAILED(nsres) || !nsnode)
        return DISP_E_UNKNOWNNAME;

    hres = get_node(This, nsnode, TRUE, &node);
    if(FAILED(hres))
        return hres;

    IHTMLDOMNode_AddRef(&node->IHTMLDOMNode_iface);
    V_VT(res) = VT_DISPATCH;
    V_DISPATCH(res) = (IDispatch*)&node->IHTMLDOMNode_iface;
    return S_OK;
}


static const dispex_static_data_vtbl_t HTMLDocumentNode_dispex_vtbl = {
    NULL,
    NULL,
2138 2139
    HTMLDocumentNode_invoke,
    NULL
2140 2141
};

2142 2143 2144 2145 2146 2147
static const NodeImplVtbl HTMLDocumentFragmentImplVtbl = {
    HTMLDocumentNode_QI,
    HTMLDocumentNode_destructor,
    HTMLDocumentFragment_clone
};

2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
static const tid_t HTMLDocumentNode_iface_tids[] = {
    IHTMLDOMNode_tid,
    IHTMLDOMNode2_tid,
    IHTMLDocument2_tid,
    IHTMLDocument3_tid,
    IHTMLDocument4_tid,
    IHTMLDocument5_tid,
    0
};

static dispex_static_data_t HTMLDocumentNode_dispex = {
2159
    &HTMLDocumentNode_dispex_vtbl,
2160 2161 2162 2163 2164
    DispHTMLDocument_tid,
    NULL,
    HTMLDocumentNode_iface_tids
};

2165
static HTMLDocumentNode *alloc_doc_node(HTMLDocumentObj *doc_obj, HTMLWindow *window)
2166 2167
{
    HTMLDocumentNode *doc;
2168

2169 2170
    doc = heap_alloc_zero(sizeof(HTMLDocumentNode));
    if(!doc)
2171
        return NULL;
2172

2173
    doc->ref = 1;
2174 2175
    doc->basedoc.doc_node = doc;
    doc->basedoc.doc_obj = doc_obj;
2176
    doc->basedoc.window = window;
2177

2178 2179 2180
    init_dispex(&doc->node.dispex, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
            &HTMLDocumentNode_dispex);
    init_doc(&doc->basedoc, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
2181
            &doc->node.dispex.IDispatchEx_iface);
2182
    HTMLDocumentNode_SecMgr_Init(doc);
2183

2184 2185 2186
    list_init(&doc->bindings);
    list_init(&doc->selection_list);
    list_init(&doc->range_list);
2187
    list_init(&doc->plugin_hosts);
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199

    return doc;
}

HRESULT create_doc_from_nsdoc(nsIDOMHTMLDocument *nsdoc, HTMLDocumentObj *doc_obj, HTMLWindow *window, HTMLDocumentNode **ret)
{
    HTMLDocumentNode *doc;

    doc = alloc_doc_node(doc_obj, window);
    if(!doc)
        return E_OUTOFMEMORY;

2200
    if(!doc_obj->basedoc.window || window == doc_obj->basedoc.window)
2201
        doc->basedoc.cp_container.forward_container = &doc_obj->basedoc.cp_container;
2202

2203
    nsIDOMHTMLDocument_AddRef(nsdoc);
2204
    doc->nsdoc = nsdoc;
2205
    init_document_mutation(doc);
2206
    doc_init_events(doc);
2207

2208 2209
    HTMLDOMNode_Init(doc, &doc->node, (nsIDOMNode*)nsdoc);
    doc->node.vtbl = &HTMLDocumentNodeImplVtbl;
2210
    doc->node.cp_container = &doc->basedoc.cp_container;
2211

2212 2213 2214
    *ret = doc;
    return S_OK;
}
2215

2216 2217 2218 2219 2220 2221 2222 2223 2224
HRESULT create_document_fragment(nsIDOMNode *nsnode, HTMLDocumentNode *doc_node, HTMLDocumentNode **ret)
{
    HTMLDocumentNode *doc_frag;

    doc_frag = alloc_doc_node(doc_node->basedoc.doc_obj, doc_node->basedoc.window);
    if(!doc_frag)
        return E_OUTOFMEMORY;

    HTMLDOMNode_Init(doc_node, &doc_frag->node, nsnode);
2225
    doc_frag->node.vtbl = &HTMLDocumentFragmentImplVtbl;
2226 2227 2228 2229 2230 2231 2232
    doc_frag->node.cp_container = &doc_frag->basedoc.cp_container;

    htmldoc_addref(&doc_frag->basedoc);
    *ret = doc_frag;
    return S_OK;
}

2233 2234 2235 2236
/**********************************************************
 * ICustomDoc implementation
 */

2237 2238 2239 2240
static inline HTMLDocumentObj *impl_from_ICustomDoc(ICustomDoc *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocumentObj, ICustomDoc_iface);
}
2241 2242 2243

static HRESULT WINAPI CustomDoc_QueryInterface(ICustomDoc *iface, REFIID riid, void **ppv)
{
2244
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2245

2246 2247 2248
    if(htmldoc_qi(&This->basedoc, riid, ppv))
        return *ppv ? S_OK : E_NOINTERFACE;

2249 2250
    if(IsEqualGUID(&IID_ICustomDoc, riid)) {
        TRACE("(%p)->(IID_ICustomDoc %p)\n", This, ppv);
2251
        *ppv = &This->ICustomDoc_iface;
2252 2253
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
2254 2255 2256 2257 2258 2259 2260 2261
    }else {
        FIXME("Unimplemented interface %s\n", debugstr_guid(riid));
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

2264
static ULONG WINAPI CustomDoc_AddRef(ICustomDoc *iface)
2265
{
2266
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2267 2268 2269 2270 2271 2272 2273
    ULONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

2274
static ULONG WINAPI CustomDoc_Release(ICustomDoc *iface)
2275
{
2276
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2277 2278 2279 2280 2281
    ULONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
2282 2283
        if(This->basedoc.doc_node) {
            This->basedoc.doc_node->basedoc.doc_obj = NULL;
2284
            htmldoc_release(&This->basedoc.doc_node->basedoc);
2285
        }
2286 2287
        if(This->basedoc.window) {
            This->basedoc.window->doc_obj = NULL;
2288
            IHTMLWindow2_Release(&This->basedoc.window->IHTMLWindow2_iface);
2289
        }
2290 2291
        if(This->basedoc.advise_holder)
            IOleAdviseHolder_Release(This->basedoc.advise_holder);
2292

2293 2294
        if(This->view_sink)
            IAdviseSink_Release(This->view_sink);
2295
        if(This->client)
2296
            IOleObject_SetClientSite(&This->basedoc.IOleObject_iface, NULL);
2297
        if(This->hostui)
2298
            ICustomDoc_SetUIHandler(&This->ICustomDoc_iface, NULL);
2299
        if(This->in_place_active)
2300
            IOleInPlaceObjectWindowless_InPlaceDeactivate(&This->basedoc.IOleInPlaceObjectWindowless_iface);
2301
        if(This->ipsite)
2302
            IOleDocumentView_SetInPlaceSite(&This->basedoc.IOleDocumentView_iface, NULL);
2303 2304
        if(This->undomgr)
            IOleUndoManager_Release(This->undomgr);
2305 2306 2307 2308 2309
        if(This->tooltips_hwnd)
            DestroyWindow(This->tooltips_hwnd);

        if(This->hwnd)
            DestroyWindow(This->hwnd);
2310
        heap_free(This->mime);
2311

2312
        destroy_htmldoc(&This->basedoc);
2313
        release_dispex(&This->dispex);
2314

2315 2316
        if(This->nscontainer)
            NSContainer_Release(This->nscontainer);
2317 2318 2319 2320 2321 2322
        heap_free(This);
    }

    return ref;
}

2323 2324
static HRESULT WINAPI CustomDoc_SetUIHandler(ICustomDoc *iface, IDocHostUIHandler *pUIHandler)
{
2325
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
    IOleCommandTarget *cmdtrg;
    HRESULT hres;

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

    if(This->custom_hostui && This->hostui == pUIHandler)
        return S_OK;

    This->custom_hostui = TRUE;

    if(This->hostui)
        IDocHostUIHandler_Release(This->hostui);
    if(pUIHandler)
        IDocHostUIHandler_AddRef(pUIHandler);
    This->hostui = pUIHandler;
    if(!pUIHandler)
        return S_OK;

    hres = IDocHostUIHandler_QueryInterface(pUIHandler, &IID_IOleCommandTarget, (void**)&cmdtrg);
    if(SUCCEEDED(hres)) {
        FIXME("custom UI handler supports IOleCommandTarget\n");
        IOleCommandTarget_Release(cmdtrg);
    }

    return S_OK;
2351 2352 2353 2354 2355 2356 2357
}

static const ICustomDocVtbl CustomDocVtbl = {
    CustomDoc_QueryInterface,
    CustomDoc_AddRef,
    CustomDoc_Release,
    CustomDoc_SetUIHandler
2358 2359
};

2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
static const tid_t HTMLDocumentObj_iface_tids[] = {
    IHTMLDocument2_tid,
    IHTMLDocument3_tid,
    IHTMLDocument4_tid,
    IHTMLDocument5_tid,
    0
};
static dispex_static_data_t HTMLDocumentObj_dispex = {
    NULL,
    DispHTMLDocument_tid,
    NULL,
    HTMLDocumentObj_iface_tids
};

2374 2375
HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
{
2376
    HTMLDocumentObj *doc;
2377
    nsIDOMWindow *nswindow = NULL;
2378
    nsresult nsres;
2379 2380 2381
    HRESULT hres;

    TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppvObject);
2382

2383 2384 2385 2386
    doc = heap_alloc_zero(sizeof(HTMLDocumentObj));
    if(!doc)
        return E_OUTOFMEMORY;

2387 2388
    init_dispex(&doc->dispex, (IUnknown*)&doc->ICustomDoc_iface, &HTMLDocumentObj_dispex);
    init_doc(&doc->basedoc, (IUnknown*)&doc->ICustomDoc_iface, &doc->dispex.IDispatchEx_iface);
2389

2390
    doc->ICustomDoc_iface.lpVtbl = &CustomDocVtbl;
2391
    doc->ref = 1;
2392
    doc->basedoc.doc_obj = doc;
2393

2394 2395
    doc->usermode = UNKNOWN_USERMODE;

2396 2397
    hres = create_nscontainer(doc, NULL, &doc->nscontainer);
    if(FAILED(hres)) {
2398 2399
        ERR("Failed to init Gecko, returning CLASS_E_CLASSNOTAVAILABLE\n");
        htmldoc_release(&doc->basedoc);
2400
        return hres;
2401 2402
    }

2403 2404
    hres = htmldoc_query_interface(&doc->basedoc, riid, ppvObject);
    htmldoc_release(&doc->basedoc);
2405 2406 2407
    if(FAILED(hres))
        return hres;

2408

2409 2410 2411
    nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
    if(NS_FAILED(nsres))
        ERR("GetContentDOMWindow failed: %08x\n", nsres);
2412

2413
    hres = HTMLWindow_Create(doc, nswindow, NULL /* FIXME */, &doc->basedoc.window);
2414 2415
    if(nswindow)
        nsIDOMWindow_Release(nswindow);
2416
    if(FAILED(hres)) {
2417
        htmldoc_release(&doc->basedoc);
2418 2419
        return hres;
    }
2420

2421 2422 2423 2424 2425
    if(!doc->basedoc.doc_node && doc->basedoc.window->doc) {
        doc->basedoc.doc_node = doc->basedoc.window->doc;
        htmldoc_addref(&doc->basedoc.doc_node->basedoc);
    }

2426 2427
    get_thread_hwnd();

2428
    return S_OK;
2429
}