urlmon: Added support for javascript URIs.
[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     IHTMLTxtRange     IHTMLTxtRange_iface;
41     IOleCommandTarget IOleCommandTarget_iface;
42
43     LONG ref;
44
45     nsIDOMRange *nsrange;
46     HTMLDocumentNode *doc;
47
48     struct list entry;
49 } HTMLTxtRange;
50
51 typedef struct {
52     WCHAR *buf;
53     DWORD len;
54     DWORD size;
55 } wstrbuf_t;
56
57 typedef struct {
58     PRUint16 type;
59     nsIDOMNode *node;
60     PRUint32 off;
61     nsAString str;
62     const PRUnichar *p;
63 } dompos_t;
64
65 typedef enum {
66     RU_UNKNOWN,
67     RU_CHAR,
68     RU_WORD,
69     RU_SENTENCE,
70     RU_TEXTEDIT
71 } range_unit_t;
72
73 static HTMLTxtRange *get_range_object(HTMLDocumentNode *doc, IHTMLTxtRange *iface)
74 {
75     HTMLTxtRange *iter;
76
77     LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
78         if(&iter->IHTMLTxtRange_iface == iface)
79             return iter;
80     }
81
82     ERR("Could not find range in document\n");
83     return NULL;
84 }
85
86 static range_unit_t string_to_unit(LPCWSTR str)
87 {
88     static const WCHAR characterW[] =
89         {'c','h','a','r','a','c','t','e','r',0};
90     static const WCHAR wordW[] =
91         {'w','o','r','d',0};
92     static const WCHAR sentenceW[] =
93         {'s','e','n','t','e','n','c','e',0};
94     static const WCHAR texteditW[] =
95         {'t','e','x','t','e','d','i','t',0};
96
97     if(!strcmpiW(str, characterW))  return RU_CHAR;
98     if(!strcmpiW(str, wordW))       return RU_WORD;
99     if(!strcmpiW(str, sentenceW))   return RU_SENTENCE;
100     if(!strcmpiW(str, texteditW))   return RU_TEXTEDIT;
101
102     return RU_UNKNOWN;
103 }
104
105 static int string_to_nscmptype(LPCWSTR str)
106 {
107     static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
108     static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
109     static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
110     static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
111
112     if(!strcmpiW(str, seW))  return NS_START_TO_END;
113     if(!strcmpiW(str, ssW))  return NS_START_TO_START;
114     if(!strcmpiW(str, esW))  return NS_END_TO_START;
115     if(!strcmpiW(str, eeW))  return NS_END_TO_END;
116
117     return -1;
118 }
119
120 static PRUint16 get_node_type(nsIDOMNode *node)
121 {
122     PRUint16 type = 0;
123
124     if(node)
125         nsIDOMNode_GetNodeType(node, &type);
126
127     return type;
128 }
129
130 static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
131 {
132     nsIDOMElement *elem;
133     nsAString tag_str;
134     const PRUnichar *tag;
135     BOOL ret = FALSE;
136     nsresult nsres;
137
138     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
139     if(NS_FAILED(nsres))
140         return FALSE;
141
142     nsAString_Init(&tag_str, NULL);
143     nsIDOMElement_GetTagName(elem, &tag_str);
144     nsIDOMElement_Release(elem);
145     nsAString_GetData(&tag_str, &tag);
146
147     ret = !strcmpiW(tag, istag);
148
149     nsAString_Finish(&tag_str);
150
151     return ret;
152 }
153
154 static BOOL is_space_elem(nsIDOMNode *node)
155 {
156     nsIDOMElement *elem;
157     nsAString tag_str;
158     const PRUnichar *tag;
159     BOOL ret = FALSE;
160     nsresult nsres;
161
162     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
163     if(NS_FAILED(nsres))
164         return FALSE;
165
166     nsAString_Init(&tag_str, NULL);
167     nsIDOMElement_GetTagName(elem, &tag_str);
168     nsIDOMElement_Release(elem);
169     nsAString_GetData(&tag_str, &tag);
170
171     ret = !strcmpiW(tag, brW) || !strcmpiW(tag, hrW);
172
173     nsAString_Finish(&tag_str);
174
175     return ret;
176 }
177
178 static inline BOOL wstrbuf_init(wstrbuf_t *buf)
179 {
180     buf->len = 0;
181     buf->size = 16;
182     buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
183     if (!buf->buf) return FALSE;
184     *buf->buf = 0;
185     return TRUE;
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     if (!wstrbuf_init(&buf))
553         return E_OUTOFMEMORY;
554     wstrbuf_append_node_rec(&buf, node->nsnode);
555     if(buf.buf) {
556         *ret = SysAllocString(buf.buf);
557         if(!*ret)
558             hres = E_OUTOFMEMORY;
559     } else {
560         *ret = NULL;
561     }
562     wstrbuf_finish(&buf);
563
564     if(SUCCEEDED(hres))
565         TRACE("ret %s\n", debugstr_w(*ret));
566     return hres;
567 }
568
569 static WCHAR get_pos_char(const dompos_t *pos)
570 {
571     switch(pos->type) {
572     case TEXT_NODE:
573         return pos->p[pos->off];
574     case ELEMENT_NODE:
575         if(is_space_elem(pos->node))
576             return '\n';
577     }
578
579     return 0;
580 }
581
582 static void end_space(const dompos_t *pos, dompos_t *new_pos)
583 {
584     const WCHAR *p;
585
586     *new_pos = *pos;
587     dompos_addref(new_pos);
588
589     if(pos->type != TEXT_NODE)
590         return;
591
592     p = new_pos->p+new_pos->off;
593
594     if(!*p || !isspace(*p))
595         return;
596
597     while(p[1] && isspace(p[1]))
598         p++;
599
600     new_pos->off = p - new_pos->p;
601 }
602
603 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
604 {
605     nsIDOMNode *iter, *tmp;
606     dompos_t last_space, tmp_pos;
607     const WCHAR *p;
608     WCHAR cspace = 0;
609
610     if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off]) {
611         p = pos->p+pos->off;
612
613         if(isspace(*p))
614             while(isspaceW(*++p));
615         else
616             p++;
617
618         if(*p && isspaceW(*p)) {
619             cspace = ' ';
620             while(p[1] && isspaceW(p[1]))
621                 p++;
622         }
623
624         if(*p) {
625             *new_pos = *pos;
626             new_pos->off = p - pos->p;
627             dompos_addref(new_pos);
628
629             return cspace ? cspace : *p;
630         }else {
631             last_space = *pos;
632             last_space.off = p - pos->p;
633             dompos_addref(&last_space);
634         }
635     }
636
637     iter = next_node(pos->node);
638
639     while(iter) {
640         switch(get_node_type(iter)) {
641         case TEXT_NODE:
642             tmp_pos.node = iter;
643             tmp_pos.type = TEXT_NODE;
644             tmp_pos.off = 0;
645             dompos_addref(&tmp_pos);
646
647             p = tmp_pos.p;
648
649             if(!*p) {
650                 dompos_release(&tmp_pos);
651                 break;
652             }else if(isspaceW(*p)) {
653                 if(cspace)
654                     dompos_release(&last_space);
655                 else
656                     cspace = ' ';
657
658                 while(p[1] && isspaceW(p[1]))
659                       p++;
660
661                 tmp_pos.off = p-tmp_pos.p;
662
663                 if(!p[1]) {
664                     last_space = tmp_pos;
665                     break;
666                 }
667
668                 *new_pos = tmp_pos;
669                 nsIDOMNode_Release(iter);
670                 return cspace;
671             }else if(cspace) {
672                 *new_pos = last_space;
673                 dompos_release(&tmp_pos);
674                 nsIDOMNode_Release(iter);
675
676                 return cspace;
677             }else if(*p) {
678                 tmp_pos.off = 0;
679                 *new_pos = tmp_pos;
680             }
681
682             nsIDOMNode_Release(iter);
683             return *p;
684
685         case ELEMENT_NODE:
686             if(is_elem_tag(iter, brW)) {
687                 if(cspace)
688                     dompos_release(&last_space);
689                 cspace = '\n';
690
691                 nsIDOMNode_AddRef(iter);
692                 last_space.node = iter;
693                 last_space.type = ELEMENT_NODE;
694                 last_space.off = 0;
695                 last_space.p = NULL;
696             }else if(is_elem_tag(iter, hrW)) {
697                 if(cspace) {
698                     *new_pos = last_space;
699                     nsIDOMNode_Release(iter);
700                     return cspace;
701                 }
702
703                 new_pos->node = iter;
704                 new_pos->type = ELEMENT_NODE;
705                 new_pos->off = 0;
706                 new_pos->p = NULL;
707                 return '\n';
708             }
709         }
710
711         tmp = iter;
712         iter = next_node(iter);
713         nsIDOMNode_Release(tmp);
714     }
715
716     if(cspace) {
717         *new_pos = last_space;
718     }else {
719         *new_pos = *pos;
720         dompos_addref(new_pos);
721     }
722
723     return cspace;
724 }
725
726 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
727 {
728     nsIDOMNode *iter, *tmp;
729     const WCHAR *p;
730     BOOL skip_space = FALSE;
731
732     if(pos->type == TEXT_NODE && isspaceW(pos->p[pos->off]))
733         skip_space = TRUE;
734
735     if(pos->type == TEXT_NODE && pos->off) {
736         p = pos->p+pos->off-1;
737
738         if(skip_space) {
739             while(p >= pos->p && isspace(*p))
740                 p--;
741         }
742
743         if(p >= pos->p) {
744             *new_pos = *pos;
745             new_pos->off = p-pos->p;
746             dompos_addref(new_pos);
747             return new_pos->p[new_pos->off];
748         }
749     }
750
751     iter = prev_node(This, pos->node);
752
753     while(iter) {
754         switch(get_node_type(iter)) {
755         case TEXT_NODE: {
756             dompos_t tmp_pos;
757
758             tmp_pos.node = iter;
759             tmp_pos.type = TEXT_NODE;
760             tmp_pos.off = 0;
761             dompos_addref(&tmp_pos);
762
763             p = tmp_pos.p + strlenW(tmp_pos.p)-1;
764
765             if(skip_space) {
766                 while(p >= tmp_pos.p && isspaceW(*p))
767                     p--;
768             }
769
770             if(p < tmp_pos.p) {
771                 dompos_release(&tmp_pos);
772                 break;
773             }
774
775             tmp_pos.off = p-tmp_pos.p;
776             *new_pos = tmp_pos;
777             nsIDOMNode_Release(iter);
778             return *p;
779         }
780
781         case ELEMENT_NODE:
782             if(is_elem_tag(iter, brW)) {
783                 if(skip_space) {
784                     skip_space = FALSE;
785                     break;
786                 }
787             }else if(!is_elem_tag(iter, hrW)) {
788                 break;
789             }
790
791             new_pos->node = iter;
792             new_pos->type = ELEMENT_NODE;
793             new_pos->off = 0;
794             new_pos->p = NULL;
795             return '\n';
796         }
797
798         tmp = iter;
799         iter = prev_node(This, iter);
800         nsIDOMNode_Release(tmp);
801     }
802
803     *new_pos = *pos;
804     dompos_addref(new_pos);
805     return 0;
806 }
807
808 static LONG move_next_chars(LONG cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
809         BOOL *bounded, dompos_t *new_pos)
810 {
811     dompos_t iter, tmp;
812     LONG ret = 0;
813     WCHAR c;
814
815     if(bounded)
816         *bounded = FALSE;
817
818     if(col)
819         ret++;
820
821     if(ret >= cnt) {
822         end_space(pos, new_pos);
823         return ret;
824     }
825
826     c = next_char(pos, &iter);
827     ret++;
828
829     while(ret < cnt) {
830         tmp = iter;
831         c = next_char(&tmp, &iter);
832         dompos_release(&tmp);
833         if(!c)
834             break;
835
836         ret++;
837         if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
838             *bounded = TRUE;
839             ret++;
840         }
841     }
842
843     *new_pos = iter;
844     return ret;
845 }
846
847 static LONG move_prev_chars(HTMLTxtRange *This, LONG cnt, const dompos_t *pos, BOOL end,
848         const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
849 {
850     dompos_t iter, tmp;
851     LONG ret = 0;
852     BOOL prev_eq = FALSE;
853     WCHAR c;
854
855     if(bounded)
856         *bounded = FALSE;
857
858     c = prev_char(This, pos, &iter);
859     if(c)
860         ret++;
861
862     while(c && ret < cnt) {
863         tmp = iter;
864         c = prev_char(This, &tmp, &iter);
865         dompos_release(&tmp);
866         if(!c) {
867             if(end)
868                 ret++;
869             break;
870         }
871
872         ret++;
873
874         if(prev_eq) {
875             *bounded = TRUE;
876             ret++;
877         }
878
879         prev_eq = bound_pos && dompos_cmp(&iter, bound_pos);
880     }
881
882     *new_pos = iter;
883     return ret;
884 }
885
886 static LONG find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
887 {
888     dompos_t iter, tmp;
889     WCHAR c;
890
891     c = prev_char(This, pos, &iter);
892     if(!c || (first_space && isspaceW(c))) {
893         *ret = iter;
894         return FALSE;
895     }
896
897     while(1) {
898         tmp = iter;
899         c = prev_char(This, &tmp, &iter);
900         if(!c || isspaceW(c)) {
901             dompos_release(&iter);
902             break;
903         }
904         dompos_release(&tmp);
905     }
906
907     *ret = tmp;
908     return TRUE;
909 }
910
911 static int find_word_end(const dompos_t *pos, dompos_t *ret)
912 {
913     dompos_t iter, tmp;
914     int cnt = 1;
915     WCHAR c;
916     c = get_pos_char(pos);
917     if(isspaceW(c)) {
918         *ret = *pos;
919         dompos_addref(ret);
920         return 0;
921     }
922
923     c = next_char(pos, &iter);
924     if(!c) {
925         *ret = iter;
926         return 0;
927     }
928     if(c == '\n') {
929         *ret = *pos;
930         dompos_addref(ret);
931         return 0;
932     }
933
934     while(c && !isspaceW(c)) {
935         tmp = iter;
936         c = next_char(&tmp, &iter);
937         if(c == '\n') {
938             dompos_release(&iter);
939             iter = tmp;
940         }else {
941             cnt++;
942             dompos_release(&tmp);
943         }
944     }
945
946     *ret = iter;
947     return cnt;
948 }
949
950 static LONG move_next_words(LONG cnt, const dompos_t *pos, dompos_t *new_pos)
951 {
952     dompos_t iter, tmp;
953     LONG ret = 0;
954     WCHAR c;
955
956     c = get_pos_char(pos);
957     if(isspaceW(c)) {
958         end_space(pos, &iter);
959         ret++;
960     }else {
961         c = next_char(pos, &iter);
962         if(c && isspaceW(c))
963             ret++;
964     }
965
966     while(c && ret < cnt) {
967         tmp = iter;
968         c = next_char(&tmp, &iter);
969         dompos_release(&tmp);
970         if(isspaceW(c))
971             ret++;
972     }
973
974     *new_pos = iter;
975     return ret;
976 }
977
978 static LONG move_prev_words(HTMLTxtRange *This, LONG cnt, const dompos_t *pos, dompos_t *new_pos)
979 {
980     dompos_t iter, tmp;
981     LONG ret = 0;
982
983     iter = *pos;
984     dompos_addref(&iter);
985
986     while(ret < cnt) {
987         if(!find_prev_space(This, &iter, FALSE, &tmp))
988             break;
989
990         dompos_release(&iter);
991         iter = tmp;
992         ret++;
993     }
994
995     *new_pos = iter;
996     return ret;
997 }
998
999 static inline HTMLTxtRange *impl_from_IHTMLTxtRange(IHTMLTxtRange *iface)
1000 {
1001     return CONTAINING_RECORD(iface, HTMLTxtRange, IHTMLTxtRange_iface);
1002 }
1003
1004 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
1005 {
1006     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1007
1008     *ppv = NULL;
1009
1010     if(IsEqualGUID(&IID_IUnknown, riid)) {
1011         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1012         *ppv = &This->IHTMLTxtRange_iface;
1013     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1014         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1015         *ppv = &This->IHTMLTxtRange_iface;
1016     }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
1017         TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
1018         *ppv = &This->IHTMLTxtRange_iface;
1019     }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1020         TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
1021         *ppv = &This->IOleCommandTarget_iface;
1022     }
1023
1024     if(*ppv) {
1025         IUnknown_AddRef((IUnknown*)*ppv);
1026         return S_OK;
1027     }
1028
1029     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1030     return E_NOINTERFACE;
1031 }
1032
1033 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
1034 {
1035     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1036     LONG ref = InterlockedIncrement(&This->ref);
1037
1038     TRACE("(%p) ref=%d\n", This, ref);
1039
1040     return ref;
1041 }
1042
1043 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1044 {
1045     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1046     LONG ref = InterlockedDecrement(&This->ref);
1047
1048     TRACE("(%p) ref=%d\n", This, ref);
1049
1050     if(!ref) {
1051         if(This->nsrange)
1052             nsISelection_Release(This->nsrange);
1053         if(This->doc)
1054             list_remove(&This->entry);
1055         heap_free(This);
1056     }
1057
1058     return ref;
1059 }
1060
1061 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1062 {
1063     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1064     FIXME("(%p)->(%p)\n", This, pctinfo);
1065     return E_NOTIMPL;
1066 }
1067
1068 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1069                                                LCID lcid, ITypeInfo **ppTInfo)
1070 {
1071     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1072     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1073     return E_NOTIMPL;
1074 }
1075
1076 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1077                                                  LPOLESTR *rgszNames, UINT cNames,
1078                                                  LCID lcid, DISPID *rgDispId)
1079 {
1080     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1081     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1082           lcid, rgDispId);
1083     return E_NOTIMPL;
1084 }
1085
1086 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1087                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1088                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1089 {
1090     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1091     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1092           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1093     return E_NOTIMPL;
1094 }
1095
1096 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1097 {
1098     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1099
1100     TRACE("(%p)->(%p)\n", This, p);
1101
1102     *p = NULL;
1103
1104     if(This->nsrange) {
1105         nsIDOMDocumentFragment *fragment;
1106         nsresult nsres;
1107
1108         nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1109         if(NS_SUCCEEDED(nsres)) {
1110             const PRUnichar *nstext;
1111             nsAString nsstr;
1112
1113             nsAString_Init(&nsstr, NULL);
1114             nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1115             nsIDOMDocumentFragment_Release(fragment);
1116
1117             nsAString_GetData(&nsstr, &nstext);
1118             *p = SysAllocString(nstext);
1119
1120             nsAString_Finish(&nsstr);
1121         }
1122     }
1123
1124     if(!*p) {
1125         const WCHAR emptyW[] = {0};
1126         *p = SysAllocString(emptyW);
1127     }
1128
1129     TRACE("return %s\n", debugstr_w(*p));
1130     return S_OK;
1131 }
1132
1133 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1134 {
1135     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1136     nsIDOMText *text_node;
1137     nsAString text_str;
1138     nsresult nsres;
1139
1140     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1141
1142     if(!This->doc)
1143         return MSHTML_E_NODOC;
1144
1145     nsAString_InitDepend(&text_str, v);
1146     nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc->nsdoc, &text_str, &text_node);
1147     nsAString_Finish(&text_str);
1148     if(NS_FAILED(nsres)) {
1149         ERR("CreateTextNode failed: %08x\n", nsres);
1150         return S_OK;
1151     }
1152     nsres = nsIDOMRange_DeleteContents(This->nsrange);
1153     if(NS_FAILED(nsres))
1154         ERR("DeleteContents failed: %08x\n", nsres);
1155
1156     nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1157     if(NS_FAILED(nsres))
1158         ERR("InsertNode failed: %08x\n", nsres);
1159
1160     nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1161     if(NS_FAILED(nsres))
1162         ERR("SetEndAfter failed: %08x\n", nsres);
1163
1164     return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_FALSE);
1165 }
1166
1167 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1168 {
1169     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1170     wstrbuf_t buf;
1171
1172     TRACE("(%p)->(%p)\n", This, p);
1173
1174     *p = NULL;
1175     if(!This->nsrange)
1176         return S_OK;
1177
1178     if (!wstrbuf_init(&buf))
1179         return E_OUTOFMEMORY;
1180     range_to_string(This, &buf);
1181     if (buf.buf)
1182         *p = SysAllocString(buf.buf);
1183     wstrbuf_finish(&buf);
1184
1185     TRACE("ret %s\n", debugstr_w(*p));
1186     return S_OK;
1187 }
1188
1189 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1190 {
1191     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1192     nsIDOMNode *nsnode, *tmp;
1193     HTMLDOMNode *node;
1194     HRESULT hres;
1195
1196     TRACE("(%p)->(%p)\n", This, parent);
1197
1198     nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1199     while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1200         nsIDOMNode_GetParentNode(nsnode, &tmp);
1201         nsIDOMNode_Release(nsnode);
1202         nsnode = tmp;
1203     }
1204
1205     if(!nsnode) {
1206         *parent = NULL;
1207         return S_OK;
1208     }
1209
1210     hres = get_node(This->doc, nsnode, TRUE, &node);
1211     nsIDOMNode_Release(nsnode);
1212     if(FAILED(hres))
1213         return hres;
1214
1215     return IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)parent);
1216 }
1217
1218 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1219 {
1220     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1221     nsIDOMRange *nsrange = NULL;
1222     HRESULT hres;
1223
1224     TRACE("(%p)->(%p)\n", This, Duplicate);
1225
1226     nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1227     hres = HTMLTxtRange_Create(This->doc, nsrange, Duplicate);
1228     nsIDOMRange_Release(nsrange);
1229
1230     return hres;
1231 }
1232
1233 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1234         VARIANT_BOOL *InRange)
1235 {
1236     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1237     HTMLTxtRange *src_range;
1238     PRInt16 nsret = 0;
1239     nsresult nsres;
1240
1241     TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1242
1243     *InRange = VARIANT_FALSE;
1244
1245     src_range = get_range_object(This->doc, Range);
1246     if(!src_range)
1247         return E_FAIL;
1248
1249     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1250             src_range->nsrange, &nsret);
1251     if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1252         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1253                 src_range->nsrange, &nsret);
1254         if(NS_SUCCEEDED(nsres) && nsret >= 0)
1255             *InRange = VARIANT_TRUE;
1256     }
1257
1258     if(NS_FAILED(nsres))
1259         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1260
1261     return S_OK;
1262 }
1263
1264 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1265         VARIANT_BOOL *IsEqual)
1266 {
1267     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1268     HTMLTxtRange *src_range;
1269     PRInt16 nsret = 0;
1270     nsresult nsres;
1271
1272     TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1273
1274     *IsEqual = VARIANT_FALSE;
1275
1276     src_range = get_range_object(This->doc, Range);
1277     if(!src_range)
1278         return E_FAIL;
1279
1280     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1281             src_range->nsrange, &nsret);
1282     if(NS_SUCCEEDED(nsres) && !nsret) {
1283         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1284                 src_range->nsrange, &nsret);
1285         if(NS_SUCCEEDED(nsres) && !nsret)
1286             *IsEqual = VARIANT_TRUE;
1287     }
1288
1289     if(NS_FAILED(nsres))
1290         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1291
1292     return S_OK;
1293 }
1294
1295 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1296 {
1297     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1298     FIXME("(%p)->(%x)\n", This, fStart);
1299     return E_NOTIMPL;
1300 }
1301
1302 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1303 {
1304     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1305
1306     TRACE("(%p)->(%x)\n", This, Start);
1307
1308     nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1309     return S_OK;
1310 }
1311
1312 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1313 {
1314     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1315     range_unit_t unit;
1316
1317     TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1318
1319     unit = string_to_unit(Unit);
1320     if(unit == RU_UNKNOWN)
1321         return E_INVALIDARG;
1322
1323     *Success = VARIANT_FALSE;
1324
1325     switch(unit) {
1326     case RU_WORD: {
1327         dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1328         PRBool collapsed;
1329
1330         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1331
1332         get_cur_pos(This, TRUE, &start_pos);
1333         get_cur_pos(This, FALSE, &end_pos);
1334
1335         if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1336             set_range_pos(This, FALSE, &new_end_pos);
1337             *Success = VARIANT_TRUE;
1338         }
1339
1340         if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1341             if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1342                 set_range_pos(This, TRUE, &new_start_pos);
1343                 *Success = VARIANT_TRUE;
1344             }
1345             dompos_release(&new_start_pos);
1346         }
1347
1348         dompos_release(&new_end_pos);
1349         dompos_release(&end_pos);
1350         dompos_release(&start_pos);
1351
1352         break;
1353     }
1354
1355     case RU_TEXTEDIT: {
1356         nsIDOMHTMLElement *nsbody = NULL;
1357         nsresult nsres;
1358
1359         nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
1360         if(NS_FAILED(nsres) || !nsbody) {
1361             ERR("Could not get body: %08x\n", nsres);
1362             break;
1363         }
1364
1365         nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1366         nsIDOMHTMLElement_Release(nsbody);
1367         if(NS_FAILED(nsres)) {
1368             ERR("Collapse failed: %08x\n", nsres);
1369             break;
1370         }
1371
1372         *Success = VARIANT_TRUE;
1373         break;
1374     }
1375
1376     default:
1377         FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1378     }
1379
1380     return S_OK;
1381 }
1382
1383 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1384         LONG Count, LONG *ActualCount)
1385 {
1386     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1387     range_unit_t unit;
1388
1389     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1390
1391     unit = string_to_unit(Unit);
1392     if(unit == RU_UNKNOWN)
1393         return E_INVALIDARG;
1394
1395     if(!Count) {
1396         *ActualCount = 0;
1397         return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1398     }
1399
1400     switch(unit) {
1401     case RU_CHAR: {
1402         dompos_t cur_pos, new_pos;
1403
1404         get_cur_pos(This, TRUE, &cur_pos);
1405
1406         if(Count > 0) {
1407             *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1408             set_range_pos(This, FALSE, &new_pos);
1409             dompos_release(&new_pos);
1410
1411             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1412         }else {
1413             *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1414             set_range_pos(This, TRUE, &new_pos);
1415             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1416             dompos_release(&new_pos);
1417         }
1418
1419         dompos_release(&cur_pos);
1420         break;
1421     }
1422
1423     case RU_WORD: {
1424         dompos_t cur_pos, new_pos;
1425
1426         get_cur_pos(This, TRUE, &cur_pos);
1427
1428         if(Count > 0) {
1429             *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1430             set_range_pos(This, FALSE, &new_pos);
1431             dompos_release(&new_pos);
1432             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1433         }else {
1434             *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1435             set_range_pos(This, TRUE, &new_pos);
1436             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1437             dompos_release(&new_pos);
1438         }
1439
1440         dompos_release(&cur_pos);
1441         break;
1442     }
1443
1444     default:
1445         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1446     }
1447
1448     TRACE("ret %d\n", *ActualCount);
1449     return S_OK;
1450 }
1451
1452 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1453         LONG Count, LONG *ActualCount)
1454 {
1455     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1456     range_unit_t unit;
1457
1458     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1459
1460     unit = string_to_unit(Unit);
1461     if(unit == RU_UNKNOWN)
1462         return E_INVALIDARG;
1463
1464     if(!Count) {
1465         *ActualCount = 0;
1466         return S_OK;
1467     }
1468
1469     switch(unit) {
1470     case RU_CHAR: {
1471         dompos_t start_pos, end_pos, new_pos;
1472         PRBool collapsed;
1473
1474         get_cur_pos(This, TRUE, &start_pos);
1475         get_cur_pos(This, FALSE, &end_pos);
1476         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1477
1478         if(Count > 0) {
1479             BOOL bounded;
1480
1481             *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1482             set_range_pos(This, !bounded, &new_pos);
1483             if(bounded)
1484                 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1485         }else {
1486             *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1487             set_range_pos(This, TRUE, &new_pos);
1488         }
1489
1490         dompos_release(&start_pos);
1491         dompos_release(&end_pos);
1492         dompos_release(&new_pos);
1493         break;
1494     }
1495
1496     default:
1497         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1498     }
1499
1500     return S_OK;
1501 }
1502
1503 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1504         LONG Count, LONG *ActualCount)
1505 {
1506     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1507     range_unit_t unit;
1508
1509     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1510
1511     unit = string_to_unit(Unit);
1512     if(unit == RU_UNKNOWN)
1513         return E_INVALIDARG;
1514
1515     if(!Count) {
1516         *ActualCount = 0;
1517         return S_OK;
1518     }
1519
1520     switch(unit) {
1521     case RU_CHAR: {
1522         dompos_t start_pos, end_pos, new_pos;
1523         PRBool collapsed;
1524
1525         get_cur_pos(This, TRUE, &start_pos);
1526         get_cur_pos(This, FALSE, &end_pos);
1527         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1528
1529         if(Count > 0) {
1530             *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1531             set_range_pos(This, FALSE, &new_pos);
1532         }else {
1533             BOOL bounded;
1534
1535             *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1536             set_range_pos(This, bounded, &new_pos);
1537             if(bounded)
1538                 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1539         }
1540
1541         dompos_release(&start_pos);
1542         dompos_release(&end_pos);
1543         dompos_release(&new_pos);
1544         break;
1545     }
1546
1547     default:
1548         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1549     }
1550
1551     return S_OK;
1552 }
1553
1554 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1555 {
1556     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1557     nsISelection *nsselection;
1558     nsresult nsres;
1559
1560     TRACE("(%p)\n", This);
1561
1562     nsres = nsIDOMWindow_GetSelection(This->doc->basedoc.window->nswindow, &nsselection);
1563     if(NS_FAILED(nsres)) {
1564         ERR("GetSelection failed: %08x\n", nsres);
1565         return E_FAIL;
1566     }
1567
1568     nsISelection_RemoveAllRanges(nsselection);
1569     nsISelection_AddRange(nsselection, This->nsrange);
1570     nsISelection_Release(nsselection);
1571     return S_OK;
1572 }
1573
1574 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1575 {
1576     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1577     FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1578     return E_NOTIMPL;
1579 }
1580
1581 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1582 {
1583     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1584     FIXME("(%p)->(%p)\n", This, element);
1585     return E_NOTIMPL;
1586 }
1587
1588 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1589         IHTMLTxtRange *SourceRange)
1590 {
1591     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1592     FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1593     return E_NOTIMPL;
1594 }
1595
1596 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1597         IHTMLTxtRange *SourceRange, LONG *ret)
1598 {
1599     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1600     HTMLTxtRange *src_range;
1601     PRInt16 nsret = 0;
1602     int nscmpt;
1603     nsresult nsres;
1604
1605     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1606
1607     nscmpt = string_to_nscmptype(how);
1608     if(nscmpt == -1)
1609         return E_INVALIDARG;
1610
1611     src_range = get_range_object(This->doc, SourceRange);
1612     if(!src_range)
1613         return E_FAIL;
1614
1615     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1616     if(NS_FAILED(nsres))
1617         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1618
1619     *ret = nsret;
1620     return S_OK;
1621 }
1622
1623 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1624         LONG count, LONG Flags, VARIANT_BOOL *Success)
1625 {
1626     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1627     FIXME("(%p)->(%s %d %08x %p)\n", This, debugstr_w(String), count, Flags, Success);
1628     return E_NOTIMPL;
1629 }
1630
1631 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, LONG x, LONG y)
1632 {
1633     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1634     FIXME("(%p)->(%d %d)\n", This, x, y);
1635     return E_NOTIMPL;
1636 }
1637
1638 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1639 {
1640     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1641     FIXME("(%p)->(%p)\n", This, Bookmark);
1642     return E_NOTIMPL;
1643 }
1644
1645 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1646         VARIANT_BOOL *Success)
1647 {
1648     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1649     FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1650     return E_NOTIMPL;
1651 }
1652
1653 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1654         VARIANT_BOOL *pfRet)
1655 {
1656     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1657     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1658     return E_NOTIMPL;
1659 }
1660
1661 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1662         VARIANT_BOOL *pfRet)
1663 {
1664     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1665     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1666     return E_NOTIMPL;
1667 }
1668
1669 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1670         VARIANT_BOOL *pfRet)
1671 {
1672     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1673     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1674     return E_NOTIMPL;
1675 }
1676
1677 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1678         VARIANT_BOOL *pfRet)
1679 {
1680     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1681     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1682     return E_NOTIMPL;
1683 }
1684
1685 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1686         BSTR *pcmdText)
1687 {
1688     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1689     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1690     return E_NOTIMPL;
1691 }
1692
1693 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1694         VARIANT *pcmdValue)
1695 {
1696     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1697     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1698     return E_NOTIMPL;
1699 }
1700
1701 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1702         VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1703 {
1704     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1705     FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1706     return E_NOTIMPL;
1707 }
1708
1709 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1710         VARIANT_BOOL *pfRet)
1711 {
1712     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1713     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1714     return E_NOTIMPL;
1715 }
1716
1717 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1718     HTMLTxtRange_QueryInterface,
1719     HTMLTxtRange_AddRef,
1720     HTMLTxtRange_Release,
1721     HTMLTxtRange_GetTypeInfoCount,
1722     HTMLTxtRange_GetTypeInfo,
1723     HTMLTxtRange_GetIDsOfNames,
1724     HTMLTxtRange_Invoke,
1725     HTMLTxtRange_get_htmlText,
1726     HTMLTxtRange_put_text,
1727     HTMLTxtRange_get_text,
1728     HTMLTxtRange_parentElement,
1729     HTMLTxtRange_duplicate,
1730     HTMLTxtRange_inRange,
1731     HTMLTxtRange_isEqual,
1732     HTMLTxtRange_scrollIntoView,
1733     HTMLTxtRange_collapse,
1734     HTMLTxtRange_expand,
1735     HTMLTxtRange_move,
1736     HTMLTxtRange_moveStart,
1737     HTMLTxtRange_moveEnd,
1738     HTMLTxtRange_select,
1739     HTMLTxtRange_pasteHTML,
1740     HTMLTxtRange_moveToElementText,
1741     HTMLTxtRange_setEndPoint,
1742     HTMLTxtRange_compareEndPoints,
1743     HTMLTxtRange_findText,
1744     HTMLTxtRange_moveToPoint,
1745     HTMLTxtRange_getBookmark,
1746     HTMLTxtRange_moveToBookmark,
1747     HTMLTxtRange_queryCommandSupported,
1748     HTMLTxtRange_queryCommandEnabled,
1749     HTMLTxtRange_queryCommandState,
1750     HTMLTxtRange_queryCommandIndeterm,
1751     HTMLTxtRange_queryCommandText,
1752     HTMLTxtRange_queryCommandValue,
1753     HTMLTxtRange_execCommand,
1754     HTMLTxtRange_execCommandShowHelp
1755 };
1756
1757 static inline HTMLTxtRange *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
1758 {
1759     return CONTAINING_RECORD(iface, HTMLTxtRange, IOleCommandTarget_iface);
1760 }
1761
1762 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1763 {
1764     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1765     return IHTMLTxtRange_QueryInterface(&This->IHTMLTxtRange_iface, riid, ppv);
1766 }
1767
1768 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1769 {
1770     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1771     return IHTMLTxtRange_AddRef(&This->IHTMLTxtRange_iface);
1772 }
1773
1774 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1775 {
1776     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1777     return IHTMLTxtRange_Release(&This->IHTMLTxtRange_iface);
1778 }
1779
1780 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1781         ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1782 {
1783     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1784     FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1785     return E_NOTIMPL;
1786 }
1787
1788 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1789 {
1790     nsIDOMHTMLElement *blockquote_elem, *p_elem;
1791     nsIDOMDocumentFragment *fragment;
1792     nsIDOMNode *tmp;
1793
1794     static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1795     static const PRUnichar pW[] = {'P',0};
1796
1797     TRACE("(%p)->(%p %p)\n", This, in, out);
1798
1799     if(!This->doc->nsdoc) {
1800         WARN("NULL nsdoc\n");
1801         return E_NOTIMPL;
1802     }
1803
1804     create_nselem(This->doc, blockquoteW, &blockquote_elem);
1805     create_nselem(This->doc, pW, &p_elem);
1806
1807     nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1808     nsIDOMElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1809     nsIDOMDocumentFragment_Release(fragment);
1810     nsIDOMNode_Release(tmp);
1811
1812     nsIDOMElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1813     nsIDOMElement_Release(p_elem);
1814     nsIDOMNode_Release(tmp);
1815
1816     nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1817     nsIDOMElement_Release(blockquote_elem);
1818
1819     return S_OK;
1820 }
1821
1822 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1823         DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1824 {
1825     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1826
1827     TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1828           nCmdexecopt, pvaIn, pvaOut);
1829
1830     if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1831         switch(nCmdID) {
1832         case IDM_INDENT:
1833             return exec_indent(This, pvaIn, pvaOut);
1834         default:
1835             FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1836         }
1837     }else {
1838         FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1839     }
1840
1841     return E_NOTIMPL;
1842 }
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->IHTMLTxtRange_iface.lpVtbl = &HTMLTxtRangeVtbl;
1861     ret->IOleCommandTarget_iface.lpVtbl = &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 = &ret->IHTMLTxtRange_iface;
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 }