kernel32: FindExSearchLimitToDirectories has no effect on FindFirstFileEx.
[wine] / dlls / mshtml / txtrange.c
1 /*
2  * Copyright 2006-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 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 static const WCHAR brW[] = {'b','r',0};
40
41 typedef struct {
42     const IHTMLTxtRangeVtbl *lpHTMLTxtRangeVtbl;
43
44     LONG ref;
45
46     nsIDOMRange *nsrange;
47     HTMLDocument *doc;
48
49     struct list entry;
50 } HTMLTxtRange;
51
52 #define HTMLTXTRANGE(x)  ((IHTMLTxtRange*)  &(x)->lpHTMLTxtRangeVtbl)
53
54 typedef struct {
55     WCHAR *buf;
56     DWORD len;
57     DWORD size;
58 } wstrbuf_t;
59
60 typedef struct {
61     PRUint16 type;
62     nsIDOMNode *node;
63     PRUint32 off;
64     nsAString str;
65     const PRUnichar *p;
66 } dompos_t;
67
68 typedef enum {
69     RU_UNKNOWN,
70     RU_CHAR,
71     RU_WORD,
72     RU_SENTENCE,
73     RU_TEXTEDIT
74 } range_unit_t;
75
76 static HTMLTxtRange *get_range_object(HTMLDocument *doc, IHTMLTxtRange *iface)
77 {
78     HTMLTxtRange *iter;
79
80     LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
81         if(HTMLTXTRANGE(iter) == iface)
82             return iter;
83     }
84
85     ERR("Could not find range in document\n");
86     return NULL;
87 }
88
89 static range_unit_t string_to_unit(LPCWSTR str)
90 {
91     static const WCHAR characterW[] =
92         {'c','h','a','r','a','c','t','e','r',0};
93     static const WCHAR wordW[] =
94         {'w','o','r','d',0};
95     static const WCHAR sentenceW[] =
96         {'s','e','n','t','e','n','c','e',0};
97     static const WCHAR texteditW[] =
98         {'t','e','x','t','e','d','i','t',0};
99
100     if(!strcmpiW(str, characterW))  return RU_CHAR;
101     if(!strcmpiW(str, wordW))       return RU_WORD;
102     if(!strcmpiW(str, sentenceW))   return RU_SENTENCE;
103     if(!strcmpiW(str, texteditW))   return RU_TEXTEDIT;
104
105     return RU_UNKNOWN;
106 }
107
108 static int string_to_nscmptype(LPCWSTR str)
109 {
110     static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
111     static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
112     static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
113     static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
114
115     if(!strcmpiW(str, seW))  return NS_START_TO_END;
116     if(!strcmpiW(str, ssW))  return NS_START_TO_START;
117     if(!strcmpiW(str, esW))  return NS_END_TO_START;
118     if(!strcmpiW(str, eeW))  return NS_END_TO_END;
119
120     return -1;
121 }
122
123 static PRUint16 get_node_type(nsIDOMNode *node)
124 {
125     PRUint16 type = 0xfff;
126
127     if(node)
128         nsIDOMNode_GetNodeType(node, &type);
129
130     return type;
131 }
132
133 static BOOL is_br_node(nsIDOMNode *node)
134 {
135     nsIDOMElement *elem;
136     nsAString tag_str;
137     const PRUnichar *tag;
138     BOOL ret = FALSE;
139     nsresult nsres;
140
141     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
142     if(NS_FAILED(nsres))
143         return FALSE;
144
145     nsAString_Init(&tag_str, NULL);
146     nsIDOMElement_GetTagName(elem, &tag_str);
147     nsIDOMElement_Release(elem);
148     nsAString_GetData(&tag_str, &tag, 0);
149
150     if(!strcmpiW(tag, brW))
151         ret = TRUE;
152
153     nsAString_Finish(&tag_str);
154
155     return ret;
156 }
157
158 static inline void wstrbuf_init(wstrbuf_t *buf)
159 {
160     buf->len = 0;
161     buf->size = 16;
162     buf->buf = mshtml_alloc(buf->size * sizeof(WCHAR));
163     *buf->buf = 0;
164 }
165
166 static inline void wstrbuf_finish(wstrbuf_t *buf)
167 {
168     mshtml_free(buf->buf);
169 }
170
171 static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
172 {
173     if(buf->len+len >= buf->size) {
174         buf->size = 2*buf->len+len;
175         buf->buf = mshtml_realloc(buf->buf, buf->size * sizeof(WCHAR));
176     }
177
178     memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
179     buf->len += len;
180     buf->buf[buf->len] = 0;
181 }
182
183 static inline void wstrbuf_append(wstrbuf_t *buf, LPCWSTR str)
184 {
185     wstrbuf_append_len(buf, str, strlenW(str));
186 }
187
188 static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node)
189 {
190
191     switch(get_node_type(node)) {
192     case TEXT_NODE: {
193         nsIDOMText *nstext;
194         nsAString data_str;
195         const PRUnichar *data;
196
197         nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
198
199         nsAString_Init(&data_str, NULL);
200         nsIDOMText_GetData(nstext, &data_str);
201         nsAString_GetData(&data_str, &data, NULL);
202         wstrbuf_append(buf, data);
203         nsAString_Finish(&data_str);
204
205        nsIDOMText_Release(nstext);
206
207         break;
208     }
209     case ELEMENT_NODE:
210         if(is_br_node(node)) {
211             static const WCHAR endlW[] = {'\r','\n'};
212             wstrbuf_append_len(buf, endlW, 2);
213         }
214     }
215 }
216
217 static BOOL fill_nodestr(dompos_t *pos)
218 {
219     nsIDOMText *text;
220     nsresult nsres;
221
222     if(pos->type != TEXT_NODE)
223         return FALSE;
224
225     nsres = nsIDOMNode_QueryInterface(pos->node, &IID_nsIDOMText, (void**)&text);
226     if(NS_FAILED(nsres))
227         return FALSE;
228
229     nsAString_Init(&pos->str, NULL);
230     nsIDOMText_GetData(text, &pos->str);
231     nsIDOMText_Release(text);
232     nsAString_GetData(&pos->str, &pos->p, NULL);
233
234     return TRUE;
235 }
236
237 static nsIDOMNode *next_node(nsIDOMNode *iter)
238 {
239     nsIDOMNode *ret, *tmp;
240     nsresult nsres;
241
242     if(!iter)
243         return NULL;
244
245     nsres = nsIDOMNode_GetFirstChild(iter, &ret);
246     if(NS_SUCCEEDED(nsres) && ret)
247         return ret;
248
249     nsIDOMNode_AddRef(iter);
250
251     do {
252         nsres = nsIDOMNode_GetNextSibling(iter, &ret);
253         if(NS_SUCCEEDED(nsres) && ret) {
254             nsIDOMNode_Release(iter);
255             return ret;
256         }
257
258         nsres = nsIDOMNode_GetParentNode(iter, &tmp);
259         nsIDOMNode_Release(iter);
260         iter = tmp;
261     }while(NS_SUCCEEDED(nsres) && iter);
262
263     return NULL;
264 }
265
266 static nsIDOMNode *prev_node(HTMLTxtRange *This, nsIDOMNode *iter)
267 {
268     nsIDOMNode *ret, *tmp;
269     nsresult nsres;
270
271     if(!iter) {
272         nsIDOMHTMLDocument *nshtmldoc;
273         nsIDOMHTMLElement *nselem;
274         nsIDOMDocument *nsdoc;
275
276         nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
277         nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
278         nsIDOMDocument_Release(nsdoc);
279         nsIDOMHTMLDocument_GetBody(nshtmldoc, &nselem);
280         nsIDOMHTMLDocument_Release(nshtmldoc);
281
282         nsIDOMElement_GetLastChild(nselem, &tmp);
283         if(!tmp)
284             return (nsIDOMNode*)nselem;
285
286         while(tmp) {
287             ret = tmp;
288             nsIDOMNode_GetLastChild(ret, &tmp);
289         }
290
291         nsIDOMElement_Release(nselem);
292
293         return ret;
294     }
295
296     nsres = nsIDOMNode_GetLastChild(iter, &ret);
297     if(NS_SUCCEEDED(nsres) && ret)
298         return ret;
299
300     nsIDOMNode_AddRef(iter);
301
302     do {
303         nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
304         if(NS_SUCCEEDED(nsres) && ret) {
305             nsIDOMNode_Release(iter);
306             return ret;
307         }
308
309         nsres = nsIDOMNode_GetParentNode(iter, &tmp);
310         nsIDOMNode_Release(iter);
311         iter = tmp;
312     }while(NS_SUCCEEDED(nsres) && iter);
313
314     return NULL;
315 }
316
317 static nsIDOMNode *get_child_node(nsIDOMNode *node, PRUint32 off)
318 {
319     nsIDOMNodeList *node_list;
320     nsIDOMNode *ret = NULL;
321
322     nsIDOMNode_GetChildNodes(node, &node_list);
323     nsIDOMNodeList_Item(node_list, off, &ret);
324     nsIDOMNodeList_Release(node_list);
325
326     return ret;
327 }
328
329 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
330 {
331     nsIDOMNode *node;
332     PRInt32 off;
333
334     pos->p = NULL;
335
336     if(!start) {
337         PRBool collapsed;
338         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
339         start = collapsed;
340     }
341
342     if(start) {
343         nsIDOMRange_GetStartContainer(This->nsrange, &node);
344         nsIDOMRange_GetStartOffset(This->nsrange, &off);
345     }else {
346         nsIDOMRange_GetEndContainer(This->nsrange, &node);
347         nsIDOMRange_GetEndOffset(This->nsrange, &off);
348     }
349
350     pos->type = get_node_type(node);
351     if(pos->type == ELEMENT_NODE) {
352         if(start) {
353             pos->node = get_child_node(node, off);
354             pos->off = 0;
355         }else {
356             pos->node = off ? get_child_node(node, off-1) : prev_node(This, node);
357             pos->off = -1;
358         }
359
360         pos->type = get_node_type(pos->node);
361         nsIDOMNode_Release(node);
362     }else if(start) {
363         pos->node = node;
364         pos->off = off;
365     }else if(off) {
366         pos->node = node;
367         pos->off = off-1;
368     }else {
369         pos->node = prev_node(This, node);
370         pos->off = -1;
371         nsIDOMNode_Release(node);
372     }
373
374     if(pos->type == TEXT_NODE)
375         fill_nodestr(pos);
376 }
377
378 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
379 {
380     nsresult nsres;
381
382     if(start) {
383         if(pos->type == TEXT_NODE)
384             nsres = nsIDOMRange_SetStart(This->nsrange, pos->node, pos->off);
385         else
386             nsres = nsIDOMRange_SetStartBefore(This->nsrange, pos->node);
387     }else {
388         if(pos->type == TEXT_NODE)
389             nsres = nsIDOMRange_SetEnd(This->nsrange, pos->node, pos->off+1);
390         else
391             nsres = nsIDOMRange_SetEndAfter(This->nsrange, pos->node);
392     }
393
394     if(NS_FAILED(nsres))
395         ERR("failed: %p %08x\n", pos->node, nsres);
396 }
397
398 static inline void dompos_release(dompos_t *pos)
399 {
400     if(pos->node)
401         nsIDOMNode_Release(pos->node);
402
403     if(pos->p)
404         nsAString_Finish(&pos->str);
405 }
406
407 static inline void dompos_addref(dompos_t *pos)
408 {
409     if(pos->node)
410         nsIDOMNode_AddRef(pos->node);
411
412     if(pos->type == TEXT_NODE)
413         fill_nodestr(pos);
414 }
415
416 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
417 {
418     return pos1->node == pos2->node && pos1->off == pos2->off;
419 }
420
421 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
422 {
423     nsIDOMNode *iter, *tmp;
424     dompos_t start_pos, end_pos;
425     PRBool collapsed;
426
427     nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
428     if(collapsed) {
429         wstrbuf_finish(buf);
430         buf->buf = NULL;
431         buf->size = 0;
432         return;
433     }
434
435     get_cur_pos(This, FALSE, &end_pos);
436     get_cur_pos(This, TRUE, &start_pos);
437
438     if(start_pos.type == TEXT_NODE) {
439         if(start_pos.node == end_pos.node) {
440             wstrbuf_append_len(buf, start_pos.p+start_pos.off, end_pos.off-start_pos.off+1);
441             iter = start_pos.node;
442             nsIDOMNode_AddRef(iter);
443         }else {
444             wstrbuf_append(buf, start_pos.p+start_pos.off);
445             iter = next_node(start_pos.node);
446         }
447     }else {
448         iter = start_pos.node;
449         nsIDOMNode_AddRef(iter);
450     }
451
452     while(iter != end_pos.node) {
453         wstrbuf_append_node(buf, iter);
454         tmp = next_node(iter);
455         nsIDOMNode_Release(iter);
456         iter = tmp;
457     }
458
459     nsIDOMNode_AddRef(end_pos.node);
460
461     if(start_pos.node != end_pos.node && !is_br_node(end_pos.node))
462         wstrbuf_append_len(buf, end_pos.p, end_pos.off+1);
463
464     nsIDOMNode_Release(iter);
465     dompos_release(&start_pos);
466     dompos_release(&end_pos);
467 }
468
469 static WCHAR get_pos_char(const dompos_t *pos)
470 {
471     switch(pos->type) {
472     case TEXT_NODE:
473         return pos->p[pos->off];
474     case ELEMENT_NODE:
475         if(is_br_node(pos->node))
476             return '\n';
477     }
478
479     return 0;
480 }
481
482 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
483 {
484     nsIDOMNode *iter, *tmp;
485
486     if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off+1]) {
487         *new_pos = *pos;
488         new_pos->off++;
489         dompos_addref(new_pos);
490         return new_pos->p[new_pos->off];
491     }
492
493     iter = next_node(pos->node);
494     if(!iter)
495         return 0;
496
497     while(1) {
498         switch(get_node_type(iter)) {
499         case TEXT_NODE:
500             new_pos->node = iter;
501             new_pos->type = TEXT_NODE;
502             new_pos->off = 0;
503             fill_nodestr(new_pos);
504             return *new_pos->p;
505
506         case ELEMENT_NODE:
507             if(!is_br_node(iter))
508                 break;
509
510             new_pos->node = iter;
511             new_pos->type = ELEMENT_NODE;
512             new_pos->off = 0;
513             new_pos->p = NULL;
514             return '\n';
515         }
516
517         tmp = iter;
518         iter = next_node(iter);
519         nsIDOMNode_Release(tmp);
520
521         if(!iter)
522             break;
523     }
524
525     return 0;
526 }
527
528 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
529 {
530     nsIDOMNode *iter, *tmp;
531
532     if(pos->type == TEXT_NODE && pos->off > 0) {
533         *new_pos = *pos;
534         new_pos->off--;
535         dompos_addref(new_pos);
536         return new_pos->p[new_pos->off];
537     }
538
539     iter = prev_node(This, pos->node);
540     if(!iter)
541         return 0;
542
543     while(1) {
544         switch(get_node_type(iter)) {
545         case TEXT_NODE:
546             new_pos->node = iter;
547             new_pos->type = TEXT_NODE;
548             fill_nodestr(new_pos);
549             new_pos->off = strlenW(new_pos->p)-1;
550             return new_pos->p[new_pos->off];
551
552         case ELEMENT_NODE:
553             if(!is_br_node(iter))
554                 break;
555
556             new_pos->node = iter;
557             new_pos->type = ELEMENT_NODE;
558             new_pos->off = 0;
559             new_pos->p = NULL;
560             return '\n';
561         }
562
563         tmp = iter;
564         iter = prev_node(This, iter);
565         nsIDOMNode_Release(tmp);
566
567         if(!iter)
568             break;
569     }
570
571     *new_pos = *pos;
572     dompos_addref(new_pos);
573     return 0;
574 }
575
576 static long move_next_chars(long cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
577         BOOL *bounded, dompos_t *new_pos)
578 {
579     dompos_t iter, tmp;
580     long ret = 0;
581     WCHAR c;
582
583     if(bounded)
584         *bounded = FALSE;
585
586     if(col)
587         ret++;
588
589     if(ret >= cnt) {
590         *new_pos = *pos;
591         dompos_addref(new_pos);
592         return ret;
593     }
594
595     c = next_char(pos, &iter);
596     ret++;
597
598     while(ret < cnt) {
599         tmp = iter;
600         c = next_char(&tmp, &iter);
601         if(!c) {
602             iter = tmp;
603             break;
604         }
605         ret++;
606         dompos_release(&tmp);
607         if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
608             *bounded = TRUE;
609             ret++;
610         }
611     }
612
613     *new_pos = iter;
614     return ret;
615 }
616
617 static long move_prev_chars(HTMLTxtRange *This, long cnt, const dompos_t *pos, BOOL end,
618         const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
619 {
620     dompos_t iter, tmp;
621     long ret = 0;
622     WCHAR c;
623
624     if(bounded)
625         *bounded = FALSE;
626
627     c = prev_char(This, pos, &iter);
628     if(c)
629         ret++;
630
631     while(c && ret < cnt) {
632         tmp = iter;
633         c = prev_char(This, &tmp, &iter);
634         if(!c) {
635             iter = tmp;
636             if(end)
637                 ret++;
638             break;
639         }
640
641         ret++;
642         dompos_release(&tmp);
643
644         if(bound_pos && dompos_cmp(&iter, bound_pos))
645             *bounded = TRUE;
646     }
647
648     *new_pos = iter;
649     return ret;
650 }
651
652 static BOOL find_next_space(const dompos_t *pos, BOOL first_space, dompos_t *ret)
653 {
654     dompos_t iter, tmp;
655     WCHAR c;
656
657     if(first_space) {
658         c = get_pos_char(pos);
659         if(c && isspaceW(c)) {
660             *ret = *pos;
661             dompos_addref(ret);
662             return FALSE;
663         }
664     }
665
666     c = next_char(pos, &iter);
667     if(!c) {
668         *ret = *pos;
669         dompos_addref(ret);
670         return FALSE;
671     }
672
673     while(!isspaceW(c)) {
674         tmp = iter;
675         c = next_char(&tmp, &iter);
676         if(!c) {
677             iter = tmp;
678             break;
679         }
680         dompos_release(&tmp);
681     }
682
683     *ret = iter;
684     return TRUE;
685 }
686
687 static long find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
688 {
689     dompos_t iter, tmp;
690     WCHAR c;
691
692     c = prev_char(This, pos, &iter);
693     if(!c || (first_space && isspaceW(c))) {
694         *ret = *pos;
695         dompos_addref(ret);
696         return FALSE;
697     }
698
699     while(1) {
700         tmp = iter;
701         c = prev_char(This, &tmp, &iter);
702         if(!c || isspaceW(c)) {
703             dompos_release(&iter);
704             break;
705         }
706         dompos_release(&tmp);
707     }
708
709     *ret = tmp;
710     return TRUE;
711 }
712
713 static long move_next_words(long cnt, const dompos_t *pos, dompos_t *new_pos)
714 {
715     dompos_t iter, tmp;
716     long ret = 0;
717
718     iter = *pos;
719     dompos_addref(&iter);
720
721     while(ret < cnt) {
722         if(!find_next_space(&iter, FALSE, &tmp))
723             break;
724
725         ret++;
726         dompos_release(&iter);
727         iter = tmp;
728     }
729
730     *new_pos = iter;
731     return ret;
732 }
733
734 static long move_prev_words(HTMLTxtRange *This, long cnt, const dompos_t *pos, dompos_t *new_pos)
735 {
736     dompos_t iter, tmp;
737     long ret = 0;
738
739     iter = *pos;
740     dompos_addref(&iter);
741
742     while(ret < cnt) {
743         if(!find_prev_space(This, &iter, FALSE, &tmp))
744             break;
745
746         dompos_release(&iter);
747         iter = tmp;
748         ret++;
749     }
750
751     *new_pos = iter;
752     return ret;
753 }
754
755 #define HTMLTXTRANGE_THIS(iface) DEFINE_THIS(HTMLTxtRange, HTMLTxtRange, iface)
756
757 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
758 {
759     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
760
761     *ppv = NULL;
762
763     if(IsEqualGUID(&IID_IUnknown, riid)) {
764         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
765         *ppv = HTMLTXTRANGE(This);
766     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
767         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
768         *ppv = HTMLTXTRANGE(This);
769     }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
770         TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
771         *ppv = HTMLTXTRANGE(This);
772     }
773
774     if(*ppv) {
775         IUnknown_AddRef((IUnknown*)*ppv);
776         return S_OK;
777     }
778
779     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
780     return E_NOINTERFACE;
781 }
782
783 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
784 {
785     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
786     LONG ref = InterlockedIncrement(&This->ref);
787
788     TRACE("(%p) ref=%d\n", This, ref);
789
790     return ref;
791 }
792
793 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
794 {
795     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
796     LONG ref = InterlockedDecrement(&This->ref);
797
798     TRACE("(%p) ref=%d\n", This, ref);
799
800     if(!ref) {
801         if(This->nsrange)
802             nsISelection_Release(This->nsrange);
803         if(This->doc)
804             list_remove(&This->entry);
805         mshtml_free(This);
806     }
807
808     return ref;
809 }
810
811 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
812 {
813     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
814     FIXME("(%p)->(%p)\n", This, pctinfo);
815     return E_NOTIMPL;
816 }
817
818 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
819                                                LCID lcid, ITypeInfo **ppTInfo)
820 {
821     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
822     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
823     return E_NOTIMPL;
824 }
825
826 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
827                                                  LPOLESTR *rgszNames, UINT cNames,
828                                                  LCID lcid, DISPID *rgDispId)
829 {
830     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
831     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
832           lcid, rgDispId);
833     return E_NOTIMPL;
834 }
835
836 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
837                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
838                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
839 {
840     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
841     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
842           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
843     return E_NOTIMPL;
844 }
845
846 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
847 {
848     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
849
850     TRACE("(%p)->(%p)\n", This, p);
851
852     *p = NULL;
853
854     if(This->nsrange) {
855         nsIDOMDocumentFragment *fragment;
856         nsresult nsres;
857
858         nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
859         if(NS_SUCCEEDED(nsres)) {
860             const PRUnichar *nstext;
861             nsAString nsstr;
862
863             nsAString_Init(&nsstr, NULL);
864             nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
865             nsIDOMDocumentFragment_Release(fragment);
866
867             nsAString_GetData(&nsstr, &nstext, NULL);
868             *p = SysAllocString(nstext);
869
870             nsAString_Finish(&nsstr);
871         }
872     }
873
874     if(!*p) {
875         const WCHAR emptyW[] = {0};
876         *p = SysAllocString(emptyW);
877     }
878
879     TRACE("return %s\n", debugstr_w(*p));
880     return S_OK;
881 }
882
883 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
884 {
885     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
886     nsIDOMDocument *nsdoc;
887     nsIDOMText *text_node;
888     nsAString text_str;
889     nsresult nsres;
890
891     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
892
893     if(!This->doc)
894         return MSHTML_E_NODOC;
895
896     nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
897     if(NS_FAILED(nsres)) {
898         ERR("GetDocument failed: %08x\n", nsres);
899         return S_OK;
900     }
901
902     nsAString_Init(&text_str, v);
903     nsres = nsIDOMDocument_CreateTextNode(nsdoc, &text_str, &text_node);
904     nsIDOMDocument_Release(nsdoc);
905     nsAString_Finish(&text_str);
906     if(NS_FAILED(nsres)) {
907         ERR("CreateTextNode failed: %08x\n", nsres);
908         return S_OK;
909     }
910     nsres = nsIDOMRange_DeleteContents(This->nsrange);
911     if(NS_FAILED(nsres))
912         ERR("DeleteContents failed: %08x\n", nsres);
913
914     nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
915     if(NS_FAILED(nsres))
916         ERR("InsertNode failed: %08x\n", nsres);
917
918     nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
919     if(NS_FAILED(nsres))
920         ERR("SetEndAfter failed: %08x\n", nsres);
921
922     return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), VARIANT_FALSE);
923 }
924
925 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
926 {
927     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
928     wstrbuf_t buf;
929
930     TRACE("(%p)->(%p)\n", This, p);
931
932     *p = NULL;
933     if(!This->nsrange)
934         return S_OK;
935
936     wstrbuf_init(&buf);
937     range_to_string(This, &buf);
938     if(buf.buf)
939         *p = SysAllocString(buf.buf);
940     wstrbuf_finish(&buf);
941
942     TRACE("ret %s\n", debugstr_w(*p));
943     return S_OK;
944 }
945
946 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
947 {
948     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
949     nsIDOMNode *nsnode, *tmp;
950     HTMLDOMNode *node;
951
952     TRACE("(%p)->(%p)\n", This, parent);
953
954     nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
955     while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
956         nsIDOMNode_GetParentNode(nsnode, &tmp);
957         nsIDOMNode_Release(nsnode);
958         nsnode = tmp;
959     }
960
961     if(!nsnode) {
962         *parent = NULL;
963         return S_OK;
964     }
965
966     node = get_node(This->doc, nsnode);
967     nsIDOMNode_Release(nsnode);
968
969     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)parent);
970 }
971
972 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
973 {
974     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
975     nsIDOMRange *nsrange = NULL;
976
977     TRACE("(%p)->(%p)\n", This, Duplicate);
978
979     nsIDOMRange_CloneRange(This->nsrange, &nsrange);
980     *Duplicate = HTMLTxtRange_Create(This->doc, nsrange);
981     nsIDOMRange_Release(nsrange);
982
983     return S_OK;
984 }
985
986 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
987         VARIANT_BOOL *InRange)
988 {
989     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
990     HTMLTxtRange *src_range;
991     PRInt16 nsret = 0;
992     nsresult nsres;
993
994     TRACE("(%p)->(%p %p)\n", This, Range, InRange);
995
996     *InRange = VARIANT_FALSE;
997
998     src_range = get_range_object(This->doc, Range);
999     if(!src_range)
1000         return E_FAIL;
1001
1002     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1003             src_range->nsrange, &nsret);
1004     if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1005         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1006                 src_range->nsrange, &nsret);
1007         if(NS_SUCCEEDED(nsres) && nsret >= 0)
1008             *InRange = VARIANT_TRUE;
1009     }
1010
1011     if(NS_FAILED(nsres))
1012         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1013
1014     return S_OK;
1015 }
1016
1017 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1018         VARIANT_BOOL *IsEqual)
1019 {
1020     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1021     HTMLTxtRange *src_range;
1022     PRInt16 nsret = 0;
1023     nsresult nsres;
1024
1025     TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1026
1027     *IsEqual = VARIANT_FALSE;
1028
1029     src_range = get_range_object(This->doc, Range);
1030     if(!src_range)
1031         return E_FAIL;
1032
1033     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1034             src_range->nsrange, &nsret);
1035     if(NS_SUCCEEDED(nsres) && !nsret) {
1036         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1037                 src_range->nsrange, &nsret);
1038         if(NS_SUCCEEDED(nsres) && !nsret)
1039             *IsEqual = VARIANT_TRUE;
1040     }
1041
1042     if(NS_FAILED(nsres))
1043         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1044
1045     return S_OK;
1046 }
1047
1048 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1049 {
1050     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1051     FIXME("(%p)->(%x)\n", This, fStart);
1052     return E_NOTIMPL;
1053 }
1054
1055 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1056 {
1057     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1058
1059     TRACE("(%p)->(%x)\n", This, Start);
1060
1061     nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1062     return S_OK;
1063 }
1064
1065 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1066 {
1067     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1068     range_unit_t unit;
1069
1070     TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1071
1072     unit = string_to_unit(Unit);
1073     if(unit == RU_UNKNOWN)
1074         return E_INVALIDARG;
1075
1076     switch(unit) {
1077     case RU_WORD: {
1078         dompos_t end_pos, start_pos, new_pos;
1079
1080         *Success = VARIANT_FALSE;
1081
1082         get_cur_pos(This, TRUE, &start_pos);
1083         get_cur_pos(This, FALSE, &end_pos);
1084         if(find_next_space(&end_pos, TRUE, &new_pos)) {
1085             set_range_pos(This, FALSE, &new_pos);
1086             *Success = VARIANT_TRUE;
1087         }
1088         dompos_release(&new_pos);
1089
1090         if(find_prev_space(This, &start_pos, TRUE, &new_pos)) {
1091             set_range_pos(This, TRUE, &new_pos);
1092             *Success = VARIANT_TRUE;
1093         }
1094
1095         dompos_release(&new_pos);
1096         dompos_release(&end_pos);
1097
1098         break;
1099     }
1100     default:
1101         FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1102     }
1103
1104     return S_OK;
1105 }
1106
1107 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1108         long Count, long *ActualCount)
1109 {
1110     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1111     range_unit_t unit;
1112
1113     TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1114
1115     unit = string_to_unit(Unit);
1116     if(unit == RU_UNKNOWN)
1117         return E_INVALIDARG;
1118
1119     IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1120
1121     if(!Count) {
1122         *ActualCount = 0;
1123         return S_OK;
1124     }
1125
1126     switch(unit) {
1127     case RU_CHAR: {
1128         dompos_t cur_pos, new_pos;
1129
1130         get_cur_pos(This, TRUE, &cur_pos);
1131
1132         if(Count > 0) {
1133             *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1134             set_range_pos(This, FALSE, &new_pos);
1135             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1136             dompos_release(&new_pos);
1137         }else {
1138             *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1139             set_range_pos(This, TRUE, &new_pos);
1140             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1141             dompos_release(&new_pos);
1142         }
1143
1144         dompos_release(&cur_pos);
1145         break;
1146     }
1147
1148     case RU_WORD: {
1149         dompos_t cur_pos, new_pos;
1150
1151         get_cur_pos(This, TRUE, &cur_pos);
1152
1153         if(Count > 0) {
1154             *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1155             set_range_pos(This, FALSE, &new_pos);
1156             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1157             dompos_release(&new_pos);
1158         }else {
1159             *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1160             set_range_pos(This, TRUE, &new_pos);
1161             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1162             dompos_release(&new_pos);
1163         }
1164
1165         dompos_release(&cur_pos);
1166         break;
1167     }
1168
1169     default:
1170         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1171     }
1172
1173     TRACE("ret %ld\n", *ActualCount);
1174     return S_OK;
1175 }
1176
1177 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1178         long Count, long *ActualCount)
1179 {
1180     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1181     range_unit_t unit;
1182
1183     TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1184
1185     unit = string_to_unit(Unit);
1186     if(unit == RU_UNKNOWN)
1187         return E_INVALIDARG;
1188
1189     if(!Count) {
1190         *ActualCount = 0;
1191         return S_OK;
1192     }
1193
1194     switch(unit) {
1195     case RU_CHAR: {
1196         dompos_t start_pos, end_pos, new_pos;
1197         PRBool collapsed;
1198
1199         get_cur_pos(This, TRUE, &start_pos);
1200         get_cur_pos(This, FALSE, &end_pos);
1201         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1202
1203         if(Count > 0) {
1204             BOOL bounded;
1205
1206             *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1207             set_range_pos(This, !bounded, &new_pos);
1208             if(bounded)
1209                 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1210         }else {
1211             *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1212             set_range_pos(This, TRUE, &new_pos);
1213         }
1214
1215         dompos_release(&start_pos);
1216         dompos_release(&end_pos);
1217         dompos_release(&new_pos);
1218         break;
1219     }
1220
1221     default:
1222         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1223     }
1224
1225     return S_OK;
1226 }
1227
1228 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1229         long Count, long *ActualCount)
1230 {
1231     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1232     range_unit_t unit;
1233
1234     TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1235
1236     unit = string_to_unit(Unit);
1237     if(unit == RU_UNKNOWN)
1238         return E_INVALIDARG;
1239
1240     if(!Count) {
1241         *ActualCount = 0;
1242         return S_OK;
1243     }
1244
1245     switch(unit) {
1246     case RU_CHAR: {
1247         dompos_t start_pos, end_pos, new_pos;
1248         PRBool collapsed;
1249
1250         get_cur_pos(This, TRUE, &start_pos);
1251         get_cur_pos(This, FALSE, &end_pos);
1252         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1253
1254         if(Count > 0) {
1255             *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1256             set_range_pos(This, FALSE, &new_pos);
1257         }else {
1258             BOOL bounded;
1259
1260             *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1261             set_range_pos(This, bounded, &new_pos);
1262             if(bounded)
1263                 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1264         }
1265
1266         dompos_release(&start_pos);
1267         dompos_release(&end_pos);
1268         dompos_release(&new_pos);
1269         break;
1270     }
1271
1272     default:
1273         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1274     }
1275
1276     return S_OK;
1277 }
1278
1279 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1280 {
1281     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1282
1283     TRACE("(%p)\n", This);
1284
1285     if(This->doc->nscontainer) {
1286         nsIDOMWindow *dom_window = NULL;
1287         nsISelection *nsselection;
1288
1289         nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &dom_window);
1290         nsIDOMWindow_GetSelection(dom_window, &nsselection);
1291         nsIDOMWindow_Release(dom_window);
1292
1293         nsISelection_RemoveAllRanges(nsselection);
1294         nsISelection_AddRange(nsselection, This->nsrange);
1295
1296         nsISelection_Release(nsselection);
1297     }
1298
1299     return S_OK;
1300 }
1301
1302 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1303 {
1304     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1305     FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1306     return E_NOTIMPL;
1307 }
1308
1309 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1310 {
1311     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1312     FIXME("(%p)->(%p)\n", This, element);
1313     return E_NOTIMPL;
1314 }
1315
1316 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1317         IHTMLTxtRange *SourceRange)
1318 {
1319     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1320     FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1321     return E_NOTIMPL;
1322 }
1323
1324 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1325         IHTMLTxtRange *SourceRange, long *ret)
1326 {
1327     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1328     HTMLTxtRange *src_range;
1329     PRInt16 nsret = 0;
1330     int nscmpt;
1331     nsresult nsres;
1332
1333     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1334
1335     nscmpt = string_to_nscmptype(how);
1336     if(nscmpt == -1)
1337         return E_INVALIDARG;
1338
1339     src_range = get_range_object(This->doc, SourceRange);
1340     if(!src_range)
1341         return E_FAIL;
1342
1343     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1344     if(NS_FAILED(nsres))
1345         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1346
1347     *ret = nsret;
1348     return S_OK;
1349 }
1350
1351 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1352         long count, long Flags, VARIANT_BOOL *Success)
1353 {
1354     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1355     FIXME("(%p)->(%s %ld %08lx %p)\n", This, debugstr_w(String), count, Flags, Success);
1356     return E_NOTIMPL;
1357 }
1358
1359 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, long x, long y)
1360 {
1361     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1362     FIXME("(%p)->(%ld %ld)\n", This, x, y);
1363     return E_NOTIMPL;
1364 }
1365
1366 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1367 {
1368     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1369     FIXME("(%p)->(%p)\n", This, Bookmark);
1370     return E_NOTIMPL;
1371 }
1372
1373 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1374         VARIANT_BOOL *Success)
1375 {
1376     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1377     FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1378     return E_NOTIMPL;
1379 }
1380
1381 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1382         VARIANT_BOOL *pfRet)
1383 {
1384     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1385     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1386     return E_NOTIMPL;
1387 }
1388
1389 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1390         VARIANT_BOOL *pfRet)
1391 {
1392     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1393     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1394     return E_NOTIMPL;
1395 }
1396
1397 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1398         VARIANT_BOOL *pfRet)
1399 {
1400     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1401     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1402     return E_NOTIMPL;
1403 }
1404
1405 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1406         VARIANT_BOOL *pfRet)
1407 {
1408     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1409     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1410     return E_NOTIMPL;
1411 }
1412
1413 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1414         BSTR *pcmdText)
1415 {
1416     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1417     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1418     return E_NOTIMPL;
1419 }
1420
1421 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1422         VARIANT *pcmdValue)
1423 {
1424     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1425     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1426     return E_NOTIMPL;
1427 }
1428
1429 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1430         VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1431 {
1432     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1433     FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1434     return E_NOTIMPL;
1435 }
1436
1437 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1438         VARIANT_BOOL *pfRet)
1439 {
1440     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1441     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1442     return E_NOTIMPL;
1443 }
1444
1445 #undef HTMLTXTRANGE_THIS
1446
1447 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1448     HTMLTxtRange_QueryInterface,
1449     HTMLTxtRange_AddRef,
1450     HTMLTxtRange_Release,
1451     HTMLTxtRange_GetTypeInfoCount,
1452     HTMLTxtRange_GetTypeInfo,
1453     HTMLTxtRange_GetIDsOfNames,
1454     HTMLTxtRange_Invoke,
1455     HTMLTxtRange_get_htmlText,
1456     HTMLTxtRange_put_text,
1457     HTMLTxtRange_get_text,
1458     HTMLTxtRange_parentElement,
1459     HTMLTxtRange_duplicate,
1460     HTMLTxtRange_inRange,
1461     HTMLTxtRange_isEqual,
1462     HTMLTxtRange_scrollIntoView,
1463     HTMLTxtRange_collapse,
1464     HTMLTxtRange_expand,
1465     HTMLTxtRange_move,
1466     HTMLTxtRange_moveStart,
1467     HTMLTxtRange_moveEnd,
1468     HTMLTxtRange_select,
1469     HTMLTxtRange_pasteHTML,
1470     HTMLTxtRange_moveToElementText,
1471     HTMLTxtRange_setEndPoint,
1472     HTMLTxtRange_compareEndPoints,
1473     HTMLTxtRange_findText,
1474     HTMLTxtRange_moveToPoint,
1475     HTMLTxtRange_getBookmark,
1476     HTMLTxtRange_moveToBookmark,
1477     HTMLTxtRange_queryCommandSupported,
1478     HTMLTxtRange_queryCommandEnabled,
1479     HTMLTxtRange_queryCommandState,
1480     HTMLTxtRange_queryCommandIndeterm,
1481     HTMLTxtRange_queryCommandText,
1482     HTMLTxtRange_queryCommandValue,
1483     HTMLTxtRange_execCommand,
1484     HTMLTxtRange_execCommandShowHelp
1485 };
1486
1487 IHTMLTxtRange *HTMLTxtRange_Create(HTMLDocument *doc, nsIDOMRange *nsrange)
1488 {
1489     HTMLTxtRange *ret = mshtml_alloc(sizeof(HTMLTxtRange));
1490
1491     ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
1492     ret->ref = 1;
1493
1494     if(nsrange)
1495         nsIDOMRange_AddRef(nsrange);
1496     ret->nsrange = nsrange;
1497
1498     ret->doc = doc;
1499     list_add_head(&doc->range_list, &ret->entry);
1500
1501     return HTMLTXTRANGE(ret);
1502 }
1503
1504 void detach_ranges(HTMLDocument *This)
1505 {
1506     HTMLTxtRange *iter;
1507
1508     LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1509         iter->doc = NULL;
1510     }
1511 }