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