mshtml: Added IHTMLStyle::[get|put]_filter implementation.
[wine] / dlls / mshtml / tests / dom.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * 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 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "mshtml.h"
30 #include "mshtmcid.h"
31 #include "mshtmhst.h"
32 #include "docobj.h"
33 #include "dispex.h"
34
35 static const char doc_blank[] = "<html></html>";
36 static const char doc_str1[] = "<html><body>test</body></html>";
37 static const char range_test_str[] =
38     "<html><body>test \na<font size=\"2\">bc\t123<br /> it's\r\n  \t</font>text<br /></body></html>";
39 static const char range_test2_str[] =
40     "<html><body>abc<hr />123<br /><hr />def</body></html>";
41 static const char elem_test_str[] =
42     "<html><head><title>test</title><style>.body { margin-right: 0px; }</style>"
43     "<body>text test<!-- a comment -->"
44     "<a href=\"http://test\" name=\"x\">link</a>"
45     "<input id=\"in\" class=\"testclass\" tabIndex=\"2\" title=\"test title\" />"
46     "<select id=\"s\"><option id=\"x\" value=\"val1\">opt1</option><option id=\"y\">opt2</option></select>"
47     "<textarea id=\"X\">text text</textarea>"
48     "<table id=\"tbl\"><tbody><tr></tr><tr id=\"row2\"><td>td1 text</td><td>td2 text</td></tr></tbody></table>"
49     "<script id=\"sc\" type=\"text/javascript\"></script>"
50     "<test />"
51     "<img id=\"imgid\"/>"
52     "<iframe src=\"about:blank\"></iframe>"
53     "</body></html>";
54 static const char indent_test_str[] =
55     "<html><head><title>test</title></head><body>abc<br /><a href=\"about:blank\">123</a></body></html>";
56
57 static const WCHAR noneW[] = {'N','o','n','e',0};
58
59 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
60 static WCHAR texteditW[] = {'t','e','x','t','e','d','i','t',0};
61 static WCHAR wordW[] = {'w','o','r','d',0};
62
63 static const WCHAR text_javascriptW[] = {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
64
65 static const WCHAR idW[] = {'i','d',0};
66
67 typedef enum {
68     ET_NONE,
69     ET_HTML,
70     ET_HEAD,
71     ET_TITLE,
72     ET_BODY,
73     ET_A,
74     ET_INPUT,
75     ET_SELECT,
76     ET_TEXTAREA,
77     ET_OPTION,
78     ET_STYLE,
79     ET_BLOCKQUOTE,
80     ET_P,
81     ET_BR,
82     ET_TABLE,
83     ET_TBODY,
84     ET_SCRIPT,
85     ET_TEST,
86     ET_TESTG,
87     ET_COMMENT,
88     ET_IMG,
89     ET_TR,
90     ET_TD,
91     ET_IFRAME
92 } elem_type_t;
93
94 static const IID * const none_iids[] = {
95     &IID_IUnknown,
96     NULL
97 };
98
99 static const IID * const elem_iids[] = {
100     &IID_IHTMLDOMNode,
101     &IID_IHTMLDOMNode2,
102     &IID_IHTMLElement,
103     &IID_IHTMLElement2,
104     &IID_IHTMLElement3,
105     &IID_IDispatchEx,
106     &IID_IConnectionPointContainer,
107     NULL
108 };
109
110 static const IID * const body_iids[] = {
111     &IID_IHTMLDOMNode,
112     &IID_IHTMLDOMNode2,
113     &IID_IHTMLElement,
114     &IID_IHTMLElement2,
115     &IID_IHTMLElement3,
116     &IID_IHTMLTextContainer,
117     &IID_IHTMLBodyElement,
118     &IID_IDispatchEx,
119     &IID_IConnectionPointContainer,
120     NULL
121 };
122
123 static const IID * const anchor_iids[] = {
124     &IID_IHTMLDOMNode,
125     &IID_IHTMLDOMNode2,
126     &IID_IHTMLElement,
127     &IID_IHTMLElement2,
128     &IID_IHTMLElement3,
129     &IID_IHTMLAnchorElement,
130     &IID_IDispatchEx,
131     &IID_IConnectionPointContainer,
132     NULL
133 };
134
135 static const IID * const input_iids[] = {
136     &IID_IHTMLDOMNode,
137     &IID_IHTMLDOMNode2,
138     &IID_IHTMLElement,
139     &IID_IHTMLElement2,
140     &IID_IHTMLElement3,
141     &IID_IHTMLInputElement,
142     &IID_IHTMLInputTextElement,
143     &IID_IDispatchEx,
144     &IID_IConnectionPointContainer,
145     NULL
146 };
147
148 static const IID * const select_iids[] = {
149     &IID_IHTMLDOMNode,
150     &IID_IHTMLDOMNode2,
151     &IID_IHTMLElement,
152     &IID_IHTMLElement2,
153     &IID_IHTMLElement3,
154     &IID_IHTMLSelectElement,
155     &IID_IDispatchEx,
156     &IID_IConnectionPointContainer,
157     NULL
158 };
159
160 static const IID * const textarea_iids[] = {
161     &IID_IHTMLDOMNode,
162     &IID_IHTMLDOMNode2,
163     &IID_IHTMLElement,
164     &IID_IHTMLElement2,
165     &IID_IHTMLElement3,
166     &IID_IHTMLTextAreaElement,
167     &IID_IDispatchEx,
168     &IID_IConnectionPointContainer,
169     NULL
170 };
171
172 static const IID * const option_iids[] = {
173     &IID_IHTMLDOMNode,
174     &IID_IHTMLDOMNode2,
175     &IID_IHTMLElement,
176     &IID_IHTMLElement2,
177     &IID_IHTMLElement3,
178     &IID_IHTMLOptionElement,
179     &IID_IDispatchEx,
180     &IID_IConnectionPointContainer,
181     NULL
182 };
183
184 static const IID * const table_iids[] = {
185     &IID_IHTMLDOMNode,
186     &IID_IHTMLDOMNode2,
187     &IID_IHTMLElement,
188     &IID_IHTMLElement2,
189     &IID_IHTMLElement3,
190     &IID_IHTMLTable,
191     &IID_IDispatchEx,
192     &IID_IConnectionPointContainer,
193     NULL
194 };
195
196 static const IID * const script_iids[] = {
197     &IID_IHTMLDOMNode,
198     &IID_IHTMLDOMNode2,
199     &IID_IHTMLElement,
200     &IID_IHTMLElement2,
201     &IID_IHTMLElement3,
202     &IID_IHTMLScriptElement,
203     &IID_IDispatchEx,
204     &IID_IConnectionPointContainer,
205     NULL
206 };
207
208 static const IID * const text_iids[] = {
209     &IID_IHTMLDOMNode,
210     &IID_IHTMLDOMNode2,
211     &IID_IHTMLDOMTextNode,
212     NULL
213 };
214
215 static const IID * const location_iids[] = {
216     &IID_IDispatch,
217     &IID_IHTMLLocation,
218     NULL
219 };
220
221 static const IID * const window_iids[] = {
222     &IID_IDispatch,
223     &IID_IHTMLWindow2,
224     &IID_IHTMLWindow3,
225     &IID_IDispatchEx,
226     NULL
227 };
228
229 static const IID * const comment_iids[] = {
230     &IID_IHTMLDOMNode,
231     &IID_IHTMLDOMNode2,
232     &IID_IHTMLElement,
233     &IID_IHTMLElement2,
234     &IID_IHTMLElement3,
235     &IID_IHTMLCommentElement,
236     &IID_IDispatchEx,
237     &IID_IConnectionPointContainer,
238     NULL
239 };
240
241 static const IID * const img_iids[] = {
242     &IID_IHTMLDOMNode,
243     &IID_IHTMLDOMNode2,
244     &IID_IHTMLElement,
245     &IID_IHTMLElement2,
246     &IID_IHTMLElement3,
247     &IID_IDispatchEx,
248     &IID_IHTMLImgElement,
249     &IID_IConnectionPointContainer,
250     NULL
251 };
252
253 static const IID * const tr_iids[] = {
254     &IID_IHTMLDOMNode,
255     &IID_IHTMLDOMNode2,
256     &IID_IHTMLElement,
257     &IID_IHTMLElement2,
258     &IID_IHTMLElement3,
259     &IID_IDispatchEx,
260     &IID_IHTMLTableRow,
261     &IID_IConnectionPointContainer,
262     NULL
263 };
264
265 static const IID * const td_iids[] = {
266     &IID_IHTMLDOMNode,
267     &IID_IHTMLDOMNode2,
268     &IID_IHTMLElement,
269     &IID_IHTMLElement2,
270     &IID_IHTMLElement3,
271     &IID_IDispatchEx,
272     &IID_IConnectionPointContainer,
273     NULL
274 };
275
276 static const IID * const iframe_iids[] = {
277     &IID_IHTMLDOMNode,
278     &IID_IHTMLDOMNode2,
279     &IID_IHTMLElement,
280     &IID_IHTMLElement2,
281     &IID_IHTMLElement3,
282     &IID_IHTMLFrameBase2,
283     &IID_IDispatchEx,
284     &IID_IConnectionPointContainer,
285     NULL
286 };
287
288 static const IID * const generic_iids[] = {
289     &IID_IHTMLDOMNode,
290     &IID_IHTMLDOMNode2,
291     &IID_IHTMLElement,
292     &IID_IHTMLElement2,
293     &IID_IHTMLElement3,
294     &IID_IHTMLGenericElement,
295     &IID_IDispatchEx,
296     &IID_IConnectionPointContainer,
297     NULL
298 };
299
300 static const IID * const style_iids[] = {
301     &IID_IUnknown,
302     &IID_IDispatch,
303     &IID_IDispatchEx,
304     &IID_IHTMLStyle,
305     &IID_IHTMLStyle2,
306     NULL
307 };
308
309 static const IID * const cstyle_iids[] = {
310     &IID_IUnknown,
311     &IID_IDispatch,
312     &IID_IDispatchEx,
313     &IID_IHTMLCurrentStyle,
314     NULL
315 };
316
317 typedef struct {
318     const char *tag;
319     REFIID *iids;
320     const IID *dispiid;
321 } elem_type_info_t;
322
323 static const elem_type_info_t elem_type_infos[] = {
324     {"",          none_iids,        NULL},
325     {"HTML",      elem_iids,        NULL},
326     {"HEAD",      elem_iids,        NULL},
327     {"TITLE",     elem_iids,        NULL},
328     {"BODY",      body_iids,        &DIID_DispHTMLBody},
329     {"A",         anchor_iids,      NULL},
330     {"INPUT",     input_iids,       &DIID_DispHTMLInputElement},
331     {"SELECT",    select_iids,      &DIID_DispHTMLSelectElement},
332     {"TEXTAREA",  textarea_iids,    NULL},
333     {"OPTION",    option_iids,      &DIID_DispHTMLOptionElement},
334     {"STYLE",     elem_iids,        NULL},
335     {"BLOCKQUOTE",elem_iids,        NULL},
336     {"P",         elem_iids,        NULL},
337     {"BR",        elem_iids,        NULL},
338     {"TABLE",     table_iids,       &DIID_DispHTMLTable},
339     {"TBODY",     elem_iids,        NULL},
340     {"SCRIPT",    script_iids,      NULL},
341     {"TEST",      elem_iids,        &DIID_DispHTMLUnknownElement},
342     {"TEST",      generic_iids,     &DIID_DispHTMLGenericElement},
343     {"!",         comment_iids,     &DIID_DispHTMLCommentElement},
344     {"IMG",       img_iids,         &DIID_DispHTMLImg},
345     {"TR",        tr_iids,          &DIID_DispHTMLTableRow},
346     {"TD",        td_iids,          NULL},
347     {"IFRAME",    iframe_iids,      &DIID_DispHTMLIFrame}
348 };
349
350 static const char *dbgstr_w(LPCWSTR str)
351 {
352     static char buf[512];
353     if(!str)
354         return "(null)";
355     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
356     return buf;
357 }
358
359 static const char *dbgstr_guid(REFIID riid)
360 {
361     static char buf[50];
362
363     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
364             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
365             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
366             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
367
368     return buf;
369 }
370
371 static int strcmp_wa(LPCWSTR strw, const char *stra)
372 {
373     WCHAR buf[512];
374     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
375     return lstrcmpW(strw, buf);
376 }
377
378 static BSTR a2bstr(const char *str)
379 {
380     BSTR ret;
381     int len;
382
383     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
384     ret = SysAllocStringLen(NULL, len);
385     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
386
387     return ret;
388 }
389
390 static IHTMLDocument2 *create_document(void)
391 {
392     IHTMLDocument2 *doc;
393     HRESULT hres;
394
395     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
396             &IID_IHTMLDocument2, (void**)&doc);
397     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
398
399     return doc;
400 }
401
402 #define test_ifaces(i,ids) _test_ifaces(__LINE__,i,ids)
403 static void _test_ifaces(unsigned line, IUnknown *iface, REFIID *iids)
404 {
405     const IID * const *piid;
406     IUnknown *unk;
407     HRESULT hres;
408
409      for(piid = iids; *piid; piid++) {
410         hres = IDispatch_QueryInterface(iface, *piid, (void**)&unk);
411         ok_(__FILE__,line) (hres == S_OK, "Could not get %s interface: %08x\n", dbgstr_guid(*piid), hres);
412         if(SUCCEEDED(hres))
413             IUnknown_Release(unk);
414     }
415 }
416
417 #define test_disp(u,id) _test_disp(__LINE__,u,id)
418 static void _test_disp(unsigned line, IUnknown *unk, const IID *diid)
419 {
420     IDispatchEx *dispex;
421     ITypeInfo *typeinfo;
422     UINT ticnt;
423     HRESULT hres;
424
425     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&dispex);
426     ok_(__FILE__,line) (hres == S_OK, "Could not get IDispatch: %08x\n", hres);
427     if(FAILED(hres))
428         return;
429
430     ticnt = 0xdeadbeef;
431     hres = IDispatchEx_GetTypeInfoCount(dispex, &ticnt);
432     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfoCount failed: %08x\n", hres);
433     ok_(__FILE__,line) (ticnt == 1, "ticnt=%u\n", ticnt);
434
435     hres = IDispatchEx_GetTypeInfo(dispex, 0, 0, &typeinfo);
436     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfo failed: %08x\n", hres);
437
438     if(SUCCEEDED(hres)) {
439         TYPEATTR *type_attr;
440
441         hres = ITypeInfo_GetTypeAttr(typeinfo, &type_attr);
442         ok_(__FILE__,line) (hres == S_OK, "GetTypeAttr failed: %08x\n", hres);
443         ok_(__FILE__,line) (IsEqualGUID(&type_attr->guid, diid), "unexpected guid %s\n", dbgstr_guid(&type_attr->guid));
444
445         ITypeInfo_ReleaseTypeAttr(typeinfo, type_attr);
446         ITypeInfo_Release(typeinfo);
447     }
448
449     IDispatchEx_Release(dispex);
450 }
451
452 #define get_elem_iface(u) _get_elem_iface(__LINE__,u)
453 static IHTMLElement *_get_elem_iface(unsigned line, IUnknown *unk)
454 {
455     IHTMLElement *elem;
456     HRESULT hres;
457
458     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
459     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement: %08x\n", hres);
460     return elem;
461 }
462
463 #define get_elem2_iface(u) _get_elem2_iface(__LINE__,u)
464 static IHTMLElement2 *_get_elem2_iface(unsigned line, IUnknown *unk)
465 {
466     IHTMLElement2 *elem;
467     HRESULT hres;
468
469     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement2, (void**)&elem);
470     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement2: %08x\n", hres);
471     return elem;
472 }
473
474 #define get_node_iface(u) _get_node_iface(__LINE__,u)
475 static IHTMLDOMNode *_get_node_iface(unsigned line, IUnknown *unk)
476 {
477     IHTMLDOMNode *node;
478     HRESULT hres;
479
480     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
481     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLDOMNode: %08x\n", hres);
482     return node;
483 }
484
485 #define get_img_iface(u) _get_img_iface(__LINE__,u)
486 static IHTMLImgElement *_get_img_iface(unsigned line, IUnknown *unk)
487 {
488     IHTMLImgElement *img;
489     HRESULT hres;
490
491     hres = IUnknown_QueryInterface(unk, &IID_IHTMLImgElement, (void**)&img);
492     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLImgElement: %08x\n", hres);
493     return img;
494 }
495
496 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
497 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
498 {
499     IHTMLDOMNode *node = _get_node_iface(line, unk);
500     BSTR name;
501     HRESULT hres;
502
503     hres = IHTMLDOMNode_get_nodeName(node, &name);
504     IHTMLDOMNode_Release(node);
505     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
506     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
507
508     SysFreeString(name);
509 }
510
511 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
512 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
513 {
514     IHTMLElement *elem = _get_elem_iface(line, unk);
515     BSTR tag;
516     HRESULT hres;
517
518     hres = IHTMLElement_get_tagName(elem, &tag);
519     IHTMLElement_Release(elem);
520     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
521     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
522
523     SysFreeString(tag);
524 }
525
526 #define test_elem_type(ifc,t) _test_elem_type(__LINE__,ifc,t)
527 static void _test_elem_type(unsigned line, IUnknown *unk, elem_type_t type)
528 {
529     _test_elem_tag(line, unk, elem_type_infos[type].tag);
530     _test_ifaces(line, unk, elem_type_infos[type].iids);
531
532     if(elem_type_infos[type].dispiid)
533         _test_disp(line, unk, elem_type_infos[type].dispiid);
534 }
535
536 #define test_elem_attr(e,n,v) _test_elem_attr(__LINE__,e,n,v)
537 static void _test_elem_attr(unsigned line, IHTMLElement *elem, LPCWSTR name, LPCWSTR exval)
538 {
539     VARIANT value;
540     BSTR tmp;
541     HRESULT hres;
542
543     VariantInit(&value);
544
545     tmp = SysAllocString(name);
546     hres = IHTMLElement_getAttribute(elem, tmp, 0, &value);
547     SysFreeString(tmp);
548     ok_(__FILE__,line) (hres == S_OK, "getAttribute failed: %08x\n", hres);
549
550     if(exval) {
551         ok_(__FILE__,line) (V_VT(&value) == VT_BSTR, "vt=%d\n", V_VT(&value));
552         ok_(__FILE__,line) (!lstrcmpW(exval, V_BSTR(&value)), "unexpected value %s\n", dbgstr_w(V_BSTR(&value)));
553     }else {
554         ok_(__FILE__,line) (V_VT(&value) == VT_NULL, "vt=%d\n", V_VT(&value));
555     }
556
557     VariantClear(&value);
558 }
559
560 #define test_elem_offset(u) _test_elem_offset(__LINE__,u)
561 static void _test_elem_offset(unsigned line, IUnknown *unk)
562 {
563     IHTMLElement *elem = _get_elem_iface(line, unk);
564     long l;
565     HRESULT hres;
566
567     hres = IHTMLElement_get_offsetTop(elem, &l);
568     ok_(__FILE__,line) (hres == S_OK, "get_offsetTop failed: %08x\n", hres);
569
570     hres = IHTMLElement_get_offsetHeight(elem, &l);
571     ok_(__FILE__,line) (hres == S_OK, "get_offsetHeight failed: %08x\n", hres);
572
573     hres = IHTMLElement_get_offsetWidth(elem, &l);
574     ok_(__FILE__,line) (hres == S_OK, "get_offsetWidth failed: %08x\n", hres);
575
576     IHTMLElement_Release(elem);
577 }
578
579 static void test_doc_elem(IHTMLDocument2 *doc)
580 {
581     IHTMLElement *elem;
582     IHTMLDocument3 *doc3;
583     HRESULT hres;
584
585     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
586     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
587
588     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
589     IHTMLDocument3_Release(doc3);
590     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
591
592     test_node_name((IUnknown*)elem, "HTML");
593     test_elem_tag((IUnknown*)elem, "HTML");
594
595     IHTMLElement_Release(elem);
596 }
597
598 #define get_doc_elem(d) _get_doc_elem(__LINE__,d)
599 static IHTMLElement *_get_doc_elem(unsigned line, IHTMLDocument2 *doc)
600 {
601     IHTMLElement *elem;
602     IHTMLDocument3 *doc3;
603     HRESULT hres;
604
605     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
606     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 interface: %08x\n", hres);
607     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
608     ok_(__FILE__,line) (hres == S_OK, "get_documentElement failed: %08x\n", hres);
609     IHTMLDocument3_Release(doc3);
610
611     return elem;
612 }
613
614 #define test_option_text(o,t) _test_option_text(__LINE__,o,t)
615 static void _test_option_text(unsigned line, IHTMLOptionElement *option, const char *text)
616 {
617     BSTR bstr;
618     HRESULT hres;
619
620     hres = IHTMLOptionElement_get_text(option, &bstr);
621     ok_(__FILE__,line) (hres == S_OK, "get_text failed: %08x\n", hres);
622     ok_(__FILE__,line) (!strcmp_wa(bstr, text), "text=%s\n", dbgstr_w(bstr));
623     SysFreeString(bstr);
624 }
625
626 #define test_option_put_text(o,t) _test_option_put_text(__LINE__,o,t)
627 static void _test_option_put_text(unsigned line, IHTMLOptionElement *option, const char *text)
628 {
629     BSTR bstr;
630     HRESULT hres;
631
632     bstr = a2bstr(text);
633     hres = IHTMLOptionElement_put_text(option, bstr);
634     SysFreeString(bstr);
635     ok(hres == S_OK, "put_text failed: %08x\n", hres);
636
637     _test_option_text(line, option, text);
638 }
639
640 #define test_option_value(o,t) _test_option_value(__LINE__,o,t)
641 static void _test_option_value(unsigned line, IHTMLOptionElement *option, const char *value)
642 {
643     BSTR bstr;
644     HRESULT hres;
645
646     hres = IHTMLOptionElement_get_value(option, &bstr);
647     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
648     ok_(__FILE__,line) (!strcmp_wa(bstr, value), "value=%s\n", dbgstr_w(bstr));
649     SysFreeString(bstr);
650 }
651
652 #define test_option_put_value(o,t) _test_option_put_value(__LINE__,o,t)
653 static void _test_option_put_value(unsigned line, IHTMLOptionElement *option, const char *value)
654 {
655     BSTR bstr;
656     HRESULT hres;
657
658     bstr = a2bstr(value);
659     hres = IHTMLOptionElement_put_value(option, bstr);
660     SysFreeString(bstr);
661     ok(hres == S_OK, "put_value failed: %08x\n", hres);
662
663     _test_option_value(line, option, value);
664 }
665
666 #define create_option_elem(d,t,v) _create_option_elem(__LINE__,d,t,v)
667 static IHTMLOptionElement *_create_option_elem(unsigned line, IHTMLDocument2 *doc,
668         const char *txt, const char *val)
669 {
670     IHTMLOptionElementFactory *factory;
671     IHTMLOptionElement *option;
672     IHTMLWindow2 *window;
673     VARIANT text, value, empty;
674     HRESULT hres;
675
676     hres = IHTMLDocument2_get_parentWindow(doc, &window);
677     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
678
679     hres = IHTMLWindow2_get_Option(window, &factory);
680     IHTMLWindow2_Release(window);
681     ok_(__FILE__,line) (hres == S_OK, "get_Option failed: %08x\n", hres);
682
683     V_VT(&text) = VT_BSTR;
684     V_BSTR(&text) = a2bstr(txt);
685     V_VT(&value) = VT_BSTR;
686     V_BSTR(&value) = a2bstr(val);
687     V_VT(&empty) = VT_EMPTY;
688
689     hres = IHTMLOptionElementFactory_create(factory, text, value, empty, empty, &option);
690     ok_(__FILE__,line) (hres == S_OK, "create failed: %08x\n", hres);
691
692     IHTMLOptionElementFactory_Release(factory);
693     VariantClear(&text);
694     VariantClear(&value);
695
696     _test_option_text(line, option, txt);
697     _test_option_value(line, option, val);
698
699     return option;
700 }
701
702 #define test_select_length(s,l) _test_select_length(__LINE__,s,l)
703 static void _test_select_length(unsigned line, IHTMLSelectElement *select, long length)
704 {
705     long len = 0xdeadbeef;
706     HRESULT hres;
707
708     hres = IHTMLSelectElement_get_length(select, &len);
709     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
710     ok_(__FILE__,line) (len == length, "len=%ld, expected %ld\n", len, length);
711 }
712
713 #define test_select_selidx(s,i) _test_select_selidx(__LINE__,s,i)
714 static void _test_select_selidx(unsigned line, IHTMLSelectElement *select, long index)
715 {
716     long idx = 0xdeadbeef;
717     HRESULT hres;
718
719     hres = IHTMLSelectElement_get_selectedIndex(select, &idx);
720     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
721     ok_(__FILE__,line) (idx == index, "idx=%ld, expected %ld\n", idx, index);
722 }
723
724 #define test_select_put_selidx(s,i) _test_select_put_selidx(__LINE__,s,i)
725 static void _test_select_put_selidx(unsigned line, IHTMLSelectElement *select, long index)
726 {
727     HRESULT hres;
728
729     hres = IHTMLSelectElement_put_selectedIndex(select, index);
730     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
731     _test_select_selidx(line, select, index);
732 }
733
734 #define test_select_value(s,v) _test_select_value(__LINE__,s,v)
735 static void _test_select_value(unsigned line, IHTMLSelectElement *select, const char *exval)
736 {
737     BSTR val;
738     HRESULT hres;
739
740     hres = IHTMLSelectElement_get_value(select, &val);
741     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
742     if(exval)
743         ok_(__FILE__,line) (!strcmp_wa(val, exval), "unexpected value %s\n", dbgstr_w(val));
744     else
745         ok_(__FILE__,line) (val == NULL, "val=%s, expected NULL\n", dbgstr_w(val));
746 }
747
748 #define test_select_set_value(s,v) _test_select_set_value(__LINE__,s,v)
749 static void _test_select_set_value(unsigned line, IHTMLSelectElement *select, const char *val)
750 {
751     BSTR bstr;
752     HRESULT hres;
753
754     bstr = a2bstr(val);
755     hres = IHTMLSelectElement_put_value(select, bstr);
756     SysFreeString(bstr);
757     ok_(__FILE__,line) (hres == S_OK, "put_value failed: %08x\n", hres);
758 }
759
760 #define test_select_type(s,t) _test_select_type(__LINE__,s,t)
761 static void _test_select_type(unsigned line, IHTMLSelectElement *select, const char *extype)
762 {
763     BSTR type;
764     HRESULT hres;
765
766     hres = IHTMLSelectElement_get_type(select, &type);
767     ok_(__FILE__,line) (hres == S_OK, "get_type failed: %08x\n", hres);
768     ok_(__FILE__,line) (!strcmp_wa(type, extype), "type=%s, expected %s\n", dbgstr_w(type), extype);
769 }
770
771 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
772 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
773 {
774     BSTR text;
775     HRESULT hres;
776
777     hres = IHTMLTxtRange_get_text(range, &text);
778     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
779
780     if(extext) {
781         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
782         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
783     }else {
784         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
785     }
786
787     SysFreeString(text);
788
789 }
790
791 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
792 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
793 {
794     HRESULT hres;
795
796     hres = IHTMLTxtRange_collapse(range, b);
797     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
798     _test_range_text(line, range, NULL);
799 }
800
801 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
802 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
803         VARIANT_BOOL exb, const char *extext)
804 {
805     VARIANT_BOOL b = 0xe0e0;
806     HRESULT hres;
807
808     hres = IHTMLTxtRange_expand(range, unit, &b);
809     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
810     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
811     _test_range_text(line, range, extext);
812 }
813
814 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
815 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
816 {
817     long c = 0xdeadbeef;
818     HRESULT hres;
819
820     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
821     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
822     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
823     _test_range_text(line, range, NULL);
824 }
825
826 #define test_range_movestart(r,u,c,e) _test_range_movestart(__LINE__,r,u,c,e)
827 static void _test_range_movestart(unsigned line, IHTMLTxtRange *range,
828         LPWSTR unit, long cnt, long excnt)
829 {
830     long c = 0xdeadbeef;
831     HRESULT hres;
832
833     hres = IHTMLTxtRange_moveStart(range, unit, cnt, &c);
834     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
835     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
836 }
837
838 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
839 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
840 {
841     long c = 0xdeadbeef;
842     HRESULT hres;
843
844     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
845     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
846     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
847 }
848
849 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
850 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, const char *text)
851 {
852     HRESULT hres;
853     BSTR bstr = a2bstr(text);
854
855     hres = IHTMLTxtRange_put_text(range, bstr);
856     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
857     SysFreeString(bstr);
858     _test_range_text(line, range, NULL);
859 }
860
861 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
862 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
863 {
864     VARIANT_BOOL b;
865     HRESULT hres;
866
867     b = 0xe0e0;
868     hres = IHTMLTxtRange_inRange(range1, range2, &b);
869     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
870     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
871 }
872
873 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
874 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
875 {
876     VARIANT_BOOL b;
877     HRESULT hres;
878
879     b = 0xe0e0;
880     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
881     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
882     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
883
884     b = 0xe0e0;
885     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
886     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
887     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
888
889     if(exb) {
890         test_range_inrange(range1, range2, VARIANT_TRUE);
891         test_range_inrange(range2, range1, VARIANT_TRUE);
892     }
893 }
894
895 #define test_range_parent(r,t) _test_range_parent(__LINE__,r,t)
896 static void _test_range_parent(unsigned line, IHTMLTxtRange *range, elem_type_t type)
897 {
898     IHTMLElement *elem;
899     HRESULT hres;
900
901     hres = IHTMLTxtRange_parentElement(range, &elem);
902     ok_(__FILE__,line) (hres == S_OK, "parentElement failed: %08x\n", hres);
903
904     _test_elem_type(line, (IUnknown*)elem, type);
905
906     IHTMLElement_Release(elem);
907 }
908
909 #define test_elem_collection(c,t,l) _test_elem_collection(__LINE__,c,t,l)
910 static void _test_elem_collection(unsigned line, IUnknown *unk,
911         const elem_type_t *elem_types, long exlen)
912 {
913     IHTMLElementCollection *col;
914     long len;
915     DWORD i;
916     VARIANT name, index;
917     IDispatch *disp;
918     HRESULT hres;
919
920     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElementCollection, (void**)&col);
921     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
922
923     test_disp((IUnknown*)col, &DIID_DispHTMLElementCollection);
924
925     hres = IHTMLElementCollection_get_length(col, &len);
926     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
927     ok_(__FILE__,line) (len == exlen, "len=%ld, expected %ld\n", len, exlen);
928
929     if(len > exlen)
930         len = exlen;
931
932     V_VT(&index) = VT_EMPTY;
933     V_VT(&name) = VT_I4;
934
935     for(i=0; i<len; i++) {
936         V_I4(&name) = i;
937         disp = (void*)0xdeadbeef;
938         hres = IHTMLElementCollection_item(col, name, index, &disp);
939         ok_(__FILE__,line) (hres == S_OK, "item(%d) failed: %08x\n", i, hres);
940         ok_(__FILE__,line) (disp != NULL, "item returned NULL\n");
941         if(FAILED(hres) || !disp)
942             continue;
943
944         _test_elem_type(line, (IUnknown*)disp, elem_types[i]);
945         IDispatch_Release(disp);
946     }
947
948     V_I4(&name) = len;
949     disp = (void*)0xdeadbeef;
950     hres = IHTMLElementCollection_item(col, name, index, &disp);
951     ok_(__FILE__,line) (hres == S_OK, "item failed: %08x\n", hres);
952     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
953
954     V_I4(&name) = -1;
955     disp = (void*)0xdeadbeef;
956     hres = IHTMLElementCollection_item(col, name, index, &disp);
957     ok_(__FILE__,line) (hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
958     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
959
960     IHTMLElementCollection_Release(col);
961 }
962
963 #define test_elem_getelembytag(u,t,l) _test_elem_getelembytag(__LINE__,u,t,l)
964 static void _test_elem_getelembytag(unsigned line, IUnknown *unk, elem_type_t type, long exlen)
965 {
966     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
967     IHTMLElementCollection *col = NULL;
968     elem_type_t *types = NULL;
969     BSTR tmp;
970     int i;
971     HRESULT hres;
972
973     tmp = a2bstr(elem_type_infos[type].tag);
974     hres = IHTMLElement2_getElementsByTagName(elem, tmp, &col);
975     SysFreeString(tmp);
976     IHTMLElement2_Release(elem);
977     ok_(__FILE__,line) (hres == S_OK, "getElementByTagName failed: %08x\n", hres);
978     ok_(__FILE__,line) (col != NULL, "col == NULL\n");
979
980     if(exlen) {
981         types = HeapAlloc(GetProcessHeap(), 0, exlen*sizeof(elem_type_t));
982         for(i=0; i<exlen; i++)
983             types[i] = type;
984     }
985
986     _test_elem_collection(line, (IUnknown*)col, types, exlen);
987
988     HeapFree(GetProcessHeap(), 0, types);
989 }
990
991 #define get_first_child(n) _get_first_child(__LINE__,n)
992 static IHTMLDOMNode *_get_first_child(unsigned line, IUnknown *unk)
993 {
994     IHTMLDOMNode *node = _get_node_iface(line, unk);
995     IHTMLDOMNode *child = NULL;
996     HRESULT hres;
997
998     hres = IHTMLDOMNode_get_firstChild(node, &child);
999     IHTMLDOMNode_Release(node);
1000     ok_(__FILE__,line) (hres == S_OK, "get_firstChild failed: %08x\n", hres);
1001
1002     return child;
1003 }
1004
1005 #define test_node_has_child(u,b) _test_node_has_child(__LINE__,u,b)
1006 static void _test_node_has_child(unsigned line, IUnknown *unk, VARIANT_BOOL exb)
1007 {
1008     IHTMLDOMNode *node = _get_node_iface(line, unk);
1009     VARIANT_BOOL b = 0xdead;
1010     HRESULT hres;
1011
1012     hres = IHTMLDOMNode_hasChildNodes(node, &b);
1013     ok_(__FILE__,line) (hres == S_OK, "hasChildNodes failed: %08x\n", hres);
1014     ok_(__FILE__,line) (b == exb, "hasChildNodes=%x, expected %x\n", b, exb);
1015
1016     IHTMLDOMNode_Release(node);
1017 }
1018
1019 #define test_node_get_parent(u) _test_node_get_parent(__LINE__,u)
1020 static IHTMLDOMNode *_test_node_get_parent(unsigned line, IUnknown *unk)
1021 {
1022     IHTMLDOMNode *node = _get_node_iface(line, unk);
1023     IHTMLDOMNode *parent;
1024     HRESULT hres;
1025
1026     hres = IHTMLDOMNode_get_parentNode(node, &parent);
1027     IHTMLDOMNode_Release(node);
1028     ok_(__FILE__,line) (hres == S_OK, "get_parentNode failed: %08x\n", hres);
1029
1030     return parent;
1031 }
1032
1033 #define get_node_type(n) _get_node_type(__LINE__,n)
1034 static long _get_node_type(unsigned line, IUnknown *unk)
1035 {
1036     IHTMLDOMNode *node = _get_node_iface(line, unk);
1037     long type = -1;
1038     HRESULT hres;
1039
1040     hres = IHTMLDOMNode_get_nodeType(node, &type);
1041     ok(hres == S_OK, "get_nodeType failed: %08x\n", hres);
1042
1043     IHTMLDOMNode_Release(node);
1044
1045     return type;
1046 }
1047
1048 #define test_elem_get_parent(u) _test_elem_get_parent(__LINE__,u)
1049 static IHTMLElement *_test_elem_get_parent(unsigned line, IUnknown *unk)
1050 {
1051     IHTMLElement *elem = _get_elem_iface(line, unk);
1052     IHTMLElement *parent;
1053     HRESULT hres;
1054
1055     hres = IHTMLElement_get_parentElement(elem, &parent);
1056     IHTMLElement_Release(elem);
1057     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
1058
1059     return parent;
1060 }
1061
1062 #define elem_get_scroll_height(u) _elem_get_scroll_height(__LINE__,u)
1063 static long _elem_get_scroll_height(unsigned line, IUnknown *unk)
1064 {
1065     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1066     IHTMLTextContainer *txtcont;
1067     long l = -1, l2 = -1;
1068     HRESULT hres;
1069
1070     hres = IHTMLElement2_get_scrollHeight(elem, &l);
1071     ok_(__FILE__,line) (hres == S_OK, "get_scrollHeight failed: %08x\n", hres);
1072     IHTMLElement2_Release(elem);
1073
1074     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1075     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1076
1077     hres = IHTMLTextContainer_get_scrollHeight(txtcont, &l2);
1078     IHTMLTextContainer_Release(txtcont);
1079     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollHeight failed: %ld\n", l2);
1080     ok_(__FILE__,line) (l == l2, "unexpected height %ld, expected %ld\n", l2, l);
1081
1082     return l;
1083 }
1084
1085 #define elem_get_scroll_width(u) _elem_get_scroll_width(__LINE__,u)
1086 static long _elem_get_scroll_width(unsigned line, IUnknown *unk)
1087 {
1088     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1089     IHTMLTextContainer *txtcont;
1090     long l = -1, l2 = -1;
1091     HRESULT hres;
1092
1093     hres = IHTMLElement2_get_scrollWidth(elem, &l);
1094     ok_(__FILE__,line) (hres == S_OK, "get_scrollWidth failed: %08x\n", hres);
1095     IHTMLElement2_Release(elem);
1096
1097     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1098     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1099
1100     hres = IHTMLTextContainer_get_scrollWidth(txtcont, &l2);
1101     IHTMLTextContainer_Release(txtcont);
1102     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollWidth failed: %ld\n", l2);
1103     ok_(__FILE__,line) (l == l2, "unexpected width %ld, expected %ld\n", l2, l);
1104
1105     return l;
1106 }
1107
1108 #define elem_get_scroll_top(u) _elem_get_scroll_top(__LINE__,u)
1109 static long _elem_get_scroll_top(unsigned line, IUnknown *unk)
1110 {
1111     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1112     IHTMLTextContainer *txtcont;
1113     long l = -1, l2 = -1;
1114     HRESULT hres;
1115
1116     hres = IHTMLElement2_get_scrollTop(elem, &l);
1117     ok_(__FILE__,line) (hres == S_OK, "get_scrollTop failed: %08x\n", hres);
1118     IHTMLElement2_Release(elem);
1119
1120     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1121     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1122
1123     hres = IHTMLTextContainer_get_scrollTop(txtcont, &l2);
1124     IHTMLTextContainer_Release(txtcont);
1125     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollTop failed: %ld\n", l2);
1126     ok_(__FILE__,line) (l == l2, "unexpected top %ld, expected %ld\n", l2, l);
1127
1128     return l;
1129 }
1130
1131 #define elem_get_scroll_left(u) _elem_get_scroll_left(__LINE__,u)
1132 static void _elem_get_scroll_left(unsigned line, IUnknown *unk)
1133 {
1134     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1135     IHTMLTextContainer *txtcont;
1136     long l = -1, l2 = -1;
1137     HRESULT hres;
1138
1139     hres = IHTMLElement2_get_scrollLeft(elem, NULL);
1140     ok(hres == E_INVALIDARG, "expect E_INVALIDARG got 0x%08x\n", hres);
1141
1142     hres = IHTMLElement2_get_scrollLeft(elem, &l);
1143     ok(hres == S_OK, "get_scrollTop failed: %08x\n", hres);
1144     IHTMLElement2_Release(elem);
1145
1146     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1147     ok(hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1148
1149     hres = IHTMLTextContainer_get_scrollLeft(txtcont, &l2);
1150     IHTMLTextContainer_Release(txtcont);
1151     ok(hres == S_OK, "IHTMLTextContainer::get_scrollLeft failed: %ld\n", l2);
1152     ok(l == l2, "unexpected left %ld, expected %ld\n", l2, l);
1153 }
1154
1155 #define test_img_set_src(u,s) _test_img_set_src(__LINE__,u,s)
1156 static void _test_img_set_src(unsigned line, IUnknown *unk, const char *src)
1157 {
1158     IHTMLImgElement *img = _get_img_iface(line, unk);
1159     BSTR tmp;
1160     HRESULT hres;
1161
1162     tmp = a2bstr(src);
1163     hres = IHTMLImgElement_put_src(img, tmp);
1164     IHTMLImgElement_Release(img);
1165     SysFreeString(tmp);
1166     ok_(__FILE__,line) (hres == S_OK, "put_src failed: %08x\n", hres);
1167 }
1168
1169 #define test_img_alt(u,a) _test_img_alt(__LINE__,u,a)
1170 static void _test_img_alt(unsigned line, IUnknown *unk, const char *exalt)
1171 {
1172     IHTMLImgElement *img = _get_img_iface(line, unk);
1173     BSTR alt;
1174     HRESULT hres;
1175
1176     hres = IHTMLImgElement_get_alt(img, &alt);
1177     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1178     if(exalt)
1179         ok_(__FILE__,line) (!strcmp_wa(alt, exalt), "inexopected alt %s\n", dbgstr_w(alt));
1180     else
1181         ok_(__FILE__,line) (!alt, "alt != NULL\n");
1182     SysFreeString(alt);
1183 }
1184
1185 #define test_img_set_alt(u,a) _test_img_set_alt(__LINE__,u,a)
1186 static void _test_img_set_alt(unsigned line, IUnknown *unk, const char *alt)
1187 {
1188     IHTMLImgElement *img = _get_img_iface(line, unk);
1189     BSTR tmp;
1190     HRESULT hres;
1191
1192     tmp = a2bstr(alt);
1193     hres = IHTMLImgElement_put_alt(img, tmp);
1194     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1195     SysFreeString(tmp);
1196
1197     _test_img_alt(line, unk, alt);
1198 }
1199
1200 #define test_input_get_disabled(i,b) _test_input_get_disabled(__LINE__,i,b)
1201 static void _test_input_get_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL exb)
1202 {
1203     VARIANT_BOOL disabled = 100;
1204     HRESULT hres;
1205
1206     hres = IHTMLInputElement_get_disabled(input, &disabled);
1207     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1208     ok_(__FILE__,line) (disabled == exb, "disabled=%x, expected %x\n", disabled, exb);
1209 }
1210
1211 #define test_input_set_disabled(i,b) _test_input_set_disabled(__LINE__,i,b)
1212 static void _test_input_set_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL b)
1213 {
1214     HRESULT hres;
1215
1216     hres = IHTMLInputElement_put_disabled(input, b);
1217     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1218
1219     _test_input_get_disabled(line, input, b);
1220 }
1221
1222 #define test_input_value(o,t) _test_input_value(__LINE__,o,t)
1223 static void _test_input_value(unsigned line, IUnknown *unk, const char *exval)
1224 {
1225     IHTMLInputElement *input;
1226     BSTR bstr;
1227     HRESULT hres;
1228
1229     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1230     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1231     if(FAILED(hres))
1232         return;
1233
1234     hres = IHTMLInputElement_get_value(input, &bstr);
1235     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1236     if(exval)
1237         ok_(__FILE__,line) (!strcmp_wa(bstr, exval), "value=%s\n", dbgstr_w(bstr));
1238     else
1239         ok_(__FILE__,line) (!exval, "exval != NULL\n");
1240     SysFreeString(bstr);
1241     IHTMLInputElement_Release(input);
1242 }
1243
1244 #define test_input_put_value(o,v) _test_input_put_value(__LINE__,o,v)
1245 static void _test_input_put_value(unsigned line, IUnknown *unk, const char *val)
1246 {
1247     IHTMLInputElement *input;
1248     BSTR bstr;
1249     HRESULT hres;
1250
1251     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1252     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1253     if(FAILED(hres))
1254         return;
1255
1256     bstr = a2bstr(val);
1257     hres = IHTMLInputElement_get_value(input, &bstr);
1258     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1259     SysFreeString(bstr);
1260     IHTMLInputElement_Release(input);
1261 }
1262
1263 #define get_child_nodes(u) _get_child_nodes(__LINE__,u)
1264 static IHTMLDOMChildrenCollection *_get_child_nodes(unsigned line, IUnknown *unk)
1265 {
1266     IHTMLDOMNode *node = _get_node_iface(line, unk);
1267     IHTMLDOMChildrenCollection *col = NULL;
1268     IDispatch *disp;
1269     HRESULT hres;
1270
1271     hres = IHTMLDOMNode_get_childNodes(node, &disp);
1272     IHTMLDOMNode_Release(node);
1273     ok_(__FILE__,line) (hres == S_OK, "get_childNodes failed: %08x\n", hres);
1274     if(FAILED(hres))
1275         return NULL;
1276
1277     hres = IDispatch_QueryInterface(disp, &IID_IHTMLDOMChildrenCollection, (void**)&col);
1278     IDispatch_Release(disp);
1279     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDOMChildrenCollection: %08x\n", hres);
1280
1281     return col;
1282 }
1283
1284 #define test_elem_class(u,c) _test_elem_class(__LINE__,u,c)
1285 static void _test_elem_class(unsigned line, IUnknown *unk, const char *exclass)
1286 {
1287     IHTMLElement *elem = _get_elem_iface(line, unk);
1288     BSTR class = (void*)0xdeadbeef;
1289     HRESULT hres;
1290
1291     hres = IHTMLElement_get_className(elem, &class);
1292     IHTMLElement_Release(elem);
1293     ok_(__FILE__,line) (hres == S_OK, "get_className failed: %08x\n", hres);
1294     if(exclass)
1295         ok_(__FILE__,line) (!strcmp_wa(class, exclass), "unexpected className %s\n", dbgstr_w(class));
1296     else
1297         ok_(__FILE__,line) (!class, "class != NULL\n");
1298     SysFreeString(class);
1299 }
1300
1301 #define test_elem_tabindex(u,i) _test_elem_tabindex(__LINE__,u,i)
1302 static void _test_elem_tabindex(unsigned line, IUnknown *unk, short exindex)
1303 {
1304     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1305     short index = -3;
1306     HRESULT hres;
1307
1308     hres = IHTMLElement2_get_tabIndex(elem2, &index);
1309     IHTMLElement2_Release(elem2);
1310     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1311     ok_(__FILE__,line) (index == exindex, "unexpected index %d\n", index);
1312 }
1313
1314 #define test_elem_set_tabindex(u,i) _test_elem_set_tabindex(__LINE__,u,i)
1315 static void _test_elem_set_tabindex(unsigned line, IUnknown *unk, short index)
1316 {
1317     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1318     HRESULT hres;
1319
1320     hres = IHTMLElement2_put_tabIndex(elem2, index);
1321     IHTMLElement2_Release(elem2);
1322     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1323
1324     _test_elem_tabindex(line, unk, index);
1325 }
1326
1327 #define test_elem_set_class(u,c) _test_elem_set_class(__LINE__,u,c)
1328 static void _test_elem_set_class(unsigned line, IUnknown *unk, const char *class)
1329 {
1330     IHTMLElement *elem = _get_elem_iface(line, unk);
1331     BSTR tmp;
1332     HRESULT hres;
1333
1334     tmp = class ? a2bstr(class) : NULL;
1335     hres = IHTMLElement_put_className(elem, tmp);
1336     IHTMLElement_Release(elem);
1337     ok_(__FILE__,line) (hres == S_OK, "put_className failed: %08x\n", hres);
1338     SysFreeString(tmp);
1339
1340     _test_elem_class(line, unk, class);
1341 }
1342
1343 #define get_child_item(c,i) _get_child_item(__LINE__,c,i)
1344 static IHTMLDOMNode *_get_child_item(unsigned line, IHTMLDOMChildrenCollection *col, long idx)
1345 {
1346     IHTMLDOMNode *node = NULL;
1347     IDispatch *disp;
1348     HRESULT hres;
1349
1350     hres = IHTMLDOMChildrenCollection_item(col, idx, &disp);
1351     ok(hres == S_OK, "item failed: %08x\n", hres);
1352
1353     node = _get_node_iface(line, (IUnknown*)disp);
1354     IDispatch_Release(disp);
1355
1356     return node;
1357 }
1358
1359 #define test_elem_id(e,i) _test_elem_id(__LINE__,e,i)
1360 static void _test_elem_id(unsigned line, IUnknown *unk, const char *exid)
1361 {
1362     IHTMLElement *elem = _get_elem_iface(line, unk);
1363     BSTR id = (void*)0xdeadbeef;
1364     HRESULT hres;
1365
1366     hres = IHTMLElement_get_id(elem, &id);
1367     IHTMLElement_Release(elem);
1368     ok_(__FILE__,line) (hres == S_OK, "get_id failed: %08x\n", hres);
1369
1370     if(exid)
1371         ok_(__FILE__,line) (!strcmp_wa(id, exid), "unexpected id %s\n", dbgstr_w(id));
1372     else
1373         ok_(__FILE__,line) (!id, "id=%s\n", dbgstr_w(id));
1374
1375     SysFreeString(id);
1376 }
1377
1378 #define test_elem_put_id(u,i) _test_elem_put_id(__LINE__,u,i)
1379 static void _test_elem_put_id(unsigned line, IUnknown *unk, const char *new_id)
1380 {
1381     IHTMLElement *elem = _get_elem_iface(line, unk);
1382     BSTR tmp = a2bstr(new_id);
1383     HRESULT hres;
1384
1385     hres = IHTMLElement_put_id(elem, tmp);
1386     IHTMLElement_Release(elem);
1387     SysFreeString(tmp);
1388     ok_(__FILE__,line) (hres == S_OK, "put_id failed: %08x\n", hres);
1389
1390     _test_elem_id(line, unk, new_id);
1391 }
1392
1393 #define test_elem_title(u,t) _test_elem_title(__LINE__,u,t)
1394 static void _test_elem_title(unsigned line, IUnknown *unk, const char *extitle)
1395 {
1396     IHTMLElement *elem = _get_elem_iface(line, unk);
1397     BSTR title;
1398     HRESULT hres;
1399
1400     hres = IHTMLElement_get_title(elem, &title);
1401     IHTMLElement_Release(elem);
1402     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1403     if(extitle)
1404         ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1405     else
1406         ok_(__FILE__,line) (!title, "title=%s, expected NULL\n", dbgstr_w(title));
1407
1408     SysFreeString(title);
1409 }
1410
1411 #define test_elem_set_title(u,t) _test_elem_set_title(__LINE__,u,t)
1412 static void _test_elem_set_title(unsigned line, IUnknown *unk, const char *title)
1413 {
1414     IHTMLElement *elem = _get_elem_iface(line, unk);
1415     BSTR tmp;
1416     HRESULT hres;
1417
1418     tmp = a2bstr(title);
1419     hres = IHTMLElement_put_title(elem, tmp);
1420     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1421
1422     IHTMLElement_Release(elem);
1423     SysFreeString(tmp);
1424 }
1425
1426 #define test_node_get_value_str(u,e) _test_node_get_value_str(__LINE__,u,e)
1427 static void _test_node_get_value_str(unsigned line, IUnknown *unk, const char *exval)
1428 {
1429     IHTMLDOMNode *node = _get_node_iface(line, unk);
1430     VARIANT var;
1431     HRESULT hres;
1432
1433     hres = IHTMLDOMNode_get_nodeValue(node, &var);
1434     IHTMLDOMNode_Release(node);
1435     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1436
1437     if(exval) {
1438         ok_(__FILE__,line) (V_VT(&var) == VT_BSTR, "vt=%d\n", V_VT(&var));
1439         ok_(__FILE__,line) (!strcmp_wa(V_BSTR(&var), exval), "unexpected value %s\n", dbgstr_w(V_BSTR(&var)));
1440     }else {
1441         ok_(__FILE__,line) (V_VT(&var) == VT_NULL, "vt=%d, expected VT_NULL\n", V_VT(&var));
1442     }
1443
1444     VariantClear(&var);
1445 }
1446
1447 #define test_node_put_value_str(u,v) _test_node_put_value_str(__LINE__,u,v)
1448 static void _test_node_put_value_str(unsigned line, IUnknown *unk, const char *val)
1449 {
1450     IHTMLDOMNode *node = _get_node_iface(line, unk);
1451     VARIANT var;
1452     HRESULT hres;
1453
1454     V_VT(&var) = VT_BSTR;
1455     V_BSTR(&var) = a2bstr(val);
1456
1457     hres = IHTMLDOMNode_put_nodeValue(node, var);
1458     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1459     IHTMLDOMNode_Release(node);
1460     VariantClear(&var);
1461 }
1462
1463 #define test_elem_client_size(u) _test_elem_client_size(__LINE__,u)
1464 static void _test_elem_client_size(unsigned line, IUnknown *unk)
1465 {
1466     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1467     long l;
1468     HRESULT hres;
1469
1470     hres = IHTMLElement2_get_clientWidth(elem, &l);
1471     ok_(__FILE__,line) (hres == S_OK, "get_clientWidth failed: %08x\n", hres);
1472     hres = IHTMLElement2_get_clientHeight(elem, &l);
1473     ok_(__FILE__,line) (hres == S_OK, "get_clientHeight failed: %08x\n", hres);
1474
1475     IHTMLElement2_Release(elem);
1476 }
1477
1478 #define test_create_elem(d,t) _test_create_elem(__LINE__,d,t)
1479 static IHTMLElement *_test_create_elem(unsigned line, IHTMLDocument2 *doc, const char *tag)
1480 {
1481     IHTMLElement *elem = NULL;
1482     BSTR tmp;
1483     HRESULT hres;
1484
1485     tmp = a2bstr(tag);
1486     hres = IHTMLDocument2_createElement(doc, tmp, &elem);
1487     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1488     ok_(__FILE__,line) (elem != NULL, "elem == NULL\n");
1489
1490     return elem;
1491 }
1492
1493 #define test_create_text(d,t) _test_create_text(__LINE__,d,t)
1494 static IHTMLDOMNode *_test_create_text(unsigned line, IHTMLDocument2 *doc, const char *text)
1495 {
1496     IHTMLDocument3 *doc3;
1497     IHTMLDOMNode *node = NULL;
1498     BSTR tmp;
1499     HRESULT hres;
1500
1501     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1502     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3: %08x\n", hres);
1503
1504     tmp = a2bstr(text);
1505     hres = IHTMLDocument3_createTextNode(doc3, tmp, &node);
1506     IHTMLDocument3_Release(doc3);
1507     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1508     ok_(__FILE__,line) (node != NULL, "node == NULL\n");
1509
1510     return node;
1511 }
1512
1513 #define test_node_append_child(n,c) _test_node_append_child(__LINE__,n,c)
1514 static IHTMLDOMNode *_test_node_append_child(unsigned line, IUnknown *node_unk, IUnknown *child_unk)
1515 {
1516     IHTMLDOMNode *node = _get_node_iface(line, node_unk);
1517     IHTMLDOMNode *child = _get_node_iface(line, child_unk);
1518     IHTMLDOMNode *new_child = NULL;
1519     HRESULT hres;
1520
1521     hres = IHTMLDOMNode_appendChild(node, child, &new_child);
1522     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1523     ok_(__FILE__,line) (new_child != NULL, "new_child == NULL\n");
1524     /* TODO  ok_(__FILE__,line) (new_child != child, "new_child == child\n"); */
1525
1526     IHTMLDOMNode_Release(node);
1527     IHTMLDOMNode_Release(child);
1528
1529     return new_child;
1530 }
1531
1532 #define test_node_remove_child(n,c) _test_node_remove_child(__LINE__,n,c)
1533 static void _test_node_remove_child(unsigned line, IUnknown *unk, IHTMLDOMNode *child)
1534 {
1535     IHTMLDOMNode *node = _get_node_iface(line, unk);
1536     IHTMLDOMNode *new_node = NULL;
1537     HRESULT hres;
1538
1539     hres = IHTMLDOMNode_removeChild(node, child, &new_node);
1540     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1541     ok_(__FILE__,line) (new_node != NULL, "new_node == NULL\n");
1542     /* TODO ok_(__FILE__,line) (new_node != child, "new_node == child\n"); */
1543
1544     IHTMLDOMNode_Release(node);
1545     IHTMLDOMNode_Release(new_node);
1546 }
1547
1548 #define test_doc_title(d,t) _test_doc_title(__LINE__,d,t)
1549 static void _test_doc_title(unsigned line, IHTMLDocument2 *doc, const char *extitle)
1550 {
1551     BSTR title = NULL;
1552     HRESULT hres;
1553
1554     hres = IHTMLDocument2_get_title(doc, &title);
1555     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1556     ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1557     SysFreeString(title);
1558 }
1559
1560 #define test_doc_set_title(d,t) _test_doc_set_title(__LINE__,d,t)
1561 static void _test_doc_set_title(unsigned line, IHTMLDocument2 *doc, const char *title)
1562 {
1563     BSTR tmp;
1564     HRESULT hres;
1565
1566     tmp = a2bstr(title);
1567     hres = IHTMLDocument2_put_title(doc, tmp);
1568     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1569     SysFreeString(tmp);
1570 }
1571
1572 static void test_elem_col_item(IHTMLElementCollection *col, LPCWSTR n,
1573         const elem_type_t *elem_types, long len)
1574 {
1575     IDispatch *disp;
1576     VARIANT name, index;
1577     DWORD i;
1578     HRESULT hres;
1579
1580     V_VT(&index) = VT_EMPTY;
1581     V_VT(&name) = VT_BSTR;
1582     V_BSTR(&name) = SysAllocString(n);
1583
1584     hres = IHTMLElementCollection_item(col, name, index, &disp);
1585     ok(hres == S_OK, "item failed: %08x\n", hres);
1586
1587     test_elem_collection((IUnknown*)disp, elem_types, len);
1588     IDispatch_Release(disp);
1589     ok(hres == S_OK, "Could not get IHTMLElementCollection interface: %08x\n", hres);
1590     if(hres != S_OK)
1591         goto cleanup;
1592
1593     V_VT(&index) = VT_I4;
1594
1595     for(i=0; i<len; i++) {
1596         V_I4(&index) = i;
1597         disp = (void*)0xdeadbeef;
1598         hres = IHTMLElementCollection_item(col, name, index, &disp);
1599         ok(hres == S_OK, "item failed: %08x\n", hres);
1600         ok(disp != NULL, "disp == NULL\n");
1601         if(FAILED(hres) || !disp)
1602             continue;
1603
1604         test_elem_type((IUnknown*)disp, elem_types[i]);
1605
1606         IDispatch_Release(disp);
1607     }
1608
1609     V_I4(&index) = len;
1610     disp = (void*)0xdeadbeef;
1611     hres = IHTMLElementCollection_item(col, name, index, &disp);
1612     ok(hres == S_OK, "item failed: %08x\n", hres);
1613     ok(disp == NULL, "disp != NULL\n");
1614
1615     V_I4(&index) = -1;
1616     disp = (void*)0xdeadbeef;
1617     hres = IHTMLElementCollection_item(col, name, index, &disp);
1618     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1619     ok(disp == NULL, "disp != NULL\n");
1620
1621 cleanup:
1622     SysFreeString(V_BSTR(&name));
1623 }
1624
1625 static IHTMLElement *get_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id, BOOL expect_success)
1626 {
1627     IHTMLElementCollection *col;
1628     IHTMLElement *elem;
1629     IDispatch *disp = (void*)0xdeadbeef;
1630     VARIANT name, index;
1631     HRESULT hres;
1632
1633     hres = IHTMLDocument2_get_all(doc, &col);
1634     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1635     ok(col != NULL, "col == NULL\n");
1636     if(FAILED(hres) || !col)
1637         return NULL;
1638
1639     V_VT(&index) = VT_EMPTY;
1640     V_VT(&name) = VT_BSTR;
1641     V_BSTR(&name) = SysAllocString(id);
1642
1643     hres = IHTMLElementCollection_item(col, name, index, &disp);
1644     IHTMLElementCollection_Release(col);
1645     SysFreeString(V_BSTR(&name));
1646     ok(hres == S_OK, "item failed: %08x\n", hres);
1647     if(!expect_success) {
1648         ok(disp == NULL, "disp != NULL\n");
1649         return NULL;
1650     }
1651
1652     ok(disp != NULL, "disp == NULL\n");
1653     if(!disp)
1654         return NULL;
1655
1656     elem = get_elem_iface((IUnknown*)disp);
1657     IDispatch_Release(disp);
1658
1659     return elem;
1660 }
1661
1662 static IHTMLElement *get_doc_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id)
1663 {
1664     IHTMLDocument3 *doc3;
1665     IHTMLElement *elem;
1666     BSTR tmp;
1667     HRESULT hres;
1668
1669     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1670     ok(hres == S_OK, "Could not get IHTMLDocument3 iface: %08x\n", hres);
1671
1672     tmp = SysAllocString(id);
1673     hres = IHTMLDocument3_getElementById(doc3, tmp, &elem);
1674     SysFreeString(tmp);
1675     ok(hres == S_OK, "getElementById(%s) failed: %08x\n", dbgstr_w(id), hres);
1676
1677     IHTMLDocument3_Release(doc3);
1678
1679     return elem;
1680 }
1681
1682 static void test_select_elem(IHTMLSelectElement *select)
1683 {
1684     test_select_type(select, "select-one");
1685     test_select_length(select, 2);
1686     test_select_selidx(select, 0);
1687     test_select_put_selidx(select, 1);
1688
1689     test_select_set_value(select, "val1");
1690     test_select_value(select, "val1");
1691 }
1692
1693 static void test_create_option_elem(IHTMLDocument2 *doc)
1694 {
1695     IHTMLOptionElement *option;
1696
1697     option = create_option_elem(doc, "test text", "test value");
1698
1699     test_option_put_text(option, "new text");
1700     test_option_put_value(option, "new value");
1701
1702     IHTMLOptionElement_Release(option);
1703 }
1704
1705 static IHTMLTxtRange *test_create_body_range(IHTMLDocument2 *doc)
1706 {
1707     IHTMLBodyElement *body;
1708     IHTMLTxtRange *range;
1709     IHTMLElement *elem;
1710     HRESULT hres;
1711
1712     hres = IHTMLDocument2_get_body(doc, &elem);
1713     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1714
1715     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
1716     IHTMLElement_Release(elem);
1717
1718     hres = IHTMLBodyElement_createTextRange(body, &range);
1719     IHTMLBodyElement_Release(body);
1720     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
1721
1722     return range;
1723 }
1724
1725 static void test_txtrange(IHTMLDocument2 *doc)
1726 {
1727     IHTMLTxtRange *body_range, *range, *range2;
1728     IHTMLSelectionObject *selection;
1729     IDispatch *disp_range;
1730     HRESULT hres;
1731
1732     body_range = test_create_body_range(doc);
1733
1734     test_range_text(body_range, "test abc 123\r\nit's text");
1735
1736     hres = IHTMLTxtRange_duplicate(body_range, &range);
1737     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1738
1739     hres = IHTMLTxtRange_duplicate(body_range, &range2);
1740     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1741     test_range_isequal(range, range2, VARIANT_TRUE);
1742
1743     test_range_text(range, "test abc 123\r\nit's text");
1744     test_range_text(body_range, "test abc 123\r\nit's text");
1745
1746     test_range_collapse(range, TRUE);
1747     test_range_isequal(range, range2, VARIANT_FALSE);
1748     test_range_inrange(range, range2, VARIANT_FALSE);
1749     test_range_inrange(range2, range, VARIANT_TRUE);
1750     IHTMLTxtRange_Release(range2);
1751
1752     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1753     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
1754     test_range_move(range, characterW, 2, 2);
1755     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1756
1757     test_range_collapse(range, FALSE);
1758     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
1759
1760     test_range_collapse(range, FALSE);
1761     test_range_expand(range, wordW, VARIANT_TRUE, "123");
1762     test_range_expand(range, wordW, VARIANT_FALSE, "123");
1763     test_range_move(range, characterW, 2, 2);
1764     test_range_expand(range, wordW, VARIANT_TRUE, "123");
1765     test_range_moveend(range, characterW, -5, -5);
1766     test_range_text(range, NULL);
1767     test_range_moveend(range, characterW, 3, 3);
1768     test_range_text(range, "c 1");
1769     test_range_expand(range, texteditW, VARIANT_TRUE, "test abc 123\r\nit's text");
1770     test_range_collapse(range, TRUE);
1771     test_range_move(range, characterW, 4, 4);
1772     test_range_moveend(range, characterW, 1, 1);
1773     test_range_text(range, " ");
1774     test_range_move(range, wordW, 1, 1);
1775     test_range_moveend(range, characterW, 2, 2);
1776     test_range_text(range, "ab");
1777
1778     IHTMLTxtRange_Release(range);
1779
1780     hres = IHTMLTxtRange_duplicate(body_range, &range);
1781     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1782
1783     test_range_text(range, "test abc 123\r\nit's text");
1784     test_range_move(range, characterW, 3, 3);
1785     test_range_moveend(range, characterW, 1, 1);
1786     test_range_text(range, "t");
1787     test_range_moveend(range, characterW, 3, 3);
1788     test_range_text(range, "t ab");
1789     test_range_moveend(range, characterW, -2, -2);
1790     test_range_text(range, "t ");
1791     test_range_move(range, characterW, 6, 6);
1792     test_range_moveend(range, characterW, 3, 3);
1793     test_range_text(range, "123");
1794     test_range_moveend(range, characterW, 2, 2);
1795     test_range_text(range, "123\r\ni");
1796
1797     IHTMLTxtRange_Release(range);
1798
1799     hres = IHTMLTxtRange_duplicate(body_range, &range);
1800     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1801
1802     test_range_move(range, wordW, 1, 1);
1803     test_range_moveend(range, characterW, 2, 2);
1804     test_range_text(range, "ab");
1805
1806     test_range_move(range, characterW, -2, -2);
1807     test_range_moveend(range, characterW, 2, 2);
1808     test_range_text(range, "t ");
1809
1810     test_range_move(range, wordW, 3, 3);
1811     test_range_move(range, wordW, -2, -2);
1812     test_range_moveend(range, characterW, 2, 2);
1813     test_range_text(range, "ab");
1814
1815     test_range_move(range, characterW, -6, -5);
1816     test_range_moveend(range, characterW, -1, 0);
1817     test_range_moveend(range, characterW, -6, 0);
1818     test_range_move(range, characterW, 2, 2);
1819     test_range_moveend(range, characterW, 2, 2);
1820     test_range_text(range, "st");
1821     test_range_moveend(range, characterW, -6, -4);
1822     test_range_moveend(range, characterW, 2, 2);
1823
1824     IHTMLTxtRange_Release(range);
1825
1826     hres = IHTMLTxtRange_duplicate(body_range, &range);
1827     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1828
1829     test_range_move(range, wordW, 2, 2);
1830     test_range_moveend(range, characterW, 2, 2);
1831     test_range_text(range, "12");
1832
1833     test_range_move(range, characterW, 15, 14);
1834     test_range_move(range, characterW, -2, -2);
1835     test_range_moveend(range, characterW, 3, 2);
1836     test_range_text(range, "t");
1837     test_range_moveend(range, characterW, -1, -1);
1838     test_range_text(range, "t");
1839     test_range_expand(range, wordW, VARIANT_TRUE, "text");
1840     test_range_move(range, characterW, -2, -2);
1841     test_range_moveend(range, characterW, 2, 2);
1842     test_range_text(range, "s ");
1843     test_range_move(range, characterW, 100, 7);
1844     test_range_move(range, wordW, 1, 0);
1845     test_range_move(range, characterW, -2, -2);
1846     test_range_moveend(range, characterW, 3, 2);
1847     test_range_text(range, "t");
1848
1849     IHTMLTxtRange_Release(range);
1850
1851     hres = IHTMLTxtRange_duplicate(body_range, &range);
1852     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1853
1854     test_range_collapse(range, TRUE);
1855     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1856     test_range_put_text(range, "word");
1857     test_range_text(body_range, "wordabc 123\r\nit's text");
1858     test_range_text(range, NULL);
1859     test_range_moveend(range, characterW, 3, 3);
1860     test_range_text(range, "abc");
1861     test_range_movestart(range, characterW, -2, -2);
1862     test_range_text(range, "rdabc");
1863     test_range_movestart(range, characterW, 3, 3);
1864     test_range_text(range, "bc");
1865     test_range_movestart(range, characterW, 4, 4);
1866     test_range_text(range, NULL);
1867     test_range_movestart(range, characterW, -3, -3);
1868     test_range_text(range, "c 1");
1869     test_range_movestart(range, characterW, -7, -6);
1870     test_range_text(range, "wordabc 1");
1871     test_range_movestart(range, characterW, 100, 22);
1872     test_range_text(range, NULL);
1873
1874     IHTMLTxtRange_Release(range);
1875     IHTMLTxtRange_Release(body_range);
1876
1877     hres = IHTMLDocument2_get_selection(doc, &selection);
1878     ok(hres == S_OK, "IHTMLDocument2_get_selection failed: %08x\n", hres);
1879
1880     hres = IHTMLSelectionObject_createRange(selection, &disp_range);
1881     ok(hres == S_OK, "IHTMLSelectionObject_createRange failed: %08x\n", hres);
1882     IHTMLSelectionObject_Release(selection);
1883
1884     hres = IDispatch_QueryInterface(disp_range, &IID_IHTMLTxtRange, (void **)&range);
1885     ok(hres == S_OK, "Could not get IID_IHTMLTxtRange interface: 0x%08x\n", hres);
1886     IDispatch_Release(disp_range);
1887
1888     test_range_text(range, NULL);
1889     test_range_moveend(range, characterW, 3, 3);
1890     test_range_text(range, "wor");
1891     test_range_parent(range, ET_BODY);
1892     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
1893     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
1894     test_range_move(range, characterW, 3, 3);
1895     test_range_expand(range, wordW, VARIANT_TRUE, "wordabc ");
1896     test_range_moveend(range, characterW, -4, -4);
1897     test_range_put_text(range, "abc def ");
1898     test_range_expand(range, texteditW, VARIANT_TRUE, "abc def abc 123\r\nit's text");
1899     test_range_move(range, wordW, 1, 1);
1900     test_range_movestart(range, characterW, -1, -1);
1901     test_range_text(range, " ");
1902     test_range_move(range, wordW, 1, 1);
1903     test_range_moveend(range, characterW, 3, 3);
1904     test_range_text(range, "def");
1905     test_range_put_text(range, "xyz");
1906     test_range_moveend(range, characterW, 1, 1);
1907     test_range_move(range, wordW, 1, 1);
1908     test_range_moveend(range, characterW, 2, 2);
1909     test_range_text(range, "ab");
1910
1911     IHTMLTxtRange_Release(range);
1912 }
1913
1914 static void test_txtrange2(IHTMLDocument2 *doc)
1915 {
1916     IHTMLTxtRange *range;
1917
1918     range = test_create_body_range(doc);
1919
1920     test_range_text(range, "abc\r\n\r\n123\r\n\r\n\r\ndef");
1921     test_range_move(range, characterW, 5, 5);
1922     test_range_moveend(range, characterW, 1, 1);
1923     test_range_text(range, "2");
1924     test_range_move(range, characterW, -3, -3);
1925     test_range_moveend(range, characterW, 3, 3);
1926     test_range_text(range, "c\r\n\r\n1");
1927     test_range_collapse(range, VARIANT_FALSE);
1928     test_range_moveend(range, characterW, 4, 4);
1929     test_range_text(range, "23");
1930     test_range_moveend(range, characterW, 1, 1);
1931     test_range_text(range, "23\r\n\r\n\r\nd");
1932     test_range_moveend(range, characterW, -1, -1);
1933     test_range_text(range, "23");
1934     test_range_moveend(range, characterW, -1, -1);
1935     test_range_text(range, "23");
1936     test_range_moveend(range, characterW, -2, -2);
1937     test_range_text(range, "2");
1938
1939     IHTMLTxtRange_Release(range);
1940 }
1941
1942 static void test_compatmode(IHTMLDocument2 *doc)
1943 {
1944     IHTMLDocument5 *doc5;
1945     BSTR mode;
1946     HRESULT hres;
1947
1948     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
1949     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
1950     if(FAILED(hres))
1951         return;
1952
1953     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
1954     IHTMLDocument5_Release(doc5);
1955     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
1956     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
1957     SysFreeString(mode);
1958 }
1959
1960 static void test_location(IHTMLDocument2 *doc)
1961 {
1962     IHTMLLocation *location, *location2;
1963     ULONG ref;
1964     HRESULT hres;
1965
1966     hres = IHTMLDocument2_get_location(doc, &location);
1967     ok(hres == S_OK, "get_location failed: %08x\n", hres);
1968
1969     hres = IHTMLDocument2_get_location(doc, &location2);
1970     ok(hres == S_OK, "get_location failed: %08x\n", hres);
1971
1972     ok(location == location2, "location != location2\n");
1973
1974     test_ifaces((IUnknown*)location, location_iids);
1975
1976     IHTMLLocation_Release(location2);
1977     ref = IHTMLLocation_Release(location);
1978     ok(!ref, "location chould be destroyed here\n");
1979 }
1980
1981 static void test_navigator(IHTMLDocument2 *doc)
1982 {
1983     IHTMLWindow2 *window;
1984     IOmNavigator *navigator, *navigator2;
1985     ULONG ref;
1986     BSTR bstr;
1987     HRESULT hres;
1988
1989     hres = IHTMLDocument2_get_parentWindow(doc, &window);
1990     ok(hres == S_OK, "parentWidnow failed: %08x\n", hres);
1991
1992     hres = IHTMLWindow2_get_navigator(window, &navigator);
1993     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1994     ok(navigator != NULL, "navigator == NULL\n");
1995     test_disp((IUnknown*)navigator, &IID_IOmNavigator);
1996
1997     hres = IHTMLWindow2_get_navigator(window, &navigator2);
1998     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1999     ok(navigator != navigator2, "navigator2 != navihgator\n");
2000
2001     IHTMLWindow2_Release(window);
2002     IOmNavigator_Release(navigator2);
2003
2004     hres = IOmNavigator_get_appCodeName(navigator, &bstr);
2005     ok(hres == S_OK, "get_appCodeName failed: %08x\n", hres);
2006     ok(!strcmp_wa(bstr, "Mozilla"), "Unexpected appCodeName %s\n", dbgstr_w(bstr));
2007     SysFreeString(bstr);
2008
2009     bstr = NULL;
2010     hres = IOmNavigator_get_platform(navigator, &bstr);
2011     ok(hres == S_OK, "get_appMinorVersion failed: %08x\n", hres);
2012     ok(!strcmp_wa(bstr, "Win32"), "unexpected platform %s\n", dbgstr_w(bstr));
2013     SysFreeString(bstr);
2014
2015     ref = IOmNavigator_Release(navigator);
2016     ok(!ref, "navigator should be destroyed here\n");
2017 }
2018
2019 static void test_current_style(IHTMLCurrentStyle *current_style)
2020 {
2021     BSTR str;
2022     HRESULT hres;
2023
2024     test_disp((IUnknown*)current_style, &DIID_DispHTMLCurrentStyle);
2025     test_ifaces((IUnknown*)current_style, cstyle_iids);
2026
2027     hres = IHTMLCurrentStyle_get_display(current_style, &str);
2028     ok(hres == S_OK, "get_display failed: %08x\n", hres);
2029     ok(!strcmp_wa(str, "block"), "get_display returned %s\n", dbgstr_w(str));
2030     SysFreeString(str);
2031 }
2032
2033 static void test_style2(IHTMLStyle2 *style2)
2034 {
2035     BSTR str;
2036     HRESULT hres;
2037
2038     str = (void*)0xdeadbeef;
2039     hres = IHTMLStyle2_get_position(style2, &str);
2040     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2041     ok(!str, "str != NULL\n");
2042
2043     str = a2bstr("absolute");
2044     hres = IHTMLStyle2_put_position(style2, str);
2045     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2046     SysFreeString(str);
2047
2048     str = NULL;
2049     hres = IHTMLStyle2_get_position(style2, &str);
2050     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2051     ok(!strcmp_wa(str, "absolute"), "get_position returned %s\n", dbgstr_w(str));
2052     SysFreeString(str);
2053 }
2054
2055 static void test_default_style(IHTMLStyle *style)
2056 {
2057     IHTMLStyle2 *style2;
2058     VARIANT_BOOL b;
2059     VARIANT v;
2060     BSTR str;
2061     HRESULT hres;
2062
2063     test_disp((IUnknown*)style, &DIID_DispHTMLStyle);
2064     test_ifaces((IUnknown*)style, style_iids);
2065
2066     str = (void*)0xdeadbeef;
2067     hres = IHTMLStyle_get_fontFamily(style, &str);
2068     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
2069     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
2070
2071     str = (void*)0xdeadbeef;
2072     hres = IHTMLStyle_get_fontWeight(style, &str);
2073     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
2074     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
2075
2076     str = (void*)0xdeadbeef;
2077     hres = IHTMLStyle_get_display(style, &str);
2078     ok(hres == S_OK, "get_display failed: %08x\n", hres);
2079     ok(!str, "display = %s\n", dbgstr_w(str));
2080
2081     str = (void*)0xdeadbeef;
2082     hres = IHTMLStyle_get_visibility(style, &str);
2083     ok(hres == S_OK, "get_visibility failed: %08x\n", hres);
2084     ok(!str, "visibility = %s\n", dbgstr_w(str));
2085
2086     V_VT(&v) = VT_NULL;
2087     hres = IHTMLStyle_get_fontSize(style, &v);
2088     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
2089     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
2090     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
2091
2092     V_VT(&v) = VT_NULL;
2093     hres = IHTMLStyle_get_color(style, &v);
2094     ok(hres == S_OK, "get_color failed: %08x\n", hres);
2095     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
2096     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
2097
2098     b = 0xfefe;
2099     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
2100     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
2101     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
2102
2103     b = 0xfefe;
2104     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
2105     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
2106     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
2107
2108     V_VT(&v) = VT_EMPTY;
2109     hres = IHTMLStyle_get_width(style, &v);
2110     ok(hres == S_OK, "get_width failed: %08x\n", hres);
2111     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2112     ok(!V_BSTR(&v), "V_BSTR(v)=%p\n", V_BSTR(&v));
2113
2114     V_VT(&v) = VT_BSTR;
2115     V_BSTR(&v) = a2bstr("auto");
2116     hres = IHTMLStyle_put_width(style, v);
2117     ok(hres == S_OK, "put_width failed: %08x\n", hres);
2118     VariantClear(&v);
2119
2120     V_VT(&v) = VT_EMPTY;
2121     hres = IHTMLStyle_get_width(style, &v);
2122     ok(hres == S_OK, "get_width failed: %08x\n", hres);
2123     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2124     ok(!strcmp_wa(V_BSTR(&v), "auto"), "V_BSTR(v)=%s\n", dbgstr_w(V_BSTR(&v)));
2125     VariantClear(&v);
2126
2127     /* margin tests */
2128     str = (void*)0xdeadbeef;
2129     hres = IHTMLStyle_get_margin(style, &str);
2130     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2131     ok(!str, "margin = %s\n", dbgstr_w(str));
2132
2133     str = a2bstr("1");
2134     hres = IHTMLStyle_put_margin(style, str);
2135     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2136     SysFreeString(str);
2137
2138     hres = IHTMLStyle_get_margin(style, &str);
2139     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2140     ok(strcmp_wa(str, "1"), "margin = %s\n", dbgstr_w(str));
2141
2142     hres = IHTMLStyle_put_margin(style, NULL);
2143     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2144
2145     str = NULL;
2146     hres = IHTMLStyle_get_border(style, &str);
2147     ok(hres == S_OK, "get_border failed: %08x\n", hres);
2148     ok(!str || !*str, "str is not empty\n");
2149     SysFreeString(str);
2150
2151     str = a2bstr("1px");
2152     hres = IHTMLStyle_put_border(style, str);
2153     ok(hres == S_OK, "get_border failed: %08x\n", hres);
2154     SysFreeString(str);
2155
2156     V_VT(&v) = VT_EMPTY;
2157     hres = IHTMLStyle_get_left(style, &v);
2158     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2159     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2160     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2161     VariantClear(&v);
2162
2163     V_VT(&v) = VT_BSTR;
2164     V_BSTR(&v) = a2bstr("3px");
2165     hres = IHTMLStyle_put_left(style, v);
2166     ok(hres == S_OK, "put_left failed: %08x\n", hres);
2167     VariantClear(&v);
2168
2169     V_VT(&v) = VT_EMPTY;
2170     hres = IHTMLStyle_get_left(style, &v);
2171     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2172     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2173     ok(!strcmp_wa(V_BSTR(&v), "3px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2174     VariantClear(&v);
2175
2176     V_VT(&v) = VT_EMPTY;
2177     hres = IHTMLStyle_get_top(style, &v);
2178     ok(hres == S_OK, "get_top failed: %08x\n", hres);
2179     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2180     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2181     VariantClear(&v);
2182
2183     V_VT(&v) = VT_BSTR;
2184     V_BSTR(&v) = a2bstr("3px");
2185     hres = IHTMLStyle_put_top(style, v);
2186     ok(hres == S_OK, "put_top failed: %08x\n", hres);
2187     VariantClear(&v);
2188
2189     V_VT(&v) = VT_EMPTY;
2190     hres = IHTMLStyle_get_top(style, &v);
2191     ok(hres == S_OK, "get_top failed: %08x\n", hres);
2192     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2193     ok(!strcmp_wa(V_BSTR(&v), "3px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2194     VariantClear(&v);
2195
2196     V_VT(&v) = VT_EMPTY;
2197     hres = IHTMLStyle_get_height(style, &v);
2198     ok(hres == S_OK, "get_height failed: %08x\n", hres);
2199     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2200     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2201     VariantClear(&v);
2202
2203     V_VT(&v) = VT_BSTR;
2204     V_BSTR(&v) = a2bstr("64px");
2205     hres = IHTMLStyle_put_height(style, v);
2206     ok(hres == S_OK, "put_height failed: %08x\n", hres);
2207     VariantClear(&v);
2208
2209     V_VT(&v) = VT_EMPTY;
2210     hres = IHTMLStyle_get_height(style, &v);
2211     ok(hres == S_OK, "get_height failed: %08x\n", hres);
2212     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2213     ok(!strcmp_wa(V_BSTR(&v), "64px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2214     VariantClear(&v);
2215
2216     str = (void*)0xdeadbeef;
2217     hres = IHTMLStyle_get_cursor(style, &str);
2218     ok(hres == S_OK, "get_cursor failed: %08x\n", hres);
2219     ok(!str, "get_cursor != NULL\n");
2220     SysFreeString(str);
2221
2222     str = a2bstr("default");
2223     hres = IHTMLStyle_put_cursor(style, str);
2224     ok(hres == S_OK, "get_cursor failed: %08x\n", hres);
2225     SysFreeString(str);
2226
2227     str = NULL;
2228     hres = IHTMLStyle_get_cursor(style, &str);
2229     ok(hres == S_OK, "get_cursor failed: %08x\n", hres);
2230     ok(!strcmp_wa(str, "default"), "get_cursor returned %s\n", dbgstr_w(str));
2231     SysFreeString(str);
2232
2233     V_VT(&v) = VT_EMPTY;
2234     hres = IHTMLStyle_get_verticalAlign(style, &v);
2235     ok(hres == S_OK, "get_vertivalAlign failed: %08x\n", hres);
2236     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2237     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2238     VariantClear(&v);
2239
2240     V_VT(&v) = VT_BSTR;
2241     V_BSTR(&v) = a2bstr("middle");
2242     hres = IHTMLStyle_put_verticalAlign(style, v);
2243     ok(hres == S_OK, "put_vertivalAlign failed: %08x\n", hres);
2244     VariantClear(&v);
2245
2246     V_VT(&v) = VT_EMPTY;
2247     hres = IHTMLStyle_get_verticalAlign(style, &v);
2248     ok(hres == S_OK, "get_verticalAlign failed: %08x\n", hres);
2249     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2250     ok(!strcmp_wa(V_BSTR(&v), "middle"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2251     VariantClear(&v);
2252
2253     str = (void*)0xdeadbeef;
2254     hres = IHTMLStyle_get_textAlign(style, &str);
2255     ok(hres == S_OK, "get_textAlign failed: %08x\n", hres);
2256     ok(!str, "textAlign != NULL\n");
2257
2258     str = a2bstr("center");
2259     hres = IHTMLStyle_put_textAlign(style, str);
2260     ok(hres == S_OK, "put_textAlign failed: %08x\n", hres);
2261     SysFreeString(str);
2262
2263     str = NULL;
2264     hres = IHTMLStyle_get_textAlign(style, &str);
2265     ok(hres == S_OK, "get_textAlign failed: %08x\n", hres);
2266     ok(!strcmp_wa(str, "center"), "textAlign = %s\n", dbgstr_w(V_BSTR(&v)));
2267     SysFreeString(str);
2268
2269     str = (void*)0xdeadbeef;
2270     hres = IHTMLStyle_get_filter(style, &str);
2271     ok(hres == S_OK, "get_filter failed: %08x\n", hres);
2272     ok(!str, "filter != NULL\n");
2273
2274     str = a2bstr("alpha(opacity=100)");
2275     hres = IHTMLStyle_put_filter(style, str);
2276     ok(hres == S_OK, "put_filter failed: %08x\n", hres);
2277     SysFreeString(str);
2278
2279     V_VT(&v) = VT_EMPTY;
2280     hres = IHTMLStyle_get_zIndex(style, &v);
2281     ok(hres == S_OK, "get_zIndex failed: %08x\n", hres);
2282     ok(V_VT(&v) == VT_I4, "V_VT(v)=%d\n", V_VT(&v));
2283     ok(!V_I4(&v), "V_I4(v) != 0\n");
2284     VariantClear(&v);
2285
2286     V_VT(&v) = VT_BSTR;
2287     V_BSTR(&v) = a2bstr("1");
2288     hres = IHTMLStyle_put_zIndex(style, v);
2289     ok(hres == S_OK, "put_zIndex failed: %08x\n", hres);
2290     VariantClear(&v);
2291
2292     V_VT(&v) = VT_EMPTY;
2293     hres = IHTMLStyle_get_zIndex(style, &v);
2294     ok(hres == S_OK, "get_zIndex failed: %08x\n", hres);
2295     ok(V_VT(&v) == VT_I4, "V_VT(v)=%d\n", V_VT(&v));
2296     ok(V_I4(&v) == 1, "V_I4(v) = %d\n", V_I4(&v));
2297     VariantClear(&v);
2298
2299     hres = IHTMLStyle_QueryInterface(style, &IID_IHTMLStyle2, (void**)&style2);
2300     ok(hres == S_OK, "Could not get IHTMLStyle2 iface: %08x\n", hres);
2301     if(SUCCEEDED(hres)) {
2302         test_style2(style2);
2303         IHTMLStyle2_Release(style2);
2304     }
2305 }
2306
2307 static void test_default_selection(IHTMLDocument2 *doc)
2308 {
2309     IHTMLSelectionObject *selection;
2310     IHTMLTxtRange *range;
2311     IDispatch *disp;
2312     BSTR str;
2313     HRESULT hres;
2314
2315     hres = IHTMLDocument2_get_selection(doc, &selection);
2316     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
2317
2318     hres = IHTMLSelectionObject_get_type(selection, &str);
2319     ok(hres == S_OK, "get_type failed: %08x\n", hres);
2320     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
2321     SysFreeString(str);
2322
2323     hres = IHTMLSelectionObject_createRange(selection, &disp);
2324     IHTMLSelectionObject_Release(selection);
2325     ok(hres == S_OK, "createRange failed: %08x\n", hres);
2326
2327     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
2328     IDispatch_Release(disp);
2329     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
2330
2331     test_range_text(range, NULL);
2332     IHTMLTxtRange_Release(range);
2333 }
2334
2335 static void test_default_body(IHTMLBodyElement *body)
2336 {
2337     long l;
2338     BSTR bstr;
2339     HRESULT hres;
2340     VARIANT v;
2341     WCHAR sBodyText[] = {'#','F','F','0','0','0','0',0};
2342     WCHAR sTextInvalid[] = {'I','n','v','a','l','i','d',0};
2343     WCHAR sResInvalid[] = {'#','0','0','a','0','d','0',0};
2344
2345     bstr = (void*)0xdeadbeef;
2346     hres = IHTMLBodyElement_get_background(body, &bstr);
2347     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2348     ok(bstr == NULL, "bstr != NULL\n");
2349
2350     l = elem_get_scroll_height((IUnknown*)body);
2351     ok(l != -1, "scrollHeight == -1\n");
2352     l = elem_get_scroll_width((IUnknown*)body);
2353     ok(l != -1, "scrollWidth == -1\n");
2354     l = elem_get_scroll_top((IUnknown*)body);
2355     ok(!l, "scrollTop = %ld\n", l);
2356     elem_get_scroll_left((IUnknown*)body);
2357
2358     /* get_text tests */
2359     hres = IHTMLBodyElement_get_text(body, &v);
2360     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2361     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2362     ok(bstr == NULL, "bstr != NULL\n");
2363
2364
2365     /* get_text - Invalid Text */
2366     V_VT(&v) = VT_BSTR;
2367     V_BSTR(&v) = SysAllocString(sTextInvalid);
2368     hres = IHTMLBodyElement_put_text(body, v);
2369     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2370
2371     V_VT(&v) = VT_NULL;
2372     hres = IHTMLBodyElement_get_text(body, &v);
2373     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2374     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2375     ok(!lstrcmpW(sResInvalid, V_BSTR(&v)), "bstr != sResInvalid\n");
2376     VariantClear(&v);
2377
2378     /* get_text - Valid Text */
2379     V_VT(&v) = VT_BSTR;
2380     V_BSTR(&v) = SysAllocString(sBodyText);
2381     hres = IHTMLBodyElement_put_text(body, v);
2382     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2383
2384     V_VT(&v) = VT_NULL;
2385     hres = IHTMLBodyElement_get_text(body, &v);
2386     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2387     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2388     ok(lstrcmpW(bstr, V_BSTR(&v)), "bstr != V_BSTR(&v)\n");
2389     VariantClear(&v);
2390 }
2391
2392 static void test_body_funs(IHTMLBodyElement *body)
2393 {
2394     static WCHAR sRed[] = {'r','e','d',0};
2395     static WCHAR sRedbg[] = {'#','f','f','0','0','0','0',0};
2396     VARIANT vbg;
2397     VARIANT vDefaultbg;
2398     HRESULT hres;
2399
2400     hres = IHTMLBodyElement_get_bgColor(body, &vDefaultbg);
2401     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2402     ok(V_VT(&vDefaultbg) == VT_BSTR, "bstr != NULL\n");
2403
2404     V_VT(&vbg) = VT_BSTR;
2405     V_BSTR(&vbg) = SysAllocString(sRed);
2406     hres = IHTMLBodyElement_put_bgColor(body, vbg);
2407     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2408     VariantClear(&vbg);
2409
2410     hres = IHTMLBodyElement_get_bgColor(body, &vbg);
2411     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2412     ok(V_VT(&vDefaultbg) == VT_BSTR, "V_VT(&vDefaultbg) != VT_BSTR\n");
2413     ok(!lstrcmpW(V_BSTR(&vbg), sRedbg), "Unexpected type %s\n", dbgstr_w(V_BSTR(&vbg)));
2414     VariantClear(&vbg);
2415
2416     /* Restore Originial */
2417     hres = IHTMLBodyElement_put_bgColor(body, vDefaultbg);
2418     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2419     VariantClear(&vDefaultbg);
2420 }
2421
2422 static void test_window(IHTMLDocument2 *doc)
2423 {
2424     IHTMLWindow2 *window, *window2, *self;
2425     IHTMLDocument2 *doc2 = NULL;
2426     IDispatch *disp;
2427     HRESULT hres;
2428
2429     hres = IHTMLDocument2_get_parentWindow(doc, &window);
2430     ok(hres == S_OK, "get_parentElement failed: %08x\n", hres);
2431     test_ifaces((IUnknown*)window, window_iids);
2432     test_disp((IUnknown*)window, &DIID_DispHTMLWindow2);
2433
2434     hres = IHTMLWindow2_get_document(window, &doc2);
2435     ok(hres == S_OK, "get_document failed: %08x\n", hres);
2436     ok(doc2 != NULL, "doc2 == NULL\n");
2437
2438     IHTMLDocument_Release(doc2);
2439
2440     hres = IHTMLWindow2_get_window(window, &window2);
2441     ok(hres == S_OK, "get_window failed: %08x\n", hres);
2442     ok(window2 != NULL, "window2 == NULL\n");
2443
2444     hres = IHTMLWindow2_get_self(window, &self);
2445     ok(hres == S_OK, "get_window failed: %08x\n", hres);
2446     ok(window2 != NULL, "self == NULL\n");
2447
2448     ok(self == window2, "self != window2\n");
2449
2450     IHTMLWindow2_Release(window2);
2451     IHTMLWindow2_Release(self);
2452
2453     disp = NULL;
2454     hres = IHTMLDocument2_get_Script(doc, &disp);
2455     ok(hres == S_OK, "get_Script failed: %08x\n", hres);
2456     ok(disp == (void*)window, "disp != window\n");
2457     IDispatch_Release(disp);
2458
2459     IHTMLWindow2_Release(window);
2460 }
2461
2462 static void test_defaults(IHTMLDocument2 *doc)
2463 {
2464     IHTMLStyleSheetsCollection *stylesheetcol;
2465     IHTMLCurrentStyle *cstyle;
2466     IHTMLBodyElement *body;
2467     IHTMLElement2 *elem2;
2468     IHTMLElement *elem;
2469     IHTMLStyle *style;
2470     long l;
2471     HRESULT hres;
2472
2473     hres = IHTMLDocument2_get_body(doc, &elem);
2474     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2475
2476     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
2477     ok(hres == S_OK, "Could not get IHTMBodyElement: %08x\n", hres);
2478     test_default_body(body);
2479     test_body_funs(body);
2480     IHTMLBodyElement_Release(body);
2481
2482     hres = IHTMLElement_get_style(elem, &style);
2483     ok(hres == S_OK, "get_style failed: %08x\n", hres);
2484
2485     test_default_style(style);
2486     test_window(doc);
2487     test_compatmode(doc);
2488     test_location(doc);
2489     test_navigator(doc);
2490
2491     IHTMLStyle_Release(style);
2492
2493     elem2 = get_elem2_iface((IUnknown*)elem);
2494     hres = IHTMLElement2_get_currentStyle(elem2, &cstyle);
2495     ok(hres == S_OK, "get_currentStyle failed: %08x\n", hres);
2496     if(SUCCEEDED(hres)) {
2497         test_current_style(cstyle);
2498         IHTMLCurrentStyle_Release(cstyle);
2499     }
2500     IHTMLElement2_Release(elem2);
2501
2502     IHTMLElement_Release(elem);
2503
2504     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
2505     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
2506
2507     l = 0xdeadbeef;
2508     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
2509     ok(hres == S_OK, "get_length failed: %08x\n", hres);
2510     ok(l == 0, "length = %ld\n", l);
2511
2512     IHTMLStyleSheetsCollection_Release(stylesheetcol);
2513
2514     test_default_selection(doc);
2515     test_doc_title(doc, "");
2516 }
2517
2518 static void test_tr_elem(IHTMLElement *elem)
2519 {
2520     IHTMLElementCollection *col;
2521     IHTMLTableRow *row;
2522     HRESULT hres;
2523
2524     static const elem_type_t cell_types[] = {ET_TD,ET_TD};
2525
2526     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLTableRow, (void**)&row);
2527     ok(hres == S_OK, "Could not get IHTMLTableRow iface: %08x\n", hres);
2528     if(FAILED(hres))
2529         return;
2530
2531     col = NULL;
2532     hres = IHTMLTableRow_get_cells(row, &col);
2533     ok(hres == S_OK, "get_cells failed: %08x\n", hres);
2534     ok(col != NULL, "get_cells returned NULL\n");
2535
2536     test_elem_collection((IUnknown*)col, cell_types, sizeof(cell_types)/sizeof(*cell_types));
2537     IHTMLElementCollection_Release(col);
2538
2539     IHTMLTable_Release(row);
2540 }
2541
2542 static void test_table_elem(IHTMLElement *elem)
2543 {
2544     IHTMLElementCollection *col;
2545     IHTMLTable *table;
2546     HRESULT hres;
2547
2548     static const elem_type_t row_types[] = {ET_TR,ET_TR};
2549
2550     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLTable, (void**)&table);
2551     ok(hres == S_OK, "Could not get IHTMLTable iface: %08x\n", hres);
2552     if(FAILED(hres))
2553         return;
2554
2555     col = NULL;
2556     hres = IHTMLTable_get_rows(table, &col);
2557     ok(hres == S_OK, "get_rows failed: %08x\n", hres);
2558     ok(col != NULL, "get_ros returned NULL\n");
2559
2560     test_elem_collection((IUnknown*)col, row_types, sizeof(row_types)/sizeof(*row_types));
2561     IHTMLElementCollection_Release(col);
2562
2563     IHTMLTable_Release(table);
2564 }
2565
2566 static void test_stylesheet(IDispatch *disp)
2567 {
2568     IHTMLStyleSheetRulesCollection *col = NULL;
2569     IHTMLStyleSheet *stylesheet;
2570     HRESULT hres;
2571
2572     hres = IDispatch_QueryInterface(disp, &IID_IHTMLStyleSheet, (void**)&stylesheet);
2573     ok(hres == S_OK, "Could not get IHTMLStyleSheet: %08x\n", hres);
2574
2575     hres = IHTMLStyleSheet_get_rules(stylesheet, &col);
2576     ok(hres == S_OK, "get_rules failed: %08x\n", hres);
2577     ok(col != NULL, "col == NULL\n");
2578
2579     IHTMLStyleSheetRulesCollection_Release(col);
2580     IHTMLStyleSheet_Release(stylesheet);
2581 }
2582
2583 static void test_stylesheets(IHTMLDocument2 *doc)
2584 {
2585     IHTMLStyleSheetsCollection *col = NULL;
2586     VARIANT idx, res;
2587     long len = 0;
2588     HRESULT hres;
2589
2590     hres = IHTMLDocument2_get_styleSheets(doc, &col);
2591     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
2592     ok(col != NULL, "col == NULL\n");
2593
2594     hres = IHTMLStyleSheetsCollection_get_length(col, &len);
2595     ok(hres == S_OK, "get_length failed: %08x\n", hres);
2596     ok(len == 1, "len=%ld\n", len);
2597
2598     VariantInit(&res);
2599     V_VT(&idx) = VT_I4;
2600     V_I4(&idx) = 0;
2601
2602     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
2603     ok(hres == S_OK, "item failed: %08x\n", hres);
2604     ok(V_VT(&res) == VT_DISPATCH, "V_VT(res) = %d\n", V_VT(&res));
2605     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
2606     test_stylesheet(V_DISPATCH(&res));
2607     VariantClear(&res);
2608
2609     V_VT(&res) = VT_I4;
2610     V_VT(&idx) = VT_I4;
2611     V_I4(&idx) = 1;
2612
2613     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
2614     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2615     ok(V_VT(&res) == VT_EMPTY, "V_VT(res) = %d\n", V_VT(&res));
2616     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
2617     VariantClear(&res);
2618
2619     IHTMLStyleSheetsCollection_Release(col);
2620 }
2621
2622 static void test_child_col_disp(IHTMLDOMChildrenCollection *col)
2623 {
2624     IDispatchEx *dispex;
2625     IHTMLDOMNode *node;
2626     DISPPARAMS dp = {NULL, NULL, 0, 0};
2627     VARIANT var;
2628     EXCEPINFO ei;
2629     long type;
2630     DISPID id;
2631     BSTR bstr;
2632     HRESULT hres;
2633
2634     static const WCHAR w0[] = {'0',0};
2635     static const WCHAR w100[] = {'1','0','0',0};
2636
2637     hres = IHTMLDOMChildrenCollection_QueryInterface(col, &IID_IDispatchEx, (void**)&dispex);
2638     ok(hres == S_OK, "Could not get IDispatchEx: %08x\n", hres);
2639
2640     bstr = SysAllocString(w0);
2641     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
2642     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
2643     SysFreeString(bstr);
2644
2645     VariantInit(&var);
2646     hres = IDispatchEx_InvokeEx(dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
2647     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
2648     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
2649     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(var) == NULL\n");
2650     node = get_node_iface((IUnknown*)V_DISPATCH(&var));
2651     type = get_node_type((IUnknown*)node);
2652     ok(type == 3, "type=%ld\n", type);
2653     IHTMLDOMNode_Release(node);
2654     VariantClear(&var);
2655
2656     bstr = SysAllocString(w100);
2657     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
2658     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
2659     SysFreeString(bstr);
2660
2661     IDispatchEx_Release(dispex);
2662 }
2663
2664
2665
2666 static void test_elems(IHTMLDocument2 *doc)
2667 {
2668     IHTMLElementCollection *col;
2669     IHTMLDOMChildrenCollection *child_col;
2670     IHTMLElement *elem, *elem2, *elem3;
2671     IHTMLDOMNode *node, *node2;
2672     IDispatch *disp;
2673     long type;
2674     HRESULT hres;
2675
2676     static const WCHAR imgidW[] = {'i','m','g','i','d',0};
2677     static const WCHAR inW[] = {'i','n',0};
2678     static const WCHAR xW[] = {'x',0};
2679     static const WCHAR sW[] = {'s',0};
2680     static const WCHAR scW[] = {'s','c',0};
2681     static const WCHAR xxxW[] = {'x','x','x',0};
2682     static const WCHAR tblW[] = {'t','b','l',0};
2683     static const WCHAR row2W[] = {'r','o','w','2',0};
2684
2685     static const elem_type_t all_types[] = {
2686         ET_HTML,
2687         ET_HEAD,
2688         ET_TITLE,
2689         ET_STYLE,
2690         ET_BODY,
2691         ET_COMMENT,
2692         ET_A,
2693         ET_INPUT,
2694         ET_SELECT,
2695         ET_OPTION,
2696         ET_OPTION,
2697         ET_TEXTAREA,
2698         ET_TABLE,
2699         ET_TBODY,
2700         ET_TR,
2701         ET_TR,
2702         ET_TD,
2703         ET_TD,
2704         ET_SCRIPT,
2705         ET_TEST,
2706         ET_IMG,
2707         ET_IFRAME
2708     };
2709
2710     static const elem_type_t item_types[] = {
2711         ET_A,
2712         ET_OPTION,
2713         ET_TEXTAREA
2714     };
2715
2716     hres = IHTMLDocument2_get_all(doc, &col);
2717     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2718     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
2719     test_elem_col_item(col, xW, item_types, sizeof(item_types)/sizeof(item_types[0]));
2720     IHTMLElementCollection_Release(col);
2721
2722     elem = get_doc_elem(doc);
2723     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
2724     hres = IHTMLElement_get_all(elem, &disp);
2725     IHTMLElement_Release(elem);
2726     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2727
2728     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&col);
2729     IDispatch_Release(disp);
2730     ok(hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
2731     test_elem_collection((IUnknown*)col, all_types+1, sizeof(all_types)/sizeof(all_types[0])-1);
2732     IHTMLElementCollection_Release(col);
2733
2734     get_elem_by_id(doc, xxxW, FALSE);
2735     elem = get_doc_elem_by_id(doc, xxxW);
2736     ok(!elem, "elem != NULL\n");
2737
2738     elem = get_doc_elem_by_id(doc, sW);
2739     ok(elem != NULL, "elem == NULL\n");
2740     if(elem) {
2741         test_elem_type((IUnknown*)elem, ET_SELECT);
2742         test_elem_attr(elem, xxxW, NULL);
2743         test_elem_attr(elem, idW, sW);
2744         test_elem_class((IUnknown*)elem, NULL);
2745         test_elem_set_class((IUnknown*)elem, "cl");
2746         test_elem_set_class((IUnknown*)elem, NULL);
2747         test_elem_tabindex((IUnknown*)elem, 0);
2748         test_elem_set_tabindex((IUnknown*)elem, 1);
2749
2750         node = test_node_get_parent((IUnknown*)elem);
2751         ok(node != NULL, "node == NULL\n");
2752         test_node_name((IUnknown*)node, "BODY");
2753         node2 = test_node_get_parent((IUnknown*)node);
2754         IHTMLDOMNode_Release(node);
2755         ok(node2 != NULL, "node == NULL\n");
2756         test_node_name((IUnknown*)node2, "HTML");
2757         node = test_node_get_parent((IUnknown*)node2);
2758         IHTMLDOMNode_Release(node2);
2759         ok(node != NULL, "node == NULL\n");
2760         test_node_name((IUnknown*)node, "#document");
2761         type = get_node_type((IUnknown*)node);
2762         ok(type == 9, "type=%ld, expected 9\n", type);
2763         node2 = test_node_get_parent((IUnknown*)node);
2764         IHTMLDOMNode_Release(node);
2765         ok(node2 == NULL, "node != NULL\n");
2766
2767         elem2 = test_elem_get_parent((IUnknown*)elem);
2768         ok(elem2 != NULL, "elem2 == NULL\n");
2769         test_node_name((IUnknown*)elem2, "BODY");
2770         elem3 = test_elem_get_parent((IUnknown*)elem2);
2771         IHTMLElement_Release(elem2);
2772         ok(elem3 != NULL, "elem3 == NULL\n");
2773         test_node_name((IUnknown*)elem3, "HTML");
2774         elem2 = test_elem_get_parent((IUnknown*)elem3);
2775         IHTMLElement_Release(elem3);
2776         ok(elem2 == NULL, "elem2 != NULL\n");
2777
2778         test_elem_getelembytag((IUnknown*)elem, ET_OPTION, 2);
2779         test_elem_getelembytag((IUnknown*)elem, ET_SELECT, 0);
2780         test_elem_getelembytag((IUnknown*)elem, ET_HTML, 0);
2781
2782         IHTMLElement_Release(elem);
2783     }
2784
2785     elem = get_elem_by_id(doc, sW, TRUE);
2786     if(elem) {
2787         IHTMLSelectElement *select;
2788
2789         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLSelectElement, (void**)&select);
2790         ok(hres == S_OK, "Could not get IHTMLSelectElement interface: %08x\n", hres);
2791
2792         test_select_elem(select);
2793
2794         test_elem_title((IUnknown*)select, NULL);
2795         test_elem_set_title((IUnknown*)select, "Title");
2796         test_elem_title((IUnknown*)select, "Title");
2797         test_elem_offset((IUnknown*)select);
2798
2799         node = get_first_child((IUnknown*)select);
2800         ok(node != NULL, "node == NULL\n");
2801         if(node) {
2802             test_elem_type((IUnknown*)node, ET_OPTION);
2803             IHTMLDOMNode_Release(node);
2804         }
2805
2806         type = get_node_type((IUnknown*)select);
2807         ok(type == 1, "type=%ld\n", type);
2808
2809         IHTMLSelectElement_Release(select);
2810         IHTMLElement_Release(elem);
2811     }
2812
2813     elem = get_elem_by_id(doc, scW, TRUE);
2814     if(elem) {
2815         IHTMLScriptElement *script;
2816         BSTR type;
2817
2818         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLScriptElement, (void**)&script);
2819         ok(hres == S_OK, "Could not get IHTMLScriptElement interface: %08x\n", hres);
2820
2821         hres = IHTMLScriptElement_get_type(script, &type);
2822         ok(hres == S_OK, "get_type failed: %08x\n", hres);
2823         ok(!lstrcmpW(type, text_javascriptW), "Unexpected type %s\n", dbgstr_w(type));
2824         SysFreeString(type);
2825
2826         IHTMLScriptElement_Release(script);
2827     }
2828
2829     elem = get_elem_by_id(doc, inW, TRUE);
2830     if(elem) {
2831         IHTMLInputElement *input;
2832
2833         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLInputElement, (void**)&input);
2834         ok(hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
2835
2836         test_elem_id((IUnknown*)elem, "in");
2837         test_elem_put_id((IUnknown*)elem, "newin");
2838         test_input_get_disabled(input, VARIANT_FALSE);
2839         test_input_set_disabled(input, VARIANT_TRUE);
2840         test_input_set_disabled(input, VARIANT_FALSE);
2841         test_elem_client_size((IUnknown*)elem);
2842
2843         test_node_get_value_str((IUnknown*)elem, NULL);
2844         test_node_put_value_str((IUnknown*)elem, "test");
2845         test_node_get_value_str((IUnknown*)elem, NULL);
2846         test_input_value((IUnknown*)elem, NULL);
2847         test_input_put_value((IUnknown*)elem, "test");
2848         test_input_value((IUnknown*)elem, NULL);
2849         test_elem_class((IUnknown*)elem, "testclass");
2850         test_elem_tabindex((IUnknown*)elem, 2);
2851         test_elem_set_tabindex((IUnknown*)elem, 3);
2852         test_elem_title((IUnknown*)elem, "test title");
2853
2854         IHTMLInputElement_Release(input);
2855         IHTMLElement_Release(elem);
2856     }
2857
2858     elem = get_elem_by_id(doc, imgidW, TRUE);
2859     if(elem) {
2860         test_img_set_src((IUnknown*)elem, "about:blank");
2861         test_img_alt((IUnknown*)elem, NULL);
2862         test_img_set_alt((IUnknown*)elem, "alt test");
2863         IHTMLElement_Release(elem);
2864     }
2865
2866     elem = get_doc_elem_by_id(doc, tblW);
2867     ok(elem != NULL, "elem == NULL\n");
2868     if(elem) {
2869         test_table_elem(elem);
2870         IHTMLElement_Release(elem);
2871     }
2872
2873     elem = get_doc_elem_by_id(doc, row2W);
2874     ok(elem != NULL, "elem == NULL\n");
2875     if(elem) {
2876         test_tr_elem(elem);
2877         IHTMLElement_Release(elem);
2878     }
2879
2880     hres = IHTMLDocument2_get_body(doc, &elem);
2881     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2882
2883     node = get_first_child((IUnknown*)elem);
2884     ok(node != NULL, "node == NULL\n");
2885     if(node) {
2886         test_ifaces((IUnknown*)node, text_iids);
2887         test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
2888
2889         node2 = get_first_child((IUnknown*)node);
2890         ok(!node2, "node2 != NULL\n");
2891
2892         type = get_node_type((IUnknown*)node);
2893         ok(type == 3, "type=%ld\n", type);
2894
2895         test_node_get_value_str((IUnknown*)node, "text test");
2896         test_node_put_value_str((IUnknown*)elem, "test text");
2897         test_node_get_value_str((IUnknown*)node, "text test");
2898
2899         IHTMLDOMNode_Release(node);
2900     }
2901
2902     child_col = get_child_nodes((IUnknown*)elem);
2903     ok(child_col != NULL, "child_coll == NULL\n");
2904     if(child_col) {
2905         long length = 0;
2906
2907         test_disp((IUnknown*)child_col, &DIID_DispDOMChildrenCollection);
2908
2909         hres = IHTMLDOMChildrenCollection_get_length(child_col, &length);
2910         ok(hres == S_OK, "get_length failed: %08x\n", hres);
2911         ok(length, "length=0\n");
2912
2913         node = get_child_item(child_col, 0);
2914         ok(node != NULL, "node == NULL\n");
2915         if(node) {
2916             type = get_node_type((IUnknown*)node);
2917             ok(type == 3, "type=%ld\n", type);
2918             IHTMLDOMNode_Release(node);
2919         }
2920
2921         node = get_child_item(child_col, 1);
2922         ok(node != NULL, "node == NULL\n");
2923         if(node) {
2924             type = get_node_type((IUnknown*)node);
2925             ok(type == 8, "type=%ld\n", type);
2926
2927             test_elem_id((IUnknown*)node, NULL);
2928             IHTMLDOMNode_Release(node);
2929         }
2930
2931         disp = (void*)0xdeadbeef;
2932         hres = IHTMLDOMChildrenCollection_item(child_col, 6000, &disp);
2933         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2934         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
2935
2936         disp = (void*)0xdeadbeef;
2937         hres = IHTMLDOMChildrenCollection_item(child_col, length, &disp);
2938         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2939         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
2940
2941         test_child_col_disp(child_col);
2942
2943         IHTMLDOMChildrenCollection_Release(child_col);
2944     }
2945
2946     IHTMLElement_Release(elem);
2947
2948     test_stylesheets(doc);
2949     test_create_option_elem(doc);
2950
2951     test_doc_title(doc, "test");
2952     test_doc_set_title(doc, "test title");
2953     test_doc_title(doc, "test title");
2954 }
2955
2956 static void test_create_elems(IHTMLDocument2 *doc)
2957 {
2958     IHTMLElement *elem, *body, *elem2;
2959     IHTMLDOMNode *node;
2960     IDispatch *disp;
2961     long type;
2962     HRESULT hres;
2963
2964     static const elem_type_t types1[] = { ET_TESTG };
2965
2966     elem = test_create_elem(doc, "TEST");
2967     test_elem_tag((IUnknown*)elem, "TEST");
2968     type = get_node_type((IUnknown*)elem);
2969     ok(type == 1, "type=%ld\n", type);
2970     test_ifaces((IUnknown*)elem, elem_iids);
2971     test_disp((IUnknown*)elem, &DIID_DispHTMLGenericElement);
2972
2973     hres = IHTMLDocument2_get_body(doc, &body);
2974     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2975     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
2976
2977     node = test_node_append_child((IUnknown*)body, (IUnknown*)elem);
2978     test_node_has_child((IUnknown*)body, VARIANT_TRUE);
2979     elem2 = get_elem_iface((IUnknown*)node);
2980     IHTMLElement_Release(elem2);
2981
2982     hres = IHTMLElement_get_all(body, &disp);
2983     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2984     test_elem_collection((IUnknown*)disp, types1, sizeof(types1)/sizeof(types1[0]));
2985     IDispatch_Release(disp);
2986
2987     test_node_remove_child((IUnknown*)body, node);
2988
2989     hres = IHTMLElement_get_all(body, &disp);
2990     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2991     test_elem_collection((IUnknown*)disp, NULL, 0);
2992     IDispatch_Release(disp);
2993     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
2994
2995     IHTMLElement_Release(body);
2996     IHTMLElement_Release(elem);
2997     IHTMLDOMNode_Release(node);
2998
2999     node = test_create_text(doc, "test");
3000     test_ifaces((IUnknown*)node, text_iids);
3001     test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
3002     IHTMLDOMNode_Release(node);
3003 }
3004
3005 static void test_exec(IUnknown *unk, const GUID *grpid, DWORD cmdid, VARIANT *in, VARIANT *out)
3006 {
3007     IOleCommandTarget *cmdtrg;
3008     HRESULT hres;
3009
3010     hres = IHTMLTxtRange_QueryInterface(unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
3011     ok(hres == S_OK, "Could not get IOleCommandTarget interface: %08x\n", hres);
3012
3013     hres = IOleCommandTarget_Exec(cmdtrg, grpid, cmdid, 0, in, out);
3014     ok(hres == S_OK, "Exec failed: %08x\n", hres);
3015
3016     IOleCommandTarget_Release(cmdtrg);
3017 }
3018
3019 static void test_indent(IHTMLDocument2 *doc)
3020 {
3021     IHTMLElementCollection *col;
3022     IHTMLTxtRange *range;
3023     HRESULT hres;
3024
3025     static const elem_type_t all_types[] = {
3026         ET_HTML,
3027         ET_HEAD,
3028         ET_TITLE,
3029         ET_BODY,
3030         ET_BR,
3031         ET_A,
3032     };
3033
3034     static const elem_type_t indent_types[] = {
3035         ET_HTML,
3036         ET_HEAD,
3037         ET_TITLE,
3038         ET_BODY,
3039         ET_BLOCKQUOTE,
3040         ET_P,
3041         ET_BR,
3042         ET_A,
3043     };
3044
3045     hres = IHTMLDocument2_get_all(doc, &col);
3046     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3047     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
3048     IHTMLElementCollection_Release(col);
3049
3050     range = test_create_body_range(doc);
3051     test_exec((IUnknown*)range, &CGID_MSHTML, IDM_INDENT, NULL, NULL);
3052     IHTMLTxtRange_Release(range);
3053
3054     hres = IHTMLDocument2_get_all(doc, &col);
3055     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3056     test_elem_collection((IUnknown*)col, indent_types, sizeof(indent_types)/sizeof(indent_types[0]));
3057     IHTMLElementCollection_Release(col);
3058 }
3059
3060 static IHTMLDocument2 *notif_doc;
3061 static BOOL doc_complete;
3062
3063 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
3064         REFIID riid, void**ppv)
3065 {
3066     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
3067         *ppv = iface;
3068         return S_OK;
3069     }
3070
3071     ok(0, "unexpected call\n");
3072     return E_NOINTERFACE;
3073 }
3074
3075 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
3076 {
3077     return 2;
3078 }
3079
3080 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
3081 {
3082     return 1;
3083 }
3084
3085 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
3086 {
3087     if(dispID == DISPID_READYSTATE){
3088         BSTR state;
3089         HRESULT hres;
3090
3091         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
3092
3093         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
3094         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
3095
3096         if(!lstrcmpW(state, completeW))
3097             doc_complete = TRUE;
3098
3099         SysFreeString(state);
3100     }
3101
3102     return S_OK;
3103 }
3104
3105 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
3106 {
3107     ok(0, "unexpected call\n");
3108     return E_NOTIMPL;
3109 }
3110
3111 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
3112     PropertyNotifySink_QueryInterface,
3113     PropertyNotifySink_AddRef,
3114     PropertyNotifySink_Release,
3115     PropertyNotifySink_OnChanged,
3116     PropertyNotifySink_OnRequestEdit
3117 };
3118
3119 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
3120
3121 static IHTMLDocument2 *create_doc_with_string(const char *str)
3122 {
3123     IPersistStreamInit *init;
3124     IStream *stream;
3125     IHTMLDocument2 *doc;
3126     HGLOBAL mem;
3127     SIZE_T len;
3128
3129     notif_doc = doc = create_document();
3130     if(!doc)
3131         return NULL;
3132
3133     doc_complete = FALSE;
3134     len = strlen(str);
3135     mem = GlobalAlloc(0, len);
3136     memcpy(mem, str, len);
3137     CreateStreamOnHGlobal(mem, TRUE, &stream);
3138
3139     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
3140
3141     IPersistStreamInit_Load(init, stream);
3142     IPersistStreamInit_Release(init);
3143     IStream_Release(stream);
3144
3145     return doc;
3146 }
3147
3148 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
3149 {
3150     IConnectionPointContainer *container;
3151     IConnectionPoint *cp;
3152     DWORD cookie;
3153     HRESULT hres;
3154
3155     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
3156     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
3157
3158     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
3159     IConnectionPointContainer_Release(container);
3160     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
3161
3162     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
3163     IConnectionPoint_Release(cp);
3164     ok(hres == S_OK, "Advise failed: %08x\n", hres);
3165 }
3166
3167 typedef void (*domtest_t)(IHTMLDocument2*);
3168
3169 static void run_domtest(const char *str, domtest_t test)
3170 {
3171     IHTMLDocument2 *doc;
3172     IHTMLElement *body = NULL;
3173     ULONG ref;
3174     MSG msg;
3175     HRESULT hres;
3176
3177     doc = create_doc_with_string(str);
3178     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
3179
3180     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
3181         TranslateMessage(&msg);
3182         DispatchMessage(&msg);
3183     }
3184
3185     hres = IHTMLDocument2_get_body(doc, &body);
3186     ok(hres == S_OK, "get_body failed: %08x\n", hres);
3187
3188     if(body) {
3189         IHTMLElement_Release(body);
3190         test(doc);
3191     }else {
3192         skip("Could not get document body. Assuming no Gecko installed.\n");
3193     }
3194
3195     ref = IHTMLDocument2_Release(doc);
3196     ok(!ref ||
3197        ref == 1, /* Vista */
3198        "ref = %d\n", ref);
3199 }
3200
3201 static void gecko_installer_workaround(BOOL disable)
3202 {
3203     HKEY hkey;
3204     DWORD res;
3205
3206     static BOOL has_url = FALSE;
3207     static char url[2048];
3208
3209     if(!disable && !has_url)
3210         return;
3211
3212     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
3213     if(res != ERROR_SUCCESS)
3214         return;
3215
3216     if(disable) {
3217         DWORD type, size = sizeof(url);
3218
3219         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
3220         if(res == ERROR_SUCCESS && type == REG_SZ)
3221             has_url = TRUE;
3222
3223         RegDeleteValue(hkey, "GeckoUrl");
3224     }else {
3225         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
3226     }
3227
3228     RegCloseKey(hkey);
3229 }
3230
3231 START_TEST(dom)
3232 {
3233     gecko_installer_workaround(TRUE);
3234     CoInitialize(NULL);
3235
3236     run_domtest(doc_str1, test_doc_elem);
3237     run_domtest(range_test_str, test_txtrange);
3238     run_domtest(range_test2_str, test_txtrange2);
3239     run_domtest(elem_test_str, test_elems);
3240     run_domtest(doc_blank, test_create_elems);
3241     run_domtest(doc_blank, test_defaults);
3242     run_domtest(indent_test_str, test_indent);
3243
3244     CoUninitialize();
3245     gecko_installer_workaround(FALSE);
3246 }