mshtml: Added noscript tag handling tests.
[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
31 #include "mshtml_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34
35 static const WCHAR brW[] = {'b','r',0};
36 static const WCHAR hrW[] = {'h','r',0};
37
38 typedef struct {
39     IHTMLTxtRange     IHTMLTxtRange_iface;
40     IOleCommandTarget IOleCommandTarget_iface;
41
42     LONG ref;
43
44     nsIDOMRange *nsrange;
45     HTMLDocumentNode *doc;
46
47     struct list entry;
48 } HTMLTxtRange;
49
50 typedef struct {
51     WCHAR *buf;
52     DWORD len;
53     DWORD size;
54 } wstrbuf_t;
55
56 typedef struct {
57     PRUint16 type;
58     nsIDOMNode *node;
59     PRUint32 off;
60     nsAString str;
61     const PRUnichar *p;
62 } dompos_t;
63
64 typedef enum {
65     RU_UNKNOWN,
66     RU_CHAR,
67     RU_WORD,
68     RU_SENTENCE,
69     RU_TEXTEDIT
70 } range_unit_t;
71
72 static HTMLTxtRange *get_range_object(HTMLDocumentNode *doc, IHTMLTxtRange *iface)
73 {
74     HTMLTxtRange *iter;
75
76     LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
77         if(&iter->IHTMLTxtRange_iface == iface)
78             return iter;
79     }
80
81     ERR("Could not find range in document\n");
82     return NULL;
83 }
84
85 static range_unit_t string_to_unit(LPCWSTR str)
86 {
87     static const WCHAR characterW[] =
88         {'c','h','a','r','a','c','t','e','r',0};
89     static const WCHAR wordW[] =
90         {'w','o','r','d',0};
91     static const WCHAR sentenceW[] =
92         {'s','e','n','t','e','n','c','e',0};
93     static const WCHAR texteditW[] =
94         {'t','e','x','t','e','d','i','t',0};
95
96     if(!strcmpiW(str, characterW))  return RU_CHAR;
97     if(!strcmpiW(str, wordW))       return RU_WORD;
98     if(!strcmpiW(str, sentenceW))   return RU_SENTENCE;
99     if(!strcmpiW(str, texteditW))   return RU_TEXTEDIT;
100
101     return RU_UNKNOWN;
102 }
103
104 static int string_to_nscmptype(LPCWSTR str)
105 {
106     static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
107     static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
108     static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
109     static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
110
111     if(!strcmpiW(str, seW))  return NS_START_TO_END;
112     if(!strcmpiW(str, ssW))  return NS_START_TO_START;
113     if(!strcmpiW(str, esW))  return NS_END_TO_START;
114     if(!strcmpiW(str, eeW))  return NS_END_TO_END;
115
116     return -1;
117 }
118
119 static PRUint16 get_node_type(nsIDOMNode *node)
120 {
121     PRUint16 type = 0;
122
123     if(node)
124         nsIDOMNode_GetNodeType(node, &type);
125
126     return type;
127 }
128
129 static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
130 {
131     nsIDOMElement *elem;
132     nsAString tag_str;
133     const PRUnichar *tag;
134     BOOL ret = FALSE;
135     nsresult nsres;
136
137     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
138     if(NS_FAILED(nsres))
139         return FALSE;
140
141     nsAString_Init(&tag_str, NULL);
142     nsIDOMElement_GetTagName(elem, &tag_str);
143     nsIDOMElement_Release(elem);
144     nsAString_GetData(&tag_str, &tag);
145
146     ret = !strcmpiW(tag, istag);
147
148     nsAString_Finish(&tag_str);
149
150     return ret;
151 }
152
153 static BOOL is_space_elem(nsIDOMNode *node)
154 {
155     nsIDOMElement *elem;
156     nsAString tag_str;
157     const PRUnichar *tag;
158     BOOL ret = FALSE;
159     nsresult nsres;
160
161     nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
162     if(NS_FAILED(nsres))
163         return FALSE;
164
165     nsAString_Init(&tag_str, NULL);
166     nsIDOMElement_GetTagName(elem, &tag_str);
167     nsIDOMElement_Release(elem);
168     nsAString_GetData(&tag_str, &tag);
169
170     ret = !strcmpiW(tag, brW) || !strcmpiW(tag, hrW);
171
172     nsAString_Finish(&tag_str);
173
174     return ret;
175 }
176
177 static inline BOOL wstrbuf_init(wstrbuf_t *buf)
178 {
179     buf->len = 0;
180     buf->size = 16;
181     buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
182     if (!buf->buf) return FALSE;
183     *buf->buf = 0;
184     return TRUE;
185 }
186
187 static inline void wstrbuf_finish(wstrbuf_t *buf)
188 {
189     heap_free(buf->buf);
190 }
191
192 static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
193 {
194     if(buf->len+len >= buf->size) {
195         buf->size = 2*buf->size+len;
196         buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
197     }
198
199     memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
200     buf->len += len;
201     buf->buf[buf->len] = 0;
202 }
203
204 static void wstrbuf_append_nodetxt(wstrbuf_t *buf, LPCWSTR str, int len)
205 {
206     const WCHAR *s = str;
207     WCHAR *d;
208
209     TRACE("%s\n", debugstr_wn(str, len));
210
211     if(buf->len+len >= buf->size) {
212         buf->size = 2*buf->size+len;
213         buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
214     }
215
216     if(buf->len && isspaceW(buf->buf[buf->len-1])) {
217         while(s < str+len && isspaceW(*s))
218             s++;
219     }
220
221     d = buf->buf+buf->len;
222     while(s < str+len) {
223         if(isspaceW(*s)) {
224             *d++ = ' ';
225             s++;
226             while(s < str+len && isspaceW(*s))
227                 s++;
228         }else {
229             *d++ = *s++;
230         }
231     }
232
233     buf->len = d - buf->buf;
234     *d = 0;
235 }
236
237 static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node)
238 {
239
240     switch(get_node_type(node)) {
241     case TEXT_NODE: {
242         nsIDOMText *nstext;
243         nsAString data_str;
244         const PRUnichar *data;
245
246         nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
247
248         nsAString_Init(&data_str, NULL);
249         nsIDOMText_GetData(nstext, &data_str);
250         nsAString_GetData(&data_str, &data);
251         wstrbuf_append_nodetxt(buf, data, strlenW(data));
252         nsAString_Finish(&data_str);
253
254         nsIDOMText_Release(nstext);
255
256         break;
257     }
258     case ELEMENT_NODE:
259         if(is_elem_tag(node, brW)) {
260             static const WCHAR endlW[] = {'\r','\n'};
261             wstrbuf_append_len(buf, endlW, 2);
262         }else if(is_elem_tag(node, hrW)) {
263             static const WCHAR endl2W[] = {'\r','\n','\r','\n'};
264             wstrbuf_append_len(buf, endl2W, 4);
265         }
266     }
267 }
268
269 static void wstrbuf_append_node_rec(wstrbuf_t *buf, nsIDOMNode *node)
270 {
271     nsIDOMNode *iter, *tmp;
272
273     wstrbuf_append_node(buf, node);
274
275     nsIDOMNode_GetFirstChild(node, &iter);
276     while(iter) {
277         wstrbuf_append_node_rec(buf, iter);
278         nsIDOMNode_GetNextSibling(iter, &tmp);
279         nsIDOMNode_Release(iter);
280         iter = tmp;
281     }
282 }
283
284 static BOOL fill_nodestr(dompos_t *pos)
285 {
286     nsIDOMText *text;
287     nsresult nsres;
288
289     if(pos->type != TEXT_NODE)
290         return FALSE;
291
292     nsres = nsIDOMNode_QueryInterface(pos->node, &IID_nsIDOMText, (void**)&text);
293     if(NS_FAILED(nsres))
294         return FALSE;
295
296     nsAString_Init(&pos->str, NULL);
297     nsIDOMText_GetData(text, &pos->str);
298     nsIDOMText_Release(text);
299     nsAString_GetData(&pos->str, &pos->p);
300
301     if(pos->off == -1)
302         pos->off = *pos->p ? strlenW(pos->p)-1 : 0;
303
304     return TRUE;
305 }
306
307 static nsIDOMNode *next_node(nsIDOMNode *iter)
308 {
309     nsIDOMNode *ret, *tmp;
310     nsresult nsres;
311
312     if(!iter)
313         return NULL;
314
315     nsres = nsIDOMNode_GetFirstChild(iter, &ret);
316     if(NS_SUCCEEDED(nsres) && ret)
317         return ret;
318
319     nsIDOMNode_AddRef(iter);
320
321     do {
322         nsres = nsIDOMNode_GetNextSibling(iter, &ret);
323         if(NS_SUCCEEDED(nsres) && ret) {
324             nsIDOMNode_Release(iter);
325             return ret;
326         }
327
328         nsres = nsIDOMNode_GetParentNode(iter, &tmp);
329         nsIDOMNode_Release(iter);
330         iter = tmp;
331     }while(NS_SUCCEEDED(nsres) && iter);
332
333     return NULL;
334 }
335
336 static nsIDOMNode *prev_node(HTMLTxtRange *This, nsIDOMNode *iter)
337 {
338     nsIDOMNode *ret, *tmp;
339     nsresult nsres;
340
341     if(!iter) {
342         nsIDOMHTMLElement *nselem;
343
344         nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nselem);
345         nsIDOMHTMLElement_GetLastChild(nselem, &tmp);
346         if(!tmp)
347             return (nsIDOMNode*)nselem;
348
349         while(tmp) {
350             ret = tmp;
351             nsIDOMNode_GetLastChild(ret, &tmp);
352         }
353
354         nsIDOMHTMLElement_Release(nselem);
355
356         return ret;
357     }
358
359     nsres = nsIDOMNode_GetLastChild(iter, &ret);
360     if(NS_SUCCEEDED(nsres) && ret)
361         return ret;
362
363     nsIDOMNode_AddRef(iter);
364
365     do {
366         nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
367         if(NS_SUCCEEDED(nsres) && ret) {
368             nsIDOMNode_Release(iter);
369             return ret;
370         }
371
372         nsres = nsIDOMNode_GetParentNode(iter, &tmp);
373         nsIDOMNode_Release(iter);
374         iter = tmp;
375     }while(NS_SUCCEEDED(nsres) && iter);
376
377     return NULL;
378 }
379
380 static nsIDOMNode *get_child_node(nsIDOMNode *node, PRUint32 off)
381 {
382     nsIDOMNodeList *node_list;
383     nsIDOMNode *ret = NULL;
384
385     nsIDOMNode_GetChildNodes(node, &node_list);
386     nsIDOMNodeList_Item(node_list, off, &ret);
387     nsIDOMNodeList_Release(node_list);
388
389     return ret;
390 }
391
392 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
393 {
394     nsIDOMNode *node;
395     PRInt32 off;
396
397     pos->p = NULL;
398
399     if(!start) {
400         cpp_bool collapsed;
401         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
402         start = collapsed;
403     }
404
405     if(start) {
406         nsIDOMRange_GetStartContainer(This->nsrange, &node);
407         nsIDOMRange_GetStartOffset(This->nsrange, &off);
408     }else {
409         nsIDOMRange_GetEndContainer(This->nsrange, &node);
410         nsIDOMRange_GetEndOffset(This->nsrange, &off);
411     }
412
413     pos->type = get_node_type(node);
414     if(pos->type == ELEMENT_NODE) {
415         if(start) {
416             pos->node = get_child_node(node, off);
417             pos->off = 0;
418         }else {
419             pos->node = off ? get_child_node(node, off-1) : prev_node(This, node);
420             pos->off = -1;
421         }
422
423         pos->type = get_node_type(pos->node);
424         nsIDOMNode_Release(node);
425     }else if(start) {
426         pos->node = node;
427         pos->off = off;
428     }else if(off) {
429         pos->node = node;
430         pos->off = off-1;
431     }else {
432         pos->node = prev_node(This, node);
433         pos->off = -1;
434         nsIDOMNode_Release(node);
435     }
436
437     if(pos->type == TEXT_NODE)
438         fill_nodestr(pos);
439 }
440
441 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
442 {
443     nsresult nsres;
444
445     if(start) {
446         if(pos->type == TEXT_NODE)
447             nsres = nsIDOMRange_SetStart(This->nsrange, pos->node, pos->off);
448         else
449             nsres = nsIDOMRange_SetStartBefore(This->nsrange, pos->node);
450     }else {
451         if(pos->type == TEXT_NODE && pos->p[pos->off+1])
452             nsres = nsIDOMRange_SetEnd(This->nsrange, pos->node, pos->off+1);
453         else
454             nsres = nsIDOMRange_SetEndAfter(This->nsrange, pos->node);
455     }
456
457     if(NS_FAILED(nsres))
458         ERR("failed: %p %08x\n", pos->node, nsres);
459 }
460
461 static inline void dompos_release(dompos_t *pos)
462 {
463     if(pos->node)
464         nsIDOMNode_Release(pos->node);
465
466     if(pos->p)
467         nsAString_Finish(&pos->str);
468 }
469
470 static inline void dompos_addref(dompos_t *pos)
471 {
472     if(pos->node)
473         nsIDOMNode_AddRef(pos->node);
474
475     if(pos->type == TEXT_NODE)
476         fill_nodestr(pos);
477 }
478
479 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
480 {
481     return pos1->node == pos2->node && pos1->off == pos2->off;
482 }
483
484 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
485 {
486     nsIDOMNode *iter, *tmp;
487     dompos_t start_pos, end_pos;
488     cpp_bool collapsed;
489
490     nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
491     if(collapsed) {
492         wstrbuf_finish(buf);
493         buf->buf = NULL;
494         buf->size = 0;
495         return;
496     }
497
498     get_cur_pos(This, FALSE, &end_pos);
499     get_cur_pos(This, TRUE, &start_pos);
500
501     if(start_pos.type == TEXT_NODE) {
502         if(start_pos.node == end_pos.node) {
503             wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, end_pos.off-start_pos.off+1);
504             iter = start_pos.node;
505             nsIDOMNode_AddRef(iter);
506         }else {
507             wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, strlenW(start_pos.p+start_pos.off));
508             iter = next_node(start_pos.node);
509         }
510     }else {
511         iter = start_pos.node;
512         nsIDOMNode_AddRef(iter);
513     }
514
515     while(iter != end_pos.node) {
516         wstrbuf_append_node(buf, iter);
517         tmp = next_node(iter);
518         nsIDOMNode_Release(iter);
519         iter = tmp;
520     }
521
522     nsIDOMNode_AddRef(end_pos.node);
523
524     if(start_pos.node != end_pos.node) {
525         if(end_pos.type == TEXT_NODE)
526             wstrbuf_append_nodetxt(buf, end_pos.p, end_pos.off+1);
527         else
528             wstrbuf_append_node(buf, end_pos.node);
529     }
530
531     nsIDOMNode_Release(iter);
532     dompos_release(&start_pos);
533     dompos_release(&end_pos);
534
535     if(buf->len) {
536         WCHAR *p;
537
538         for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);
539
540         p = strchrW(p, '\r');
541         if(p)
542             *p = 0;
543     }
544 }
545
546 HRESULT get_node_text(HTMLDOMNode *node, BSTR *ret)
547 {
548     wstrbuf_t buf;
549     HRESULT hres = S_OK;
550
551     if (!wstrbuf_init(&buf))
552         return E_OUTOFMEMORY;
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 static inline HTMLTxtRange *impl_from_IHTMLTxtRange(IHTMLTxtRange *iface)
999 {
1000     return CONTAINING_RECORD(iface, HTMLTxtRange, IHTMLTxtRange_iface);
1001 }
1002
1003 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
1004 {
1005     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1006
1007     *ppv = NULL;
1008
1009     if(IsEqualGUID(&IID_IUnknown, riid)) {
1010         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1011         *ppv = &This->IHTMLTxtRange_iface;
1012     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1013         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1014         *ppv = &This->IHTMLTxtRange_iface;
1015     }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
1016         TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
1017         *ppv = &This->IHTMLTxtRange_iface;
1018     }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1019         TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
1020         *ppv = &This->IOleCommandTarget_iface;
1021     }
1022
1023     if(*ppv) {
1024         IUnknown_AddRef((IUnknown*)*ppv);
1025         return S_OK;
1026     }
1027
1028     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1029     return E_NOINTERFACE;
1030 }
1031
1032 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
1033 {
1034     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1035     LONG ref = InterlockedIncrement(&This->ref);
1036
1037     TRACE("(%p) ref=%d\n", This, ref);
1038
1039     return ref;
1040 }
1041
1042 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1043 {
1044     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1045     LONG ref = InterlockedDecrement(&This->ref);
1046
1047     TRACE("(%p) ref=%d\n", This, ref);
1048
1049     if(!ref) {
1050         if(This->nsrange)
1051             nsIDOMRange_Release(This->nsrange);
1052         if(This->doc)
1053             list_remove(&This->entry);
1054         heap_free(This);
1055     }
1056
1057     return ref;
1058 }
1059
1060 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1061 {
1062     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1063     FIXME("(%p)->(%p)\n", This, pctinfo);
1064     return E_NOTIMPL;
1065 }
1066
1067 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1068                                                LCID lcid, ITypeInfo **ppTInfo)
1069 {
1070     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1071     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1072     return E_NOTIMPL;
1073 }
1074
1075 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1076                                                  LPOLESTR *rgszNames, UINT cNames,
1077                                                  LCID lcid, DISPID *rgDispId)
1078 {
1079     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1080     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1081           lcid, rgDispId);
1082     return E_NOTIMPL;
1083 }
1084
1085 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1086                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1087                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1088 {
1089     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1090     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1091           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1092     return E_NOTIMPL;
1093 }
1094
1095 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1096 {
1097     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1098
1099     TRACE("(%p)->(%p)\n", This, p);
1100
1101     *p = NULL;
1102
1103     if(This->nsrange) {
1104         nsIDOMDocumentFragment *fragment;
1105         nsresult nsres;
1106
1107         nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1108         if(NS_SUCCEEDED(nsres)) {
1109             const PRUnichar *nstext;
1110             nsAString nsstr;
1111
1112             nsAString_Init(&nsstr, NULL);
1113             nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1114             nsIDOMDocumentFragment_Release(fragment);
1115
1116             nsAString_GetData(&nsstr, &nstext);
1117             *p = SysAllocString(nstext);
1118
1119             nsAString_Finish(&nsstr);
1120         }
1121     }
1122
1123     if(!*p) {
1124         const WCHAR emptyW[] = {0};
1125         *p = SysAllocString(emptyW);
1126     }
1127
1128     TRACE("return %s\n", debugstr_w(*p));
1129     return S_OK;
1130 }
1131
1132 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1133 {
1134     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1135     nsIDOMText *text_node;
1136     nsAString text_str;
1137     nsresult nsres;
1138
1139     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1140
1141     if(!This->doc)
1142         return MSHTML_E_NODOC;
1143
1144     nsAString_InitDepend(&text_str, v);
1145     nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc->nsdoc, &text_str, &text_node);
1146     nsAString_Finish(&text_str);
1147     if(NS_FAILED(nsres)) {
1148         ERR("CreateTextNode failed: %08x\n", nsres);
1149         return S_OK;
1150     }
1151     nsres = nsIDOMRange_DeleteContents(This->nsrange);
1152     if(NS_FAILED(nsres))
1153         ERR("DeleteContents failed: %08x\n", nsres);
1154
1155     nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1156     if(NS_FAILED(nsres))
1157         ERR("InsertNode failed: %08x\n", nsres);
1158
1159     nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1160     if(NS_FAILED(nsres))
1161         ERR("SetEndAfter failed: %08x\n", nsres);
1162
1163     return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_FALSE);
1164 }
1165
1166 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1167 {
1168     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1169     wstrbuf_t buf;
1170
1171     TRACE("(%p)->(%p)\n", This, p);
1172
1173     *p = NULL;
1174     if(!This->nsrange)
1175         return S_OK;
1176
1177     if (!wstrbuf_init(&buf))
1178         return E_OUTOFMEMORY;
1179     range_to_string(This, &buf);
1180     if (buf.buf)
1181         *p = SysAllocString(buf.buf);
1182     wstrbuf_finish(&buf);
1183
1184     TRACE("ret %s\n", debugstr_w(*p));
1185     return S_OK;
1186 }
1187
1188 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1189 {
1190     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1191     nsIDOMNode *nsnode, *tmp;
1192     HTMLDOMNode *node;
1193     HRESULT hres;
1194
1195     TRACE("(%p)->(%p)\n", This, parent);
1196
1197     nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1198     while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1199         nsIDOMNode_GetParentNode(nsnode, &tmp);
1200         nsIDOMNode_Release(nsnode);
1201         nsnode = tmp;
1202     }
1203
1204     if(!nsnode) {
1205         *parent = NULL;
1206         return S_OK;
1207     }
1208
1209     hres = get_node(This->doc, nsnode, TRUE, &node);
1210     nsIDOMNode_Release(nsnode);
1211     if(FAILED(hres))
1212         return hres;
1213
1214     hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)parent);
1215     node_release(node);
1216     return hres;
1217 }
1218
1219 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1220 {
1221     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1222     nsIDOMRange *nsrange = NULL;
1223     HRESULT hres;
1224
1225     TRACE("(%p)->(%p)\n", This, Duplicate);
1226
1227     nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1228     hres = HTMLTxtRange_Create(This->doc, nsrange, Duplicate);
1229     nsIDOMRange_Release(nsrange);
1230
1231     return hres;
1232 }
1233
1234 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1235         VARIANT_BOOL *InRange)
1236 {
1237     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1238     HTMLTxtRange *src_range;
1239     PRInt16 nsret = 0;
1240     nsresult nsres;
1241
1242     TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1243
1244     *InRange = VARIANT_FALSE;
1245
1246     src_range = get_range_object(This->doc, Range);
1247     if(!src_range)
1248         return E_FAIL;
1249
1250     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1251             src_range->nsrange, &nsret);
1252     if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1253         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1254                 src_range->nsrange, &nsret);
1255         if(NS_SUCCEEDED(nsres) && nsret >= 0)
1256             *InRange = VARIANT_TRUE;
1257     }
1258
1259     if(NS_FAILED(nsres))
1260         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1261
1262     return S_OK;
1263 }
1264
1265 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1266         VARIANT_BOOL *IsEqual)
1267 {
1268     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1269     HTMLTxtRange *src_range;
1270     PRInt16 nsret = 0;
1271     nsresult nsres;
1272
1273     TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1274
1275     *IsEqual = VARIANT_FALSE;
1276
1277     src_range = get_range_object(This->doc, Range);
1278     if(!src_range)
1279         return E_FAIL;
1280
1281     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1282             src_range->nsrange, &nsret);
1283     if(NS_SUCCEEDED(nsres) && !nsret) {
1284         nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1285                 src_range->nsrange, &nsret);
1286         if(NS_SUCCEEDED(nsres) && !nsret)
1287             *IsEqual = VARIANT_TRUE;
1288     }
1289
1290     if(NS_FAILED(nsres))
1291         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1292
1293     return S_OK;
1294 }
1295
1296 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1297 {
1298     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1299     FIXME("(%p)->(%x)\n", This, fStart);
1300     return E_NOTIMPL;
1301 }
1302
1303 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1304 {
1305     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1306
1307     TRACE("(%p)->(%x)\n", This, Start);
1308
1309     nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1310     return S_OK;
1311 }
1312
1313 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1314 {
1315     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1316     range_unit_t unit;
1317
1318     TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1319
1320     unit = string_to_unit(Unit);
1321     if(unit == RU_UNKNOWN)
1322         return E_INVALIDARG;
1323
1324     *Success = VARIANT_FALSE;
1325
1326     switch(unit) {
1327     case RU_WORD: {
1328         dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1329         cpp_bool collapsed;
1330
1331         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1332
1333         get_cur_pos(This, TRUE, &start_pos);
1334         get_cur_pos(This, FALSE, &end_pos);
1335
1336         if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1337             set_range_pos(This, FALSE, &new_end_pos);
1338             *Success = VARIANT_TRUE;
1339         }
1340
1341         if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1342             if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1343                 set_range_pos(This, TRUE, &new_start_pos);
1344                 *Success = VARIANT_TRUE;
1345             }
1346             dompos_release(&new_start_pos);
1347         }
1348
1349         dompos_release(&new_end_pos);
1350         dompos_release(&end_pos);
1351         dompos_release(&start_pos);
1352
1353         break;
1354     }
1355
1356     case RU_TEXTEDIT: {
1357         nsIDOMHTMLElement *nsbody = NULL;
1358         nsresult nsres;
1359
1360         nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
1361         if(NS_FAILED(nsres) || !nsbody) {
1362             ERR("Could not get body: %08x\n", nsres);
1363             break;
1364         }
1365
1366         nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1367         nsIDOMHTMLElement_Release(nsbody);
1368         if(NS_FAILED(nsres)) {
1369             ERR("Collapse failed: %08x\n", nsres);
1370             break;
1371         }
1372
1373         *Success = VARIANT_TRUE;
1374         break;
1375     }
1376
1377     default:
1378         FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1379     }
1380
1381     return S_OK;
1382 }
1383
1384 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1385         LONG Count, LONG *ActualCount)
1386 {
1387     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1388     range_unit_t unit;
1389
1390     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1391
1392     unit = string_to_unit(Unit);
1393     if(unit == RU_UNKNOWN)
1394         return E_INVALIDARG;
1395
1396     if(!Count) {
1397         *ActualCount = 0;
1398         return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1399     }
1400
1401     switch(unit) {
1402     case RU_CHAR: {
1403         dompos_t cur_pos, new_pos;
1404
1405         get_cur_pos(This, TRUE, &cur_pos);
1406
1407         if(Count > 0) {
1408             *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1409             set_range_pos(This, FALSE, &new_pos);
1410             dompos_release(&new_pos);
1411
1412             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1413         }else {
1414             *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1415             set_range_pos(This, TRUE, &new_pos);
1416             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1417             dompos_release(&new_pos);
1418         }
1419
1420         dompos_release(&cur_pos);
1421         break;
1422     }
1423
1424     case RU_WORD: {
1425         dompos_t cur_pos, new_pos;
1426
1427         get_cur_pos(This, TRUE, &cur_pos);
1428
1429         if(Count > 0) {
1430             *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1431             set_range_pos(This, FALSE, &new_pos);
1432             dompos_release(&new_pos);
1433             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1434         }else {
1435             *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1436             set_range_pos(This, TRUE, &new_pos);
1437             IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1438             dompos_release(&new_pos);
1439         }
1440
1441         dompos_release(&cur_pos);
1442         break;
1443     }
1444
1445     default:
1446         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1447     }
1448
1449     TRACE("ret %d\n", *ActualCount);
1450     return S_OK;
1451 }
1452
1453 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1454         LONG Count, LONG *ActualCount)
1455 {
1456     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1457     range_unit_t unit;
1458
1459     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1460
1461     unit = string_to_unit(Unit);
1462     if(unit == RU_UNKNOWN)
1463         return E_INVALIDARG;
1464
1465     if(!Count) {
1466         *ActualCount = 0;
1467         return S_OK;
1468     }
1469
1470     switch(unit) {
1471     case RU_CHAR: {
1472         dompos_t start_pos, end_pos, new_pos;
1473         cpp_bool collapsed;
1474
1475         get_cur_pos(This, TRUE, &start_pos);
1476         get_cur_pos(This, FALSE, &end_pos);
1477         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1478
1479         if(Count > 0) {
1480             BOOL bounded;
1481
1482             *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1483             set_range_pos(This, !bounded, &new_pos);
1484             if(bounded)
1485                 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1486         }else {
1487             *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1488             set_range_pos(This, TRUE, &new_pos);
1489         }
1490
1491         dompos_release(&start_pos);
1492         dompos_release(&end_pos);
1493         dompos_release(&new_pos);
1494         break;
1495     }
1496
1497     default:
1498         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1499     }
1500
1501     return S_OK;
1502 }
1503
1504 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1505         LONG Count, LONG *ActualCount)
1506 {
1507     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1508     range_unit_t unit;
1509
1510     TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1511
1512     unit = string_to_unit(Unit);
1513     if(unit == RU_UNKNOWN)
1514         return E_INVALIDARG;
1515
1516     if(!Count) {
1517         *ActualCount = 0;
1518         return S_OK;
1519     }
1520
1521     switch(unit) {
1522     case RU_CHAR: {
1523         dompos_t start_pos, end_pos, new_pos;
1524         cpp_bool collapsed;
1525
1526         get_cur_pos(This, TRUE, &start_pos);
1527         get_cur_pos(This, FALSE, &end_pos);
1528         nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1529
1530         if(Count > 0) {
1531             *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1532             set_range_pos(This, FALSE, &new_pos);
1533         }else {
1534             BOOL bounded;
1535
1536             *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1537             set_range_pos(This, bounded, &new_pos);
1538             if(bounded)
1539                 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1540         }
1541
1542         dompos_release(&start_pos);
1543         dompos_release(&end_pos);
1544         dompos_release(&new_pos);
1545         break;
1546     }
1547
1548     default:
1549         FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1550     }
1551
1552     return S_OK;
1553 }
1554
1555 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1556 {
1557     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1558     nsISelection *nsselection;
1559     nsresult nsres;
1560
1561     TRACE("(%p)\n", This);
1562
1563     nsres = nsIDOMWindow_GetSelection(This->doc->basedoc.window->nswindow, &nsselection);
1564     if(NS_FAILED(nsres)) {
1565         ERR("GetSelection failed: %08x\n", nsres);
1566         return E_FAIL;
1567     }
1568
1569     nsISelection_RemoveAllRanges(nsselection);
1570     nsISelection_AddRange(nsselection, This->nsrange);
1571     nsISelection_Release(nsselection);
1572     return S_OK;
1573 }
1574
1575 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1576 {
1577     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1578     FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1579     return E_NOTIMPL;
1580 }
1581
1582 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1583 {
1584     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1585     FIXME("(%p)->(%p)\n", This, element);
1586     return E_NOTIMPL;
1587 }
1588
1589 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1590         IHTMLTxtRange *SourceRange)
1591 {
1592     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1593     FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1594     return E_NOTIMPL;
1595 }
1596
1597 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1598         IHTMLTxtRange *SourceRange, LONG *ret)
1599 {
1600     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1601     HTMLTxtRange *src_range;
1602     PRInt16 nsret = 0;
1603     int nscmpt;
1604     nsresult nsres;
1605
1606     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1607
1608     nscmpt = string_to_nscmptype(how);
1609     if(nscmpt == -1)
1610         return E_INVALIDARG;
1611
1612     src_range = get_range_object(This->doc, SourceRange);
1613     if(!src_range)
1614         return E_FAIL;
1615
1616     nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1617     if(NS_FAILED(nsres))
1618         ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1619
1620     *ret = nsret;
1621     return S_OK;
1622 }
1623
1624 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1625         LONG count, LONG Flags, VARIANT_BOOL *Success)
1626 {
1627     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1628     FIXME("(%p)->(%s %d %08x %p)\n", This, debugstr_w(String), count, Flags, Success);
1629     return E_NOTIMPL;
1630 }
1631
1632 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, LONG x, LONG y)
1633 {
1634     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1635     FIXME("(%p)->(%d %d)\n", This, x, y);
1636     return E_NOTIMPL;
1637 }
1638
1639 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1640 {
1641     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1642     FIXME("(%p)->(%p)\n", This, Bookmark);
1643     return E_NOTIMPL;
1644 }
1645
1646 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1647         VARIANT_BOOL *Success)
1648 {
1649     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1650     FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1651     return E_NOTIMPL;
1652 }
1653
1654 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1655         VARIANT_BOOL *pfRet)
1656 {
1657     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1658     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1659     return E_NOTIMPL;
1660 }
1661
1662 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1663         VARIANT_BOOL *pfRet)
1664 {
1665     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1666     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1667     return E_NOTIMPL;
1668 }
1669
1670 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1671         VARIANT_BOOL *pfRet)
1672 {
1673     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1674     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1675     return E_NOTIMPL;
1676 }
1677
1678 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1679         VARIANT_BOOL *pfRet)
1680 {
1681     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1682     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1683     return E_NOTIMPL;
1684 }
1685
1686 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1687         BSTR *pcmdText)
1688 {
1689     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1690     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1691     return E_NOTIMPL;
1692 }
1693
1694 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1695         VARIANT *pcmdValue)
1696 {
1697     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1698     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1699     return E_NOTIMPL;
1700 }
1701
1702 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1703         VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1704 {
1705     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1706     FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1707     return E_NOTIMPL;
1708 }
1709
1710 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1711         VARIANT_BOOL *pfRet)
1712 {
1713     HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1714     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1715     return E_NOTIMPL;
1716 }
1717
1718 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1719     HTMLTxtRange_QueryInterface,
1720     HTMLTxtRange_AddRef,
1721     HTMLTxtRange_Release,
1722     HTMLTxtRange_GetTypeInfoCount,
1723     HTMLTxtRange_GetTypeInfo,
1724     HTMLTxtRange_GetIDsOfNames,
1725     HTMLTxtRange_Invoke,
1726     HTMLTxtRange_get_htmlText,
1727     HTMLTxtRange_put_text,
1728     HTMLTxtRange_get_text,
1729     HTMLTxtRange_parentElement,
1730     HTMLTxtRange_duplicate,
1731     HTMLTxtRange_inRange,
1732     HTMLTxtRange_isEqual,
1733     HTMLTxtRange_scrollIntoView,
1734     HTMLTxtRange_collapse,
1735     HTMLTxtRange_expand,
1736     HTMLTxtRange_move,
1737     HTMLTxtRange_moveStart,
1738     HTMLTxtRange_moveEnd,
1739     HTMLTxtRange_select,
1740     HTMLTxtRange_pasteHTML,
1741     HTMLTxtRange_moveToElementText,
1742     HTMLTxtRange_setEndPoint,
1743     HTMLTxtRange_compareEndPoints,
1744     HTMLTxtRange_findText,
1745     HTMLTxtRange_moveToPoint,
1746     HTMLTxtRange_getBookmark,
1747     HTMLTxtRange_moveToBookmark,
1748     HTMLTxtRange_queryCommandSupported,
1749     HTMLTxtRange_queryCommandEnabled,
1750     HTMLTxtRange_queryCommandState,
1751     HTMLTxtRange_queryCommandIndeterm,
1752     HTMLTxtRange_queryCommandText,
1753     HTMLTxtRange_queryCommandValue,
1754     HTMLTxtRange_execCommand,
1755     HTMLTxtRange_execCommandShowHelp
1756 };
1757
1758 static inline HTMLTxtRange *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
1759 {
1760     return CONTAINING_RECORD(iface, HTMLTxtRange, IOleCommandTarget_iface);
1761 }
1762
1763 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1764 {
1765     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1766     return IHTMLTxtRange_QueryInterface(&This->IHTMLTxtRange_iface, riid, ppv);
1767 }
1768
1769 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1770 {
1771     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1772     return IHTMLTxtRange_AddRef(&This->IHTMLTxtRange_iface);
1773 }
1774
1775 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1776 {
1777     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1778     return IHTMLTxtRange_Release(&This->IHTMLTxtRange_iface);
1779 }
1780
1781 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1782         ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1783 {
1784     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1785     FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1786     return E_NOTIMPL;
1787 }
1788
1789 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1790 {
1791     nsIDOMHTMLElement *blockquote_elem, *p_elem;
1792     nsIDOMDocumentFragment *fragment;
1793     nsIDOMNode *tmp;
1794
1795     static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1796     static const PRUnichar pW[] = {'P',0};
1797
1798     TRACE("(%p)->(%p %p)\n", This, in, out);
1799
1800     if(!This->doc->nsdoc) {
1801         WARN("NULL nsdoc\n");
1802         return E_NOTIMPL;
1803     }
1804
1805     create_nselem(This->doc, blockquoteW, &blockquote_elem);
1806     create_nselem(This->doc, pW, &p_elem);
1807
1808     nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1809     nsIDOMHTMLElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1810     nsIDOMDocumentFragment_Release(fragment);
1811     nsIDOMNode_Release(tmp);
1812
1813     nsIDOMHTMLElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1814     nsIDOMHTMLElement_Release(p_elem);
1815     nsIDOMNode_Release(tmp);
1816
1817     nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1818     nsIDOMHTMLElement_Release(blockquote_elem);
1819
1820     return S_OK;
1821 }
1822
1823 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1824         DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1825 {
1826     HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1827
1828     TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1829           nCmdexecopt, pvaIn, pvaOut);
1830
1831     if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1832         switch(nCmdID) {
1833         case IDM_INDENT:
1834             return exec_indent(This, pvaIn, pvaOut);
1835         default:
1836             FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1837         }
1838     }else {
1839         FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1840     }
1841
1842     return E_NOTIMPL;
1843 }
1844
1845 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1846     RangeCommandTarget_QueryInterface,
1847     RangeCommandTarget_AddRef,
1848     RangeCommandTarget_Release,
1849     RangeCommandTarget_QueryStatus,
1850     RangeCommandTarget_Exec
1851 };
1852
1853 HRESULT HTMLTxtRange_Create(HTMLDocumentNode *doc, nsIDOMRange *nsrange, IHTMLTxtRange **p)
1854 {
1855     HTMLTxtRange *ret;
1856
1857     ret = heap_alloc(sizeof(HTMLTxtRange));
1858     if(!ret)
1859         return E_OUTOFMEMORY;
1860
1861     ret->IHTMLTxtRange_iface.lpVtbl = &HTMLTxtRangeVtbl;
1862     ret->IOleCommandTarget_iface.lpVtbl = &OleCommandTargetVtbl;
1863     ret->ref = 1;
1864
1865     if(nsrange)
1866         nsIDOMRange_AddRef(nsrange);
1867     ret->nsrange = nsrange;
1868
1869     ret->doc = doc;
1870     list_add_head(&doc->range_list, &ret->entry);
1871
1872     *p = &ret->IHTMLTxtRange_iface;
1873     return S_OK;
1874 }
1875
1876 void detach_ranges(HTMLDocumentNode *This)
1877 {
1878     HTMLTxtRange *iter;
1879
1880     LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1881         iter->doc = NULL;
1882     }
1883 }