resources: Define a generic mechanism for adding extra values to the common version...
[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 <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmcid.h"
28
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31
32 #include "mshtml_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35
36 static const WCHAR brW[] = {'b','r',0};
37 static const WCHAR hrW[] = {'h','r',0};
38
39 typedef struct {
40     const IHTMLTxtRangeVtbl *lpHTMLTxtRangeVtbl;
41     const IOleCommandTargetVtbl *lpOleCommandTargetVtbl;
42
43     LONG ref;
44
45     nsIDOMRange *nsrange;
46     HTMLDocumentNode *doc;
47
48     struct list entry;
49 } HTMLTxtRange;
50
51 #define HTMLTXTRANGE(x)  ((IHTMLTxtRange*)  &(x)->lpHTMLTxtRangeVtbl)
52
53 typedef struct {
54     WCHAR *buf;
55     DWORD len;
56     DWORD size;
57 } wstrbuf_t;
58
59 typedef struct {
60     PRUint16 type;
61     nsIDOMNode *node;
62     PRUint32 off;
63     nsAString str;
64     const PRUnichar *p;
65 } dompos_t;
66
67 typedef enum {
68     RU_UNKNOWN,
69     RU_CHAR,
70     RU_WORD,
71     RU_SENTENCE,
72     RU_TEXTEDIT
73 } range_unit_t;
74
75 static HTMLTxtRange *get_range_object(HTMLDocumentNode *doc, IHTMLTxtRange *iface)
76 {
77     HTMLTxtRange *iter;
78
79     LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
80         if(HTMLTXTRANGE(iter) == iface)
81             return iter;
82     }
83
84     ERR("Could not find range in document\n");
85     return NULL;
86 }
87
88 static range_unit_t string_to_unit(LPCWSTR str)
89 {
90     static const WCHAR characterW[] =
91         {'c','h','a','r','a','c','t','e','r',0};
92     static const WCHAR wordW[] =
93         {'w','o','r','d',0};
94     static const WCHAR sentenceW[] =
95         {'s','e','n','t','e','n','c','e',0};
96     static const WCHAR texteditW[] =
97         {'t','e','x','t','e','d','i','t',0};
98
99     if(!strcmpiW(str, characterW))  return RU_CHAR;
100     if(!strcmpiW(str, wordW))       return RU_WORD;
101     if(!strcmpiW(str, sentenceW))   return RU_SENTENCE;
102     if(!strcmpiW(str, texteditW))   return RU_TEXTEDIT;
103
104     return RU_UNKNOWN;
105 }
106
107 static int string_to_nscmptype(LPCWSTR str)
108 {
109     static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
110     static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
111     static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
112     static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
113
114     if(!strcmpiW(str, seW))  return NS_START_TO_END;
115     if(!strcmpiW(str, ssW))  return NS_START_TO_START;
116     if(!strcmpiW(str, esW))  return NS_END_TO_START;
117     if(!strcmpiW(str, eeW))  return NS_END_TO_END;
118
119     return -1;
120 }
121
122 static PRUint16 get_node_type(nsIDOMNode *node)
123 {
124     PRUint16 type = 0;
125
126     if(node)
127         nsIDOMNode_GetNodeType(node, &type);
128
129     return type;
130 }
131
132 static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
133 {
134     nsIDOMElement *elem;
135     nsAString tag_str;
136     const PRUnichar *tag;
137     BOOL ret = FALSE;
138     nsresult nsres;
139
140     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
141     if(NS_FAILED(nsres))
142         return FALSE;
143
144     nsAString_Init(&tag_str, NULL);
145     nsIDOMElement_GetTagName(elem, &tag_str);
146     nsIDOMElement_Release(elem);
147     nsAString_GetData(&tag_str, &tag);
148
149     ret = !strcmpiW(tag, istag);
150
151     nsAString_Finish(&tag_str);
152
153     return ret;
154 }
155
156 static BOOL is_space_elem(nsIDOMNode *node)
157 {
158     nsIDOMElement *elem;
159     nsAString tag_str;
160     const PRUnichar *tag;
161     BOOL ret = FALSE;
162     nsresult nsres;
163
164     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
165     if(NS_FAILED(nsres))
166         return FALSE;
167
168     nsAString_Init(&tag_str, NULL);
169     nsIDOMElement_GetTagName(elem, &tag_str);
170     nsIDOMElement_Release(elem);
171     nsAString_GetData(&tag_str, &tag);
172
173     ret = !strcmpiW(tag, brW) || !strcmpiW(tag, hrW);
174
175     nsAString_Finish(&tag_str);
176
177     return ret;
178 }
179
180 static inline BOOL wstrbuf_init(wstrbuf_t *buf)
181 {
182     buf->len = 0;
183     buf->size = 16;
184     buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
185     if (!buf->buf) return FALSE;
186     *buf->buf = 0;
187     return TRUE;
188 }
189
190 static inline void wstrbuf_finish(wstrbuf_t *buf)
191 {
192     heap_free(buf->buf);
193 }
194
195 static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
196 {
197     if(buf->len+len >= buf->size) {
198         buf->size = 2*buf->size+len;
199         buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
200     }
201
202     memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
203     buf->len += len;
204     buf->buf[buf->len] = 0;
205 }
206
207 static void wstrbuf_append_nodetxt(wstrbuf_t *buf, LPCWSTR str, int len)
208 {
209     const WCHAR *s = str;
210     WCHAR *d;
211
212     TRACE("%s\n", debugstr_wn(str, len));
213
214     if(buf->len+len >= buf->size) {
215         buf->size = 2*buf->size+len;
216         buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
217     }
218
219     if(buf->len && isspaceW(buf->buf[buf->len-1])) {
220         while(s < str+len && isspaceW(*s))
221             s++;
222     }
223
224     d = buf->buf+buf->len;
225     while(s < str+len) {
226         if(isspaceW(*s)) {
227             *d++ = ' ';
228             s++;
229             while(s < str+len && isspaceW(*s))
230                 s++;
231         }else {
232             *d++ = *s++;
233         }
234     }
235
236     buf->len = d - buf->buf;
237     *d = 0;
238 }
239
240 static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node)
241 {
242
243     switch(get_node_type(node)) {
244     case TEXT_NODE: {
245         nsIDOMText *nstext;
246         nsAString data_str;
247         const PRUnichar *data;
248
249         nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
250
251         nsAString_Init(&data_str, NULL);
252         nsIDOMText_GetData(nstext, &data_str);
253         nsAString_GetData(&data_str, &data);
254         wstrbuf_append_nodetxt(buf, data, strlenW(data));
255         nsAString_Finish(&data_str);
256
257         nsIDOMText_Release(nstext);
258
259         break;
260     }
261     case ELEMENT_NODE:
262         if(is_elem_tag(node, brW)) {
263             static const WCHAR endlW[] = {'\r','\n'};
264             wstrbuf_append_len(buf, endlW, 2);
265         }else if(is_elem_tag(node, hrW)) {
266             static const WCHAR endl2W[] = {'\r','\n','\r','\n'};
267             wstrbuf_append_len(buf, endl2W, 4);
268         }
269     }
270 }
271
272 static void wstrbuf_append_node_rec(wstrbuf_t *buf, nsIDOMNode *node)
273 {
274     nsIDOMNode *iter, *tmp;
275
276     wstrbuf_append_node(buf, node);
277
278     nsIDOMNode_GetFirstChild(node, &iter);
279     while(iter) {
280         wstrbuf_append_node_rec(buf, iter);
281         nsIDOMNode_GetNextSibling(iter, &tmp);
282         nsIDOMNode_Release(iter);
283         iter = tmp;
284     }
285 }
286
287 static BOOL fill_nodestr(dompos_t *pos)
288 {
289     nsIDOMText *text;
290     nsresult nsres;
291
292     if(pos->type != TEXT_NODE)
293         return FALSE;
294
295     nsres = nsIDOMNode_QueryInterface(pos->node, &IID_nsIDOMText, (void**)&text);
296     if(NS_FAILED(nsres))
297         return FALSE;
298
299     nsAString_Init(&pos->str, NULL);
300     nsIDOMText_GetData(text, &pos->str);
301     nsIDOMText_Release(text);
302     nsAString_GetData(&pos->str, &pos->p);
303
304     if(pos->off == -1)
305         pos->off = *pos->p ? strlenW(pos->p)-1 : 0;
306
307     return TRUE;
308 }
309
310 static nsIDOMNode *next_node(nsIDOMNode *iter)
311 {
312     nsIDOMNode *ret, *tmp;
313     nsresult nsres;
314
315     if(!iter)
316         return NULL;
317
318     nsres = nsIDOMNode_GetFirstChild(iter, &ret);
319     if(NS_SUCCEEDED(nsres) && ret)
320         return ret;
321
322     nsIDOMNode_AddRef(iter);
323
324     do {
325         nsres = nsIDOMNode_GetNextSibling(iter, &ret);
326         if(NS_SUCCEEDED(nsres) && ret) {
327             nsIDOMNode_Release(iter);
328             return ret;
329         }
330
331         nsres = nsIDOMNode_GetParentNode(iter, &tmp);
332         nsIDOMNode_Release(iter);
333         iter = tmp;
334     }while(NS_SUCCEEDED(nsres) && iter);
335
336     return NULL;
337 }
338
339 static nsIDOMNode *prev_node(HTMLTxtRange *This, nsIDOMNode *iter)
340 {
341     nsIDOMNode *ret, *tmp;
342     nsresult nsres;
343
344     if(!iter) {
345         nsIDOMHTMLElement *nselem;
346
347         nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nselem);
348         nsIDOMElement_GetLastChild(nselem, &tmp);
349         if(!tmp)
350             return (nsIDOMNode*)nselem;
351
352         while(tmp) {
353             ret = tmp;
354             nsIDOMNode_GetLastChild(ret, &tmp);
355         }
356
357         nsIDOMElement_Release(nselem);
358
359         return ret;
360     }
361
362     nsres = nsIDOMNode_GetLastChild(iter, &ret);
363     if(NS_SUCCEEDED(nsres) && ret)
364         return ret;
365
366     nsIDOMNode_AddRef(iter);
367
368     do {
369         nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
370         if(NS_SUCCEEDED(nsres) && ret) {
371             nsIDOMNode_Release(iter);
372             return ret;
373         }
374
375         nsres = nsIDOMNode_GetParentNode(iter, &tmp);
376         nsIDOMNode_Release(iter);
377         iter = tmp;
378     }while(NS_SUCCEEDED(nsres) && iter);
379
380     return NULL;
381 }
382
383 static nsIDOMNode *get_child_node(nsIDOMNode *node, PRUint32 off)
384 {
385     nsIDOMNodeList *node_list;
386     nsIDOMNode *ret = NULL;
387
388     nsIDOMNode_GetChildNodes(node, &node_list);
389     nsIDOMNodeList_Item(node_list, off, &ret);
390     nsIDOMNodeList_Release(node_list);
391
392     return ret;
393 }
394
395 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
396 {
397     nsIDOMNode *node;
398     PRInt32 off;
399
400     pos->p = NULL;
401
402     if(!start) {
403         PRBool collapsed;
404         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
405         start = collapsed;
406     }
407
408     if(start) {
409         nsIDOMRange_GetStartContainer(This->nsrange, &node);
410         nsIDOMRange_GetStartOffset(This->nsrange, &off);
411     }else {
412         nsIDOMRange_GetEndContainer(This->nsrange, &node);
413         nsIDOMRange_GetEndOffset(This->nsrange, &off);
414     }
415
416     pos->type = get_node_type(node);
417     if(pos->type == ELEMENT_NODE) {
418         if(start) {
419             pos->node = get_child_node(node, off);
420             pos->off = 0;
421         }else {
422             pos->node = off ? get_child_node(node, off-1) : prev_node(This, node);
423             pos->off = -1;
424         }
425
426         pos->type = get_node_type(pos->node);
427         nsIDOMNode_Release(node);
428     }else if(start) {
429         pos->node = node;
430         pos->off = off;
431     }else if(off) {
432         pos->node = node;
433         pos->off = off-1;
434     }else {
435         pos->node = prev_node(This, node);
436         pos->off = -1;
437         nsIDOMNode_Release(node);
438     }
439
440     if(pos->type == TEXT_NODE)
441         fill_nodestr(pos);
442 }
443
444 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
445 {
446     nsresult nsres;
447
448     if(start) {
449         if(pos->type == TEXT_NODE)
450             nsres = nsIDOMRange_SetStart(This->nsrange, pos->node, pos->off);
451         else
452             nsres = nsIDOMRange_SetStartBefore(This->nsrange, pos->node);
453     }else {
454         if(pos->type == TEXT_NODE && pos->p[pos->off+1])
455             nsres = nsIDOMRange_SetEnd(This->nsrange, pos->node, pos->off+1);
456         else
457             nsres = nsIDOMRange_SetEndAfter(This->nsrange, pos->node);
458     }
459
460     if(NS_FAILED(nsres))
461         ERR("failed: %p %08x\n", pos->node, nsres);
462 }
463
464 static inline void dompos_release(dompos_t *pos)
465 {
466     if(pos->node)
467         nsIDOMNode_Release(pos->node);
468
469     if(pos->p)
470         nsAString_Finish(&pos->str);
471 }
472
473 static inline void dompos_addref(dompos_t *pos)
474 {
475     if(pos->node)
476         nsIDOMNode_AddRef(pos->node);
477
478     if(pos->type == TEXT_NODE)
479         fill_nodestr(pos);
480 }
481
482 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
483 {
484     return pos1->node == pos2->node && pos1->off == pos2->off;
485 }
486
487 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
488 {
489     nsIDOMNode *iter, *tmp;
490     dompos_t start_pos, end_pos;
491     PRBool collapsed;
492
493     nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
494     if(collapsed) {
495         wstrbuf_finish(buf);
496         buf->buf = NULL;
497         buf->size = 0;
498         return;
499     }
500
501     get_cur_pos(This, FALSE, &end_pos);
502     get_cur_pos(This, TRUE, &start_pos);
503
504     if(start_pos.type == TEXT_NODE) {
505         if(start_pos.node == end_pos.node) {
506             wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, end_pos.off-start_pos.off+1);
507             iter = start_pos.node;
508             nsIDOMNode_AddRef(iter);
509         }else {
510             wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, strlenW(start_pos.p+start_pos.off));
511             iter = next_node(start_pos.node);
512         }
513     }else {
514         iter = start_pos.node;
515         nsIDOMNode_AddRef(iter);
516     }
517
518     while(iter != end_pos.node) {
519         wstrbuf_append_node(buf, iter);
520         tmp = next_node(iter);
521         nsIDOMNode_Release(iter);
522         iter = tmp;
523     }
524
525     nsIDOMNode_AddRef(end_pos.node);
526
527     if(start_pos.node != end_pos.node) {
528         if(end_pos.type == TEXT_NODE)
529             wstrbuf_append_nodetxt(buf, end_pos.p, end_pos.off+1);
530         else
531             wstrbuf_append_node(buf, end_pos.node);
532     }
533
534     nsIDOMNode_Release(iter);
535     dompos_release(&start_pos);
536     dompos_release(&end_pos);
537
538     if(buf->len) {
539         WCHAR *p;
540
541         for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);
542
543         p = strchrW(p, '\r');
544         if(p)
545             *p = 0;
546     }
547 }
548
549 HRESULT get_node_text(HTMLDOMNode *node, BSTR *ret)
550 {
551     wstrbuf_t buf;
552     HRESULT hres = S_OK;
553
554     if (!wstrbuf_init(&buf))
555         return E_OUTOFMEMORY;
556     wstrbuf_append_node_rec(&buf, node->nsnode);
557     if(buf.buf) {
558         *ret = SysAllocString(buf.buf);
559         if(!*ret)
560             hres = E_OUTOFMEMORY;
561     } else {
562         *ret = NULL;
563     }
564     wstrbuf_finish(&buf);
565
566     if(SUCCEEDED(hres))
567         TRACE("ret %s\n", debugstr_w(*ret));
568     return hres;
569 }
570
571 static WCHAR get_pos_char(const dompos_t *pos)
572 {
573     switch(pos->type) {
574     case TEXT_NODE:
575         return pos->p[pos->off];
576     case ELEMENT_NODE:
577         if(is_space_elem(pos->node))
578             return '\n';
579     }
580
581     return 0;
582 }
583
584 static void end_space(const dompos_t *pos, dompos_t *new_pos)
585 {
586     const WCHAR *p;
587
588     *new_pos = *pos;
589     dompos_addref(new_pos);
590
591     if(pos->type != TEXT_NODE)
592         return;
593
594     p = new_pos->p+new_pos->off;
595
596     if(!*p || !isspace(*p))
597         return;
598
599     while(p[1] && isspace(p[1]))
600         p++;
601
602     new_pos->off = p - new_pos->p;
603 }
604
605 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
606 {
607     nsIDOMNode *iter, *tmp;
608     dompos_t last_space, tmp_pos;
609     const WCHAR *p;
610     WCHAR cspace = 0;
611
612     if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off]) {
613         p = pos->p+pos->off;
614
615         if(isspace(*p))
616             while(isspaceW(*++p));
617         else
618             p++;
619
620         if(*p && isspaceW(*p)) {
621             cspace = ' ';
622             while(p[1] && isspaceW(p[1]))
623                 p++;
624         }
625
626         if(*p) {
627             *new_pos = *pos;
628             new_pos->off = p - pos->p;
629             dompos_addref(new_pos);
630
631             return cspace ? cspace : *p;
632         }else {
633             last_space = *pos;
634             last_space.off = p - pos->p;
635             dompos_addref(&last_space);
636         }
637     }
638
639     iter = next_node(pos->node);
640
641     while(iter) {
642         switch(get_node_type(iter)) {
643         case TEXT_NODE:
644             tmp_pos.node = iter;
645             tmp_pos.type = TEXT_NODE;
646             tmp_pos.off = 0;
647             dompos_addref(&tmp_pos);
648
649             p = tmp_pos.p;
650
651             if(!*p) {
652                 dompos_release(&tmp_pos);
653                 break;
654             }else if(isspaceW(*p)) {
655                 if(cspace)
656                     dompos_release(&last_space);
657                 else
658                     cspace = ' ';
659
660                 while(p[1] && isspaceW(p[1]))
661                       p++;
662
663                 tmp_pos.off = p-tmp_pos.p;
664
665                 if(!p[1]) {
666                     last_space = tmp_pos;
667                     break;
668                 }
669
670                 *new_pos = tmp_pos;
671                 nsIDOMNode_Release(iter);
672                 return cspace;
673             }else if(cspace) {
674                 *new_pos = last_space;
675                 dompos_release(&tmp_pos);
676                 nsIDOMNode_Release(iter);
677
678                 return cspace;
679             }else if(*p) {
680                 tmp_pos.off = 0;
681                 *new_pos = tmp_pos;
682             }
683
684             nsIDOMNode_Release(iter);
685             return *p;
686
687         case ELEMENT_NODE:
688             if(is_elem_tag(iter, brW)) {
689                 if(cspace)
690                     dompos_release(&last_space);
691                 cspace = '\n';
692
693                 nsIDOMNode_AddRef(iter);
694                 last_space.node = iter;
695                 last_space.type = ELEMENT_NODE;
696                 last_space.off = 0;
697                 last_space.p = NULL;
698             }else if(is_elem_tag(iter, hrW)) {
699                 if(cspace) {
700                     *new_pos = last_space;
701                     nsIDOMNode_Release(iter);
702                     return cspace;
703                 }
704
705                 new_pos->node = iter;
706                 new_pos->type = ELEMENT_NODE;
707                 new_pos->off = 0;
708                 new_pos->p = NULL;
709                 return '\n';
710             }
711         }
712
713         tmp = iter;
714         iter = next_node(iter);
715         nsIDOMNode_Release(tmp);
716     }
717
718     if(cspace) {
719         *new_pos = last_space;
720     }else {
721         *new_pos = *pos;
722         dompos_addref(new_pos);
723     }
724
725     return cspace;
726 }
727
728 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
729 {
730     nsIDOMNode *iter, *tmp;
731     const WCHAR *p;
732     BOOL skip_space = FALSE;
733
734     if(pos->type == TEXT_NODE && isspaceW(pos->p[pos->off]))
735         skip_space = TRUE;
736
737     if(pos->type == TEXT_NODE && pos->off) {
738         p = pos->p+pos->off-1;
739
740         if(skip_space) {
741             while(p >= pos->p && isspace(*p))
742                 p--;
743         }
744
745         if(p >= pos->p) {
746             *new_pos = *pos;
747             new_pos->off = p-pos->p;
748             dompos_addref(new_pos);
749             return new_pos->p[new_pos->off];
750         }
751     }
752
753     iter = prev_node(This, pos->node);
754
755     while(iter) {
756         switch(get_node_type(iter)) {
757         case TEXT_NODE: {
758             dompos_t tmp_pos;
759
760             tmp_pos.node = iter;
761             tmp_pos.type = TEXT_NODE;
762             tmp_pos.off = 0;
763             dompos_addref(&tmp_pos);
764
765             p = tmp_pos.p + strlenW(tmp_pos.p)-1;
766
767             if(skip_space) {
768                 while(p >= tmp_pos.p && isspaceW(*p))
769                     p--;
770             }
771
772             if(p < tmp_pos.p) {
773                 dompos_release(&tmp_pos);
774                 break;
775             }
776
777             tmp_pos.off = p-tmp_pos.p;
778             *new_pos = tmp_pos;
779             nsIDOMNode_Release(iter);
780             return *p;
781         }
782
783         case ELEMENT_NODE:
784             if(is_elem_tag(iter, brW)) {
785                 if(skip_space) {
786                     skip_space = FALSE;
787                     break;
788                 }
789             }else if(!is_elem_tag(iter, hrW)) {
790                 break;
791             }
792
793             new_pos->node = iter;
794             new_pos->type = ELEMENT_NODE;
795             new_pos->off = 0;
796             new_pos->p = NULL;
797             return '\n';
798         }
799
800         tmp = iter;
801         iter = prev_node(This, iter);
802         nsIDOMNode_Release(tmp);
803     }
804
805     *new_pos = *pos;
806     dompos_addref(new_pos);
807     return 0;
808 }
809
810 static LONG move_next_chars(LONG cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
811         BOOL *bounded, dompos_t *new_pos)
812 {
813     dompos_t iter, tmp;
814     LONG ret = 0;
815     WCHAR c;
816
817     if(bounded)
818         *bounded = FALSE;
819
820     if(col)
821         ret++;
822
823     if(ret >= cnt) {
824         end_space(pos, new_pos);
825         return ret;
826     }
827
828     c = next_char(pos, &iter);
829     ret++;
830
831     while(ret < cnt) {
832         tmp = iter;
833         c = next_char(&tmp, &iter);
834         dompos_release(&tmp);
835         if(!c)
836             break;
837
838         ret++;
839         if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
840             *bounded = TRUE;
841             ret++;
842         }
843     }
844
845     *new_pos = iter;
846     return ret;
847 }
848
849 static LONG move_prev_chars(HTMLTxtRange *This, LONG cnt, const dompos_t *pos, BOOL end,
850         const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
851 {
852     dompos_t iter, tmp;
853     LONG ret = 0;
854     BOOL prev_eq = FALSE;
855     WCHAR c;
856
857     if(bounded)
858         *bounded = FALSE;
859
860     c = prev_char(This, pos, &iter);
861     if(c)
862         ret++;
863
864     while(c && ret < cnt) {
865         tmp = iter;
866         c = prev_char(This, &tmp, &iter);
867         dompos_release(&tmp);
868         if(!c) {
869             if(end)
870                 ret++;
871             break;
872         }
873
874         ret++;
875
876         if(prev_eq) {
877             *bounded = TRUE;
878             ret++;
879         }
880
881         prev_eq = bound_pos && dompos_cmp(&iter, bound_pos);
882     }
883
884     *new_pos = iter;
885     return ret;
886 }
887
888 static LONG find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
889 {
890     dompos_t iter, tmp;
891     WCHAR c;
892
893     c = prev_char(This, pos, &iter);
894     if(!c || (first_space && isspaceW(c))) {
895         *ret = iter;
896         return FALSE;
897     }
898
899     while(1) {
900         tmp = iter;
901         c = prev_char(This, &tmp, &iter);
902         if(!c || isspaceW(c)) {
903             dompos_release(&iter);
904             break;
905         }
906         dompos_release(&tmp);
907     }
908
909     *ret = tmp;
910     return TRUE;
911 }
912
913 static int find_word_end(const dompos_t *pos, dompos_t *ret)
914 {
915     dompos_t iter, tmp;
916     int cnt = 1;
917     WCHAR c;
918     c = get_pos_char(pos);
919     if(isspaceW(c)) {
920         *ret = *pos;
921         dompos_addref(ret);
922         return 0;
923     }
924
925     c = next_char(pos, &iter);
926     if(!c) {
927         *ret = iter;
928         return 0;
929     }
930     if(c == '\n') {
931         *ret = *pos;
932         dompos_addref(ret);
933         return 0;
934     }
935
936     while(c && !isspaceW(c)) {
937         tmp = iter;
938         c = next_char(&tmp, &iter);
939         if(c == '\n') {
940             dompos_release(&iter);
941             iter = tmp;
942         }else {
943             cnt++;
944             dompos_release(&tmp);
945         }
946     }
947
948     *ret = iter;
949     return cnt;
950 }
951
952 static LONG move_next_words(LONG cnt, const dompos_t *pos, dompos_t *new_pos)
953 {
954     dompos_t iter, tmp;
955     LONG ret = 0;
956     WCHAR c;
957
958     c = get_pos_char(pos);
959     if(isspaceW(c)) {
960         end_space(pos, &iter);
961         ret++;
962     }else {
963         c = next_char(pos, &iter);
964         if(c && isspaceW(c))
965             ret++;
966     }
967
968     while(c && ret < cnt) {
969         tmp = iter;
970         c = next_char(&tmp, &iter);
971         dompos_release(&tmp);
972         if(isspaceW(c))
973             ret++;
974     }
975
976     *new_pos = iter;
977     return ret;
978 }
979
980 static LONG move_prev_words(HTMLTxtRange *This, LONG cnt, const dompos_t *pos, dompos_t *new_pos)
981 {
982     dompos_t iter, tmp;
983     LONG ret = 0;
984
985     iter = *pos;
986     dompos_addref(&iter);
987
988     while(ret < cnt) {
989         if(!find_prev_space(This, &iter, FALSE, &tmp))
990             break;
991
992         dompos_release(&iter);
993         iter = tmp;
994         ret++;
995     }
996
997     *new_pos = iter;
998     return ret;
999 }
1000
1001 #define HTMLTXTRANGE_THIS(iface) DEFINE_THIS(HTMLTxtRange, HTMLTxtRange, iface)
1002
1003 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
1004 {
1005     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1006
1007     *ppv = NULL;
1008
1009     if(IsEqualGUID(&IID_IUnknown, riid)) {
1010         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1011         *ppv = HTMLTXTRANGE(This);
1012     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1013         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1014         *ppv = HTMLTXTRANGE(This);
1015     }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
1016         TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
1017         *ppv = HTMLTXTRANGE(This);
1018     }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1019         TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
1020         *ppv = CMDTARGET(This);
1021     }
1022
1023     if(*ppv) {
1024         IUnknown_AddRef((IUnknown*)*ppv);
1025         return S_OK;
1026     }
1027
1028     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1029     return E_NOINTERFACE;
1030 }
1031
1032 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
1033 {
1034     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1035     LONG ref = InterlockedIncrement(&This->ref);
1036
1037     TRACE("(%p) ref=%d\n", This, ref);
1038
1039     return ref;
1040 }
1041
1042 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1043 {
1044     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1045     LONG ref = InterlockedDecrement(&This->ref);
1046
1047     TRACE("(%p) ref=%d\n", This, ref);
1048
1049     if(!ref) {
1050         if(This->nsrange)
1051             nsISelection_Release(This->nsrange);
1052         if(This->doc)
1053             list_remove(&This->entry);
1054         heap_free(This);
1055     }
1056
1057     return ref;
1058 }
1059
1060 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1061 {
1062     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1063     FIXME("(%p)->(%p)\n", This, pctinfo);
1064     return E_NOTIMPL;
1065 }
1066
1067 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1068                                                LCID lcid, ITypeInfo **ppTInfo)
1069 {
1070     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1071     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1072     return E_NOTIMPL;
1073 }
1074
1075 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1076                                                  LPOLESTR *rgszNames, UINT cNames,
1077                                                  LCID lcid, DISPID *rgDispId)
1078 {
1079     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1080     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1081           lcid, rgDispId);
1082     return E_NOTIMPL;
1083 }
1084
1085 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1086                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1087                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1088 {
1089     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1090     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1091           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1092     return E_NOTIMPL;
1093 }
1094
1095 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1096 {
1097     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1098
1099     TRACE("(%p)->(%p)\n", This, p);
1100
1101     *p = NULL;
1102
1103     if(This->nsrange) {
1104         nsIDOMDocumentFragment *fragment;
1105         nsresult nsres;
1106
1107         nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1108         if(NS_SUCCEEDED(nsres)) {
1109             const PRUnichar *nstext;
1110             nsAString nsstr;
1111
1112             nsAString_Init(&nsstr, NULL);
1113             nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1114             nsIDOMDocumentFragment_Release(fragment);
1115
1116             nsAString_GetData(&nsstr, &nstext);
1117             *p = SysAllocString(nstext);
1118
1119             nsAString_Finish(&nsstr);
1120         }
1121     }
1122
1123     if(!*p) {
1124         const WCHAR emptyW[] = {0};
1125         *p = SysAllocString(emptyW);
1126     }
1127
1128     TRACE("return %s\n", debugstr_w(*p));
1129     return S_OK;
1130 }
1131
1132 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1133 {
1134     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1135     nsIDOMText *text_node;
1136     nsAString text_str;
1137     nsresult nsres;
1138
1139     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1140
1141     if(!This->doc)
1142         return MSHTML_E_NODOC;
1143
1144     nsAString_InitDepend(&text_str, v);
1145     nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc->nsdoc, &text_str, &text_node);
1146     nsAString_Finish(&text_str);
1147     if(NS_FAILED(nsres)) {
1148         ERR("CreateTextNode failed: %08x\n", nsres);
1149         return S_OK;
1150     }
1151     nsres = nsIDOMRange_DeleteContents(This->nsrange);
1152     if(NS_FAILED(nsres))
1153         ERR("DeleteContents failed: %08x\n", nsres);
1154
1155     nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1156     if(NS_FAILED(nsres))
1157         ERR("InsertNode failed: %08x\n", nsres);
1158
1159     nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1160     if(NS_FAILED(nsres))
1161         ERR("SetEndAfter failed: %08x\n", nsres);
1162
1163     return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), VARIANT_FALSE);
1164 }
1165
1166 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1167 {
1168     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1169     wstrbuf_t buf;
1170
1171     TRACE("(%p)->(%p)\n", This, p);
1172
1173     *p = NULL;
1174     if(!This->nsrange)
1175         return S_OK;
1176
1177     if (!wstrbuf_init(&buf))
1178         return E_OUTOFMEMORY;
1179     range_to_string(This, &buf);
1180     if (buf.buf)
1181         *p = SysAllocString(buf.buf);
1182     wstrbuf_finish(&buf);
1183
1184     TRACE("ret %s\n", debugstr_w(*p));
1185     return S_OK;
1186 }
1187
1188 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1189 {
1190     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1191     nsIDOMNode *nsnode, *tmp;
1192     HTMLDOMNode *node;
1193     HRESULT hres;
1194
1195     TRACE("(%p)->(%p)\n", This, parent);
1196
1197     nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1198     while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1199         nsIDOMNode_GetParentNode(nsnode, &tmp);
1200         nsIDOMNode_Release(nsnode);
1201         nsnode = tmp;
1202     }
1203
1204     if(!nsnode) {
1205         *parent = NULL;
1206         return S_OK;
1207     }
1208
1209     hres = get_node(This->doc, nsnode, TRUE, &node);
1210     nsIDOMNode_Release(nsnode);
1211     if(FAILED(hres))
1212         return hres;
1213
1214     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)parent);
1215 }
1216
1217 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1218 {
1219     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1220     nsIDOMRange *nsrange = NULL;
1221     HRESULT hres;
1222
1223     TRACE("(%p)->(%p)\n", This, Duplicate);
1224
1225     nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1226     hres = HTMLTxtRange_Create(This->doc, nsrange, Duplicate);
1227     nsIDOMRange_Release(nsrange);
1228
1229     return hres;
1230 }
1231
1232 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1233         VARIANT_BOOL *InRange)
1234 {
1235     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1236     HTMLTxtRange *src_range;
1237     PRInt16 nsret = 0;
1238     nsresult nsres;
1239
1240     TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1241
1242     *InRange = VARIANT_FALSE;
1243
1244     src_range = get_range_object(This->doc, Range);
1245     if(!src_range)
1246         return E_FAIL;
1247
1248     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1249             src_range->nsrange, &nsret);
1250     if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1251         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1252                 src_range->nsrange, &nsret);
1253         if(NS_SUCCEEDED(nsres) && nsret >= 0)
1254             *InRange = VARIANT_TRUE;
1255     }
1256
1257     if(NS_FAILED(nsres))
1258         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1259
1260     return S_OK;
1261 }
1262
1263 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1264         VARIANT_BOOL *IsEqual)
1265 {
1266     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1267     HTMLTxtRange *src_range;
1268     PRInt16 nsret = 0;
1269     nsresult nsres;
1270
1271     TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1272
1273     *IsEqual = VARIANT_FALSE;
1274
1275     src_range = get_range_object(This->doc, Range);
1276     if(!src_range)
1277         return E_FAIL;
1278
1279     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1280             src_range->nsrange, &nsret);
1281     if(NS_SUCCEEDED(nsres) && !nsret) {
1282         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1283                 src_range->nsrange, &nsret);
1284         if(NS_SUCCEEDED(nsres) && !nsret)
1285             *IsEqual = VARIANT_TRUE;
1286     }
1287
1288     if(NS_FAILED(nsres))
1289         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1290
1291     return S_OK;
1292 }
1293
1294 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1295 {
1296     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1297     FIXME("(%p)->(%x)\n", This, fStart);
1298     return E_NOTIMPL;
1299 }
1300
1301 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1302 {
1303     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1304
1305     TRACE("(%p)->(%x)\n", This, Start);
1306
1307     nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1308     return S_OK;
1309 }
1310
1311 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1312 {
1313     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1314     range_unit_t unit;
1315
1316     TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1317
1318     unit = string_to_unit(Unit);
1319     if(unit == RU_UNKNOWN)
1320         return E_INVALIDARG;
1321
1322     *Success = VARIANT_FALSE;
1323
1324     switch(unit) {
1325     case RU_WORD: {
1326         dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1327         PRBool collapsed;
1328
1329         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1330
1331         get_cur_pos(This, TRUE, &start_pos);
1332         get_cur_pos(This, FALSE, &end_pos);
1333
1334         if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1335             set_range_pos(This, FALSE, &new_end_pos);
1336             *Success = VARIANT_TRUE;
1337         }
1338
1339         if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1340             if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1341                 set_range_pos(This, TRUE, &new_start_pos);
1342                 *Success = VARIANT_TRUE;
1343             }
1344             dompos_release(&new_start_pos);
1345         }
1346
1347         dompos_release(&new_end_pos);
1348         dompos_release(&end_pos);
1349         dompos_release(&start_pos);
1350
1351         break;
1352     }
1353
1354     case RU_TEXTEDIT: {
1355         nsIDOMHTMLElement *nsbody = NULL;
1356         nsresult nsres;
1357
1358         nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
1359         if(NS_FAILED(nsres) || !nsbody) {
1360             ERR("Could not get body: %08x\n", nsres);
1361             break;
1362         }
1363
1364         nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1365         nsIDOMHTMLElement_Release(nsbody);
1366         if(NS_FAILED(nsres)) {
1367             ERR("Collapse failed: %08x\n", nsres);
1368             break;
1369         }
1370
1371         *Success = VARIANT_TRUE;
1372         break;
1373     }
1374
1375     default:
1376         FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1377     }
1378
1379     return S_OK;
1380 }
1381
1382 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1383         LONG Count, LONG *ActualCount)
1384 {
1385     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1386     range_unit_t unit;
1387
1388     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1389
1390     unit = string_to_unit(Unit);
1391     if(unit == RU_UNKNOWN)
1392         return E_INVALIDARG;
1393
1394     if(!Count) {
1395         *ActualCount = 0;
1396         return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1397     }
1398
1399     switch(unit) {
1400     case RU_CHAR: {
1401         dompos_t cur_pos, new_pos;
1402
1403         get_cur_pos(This, TRUE, &cur_pos);
1404
1405         if(Count > 0) {
1406             *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1407             set_range_pos(This, FALSE, &new_pos);
1408             dompos_release(&new_pos);
1409
1410             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1411         }else {
1412             *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1413             set_range_pos(This, TRUE, &new_pos);
1414             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1415             dompos_release(&new_pos);
1416         }
1417
1418         dompos_release(&cur_pos);
1419         break;
1420     }
1421
1422     case RU_WORD: {
1423         dompos_t cur_pos, new_pos;
1424
1425         get_cur_pos(This, TRUE, &cur_pos);
1426
1427         if(Count > 0) {
1428             *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1429             set_range_pos(This, FALSE, &new_pos);
1430             dompos_release(&new_pos);
1431             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1432         }else {
1433             *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1434             set_range_pos(This, TRUE, &new_pos);
1435             IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1436             dompos_release(&new_pos);
1437         }
1438
1439         dompos_release(&cur_pos);
1440         break;
1441     }
1442
1443     default:
1444         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1445     }
1446
1447     TRACE("ret %d\n", *ActualCount);
1448     return S_OK;
1449 }
1450
1451 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1452         LONG Count, LONG *ActualCount)
1453 {
1454     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1455     range_unit_t unit;
1456
1457     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1458
1459     unit = string_to_unit(Unit);
1460     if(unit == RU_UNKNOWN)
1461         return E_INVALIDARG;
1462
1463     if(!Count) {
1464         *ActualCount = 0;
1465         return S_OK;
1466     }
1467
1468     switch(unit) {
1469     case RU_CHAR: {
1470         dompos_t start_pos, end_pos, new_pos;
1471         PRBool collapsed;
1472
1473         get_cur_pos(This, TRUE, &start_pos);
1474         get_cur_pos(This, FALSE, &end_pos);
1475         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1476
1477         if(Count > 0) {
1478             BOOL bounded;
1479
1480             *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1481             set_range_pos(This, !bounded, &new_pos);
1482             if(bounded)
1483                 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1484         }else {
1485             *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1486             set_range_pos(This, TRUE, &new_pos);
1487         }
1488
1489         dompos_release(&start_pos);
1490         dompos_release(&end_pos);
1491         dompos_release(&new_pos);
1492         break;
1493     }
1494
1495     default:
1496         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1497     }
1498
1499     return S_OK;
1500 }
1501
1502 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1503         LONG Count, LONG *ActualCount)
1504 {
1505     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1506     range_unit_t unit;
1507
1508     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1509
1510     unit = string_to_unit(Unit);
1511     if(unit == RU_UNKNOWN)
1512         return E_INVALIDARG;
1513
1514     if(!Count) {
1515         *ActualCount = 0;
1516         return S_OK;
1517     }
1518
1519     switch(unit) {
1520     case RU_CHAR: {
1521         dompos_t start_pos, end_pos, new_pos;
1522         PRBool collapsed;
1523
1524         get_cur_pos(This, TRUE, &start_pos);
1525         get_cur_pos(This, FALSE, &end_pos);
1526         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1527
1528         if(Count > 0) {
1529             *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1530             set_range_pos(This, FALSE, &new_pos);
1531         }else {
1532             BOOL bounded;
1533
1534             *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1535             set_range_pos(This, bounded, &new_pos);
1536             if(bounded)
1537                 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1538         }
1539
1540         dompos_release(&start_pos);
1541         dompos_release(&end_pos);
1542         dompos_release(&new_pos);
1543         break;
1544     }
1545
1546     default:
1547         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1548     }
1549
1550     return S_OK;
1551 }
1552
1553 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1554 {
1555     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1556     nsISelection *nsselection;
1557     nsresult nsres;
1558
1559     TRACE("(%p)\n", This);
1560
1561     nsres = nsIDOMWindow_GetSelection(This->doc->basedoc.window->nswindow, &nsselection);
1562     if(NS_FAILED(nsres)) {
1563         ERR("GetSelection failed: %08x\n", nsres);
1564         return E_FAIL;
1565     }
1566
1567     nsISelection_RemoveAllRanges(nsselection);
1568     nsISelection_AddRange(nsselection, This->nsrange);
1569     nsISelection_Release(nsselection);
1570     return S_OK;
1571 }
1572
1573 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1574 {
1575     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1576     FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1577     return E_NOTIMPL;
1578 }
1579
1580 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1581 {
1582     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1583     FIXME("(%p)->(%p)\n", This, element);
1584     return E_NOTIMPL;
1585 }
1586
1587 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1588         IHTMLTxtRange *SourceRange)
1589 {
1590     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1591     FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1592     return E_NOTIMPL;
1593 }
1594
1595 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1596         IHTMLTxtRange *SourceRange, LONG *ret)
1597 {
1598     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1599     HTMLTxtRange *src_range;
1600     PRInt16 nsret = 0;
1601     int nscmpt;
1602     nsresult nsres;
1603
1604     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1605
1606     nscmpt = string_to_nscmptype(how);
1607     if(nscmpt == -1)
1608         return E_INVALIDARG;
1609
1610     src_range = get_range_object(This->doc, SourceRange);
1611     if(!src_range)
1612         return E_FAIL;
1613
1614     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1615     if(NS_FAILED(nsres))
1616         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1617
1618     *ret = nsret;
1619     return S_OK;
1620 }
1621
1622 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1623         LONG count, LONG Flags, VARIANT_BOOL *Success)
1624 {
1625     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1626     FIXME("(%p)->(%s %d %08x %p)\n", This, debugstr_w(String), count, Flags, Success);
1627     return E_NOTIMPL;
1628 }
1629
1630 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, LONG x, LONG y)
1631 {
1632     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1633     FIXME("(%p)->(%d %d)\n", This, x, y);
1634     return E_NOTIMPL;
1635 }
1636
1637 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1638 {
1639     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1640     FIXME("(%p)->(%p)\n", This, Bookmark);
1641     return E_NOTIMPL;
1642 }
1643
1644 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1645         VARIANT_BOOL *Success)
1646 {
1647     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1648     FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1649     return E_NOTIMPL;
1650 }
1651
1652 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1653         VARIANT_BOOL *pfRet)
1654 {
1655     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1656     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1657     return E_NOTIMPL;
1658 }
1659
1660 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1661         VARIANT_BOOL *pfRet)
1662 {
1663     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1664     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1665     return E_NOTIMPL;
1666 }
1667
1668 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1669         VARIANT_BOOL *pfRet)
1670 {
1671     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1672     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1673     return E_NOTIMPL;
1674 }
1675
1676 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1677         VARIANT_BOOL *pfRet)
1678 {
1679     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1680     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1681     return E_NOTIMPL;
1682 }
1683
1684 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1685         BSTR *pcmdText)
1686 {
1687     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1688     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1689     return E_NOTIMPL;
1690 }
1691
1692 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1693         VARIANT *pcmdValue)
1694 {
1695     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1696     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1697     return E_NOTIMPL;
1698 }
1699
1700 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1701         VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1702 {
1703     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1704     FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1705     return E_NOTIMPL;
1706 }
1707
1708 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1709         VARIANT_BOOL *pfRet)
1710 {
1711     HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1712     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1713     return E_NOTIMPL;
1714 }
1715
1716 #undef HTMLTXTRANGE_THIS
1717
1718 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1719     HTMLTxtRange_QueryInterface,
1720     HTMLTxtRange_AddRef,
1721     HTMLTxtRange_Release,
1722     HTMLTxtRange_GetTypeInfoCount,
1723     HTMLTxtRange_GetTypeInfo,
1724     HTMLTxtRange_GetIDsOfNames,
1725     HTMLTxtRange_Invoke,
1726     HTMLTxtRange_get_htmlText,
1727     HTMLTxtRange_put_text,
1728     HTMLTxtRange_get_text,
1729     HTMLTxtRange_parentElement,
1730     HTMLTxtRange_duplicate,
1731     HTMLTxtRange_inRange,
1732     HTMLTxtRange_isEqual,
1733     HTMLTxtRange_scrollIntoView,
1734     HTMLTxtRange_collapse,
1735     HTMLTxtRange_expand,
1736     HTMLTxtRange_move,
1737     HTMLTxtRange_moveStart,
1738     HTMLTxtRange_moveEnd,
1739     HTMLTxtRange_select,
1740     HTMLTxtRange_pasteHTML,
1741     HTMLTxtRange_moveToElementText,
1742     HTMLTxtRange_setEndPoint,
1743     HTMLTxtRange_compareEndPoints,
1744     HTMLTxtRange_findText,
1745     HTMLTxtRange_moveToPoint,
1746     HTMLTxtRange_getBookmark,
1747     HTMLTxtRange_moveToBookmark,
1748     HTMLTxtRange_queryCommandSupported,
1749     HTMLTxtRange_queryCommandEnabled,
1750     HTMLTxtRange_queryCommandState,
1751     HTMLTxtRange_queryCommandIndeterm,
1752     HTMLTxtRange_queryCommandText,
1753     HTMLTxtRange_queryCommandValue,
1754     HTMLTxtRange_execCommand,
1755     HTMLTxtRange_execCommandShowHelp
1756 };
1757
1758 #define OLECMDTRG_THIS(iface) DEFINE_THIS(HTMLTxtRange, OleCommandTarget, iface)
1759
1760 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1761 {
1762     HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1763     return IHTMLTxtRange_QueryInterface(HTMLTXTRANGE(This), riid, ppv);
1764 }
1765
1766 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1767 {
1768     HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1769     return IHTMLTxtRange_AddRef(HTMLTXTRANGE(This));
1770 }
1771
1772 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1773 {
1774     HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1775     return IHTMLTxtRange_Release(HTMLTXTRANGE(This));
1776 }
1777
1778 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1779         ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1780 {
1781     HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1782     FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1783     return E_NOTIMPL;
1784 }
1785
1786 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1787 {
1788     nsIDOMHTMLElement *blockquote_elem, *p_elem;
1789     nsIDOMDocumentFragment *fragment;
1790     nsIDOMNode *tmp;
1791
1792     static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1793     static const PRUnichar pW[] = {'P',0};
1794
1795     TRACE("(%p)->(%p %p)\n", This, in, out);
1796
1797     if(!This->doc->nsdoc) {
1798         WARN("NULL nsdoc\n");
1799         return E_NOTIMPL;
1800     }
1801
1802     create_nselem(This->doc, blockquoteW, &blockquote_elem);
1803     create_nselem(This->doc, pW, &p_elem);
1804
1805     nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1806     nsIDOMElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1807     nsIDOMDocumentFragment_Release(fragment);
1808     nsIDOMNode_Release(tmp);
1809
1810     nsIDOMElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1811     nsIDOMElement_Release(p_elem);
1812     nsIDOMNode_Release(tmp);
1813
1814     nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1815     nsIDOMElement_Release(blockquote_elem);
1816
1817     return S_OK;
1818 }
1819
1820 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1821         DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1822 {
1823     HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1824
1825     TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1826           nCmdexecopt, pvaIn, pvaOut);
1827
1828     if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1829         switch(nCmdID) {
1830         case IDM_INDENT:
1831             return exec_indent(This, pvaIn, pvaOut);
1832         default:
1833             FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1834         }
1835     }else {
1836         FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1837     }
1838
1839     return E_NOTIMPL;
1840 }
1841
1842 #undef OLECMDTRG_THIS
1843
1844 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1845     RangeCommandTarget_QueryInterface,
1846     RangeCommandTarget_AddRef,
1847     RangeCommandTarget_Release,
1848     RangeCommandTarget_QueryStatus,
1849     RangeCommandTarget_Exec
1850 };
1851
1852 HRESULT HTMLTxtRange_Create(HTMLDocumentNode *doc, nsIDOMRange *nsrange, IHTMLTxtRange **p)
1853 {
1854     HTMLTxtRange *ret;
1855
1856     ret = heap_alloc(sizeof(HTMLTxtRange));
1857     if(!ret)
1858         return E_OUTOFMEMORY;
1859
1860     ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
1861     ret->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
1862     ret->ref = 1;
1863
1864     if(nsrange)
1865         nsIDOMRange_AddRef(nsrange);
1866     ret->nsrange = nsrange;
1867
1868     ret->doc = doc;
1869     list_add_head(&doc->range_list, &ret->entry);
1870
1871     *p = HTMLTXTRANGE(ret);
1872     return S_OK;
1873 }
1874
1875 void detach_ranges(HTMLDocumentNode *This)
1876 {
1877     HTMLTxtRange *iter;
1878
1879     LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1880         iter->doc = NULL;
1881     }
1882 }