htmldoc.c 75.6 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
 */

#include "config.h"

#include <stdarg.h>
#include <stdio.h>
23
#include <assert.h>
24 25 26 27 28 29

#define COBJMACROS

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

#include "wine/debug.h"

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

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

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

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

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

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

    return htmldoc_addref(This);
60 61 62 63
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

141 142
    hres = get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE, &node);
    nsIDOMElement_Release(nselem);
143 144 145 146 147
    if(FAILED(hres))
        return hres;

    *p = create_all_collection(node, TRUE);
    node_release(node);
148
    return hres;
149 150 151 152
}

static HRESULT WINAPI HTMLDocument_get_body(IHTMLDocument2 *iface, IHTMLElement **p)
{
153
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
154 155
    nsIDOMHTMLElement *nsbody = NULL;
    HTMLDOMNode *node;
156
    HRESULT hres;
157 158 159

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

160 161
    if(This->doc_node->nsdoc) {
        nsresult nsres;
162

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

170
    if(!nsbody) {
171
        *p = NULL;
172
        return S_OK;
173
    }
174

175 176 177 178 179
    hres = get_node(This->doc_node, (nsIDOMNode*)nsbody, TRUE, &node);
    nsIDOMHTMLElement_Release(nsbody);
    if(FAILED(hres))
        return hres;

180 181 182
    hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
    node_release(node);
    return hres;
183 184 185 186
}

static HRESULT WINAPI HTMLDocument_get_activeElement(IHTMLDocument2 *iface, IHTMLElement **p)
{
187
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
188
    FIXME("(%p)->(%p)\n", This, p);
189 190 191 192 193
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_images(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
194
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
195 196 197 198 199 200 201 202 203 204
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

205
    if(!This->doc_node->nsdoc) {
206 207 208 209
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

210
    nsres = nsIDOMHTMLDocument_GetImages(This->doc_node->nsdoc, &nscoll);
211 212 213 214 215 216
    if(NS_FAILED(nsres)) {
        ERR("GetImages failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
217
        *p = create_collection_from_htmlcol(This->doc_node, nscoll);
218
        nsIDOMHTMLCollection_Release(nscoll);
219 220 221
    }

    return S_OK;
222 223 224 225
}

static HRESULT WINAPI HTMLDocument_get_applets(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
226
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
227 228 229 230 231 232 233 234 235 236
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

237
    if(!This->doc_node->nsdoc) {
238 239 240 241
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

242
    nsres = nsIDOMHTMLDocument_GetApplets(This->doc_node->nsdoc, &nscoll);
243 244 245 246 247 248
    if(NS_FAILED(nsres)) {
        ERR("GetApplets failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
249
        *p = create_collection_from_htmlcol(This->doc_node, nscoll);
250
        nsIDOMHTMLCollection_Release(nscoll);
251 252 253
    }

    return S_OK;
254 255 256 257
}

static HRESULT WINAPI HTMLDocument_get_links(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
258
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
259 260 261 262 263 264 265 266 267 268
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

269
    if(!This->doc_node->nsdoc) {
270 271 272 273
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

274
    nsres = nsIDOMHTMLDocument_GetLinks(This->doc_node->nsdoc, &nscoll);
275 276 277 278 279 280
    if(NS_FAILED(nsres)) {
        ERR("GetLinks failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
281
        *p = create_collection_from_htmlcol(This->doc_node, nscoll);
282
        nsIDOMHTMLCollection_Release(nscoll);
283 284 285
    }

    return S_OK;
286 287 288 289
}

static HRESULT WINAPI HTMLDocument_get_forms(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
290
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
291 292 293 294 295 296 297 298 299 300
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

301
    if(!This->doc_node->nsdoc) {
302 303 304 305
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

306
    nsres = nsIDOMHTMLDocument_GetForms(This->doc_node->nsdoc, &nscoll);
307 308 309 310 311 312
    if(NS_FAILED(nsres)) {
        ERR("GetForms failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
313
        *p = create_collection_from_htmlcol(This->doc_node, nscoll);
314
        nsIDOMHTMLCollection_Release(nscoll);
315 316 317
    }

    return S_OK;
318 319 320 321
}

static HRESULT WINAPI HTMLDocument_get_anchors(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
322
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
323 324 325 326 327 328 329 330 331 332
    nsIDOMHTMLCollection *nscoll = NULL;
    nsresult nsres;

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

    if(!p)
        return E_INVALIDARG;

    *p = NULL;

333
    if(!This->doc_node->nsdoc) {
334 335 336 337
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

338
    nsres = nsIDOMHTMLDocument_GetAnchors(This->doc_node->nsdoc, &nscoll);
339 340 341 342 343 344
    if(NS_FAILED(nsres)) {
        ERR("GetAnchors failed: %08x\n", nsres);
        return E_FAIL;
    }

    if(nscoll) {
345
        *p = create_collection_from_htmlcol(This->doc_node, nscoll);
346
        nsIDOMHTMLCollection_Release(nscoll);
347 348 349
    }

    return S_OK;
350 351 352 353
}

static HRESULT WINAPI HTMLDocument_put_title(IHTMLDocument2 *iface, BSTR v)
{
354
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
355 356 357 358 359
    nsAString nsstr;
    nsresult nsres;

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

360
    if(!This->doc_node->nsdoc) {
361 362
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
363 364
    }

365
    nsAString_InitDepend(&nsstr, v);
366
    nsres = nsIDOMHTMLDocument_SetTitle(This->doc_node->nsdoc, &nsstr);
367 368 369 370 371
    nsAString_Finish(&nsstr);
    if(NS_FAILED(nsres))
        ERR("SetTitle failed: %08x\n", nsres);

    return S_OK;
372 373 374 375
}

static HRESULT WINAPI HTMLDocument_get_title(IHTMLDocument2 *iface, BSTR *p)
{
376
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
377 378 379 380 381 382
    const PRUnichar *ret;
    nsAString nsstr;
    nsresult nsres;

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

383
    if(!This->doc_node->nsdoc) {
384 385
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
386 387 388 389
    }


    nsAString_Init(&nsstr, NULL);
390
    nsres = nsIDOMHTMLDocument_GetTitle(This->doc_node->nsdoc, &nsstr);
391 392 393 394 395
    if (NS_SUCCEEDED(nsres)) {
        nsAString_GetData(&nsstr, &ret);
        *p = SysAllocString(ret);
    }
    nsAString_Finish(&nsstr);
396

397
    if(NS_FAILED(nsres)) {
398
        ERR("GetTitle failed: %08x\n", nsres);
399 400
        return E_FAIL;
    }
401 402

    return S_OK;
403 404 405 406
}

static HRESULT WINAPI HTMLDocument_get_scripts(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
407
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
408
    FIXME("(%p)->(%p)\n", This, p);
409 410 411 412 413
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_designMode(IHTMLDocument2 *iface, BSTR v)
{
414
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
415
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
416 417 418 419 420
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_designMode(IHTMLDocument2 *iface, BSTR *p)
{
421
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
422 423 424 425 426 427 428 429 430
    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;
431 432 433 434
}

static HRESULT WINAPI HTMLDocument_get_selection(IHTMLDocument2 *iface, IHTMLSelectionObject **p)
{
435
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
436 437
    nsISelection *nsselection;
    nsresult nsres;
438 439 440

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

441 442 443 444
    nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
    if(NS_FAILED(nsres)) {
        ERR("GetSelection failed: %08x\n", nsres);
        return E_FAIL;
445 446
    }

447
    return HTMLSelectionObject_Create(This->doc_node, nsselection, p);
448 449 450 451
}

static HRESULT WINAPI HTMLDocument_get_readyState(IHTMLDocument2 *iface, BSTR *p)
{
452
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

    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;

473
    *p = SysAllocString(readystate_str[This->window->readystate]);
474
    return S_OK;
475 476 477 478
}

static HRESULT WINAPI HTMLDocument_get_frames(IHTMLDocument2 *iface, IHTMLFramesCollection2 **p)
{
479
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
480 481 482

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

483
    return IHTMLWindow2_get_frames(&This->window->base.IHTMLWindow2_iface, p);
484 485 486 487
}

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

static HRESULT WINAPI HTMLDocument_get_plugins(IHTMLDocument2 *iface, IHTMLElementCollection **p)
{
495
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
496
    FIXME("(%p)->(%p)\n", This, p);
497 498 499 500 501
    return E_NOTIMPL;
}

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

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

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

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

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

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

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

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

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

static HRESULT WINAPI HTMLDocument_get_vlinkColor(IHTMLDocument2 *iface, VARIANT *p)
{
565
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
566
    FIXME("(%p)->(%p)\n", This, p);
567 568 569 570 571
    return E_NOTIMPL;
}

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

574
    FIXME("(%p)->(%p)\n", This, p);
575 576 577 578

    *p = NULL;
    return S_OK;
 }
579 580 581

static HRESULT WINAPI HTMLDocument_get_location(IHTMLDocument2 *iface, IHTMLLocation **p)
{
582
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
583 584 585

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

586 587 588 589 590
    if(!This->doc_node->nsdoc) {
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

591
    return IHTMLWindow2_get_location(&This->window->base.IHTMLWindow2_iface, p);
592 593 594 595
}

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

static HRESULT WINAPI HTMLDocument_put_URL(IHTMLDocument2 *iface, BSTR v)
{
603
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
604 605 606 607 608 609 610 611

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

    if(!This->window) {
        FIXME("No window available\n");
        return E_FAIL;
    }

612
    return navigate_url(This->window, v, This->window->uri);
613 614 615 616
}

static HRESULT WINAPI HTMLDocument_get_URL(IHTMLDocument2 *iface, BSTR *p)
{
617
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
618 619 620 621 622 623

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

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

624
    *p = SysAllocString(This->window->url ? This->window->url : about_blank_url);
625
    return *p ? S_OK : E_OUTOFMEMORY;
626 627 628 629
}

static HRESULT WINAPI HTMLDocument_put_domain(IHTMLDocument2 *iface, BSTR v)
{
630
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
631
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
632 633 634 635 636
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_domain(IHTMLDocument2 *iface, BSTR *p)
{
637
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
638 639 640 641 642 643 644 645 646 647 648
    HRESULT hres;

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

    if(!This->window || !This->window->uri) {
        FIXME("No current URI\n");
        return E_FAIL;
    }

    hres = IUri_GetHost(This->window->uri, p);
    return FAILED(hres) ? hres : S_OK;
649 650 651 652
}

static HRESULT WINAPI HTMLDocument_put_cookie(IHTMLDocument2 *iface, BSTR v)
{
653
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
654 655 656 657 658 659 660 661 662 663 664
    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;
665 666 667 668
}

static HRESULT WINAPI HTMLDocument_get_cookie(IHTMLDocument2 *iface, BSTR *p)
{
669
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    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;
706 707 708 709
}

static HRESULT WINAPI HTMLDocument_put_expando(IHTMLDocument2 *iface, VARIANT_BOOL v)
{
710
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
711
    FIXME("(%p)->(%x)\n", This, v);
712 713 714 715 716
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_expando(IHTMLDocument2 *iface, VARIANT_BOOL *p)
{
717
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
718
    FIXME("(%p)->(%p)\n", This, p);
719 720 721 722 723
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_charset(IHTMLDocument2 *iface, BSTR v)
{
724
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
725
    FIXME("(%p)->(%s)\n", This, debugstr_w(v));
726 727 728 729 730
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_charset(IHTMLDocument2 *iface, BSTR *p)
{
731
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
732 733 734 735 736 737 738 739 740 741 742 743 744
    nsAString charset_str;
    nsresult nsres;

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

    if(!This->doc_node->nsdoc) {
        FIXME("NULL nsdoc\n");
        return E_FAIL;
    }

    nsAString_Init(&charset_str, NULL);
    nsres = nsIDOMHTMLDocument_GetCharacterSet(This->doc_node->nsdoc, &charset_str);
    return return_nsstr(nsres, &charset_str, p);
745 746 747 748
}

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

static HRESULT WINAPI HTMLDocument_get_defaultCharset(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_mimeType(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_fileSize(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_fileCreatedDate(IHTMLDocument2 *iface, BSTR *p)
{
777
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
778
    FIXME("(%p)->(%p)\n", This, p);
779 780 781 782 783
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fileModifiedDate(IHTMLDocument2 *iface, BSTR *p)
{
784
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
785
    FIXME("(%p)->(%p)\n", This, p);
786 787 788 789 790
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_fileUpdatedDate(IHTMLDocument2 *iface, BSTR *p)
{
791
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
792
    FIXME("(%p)->(%p)\n", This, p);
793 794 795 796 797
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_security(IHTMLDocument2 *iface, BSTR *p)
{
798
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
799
    FIXME("(%p)->(%p)\n", This, p);
800 801 802 803 804
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_protocol(IHTMLDocument2 *iface, BSTR *p)
{
805
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
806
    FIXME("(%p)->(%p)\n", This, p);
807 808 809 810 811
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_nameProp(IHTMLDocument2 *iface, BSTR *p)
{
812
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
813
    FIXME("(%p)->(%p)\n", This, p);
814 815 816
    return E_NOTIMPL;
}

817
static HRESULT document_write(HTMLDocument *This, SAFEARRAY *psarray, BOOL ln)
818
{
819
    VARIANT *var, tmp;
820
    nsAString nsstr;
821
    ULONG i, argc;
822 823 824
    nsresult nsres;
    HRESULT hres;

825
    if(!This->doc_node->nsdoc) {
826 827 828 829
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
    }

830 831 832
    if (!psarray)
        return S_OK;

833 834 835 836 837 838 839 840 841 842 843
    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;
    }

844
    V_VT(&tmp) = VT_EMPTY;
845

846 847
    argc = psarray->rgsabound[0].cElements;
    for(i=0; i < argc; i++) {
848
        if(V_VT(var+i) == VT_BSTR) {
849
            nsAString_InitDepend(&nsstr, V_BSTR(var+i));
850
        }else {
851
            hres = VariantChangeTypeEx(&tmp, var+i, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
            if(FAILED(hres)) {
                WARN("Could not convert %s to string\n", debugstr_variant(var+i));
                break;
            }
            nsAString_InitDepend(&nsstr, V_BSTR(&tmp));
        }

        if(!ln || i != argc-1)
            nsres = nsIDOMHTMLDocument_Write(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
        else
            nsres = nsIDOMHTMLDocument_Writeln(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
        nsAString_Finish(&nsstr);
        if(V_VT(var+i) != VT_BSTR)
            VariantClear(&tmp);
        if(NS_FAILED(nsres)) {
            ERR("Write failed: %08x\n", nsres);
            hres = E_FAIL;
            break;
870 871 872 873 874
        }
    }

    SafeArrayUnaccessData(psarray);

875
    return hres;
876 877
}

878 879
static HRESULT WINAPI HTMLDocument_write(IHTMLDocument2 *iface, SAFEARRAY *psarray)
{
880
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
881 882 883 884 885 886

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

    return document_write(This, psarray, FALSE);
}

887 888
static HRESULT WINAPI HTMLDocument_writeln(IHTMLDocument2 *iface, SAFEARRAY *psarray)
{
889
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
890 891 892 893

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

    return document_write(This, psarray, TRUE);
894 895 896 897 898
}

static HRESULT WINAPI HTMLDocument_open(IHTMLDocument2 *iface, BSTR url, VARIANT name,
                        VARIANT features, VARIANT replace, IDispatch **pomWindowResult)
{
899
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
900
    nsISupports *tmp;
901 902 903 904 905 906 907
    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);

908
    if(!This->doc_node->nsdoc) {
909 910 911 912 913 914 915 916
        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");

917
    nsres = nsIDOMHTMLDocument_Open(This->doc_node->nsdoc, NULL, NULL, NULL, NULL, 0, &tmp);
918 919 920 921 922
    if(NS_FAILED(nsres)) {
        ERR("Open failed: %08x\n", nsres);
        return E_FAIL;
    }

923 924 925
    if(tmp)
        nsISupports_Release(tmp);

926 927
    *pomWindowResult = (IDispatch*)&This->window->base.IHTMLWindow2_iface;
    IHTMLWindow2_AddRef(&This->window->base.IHTMLWindow2_iface);
928
    return S_OK;
929 930 931 932
}

static HRESULT WINAPI HTMLDocument_close(IHTMLDocument2 *iface)
{
933
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
934 935 936 937
    nsresult nsres;

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

938
    if(!This->doc_node->nsdoc) {
939 940 941 942
        ERR("!nsdoc\n");
        return E_NOTIMPL;
    }

943
    nsres = nsIDOMHTMLDocument_Close(This->doc_node->nsdoc);
944 945 946 947 948 949
    if(NS_FAILED(nsres)) {
        ERR("Close failed: %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
950 951 952 953
}

static HRESULT WINAPI HTMLDocument_clear(IHTMLDocument2 *iface)
{
954
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
955 956 957 958
    nsresult nsres;

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

959
    nsres = nsIDOMHTMLDocument_Clear(This->doc_node->nsdoc);
960 961 962 963 964 965
    if(NS_FAILED(nsres)) {
        ERR("Clear failed: %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
966 967 968 969 970
}

static HRESULT WINAPI HTMLDocument_queryCommandSupported(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
971
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
972
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
973 974 975 976 977 978
    return E_NOTIMPL;
}

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

static HRESULT WINAPI HTMLDocument_queryCommandState(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
987
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
988
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
989 990 991 992 993 994
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandIndeterm(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
995
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
996
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
997 998 999 1000 1001 1002
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandText(IHTMLDocument2 *iface, BSTR cmdID,
                                                        BSTR *pfRet)
{
1003
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1004
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1005 1006 1007 1008 1009 1010
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_queryCommandValue(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT *pfRet)
{
1011
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1012
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1013 1014 1015 1016 1017 1018
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_execCommand(IHTMLDocument2 *iface, BSTR cmdID,
                                VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
{
1019
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1020
    FIXME("(%p)->(%s %x %s %p)\n", This, debugstr_w(cmdID), showUI, debugstr_variant(&value), pfRet);
1021 1022 1023 1024 1025 1026
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_execCommandShowHelp(IHTMLDocument2 *iface, BSTR cmdID,
                                                        VARIANT_BOOL *pfRet)
{
1027
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1028
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1029 1030 1031 1032
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_createElement(IHTMLDocument2 *iface, BSTR eTag,
1033
                                                 IHTMLElement **newElem)
1034
{
1035
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1036
    HTMLDocumentNode *doc_node;
1037
    nsIDOMHTMLElement *nselem;
1038
    HTMLElement *elem;
1039
    HRESULT hres;
1040 1041 1042

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

1043 1044 1045 1046 1047 1048
    /* Use owner doc if called on document fragment */
    doc_node = This->doc_node;
    if(!doc_node->nsdoc)
        doc_node = doc_node->node.doc;

    hres = create_nselem(doc_node, eTag, &nselem);
1049 1050
    if(FAILED(hres))
        return hres;
1051

1052
    hres = HTMLElement_Create(doc_node, (nsIDOMNode*)nselem, TRUE, &elem);
1053
    nsIDOMHTMLElement_Release(nselem);
1054 1055
    if(FAILED(hres))
        return hres;
1056

1057
    *newElem = &elem->IHTMLElement_iface;
1058
    return S_OK;
1059 1060 1061 1062
}

static HRESULT WINAPI HTMLDocument_put_onhelp(IHTMLDocument2 *iface, VARIANT v)
{
1063
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1064
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1065 1066 1067 1068 1069
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onhelp(IHTMLDocument2 *iface, VARIANT *p)
{
1070
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1071
    FIXME("(%p)->(%p)\n", This, p);
1072 1073 1074 1075 1076
    return E_NOTIMPL;
}

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

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

    return set_doc_event(This, EVENTID_CLICK, &v);
1082 1083 1084 1085
}

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

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

    return get_doc_event(This, EVENTID_CLICK, p);
1091 1092 1093 1094
}

static HRESULT WINAPI HTMLDocument_put_ondblclick(IHTMLDocument2 *iface, VARIANT v)
{
1095
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1096
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1097 1098 1099 1100 1101
    return E_NOTIMPL;
}

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

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

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

    return set_doc_event(This, EVENTID_KEYUP, &v);
1114 1115 1116 1117
}

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

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

    return get_doc_event(This, EVENTID_KEYUP, p);
1123 1124 1125 1126
}

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

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

    return set_doc_event(This, EVENTID_KEYDOWN, &v);
1132 1133 1134 1135
}

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

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

    return get_doc_event(This, EVENTID_KEYDOWN, p);
1141 1142 1143 1144
}

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

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

    return set_doc_event(This, EVENTID_KEYPRESS, &v);
1150 1151 1152 1153
}

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

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

    return get_doc_event(This, EVENTID_KEYPRESS, p);
1159 1160 1161 1162
}

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

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

    return set_doc_event(This, EVENTID_MOUSEUP, &v);
1168 1169 1170 1171
}

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

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

    return get_doc_event(This, EVENTID_MOUSEUP, p);
1177 1178 1179 1180
}

static HRESULT WINAPI HTMLDocument_put_onmousedown(IHTMLDocument2 *iface, VARIANT v)
{
1181
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1182

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

    return set_doc_event(This, EVENTID_MOUSEDOWN, &v);
1186 1187 1188 1189
}

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

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

    return get_doc_event(This, EVENTID_MOUSEDOWN, p);
1195 1196 1197 1198
}

static HRESULT WINAPI HTMLDocument_put_onmousemove(IHTMLDocument2 *iface, VARIANT v)
{
1199
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1200

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

    return set_doc_event(This, EVENTID_MOUSEMOVE, &v);
1204 1205 1206 1207
}

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

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

    return get_doc_event(This, EVENTID_MOUSEMOVE, p);
1213 1214 1215 1216
}

static HRESULT WINAPI HTMLDocument_put_onmouseout(IHTMLDocument2 *iface, VARIANT v)
{
1217
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1218 1219 1220 1221

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

    return set_doc_event(This, EVENTID_MOUSEOUT, &v);
1222 1223 1224 1225
}

static HRESULT WINAPI HTMLDocument_get_onmouseout(IHTMLDocument2 *iface, VARIANT *p)
{
1226
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1227 1228 1229 1230

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

    return get_doc_event(This, EVENTID_MOUSEOUT, p);
1231 1232 1233 1234
}

static HRESULT WINAPI HTMLDocument_put_onmouseover(IHTMLDocument2 *iface, VARIANT v)
{
1235
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1236

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

    return set_doc_event(This, EVENTID_MOUSEOVER, &v);
1240 1241 1242 1243
}

static HRESULT WINAPI HTMLDocument_get_onmouseover(IHTMLDocument2 *iface, VARIANT *p)
{
1244
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1245 1246 1247 1248

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

    return get_doc_event(This, EVENTID_MOUSEOVER, p);
1249 1250 1251 1252
}

static HRESULT WINAPI HTMLDocument_put_onreadystatechange(IHTMLDocument2 *iface, VARIANT v)
{
1253
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1254 1255 1256 1257

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

    return set_doc_event(This, EVENTID_READYSTATECHANGE, &v);
1258 1259 1260 1261
}

static HRESULT WINAPI HTMLDocument_get_onreadystatechange(IHTMLDocument2 *iface, VARIANT *p)
{
1262
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1263 1264 1265 1266

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

    return get_doc_event(This, EVENTID_READYSTATECHANGE, p);
1267 1268 1269 1270
}

static HRESULT WINAPI HTMLDocument_put_onafterupdate(IHTMLDocument2 *iface, VARIANT v)
{
1271
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1272
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1273 1274 1275 1276 1277
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onafterupdate(IHTMLDocument2 *iface, VARIANT *p)
{
1278
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1279
    FIXME("(%p)->(%p)\n", This, p);
1280 1281 1282 1283 1284
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onrowexit(IHTMLDocument2 *iface, VARIANT v)
{
1285
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1286
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1287 1288 1289 1290 1291
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onrowexit(IHTMLDocument2 *iface, VARIANT *p)
{
1292
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1293
    FIXME("(%p)->(%p)\n", This, p);
1294 1295 1296 1297 1298
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onrowenter(IHTMLDocument2 *iface, VARIANT v)
{
1299
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1300
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1301 1302 1303 1304 1305
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onrowenter(IHTMLDocument2 *iface, VARIANT *p)
{
1306
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1307
    FIXME("(%p)->(%p)\n", This, p);
1308 1309 1310 1311 1312
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_ondragstart(IHTMLDocument2 *iface, VARIANT v)
{
1313
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1314 1315 1316 1317

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

    return set_doc_event(This, EVENTID_DRAGSTART, &v);
1318 1319 1320 1321
}

static HRESULT WINAPI HTMLDocument_get_ondragstart(IHTMLDocument2 *iface, VARIANT *p)
{
1322
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1323 1324 1325 1326

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

    return get_doc_event(This, EVENTID_DRAGSTART, p);
1327 1328 1329 1330
}

static HRESULT WINAPI HTMLDocument_put_onselectstart(IHTMLDocument2 *iface, VARIANT v)
{
1331
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1332 1333 1334 1335

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

    return set_doc_event(This, EVENTID_SELECTSTART, &v);
1336 1337 1338 1339
}

static HRESULT WINAPI HTMLDocument_get_onselectstart(IHTMLDocument2 *iface, VARIANT *p)
{
1340
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1341 1342 1343 1344

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

    return get_doc_event(This, EVENTID_SELECTSTART, p);
1345 1346
}

1347
static HRESULT WINAPI HTMLDocument_elementFromPoint(IHTMLDocument2 *iface, LONG x, LONG y,
1348 1349
                                                        IHTMLElement **elementHit)
{
1350
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1351 1352 1353 1354 1355 1356 1357
    nsIDOMElement *nselem;
    HTMLDOMNode *node;
    nsresult nsres;
    HRESULT hres;

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

1358
    nsres = nsIDOMHTMLDocument_ElementFromPoint(This->doc_node->nsdoc, x, y, &nselem);
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    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;

1374 1375 1376
    hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)elementHit);
    node_release(node);
    return hres;
1377 1378 1379 1380
}

static HRESULT WINAPI HTMLDocument_get_parentWindow(IHTMLDocument2 *iface, IHTMLWindow2 **p)
{
1381
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1382 1383 1384

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

1385
    *p = &This->window->base.IHTMLWindow2_iface;
1386 1387
    IHTMLWindow2_AddRef(*p);
    return S_OK;
1388 1389 1390
}

static HRESULT WINAPI HTMLDocument_get_styleSheets(IHTMLDocument2 *iface,
1391
                                                   IHTMLStyleSheetsCollection **p)
1392
{
1393
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1394 1395 1396 1397 1398 1399 1400
    nsIDOMStyleSheetList *nsstylelist;
    nsresult nsres;

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

    *p = NULL;

1401
    if(!This->doc_node->nsdoc) {
1402 1403
        WARN("NULL nsdoc\n");
        return E_UNEXPECTED;
1404 1405
    }

1406
    nsres = nsIDOMHTMLDocument_GetStyleSheets(This->doc_node->nsdoc, &nsstylelist);
1407 1408 1409 1410
    if(NS_FAILED(nsres)) {
        ERR("GetStyleSheets failed: %08x\n", nsres);
        return E_FAIL;
    }
1411 1412

    *p = HTMLStyleSheetsCollection_Create(nsstylelist);
1413
    nsIDOMStyleSheetList_Release(nsstylelist);
1414 1415

    return S_OK;
1416 1417 1418 1419
}

static HRESULT WINAPI HTMLDocument_put_onbeforeupdate(IHTMLDocument2 *iface, VARIANT v)
{
1420
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1421
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1422 1423 1424 1425 1426
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onbeforeupdate(IHTMLDocument2 *iface, VARIANT *p)
{
1427
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1428
    FIXME("(%p)->(%p)\n", This, p);
1429 1430 1431 1432 1433
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_put_onerrorupdate(IHTMLDocument2 *iface, VARIANT v)
{
1434
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1435
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1436 1437 1438 1439 1440
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_get_onerrorupdate(IHTMLDocument2 *iface, VARIANT *p)
{
1441
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1442
    FIXME("(%p)->(%p)\n", This, p);
1443 1444 1445 1446 1447
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_toString(IHTMLDocument2 *iface, BSTR *String)
{
1448
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1449
    FIXME("(%p)->(%p)\n", This, String);
1450 1451 1452 1453
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLDocument_createStyleSheet(IHTMLDocument2 *iface, BSTR bstrHref,
1454
                                            LONG lIndex, IHTMLStyleSheet **ppnewStyleSheet)
1455
{
1456
    HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1457

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

1460
    *ppnewStyleSheet = HTMLStyleSheet_Create(NULL);
1461
    return S_OK;
1462 1463
}

1464
static const IHTMLDocument2Vtbl HTMLDocumentVtbl = {
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 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    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
};

1583 1584
static void HTMLDocument_on_advise(IUnknown *iface, cp_static_data_t *cp)
{
1585
    HTMLDocument *This = impl_from_IHTMLDocument2((IHTMLDocument2*)iface);
1586 1587

    if(This->window)
1588
        update_cp_events(This->window->base.inner_window, &This->doc_node->node.event_target, cp, This->doc_node->node.nsnode);
1589 1590
}

1591 1592 1593 1594
static inline HTMLDocument *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocument, ISupportErrorInfo_iface);
}
1595 1596 1597

static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **ppv)
{
1598
    HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1599
    return htmldoc_query_interface(This, riid, ppv);
1600 1601 1602 1603
}

static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
{
1604
    HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1605
    return htmldoc_addref(This);
1606 1607 1608 1609
}

static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
{
1610
    HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1611
    return htmldoc_release(This);
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
}

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
};

1627 1628 1629 1630
static inline HTMLDocument *impl_from_IDispatchEx(IDispatchEx *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocument, IDispatchEx_iface);
}
1631

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 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 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
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;
}

1690 1691
static HRESULT WINAPI DocDispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
{
1692
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1693

1694
    return htmldoc_query_interface(This, riid, ppv);
1695 1696 1697 1698
}

static ULONG WINAPI DocDispatchEx_AddRef(IDispatchEx *iface)
{
1699
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1700

1701
    return htmldoc_addref(This);
1702 1703 1704 1705
}

static ULONG WINAPI DocDispatchEx_Release(IDispatchEx *iface)
{
1706
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1707

1708
    return htmldoc_release(This);
1709 1710 1711 1712
}

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

1715
    return IDispatchEx_GetTypeInfoCount(This->dispex, pctinfo);
1716 1717 1718 1719 1720
}

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

1723
    return IDispatchEx_GetTypeInfo(This->dispex, iTInfo, lcid, ppTInfo);
1724 1725 1726 1727 1728 1729
}

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

1732
    return IDispatchEx_GetIDsOfNames(This->dispex, riid, rgszNames, cNames, lcid, rgDispId);
1733 1734 1735 1736 1737 1738
}

static HRESULT WINAPI DocDispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
1739
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751

    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;
1752
        V_I4(pVarResult) = This->window->readystate;
1753 1754 1755
        return S_OK;
    }

1756
    return IDispatchEx_Invoke(This->dispex, dispIdMember, riid, lcid, wFlags, pDispParams,
1757 1758 1759 1760 1761
                              pVarResult, pExcepInfo, puArgErr);
}

static HRESULT WINAPI DocDispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
{
1762
    HTMLDocument *This = impl_from_IDispatchEx(iface);
1763
    HRESULT hres;
1764

1765 1766 1767 1768 1769
    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);
1770 1771 1772 1773 1774
}

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

1777
    if(This->window && id == DISPID_IHTMLDOCUMENT2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT))
1778
        return IDispatchEx_InvokeEx(&This->window->base.IDispatchEx_iface, DISPID_IHTMLWINDOW2_LOCATION,
1779
                lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1780 1781


1782
    return IDispatchEx_InvokeEx(This->dispex, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1783 1784 1785 1786
}

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

1789
    return IDispatchEx_DeleteMemberByName(This->dispex, bstrName, grfdex);
1790 1791 1792 1793
}

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

1796
    return IDispatchEx_DeleteMemberByDispID(This->dispex, id);
1797 1798 1799 1800
}

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

1803
    return IDispatchEx_GetMemberProperties(This->dispex, id, grfdexFetch, pgrfdex);
1804 1805 1806 1807
}

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

1810
    return IDispatchEx_GetMemberName(This->dispex, id, pbstrName);
1811 1812 1813 1814
}

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

1817
    return IDispatchEx_GetNextDispID(This->dispex, grfdex, id, pid);
1818 1819 1820 1821
}

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

1824
    return IDispatchEx_GetNameSpaceParent(This->dispex, ppunk);
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
}

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
};

1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
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);
1873 1874
    TRACE("(%p)->(%p)\n", This, ppTI);
    return get_htmldoc_classinfo(ppTI);
1875 1876 1877 1878 1879 1880 1881 1882 1883
}

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

1884
static BOOL htmldoc_qi(HTMLDocument *This, REFIID riid, void **ppv)
1885 1886 1887 1888 1889
{
    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
1890
        *ppv = &This->IHTMLDocument2_iface;
1891 1892
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch, %p)\n", This, ppv);
1893
        *ppv = &This->IDispatchEx_iface;
1894 1895
    }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
        TRACE("(%p)->(IID_IDispatchEx, %p)\n", This, ppv);
1896
        *ppv = &This->IDispatchEx_iface;
1897 1898
    }else if(IsEqualGUID(&IID_IHTMLDocument, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument, %p)\n", This, ppv);
1899
        *ppv = &This->IHTMLDocument2_iface;
1900 1901
    }else if(IsEqualGUID(&IID_IHTMLDocument2, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument2, %p)\n", This, ppv);
1902
        *ppv = &This->IHTMLDocument2_iface;
1903 1904
    }else if(IsEqualGUID(&IID_IHTMLDocument3, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument3, %p)\n", This, ppv);
1905
        *ppv = &This->IHTMLDocument3_iface;
1906 1907
    }else if(IsEqualGUID(&IID_IHTMLDocument4, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument4, %p)\n", This, ppv);
1908
        *ppv = &This->IHTMLDocument4_iface;
1909 1910
    }else if(IsEqualGUID(&IID_IHTMLDocument5, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument5, %p)\n", This, ppv);
1911
        *ppv = &This->IHTMLDocument5_iface;
1912 1913
    }else if(IsEqualGUID(&IID_IHTMLDocument6, riid)) {
        TRACE("(%p)->(IID_IHTMLDocument6, %p)\n", This, ppv);
1914
        *ppv = &This->IHTMLDocument6_iface;
1915 1916
    }else if(IsEqualGUID(&IID_IPersist, riid)) {
        TRACE("(%p)->(IID_IPersist, %p)\n", This, ppv);
1917
        *ppv = &This->IPersistFile_iface;
1918 1919
    }else if(IsEqualGUID(&IID_IPersistMoniker, riid)) {
        TRACE("(%p)->(IID_IPersistMoniker, %p)\n", This, ppv);
1920
        *ppv = &This->IPersistMoniker_iface;
1921 1922
    }else if(IsEqualGUID(&IID_IPersistFile, riid)) {
        TRACE("(%p)->(IID_IPersistFile, %p)\n", This, ppv);
1923
        *ppv = &This->IPersistFile_iface;
1924 1925
    }else if(IsEqualGUID(&IID_IMonikerProp, riid)) {
        TRACE("(%p)->(IID_IMonikerProp, %p)\n", This, ppv);
1926
        *ppv = &This->IMonikerProp_iface;
1927 1928
    }else if(IsEqualGUID(&IID_IOleObject, riid)) {
        TRACE("(%p)->(IID_IOleObject, %p)\n", This, ppv);
1929
        *ppv = &This->IOleObject_iface;
1930 1931
    }else if(IsEqualGUID(&IID_IOleDocument, riid)) {
        TRACE("(%p)->(IID_IOleDocument, %p)\n", This, ppv);
1932
        *ppv = &This->IOleDocument_iface;
1933 1934
    }else if(IsEqualGUID(&IID_IOleDocumentView, riid)) {
        TRACE("(%p)->(IID_IOleDocumentView, %p)\n", This, ppv);
1935
        *ppv = &This->IOleDocumentView_iface;
1936 1937
    }else if(IsEqualGUID(&IID_IOleInPlaceActiveObject, riid)) {
        TRACE("(%p)->(IID_IOleInPlaceActiveObject, %p)\n", This, ppv);
1938
        *ppv = &This->IOleInPlaceActiveObject_iface;
1939 1940
    }else if(IsEqualGUID(&IID_IViewObject, riid)) {
        TRACE("(%p)->(IID_IViewObject, %p)\n", This, ppv);
1941
        *ppv = &This->IViewObjectEx_iface;
1942 1943
    }else if(IsEqualGUID(&IID_IViewObject2, riid)) {
        TRACE("(%p)->(IID_IViewObject2, %p)\n", This, ppv);
1944
        *ppv = &This->IViewObjectEx_iface;
1945 1946
    }else if(IsEqualGUID(&IID_IViewObjectEx, riid)) {
        TRACE("(%p)->(IID_IViewObjectEx, %p)\n", This, ppv);
1947
        *ppv = &This->IViewObjectEx_iface;
1948 1949
    }else if(IsEqualGUID(&IID_IOleWindow, riid)) {
        TRACE("(%p)->(IID_IOleWindow, %p)\n", This, ppv);
1950
        *ppv = &This->IOleInPlaceActiveObject_iface;
1951 1952
    }else if(IsEqualGUID(&IID_IOleInPlaceObject, riid)) {
        TRACE("(%p)->(IID_IOleInPlaceObject, %p)\n", This, ppv);
1953
        *ppv = &This->IOleInPlaceObjectWindowless_iface;
1954 1955
    }else if(IsEqualGUID(&IID_IOleInPlaceObjectWindowless, riid)) {
        TRACE("(%p)->(IID_IOleInPlaceObjectWindowless, %p)\n", This, ppv);
1956
        *ppv = &This->IOleInPlaceObjectWindowless_iface;
1957 1958
    }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
        TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
1959
        *ppv = &This->IServiceProvider_iface;
1960 1961
    }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
        TRACE("(%p)->(IID_IOleCommandTarget, %p)\n", This, ppv);
1962
        *ppv = &This->IOleCommandTarget_iface;
1963 1964
    }else if(IsEqualGUID(&IID_IOleControl, riid)) {
        TRACE("(%p)->(IID_IOleControl, %p)\n", This, ppv);
1965
        *ppv = &This->IOleControl_iface;
1966 1967
    }else if(IsEqualGUID(&IID_IHlinkTarget, riid)) {
        TRACE("(%p)->(IID_IHlinkTarget, %p)\n", This, ppv);
1968
        *ppv = &This->IHlinkTarget_iface;
1969 1970
    }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
        TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1971
        *ppv = &This->cp_container.IConnectionPointContainer_iface;
1972 1973
    }else if(IsEqualGUID(&IID_IPersistStreamInit, riid)) {
        TRACE("(%p)->(IID_IPersistStreamInit %p)\n", This, ppv);
1974
        *ppv = &This->IPersistStreamInit_iface;
1975 1976
    }else if(IsEqualGUID(&DIID_DispHTMLDocument, riid)) {
        TRACE("(%p)->(DIID_DispHTMLDocument %p)\n", This, ppv);
1977
        *ppv = &This->IHTMLDocument2_iface;
1978 1979
    }else if(IsEqualGUID(&IID_ISupportErrorInfo, riid)) {
        TRACE("(%p)->(IID_ISupportErrorInfo %p)\n", This, ppv);
1980
        *ppv = &This->ISupportErrorInfo_iface;
1981 1982
    }else if(IsEqualGUID(&IID_IPersistHistory, riid)) {
        TRACE("(%p)->(IID_IPersistHistory %p)\n", This, ppv);
1983
        *ppv = &This->IPersistHistory_iface;
1984 1985
    }else if(IsEqualGUID(&CLSID_CMarkup, riid)) {
        FIXME("(%p)->(CLSID_CMarkup %p)\n", This, ppv);
1986
        *ppv = NULL;
1987 1988
    }else if(IsEqualGUID(&IID_IRunnableObject, riid)) {
        TRACE("(%p)->(IID_IRunnableObject %p) returning NULL\n", This, ppv);
1989
        *ppv = NULL;
1990 1991
    }else if(IsEqualGUID(&IID_IPersistPropertyBag, riid)) {
        TRACE("(%p)->(IID_IPersistPropertyBag %p) returning NULL\n", This, ppv);
1992
        *ppv = NULL;
1993 1994
    }else if(IsEqualGUID(&IID_IMarshal, riid)) {
        TRACE("(%p)->(IID_IMarshal %p) returning NULL\n", This, ppv);
1995
        *ppv = NULL;
1996 1997 1998
    }else if(IsEqualGUID(&IID_IExternalConnection, riid)) {
        TRACE("(%p)->(IID_IExternalConnection %p) returning NULL\n", This, ppv);
        *ppv = NULL;
1999 2000 2001
    }else if(IsEqualGUID(&IID_IStdMarshalInfo, riid)) {
        TRACE("(%p)->(IID_IStdMarshalInfo %p) returning NULL\n", This, ppv);
        *ppv = NULL;
2002 2003
    }else if(IsEqualGUID(&IID_IObjectWithSite, riid)) {
        TRACE("(%p)->(IID_IObjectWithSite %p)\n", This, ppv);
2004
        *ppv = &This->IObjectWithSite_iface;
2005 2006 2007
    }else if(IsEqualGUID(&IID_IOleContainer, riid)) {
        TRACE("(%p)->(IID_IOleContainer %p)\n", This, ppv);
        *ppv = &This->IOleContainer_iface;
2008 2009 2010
    }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
        TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
        *ppv = &This->IObjectSafety_iface;
2011 2012 2013
    }else if(IsEqualGUID(&IID_IProvideClassInfo, riid)) {
        TRACE("(%p)->(IID_IProvideClassInfo, %p)\n", This, ppv);
        *ppv = &This->IProvideClassInfo_iface;
2014
    }else {
2015
        return FALSE;
2016 2017
    }

2018 2019 2020
    if(*ppv)
        IUnknown_AddRef((IUnknown*)*ppv);
    return TRUE;
2021 2022
}

2023
static cp_static_data_t HTMLDocumentEvents_data = { HTMLDocumentEvents_tid, HTMLDocument_on_advise };
2024

2025
static void init_doc(HTMLDocument *doc, IUnknown *unk_impl, IDispatchEx *dispex)
2026
{
2027
    doc->IHTMLDocument2_iface.lpVtbl = &HTMLDocumentVtbl;
2028
    doc->IDispatchEx_iface.lpVtbl = &DocDispatchExVtbl;
2029
    doc->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
2030
    doc->IProvideClassInfo_iface.lpVtbl = &ProvideClassInfoVtbl;
2031

2032
    doc->unk_impl = unk_impl;
2033
    doc->dispex = dispex;
2034
    doc->task_magic = get_task_target_magic();
2035

2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
    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);

2046
    ConnectionPointContainer_Init(&doc->cp_container, (IUnknown*)&doc->IHTMLDocument2_iface);
2047
    ConnectionPoint_Init(&doc->cp_dispatch, &doc->cp_container, &IID_IDispatch, &HTMLDocumentEvents_data);
2048 2049 2050
    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);
2051
}
2052

2053 2054
static void destroy_htmldoc(HTMLDocument *This)
{
2055
    remove_target_tasks(This->task_magic);
2056 2057 2058 2059

    ConnectionPointContainer_Destroy(&This->cp_container);
}

2060 2061 2062 2063
static inline HTMLDocumentNode *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocumentNode, node);
}
2064 2065 2066

static HRESULT HTMLDocumentNode_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
{
2067
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2068 2069 2070 2071

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

2072 2073
    if(IsEqualGUID(&IID_IInternetHostSecurityManager, riid)) {
        TRACE("(%p)->(IID_IInternetHostSecurityManager %p)\n", This, ppv);
2074
        *ppv = &This->IInternetHostSecurityManager_iface;
2075 2076 2077 2078 2079 2080
    }else {
        return HTMLDOMNode_QI(&This->node, riid, ppv);
    }

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

2083
static void HTMLDocumentNode_destructor(HTMLDOMNode *iface)
2084
{
2085
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2086 2087 2088 2089 2090
    unsigned i;

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

2092
    detach_events(This);
2093 2094
    if(This->body_event_target)
        release_event_target(This->body_event_target);
2095 2096
    if(This->catmgr)
        ICatInformation_Release(This->catmgr);
2097

2098 2099
    detach_selection(This);
    detach_ranges(This);
2100

2101 2102 2103
    while(!list_empty(&This->plugin_hosts))
        detach_plugin_host(LIST_ENTRY(list_head(&This->plugin_hosts), PluginHost, entry));

2104
    if(This->nsdoc) {
2105
        assert(!This->window);
2106
        release_document_mutation(This);
2107
        nsIDOMHTMLDocument_Release(This->nsdoc);
2108 2109 2110 2111
    }else if(This->window) {
        /* document fragments own reference to inner window */
        IHTMLWindow2_Release(&This->window->base.IHTMLWindow2_iface);
        This->window = NULL;
2112
    }
2113

2114
    heap_free(This->event_vector);
2115 2116 2117
    destroy_htmldoc(&This->basedoc);
}

2118 2119
static HRESULT HTMLDocumentNode_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
{
2120
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2121 2122 2123 2124
    FIXME("%p\n", This);
    return E_NOTIMPL;
}

2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
static void HTMLDocumentNode_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
{
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);

    if(This->nsdoc)
        note_cc_edge((nsISupports*)This->nsdoc, "This->nsdoc", cb);
}

static void HTMLDocumentNode_unlink(HTMLDOMNode *iface)
{
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);

    if(This->nsdoc) {
        nsIDOMHTMLDocument *nsdoc = This->nsdoc;

        release_document_mutation(This);
        This->nsdoc = NULL;
        nsIDOMHTMLDocument_Release(nsdoc);
2143
        This->window = NULL;
2144 2145 2146
    }
}

2147 2148
static const NodeImplVtbl HTMLDocumentNodeImplVtbl = {
    HTMLDocumentNode_QI,
2149
    HTMLDocumentNode_destructor,
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
    HTMLDocumentNode_clone,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    HTMLDocumentNode_traverse,
    HTMLDocumentNode_unlink
2164 2165
};

2166 2167
static HRESULT HTMLDocumentFragment_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
{
2168
    HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
    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;
}

2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
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;

2197
    if(flags != DISPATCH_PROPERTYGET && flags != (DISPATCH_METHOD|DISPATCH_PROPERTYGET)) {
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
        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;

    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,
2231 2232
    HTMLDocumentNode_invoke,
    NULL
2233 2234
};

2235 2236 2237 2238 2239 2240
static const NodeImplVtbl HTMLDocumentFragmentImplVtbl = {
    HTMLDocumentNode_QI,
    HTMLDocumentNode_destructor,
    HTMLDocumentFragment_clone
};

2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
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 = {
2252
    &HTMLDocumentNode_dispex_vtbl,
2253 2254 2255 2256 2257
    DispHTMLDocument_tid,
    NULL,
    HTMLDocumentNode_iface_tids
};

2258
static HTMLDocumentNode *alloc_doc_node(HTMLDocumentObj *doc_obj, HTMLInnerWindow *window)
2259 2260
{
    HTMLDocumentNode *doc;
2261

2262 2263
    doc = heap_alloc_zero(sizeof(HTMLDocumentNode));
    if(!doc)
2264
        return NULL;
2265

2266
    doc->ref = 1;
2267 2268
    doc->basedoc.doc_node = doc;
    doc->basedoc.doc_obj = doc_obj;
2269 2270
    doc->basedoc.window = window->base.outer_window;
    doc->window = window;
2271

2272 2273 2274
    init_dispex(&doc->node.dispex, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
            &HTMLDocumentNode_dispex);
    init_doc(&doc->basedoc, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
2275
            &doc->node.dispex.IDispatchEx_iface);
2276
    HTMLDocumentNode_SecMgr_Init(doc);
2277

2278 2279
    list_init(&doc->selection_list);
    list_init(&doc->range_list);
2280
    list_init(&doc->plugin_hosts);
2281 2282 2283 2284

    return doc;
}

2285
HRESULT create_doc_from_nsdoc(nsIDOMHTMLDocument *nsdoc, HTMLDocumentObj *doc_obj, HTMLInnerWindow *window, HTMLDocumentNode **ret)
2286 2287 2288 2289 2290 2291 2292
{
    HTMLDocumentNode *doc;

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

2293
    if(!doc_obj->basedoc.window || window->base.outer_window == doc_obj->basedoc.window)
2294
        doc->basedoc.cp_container.forward_container = &doc_obj->basedoc.cp_container;
2295

2296 2297
    HTMLDOMNode_Init(doc, &doc->node, (nsIDOMNode*)nsdoc);

2298
    nsIDOMHTMLDocument_AddRef(nsdoc);
2299
    doc->nsdoc = nsdoc;
2300

2301
    init_document_mutation(doc);
2302
    doc_init_events(doc);
2303

2304
    doc->node.vtbl = &HTMLDocumentNodeImplVtbl;
2305
    doc->node.cp_container = &doc->basedoc.cp_container;
2306

2307 2308 2309
    *ret = doc;
    return S_OK;
}
2310

2311 2312 2313 2314
HRESULT create_document_fragment(nsIDOMNode *nsnode, HTMLDocumentNode *doc_node, HTMLDocumentNode **ret)
{
    HTMLDocumentNode *doc_frag;

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

2319 2320
    IHTMLWindow2_AddRef(&doc_frag->window->base.IHTMLWindow2_iface);

2321
    HTMLDOMNode_Init(doc_node, &doc_frag->node, nsnode);
2322
    doc_frag->node.vtbl = &HTMLDocumentFragmentImplVtbl;
2323 2324 2325 2326 2327 2328
    doc_frag->node.cp_container = &doc_frag->basedoc.cp_container;

    *ret = doc_frag;
    return S_OK;
}

2329 2330 2331 2332
/**********************************************************
 * ICustomDoc implementation
 */

2333 2334 2335 2336
static inline HTMLDocumentObj *impl_from_ICustomDoc(ICustomDoc *iface)
{
    return CONTAINING_RECORD(iface, HTMLDocumentObj, ICustomDoc_iface);
}
2337 2338 2339

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

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

2345 2346
    if(IsEqualGUID(&IID_ICustomDoc, riid)) {
        TRACE("(%p)->(IID_ICustomDoc %p)\n", This, ppv);
2347
        *ppv = &This->ICustomDoc_iface;
2348 2349 2350
    }else if(IsEqualGUID(&IID_ITargetContainer, riid)) {
        TRACE("(%p)->(IID_ITargetContainer %p)\n", This, ppv);
        *ppv = &This->ITargetContainer_iface;
2351 2352
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
2353 2354 2355 2356 2357 2358 2359 2360
    }else {
        FIXME("Unimplemented interface %s\n", debugstr_guid(riid));
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

2363
static ULONG WINAPI CustomDoc_AddRef(ICustomDoc *iface)
2364
{
2365
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2366 2367 2368 2369 2370 2371 2372
    ULONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

2373
static ULONG WINAPI CustomDoc_Release(ICustomDoc *iface)
2374
{
2375
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2376 2377 2378 2379 2380
    ULONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
2381 2382 2383 2384 2385
        nsIDOMWindowUtils *window_utils = NULL;

        if(This->basedoc.window && This->basedoc.window->nswindow)
            get_nsinterface((nsISupports*)This->basedoc.window->nswindow, &IID_nsIDOMWindowUtils, (void**)&window_utils);

2386 2387
        if(This->basedoc.doc_node) {
            This->basedoc.doc_node->basedoc.doc_obj = NULL;
2388
            htmldoc_release(&This->basedoc.doc_node->basedoc);
2389
        }
2390 2391
        if(This->basedoc.window) {
            This->basedoc.window->doc_obj = NULL;
2392
            IHTMLWindow2_Release(&This->basedoc.window->base.IHTMLWindow2_iface);
2393
        }
2394 2395
        if(This->basedoc.advise_holder)
            IOleAdviseHolder_Release(This->basedoc.advise_holder);
2396

2397 2398
        if(This->view_sink)
            IAdviseSink_Release(This->view_sink);
2399
        if(This->client)
2400
            IOleObject_SetClientSite(&This->basedoc.IOleObject_iface, NULL);
2401
        if(This->hostui)
2402
            ICustomDoc_SetUIHandler(&This->ICustomDoc_iface, NULL);
2403
        if(This->in_place_active)
2404
            IOleInPlaceObjectWindowless_InPlaceDeactivate(&This->basedoc.IOleInPlaceObjectWindowless_iface);
2405
        if(This->ipsite)
2406
            IOleDocumentView_SetInPlaceSite(&This->basedoc.IOleDocumentView_iface, NULL);
2407 2408
        if(This->undomgr)
            IOleUndoManager_Release(This->undomgr);
2409 2410 2411 2412 2413
        if(This->tooltips_hwnd)
            DestroyWindow(This->tooltips_hwnd);

        if(This->hwnd)
            DestroyWindow(This->hwnd);
2414
        heap_free(This->mime);
2415

2416
        destroy_htmldoc(&This->basedoc);
2417
        release_dispex(&This->dispex);
2418

2419 2420
        if(This->nscontainer)
            NSContainer_Release(This->nscontainer);
2421
        heap_free(This);
2422 2423 2424 2425 2426 2427

        /* Force cycle collection */
        if(window_utils) {
            nsIDOMWindowUtils_CycleCollect(window_utils, NULL, 0);
            nsIDOMWindowUtils_Release(window_utils);
        }
2428 2429 2430 2431 2432
    }

    return ref;
}

2433 2434
static HRESULT WINAPI CustomDoc_SetUIHandler(ICustomDoc *iface, IDocHostUIHandler *pUIHandler)
{
2435
    HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
    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;
2461 2462 2463 2464 2465 2466 2467
}

static const ICustomDocVtbl CustomDocVtbl = {
    CustomDoc_QueryInterface,
    CustomDoc_AddRef,
    CustomDoc_Release,
    CustomDoc_SetUIHandler
2468 2469
};

2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
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
};

2484 2485
HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
{
2486
    HTMLDocumentObj *doc;
2487
    nsIDOMWindow *nswindow = NULL;
2488
    nsresult nsres;
2489 2490 2491
    HRESULT hres;

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

2493 2494 2495 2496
    doc = heap_alloc_zero(sizeof(HTMLDocumentObj));
    if(!doc)
        return E_OUTOFMEMORY;

2497 2498
    init_dispex(&doc->dispex, (IUnknown*)&doc->ICustomDoc_iface, &HTMLDocumentObj_dispex);
    init_doc(&doc->basedoc, (IUnknown*)&doc->ICustomDoc_iface, &doc->dispex.IDispatchEx_iface);
2499
    TargetContainer_Init(doc);
2500

2501
    doc->ICustomDoc_iface.lpVtbl = &CustomDocVtbl;
2502
    doc->ref = 1;
2503
    doc->basedoc.doc_obj = doc;
2504

2505 2506
    doc->usermode = UNKNOWN_USERMODE;

2507 2508
    init_binding_ui(doc);

2509
    hres = create_nscontainer(doc, &doc->nscontainer);
2510
    if(FAILED(hres)) {
2511 2512
        ERR("Failed to init Gecko, returning CLASS_E_CLASSNOTAVAILABLE\n");
        htmldoc_release(&doc->basedoc);
2513
        return hres;
2514 2515
    }

2516 2517
    hres = htmldoc_query_interface(&doc->basedoc, riid, ppvObject);
    htmldoc_release(&doc->basedoc);
2518 2519 2520
    if(FAILED(hres))
        return hres;

2521 2522 2523
    nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
    if(NS_FAILED(nsres))
        ERR("GetContentDOMWindow failed: %08x\n", nsres);
2524

2525
    hres = HTMLOuterWindow_Create(doc, nswindow, NULL /* FIXME */, &doc->basedoc.window);
2526 2527
    if(nswindow)
        nsIDOMWindow_Release(nswindow);
2528
    if(FAILED(hres)) {
2529
        htmldoc_release(&doc->basedoc);
2530 2531
        return hres;
    }
2532

2533 2534
    if(!doc->basedoc.doc_node && doc->basedoc.window->base.inner_window->doc) {
        doc->basedoc.doc_node = doc->basedoc.window->base.inner_window->doc;
2535 2536 2537
        htmldoc_addref(&doc->basedoc.doc_node->basedoc);
    }

2538 2539
    get_thread_hwnd();

2540
    return S_OK;
2541
}