msi: Set all folders' source paths to the root directory if the source type is compre...
[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><tbody></tbody></table>"
49     "<script id=\"sc\" type=\"text/javascript\"></script>"
50     "<test />"
51     "<img id=\"imgid\"/>"
52     "</body></html>";
53 static const char indent_test_str[] =
54     "<html><head><title>test</title></head><body>abc<br /><a href=\"about:blank\">123</a></body></html>";
55
56 static const WCHAR noneW[] = {'N','o','n','e',0};
57
58 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
59 static WCHAR texteditW[] = {'t','e','x','t','e','d','i','t',0};
60 static WCHAR wordW[] = {'w','o','r','d',0};
61
62 static const WCHAR text_javascriptW[] = {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
63
64 static const WCHAR idW[] = {'i','d',0};
65
66 typedef enum {
67     ET_NONE,
68     ET_HTML,
69     ET_HEAD,
70     ET_TITLE,
71     ET_BODY,
72     ET_A,
73     ET_INPUT,
74     ET_SELECT,
75     ET_TEXTAREA,
76     ET_OPTION,
77     ET_STYLE,
78     ET_BLOCKQUOTE,
79     ET_P,
80     ET_BR,
81     ET_TABLE,
82     ET_TBODY,
83     ET_SCRIPT,
84     ET_TEST,
85     ET_TESTG,
86     ET_COMMENT,
87     ET_IMG
88 } elem_type_t;
89
90 static REFIID const none_iids[] = {
91     &IID_IUnknown,
92     NULL
93 };
94
95 static REFIID const elem_iids[] = {
96     &IID_IHTMLDOMNode,
97     &IID_IHTMLDOMNode2,
98     &IID_IHTMLElement,
99     &IID_IHTMLElement2,
100     &IID_IDispatchEx,
101     &IID_IConnectionPointContainer,
102     NULL
103 };
104
105 static REFIID const body_iids[] = {
106     &IID_IHTMLDOMNode,
107     &IID_IHTMLDOMNode2,
108     &IID_IHTMLElement,
109     &IID_IHTMLElement2,
110     &IID_IHTMLTextContainer,
111     &IID_IHTMLBodyElement,
112     &IID_IDispatchEx,
113     &IID_IConnectionPointContainer,
114     NULL
115 };
116
117 static REFIID const anchor_iids[] = {
118     &IID_IHTMLDOMNode,
119     &IID_IHTMLDOMNode2,
120     &IID_IHTMLElement,
121     &IID_IHTMLElement2,
122     &IID_IHTMLAnchorElement,
123     &IID_IDispatchEx,
124     &IID_IConnectionPointContainer,
125     NULL
126 };
127
128 static REFIID const input_iids[] = {
129     &IID_IHTMLDOMNode,
130     &IID_IHTMLDOMNode2,
131     &IID_IHTMLElement,
132     &IID_IHTMLElement2,
133     &IID_IHTMLInputElement,
134     &IID_IHTMLInputTextElement,
135     &IID_IDispatchEx,
136     &IID_IConnectionPointContainer,
137     NULL
138 };
139
140 static REFIID const select_iids[] = {
141     &IID_IHTMLDOMNode,
142     &IID_IHTMLDOMNode2,
143     &IID_IHTMLElement,
144     &IID_IHTMLElement2,
145     &IID_IHTMLSelectElement,
146     &IID_IDispatchEx,
147     &IID_IConnectionPointContainer,
148     NULL
149 };
150
151 static REFIID const textarea_iids[] = {
152     &IID_IHTMLDOMNode,
153     &IID_IHTMLDOMNode2,
154     &IID_IHTMLElement,
155     &IID_IHTMLElement2,
156     &IID_IHTMLTextAreaElement,
157     &IID_IDispatchEx,
158     &IID_IConnectionPointContainer,
159     NULL
160 };
161
162 static REFIID const option_iids[] = {
163     &IID_IHTMLDOMNode,
164     &IID_IHTMLDOMNode2,
165     &IID_IHTMLElement,
166     &IID_IHTMLElement2,
167     &IID_IHTMLOptionElement,
168     &IID_IDispatchEx,
169     &IID_IConnectionPointContainer,
170     NULL
171 };
172
173 static REFIID const table_iids[] = {
174     &IID_IHTMLDOMNode,
175     &IID_IHTMLDOMNode2,
176     &IID_IHTMLElement,
177     &IID_IHTMLElement2,
178     &IID_IHTMLTable,
179     &IID_IDispatchEx,
180     &IID_IConnectionPointContainer,
181     NULL
182 };
183
184 static REFIID const script_iids[] = {
185     &IID_IHTMLDOMNode,
186     &IID_IHTMLDOMNode2,
187     &IID_IHTMLElement,
188     &IID_IHTMLElement2,
189     &IID_IHTMLScriptElement,
190     &IID_IDispatchEx,
191     &IID_IConnectionPointContainer,
192     NULL
193 };
194
195 static REFIID const text_iids[] = {
196     &IID_IHTMLDOMNode,
197     &IID_IHTMLDOMNode2,
198     &IID_IHTMLDOMTextNode,
199     NULL
200 };
201
202 static REFIID const location_iids[] = {
203     &IID_IDispatch,
204     &IID_IHTMLLocation,
205     NULL
206 };
207
208 static REFIID const window_iids[] = {
209     &IID_IDispatch,
210     &IID_IHTMLWindow2,
211     &IID_IHTMLWindow3,
212     &IID_IDispatchEx,
213     NULL
214 };
215
216 static REFIID const comment_iids[] = {
217     &IID_IHTMLDOMNode,
218     &IID_IHTMLDOMNode2,
219     &IID_IHTMLElement,
220     &IID_IHTMLElement2,
221     &IID_IHTMLCommentElement,
222     &IID_IDispatchEx,
223     &IID_IConnectionPointContainer,
224     NULL
225 };
226
227 static REFIID const img_iids[] = {
228     &IID_IHTMLDOMNode,
229     &IID_IHTMLDOMNode2,
230     &IID_IHTMLElement,
231     &IID_IHTMLElement2,
232     &IID_IDispatchEx,
233     &IID_IHTMLImgElement,
234     &IID_IConnectionPointContainer,
235     NULL
236 };
237
238 static REFIID const generic_iids[] = {
239     &IID_IHTMLDOMNode,
240     &IID_IHTMLDOMNode2,
241     &IID_IHTMLElement,
242     &IID_IHTMLElement2,
243     &IID_IHTMLGenericElement,
244     &IID_IDispatchEx,
245     &IID_IConnectionPointContainer,
246     NULL
247 };
248
249 typedef struct {
250     const char *tag;
251     REFIID *iids;
252     const IID *dispiid;
253 } elem_type_info_t;
254
255 static const elem_type_info_t elem_type_infos[] = {
256     {"",          none_iids,        NULL},
257     {"HTML",      elem_iids,        NULL},
258     {"HEAD",      elem_iids,        NULL},
259     {"TITLE",     elem_iids,        NULL},
260     {"BODY",      body_iids,        NULL},
261     {"A",         anchor_iids,      NULL},
262     {"INPUT",     input_iids,       &DIID_DispHTMLInputElement},
263     {"SELECT",    select_iids,      &DIID_DispHTMLSelectElement},
264     {"TEXTAREA",  textarea_iids,    NULL},
265     {"OPTION",    option_iids,      &DIID_DispHTMLOptionElement},
266     {"STYLE",     elem_iids,        NULL},
267     {"BLOCKQUOTE",elem_iids,        NULL},
268     {"P",         elem_iids,        NULL},
269     {"BR",        elem_iids,        NULL},
270     {"TABLE",     table_iids,       NULL},
271     {"TBODY",     elem_iids,        NULL},
272     {"SCRIPT",    script_iids,      NULL},
273     {"TEST",      elem_iids,        &DIID_DispHTMLUnknownElement},
274     {"TEST",      generic_iids,     &DIID_DispHTMLGenericElement},
275     {"!",         comment_iids,     &DIID_DispHTMLCommentElement},
276     {"IMG",       img_iids,         &DIID_DispHTMLImg}
277 };
278
279 static const char *dbgstr_w(LPCWSTR str)
280 {
281     static char buf[512];
282     if(!str)
283         return "(null)";
284     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
285     return buf;
286 }
287
288 static const char *dbgstr_guid(REFIID riid)
289 {
290     static char buf[50];
291
292     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
293             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
294             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
295             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
296
297     return buf;
298 }
299
300 static int strcmp_wa(LPCWSTR strw, const char *stra)
301 {
302     WCHAR buf[512];
303     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
304     return lstrcmpW(strw, buf);
305 }
306
307 static BSTR a2bstr(const char *str)
308 {
309     BSTR ret;
310     int len;
311
312     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
313     ret = SysAllocStringLen(NULL, len);
314     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
315
316     return ret;
317 }
318
319 static IHTMLDocument2 *create_document(void)
320 {
321     IHTMLDocument2 *doc;
322     HRESULT hres;
323
324     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
325             &IID_IHTMLDocument2, (void**)&doc);
326     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
327
328     return doc;
329 }
330
331 #define test_ifaces(i,ids) _test_ifaces(__LINE__,i,ids)
332 static void _test_ifaces(unsigned line, IUnknown *iface, REFIID *iids)
333 {
334     const IID * const *piid;
335     IUnknown *unk;
336     HRESULT hres;
337
338      for(piid = iids; *piid; piid++) {
339         hres = IDispatch_QueryInterface(iface, *piid, (void**)&unk);
340         ok_(__FILE__,line) (hres == S_OK, "Could not get %s interface: %08x\n", dbgstr_guid(*piid), hres);
341         if(SUCCEEDED(hres))
342             IUnknown_Release(unk);
343     }
344 }
345
346 #define test_disp(u,id) _test_disp(__LINE__,u,id)
347 static void _test_disp(unsigned line, IUnknown *unk, const IID *diid)
348 {
349     IDispatchEx *dispex;
350     ITypeInfo *typeinfo;
351     UINT ticnt;
352     HRESULT hres;
353
354     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&dispex);
355     ok_(__FILE__,line) (hres == S_OK, "Could not get IDispatch: %08x\n", hres);
356     if(FAILED(hres))
357         return;
358
359     ticnt = 0xdeadbeef;
360     hres = IDispatchEx_GetTypeInfoCount(dispex, &ticnt);
361     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfoCount failed: %08x\n", hres);
362     ok_(__FILE__,line) (ticnt == 1, "ticnt=%u\n", ticnt);
363
364     hres = IDispatchEx_GetTypeInfo(dispex, 0, 0, &typeinfo);
365     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfo failed: %08x\n", hres);
366
367     if(SUCCEEDED(hres)) {
368         TYPEATTR *type_attr;
369
370         hres = ITypeInfo_GetTypeAttr(typeinfo, &type_attr);
371         ok_(__FILE__,line) (hres == S_OK, "GetTypeAttr failed: %08x\n", hres);
372         ok_(__FILE__,line) (IsEqualGUID(&type_attr->guid, diid), "unexpected guid %s\n", dbgstr_guid(&type_attr->guid));
373
374         ITypeInfo_ReleaseTypeAttr(typeinfo, type_attr);
375         ITypeInfo_Release(typeinfo);
376     }
377
378     IDispatchEx_Release(dispex);
379 }
380
381 #define get_elem_iface(u) _get_elem_iface(__LINE__,u)
382 static IHTMLElement *_get_elem_iface(unsigned line, IUnknown *unk)
383 {
384     IHTMLElement *elem;
385     HRESULT hres;
386
387     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
388     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement: %08x\n", hres);
389     return elem;
390 }
391
392 #define get_elem2_iface(u) _get_elem2_iface(__LINE__,u)
393 static IHTMLElement2 *_get_elem2_iface(unsigned line, IUnknown *unk)
394 {
395     IHTMLElement2 *elem;
396     HRESULT hres;
397
398     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement2, (void**)&elem);
399     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement2: %08x\n", hres);
400     return elem;
401 }
402
403 #define get_node_iface(u) _get_node_iface(__LINE__,u)
404 static IHTMLDOMNode *_get_node_iface(unsigned line, IUnknown *unk)
405 {
406     IHTMLDOMNode *node;
407     HRESULT hres;
408
409     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
410     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLDOMNode: %08x\n", hres);
411     return node;
412 }
413
414 #define get_img_iface(u) _get_img_iface(__LINE__,u)
415 static IHTMLImgElement *_get_img_iface(unsigned line, IUnknown *unk)
416 {
417     IHTMLImgElement *img;
418     HRESULT hres;
419
420     hres = IUnknown_QueryInterface(unk, &IID_IHTMLImgElement, (void**)&img);
421     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLImgElement: %08x\n", hres);
422     return img;
423 }
424
425 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
426 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
427 {
428     IHTMLDOMNode *node = _get_node_iface(line, unk);
429     BSTR name;
430     HRESULT hres;
431
432     hres = IHTMLDOMNode_get_nodeName(node, &name);
433     IHTMLDOMNode_Release(node);
434     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
435     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
436
437     SysFreeString(name);
438 }
439
440 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
441 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
442 {
443     IHTMLElement *elem = _get_elem_iface(line, unk);
444     BSTR tag;
445     HRESULT hres;
446
447     hres = IHTMLElement_get_tagName(elem, &tag);
448     IHTMLElement_Release(elem);
449     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
450     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
451
452     SysFreeString(tag);
453 }
454
455 #define test_elem_type(ifc,t) _test_elem_type(__LINE__,ifc,t)
456 static void _test_elem_type(unsigned line, IUnknown *unk, elem_type_t type)
457 {
458     _test_elem_tag(line, unk, elem_type_infos[type].tag);
459     _test_ifaces(line, unk, elem_type_infos[type].iids);
460
461     if(elem_type_infos[type].dispiid)
462         _test_disp(line, unk, elem_type_infos[type].dispiid);
463 }
464
465 #define test_elem_attr(e,n,v) _test_elem_attr(__LINE__,e,n,v)
466 static void _test_elem_attr(unsigned line, IHTMLElement *elem, LPCWSTR name, LPCWSTR exval)
467 {
468     VARIANT value;
469     BSTR tmp;
470     HRESULT hres;
471
472     VariantInit(&value);
473
474     tmp = SysAllocString(name);
475     hres = IHTMLElement_getAttribute(elem, tmp, 0, &value);
476     SysFreeString(tmp);
477     ok_(__FILE__,line) (hres == S_OK, "getAttribute failed: %08x\n", hres);
478
479     if(exval) {
480         ok_(__FILE__,line) (V_VT(&value) == VT_BSTR, "vt=%d\n", V_VT(&value));
481         ok_(__FILE__,line) (!lstrcmpW(exval, V_BSTR(&value)), "unexpected value %s\n", dbgstr_w(V_BSTR(&value)));
482     }else {
483         ok_(__FILE__,line) (V_VT(&value) == VT_NULL, "vt=%d\n", V_VT(&value));
484     }
485
486     VariantClear(&value);
487 }
488
489 #define test_elem_offset(u) _test_elem_offset(__LINE__,u)
490 static void _test_elem_offset(unsigned line, IUnknown *unk)
491 {
492     IHTMLElement *elem = _get_elem_iface(line, unk);
493     long l;
494     HRESULT hres;
495
496     hres = IHTMLElement_get_offsetTop(elem, &l);
497     ok_(__FILE__,line) (hres == S_OK, "get_offsetTop failed: %08x\n", hres);
498
499     IHTMLElement_Release(elem);
500 }
501
502 static void test_doc_elem(IHTMLDocument2 *doc)
503 {
504     IHTMLElement *elem;
505     IHTMLDocument3 *doc3;
506     HRESULT hres;
507
508     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
509     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
510
511     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
512     IHTMLDocument3_Release(doc3);
513     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
514
515     test_node_name((IUnknown*)elem, "HTML");
516     test_elem_tag((IUnknown*)elem, "HTML");
517
518     IHTMLElement_Release(elem);
519 }
520
521 #define get_doc_elem(d) _get_doc_elem(__LINE__,d)
522 static IHTMLElement *_get_doc_elem(unsigned line, IHTMLDocument2 *doc)
523 {
524     IHTMLElement *elem;
525     IHTMLDocument3 *doc3;
526     HRESULT hres;
527
528     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
529     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 interface: %08x\n", hres);
530     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
531     ok_(__FILE__,line) (hres == S_OK, "get_documentElement failed: %08x\n", hres);
532     IHTMLDocument3_Release(doc3);
533
534     return elem;
535 }
536
537 #define test_option_text(o,t) _test_option_text(__LINE__,o,t)
538 static void _test_option_text(unsigned line, IHTMLOptionElement *option, const char *text)
539 {
540     BSTR bstr;
541     HRESULT hres;
542
543     hres = IHTMLOptionElement_get_text(option, &bstr);
544     ok_(__FILE__,line) (hres == S_OK, "get_text failed: %08x\n", hres);
545     ok_(__FILE__,line) (!strcmp_wa(bstr, text), "text=%s\n", dbgstr_w(bstr));
546     SysFreeString(bstr);
547 }
548
549 #define test_option_put_text(o,t) _test_option_put_text(__LINE__,o,t)
550 static void _test_option_put_text(unsigned line, IHTMLOptionElement *option, const char *text)
551 {
552     BSTR bstr;
553     HRESULT hres;
554
555     bstr = a2bstr(text);
556     hres = IHTMLOptionElement_put_text(option, bstr);
557     SysFreeString(bstr);
558     ok(hres == S_OK, "put_text failed: %08x\n", hres);
559
560     _test_option_text(line, option, text);
561 }
562
563 #define test_option_value(o,t) _test_option_value(__LINE__,o,t)
564 static void _test_option_value(unsigned line, IHTMLOptionElement *option, const char *value)
565 {
566     BSTR bstr;
567     HRESULT hres;
568
569     hres = IHTMLOptionElement_get_value(option, &bstr);
570     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
571     ok_(__FILE__,line) (!strcmp_wa(bstr, value), "value=%s\n", dbgstr_w(bstr));
572     SysFreeString(bstr);
573 }
574
575 #define test_option_put_value(o,t) _test_option_put_value(__LINE__,o,t)
576 static void _test_option_put_value(unsigned line, IHTMLOptionElement *option, const char *value)
577 {
578     BSTR bstr;
579     HRESULT hres;
580
581     bstr = a2bstr(value);
582     hres = IHTMLOptionElement_put_value(option, bstr);
583     SysFreeString(bstr);
584     ok(hres == S_OK, "put_value failed: %08x\n", hres);
585
586     _test_option_value(line, option, value);
587 }
588
589 #define create_option_elem(d,t,v) _create_option_elem(__LINE__,d,t,v)
590 static IHTMLOptionElement *_create_option_elem(unsigned line, IHTMLDocument2 *doc,
591         const char *txt, const char *val)
592 {
593     IHTMLOptionElementFactory *factory;
594     IHTMLOptionElement *option;
595     IHTMLWindow2 *window;
596     VARIANT text, value, empty;
597     HRESULT hres;
598
599     hres = IHTMLDocument2_get_parentWindow(doc, &window);
600     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
601
602     hres = IHTMLWindow2_get_Option(window, &factory);
603     IHTMLWindow2_Release(window);
604     ok_(__FILE__,line) (hres == S_OK, "get_Option failed: %08x\n", hres);
605
606     V_VT(&text) = VT_BSTR;
607     V_BSTR(&text) = a2bstr(txt);
608     V_VT(&value) = VT_BSTR;
609     V_BSTR(&value) = a2bstr(val);
610     V_VT(&empty) = VT_EMPTY;
611
612     hres = IHTMLOptionElementFactory_create(factory, text, value, empty, empty, &option);
613     ok_(__FILE__,line) (hres == S_OK, "create failed: %08x\n", hres);
614
615     IHTMLOptionElementFactory_Release(factory);
616     VariantClear(&text);
617     VariantClear(&value);
618
619     _test_option_text(line, option, txt);
620     _test_option_value(line, option, val);
621
622     return option;
623 }
624
625 #define test_select_length(s,l) _test_select_length(__LINE__,s,l)
626 static void _test_select_length(unsigned line, IHTMLSelectElement *select, long length)
627 {
628     long len = 0xdeadbeef;
629     HRESULT hres;
630
631     hres = IHTMLSelectElement_get_length(select, &len);
632     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
633     ok_(__FILE__,line) (len == length, "len=%ld, expected %ld\n", len, length);
634 }
635
636 #define test_select_selidx(s,i) _test_select_selidx(__LINE__,s,i)
637 static void _test_select_selidx(unsigned line, IHTMLSelectElement *select, long index)
638 {
639     long idx = 0xdeadbeef;
640     HRESULT hres;
641
642     hres = IHTMLSelectElement_get_selectedIndex(select, &idx);
643     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
644     ok_(__FILE__,line) (idx == index, "idx=%ld, expected %ld\n", idx, index);
645 }
646
647 #define test_select_put_selidx(s,i) _test_select_put_selidx(__LINE__,s,i)
648 static void _test_select_put_selidx(unsigned line, IHTMLSelectElement *select, long index)
649 {
650     HRESULT hres;
651
652     hres = IHTMLSelectElement_put_selectedIndex(select, index);
653     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
654     _test_select_selidx(line, select, index);
655 }
656
657 #define test_select_value(s,v) _test_select_value(__LINE__,s,v)
658 static void _test_select_value(unsigned line, IHTMLSelectElement *select, const char *exval)
659 {
660     BSTR val;
661     HRESULT hres;
662
663     hres = IHTMLSelectElement_get_value(select, &val);
664     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
665     if(exval)
666         ok_(__FILE__,line) (!strcmp_wa(val, exval), "unexpected value %s\n", dbgstr_w(val));
667     else
668         ok_(__FILE__,line) (val == NULL, "val=%s, expected NULL\n", dbgstr_w(val));
669 }
670
671 #define test_select_set_value(s,v) _test_select_set_value(__LINE__,s,v)
672 static void _test_select_set_value(unsigned line, IHTMLSelectElement *select, const char *val)
673 {
674     BSTR bstr;
675     HRESULT hres;
676
677     bstr = a2bstr(val);
678     hres = IHTMLSelectElement_put_value(select, bstr);
679     SysFreeString(bstr);
680     ok_(__FILE__,line) (hres == S_OK, "put_value failed: %08x\n", hres);
681 }
682
683 #define test_select_type(s,t) _test_select_type(__LINE__,s,t)
684 static void _test_select_type(unsigned line, IHTMLSelectElement *select, const char *extype)
685 {
686     BSTR type;
687     HRESULT hres;
688
689     hres = IHTMLSelectElement_get_type(select, &type);
690     ok_(__FILE__,line) (hres == S_OK, "get_type failed: %08x\n", hres);
691     ok_(__FILE__,line) (!strcmp_wa(type, extype), "type=%s, expected %s\n", dbgstr_w(type), extype);
692 }
693
694 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
695 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
696 {
697     BSTR text;
698     HRESULT hres;
699
700     hres = IHTMLTxtRange_get_text(range, &text);
701     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
702
703     if(extext) {
704         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
705         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
706     }else {
707         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
708     }
709
710     SysFreeString(text);
711
712 }
713
714 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
715 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
716 {
717     HRESULT hres;
718
719     hres = IHTMLTxtRange_collapse(range, b);
720     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
721     _test_range_text(line, range, NULL);
722 }
723
724 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
725 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
726         VARIANT_BOOL exb, const char *extext)
727 {
728     VARIANT_BOOL b = 0xe0e0;
729     HRESULT hres;
730
731     hres = IHTMLTxtRange_expand(range, unit, &b);
732     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
733     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
734     _test_range_text(line, range, extext);
735 }
736
737 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
738 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
739 {
740     long c = 0xdeadbeef;
741     HRESULT hres;
742
743     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
744     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
745     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
746     _test_range_text(line, range, NULL);
747 }
748
749 #define test_range_movestart(r,u,c,e) _test_range_movestart(__LINE__,r,u,c,e)
750 static void _test_range_movestart(unsigned line, IHTMLTxtRange *range,
751         LPWSTR unit, long cnt, long excnt)
752 {
753     long c = 0xdeadbeef;
754     HRESULT hres;
755
756     hres = IHTMLTxtRange_moveStart(range, unit, cnt, &c);
757     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
758     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
759 }
760
761 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
762 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
763 {
764     long c = 0xdeadbeef;
765     HRESULT hres;
766
767     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
768     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
769     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
770 }
771
772 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
773 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, const char *text)
774 {
775     HRESULT hres;
776     BSTR bstr = a2bstr(text);
777
778     hres = IHTMLTxtRange_put_text(range, bstr);
779     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
780     SysFreeString(bstr);
781     _test_range_text(line, range, NULL);
782 }
783
784 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
785 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
786 {
787     VARIANT_BOOL b;
788     HRESULT hres;
789
790     b = 0xe0e0;
791     hres = IHTMLTxtRange_inRange(range1, range2, &b);
792     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
793     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
794 }
795
796 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
797 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
798 {
799     VARIANT_BOOL b;
800     HRESULT hres;
801
802     b = 0xe0e0;
803     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
804     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
805     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
806
807     b = 0xe0e0;
808     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
809     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
810     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
811
812     if(exb) {
813         test_range_inrange(range1, range2, VARIANT_TRUE);
814         test_range_inrange(range2, range1, VARIANT_TRUE);
815     }
816 }
817
818 #define test_range_parent(r,t) _test_range_parent(__LINE__,r,t)
819 static void _test_range_parent(unsigned line, IHTMLTxtRange *range, elem_type_t type)
820 {
821     IHTMLElement *elem;
822     HRESULT hres;
823
824     hres = IHTMLTxtRange_parentElement(range, &elem);
825     ok_(__FILE__,line) (hres == S_OK, "parentElement failed: %08x\n", hres);
826
827     _test_elem_type(line, (IUnknown*)elem, type);
828
829     IHTMLElement_Release(elem);
830 }
831
832 #define test_elem_collection(c,t,l) _test_elem_collection(__LINE__,c,t,l)
833 static void _test_elem_collection(unsigned line, IUnknown *unk,
834         const elem_type_t *elem_types, long exlen)
835 {
836     IHTMLElementCollection *col;
837     long len;
838     DWORD i;
839     VARIANT name, index;
840     IDispatch *disp;
841     HRESULT hres;
842
843     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElementCollection, (void**)&col);
844     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
845
846     test_disp((IUnknown*)col, &DIID_DispHTMLElementCollection);
847
848     hres = IHTMLElementCollection_get_length(col, &len);
849     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
850     ok_(__FILE__,line) (len == exlen, "len=%ld, expected %ld\n", len, exlen);
851
852     if(len > exlen)
853         len = exlen;
854
855     V_VT(&index) = VT_EMPTY;
856     V_VT(&name) = VT_I4;
857
858     for(i=0; i<len; i++) {
859         V_I4(&name) = i;
860         disp = (void*)0xdeadbeef;
861         hres = IHTMLElementCollection_item(col, name, index, &disp);
862         ok_(__FILE__,line) (hres == S_OK, "item(%d) failed: %08x\n", i, hres);
863         ok_(__FILE__,line) (disp != NULL, "item returned NULL\n");
864         if(FAILED(hres) || !disp)
865             continue;
866
867         _test_elem_type(line, (IUnknown*)disp, elem_types[i]);
868         IDispatch_Release(disp);
869     }
870
871     V_I4(&name) = len;
872     disp = (void*)0xdeadbeef;
873     hres = IHTMLElementCollection_item(col, name, index, &disp);
874     ok_(__FILE__,line) (hres == S_OK, "item failed: %08x\n", hres);
875     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
876
877     V_I4(&name) = -1;
878     disp = (void*)0xdeadbeef;
879     hres = IHTMLElementCollection_item(col, name, index, &disp);
880     ok_(__FILE__,line) (hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
881     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
882
883     IHTMLElementCollection_Release(col);
884 }
885
886 #define test_elem_getelembytag(u,t,l) _test_elem_getelembytag(__LINE__,u,t,l)
887 static void _test_elem_getelembytag(unsigned line, IUnknown *unk, elem_type_t type, long exlen)
888 {
889     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
890     IHTMLElementCollection *col = NULL;
891     elem_type_t *types = NULL;
892     BSTR tmp;
893     int i;
894     HRESULT hres;
895
896     tmp = a2bstr(elem_type_infos[type].tag);
897     hres = IHTMLElement2_getElementsByTagName(elem, tmp, &col);
898     SysFreeString(tmp);
899     IHTMLElement2_Release(elem);
900     ok_(__FILE__,line) (hres == S_OK, "getElementByTagName failed: %08x\n", hres);
901     ok_(__FILE__,line) (col != NULL, "col == NULL\n");
902
903     if(exlen) {
904         types = HeapAlloc(GetProcessHeap(), 0, exlen*sizeof(elem_type_t));
905         for(i=0; i<exlen; i++)
906             types[i] = type;
907     }
908
909     _test_elem_collection(line, (IUnknown*)col, types, exlen);
910
911     HeapFree(GetProcessHeap(), 0, types);
912 }
913
914 #define get_first_child(n) _get_first_child(__LINE__,n)
915 static IHTMLDOMNode *_get_first_child(unsigned line, IUnknown *unk)
916 {
917     IHTMLDOMNode *node = _get_node_iface(line, unk);
918     IHTMLDOMNode *child = NULL;
919     HRESULT hres;
920
921     hres = IHTMLDOMNode_get_firstChild(node, &child);
922     IHTMLDOMNode_Release(node);
923     ok_(__FILE__,line) (hres == S_OK, "get_firstChild failed: %08x\n", hres);
924
925     return child;
926 }
927
928 #define test_node_has_child(u,b) _test_node_has_child(__LINE__,u,b)
929 static void _test_node_has_child(unsigned line, IUnknown *unk, VARIANT_BOOL exb)
930 {
931     IHTMLDOMNode *node = _get_node_iface(line, unk);
932     VARIANT_BOOL b = 0xdead;
933     HRESULT hres;
934
935     hres = IHTMLDOMNode_hasChildNodes(node, &b);
936     ok_(__FILE__,line) (hres == S_OK, "hasChildNodes failed: %08x\n", hres);
937     ok_(__FILE__,line) (b == exb, "hasChildNodes=%x, expected %x\n", b, exb);
938
939     IHTMLDOMNode_Release(node);
940 }
941
942 #define test_node_get_parent(u) _test_node_get_parent(__LINE__,u)
943 static IHTMLDOMNode *_test_node_get_parent(unsigned line, IUnknown *unk)
944 {
945     IHTMLDOMNode *node = _get_node_iface(line, unk);
946     IHTMLDOMNode *parent;
947     HRESULT hres;
948
949     hres = IHTMLDOMNode_get_parentNode(node, &parent);
950     IHTMLDOMNode_Release(node);
951     ok_(__FILE__,line) (hres == S_OK, "get_parentNode failed: %08x\n", hres);
952
953     return parent;
954 }
955
956 #define get_node_type(n) _get_node_type(__LINE__,n)
957 static long _get_node_type(unsigned line, IUnknown *unk)
958 {
959     IHTMLDOMNode *node = _get_node_iface(line, unk);
960     long type = -1;
961     HRESULT hres;
962
963     hres = IHTMLDOMNode_get_nodeType(node, &type);
964     ok(hres == S_OK, "get_nodeType failed: %08x\n", hres);
965
966     IHTMLDOMNode_Release(node);
967
968     return type;
969 }
970
971 #define test_img_set_src(u,s) _test_img_set_src(__LINE__,u,s)
972 static void _test_img_set_src(unsigned line, IUnknown *unk, const char *src)
973 {
974     IHTMLImgElement *img = _get_img_iface(line, unk);
975     BSTR tmp;
976     HRESULT hres;
977
978     tmp = a2bstr(src);
979     hres = IHTMLImgElement_put_src(img, tmp);
980     IHTMLImgElement_Release(img);
981     SysFreeString(tmp);
982     ok_(__FILE__,line) (hres == S_OK, "put_src failed: %08x\n", hres);
983 }
984
985 #define test_img_alt(u,a) _test_img_alt(__LINE__,u,a)
986 static void _test_img_alt(unsigned line, IUnknown *unk, const char *exalt)
987 {
988     IHTMLImgElement *img = _get_img_iface(line, unk);
989     BSTR alt;
990     HRESULT hres;
991
992     hres = IHTMLImgElement_get_alt(img, &alt);
993     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
994     if(exalt)
995         ok_(__FILE__,line) (!strcmp_wa(alt, exalt), "inexopected alt %s\n", dbgstr_w(alt));
996     else
997         ok_(__FILE__,line) (!alt, "alt != NULL\n");
998     SysFreeString(alt);
999 }
1000
1001 #define test_img_set_alt(u,a) _test_img_set_alt(__LINE__,u,a)
1002 static void _test_img_set_alt(unsigned line, IUnknown *unk, const char *alt)
1003 {
1004     IHTMLImgElement *img = _get_img_iface(line, unk);
1005     BSTR tmp;
1006     HRESULT hres;
1007
1008     tmp = a2bstr(alt);
1009     hres = IHTMLImgElement_put_alt(img, tmp);
1010     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1011     SysFreeString(tmp);
1012
1013     _test_img_alt(line, unk, alt);
1014 }
1015
1016 #define test_input_get_disabled(i,b) _test_input_get_disabled(__LINE__,i,b)
1017 static void _test_input_get_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL exb)
1018 {
1019     VARIANT_BOOL disabled = 100;
1020     HRESULT hres;
1021
1022     hres = IHTMLInputElement_get_disabled(input, &disabled);
1023     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1024     ok_(__FILE__,line) (disabled == exb, "disabled=%x, expected %x\n", disabled, exb);
1025 }
1026
1027 #define test_input_set_disabled(i,b) _test_input_set_disabled(__LINE__,i,b)
1028 static void _test_input_set_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL b)
1029 {
1030     HRESULT hres;
1031
1032     hres = IHTMLInputElement_put_disabled(input, b);
1033     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1034
1035     _test_input_get_disabled(line, input, b);
1036 }
1037
1038 #define test_input_value(o,t) _test_input_value(__LINE__,o,t)
1039 static void _test_input_value(unsigned line, IUnknown *unk, const char *exval)
1040 {
1041     IHTMLInputElement *input;
1042     BSTR bstr;
1043     HRESULT hres;
1044
1045     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1046     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1047     if(FAILED(hres))
1048         return;
1049
1050     hres = IHTMLInputElement_get_value(input, &bstr);
1051     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1052     if(exval)
1053         ok_(__FILE__,line) (!strcmp_wa(bstr, exval), "value=%s\n", dbgstr_w(bstr));
1054     else
1055         ok_(__FILE__,line) (!exval, "exval != NULL\n");
1056     SysFreeString(bstr);
1057     IHTMLInputElement_Release(input);
1058 }
1059
1060 #define test_input_put_value(o,v) _test_input_put_value(__LINE__,o,v)
1061 static void _test_input_put_value(unsigned line, IUnknown *unk, const char *val)
1062 {
1063     IHTMLInputElement *input;
1064     BSTR bstr;
1065     HRESULT hres;
1066
1067     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1068     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1069     if(FAILED(hres))
1070         return;
1071
1072     bstr = a2bstr(val);
1073     hres = IHTMLInputElement_get_value(input, &bstr);
1074     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1075     SysFreeString(bstr);
1076     IHTMLInputElement_Release(input);
1077 }
1078
1079 #define get_child_nodes(u) _get_child_nodes(__LINE__,u)
1080 static IHTMLDOMChildrenCollection *_get_child_nodes(unsigned line, IUnknown *unk)
1081 {
1082     IHTMLDOMNode *node = _get_node_iface(line, unk);
1083     IHTMLDOMChildrenCollection *col = NULL;
1084     IDispatch *disp;
1085     HRESULT hres;
1086
1087     hres = IHTMLDOMNode_get_childNodes(node, &disp);
1088     IHTMLDOMNode_Release(node);
1089     ok_(__FILE__,line) (hres == S_OK, "get_childNodes failed: %08x\n", hres);
1090     if(FAILED(hres))
1091         return NULL;
1092
1093     hres = IDispatch_QueryInterface(disp, &IID_IHTMLDOMChildrenCollection, (void**)&col);
1094     IDispatch_Release(disp);
1095     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDOMChildrenCollection: %08x\n", hres);
1096
1097     return col;
1098 }
1099
1100 #define test_elem_class(u,c) _test_elem_class(__LINE__,u,c)
1101 static void _test_elem_class(unsigned line, IUnknown *unk, const char *exclass)
1102 {
1103     IHTMLElement *elem = _get_elem_iface(line, unk);
1104     BSTR class = (void*)0xdeadbeef;
1105     HRESULT hres;
1106
1107     hres = IHTMLElement_get_className(elem, &class);
1108     IHTMLElement_Release(elem);
1109     ok_(__FILE__,line) (hres == S_OK, "get_className failed: %08x\n", hres);
1110     if(exclass)
1111         ok_(__FILE__,line) (!strcmp_wa(class, exclass), "unexpected className %s\n", dbgstr_w(class));
1112     else
1113         ok_(__FILE__,line) (!class, "class != NULL\n");
1114     SysFreeString(class);
1115 }
1116
1117 #define test_elem_tabindex(u,i) _test_elem_tabindex(__LINE__,u,i)
1118 static void _test_elem_tabindex(unsigned line, IUnknown *unk, short exindex)
1119 {
1120     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1121     short index = -3;
1122     HRESULT hres;
1123
1124     hres = IHTMLElement2_get_tabIndex(elem2, &index);
1125     IHTMLElement2_Release(elem2);
1126     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1127     ok_(__FILE__,line) (index == exindex, "unexpected index %d\n", index);
1128 }
1129
1130 #define test_elem_set_tabindex(u,i) _test_elem_set_tabindex(__LINE__,u,i)
1131 static void _test_elem_set_tabindex(unsigned line, IUnknown *unk, short index)
1132 {
1133     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1134     HRESULT hres;
1135
1136     hres = IHTMLElement2_put_tabIndex(elem2, index);
1137     IHTMLElement2_Release(elem2);
1138     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1139
1140     _test_elem_tabindex(line, unk, index);
1141 }
1142
1143 #define test_elem_set_class(u,c) _test_elem_set_class(__LINE__,u,c)
1144 static void _test_elem_set_class(unsigned line, IUnknown *unk, const char *class)
1145 {
1146     IHTMLElement *elem = _get_elem_iface(line, unk);
1147     BSTR tmp;
1148     HRESULT hres;
1149
1150     tmp = class ? a2bstr(class) : NULL;
1151     hres = IHTMLElement_put_className(elem, tmp);
1152     IHTMLElement_Release(elem);
1153     ok_(__FILE__,line) (hres == S_OK, "put_className failed: %08x\n", hres);
1154     SysFreeString(tmp);
1155
1156     _test_elem_class(line, unk, class);
1157 }
1158
1159 #define get_child_item(c,i) _get_child_item(__LINE__,c,i)
1160 static IHTMLDOMNode *_get_child_item(unsigned line, IHTMLDOMChildrenCollection *col, long idx)
1161 {
1162     IHTMLDOMNode *node = NULL;
1163     IDispatch *disp;
1164     HRESULT hres;
1165
1166     hres = IHTMLDOMChildrenCollection_item(col, idx, &disp);
1167     ok(hres == S_OK, "item failed: %08x\n", hres);
1168
1169     node = _get_node_iface(line, (IUnknown*)disp);
1170     IDispatch_Release(disp);
1171
1172     return node;
1173 }
1174
1175 #define test_elem_id(e,i) _test_elem_id(__LINE__,e,i)
1176 static void _test_elem_id(unsigned line, IUnknown *unk, const char *exid)
1177 {
1178     IHTMLElement *elem = _get_elem_iface(line, unk);
1179     BSTR id = (void*)0xdeadbeef;
1180     HRESULT hres;
1181
1182     hres = IHTMLElement_get_id(elem, &id);
1183     IHTMLElement_Release(elem);
1184     ok_(__FILE__,line) (hres == S_OK, "get_id failed: %08x\n", hres);
1185
1186     if(exid)
1187         ok_(__FILE__,line) (!strcmp_wa(id, exid), "unexpected id %s\n", dbgstr_w(id));
1188     else
1189         ok_(__FILE__,line) (!id, "id=%s\n", dbgstr_w(id));
1190
1191     SysFreeString(id);
1192 }
1193
1194 #define test_elem_put_id(u,i) _test_elem_put_id(__LINE__,u,i)
1195 static void _test_elem_put_id(unsigned line, IUnknown *unk, const char *new_id)
1196 {
1197     IHTMLElement *elem = _get_elem_iface(line, unk);
1198     BSTR tmp = a2bstr(new_id);
1199     HRESULT hres;
1200
1201     hres = IHTMLElement_put_id(elem, tmp);
1202     IHTMLElement_Release(elem);
1203     SysFreeString(tmp);
1204     ok_(__FILE__,line) (hres == S_OK, "put_id failed: %08x\n", hres);
1205
1206     _test_elem_id(line, unk, new_id);
1207 }
1208
1209 #define test_elem_title(u,t) _test_elem_title(__LINE__,u,t)
1210 static void _test_elem_title(unsigned line, IUnknown *unk, const char *extitle)
1211 {
1212     IHTMLElement *elem = _get_elem_iface(line, unk);
1213     BSTR title;
1214     HRESULT hres;
1215
1216     hres = IHTMLElement_get_title(elem, &title);
1217     IHTMLElement_Release(elem);
1218     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1219     if(extitle)
1220         ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1221     else
1222         ok_(__FILE__,line) (!title, "title=%s, expected NULL\n", dbgstr_w(title));
1223
1224     SysFreeString(title);
1225 }
1226
1227 #define test_elem_set_title(u,t) _test_elem_set_title(__LINE__,u,t)
1228 static void _test_elem_set_title(unsigned line, IUnknown *unk, const char *title)
1229 {
1230     IHTMLElement *elem = _get_elem_iface(line, unk);
1231     BSTR tmp;
1232     HRESULT hres;
1233
1234     tmp = a2bstr(title);
1235     hres = IHTMLElement_put_title(elem, tmp);
1236     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1237
1238     IHTMLElement_Release(elem);
1239     SysFreeString(tmp);
1240 }
1241
1242 #define test_node_get_value_str(u,e) _test_node_get_value_str(__LINE__,u,e)
1243 static void _test_node_get_value_str(unsigned line, IUnknown *unk, const char *exval)
1244 {
1245     IHTMLDOMNode *node = _get_node_iface(line, unk);
1246     VARIANT var;
1247     HRESULT hres;
1248
1249     hres = IHTMLDOMNode_get_nodeValue(node, &var);
1250     IHTMLDOMNode_Release(node);
1251     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1252
1253     if(exval) {
1254         ok_(__FILE__,line) (V_VT(&var) == VT_BSTR, "vt=%d\n", V_VT(&var));
1255         ok_(__FILE__,line) (!strcmp_wa(V_BSTR(&var), exval), "unexpected value %s\n", dbgstr_w(V_BSTR(&var)));
1256     }else {
1257         ok_(__FILE__,line) (V_VT(&var) == VT_NULL, "vt=%d, expected VT_NULL\n", V_VT(&var));
1258     }
1259
1260     VariantClear(&var);
1261 }
1262
1263 #define test_node_put_value_str(u,v) _test_node_put_value_str(__LINE__,u,v)
1264 static void _test_node_put_value_str(unsigned line, IUnknown *unk, const char *val)
1265 {
1266     IHTMLDOMNode *node = _get_node_iface(line, unk);
1267     VARIANT var;
1268     HRESULT hres;
1269
1270     V_VT(&var) = VT_BSTR;
1271     V_BSTR(&var) = a2bstr(val);
1272
1273     hres = IHTMLDOMNode_put_nodeValue(node, var);
1274     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1275     IHTMLDOMNode_Release(node);
1276     VariantClear(&var);
1277 }
1278
1279 #define test_elem_client_size(u) _test_elem_client_size(__LINE__,u)
1280 static void _test_elem_client_size(unsigned line, IUnknown *unk)
1281 {
1282     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1283     long l;
1284     HRESULT hres;
1285
1286     hres = IHTMLElement2_get_clientWidth(elem, &l);
1287     ok_(__FILE__,line) (hres == S_OK, "get_clientWidth failed: %08x\n", hres);
1288     hres = IHTMLElement2_get_clientHeight(elem, &l);
1289     ok_(__FILE__,line) (hres == S_OK, "get_clientHeight failed: %08x\n", hres);
1290
1291     IHTMLElement2_Release(elem);
1292 }
1293
1294 #define test_create_elem(d,t) _test_create_elem(__LINE__,d,t)
1295 static IHTMLElement *_test_create_elem(unsigned line, IHTMLDocument2 *doc, const char *tag)
1296 {
1297     IHTMLElement *elem = NULL;
1298     BSTR tmp;
1299     HRESULT hres;
1300
1301     tmp = a2bstr(tag);
1302     hres = IHTMLDocument2_createElement(doc, tmp, &elem);
1303     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1304     ok_(__FILE__,line) (elem != NULL, "elem == NULL\n");
1305
1306     return elem;
1307 }
1308
1309 #define test_create_text(d,t) _test_create_text(__LINE__,d,t)
1310 static IHTMLDOMNode *_test_create_text(unsigned line, IHTMLDocument2 *doc, const char *text)
1311 {
1312     IHTMLDocument3 *doc3;
1313     IHTMLDOMNode *node = NULL;
1314     BSTR tmp;
1315     HRESULT hres;
1316
1317     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1318     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3: %08x\n", hres);
1319
1320     tmp = a2bstr(text);
1321     hres = IHTMLDocument3_createTextNode(doc3, tmp, &node);
1322     IHTMLDocument3_Release(doc3);
1323     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1324     ok_(__FILE__,line) (node != NULL, "node == NULL\n");
1325
1326     return node;
1327 }
1328
1329 #define test_node_append_child(n,c) _test_node_append_child(__LINE__,n,c)
1330 static IHTMLDOMNode *_test_node_append_child(unsigned line, IUnknown *node_unk, IUnknown *child_unk)
1331 {
1332     IHTMLDOMNode *node = _get_node_iface(line, node_unk);
1333     IHTMLDOMNode *child = _get_node_iface(line, child_unk);
1334     IHTMLDOMNode *new_child = NULL;
1335     HRESULT hres;
1336
1337     hres = IHTMLDOMNode_appendChild(node, child, &new_child);
1338     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1339     ok_(__FILE__,line) (new_child != NULL, "new_child == NULL\n");
1340     /* TODO  ok_(__FILE__,line) (new_child != child, "new_child == child\n"); */
1341
1342     IHTMLDOMNode_Release(node);
1343     IHTMLDOMNode_Release(child);
1344
1345     return new_child;
1346 }
1347
1348 #define test_node_remove_child(n,c) _test_node_remove_child(__LINE__,n,c)
1349 static void _test_node_remove_child(unsigned line, IUnknown *unk, IHTMLDOMNode *child)
1350 {
1351     IHTMLDOMNode *node = _get_node_iface(line, unk);
1352     IHTMLDOMNode *new_node = NULL;
1353     HRESULT hres;
1354
1355     hres = IHTMLDOMNode_removeChild(node, child, &new_node);
1356     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1357     ok_(__FILE__,line) (new_node != NULL, "new_node == NULL\n");
1358     /* TODO ok_(__FILE__,line) (new_node != child, "new_node == child\n"); */
1359
1360     IHTMLDOMNode_Release(node);
1361     IHTMLDOMNode_Release(new_node);
1362 }
1363
1364 static void test_elem_col_item(IHTMLElementCollection *col, LPCWSTR n,
1365         const elem_type_t *elem_types, long len)
1366 {
1367     IDispatch *disp;
1368     VARIANT name, index;
1369     DWORD i;
1370     HRESULT hres;
1371
1372     V_VT(&index) = VT_EMPTY;
1373     V_VT(&name) = VT_BSTR;
1374     V_BSTR(&name) = SysAllocString(n);
1375
1376     hres = IHTMLElementCollection_item(col, name, index, &disp);
1377     ok(hres == S_OK, "item failed: %08x\n", hres);
1378
1379     test_elem_collection((IUnknown*)disp, elem_types, len);
1380     IDispatch_Release(disp);
1381     ok(hres == S_OK, "Could not get IHTMLElementCollection interface: %08x\n", hres);
1382     if(hres != S_OK)
1383         goto cleanup;
1384
1385     V_VT(&index) = VT_I4;
1386
1387     for(i=0; i<len; i++) {
1388         V_I4(&index) = i;
1389         disp = (void*)0xdeadbeef;
1390         hres = IHTMLElementCollection_item(col, name, index, &disp);
1391         ok(hres == S_OK, "item failed: %08x\n", hres);
1392         ok(disp != NULL, "disp == NULL\n");
1393         if(FAILED(hres) || !disp)
1394             continue;
1395
1396         test_elem_type((IUnknown*)disp, elem_types[i]);
1397
1398         IDispatch_Release(disp);
1399     }
1400
1401     V_I4(&index) = len;
1402     disp = (void*)0xdeadbeef;
1403     hres = IHTMLElementCollection_item(col, name, index, &disp);
1404     ok(hres == S_OK, "item failed: %08x\n", hres);
1405     ok(disp == NULL, "disp != NULL\n");
1406
1407     V_I4(&index) = -1;
1408     disp = (void*)0xdeadbeef;
1409     hres = IHTMLElementCollection_item(col, name, index, &disp);
1410     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1411     ok(disp == NULL, "disp != NULL\n");
1412
1413 cleanup:
1414     SysFreeString(V_BSTR(&name));
1415 }
1416
1417 static IHTMLElement *get_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id, BOOL expect_success)
1418 {
1419     IHTMLElementCollection *col;
1420     IHTMLElement *elem;
1421     IDispatch *disp = (void*)0xdeadbeef;
1422     VARIANT name, index;
1423     HRESULT hres;
1424
1425     hres = IHTMLDocument2_get_all(doc, &col);
1426     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1427     ok(col != NULL, "col == NULL\n");
1428     if(FAILED(hres) || !col)
1429         return NULL;
1430
1431     V_VT(&index) = VT_EMPTY;
1432     V_VT(&name) = VT_BSTR;
1433     V_BSTR(&name) = SysAllocString(id);
1434
1435     hres = IHTMLElementCollection_item(col, name, index, &disp);
1436     IHTMLElementCollection_Release(col);
1437     SysFreeString(V_BSTR(&name));
1438     ok(hres == S_OK, "item failed: %08x\n", hres);
1439     if(!expect_success) {
1440         ok(disp == NULL, "disp != NULL\n");
1441         return NULL;
1442     }
1443
1444     ok(disp != NULL, "disp == NULL\n");
1445     if(!disp)
1446         return NULL;
1447
1448     elem = get_elem_iface((IUnknown*)disp);
1449     IDispatch_Release(disp);
1450
1451     return elem;
1452 }
1453
1454 static IHTMLElement *get_doc_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id)
1455 {
1456     IHTMLDocument3 *doc3;
1457     IHTMLElement *elem;
1458     BSTR tmp;
1459     HRESULT hres;
1460
1461     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1462     ok(hres == S_OK, "Could not get IHTMLDocument3 iface: %08x\n", hres);
1463
1464     tmp = SysAllocString(id);
1465     hres = IHTMLDocument3_getElementById(doc3, tmp, &elem);
1466     SysFreeString(tmp);
1467     ok(hres == S_OK, "getElementById(%s) failed: %08x\n", dbgstr_w(id), hres);
1468
1469     IHTMLDocument3_Release(doc3);
1470
1471     return elem;
1472 }
1473
1474 static void test_select_elem(IHTMLSelectElement *select)
1475 {
1476     test_select_type(select, "select-one");
1477     test_select_length(select, 2);
1478     test_select_selidx(select, 0);
1479     test_select_put_selidx(select, 1);
1480
1481     test_select_set_value(select, "val1");
1482     test_select_value(select, "val1");
1483 }
1484
1485 static void test_create_option_elem(IHTMLDocument2 *doc)
1486 {
1487     IHTMLOptionElement *option;
1488
1489     option = create_option_elem(doc, "test text", "test value");
1490
1491     test_option_put_text(option, "new text");
1492     test_option_put_value(option, "new value");
1493
1494     IHTMLOptionElement_Release(option);
1495 }
1496
1497 static IHTMLTxtRange *test_create_body_range(IHTMLDocument2 *doc)
1498 {
1499     IHTMLBodyElement *body;
1500     IHTMLTxtRange *range;
1501     IHTMLElement *elem;
1502     HRESULT hres;
1503
1504     hres = IHTMLDocument2_get_body(doc, &elem);
1505     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1506
1507     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
1508     IHTMLElement_Release(elem);
1509
1510     hres = IHTMLBodyElement_createTextRange(body, &range);
1511     IHTMLBodyElement_Release(body);
1512     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
1513
1514     return range;
1515 }
1516
1517 static void test_txtrange(IHTMLDocument2 *doc)
1518 {
1519     IHTMLTxtRange *body_range, *range, *range2;
1520     IHTMLSelectionObject *selection;
1521     IDispatch *disp_range;
1522     HRESULT hres;
1523
1524     body_range = test_create_body_range(doc);
1525
1526     test_range_text(body_range, "test abc 123\r\nit's text");
1527
1528     hres = IHTMLTxtRange_duplicate(body_range, &range);
1529     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1530
1531     hres = IHTMLTxtRange_duplicate(body_range, &range2);
1532     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1533     test_range_isequal(range, range2, VARIANT_TRUE);
1534
1535     test_range_text(range, "test abc 123\r\nit's text");
1536     test_range_text(body_range, "test abc 123\r\nit's text");
1537
1538     test_range_collapse(range, TRUE);
1539     test_range_isequal(range, range2, VARIANT_FALSE);
1540     test_range_inrange(range, range2, VARIANT_FALSE);
1541     test_range_inrange(range2, range, VARIANT_TRUE);
1542     IHTMLTxtRange_Release(range2);
1543
1544     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1545     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
1546     test_range_move(range, characterW, 2, 2);
1547     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1548
1549     test_range_collapse(range, FALSE);
1550     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
1551
1552     test_range_collapse(range, FALSE);
1553     test_range_expand(range, wordW, VARIANT_TRUE, "123");
1554     test_range_expand(range, wordW, VARIANT_FALSE, "123");
1555     test_range_move(range, characterW, 2, 2);
1556     test_range_expand(range, wordW, VARIANT_TRUE, "123");
1557     test_range_moveend(range, characterW, -5, -5);
1558     test_range_text(range, NULL);
1559     test_range_moveend(range, characterW, 3, 3);
1560     test_range_text(range, "c 1");
1561     test_range_expand(range, texteditW, VARIANT_TRUE, "test abc 123\r\nit's text");
1562     test_range_collapse(range, TRUE);
1563     test_range_move(range, characterW, 4, 4);
1564     test_range_moveend(range, characterW, 1, 1);
1565     test_range_text(range, " ");
1566     test_range_move(range, wordW, 1, 1);
1567     test_range_moveend(range, characterW, 2, 2);
1568     test_range_text(range, "ab");
1569
1570     IHTMLTxtRange_Release(range);
1571
1572     hres = IHTMLTxtRange_duplicate(body_range, &range);
1573     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1574
1575     test_range_text(range, "test abc 123\r\nit's text");
1576     test_range_move(range, characterW, 3, 3);
1577     test_range_moveend(range, characterW, 1, 1);
1578     test_range_text(range, "t");
1579     test_range_moveend(range, characterW, 3, 3);
1580     test_range_text(range, "t ab");
1581     test_range_moveend(range, characterW, -2, -2);
1582     test_range_text(range, "t ");
1583     test_range_move(range, characterW, 6, 6);
1584     test_range_moveend(range, characterW, 3, 3);
1585     test_range_text(range, "123");
1586     test_range_moveend(range, characterW, 2, 2);
1587     test_range_text(range, "123\r\ni");
1588
1589     IHTMLTxtRange_Release(range);
1590
1591     hres = IHTMLTxtRange_duplicate(body_range, &range);
1592     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1593
1594     test_range_move(range, wordW, 1, 1);
1595     test_range_moveend(range, characterW, 2, 2);
1596     test_range_text(range, "ab");
1597
1598     test_range_move(range, characterW, -2, -2);
1599     test_range_moveend(range, characterW, 2, 2);
1600     test_range_text(range, "t ");
1601
1602     test_range_move(range, wordW, 3, 3);
1603     test_range_move(range, wordW, -2, -2);
1604     test_range_moveend(range, characterW, 2, 2);
1605     test_range_text(range, "ab");
1606
1607     test_range_move(range, characterW, -6, -5);
1608     test_range_moveend(range, characterW, -1, 0);
1609     test_range_moveend(range, characterW, -6, 0);
1610     test_range_move(range, characterW, 2, 2);
1611     test_range_moveend(range, characterW, 2, 2);
1612     test_range_text(range, "st");
1613     test_range_moveend(range, characterW, -6, -4);
1614     test_range_moveend(range, characterW, 2, 2);
1615
1616     IHTMLTxtRange_Release(range);
1617
1618     hres = IHTMLTxtRange_duplicate(body_range, &range);
1619     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1620
1621     test_range_move(range, wordW, 2, 2);
1622     test_range_moveend(range, characterW, 2, 2);
1623     test_range_text(range, "12");
1624
1625     test_range_move(range, characterW, 15, 14);
1626     test_range_move(range, characterW, -2, -2);
1627     test_range_moveend(range, characterW, 3, 2);
1628     test_range_text(range, "t");
1629     test_range_moveend(range, characterW, -1, -1);
1630     test_range_text(range, "t");
1631     test_range_expand(range, wordW, VARIANT_TRUE, "text");
1632     test_range_move(range, characterW, -2, -2);
1633     test_range_moveend(range, characterW, 2, 2);
1634     test_range_text(range, "s ");
1635     test_range_move(range, characterW, 100, 7);
1636     test_range_move(range, wordW, 1, 0);
1637     test_range_move(range, characterW, -2, -2);
1638     test_range_moveend(range, characterW, 3, 2);
1639     test_range_text(range, "t");
1640
1641     IHTMLTxtRange_Release(range);
1642
1643     hres = IHTMLTxtRange_duplicate(body_range, &range);
1644     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1645
1646     test_range_collapse(range, TRUE);
1647     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1648     test_range_put_text(range, "word");
1649     test_range_text(body_range, "wordabc 123\r\nit's text");
1650     test_range_text(range, NULL);
1651     test_range_moveend(range, characterW, 3, 3);
1652     test_range_text(range, "abc");
1653     test_range_movestart(range, characterW, -2, -2);
1654     test_range_text(range, "rdabc");
1655     test_range_movestart(range, characterW, 3, 3);
1656     test_range_text(range, "bc");
1657     test_range_movestart(range, characterW, 4, 4);
1658     test_range_text(range, NULL);
1659     test_range_movestart(range, characterW, -3, -3);
1660     test_range_text(range, "c 1");
1661     test_range_movestart(range, characterW, -7, -6);
1662     test_range_text(range, "wordabc 1");
1663     test_range_movestart(range, characterW, 100, 22);
1664     test_range_text(range, NULL);
1665
1666     IHTMLTxtRange_Release(range);
1667     IHTMLTxtRange_Release(body_range);
1668
1669     hres = IHTMLDocument2_get_selection(doc, &selection);
1670     ok(hres == S_OK, "IHTMLDocument2_get_selection failed: %08x\n", hres);
1671
1672     hres = IHTMLSelectionObject_createRange(selection, &disp_range);
1673     ok(hres == S_OK, "IHTMLSelectionObject_createRange failed: %08x\n", hres);
1674     IHTMLSelectionObject_Release(selection);
1675
1676     hres = IDispatch_QueryInterface(disp_range, &IID_IHTMLTxtRange, (void **)&range);
1677     ok(hres == S_OK, "Could not get IID_IHTMLTxtRange interface: 0x%08x\n", hres);
1678     IDispatch_Release(disp_range);
1679
1680     test_range_text(range, NULL);
1681     test_range_moveend(range, characterW, 3, 3);
1682     test_range_text(range, "wor");
1683     test_range_parent(range, ET_BODY);
1684     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
1685     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
1686     test_range_move(range, characterW, 3, 3);
1687     test_range_expand(range, wordW, VARIANT_TRUE, "wordabc ");
1688     test_range_moveend(range, characterW, -4, -4);
1689     test_range_put_text(range, "abc def ");
1690     test_range_expand(range, texteditW, VARIANT_TRUE, "abc def abc 123\r\nit's text");
1691     test_range_move(range, wordW, 1, 1);
1692     test_range_movestart(range, characterW, -1, -1);
1693     test_range_text(range, " ");
1694     test_range_move(range, wordW, 1, 1);
1695     test_range_moveend(range, characterW, 3, 3);
1696     test_range_text(range, "def");
1697     test_range_put_text(range, "xyz");
1698     test_range_moveend(range, characterW, 1, 1);
1699     test_range_move(range, wordW, 1, 1);
1700     test_range_moveend(range, characterW, 2, 2);
1701     test_range_text(range, "ab");
1702
1703     IHTMLTxtRange_Release(range);
1704 }
1705
1706 static void test_txtrange2(IHTMLDocument2 *doc)
1707 {
1708     IHTMLTxtRange *range;
1709
1710     range = test_create_body_range(doc);
1711
1712     test_range_text(range, "abc\r\n\r\n123\r\n\r\n\r\ndef");
1713     test_range_move(range, characterW, 5, 5);
1714     test_range_moveend(range, characterW, 1, 1);
1715     test_range_text(range, "2");
1716     test_range_move(range, characterW, -3, -3);
1717     test_range_moveend(range, characterW, 3, 3);
1718     test_range_text(range, "c\r\n\r\n1");
1719     test_range_collapse(range, VARIANT_FALSE);
1720     test_range_moveend(range, characterW, 4, 4);
1721     test_range_text(range, "23");
1722     test_range_moveend(range, characterW, 1, 1);
1723     test_range_text(range, "23\r\n\r\n\r\nd");
1724     test_range_moveend(range, characterW, -1, -1);
1725     test_range_text(range, "23");
1726     test_range_moveend(range, characterW, -1, -1);
1727     test_range_text(range, "23");
1728     test_range_moveend(range, characterW, -2, -2);
1729     test_range_text(range, "2");
1730
1731     IHTMLTxtRange_Release(range);
1732 }
1733
1734 static void test_compatmode(IHTMLDocument2 *doc)
1735 {
1736     IHTMLDocument5 *doc5;
1737     BSTR mode;
1738     HRESULT hres;
1739
1740     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
1741     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
1742     if(FAILED(hres))
1743         return;
1744
1745     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
1746     IHTMLDocument5_Release(doc5);
1747     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
1748     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
1749     SysFreeString(mode);
1750 }
1751
1752 static void test_location(IHTMLDocument2 *doc)
1753 {
1754     IHTMLLocation *location, *location2;
1755     ULONG ref;
1756     HRESULT hres;
1757
1758     hres = IHTMLDocument2_get_location(doc, &location);
1759     ok(hres == S_OK, "get_location failed: %08x\n", hres);
1760
1761     hres = IHTMLDocument2_get_location(doc, &location2);
1762     ok(hres == S_OK, "get_location failed: %08x\n", hres);
1763
1764     ok(location == location2, "location != location2\n");
1765
1766     test_ifaces((IUnknown*)location, location_iids);
1767
1768     IHTMLLocation_Release(location2);
1769     ref = IHTMLLocation_Release(location);
1770     ok(!ref, "location chould be destroyed here\n");
1771 }
1772
1773 static void test_navigator(IHTMLDocument2 *doc)
1774 {
1775     IHTMLWindow2 *window;
1776     IOmNavigator *navigator, *navigator2;
1777     ULONG ref;
1778     HRESULT hres;
1779
1780     hres = IHTMLDocument2_get_parentWindow(doc, &window);
1781     ok(hres == S_OK, "parentWidnow failed: %08x\n", hres);
1782
1783     hres = IHTMLWindow2_get_navigator(window, &navigator);
1784     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1785     ok(navigator != NULL, "navigator == NULL\n");
1786     test_disp((IUnknown*)navigator, &IID_IOmNavigator);
1787
1788     hres = IHTMLWindow2_get_navigator(window, &navigator2);
1789     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1790     ok(navigator != navigator2, "navigator2 != navihgator\n");
1791
1792     IHTMLWindow2_Release(window);
1793     IOmNavigator_Release(navigator2);
1794     ref = IOmNavigator_Release(navigator);
1795     ok(!ref, "navigator should be destroyed here\n");
1796 }
1797
1798 static void test_default_style(IHTMLStyle *style)
1799 {
1800     VARIANT_BOOL b;
1801     VARIANT v;
1802     BSTR str;
1803     HRESULT hres;
1804
1805     test_disp((IUnknown*)style, &DIID_DispHTMLStyle);
1806
1807     str = (void*)0xdeadbeef;
1808     hres = IHTMLStyle_get_fontFamily(style, &str);
1809     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
1810     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
1811
1812     str = (void*)0xdeadbeef;
1813     hres = IHTMLStyle_get_fontWeight(style, &str);
1814     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
1815     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
1816
1817     str = (void*)0xdeadbeef;
1818     hres = IHTMLStyle_get_display(style, &str);
1819     ok(hres == S_OK, "get_display failed: %08x\n", hres);
1820     ok(!str, "display = %s\n", dbgstr_w(str));
1821
1822     str = (void*)0xdeadbeef;
1823     hres = IHTMLStyle_get_visibility(style, &str);
1824     ok(hres == S_OK, "get_visibility failed: %08x\n", hres);
1825     ok(!str, "visibility = %s\n", dbgstr_w(str));
1826
1827     V_VT(&v) = VT_NULL;
1828     hres = IHTMLStyle_get_fontSize(style, &v);
1829     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
1830     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
1831     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
1832
1833     V_VT(&v) = VT_NULL;
1834     hres = IHTMLStyle_get_color(style, &v);
1835     ok(hres == S_OK, "get_color failed: %08x\n", hres);
1836     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
1837     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
1838
1839     b = 0xfefe;
1840     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
1841     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
1842     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
1843
1844     b = 0xfefe;
1845     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
1846     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
1847     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
1848
1849     V_VT(&v) = VT_EMPTY;
1850     hres = IHTMLStyle_get_width(style, &v);
1851     ok(hres == S_OK, "get_width failed: %08x\n", hres);
1852     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
1853     ok(!V_BSTR(&v), "V_BSTR(v)=%p\n", V_BSTR(&v));
1854
1855     V_VT(&v) = VT_BSTR;
1856     V_BSTR(&v) = a2bstr("auto");
1857     hres = IHTMLStyle_put_width(style, v);
1858     ok(hres == S_OK, "put_width failed: %08x\n", hres);
1859     VariantClear(&v);
1860
1861     V_VT(&v) = VT_EMPTY;
1862     hres = IHTMLStyle_get_width(style, &v);
1863     ok(hres == S_OK, "get_width failed: %08x\n", hres);
1864     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
1865     ok(!strcmp_wa(V_BSTR(&v), "auto"), "V_BSTR(v)=%s\n", dbgstr_w(V_BSTR(&v)));
1866     VariantClear(&v);
1867 }
1868
1869 static void test_default_selection(IHTMLDocument2 *doc)
1870 {
1871     IHTMLSelectionObject *selection;
1872     IHTMLTxtRange *range;
1873     IDispatch *disp;
1874     BSTR str;
1875     HRESULT hres;
1876
1877     hres = IHTMLDocument2_get_selection(doc, &selection);
1878     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
1879
1880     hres = IHTMLSelectionObject_get_type(selection, &str);
1881     ok(hres == S_OK, "get_type failed: %08x\n", hres);
1882     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
1883     SysFreeString(str);
1884
1885     hres = IHTMLSelectionObject_createRange(selection, &disp);
1886     IHTMLSelectionObject_Release(selection);
1887     ok(hres == S_OK, "createRange failed: %08x\n", hres);
1888
1889     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
1890     IDispatch_Release(disp);
1891     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
1892
1893     test_range_text(range, NULL);
1894     IHTMLTxtRange_Release(range);
1895 }
1896
1897 static void test_default_body(IHTMLBodyElement *body)
1898 {
1899     BSTR bstr;
1900     HRESULT hres;
1901
1902     bstr = (void*)0xdeadbeef;
1903     hres = IHTMLBodyElement_get_background(body, &bstr);
1904     ok(hres == S_OK, "get_background failed: %08x\n", hres);
1905     ok(bstr == NULL, "bstr != NULL\n");
1906 }
1907
1908 static void test_window(IHTMLDocument2 *doc)
1909 {
1910     IHTMLWindow2 *window, *window2, *self;
1911     IHTMLDocument2 *doc2 = NULL;
1912     HRESULT hres;
1913
1914     hres = IHTMLDocument2_get_parentWindow(doc, &window);
1915     ok(hres == S_OK, "get_parentElement failed: %08x\n", hres);
1916     test_ifaces((IUnknown*)window, window_iids);
1917     test_disp((IUnknown*)window, &DIID_DispHTMLWindow2);
1918
1919     hres = IHTMLWindow2_get_document(window, &doc2);
1920     ok(hres == S_OK, "get_document failed: %08x\n", hres);
1921     ok(doc2 != NULL, "doc2 == NULL\n");
1922
1923     IHTMLDocument_Release(doc2);
1924
1925     hres = IHTMLWindow2_get_window(window, &window2);
1926     ok(hres == S_OK, "get_window failed: %08x\n", hres);
1927     ok(window2 != NULL, "window2 == NULL\n");
1928
1929     hres = IHTMLWindow2_get_self(window, &self);
1930     ok(hres == S_OK, "get_window failed: %08x\n", hres);
1931     ok(window2 != NULL, "self == NULL\n");
1932
1933     ok(self == window2, "self != window2\n");
1934
1935     IHTMLWindow2_Release(window2);
1936     IHTMLWindow2_Release(self);
1937
1938     IHTMLWindow2_Release(window);
1939 }
1940
1941 static void test_defaults(IHTMLDocument2 *doc)
1942 {
1943     IHTMLStyleSheetsCollection *stylesheetcol;
1944     IHTMLBodyElement *body;
1945     IHTMLElement *elem;
1946     IHTMLStyle *style;
1947     long l;
1948     HRESULT hres;
1949
1950     hres = IHTMLDocument2_get_body(doc, &elem);
1951     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1952
1953     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
1954     ok(hres == S_OK, "Could not get IHTMBodyElement: %08x\n", hres);
1955     test_default_body(body);
1956     IHTMLBodyElement_Release(body);
1957
1958     hres = IHTMLElement_get_style(elem, &style);
1959     IHTMLElement_Release(elem);
1960     ok(hres == S_OK, "get_style failed: %08x\n", hres);
1961
1962     test_default_style(style);
1963     test_window(doc);
1964     test_compatmode(doc);
1965     test_location(doc);
1966     test_navigator(doc);
1967
1968     IHTMLStyle_Release(style);
1969
1970     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
1971     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
1972
1973     l = 0xdeadbeef;
1974     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
1975     ok(hres == S_OK, "get_length failed: %08x\n", hres);
1976     ok(l == 0, "length = %ld\n", l);
1977
1978     IHTMLStyleSheetsCollection_Release(stylesheetcol);
1979
1980     test_default_selection(doc);
1981 }
1982
1983 static void test_stylesheet(IDispatch *disp)
1984 {
1985     IHTMLStyleSheetRulesCollection *col = NULL;
1986     IHTMLStyleSheet *stylesheet;
1987     HRESULT hres;
1988
1989     hres = IDispatch_QueryInterface(disp, &IID_IHTMLStyleSheet, (void**)&stylesheet);
1990     ok(hres == S_OK, "Could not get IHTMLStyleSheet: %08x\n", hres);
1991
1992     hres = IHTMLStyleSheet_get_rules(stylesheet, &col);
1993     ok(hres == S_OK, "get_rules failed: %08x\n", hres);
1994     ok(col != NULL, "col == NULL\n");
1995
1996     IHTMLStyleSheetRulesCollection_Release(col);
1997     IHTMLStyleSheet_Release(stylesheet);
1998 }
1999
2000 static void test_stylesheets(IHTMLDocument2 *doc)
2001 {
2002     IHTMLStyleSheetsCollection *col = NULL;
2003     VARIANT idx, res;
2004     long len = 0;
2005     HRESULT hres;
2006
2007     hres = IHTMLDocument2_get_styleSheets(doc, &col);
2008     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
2009     ok(col != NULL, "col == NULL\n");
2010
2011     hres = IHTMLStyleSheetsCollection_get_length(col, &len);
2012     ok(hres == S_OK, "get_length failed: %08x\n", hres);
2013     ok(len == 1, "len=%ld\n", len);
2014
2015     VariantInit(&res);
2016     V_VT(&idx) = VT_I4;
2017     V_I4(&idx) = 0;
2018
2019     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
2020     ok(hres == S_OK, "item failed: %08x\n", hres);
2021     ok(V_VT(&res) == VT_DISPATCH, "V_VT(res) = %d\n", V_VT(&res));
2022     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
2023     test_stylesheet(V_DISPATCH(&res));
2024     VariantClear(&res);
2025
2026     V_VT(&res) = VT_I4;
2027     V_VT(&idx) = VT_I4;
2028     V_I4(&idx) = 1;
2029
2030     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
2031     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2032     ok(V_VT(&res) == VT_EMPTY, "V_VT(res) = %d\n", V_VT(&res));
2033     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
2034     VariantClear(&res);
2035
2036     IHTMLStyleSheetsCollection_Release(col);
2037 }
2038
2039 static void test_child_col_disp(IHTMLDOMChildrenCollection *col)
2040 {
2041     IDispatchEx *dispex;
2042     IHTMLDOMNode *node;
2043     DISPPARAMS dp = {NULL, NULL, 0, 0};
2044     VARIANT var;
2045     EXCEPINFO ei;
2046     long type;
2047     DISPID id;
2048     BSTR bstr;
2049     HRESULT hres;
2050
2051     static const WCHAR w0[] = {'0',0};
2052     static const WCHAR w100[] = {'1','0','0',0};
2053
2054     hres = IHTMLDOMChildrenCollection_QueryInterface(col, &IID_IDispatchEx, (void**)&dispex);
2055     ok(hres == S_OK, "Could not get IDispatchEx: %08x\n", hres);
2056
2057     bstr = SysAllocString(w0);
2058     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
2059     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
2060     SysFreeString(bstr);
2061
2062     VariantInit(&var);
2063     hres = IDispatchEx_InvokeEx(dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
2064     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
2065     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
2066     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(var) == NULL\n");
2067     node = get_node_iface((IUnknown*)V_DISPATCH(&var));
2068     type = get_node_type((IUnknown*)node);
2069     ok(type == 3, "type=%ld\n", type);
2070     IHTMLDOMNode_Release(node);
2071     VariantClear(&var);
2072
2073     bstr = SysAllocString(w100);
2074     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
2075     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
2076     SysFreeString(bstr);
2077
2078     IDispatchEx_Release(dispex);
2079 }
2080
2081
2082
2083 static void test_elems(IHTMLDocument2 *doc)
2084 {
2085     IHTMLElementCollection *col;
2086     IHTMLDOMChildrenCollection *child_col;
2087     IHTMLElement *elem;
2088     IHTMLDOMNode *node, *node2;
2089     IDispatch *disp;
2090     long type;
2091     HRESULT hres;
2092
2093     static const WCHAR imgidW[] = {'i','m','g','i','d',0};
2094     static const WCHAR inW[] = {'i','n',0};
2095     static const WCHAR xW[] = {'x',0};
2096     static const WCHAR sW[] = {'s',0};
2097     static const WCHAR scW[] = {'s','c',0};
2098     static const WCHAR xxxW[] = {'x','x','x',0};
2099
2100     static const elem_type_t all_types[] = {
2101         ET_HTML,
2102         ET_HEAD,
2103         ET_TITLE,
2104         ET_STYLE,
2105         ET_BODY,
2106         ET_COMMENT,
2107         ET_A,
2108         ET_INPUT,
2109         ET_SELECT,
2110         ET_OPTION,
2111         ET_OPTION,
2112         ET_TEXTAREA,
2113         ET_TABLE,
2114         ET_TBODY,
2115         ET_SCRIPT,
2116         ET_TEST,
2117         ET_IMG
2118     };
2119
2120     static const elem_type_t item_types[] = {
2121         ET_A,
2122         ET_OPTION,
2123         ET_TEXTAREA
2124     };
2125
2126     hres = IHTMLDocument2_get_all(doc, &col);
2127     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2128     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
2129     test_elem_col_item(col, xW, item_types, sizeof(item_types)/sizeof(item_types[0]));
2130     IHTMLElementCollection_Release(col);
2131
2132     elem = get_doc_elem(doc);
2133     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
2134     hres = IHTMLElement_get_all(elem, &disp);
2135     IHTMLElement_Release(elem);
2136     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2137
2138     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&col);
2139     IDispatch_Release(disp);
2140     ok(hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
2141     test_elem_collection((IUnknown*)col, all_types+1, sizeof(all_types)/sizeof(all_types[0])-1);
2142     IHTMLElementCollection_Release(col);
2143
2144     get_elem_by_id(doc, xxxW, FALSE);
2145     elem = get_doc_elem_by_id(doc, xxxW);
2146     ok(!elem, "elem != NULL\n");
2147
2148     elem = get_doc_elem_by_id(doc, sW);
2149     ok(elem != NULL, "elem == NULL\n");
2150     if(elem) {
2151         test_elem_type((IUnknown*)elem, ET_SELECT);
2152         test_elem_attr(elem, xxxW, NULL);
2153         test_elem_attr(elem, idW, sW);
2154         test_elem_class((IUnknown*)elem, NULL);
2155         test_elem_set_class((IUnknown*)elem, "cl");
2156         test_elem_set_class((IUnknown*)elem, NULL);
2157         test_elem_tabindex((IUnknown*)elem, 0);
2158         test_elem_set_tabindex((IUnknown*)elem, 1);
2159
2160         node = test_node_get_parent((IUnknown*)elem);
2161         ok(node != NULL, "node == NULL\n");
2162         test_node_name((IUnknown*)node, "BODY");
2163         node2 = test_node_get_parent((IUnknown*)node);
2164         IHTMLDOMNode_Release(node);
2165         ok(node2 != NULL, "node == NULL\n");
2166         test_node_name((IUnknown*)node2, "HTML");
2167         node = test_node_get_parent((IUnknown*)node2);
2168         IHTMLDOMNode_Release(node2);
2169         ok(node != NULL, "node == NULL\n");
2170         test_node_name((IUnknown*)node, "#document");
2171         type = get_node_type((IUnknown*)node);
2172         ok(type == 9, "type=%ld, expected 9\n", type);
2173         node2 = test_node_get_parent((IUnknown*)node);
2174         IHTMLDOMNode_Release(node);
2175         ok(node2 == NULL, "node != NULL\n");
2176
2177         test_elem_getelembytag((IUnknown*)elem, ET_OPTION, 2);
2178         test_elem_getelembytag((IUnknown*)elem, ET_SELECT, 0);
2179         test_elem_getelembytag((IUnknown*)elem, ET_HTML, 0);
2180
2181         IHTMLElement_Release(elem);
2182     }
2183
2184     elem = get_elem_by_id(doc, sW, TRUE);
2185     if(elem) {
2186         IHTMLSelectElement *select;
2187
2188         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLSelectElement, (void**)&select);
2189         ok(hres == S_OK, "Could not get IHTMLSelectElement interface: %08x\n", hres);
2190
2191         test_select_elem(select);
2192
2193         test_elem_title((IUnknown*)select, NULL);
2194         test_elem_set_title((IUnknown*)select, "Title");
2195         test_elem_title((IUnknown*)select, "Title");
2196         test_elem_offset((IUnknown*)select);
2197
2198         node = get_first_child((IUnknown*)select);
2199         ok(node != NULL, "node == NULL\n");
2200         if(node) {
2201             test_elem_type((IUnknown*)node, ET_OPTION);
2202             IHTMLDOMNode_Release(node);
2203         }
2204
2205         type = get_node_type((IUnknown*)select);
2206         ok(type == 1, "type=%ld\n", type);
2207
2208         IHTMLSelectElement_Release(select);
2209         IHTMLElement_Release(elem);
2210     }
2211
2212     elem = get_elem_by_id(doc, scW, TRUE);
2213     if(elem) {
2214         IHTMLScriptElement *script;
2215         BSTR type;
2216
2217         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLScriptElement, (void**)&script);
2218         ok(hres == S_OK, "Could not get IHTMLScriptElement interface: %08x\n", hres);
2219
2220         hres = IHTMLScriptElement_get_type(script, &type);
2221         ok(hres == S_OK, "get_type failed: %08x\n", hres);
2222         ok(!lstrcmpW(type, text_javascriptW), "Unexpected type %s\n", dbgstr_w(type));
2223         SysFreeString(type);
2224
2225         IHTMLScriptElement_Release(script);
2226     }
2227
2228     elem = get_elem_by_id(doc, inW, TRUE);
2229     if(elem) {
2230         IHTMLInputElement *input;
2231
2232         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLInputElement, (void**)&input);
2233         ok(hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
2234
2235         test_elem_id((IUnknown*)elem, "in");
2236         test_elem_put_id((IUnknown*)elem, "newin");
2237         test_input_get_disabled(input, VARIANT_FALSE);
2238         test_input_set_disabled(input, VARIANT_TRUE);
2239         test_input_set_disabled(input, VARIANT_FALSE);
2240         test_elem_client_size((IUnknown*)elem);
2241
2242         test_node_get_value_str((IUnknown*)elem, NULL);
2243         test_node_put_value_str((IUnknown*)elem, "test");
2244         test_node_get_value_str((IUnknown*)elem, NULL);
2245         test_input_value((IUnknown*)elem, NULL);
2246         test_input_put_value((IUnknown*)elem, "test");
2247         test_input_value((IUnknown*)elem, NULL);
2248         test_elem_class((IUnknown*)elem, "testclass");
2249         test_elem_tabindex((IUnknown*)elem, 2);
2250         test_elem_set_tabindex((IUnknown*)elem, 3);
2251         test_elem_title((IUnknown*)elem, "test title");
2252
2253         IHTMLInputElement_Release(input);
2254         IHTMLElement_Release(elem);
2255     }
2256
2257     elem = get_elem_by_id(doc, imgidW, TRUE);
2258     if(elem) {
2259         test_img_set_src((IUnknown*)elem, "about:blank");
2260         test_img_alt((IUnknown*)elem, NULL);
2261         test_img_set_alt((IUnknown*)elem, "alt test");
2262         IHTMLElement_Release(elem);
2263     }
2264
2265     hres = IHTMLDocument2_get_body(doc, &elem);
2266     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2267
2268     node = get_first_child((IUnknown*)elem);
2269     ok(node != NULL, "node == NULL\n");
2270     if(node) {
2271         test_ifaces((IUnknown*)node, text_iids);
2272         test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
2273
2274         node2 = get_first_child((IUnknown*)node);
2275         ok(!node2, "node2 != NULL\n");
2276
2277         type = get_node_type((IUnknown*)node);
2278         ok(type == 3, "type=%ld\n", type);
2279
2280         test_node_get_value_str((IUnknown*)node, "text test");
2281         test_node_put_value_str((IUnknown*)elem, "test text");
2282         test_node_get_value_str((IUnknown*)node, "text test");
2283
2284         IHTMLDOMNode_Release(node);
2285     }
2286
2287     child_col = get_child_nodes((IUnknown*)elem);
2288     ok(child_col != NULL, "child_coll == NULL\n");
2289     if(child_col) {
2290         long length = 0;
2291
2292         test_disp((IUnknown*)child_col, &DIID_DispDOMChildrenCollection);
2293
2294         hres = IHTMLDOMChildrenCollection_get_length(child_col, &length);
2295         ok(hres == S_OK, "get_length failed: %08x\n", hres);
2296         ok(length, "length=0\n");
2297
2298         node = get_child_item(child_col, 0);
2299         ok(node != NULL, "node == NULL\n");
2300         if(node) {
2301             type = get_node_type((IUnknown*)node);
2302             ok(type == 3, "type=%ld\n", type);
2303             IHTMLDOMNode_Release(node);
2304         }
2305
2306         node = get_child_item(child_col, 1);
2307         ok(node != NULL, "node == NULL\n");
2308         if(node) {
2309             type = get_node_type((IUnknown*)node);
2310             ok(type == 8, "type=%ld\n", type);
2311
2312             test_elem_id((IUnknown*)node, NULL);
2313             IHTMLDOMNode_Release(node);
2314         }
2315
2316         disp = (void*)0xdeadbeef;
2317         hres = IHTMLDOMChildrenCollection_item(child_col, 6000, &disp);
2318         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2319         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
2320
2321         disp = (void*)0xdeadbeef;
2322         hres = IHTMLDOMChildrenCollection_item(child_col, length, &disp);
2323         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2324         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
2325
2326         test_child_col_disp(child_col);
2327
2328         IHTMLDOMChildrenCollection_Release(child_col);
2329     }
2330
2331     IHTMLElement_Release(elem);
2332
2333     test_stylesheets(doc);
2334     test_create_option_elem(doc);
2335 }
2336
2337 static void test_create_elems(IHTMLDocument2 *doc)
2338 {
2339     IHTMLElement *elem, *body, *elem2;
2340     IHTMLDOMNode *node;
2341     IDispatch *disp;
2342     long type;
2343     HRESULT hres;
2344
2345     static const elem_type_t types1[] = { ET_TESTG };
2346
2347     elem = test_create_elem(doc, "TEST");
2348     test_elem_tag((IUnknown*)elem, "TEST");
2349     type = get_node_type((IUnknown*)elem);
2350     ok(type == 1, "type=%ld\n", type);
2351     test_ifaces((IUnknown*)elem, elem_iids);
2352     test_disp((IUnknown*)elem, &DIID_DispHTMLGenericElement);
2353
2354     hres = IHTMLDocument2_get_body(doc, &body);
2355     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2356     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
2357
2358     node = test_node_append_child((IUnknown*)body, (IUnknown*)elem);
2359     test_node_has_child((IUnknown*)body, VARIANT_TRUE);
2360     elem2 = get_elem_iface((IUnknown*)node);
2361     IHTMLElement_Release(elem2);
2362
2363     hres = IHTMLElement_get_all(body, &disp);
2364     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2365     test_elem_collection((IUnknown*)disp, types1, sizeof(types1)/sizeof(types1[0]));
2366     IDispatch_Release(disp);
2367
2368     test_node_remove_child((IUnknown*)body, node);
2369
2370     hres = IHTMLElement_get_all(body, &disp);
2371     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2372     test_elem_collection((IUnknown*)disp, NULL, 0);
2373     IDispatch_Release(disp);
2374     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
2375
2376     IHTMLElement_Release(body);
2377     IHTMLElement_Release(elem);
2378     IHTMLDOMNode_Release(node);
2379
2380     node = test_create_text(doc, "test");
2381     test_ifaces((IUnknown*)node, text_iids);
2382     test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
2383     IHTMLDOMNode_Release(node);
2384 }
2385
2386 static void test_exec(IUnknown *unk, const GUID *grpid, DWORD cmdid, VARIANT *in, VARIANT *out)
2387 {
2388     IOleCommandTarget *cmdtrg;
2389     HRESULT hres;
2390
2391     hres = IHTMLTxtRange_QueryInterface(unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
2392     ok(hres == S_OK, "Could not get IOleCommandTarget interface: %08x\n", hres);
2393
2394     hres = IOleCommandTarget_Exec(cmdtrg, grpid, cmdid, 0, in, out);
2395     ok(hres == S_OK, "Exec failed: %08x\n", hres);
2396
2397     IOleCommandTarget_Release(cmdtrg);
2398 }
2399
2400 static void test_indent(IHTMLDocument2 *doc)
2401 {
2402     IHTMLElementCollection *col;
2403     IHTMLTxtRange *range;
2404     HRESULT hres;
2405
2406     static const elem_type_t all_types[] = {
2407         ET_HTML,
2408         ET_HEAD,
2409         ET_TITLE,
2410         ET_BODY,
2411         ET_BR,
2412         ET_A,
2413     };
2414
2415     static const elem_type_t indent_types[] = {
2416         ET_HTML,
2417         ET_HEAD,
2418         ET_TITLE,
2419         ET_BODY,
2420         ET_BLOCKQUOTE,
2421         ET_P,
2422         ET_BR,
2423         ET_A,
2424     };
2425
2426     hres = IHTMLDocument2_get_all(doc, &col);
2427     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2428     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
2429     IHTMLElementCollection_Release(col);
2430
2431     range = test_create_body_range(doc);
2432     test_exec((IUnknown*)range, &CGID_MSHTML, IDM_INDENT, NULL, NULL);
2433     IHTMLTxtRange_Release(range);
2434
2435     hres = IHTMLDocument2_get_all(doc, &col);
2436     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2437     test_elem_collection((IUnknown*)col, indent_types, sizeof(indent_types)/sizeof(indent_types[0]));
2438     IHTMLElementCollection_Release(col);
2439 }
2440
2441 static IHTMLDocument2 *notif_doc;
2442 static BOOL doc_complete;
2443
2444 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
2445         REFIID riid, void**ppv)
2446 {
2447     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
2448         *ppv = iface;
2449         return S_OK;
2450     }
2451
2452     ok(0, "unexpected call\n");
2453     return E_NOINTERFACE;
2454 }
2455
2456 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
2457 {
2458     return 2;
2459 }
2460
2461 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
2462 {
2463     return 1;
2464 }
2465
2466 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
2467 {
2468     if(dispID == DISPID_READYSTATE){
2469         BSTR state;
2470         HRESULT hres;
2471
2472         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
2473
2474         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
2475         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
2476
2477         if(!lstrcmpW(state, completeW))
2478             doc_complete = TRUE;
2479
2480         SysFreeString(state);
2481     }
2482
2483     return S_OK;
2484 }
2485
2486 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
2487 {
2488     ok(0, "unexpected call\n");
2489     return E_NOTIMPL;
2490 }
2491
2492 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
2493     PropertyNotifySink_QueryInterface,
2494     PropertyNotifySink_AddRef,
2495     PropertyNotifySink_Release,
2496     PropertyNotifySink_OnChanged,
2497     PropertyNotifySink_OnRequestEdit
2498 };
2499
2500 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
2501
2502 static IHTMLDocument2 *create_doc_with_string(const char *str)
2503 {
2504     IPersistStreamInit *init;
2505     IStream *stream;
2506     IHTMLDocument2 *doc;
2507     HGLOBAL mem;
2508     SIZE_T len;
2509
2510     notif_doc = doc = create_document();
2511     if(!doc)
2512         return NULL;
2513
2514     doc_complete = FALSE;
2515     len = strlen(str);
2516     mem = GlobalAlloc(0, len);
2517     memcpy(mem, str, len);
2518     CreateStreamOnHGlobal(mem, TRUE, &stream);
2519
2520     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
2521
2522     IPersistStreamInit_Load(init, stream);
2523     IPersistStreamInit_Release(init);
2524     IStream_Release(stream);
2525
2526     return doc;
2527 }
2528
2529 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
2530 {
2531     IConnectionPointContainer *container;
2532     IConnectionPoint *cp;
2533     DWORD cookie;
2534     HRESULT hres;
2535
2536     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
2537     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
2538
2539     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
2540     IConnectionPointContainer_Release(container);
2541     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
2542
2543     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
2544     IConnectionPoint_Release(cp);
2545     ok(hres == S_OK, "Advise failed: %08x\n", hres);
2546 }
2547
2548 typedef void (*domtest_t)(IHTMLDocument2*);
2549
2550 static void run_domtest(const char *str, domtest_t test)
2551 {
2552     IHTMLDocument2 *doc;
2553     IHTMLElement *body = NULL;
2554     ULONG ref;
2555     MSG msg;
2556     HRESULT hres;
2557
2558     doc = create_doc_with_string(str);
2559     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
2560
2561     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
2562         TranslateMessage(&msg);
2563         DispatchMessage(&msg);
2564     }
2565
2566     hres = IHTMLDocument2_get_body(doc, &body);
2567     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2568
2569     if(body) {
2570         IHTMLElement_Release(body);
2571         test(doc);
2572     }else {
2573         skip("Could not get document body. Assuming no Gecko installed.\n");
2574     }
2575
2576     ref = IHTMLDocument2_Release(doc);
2577     ok(!ref, "ref = %d\n", ref);
2578 }
2579
2580 static void gecko_installer_workaround(BOOL disable)
2581 {
2582     HKEY hkey;
2583     DWORD res;
2584
2585     static BOOL has_url = FALSE;
2586     static char url[2048];
2587
2588     if(!disable && !has_url)
2589         return;
2590
2591     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
2592     if(res != ERROR_SUCCESS)
2593         return;
2594
2595     if(disable) {
2596         DWORD type, size = sizeof(url);
2597
2598         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
2599         if(res == ERROR_SUCCESS && type == REG_SZ)
2600             has_url = TRUE;
2601
2602         RegDeleteValue(hkey, "GeckoUrl");
2603     }else {
2604         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
2605     }
2606
2607     RegCloseKey(hkey);
2608 }
2609
2610 START_TEST(dom)
2611 {
2612     gecko_installer_workaround(TRUE);
2613     CoInitialize(NULL);
2614
2615     run_domtest(doc_str1, test_doc_elem);
2616     run_domtest(range_test_str, test_txtrange);
2617     run_domtest(range_test2_str, test_txtrange2);
2618     run_domtest(elem_test_str, test_elems);
2619     run_domtest(doc_blank, test_create_elems);
2620     run_domtest(doc_blank, test_defaults);
2621     run_domtest(indent_test_str, test_indent);
2622
2623     CoUninitialize();
2624     gecko_installer_workaround(FALSE);
2625 }