quartz: Silence requests for ipin on filters.
[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
34 static const char doc_blank[] = "<html></html>";
35 static const char doc_str1[] = "<html><body>test</body></html>";
36 static const char range_test_str[] =
37     "<html><body>test \na<font size=\"2\">bc\t123<br /> it's\r\n  \t</font>text<br /></body></html>";
38 static const char range_test2_str[] =
39     "<html><body>abc<hr />123<br /><hr />def</body></html>";
40 static const char elem_test_str[] =
41     "<html><head><title>test</title><style>.body { margin-right: 0px; }</style>"
42     "<body><a href=\"http://test\" name=\"x\">link</a><input />"
43     "<select id=\"s\"><option id=\"x\">opt1</option><option id=\"y\">opt2</option></select>"
44     "<textarea id=\"X\">text text</textarea>"
45     "<table><tbody></tbody></table>"
46     "<script id=\"sc\" type=\"text/javascript\"></script>"
47     "</body></html>";
48 static const char indent_test_str[] =
49     "<html><head><title>test</title></head><body>abc<br /><a href=\"about:blank\">123</a></body></html>";
50
51 static const WCHAR noneW[] = {'N','o','n','e',0};
52
53 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
54 static WCHAR texteditW[] = {'t','e','x','t','e','d','i','t',0};
55 static WCHAR wordW[] = {'w','o','r','d',0};
56
57 static const WCHAR text_javascriptW[] = {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
58
59 typedef enum {
60     ET_NONE,
61     ET_HTML,
62     ET_HEAD,
63     ET_TITLE,
64     ET_BODY,
65     ET_A,
66     ET_INPUT,
67     ET_SELECT,
68     ET_TEXTAREA,
69     ET_OPTION,
70     ET_STYLE,
71     ET_BLOCKQUOTE,
72     ET_P,
73     ET_BR,
74     ET_TABLE,
75     ET_TBODY,
76     ET_SCRIPT
77 } elem_type_t;
78
79 static REFIID const none_iids[] = {
80     &IID_IUnknown,
81     NULL
82 };
83
84 static REFIID const elem_iids[] = {
85     &IID_IHTMLDOMNode,
86     &IID_IHTMLElement,
87     &IID_IHTMLElement2,
88     &IID_IConnectionPointContainer,
89     NULL
90 };
91
92 static REFIID const body_iids[] = {
93     &IID_IHTMLDOMNode,
94     &IID_IHTMLElement,
95     &IID_IHTMLElement2,
96     &IID_IHTMLTextContainer,
97     &IID_IHTMLBodyElement,
98     &IID_IConnectionPointContainer,
99     NULL
100 };
101
102 static REFIID const anchor_iids[] = {
103     &IID_IHTMLDOMNode,
104     &IID_IHTMLElement,
105     &IID_IHTMLElement2,
106     &IID_IHTMLAnchorElement,
107     &IID_IConnectionPointContainer,
108     NULL
109 };
110
111 static REFIID const input_iids[] = {
112     &IID_IHTMLDOMNode,
113     &IID_IHTMLElement,
114     &IID_IHTMLElement2,
115     &IID_IHTMLInputElement,
116     &IID_IHTMLInputTextElement,
117     &IID_IConnectionPointContainer,
118     NULL
119 };
120
121 static REFIID const select_iids[] = {
122     &IID_IHTMLDOMNode,
123     &IID_IHTMLElement,
124     &IID_IHTMLElement2,
125     &IID_IHTMLSelectElement,
126     &IID_IConnectionPointContainer,
127     NULL
128 };
129
130 static REFIID const textarea_iids[] = {
131     &IID_IHTMLDOMNode,
132     &IID_IHTMLElement,
133     &IID_IHTMLElement2,
134     &IID_IHTMLTextAreaElement,
135     &IID_IConnectionPointContainer,
136     NULL
137 };
138
139 static REFIID const option_iids[] = {
140     &IID_IHTMLDOMNode,
141     &IID_IHTMLElement,
142     &IID_IHTMLElement2,
143     &IID_IHTMLOptionElement,
144     &IID_IConnectionPointContainer,
145     NULL
146 };
147
148 static REFIID const table_iids[] = {
149     &IID_IHTMLDOMNode,
150     &IID_IHTMLElement,
151     &IID_IHTMLElement2,
152     &IID_IHTMLTable,
153     &IID_IConnectionPointContainer,
154     NULL
155 };
156
157 static REFIID const script_iids[] = {
158     &IID_IHTMLDOMNode,
159     &IID_IHTMLElement,
160     &IID_IHTMLElement2,
161     &IID_IHTMLScriptElement,
162     &IID_IConnectionPointContainer,
163     NULL
164 };
165
166 typedef struct {
167     const char *tag;
168     REFIID *iids;
169 } elem_type_info_t;
170
171 static const elem_type_info_t elem_type_infos[] = {
172     {"",          none_iids},
173     {"HTML",      elem_iids},
174     {"HEAD",      elem_iids},
175     {"TITLE",     elem_iids},
176     {"BODY",      body_iids},
177     {"A",         anchor_iids},
178     {"INPUT",     input_iids},
179     {"SELECT",    select_iids},
180     {"TEXTAREA",  textarea_iids},
181     {"OPTION",    option_iids},
182     {"STYLE",     elem_iids},
183     {"BLOCKQUOTE",elem_iids},
184     {"P",         elem_iids},
185     {"BR",        elem_iids},
186     {"TABLE",     table_iids},
187     {"TBODY",     elem_iids},
188     {"SCRIPT",    script_iids}
189 };
190
191 static const char *dbgstr_w(LPCWSTR str)
192 {
193     static char buf[512];
194     if(!str)
195         return "(null)";
196     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
197     return buf;
198 }
199
200 static const char *dbgstr_guid(REFIID riid)
201 {
202     static char buf[50];
203
204     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
205             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
206             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
207             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
208
209     return buf;
210 }
211
212 static int strcmp_wa(LPCWSTR strw, const char *stra)
213 {
214     WCHAR buf[512];
215     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
216     return lstrcmpW(strw, buf);
217 }
218
219 static BSTR a2bstr(const char *str)
220 {
221     BSTR ret;
222     int len;
223
224     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
225     ret = SysAllocStringLen(NULL, len);
226     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
227
228     return ret;
229 }
230
231 static IHTMLDocument2 *create_document(void)
232 {
233     IHTMLDocument2 *doc;
234     HRESULT hres;
235
236     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
237             &IID_IHTMLDocument2, (void**)&doc);
238     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
239
240     return doc;
241 }
242
243 #define test_ifaces(i,ids) _test_ifaces(__LINE__,i,ids)
244 static void _test_ifaces(unsigned line, IUnknown *iface, REFIID *iids)
245 {
246     const IID * const *piid;
247     IUnknown *unk;
248     HRESULT hres;
249
250      for(piid = iids; *piid; piid++) {
251         hres = IDispatch_QueryInterface(iface, *piid, (void**)&unk);
252         ok_(__FILE__,line) (hres == S_OK, "Could not get %s interface: %08x\n", dbgstr_guid(*piid), hres);
253         if(SUCCEEDED(hres))
254             IUnknown_Release(unk);
255     }
256 }
257
258 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
259 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
260 {
261     IHTMLDOMNode *node;
262     BSTR name;
263     HRESULT hres;
264
265     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
266     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLNode) failed: %08x\n", hres);
267
268     hres = IHTMLDOMNode_get_nodeName(node, &name);
269     IHTMLDOMNode_Release(node);
270     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
271     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
272
273     SysFreeString(name);
274 }
275
276 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
277 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
278 {
279     IHTMLElement *elem;
280     BSTR tag;
281     HRESULT hres;
282
283     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
284     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLElement) failed: %08x\n", hres);
285
286     hres = IHTMLElement_get_tagName(elem, &tag);
287     IHTMLElement_Release(elem);
288     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
289     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
290
291     SysFreeString(tag);
292 }
293
294 #define test_elem_type(ifc,t) _test_elem_type(__LINE__,ifc,t)
295 static void _test_elem_type(unsigned line, IUnknown *unk, elem_type_t type)
296 {
297     _test_elem_tag(line, unk, elem_type_infos[type].tag);
298     _test_ifaces(line, unk, elem_type_infos[type].iids);
299 }
300
301 static void test_doc_elem(IHTMLDocument2 *doc)
302 {
303     IHTMLElement *elem;
304     IHTMLDocument3 *doc3;
305     HRESULT hres;
306
307     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
308     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
309
310     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
311     IHTMLDocument3_Release(doc3);
312     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
313
314     test_node_name((IUnknown*)elem, "HTML");
315     test_elem_tag((IUnknown*)elem, "HTML");
316
317     IHTMLElement_Release(elem);
318 }
319
320 #define get_doc_elem(d) _get_doc_elem(__LINE__,d)
321 static IHTMLElement *_get_doc_elem(unsigned line, IHTMLDocument2 *doc)
322 {
323     IHTMLElement *elem;
324     IHTMLDocument3 *doc3;
325     HRESULT hres;
326
327     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
328     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 interface: %08x\n", hres);
329     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
330     ok_(__FILE__,line) (hres == S_OK, "get_documentElement failed: %08x\n", hres);
331     IHTMLDocument3_Release(doc3);
332
333     return elem;
334 }
335
336 #define test_option_text(o,t) _test_option_text(__LINE__,o,t)
337 static void _test_option_text(unsigned line, IHTMLOptionElement *option, const char *text)
338 {
339     BSTR bstr;
340     HRESULT hres;
341
342     hres = IHTMLOptionElement_get_text(option, &bstr);
343     ok_(__FILE__,line) (hres == S_OK, "get_text failed: %08x\n", hres);
344     ok_(__FILE__,line) (!strcmp_wa(bstr, text), "text=%s\n", dbgstr_w(bstr));
345     SysFreeString(bstr);
346 }
347
348 #define test_option_put_text(o,t) _test_option_put_text(__LINE__,o,t)
349 static void _test_option_put_text(unsigned line, IHTMLOptionElement *option, const char *text)
350 {
351     BSTR bstr;
352     HRESULT hres;
353
354     bstr = a2bstr(text);
355     hres = IHTMLOptionElement_put_text(option, bstr);
356     SysFreeString(bstr);
357     ok(hres == S_OK, "put_text failed: %08x\n", hres);
358
359     _test_option_text(line, option, text);
360 }
361
362 #define test_option_value(o,t) _test_option_value(__LINE__,o,t)
363 static void _test_option_value(unsigned line, IHTMLOptionElement *option, const char *value)
364 {
365     BSTR bstr;
366     HRESULT hres;
367
368     hres = IHTMLOptionElement_get_value(option, &bstr);
369     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
370     ok_(__FILE__,line) (!strcmp_wa(bstr, value), "value=%s\n", dbgstr_w(bstr));
371     SysFreeString(bstr);
372 }
373
374 #define test_option_put_value(o,t) _test_option_put_value(__LINE__,o,t)
375 static void _test_option_put_value(unsigned line, IHTMLOptionElement *option, const char *value)
376 {
377     BSTR bstr;
378     HRESULT hres;
379
380     bstr = a2bstr(value);
381     hres = IHTMLOptionElement_put_value(option, bstr);
382     SysFreeString(bstr);
383     ok(hres == S_OK, "put_value failed: %08x\n", hres);
384
385     _test_option_value(line, option, value);
386 }
387
388 #define create_option_elem(d,t,v) _create_option_elem(__LINE__,d,t,v)
389 static IHTMLOptionElement *_create_option_elem(unsigned line, IHTMLDocument2 *doc,
390         const char *txt, const char *val)
391 {
392     IHTMLOptionElementFactory *factory;
393     IHTMLOptionElement *option;
394     IHTMLWindow2 *window;
395     VARIANT text, value, empty;
396     HRESULT hres;
397
398     hres = IHTMLDocument2_get_parentWindow(doc, &window);
399     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
400
401     hres = IHTMLWindow2_get_Option(window, &factory);
402     IHTMLWindow2_Release(window);
403     ok_(__FILE__,line) (hres == S_OK, "get_Option failed: %08x\n", hres);
404
405     V_VT(&text) = VT_BSTR;
406     V_BSTR(&text) = a2bstr(txt);
407     V_VT(&value) = VT_BSTR;
408     V_BSTR(&value) = a2bstr(val);
409     V_VT(&empty) = VT_EMPTY;
410
411     hres = IHTMLOptionElementFactory_create(factory, text, value, empty, empty, &option);
412     ok_(__FILE__,line) (hres == S_OK, "create failed: %08x\n", hres);
413
414     IHTMLOptionElementFactory_Release(factory);
415     VariantClear(&text);
416     VariantClear(&value);
417
418     _test_option_text(line, option, txt);
419     _test_option_value(line, option, val);
420
421     return option;
422 }
423
424 #define test_select_length(s,l) _test_select_length(__LINE__,s,l)
425 static void _test_select_length(unsigned line, IHTMLSelectElement *select, long length)
426 {
427     long len = 0xdeadbeef;
428     HRESULT hres;
429
430     hres = IHTMLSelectElement_get_length(select, &len);
431     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
432     ok_(__FILE__,line) (len == length, "len=%ld, expected %ld\n", len, length);
433 }
434
435 #define test_select_selidx(s,i) _test_select_selidx(__LINE__,s,i)
436 static void _test_select_selidx(unsigned line, IHTMLSelectElement *select, long index)
437 {
438     long idx = 0xdeadbeef;
439     HRESULT hres;
440
441     hres = IHTMLSelectElement_get_selectedIndex(select, &idx);
442     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
443     ok_(__FILE__,line) (idx == index, "idx=%ld, expected %ld\n", idx, index);
444 }
445
446 #define test_select_put_selidx(s,i) _test_select_put_selidx(__LINE__,s,i)
447 static void _test_select_put_selidx(unsigned line, IHTMLSelectElement *select, long index)
448 {
449     HRESULT hres;
450
451     hres = IHTMLSelectElement_put_selectedIndex(select, index);
452     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
453     _test_select_selidx(line, select, index);
454 }
455
456 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
457 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
458 {
459     BSTR text;
460     HRESULT hres;
461
462     hres = IHTMLTxtRange_get_text(range, &text);
463     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
464
465     if(extext) {
466         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
467         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
468     }else {
469         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
470     }
471
472     SysFreeString(text);
473
474 }
475
476 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
477 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
478 {
479     HRESULT hres;
480
481     hres = IHTMLTxtRange_collapse(range, b);
482     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
483     _test_range_text(line, range, NULL);
484 }
485
486 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
487 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
488         VARIANT_BOOL exb, const char *extext)
489 {
490     VARIANT_BOOL b = 0xe0e0;
491     HRESULT hres;
492
493     hres = IHTMLTxtRange_expand(range, unit, &b);
494     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
495     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
496     _test_range_text(line, range, extext);
497 }
498
499 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
500 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
501 {
502     long c = 0xdeadbeef;
503     HRESULT hres;
504
505     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
506     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
507     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
508     _test_range_text(line, range, NULL);
509 }
510
511 #define test_range_movestart(r,u,c,e) _test_range_movestart(__LINE__,r,u,c,e)
512 static void _test_range_movestart(unsigned line, IHTMLTxtRange *range,
513         LPWSTR unit, long cnt, long excnt)
514 {
515     long c = 0xdeadbeef;
516     HRESULT hres;
517
518     hres = IHTMLTxtRange_moveStart(range, unit, cnt, &c);
519     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
520     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
521 }
522
523 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
524 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
525 {
526     long c = 0xdeadbeef;
527     HRESULT hres;
528
529     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
530     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
531     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
532 }
533
534 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
535 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, const char *text)
536 {
537     HRESULT hres;
538     BSTR bstr = a2bstr(text);
539
540     hres = IHTMLTxtRange_put_text(range, bstr);
541     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
542     SysFreeString(bstr);
543     _test_range_text(line, range, NULL);
544 }
545
546 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
547 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
548 {
549     VARIANT_BOOL b;
550     HRESULT hres;
551
552     b = 0xe0e0;
553     hres = IHTMLTxtRange_inRange(range1, range2, &b);
554     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
555     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
556 }
557
558 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
559 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
560 {
561     VARIANT_BOOL b;
562     HRESULT hres;
563
564     b = 0xe0e0;
565     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
566     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
567     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
568
569     b = 0xe0e0;
570     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
571     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
572     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
573
574     if(exb) {
575         test_range_inrange(range1, range2, VARIANT_TRUE);
576         test_range_inrange(range2, range1, VARIANT_TRUE);
577     }
578 }
579
580 #define test_range_parent(r,t) _test_range_parent(__LINE__,r,t)
581 static void _test_range_parent(unsigned line, IHTMLTxtRange *range, elem_type_t type)
582 {
583     IHTMLElement *elem;
584     HRESULT hres;
585
586     hres = IHTMLTxtRange_parentElement(range, &elem);
587     ok_(__FILE__,line) (hres == S_OK, "parentElement failed: %08x\n", hres);
588
589     _test_elem_type(line, (IUnknown*)elem, type);
590
591     IHTMLElement_Release(elem);
592 }
593
594 #define test_elem_collection(c,t,l) _test_elem_collection(__LINE__,c,t,l)
595 static void _test_elem_collection(unsigned line, IHTMLElementCollection *col,
596         const elem_type_t *elem_types, long exlen)
597 {
598     long len;
599     DWORD i;
600     VARIANT name, index;
601     IDispatch *disp;
602     HRESULT hres;
603
604     hres = IHTMLElementCollection_get_length(col, &len);
605     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
606     ok_(__FILE__,line) (len == exlen, "len=%ld, expected %ld\n", len, exlen);
607
608     if(len > exlen)
609         len = exlen;
610
611     V_VT(&index) = VT_EMPTY;
612     V_VT(&name) = VT_I4;
613
614     for(i=0; i<len; i++) {
615         V_I4(&name) = i;
616         disp = (void*)0xdeadbeef;
617         hres = IHTMLElementCollection_item(col, name, index, &disp);
618         ok_(__FILE__,line) (hres == S_OK, "item(%d) failed: %08x\n", i, hres);
619         ok_(__FILE__,line) (disp != NULL, "item returned NULL\n");
620         if(FAILED(hres) || !disp)
621             continue;
622
623         _test_elem_type(line, (IUnknown*)disp, elem_types[i]);
624         IDispatch_Release(disp);
625     }
626
627     V_I4(&name) = len;
628     disp = (void*)0xdeadbeef;
629     hres = IHTMLElementCollection_item(col, name, index, &disp);
630     ok_(__FILE__,line) (hres == S_OK, "item failed: %08x\n", hres);
631     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
632
633     V_I4(&name) = -1;
634     disp = (void*)0xdeadbeef;
635     hres = IHTMLElementCollection_item(col, name, index, &disp);
636     ok_(__FILE__,line) (hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
637     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
638 }
639
640 static void test_elem_col_item(IHTMLElementCollection *col, LPCWSTR n,
641         const elem_type_t *elem_types, long len)
642 {
643     IHTMLElementCollection *elcol;
644     IDispatch *disp;
645     VARIANT name, index;
646     DWORD i;
647     HRESULT hres;
648
649     V_VT(&index) = VT_EMPTY;
650     V_VT(&name) = VT_BSTR;
651     V_BSTR(&name) = SysAllocString(n);
652
653     hres = IHTMLElementCollection_item(col, name, index, &disp);
654     ok(hres == S_OK, "item failed: %08x\n", hres);
655
656     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&elcol);
657     IDispatch_Release(disp);
658     ok(hres == S_OK, "Could not get IHTMLElementCollection interface: %08x\n", hres);
659     test_elem_collection(elcol, elem_types, len);
660     IHTMLElementCollection_Release(elcol);
661
662     V_VT(&index) = VT_I4;
663
664     for(i=0; i<len; i++) {
665         V_I4(&index) = i;
666         disp = (void*)0xdeadbeef;
667         hres = IHTMLElementCollection_item(col, name, index, &disp);
668         ok(hres == S_OK, "item failed: %08x\n", hres);
669         ok(disp != NULL, "disp == NULL\n");
670         if(FAILED(hres) || !disp)
671             continue;
672
673         test_elem_type((IUnknown*)disp, elem_types[i]);
674
675         IDispatch_Release(disp);
676     }
677
678     V_I4(&index) = len;
679     disp = (void*)0xdeadbeef;
680     hres = IHTMLElementCollection_item(col, name, index, &disp);
681     ok(hres == S_OK, "item failed: %08x\n", hres);
682     ok(disp == NULL, "disp != NULL\n");
683
684     V_I4(&index) = -1;
685     disp = (void*)0xdeadbeef;
686     hres = IHTMLElementCollection_item(col, name, index, &disp);
687     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
688     ok(disp == NULL, "disp != NULL\n");
689
690     SysFreeString(V_BSTR(&name));
691 }
692
693 static IHTMLElement *get_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id, BOOL expect_success)
694 {
695     IHTMLElementCollection *col;
696     IHTMLElement *elem;
697     IDispatch *disp = (void*)0xdeadbeef;
698     VARIANT name, index;
699     HRESULT hres;
700
701     hres = IHTMLDocument2_get_all(doc, &col);
702     ok(hres == S_OK, "get_all failed: %08x\n", hres);
703     ok(col != NULL, "col == NULL\n");
704     if(FAILED(hres) || !col)
705         return NULL;
706
707     V_VT(&index) = VT_EMPTY;
708     V_VT(&name) = VT_BSTR;
709     V_BSTR(&name) = SysAllocString(id);
710
711     hres = IHTMLElementCollection_item(col, name, index, &disp);
712     IHTMLElementCollection_Release(col);
713     SysFreeString(V_BSTR(&name));
714     ok(hres == S_OK, "item failed: %08x\n", hres);
715     if(!expect_success) {
716         ok(disp == NULL, "disp != NULL\n");
717         return NULL;
718     }
719
720     ok(disp != NULL, "disp == NULL\n");
721     if(!disp)
722         return NULL;
723
724     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElement, (void**)&elem);
725     IDispatch_Release(disp);
726     ok(hres == S_OK, "Could not get IHTMLElement interface: %08x\n", hres);
727
728     return elem;
729 }
730
731 static void test_select_elem(IHTMLSelectElement *select)
732 {
733     test_select_length(select, 2);
734     test_select_selidx(select, 0);
735     test_select_put_selidx(select, 1);
736 }
737
738 static void test_create_option_elem(IHTMLDocument2 *doc)
739 {
740     IHTMLOptionElement *option;
741
742     option = create_option_elem(doc, "test text", "test value");
743
744     test_option_put_text(option, "new text");
745     test_option_put_value(option, "new value");
746
747     IHTMLOptionElement_Release(option);
748 }
749
750 static IHTMLTxtRange *test_create_body_range(IHTMLDocument2 *doc)
751 {
752     IHTMLBodyElement *body;
753     IHTMLTxtRange *range;
754     IHTMLElement *elem;
755     HRESULT hres;
756
757     hres = IHTMLDocument2_get_body(doc, &elem);
758     ok(hres == S_OK, "get_body failed: %08x\n", hres);
759
760     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
761     IHTMLElement_Release(elem);
762
763     hres = IHTMLBodyElement_createTextRange(body, &range);
764     IHTMLBodyElement_Release(body);
765     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
766
767     return range;
768 }
769
770 static void test_txtrange(IHTMLDocument2 *doc)
771 {
772     IHTMLTxtRange *body_range, *range, *range2;
773     IHTMLSelectionObject *selection;
774     IDispatch *disp_range;
775     HRESULT hres;
776
777     body_range = test_create_body_range(doc);
778
779     test_range_text(body_range, "test abc 123\r\nit's text");
780
781     hres = IHTMLTxtRange_duplicate(body_range, &range);
782     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
783
784     hres = IHTMLTxtRange_duplicate(body_range, &range2);
785     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
786     test_range_isequal(range, range2, VARIANT_TRUE);
787
788     test_range_text(range, "test abc 123\r\nit's text");
789     test_range_text(body_range, "test abc 123\r\nit's text");
790
791     test_range_collapse(range, TRUE);
792     test_range_isequal(range, range2, VARIANT_FALSE);
793     test_range_inrange(range, range2, VARIANT_FALSE);
794     test_range_inrange(range2, range, VARIANT_TRUE);
795     IHTMLTxtRange_Release(range2);
796
797     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
798     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
799     test_range_move(range, characterW, 2, 2);
800     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
801
802     test_range_collapse(range, FALSE);
803     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
804
805     test_range_collapse(range, FALSE);
806     test_range_expand(range, wordW, VARIANT_TRUE, "123");
807     test_range_expand(range, wordW, VARIANT_FALSE, "123");
808     test_range_move(range, characterW, 2, 2);
809     test_range_expand(range, wordW, VARIANT_TRUE, "123");
810     test_range_moveend(range, characterW, -5, -5);
811     test_range_text(range, NULL);
812     test_range_moveend(range, characterW, 3, 3);
813     test_range_text(range, "c 1");
814     test_range_expand(range, texteditW, VARIANT_TRUE, "test abc 123\r\nit's text");
815     test_range_collapse(range, TRUE);
816     test_range_move(range, characterW, 4, 4);
817     test_range_moveend(range, characterW, 1, 1);
818     test_range_text(range, " ");
819     test_range_move(range, wordW, 1, 1);
820     test_range_moveend(range, characterW, 2, 2);
821     test_range_text(range, "ab");
822
823     IHTMLTxtRange_Release(range);
824
825     hres = IHTMLTxtRange_duplicate(body_range, &range);
826     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
827
828     test_range_text(range, "test abc 123\r\nit's text");
829     test_range_move(range, characterW, 3, 3);
830     test_range_moveend(range, characterW, 1, 1);
831     test_range_text(range, "t");
832     test_range_moveend(range, characterW, 3, 3);
833     test_range_text(range, "t ab");
834     test_range_moveend(range, characterW, -2, -2);
835     test_range_text(range, "t ");
836     test_range_move(range, characterW, 6, 6);
837     test_range_moveend(range, characterW, 3, 3);
838     test_range_text(range, "123");
839     test_range_moveend(range, characterW, 2, 2);
840     test_range_text(range, "123\r\ni");
841
842     IHTMLTxtRange_Release(range);
843
844     hres = IHTMLTxtRange_duplicate(body_range, &range);
845     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
846
847     test_range_move(range, wordW, 1, 1);
848     test_range_moveend(range, characterW, 2, 2);
849     test_range_text(range, "ab");
850
851     test_range_move(range, characterW, -2, -2);
852     test_range_moveend(range, characterW, 2, 2);
853     test_range_text(range, "t ");
854
855     test_range_move(range, wordW, 3, 3);
856     test_range_move(range, wordW, -2, -2);
857     test_range_moveend(range, characterW, 2, 2);
858     test_range_text(range, "ab");
859
860     test_range_move(range, characterW, -6, -5);
861     test_range_moveend(range, characterW, -1, 0);
862     test_range_moveend(range, characterW, -6, 0);
863     test_range_move(range, characterW, 2, 2);
864     test_range_moveend(range, characterW, 2, 2);
865     test_range_text(range, "st");
866     test_range_moveend(range, characterW, -6, -4);
867     test_range_moveend(range, characterW, 2, 2);
868
869     IHTMLTxtRange_Release(range);
870
871     hres = IHTMLTxtRange_duplicate(body_range, &range);
872     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
873
874     test_range_move(range, wordW, 2, 2);
875     test_range_moveend(range, characterW, 2, 2);
876     test_range_text(range, "12");
877
878     test_range_move(range, characterW, 15, 14);
879     test_range_move(range, characterW, -2, -2);
880     test_range_moveend(range, characterW, 3, 2);
881     test_range_text(range, "t");
882     test_range_moveend(range, characterW, -1, -1);
883     test_range_text(range, "t");
884     test_range_expand(range, wordW, VARIANT_TRUE, "text");
885     test_range_move(range, characterW, -2, -2);
886     test_range_moveend(range, characterW, 2, 2);
887     test_range_text(range, "s ");
888     test_range_move(range, characterW, 100, 7);
889     test_range_move(range, wordW, 1, 0);
890     test_range_move(range, characterW, -2, -2);
891     test_range_moveend(range, characterW, 3, 2);
892     test_range_text(range, "t");
893
894     IHTMLTxtRange_Release(range);
895
896     hres = IHTMLTxtRange_duplicate(body_range, &range);
897     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
898
899     test_range_collapse(range, TRUE);
900     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
901     test_range_put_text(range, "word");
902     test_range_text(body_range, "wordabc 123\r\nit's text");
903     test_range_text(range, NULL);
904     test_range_moveend(range, characterW, 3, 3);
905     test_range_text(range, "abc");
906     test_range_movestart(range, characterW, -2, -2);
907     test_range_text(range, "rdabc");
908     test_range_movestart(range, characterW, 3, 3);
909     test_range_text(range, "bc");
910     test_range_movestart(range, characterW, 4, 4);
911     test_range_text(range, NULL);
912     test_range_movestart(range, characterW, -3, -3);
913     test_range_text(range, "c 1");
914     test_range_movestart(range, characterW, -7, -6);
915     test_range_text(range, "wordabc 1");
916     test_range_movestart(range, characterW, 100, 22);
917     test_range_text(range, NULL);
918
919     IHTMLTxtRange_Release(range);
920     IHTMLTxtRange_Release(body_range);
921
922     hres = IHTMLDocument2_get_selection(doc, &selection);
923     ok(hres == S_OK, "IHTMLDocument2_get_selection failed: %08x\n", hres);
924
925     hres = IHTMLSelectionObject_createRange(selection, &disp_range);
926     ok(hres == S_OK, "IHTMLSelectionObject_createRange failed: %08x\n", hres);
927     IHTMLSelectionObject_Release(selection);
928
929     hres = IDispatch_QueryInterface(disp_range, &IID_IHTMLTxtRange, (void **)&range);
930     ok(hres == S_OK, "Could not get IID_IHTMLTxtRange interface: 0x%08x\n", hres);
931     IDispatch_Release(disp_range);
932
933     test_range_text(range, NULL);
934     test_range_moveend(range, characterW, 3, 3);
935     test_range_text(range, "wor");
936     test_range_parent(range, ET_BODY);
937     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
938     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
939     test_range_move(range, characterW, 3, 3);
940     test_range_expand(range, wordW, VARIANT_TRUE, "wordabc ");
941     test_range_moveend(range, characterW, -4, -4);
942     test_range_put_text(range, "abc def ");
943     test_range_expand(range, texteditW, VARIANT_TRUE, "abc def abc 123\r\nit's text");
944     test_range_move(range, wordW, 1, 1);
945     test_range_movestart(range, characterW, -1, -1);
946     test_range_text(range, " ");
947     test_range_move(range, wordW, 1, 1);
948     test_range_moveend(range, characterW, 3, 3);
949     test_range_text(range, "def");
950     test_range_put_text(range, "xyz");
951     test_range_moveend(range, characterW, 1, 1);
952     test_range_move(range, wordW, 1, 1);
953     test_range_moveend(range, characterW, 2, 2);
954     test_range_text(range, "ab");
955
956     IHTMLTxtRange_Release(range);
957 }
958
959 static void test_txtrange2(IHTMLDocument2 *doc)
960 {
961     IHTMLTxtRange *range;
962
963     range = test_create_body_range(doc);
964
965     test_range_text(range, "abc\r\n\r\n123\r\n\r\n\r\ndef");
966     test_range_move(range, characterW, 5, 5);
967     test_range_moveend(range, characterW, 1, 1);
968     test_range_text(range, "2");
969     test_range_move(range, characterW, -3, -3);
970     test_range_moveend(range, characterW, 3, 3);
971     test_range_text(range, "c\r\n\r\n1");
972     test_range_collapse(range, VARIANT_FALSE);
973     test_range_moveend(range, characterW, 4, 4);
974     test_range_text(range, "23");
975     test_range_moveend(range, characterW, 1, 1);
976     test_range_text(range, "23\r\n\r\n\r\nd");
977     test_range_moveend(range, characterW, -1, -1);
978     test_range_text(range, "23");
979     test_range_moveend(range, characterW, -1, -1);
980     test_range_text(range, "23");
981     test_range_moveend(range, characterW, -2, -2);
982     test_range_text(range, "2");
983
984     IHTMLTxtRange_Release(range);
985 }
986
987 static void test_compatmode(IHTMLDocument2 *doc)
988 {
989     IHTMLDocument5 *doc5;
990     BSTR mode;
991     HRESULT hres;
992
993     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
994     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
995     if(FAILED(hres))
996         return;
997
998     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
999     IHTMLDocument5_Release(doc5);
1000     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
1001     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
1002     SysFreeString(mode);
1003 }
1004
1005 static void test_default_style(IHTMLStyle *style)
1006 {
1007     VARIANT_BOOL b;
1008     VARIANT v;
1009     BSTR str;
1010     HRESULT hres;
1011
1012     str = (void*)0xdeadbeef;
1013     hres = IHTMLStyle_get_fontFamily(style, &str);
1014     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
1015     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
1016
1017     str = (void*)0xdeadbeef;
1018     hres = IHTMLStyle_get_fontWeight(style, &str);
1019     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
1020     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
1021
1022     str = (void*)0xdeadbeef;
1023     hres = IHTMLStyle_get_display(style, &str);
1024     ok(hres == S_OK, "get_display failed: %08x\n", hres);
1025     ok(!str, "display = %s\n", dbgstr_w(str));
1026
1027     str = (void*)0xdeadbeef;
1028     hres = IHTMLStyle_get_visibility(style, &str);
1029     ok(hres == S_OK, "get_visibility failed: %08x\n", hres);
1030     ok(!str, "visibility = %s\n", dbgstr_w(str));
1031
1032     V_VT(&v) = VT_NULL;
1033     hres = IHTMLStyle_get_fontSize(style, &v);
1034     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
1035     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
1036     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
1037
1038     V_VT(&v) = VT_NULL;
1039     hres = IHTMLStyle_get_color(style, &v);
1040     ok(hres == S_OK, "get_color failed: %08x\n", hres);
1041     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
1042     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
1043
1044     b = 0xfefe;
1045     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
1046     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
1047     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
1048
1049     b = 0xfefe;
1050     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
1051     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
1052     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
1053 }
1054
1055 static void test_default_selection(IHTMLDocument2 *doc)
1056 {
1057     IHTMLSelectionObject *selection;
1058     IHTMLTxtRange *range;
1059     IDispatch *disp;
1060     BSTR str;
1061     HRESULT hres;
1062
1063     hres = IHTMLDocument2_get_selection(doc, &selection);
1064     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
1065
1066     hres = IHTMLSelectionObject_get_type(selection, &str);
1067     ok(hres == S_OK, "get_type failed: %08x\n", hres);
1068     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
1069     SysFreeString(str);
1070
1071     hres = IHTMLSelectionObject_createRange(selection, &disp);
1072     IHTMLSelectionObject_Release(selection);
1073     ok(hres == S_OK, "createRange failed: %08x\n", hres);
1074
1075     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
1076     IDispatch_Release(disp);
1077     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
1078
1079     test_range_text(range, NULL);
1080     IHTMLTxtRange_Release(range);
1081 }
1082
1083 static void test_default_body(IHTMLBodyElement *body)
1084 {
1085     BSTR bstr;
1086     HRESULT hres;
1087
1088     bstr = (void*)0xdeadbeef;
1089     hres = IHTMLBodyElement_get_background(body, &bstr);
1090     ok(hres == S_OK, "get_background failed: %08x\n", hres);
1091     ok(bstr == NULL, "bstr != NULL\n");
1092 }
1093
1094 static void test_defaults(IHTMLDocument2 *doc)
1095 {
1096     IHTMLStyleSheetsCollection *stylesheetcol;
1097     IHTMLBodyElement *body;
1098     IHTMLElement *elem;
1099     IHTMLStyle *style;
1100     long l;
1101     HRESULT hres;
1102
1103     hres = IHTMLDocument2_get_body(doc, &elem);
1104     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1105
1106     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
1107     ok(hres == S_OK, "Could not get IHTMBodyElement: %08x\n", hres);
1108     test_default_body(body);
1109     IHTMLBodyElement_Release(body);
1110
1111     hres = IHTMLElement_get_style(elem, &style);
1112     IHTMLElement_Release(elem);
1113     ok(hres == S_OK, "get_style failed: %08x\n", hres);
1114
1115     test_default_style(style);
1116     test_compatmode(doc);
1117
1118     IHTMLStyle_Release(style);
1119
1120     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
1121     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
1122
1123     l = 0xdeadbeef;
1124     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
1125     ok(hres == S_OK, "get_length failed: %08x\n", hres);
1126     ok(l == 0, "length = %ld\n", l);
1127
1128     IHTMLStyleSheetsCollection_Release(stylesheetcol);
1129
1130     test_default_selection(doc);
1131 }
1132
1133 static void test_stylesheet(IDispatch *disp)
1134 {
1135     IHTMLStyleSheetRulesCollection *col = NULL;
1136     IHTMLStyleSheet *stylesheet;
1137     HRESULT hres;
1138
1139     hres = IDispatch_QueryInterface(disp, &IID_IHTMLStyleSheet, (void**)&stylesheet);
1140     ok(hres == S_OK, "Could not get IHTMLStyleSheet: %08x\n", hres);
1141
1142     hres = IHTMLStyleSheet_get_rules(stylesheet, &col);
1143     ok(hres == S_OK, "get_rules failed: %08x\n", hres);
1144     ok(col != NULL, "col == NULL\n");
1145
1146     IHTMLStyleSheetRulesCollection_Release(col);
1147     IHTMLStyleSheet_Release(stylesheet);
1148 }
1149
1150 static void test_stylesheets(IHTMLDocument2 *doc)
1151 {
1152     IHTMLStyleSheetsCollection *col = NULL;
1153     VARIANT idx, res;
1154     long len = 0;
1155     HRESULT hres;
1156
1157     hres = IHTMLDocument2_get_styleSheets(doc, &col);
1158     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
1159     ok(col != NULL, "col == NULL\n");
1160
1161     hres = IHTMLStyleSheetsCollection_get_length(col, &len);
1162     ok(hres == S_OK, "get_length failed: %08x\n", hres);
1163     ok(len == 1, "len=%ld\n", len);
1164
1165     VariantInit(&res);
1166     V_VT(&idx) = VT_I4;
1167     V_I4(&idx) = 0;
1168
1169     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
1170     ok(hres == S_OK, "item failed: %08x\n", hres);
1171     ok(V_VT(&res) == VT_DISPATCH, "V_VT(res) = %d\n", V_VT(&res));
1172     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
1173     test_stylesheet(V_DISPATCH(&res));
1174     VariantClear(&res);
1175
1176     V_VT(&res) = VT_I4;
1177     V_VT(&idx) = VT_I4;
1178     V_I4(&idx) = 1;
1179
1180     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
1181     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1182     ok(V_VT(&res) == VT_EMPTY, "V_VT(res) = %d\n", V_VT(&res));
1183     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
1184     VariantClear(&res);
1185
1186     IHTMLStyleSheetsCollection_Release(col);
1187 }
1188
1189 static void test_elems(IHTMLDocument2 *doc)
1190 {
1191     IHTMLElementCollection *col;
1192     IHTMLElement *elem;
1193     IDispatch *disp;
1194     HRESULT hres;
1195
1196     static const WCHAR xW[] = {'x',0};
1197     static const WCHAR sW[] = {'s',0};
1198     static const WCHAR scW[] = {'s','c',0};
1199     static const WCHAR xxxW[] = {'x','x','x',0};
1200
1201     static const elem_type_t all_types[] = {
1202         ET_HTML,
1203         ET_HEAD,
1204         ET_TITLE,
1205         ET_STYLE,
1206         ET_BODY,
1207         ET_A,
1208         ET_INPUT,
1209         ET_SELECT,
1210         ET_OPTION,
1211         ET_OPTION,
1212         ET_TEXTAREA,
1213         ET_TABLE,
1214         ET_TBODY,
1215         ET_SCRIPT
1216     };
1217
1218     static const elem_type_t item_types[] = {
1219         ET_A,
1220         ET_OPTION,
1221         ET_TEXTAREA
1222     };
1223
1224     hres = IHTMLDocument2_get_all(doc, &col);
1225     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1226     test_elem_collection(col, all_types, sizeof(all_types)/sizeof(all_types[0]));
1227     test_elem_col_item(col, xW, item_types, sizeof(item_types)/sizeof(item_types[0]));
1228     IHTMLElementCollection_Release(col);
1229
1230     elem = get_doc_elem(doc);
1231     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
1232     hres = IHTMLElement_get_all(elem, &disp);
1233     IHTMLElement_Release(elem);
1234     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1235
1236     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&col);
1237     IDispatch_Release(disp);
1238     ok(hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
1239     test_elem_collection(col, all_types+1, sizeof(all_types)/sizeof(all_types[0])-1);
1240     IHTMLElementCollection_Release(col);
1241
1242     get_elem_by_id(doc, xxxW, FALSE);
1243     elem = get_elem_by_id(doc, sW, TRUE);
1244     if(elem) {
1245         IHTMLSelectElement *select;
1246
1247         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLSelectElement, (void**)&select);
1248         ok(hres == S_OK, "Could not get IHTMLSelectElement interface: %08x\n", hres);
1249
1250         test_select_elem(select);
1251
1252         IHTMLSelectElement_Release(select);
1253         IHTMLElement_Release(elem);
1254     }
1255
1256     elem = get_elem_by_id(doc, scW, TRUE);
1257     if(elem) {
1258         IHTMLScriptElement *script;
1259         BSTR type;
1260
1261         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLScriptElement, (void**)&script);
1262         ok(hres == S_OK, "Could not get IHTMLScriptElement interface: %08x\n", hres);
1263
1264         hres = IHTMLScriptElement_get_type(script, &type);
1265         ok(hres == S_OK, "get_type failed: %08x\n", hres);
1266         ok(!lstrcmpW(type, text_javascriptW), "Unexpected type %s\n", dbgstr_w(type));
1267         SysFreeString(type);
1268     }
1269
1270     test_stylesheets(doc);
1271     test_create_option_elem(doc);
1272 }
1273
1274 static void test_exec(IUnknown *unk, const GUID *grpid, DWORD cmdid, VARIANT *in, VARIANT *out)
1275 {
1276     IOleCommandTarget *cmdtrg;
1277     HRESULT hres;
1278
1279     hres = IHTMLTxtRange_QueryInterface(unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
1280     ok(hres == S_OK, "Could not get IOleCommandTarget interface: %08x\n", hres);
1281
1282     hres = IOleCommandTarget_Exec(cmdtrg, grpid, cmdid, 0, in, out);
1283     ok(hres == S_OK, "Exec failed: %08x\n", hres);
1284
1285     IOleCommandTarget_Release(cmdtrg);
1286 }
1287
1288 static void test_indent(IHTMLDocument2 *doc)
1289 {
1290     IHTMLElementCollection *col;
1291     IHTMLTxtRange *range;
1292     HRESULT hres;
1293
1294     static const elem_type_t all_types[] = {
1295         ET_HTML,
1296         ET_HEAD,
1297         ET_TITLE,
1298         ET_BODY,
1299         ET_BR,
1300         ET_A,
1301     };
1302
1303     static const elem_type_t indent_types[] = {
1304         ET_HTML,
1305         ET_HEAD,
1306         ET_TITLE,
1307         ET_BODY,
1308         ET_BLOCKQUOTE,
1309         ET_P,
1310         ET_BR,
1311         ET_A,
1312     };
1313
1314     hres = IHTMLDocument2_get_all(doc, &col);
1315     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1316     test_elem_collection(col, all_types, sizeof(all_types)/sizeof(all_types[0]));
1317     IHTMLElementCollection_Release(col);
1318
1319     range = test_create_body_range(doc);
1320     test_exec((IUnknown*)range, &CGID_MSHTML, IDM_INDENT, NULL, NULL);
1321     IHTMLTxtRange_Release(range);
1322
1323     hres = IHTMLDocument2_get_all(doc, &col);
1324     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1325     test_elem_collection(col, indent_types, sizeof(indent_types)/sizeof(indent_types[0]));
1326     IHTMLElementCollection_Release(col);
1327 }
1328
1329 static IHTMLDocument2 *notif_doc;
1330 static BOOL doc_complete;
1331
1332 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
1333         REFIID riid, void**ppv)
1334 {
1335     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
1336         *ppv = iface;
1337         return S_OK;
1338     }
1339
1340     ok(0, "unexpected call\n");
1341     return E_NOINTERFACE;
1342 }
1343
1344 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
1345 {
1346     return 2;
1347 }
1348
1349 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
1350 {
1351     return 1;
1352 }
1353
1354 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
1355 {
1356     if(dispID == DISPID_READYSTATE){
1357         BSTR state;
1358         HRESULT hres;
1359
1360         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
1361
1362         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
1363         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
1364
1365         if(!lstrcmpW(state, completeW))
1366             doc_complete = TRUE;
1367
1368         SysFreeString(state);        
1369     }
1370
1371     return S_OK;
1372 }
1373
1374 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
1375 {
1376     ok(0, "unexpected call\n");
1377     return E_NOTIMPL;
1378 }
1379
1380 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
1381     PropertyNotifySink_QueryInterface,
1382     PropertyNotifySink_AddRef,
1383     PropertyNotifySink_Release,
1384     PropertyNotifySink_OnChanged,
1385     PropertyNotifySink_OnRequestEdit
1386 };
1387
1388 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
1389
1390 static IHTMLDocument2 *create_doc_with_string(const char *str)
1391 {
1392     IPersistStreamInit *init;
1393     IStream *stream;
1394     IHTMLDocument2 *doc;
1395     HGLOBAL mem;
1396     SIZE_T len;
1397
1398     notif_doc = doc = create_document();
1399     if(!doc)
1400         return NULL;
1401
1402     doc_complete = FALSE;
1403     len = strlen(str);
1404     mem = GlobalAlloc(0, len);
1405     memcpy(mem, str, len);
1406     CreateStreamOnHGlobal(mem, TRUE, &stream);
1407
1408     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
1409
1410     IPersistStreamInit_Load(init, stream);
1411     IPersistStreamInit_Release(init);
1412     IStream_Release(stream);
1413
1414     return doc;
1415 }
1416
1417 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
1418 {
1419     IConnectionPointContainer *container;
1420     IConnectionPoint *cp;
1421     DWORD cookie;
1422     HRESULT hres;
1423
1424     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
1425     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
1426
1427     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
1428     IConnectionPointContainer_Release(container);
1429     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
1430
1431     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
1432     IConnectionPoint_Release(cp);
1433     ok(hres == S_OK, "Advise failed: %08x\n", hres);
1434 }
1435
1436 typedef void (*domtest_t)(IHTMLDocument2*);
1437
1438 static void run_domtest(const char *str, domtest_t test)
1439 {
1440     IHTMLDocument2 *doc;
1441     IHTMLElement *body = NULL;
1442     ULONG ref;
1443     MSG msg;
1444     HRESULT hres;
1445
1446     doc = create_doc_with_string(str);
1447     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
1448
1449     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
1450         TranslateMessage(&msg);
1451         DispatchMessage(&msg);
1452     }
1453
1454     hres = IHTMLDocument2_get_body(doc, &body);
1455     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1456
1457     if(body) {
1458         IHTMLElement_Release(body);
1459         test(doc);
1460     }else {
1461         skip("Could not get document body. Assuming no Gecko installed.\n");
1462     }
1463
1464     ref = IHTMLDocument2_Release(doc);
1465     ok(!ref, "ref = %d\n", ref);
1466 }
1467
1468 static void gecko_installer_workaround(BOOL disable)
1469 {
1470     HKEY hkey;
1471     DWORD res;
1472
1473     static BOOL has_url = FALSE;
1474     static char url[2048];
1475
1476     if(!disable && !has_url)
1477         return;
1478
1479     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
1480     if(res != ERROR_SUCCESS)
1481         return;
1482
1483     if(disable) {
1484         DWORD type, size = sizeof(url);
1485
1486         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
1487         if(res == ERROR_SUCCESS && type == REG_SZ)
1488             has_url = TRUE;
1489
1490         RegDeleteValue(hkey, "GeckoUrl");
1491     }else {
1492         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
1493     }
1494
1495     RegCloseKey(hkey);
1496 }
1497
1498 START_TEST(dom)
1499 {
1500     gecko_installer_workaround(TRUE);
1501     CoInitialize(NULL);
1502
1503     run_domtest(doc_str1, test_doc_elem);
1504     run_domtest(range_test_str, test_txtrange);
1505     run_domtest(range_test2_str, test_txtrange2);
1506     run_domtest(elem_test_str, test_elems);
1507     run_domtest(doc_blank, test_defaults);
1508     run_domtest(indent_test_str, test_indent);
1509
1510     CoUninitialize();
1511     gecko_installer_workaround(FALSE);
1512 }