dplayx: Tests for checking the behaviour of groups in a p2p session.
[wine/gsoc_dplay.git] / dlls / mshtml / txtrange.c
blob9d96405889d7cd20aa9a1dd962c8f8ed4d44aa5a
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27 #include "mshtmcid.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 static const WCHAR brW[] = {'b','r',0};
37 static const WCHAR hrW[] = {'h','r',0};
39 typedef struct {
40 const IHTMLTxtRangeVtbl *lpHTMLTxtRangeVtbl;
41 const IOleCommandTargetVtbl *lpOleCommandTargetVtbl;
43 LONG ref;
45 nsIDOMRange *nsrange;
46 HTMLDocument *doc;
48 struct list entry;
49 } HTMLTxtRange;
51 #define HTMLTXTRANGE(x) ((IHTMLTxtRange*) &(x)->lpHTMLTxtRangeVtbl)
53 typedef struct {
54 WCHAR *buf;
55 DWORD len;
56 DWORD size;
57 } wstrbuf_t;
59 typedef struct {
60 PRUint16 type;
61 nsIDOMNode *node;
62 PRUint32 off;
63 nsAString str;
64 const PRUnichar *p;
65 } dompos_t;
67 typedef enum {
68 RU_UNKNOWN,
69 RU_CHAR,
70 RU_WORD,
71 RU_SENTENCE,
72 RU_TEXTEDIT
73 } range_unit_t;
75 static HTMLTxtRange *get_range_object(HTMLDocument *doc, IHTMLTxtRange *iface)
77 HTMLTxtRange *iter;
79 LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
80 if(HTMLTXTRANGE(iter) == iface)
81 return iter;
84 ERR("Could not find range in document\n");
85 return NULL;
88 static range_unit_t string_to_unit(LPCWSTR str)
90 static const WCHAR characterW[] =
91 {'c','h','a','r','a','c','t','e','r',0};
92 static const WCHAR wordW[] =
93 {'w','o','r','d',0};
94 static const WCHAR sentenceW[] =
95 {'s','e','n','t','e','n','c','e',0};
96 static const WCHAR texteditW[] =
97 {'t','e','x','t','e','d','i','t',0};
99 if(!strcmpiW(str, characterW)) return RU_CHAR;
100 if(!strcmpiW(str, wordW)) return RU_WORD;
101 if(!strcmpiW(str, sentenceW)) return RU_SENTENCE;
102 if(!strcmpiW(str, texteditW)) return RU_TEXTEDIT;
104 return RU_UNKNOWN;
107 static int string_to_nscmptype(LPCWSTR str)
109 static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
110 static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
111 static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
112 static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
114 if(!strcmpiW(str, seW)) return NS_START_TO_END;
115 if(!strcmpiW(str, ssW)) return NS_START_TO_START;
116 if(!strcmpiW(str, esW)) return NS_END_TO_START;
117 if(!strcmpiW(str, eeW)) return NS_END_TO_END;
119 return -1;
122 static PRUint16 get_node_type(nsIDOMNode *node)
124 PRUint16 type = 0;
126 if(node)
127 nsIDOMNode_GetNodeType(node, &type);
129 return type;
132 static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
134 nsIDOMElement *elem;
135 nsAString tag_str;
136 const PRUnichar *tag;
137 BOOL ret = FALSE;
138 nsresult nsres;
140 nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
141 if(NS_FAILED(nsres))
142 return FALSE;
144 nsAString_Init(&tag_str, NULL);
145 nsIDOMElement_GetTagName(elem, &tag_str);
146 nsIDOMElement_Release(elem);
147 nsAString_GetData(&tag_str, &tag);
149 ret = !strcmpiW(tag, istag);
151 nsAString_Finish(&tag_str);
153 return ret;
156 static BOOL is_space_elem(nsIDOMNode *node)
158 nsIDOMElement *elem;
159 nsAString tag_str;
160 const PRUnichar *tag;
161 BOOL ret = FALSE;
162 nsresult nsres;
164 nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
165 if(NS_FAILED(nsres))
166 return FALSE;
168 nsAString_Init(&tag_str, NULL);
169 nsIDOMElement_GetTagName(elem, &tag_str);
170 nsIDOMElement_Release(elem);
171 nsAString_GetData(&tag_str, &tag);
173 ret = !strcmpiW(tag, brW) || !strcmpiW(tag, hrW);
175 nsAString_Finish(&tag_str);
177 return ret;
180 static inline void wstrbuf_init(wstrbuf_t *buf)
182 buf->len = 0;
183 buf->size = 16;
184 buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
185 *buf->buf = 0;
188 static inline void wstrbuf_finish(wstrbuf_t *buf)
190 heap_free(buf->buf);
193 static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
195 if(buf->len+len >= buf->size) {
196 buf->size = 2*buf->len+len;
197 buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
200 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
201 buf->len += len;
202 buf->buf[buf->len] = 0;
205 static void wstrbuf_append_nodetxt(wstrbuf_t *buf, LPCWSTR str, int len)
207 const WCHAR *s = str;
208 WCHAR *d;
210 TRACE("%s\n", debugstr_wn(str, len));
212 if(buf->len+len >= buf->size) {
213 buf->size = 2*buf->len+len;
214 buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
217 if(buf->len && isspaceW(buf->buf[buf->len-1])) {
218 while(s < str+len && isspaceW(*s))
219 s++;
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++;
234 buf->len = d - buf->buf;
235 *d = 0;
238 static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node)
241 switch(get_node_type(node)) {
242 case TEXT_NODE: {
243 nsIDOMText *nstext;
244 nsAString data_str;
245 const PRUnichar *data;
247 nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
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);
255 nsIDOMText_Release(nstext);
257 break;
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);
270 static BOOL fill_nodestr(dompos_t *pos)
272 nsIDOMText *text;
273 nsresult nsres;
275 if(pos->type != TEXT_NODE)
276 return FALSE;
278 nsres = nsIDOMNode_QueryInterface(pos->node, &IID_nsIDOMText, (void**)&text);
279 if(NS_FAILED(nsres))
280 return FALSE;
282 nsAString_Init(&pos->str, NULL);
283 nsIDOMText_GetData(text, &pos->str);
284 nsIDOMText_Release(text);
285 nsAString_GetData(&pos->str, &pos->p);
287 if(pos->off == -1)
288 pos->off = *pos->p ? strlenW(pos->p)-1 : 0;
290 return TRUE;
293 static nsIDOMNode *next_node(nsIDOMNode *iter)
295 nsIDOMNode *ret, *tmp;
296 nsresult nsres;
298 if(!iter)
299 return NULL;
301 nsres = nsIDOMNode_GetFirstChild(iter, &ret);
302 if(NS_SUCCEEDED(nsres) && ret)
303 return ret;
305 nsIDOMNode_AddRef(iter);
307 do {
308 nsres = nsIDOMNode_GetNextSibling(iter, &ret);
309 if(NS_SUCCEEDED(nsres) && ret) {
310 nsIDOMNode_Release(iter);
311 return ret;
314 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
315 nsIDOMNode_Release(iter);
316 iter = tmp;
317 }while(NS_SUCCEEDED(nsres) && iter);
319 return NULL;
322 static nsIDOMNode *prev_node(HTMLTxtRange *This, nsIDOMNode *iter)
324 nsIDOMNode *ret, *tmp;
325 nsresult nsres;
327 if(!iter) {
328 nsIDOMHTMLDocument *nshtmldoc;
329 nsIDOMHTMLElement *nselem;
330 nsIDOMDocument *nsdoc;
332 nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
333 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
334 nsIDOMDocument_Release(nsdoc);
335 nsIDOMHTMLDocument_GetBody(nshtmldoc, &nselem);
336 nsIDOMHTMLDocument_Release(nshtmldoc);
338 nsIDOMElement_GetLastChild(nselem, &tmp);
339 if(!tmp)
340 return (nsIDOMNode*)nselem;
342 while(tmp) {
343 ret = tmp;
344 nsIDOMNode_GetLastChild(ret, &tmp);
347 nsIDOMElement_Release(nselem);
349 return ret;
352 nsres = nsIDOMNode_GetLastChild(iter, &ret);
353 if(NS_SUCCEEDED(nsres) && ret)
354 return ret;
356 nsIDOMNode_AddRef(iter);
358 do {
359 nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
360 if(NS_SUCCEEDED(nsres) && ret) {
361 nsIDOMNode_Release(iter);
362 return ret;
365 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
366 nsIDOMNode_Release(iter);
367 iter = tmp;
368 }while(NS_SUCCEEDED(nsres) && iter);
370 return NULL;
373 static nsIDOMNode *get_child_node(nsIDOMNode *node, PRUint32 off)
375 nsIDOMNodeList *node_list;
376 nsIDOMNode *ret = NULL;
378 nsIDOMNode_GetChildNodes(node, &node_list);
379 nsIDOMNodeList_Item(node_list, off, &ret);
380 nsIDOMNodeList_Release(node_list);
382 return ret;
385 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
387 nsIDOMNode *node;
388 PRInt32 off;
390 pos->p = NULL;
392 if(!start) {
393 PRBool collapsed;
394 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
395 start = collapsed;
398 if(start) {
399 nsIDOMRange_GetStartContainer(This->nsrange, &node);
400 nsIDOMRange_GetStartOffset(This->nsrange, &off);
401 }else {
402 nsIDOMRange_GetEndContainer(This->nsrange, &node);
403 nsIDOMRange_GetEndOffset(This->nsrange, &off);
406 pos->type = get_node_type(node);
407 if(pos->type == ELEMENT_NODE) {
408 if(start) {
409 pos->node = get_child_node(node, off);
410 pos->off = 0;
411 }else {
412 pos->node = off ? get_child_node(node, off-1) : prev_node(This, node);
413 pos->off = -1;
416 pos->type = get_node_type(pos->node);
417 nsIDOMNode_Release(node);
418 }else if(start) {
419 pos->node = node;
420 pos->off = off;
421 }else if(off) {
422 pos->node = node;
423 pos->off = off-1;
424 }else {
425 pos->node = prev_node(This, node);
426 pos->off = -1;
427 nsIDOMNode_Release(node);
430 if(pos->type == TEXT_NODE)
431 fill_nodestr(pos);
434 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
436 nsresult nsres;
438 if(start) {
439 if(pos->type == TEXT_NODE)
440 nsres = nsIDOMRange_SetStart(This->nsrange, pos->node, pos->off);
441 else
442 nsres = nsIDOMRange_SetStartBefore(This->nsrange, pos->node);
443 }else {
444 if(pos->type == TEXT_NODE && pos->p[pos->off+1])
445 nsres = nsIDOMRange_SetEnd(This->nsrange, pos->node, pos->off+1);
446 else
447 nsres = nsIDOMRange_SetEndAfter(This->nsrange, pos->node);
450 if(NS_FAILED(nsres))
451 ERR("failed: %p %08x\n", pos->node, nsres);
454 static inline void dompos_release(dompos_t *pos)
456 if(pos->node)
457 nsIDOMNode_Release(pos->node);
459 if(pos->p)
460 nsAString_Finish(&pos->str);
463 static inline void dompos_addref(dompos_t *pos)
465 if(pos->node)
466 nsIDOMNode_AddRef(pos->node);
468 if(pos->type == TEXT_NODE)
469 fill_nodestr(pos);
472 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
474 return pos1->node == pos2->node && pos1->off == pos2->off;
477 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
479 nsIDOMNode *iter, *tmp;
480 dompos_t start_pos, end_pos;
481 PRBool collapsed;
483 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
484 if(collapsed) {
485 wstrbuf_finish(buf);
486 buf->buf = NULL;
487 buf->size = 0;
488 return;
491 get_cur_pos(This, FALSE, &end_pos);
492 get_cur_pos(This, TRUE, &start_pos);
494 if(start_pos.type == TEXT_NODE) {
495 if(start_pos.node == end_pos.node) {
496 wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, end_pos.off-start_pos.off+1);
497 iter = start_pos.node;
498 nsIDOMNode_AddRef(iter);
499 }else {
500 wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, strlenW(start_pos.p+start_pos.off));
501 iter = next_node(start_pos.node);
503 }else {
504 iter = start_pos.node;
505 nsIDOMNode_AddRef(iter);
508 while(iter != end_pos.node) {
509 wstrbuf_append_node(buf, iter);
510 tmp = next_node(iter);
511 nsIDOMNode_Release(iter);
512 iter = tmp;
515 nsIDOMNode_AddRef(end_pos.node);
517 if(start_pos.node != end_pos.node) {
518 if(end_pos.type == TEXT_NODE)
519 wstrbuf_append_nodetxt(buf, end_pos.p, end_pos.off+1);
520 else
521 wstrbuf_append_node(buf, end_pos.node);
524 nsIDOMNode_Release(iter);
525 dompos_release(&start_pos);
526 dompos_release(&end_pos);
528 if(buf->len) {
529 WCHAR *p;
531 for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);
533 p = strchrW(p, '\r');
534 if(p)
535 *p = 0;
539 static WCHAR get_pos_char(const dompos_t *pos)
541 switch(pos->type) {
542 case TEXT_NODE:
543 return pos->p[pos->off];
544 case ELEMENT_NODE:
545 if(is_space_elem(pos->node))
546 return '\n';
549 return 0;
552 static void end_space(const dompos_t *pos, dompos_t *new_pos)
554 const WCHAR *p;
556 *new_pos = *pos;
557 dompos_addref(new_pos);
559 if(pos->type != TEXT_NODE)
560 return;
562 p = new_pos->p+new_pos->off;
564 if(!*p || !isspace(*p))
565 return;
567 while(p[1] && isspace(p[1]))
568 p++;
570 new_pos->off = p - new_pos->p;
573 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
575 nsIDOMNode *iter, *tmp;
576 dompos_t last_space, tmp_pos;
577 const WCHAR *p;
578 WCHAR cspace = 0;
580 if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off]) {
581 p = pos->p+pos->off;
583 if(isspace(*p))
584 while(isspaceW(*++p));
585 else
586 p++;
588 if(*p && isspaceW(*p)) {
589 cspace = ' ';
590 while(p[1] && isspaceW(p[1]))
591 p++;
594 if(*p) {
595 *new_pos = *pos;
596 new_pos->off = p - pos->p;
597 dompos_addref(new_pos);
599 return cspace ? cspace : *p;
600 }else {
601 last_space = *pos;
602 last_space.off = p - pos->p;
603 dompos_addref(&last_space);
607 iter = next_node(pos->node);
609 while(iter) {
610 switch(get_node_type(iter)) {
611 case TEXT_NODE:
612 tmp_pos.node = iter;
613 tmp_pos.type = TEXT_NODE;
614 tmp_pos.off = 0;
615 dompos_addref(&tmp_pos);
617 p = tmp_pos.p;
619 if(!*p) {
620 dompos_release(&tmp_pos);
621 break;
622 }else if(isspaceW(*p)) {
623 if(cspace)
624 dompos_release(&last_space);
625 else
626 cspace = ' ';
628 while(p[1] && isspaceW(p[1]))
629 p++;
631 tmp_pos.off = p-tmp_pos.p;
633 if(!p[1]) {
634 last_space = tmp_pos;
635 break;
638 *new_pos = tmp_pos;
639 nsIDOMNode_Release(iter);
640 return cspace;
641 }else if(cspace) {
642 *new_pos = last_space;
643 dompos_release(&tmp_pos);
644 nsIDOMNode_Release(iter);
646 return cspace;
647 }else if(*p) {
648 tmp_pos.off = 0;
649 *new_pos = tmp_pos;
652 nsIDOMNode_Release(iter);
653 return *p;
655 case ELEMENT_NODE:
656 if(is_elem_tag(iter, brW)) {
657 if(cspace)
658 dompos_release(&last_space);
659 cspace = '\n';
661 nsIDOMNode_AddRef(iter);
662 last_space.node = iter;
663 last_space.type = ELEMENT_NODE;
664 last_space.off = 0;
665 last_space.p = NULL;
666 }else if(is_elem_tag(iter, hrW)) {
667 if(cspace) {
668 *new_pos = last_space;
669 nsIDOMNode_Release(iter);
670 return cspace;
673 new_pos->node = iter;
674 new_pos->type = ELEMENT_NODE;
675 new_pos->off = 0;
676 new_pos->p = NULL;
677 return '\n';
681 tmp = iter;
682 iter = next_node(iter);
683 nsIDOMNode_Release(tmp);
686 if(cspace) {
687 *new_pos = last_space;
688 }else {
689 *new_pos = *pos;
690 dompos_addref(new_pos);
693 return cspace;
696 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
698 nsIDOMNode *iter, *tmp;
699 const WCHAR *p;
700 BOOL skip_space = FALSE;
702 if(pos->type == TEXT_NODE && isspaceW(pos->p[pos->off]))
703 skip_space = TRUE;
705 if(pos->type == TEXT_NODE && pos->off) {
706 p = pos->p+pos->off-1;
708 if(skip_space) {
709 while(p >= pos->p && isspace(*p))
710 p--;
713 if(p >= pos->p) {
714 *new_pos = *pos;
715 new_pos->off = p-pos->p;
716 dompos_addref(new_pos);
717 return new_pos->p[new_pos->off];
721 iter = prev_node(This, pos->node);
723 while(iter) {
724 switch(get_node_type(iter)) {
725 case TEXT_NODE: {
726 dompos_t tmp_pos;
728 tmp_pos.node = iter;
729 tmp_pos.type = TEXT_NODE;
730 tmp_pos.off = 0;
731 dompos_addref(&tmp_pos);
733 p = tmp_pos.p + strlenW(tmp_pos.p)-1;
735 if(skip_space) {
736 while(p >= tmp_pos.p && isspaceW(*p))
737 p--;
740 if(p < tmp_pos.p) {
741 dompos_release(&tmp_pos);
742 break;
745 tmp_pos.off = p-tmp_pos.p;
746 *new_pos = tmp_pos;
747 nsIDOMNode_Release(iter);
748 return *p;
751 case ELEMENT_NODE:
752 if(is_elem_tag(iter, brW)) {
753 if(skip_space) {
754 skip_space = FALSE;
755 break;
757 }else if(!is_elem_tag(iter, hrW)) {
758 break;
761 new_pos->node = iter;
762 new_pos->type = ELEMENT_NODE;
763 new_pos->off = 0;
764 new_pos->p = NULL;
765 return '\n';
768 tmp = iter;
769 iter = prev_node(This, iter);
770 nsIDOMNode_Release(tmp);
773 *new_pos = *pos;
774 dompos_addref(new_pos);
775 return 0;
778 static long move_next_chars(long cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
779 BOOL *bounded, dompos_t *new_pos)
781 dompos_t iter, tmp;
782 long ret = 0;
783 WCHAR c;
785 if(bounded)
786 *bounded = FALSE;
788 if(col)
789 ret++;
791 if(ret >= cnt) {
792 end_space(pos, new_pos);
793 return ret;
796 c = next_char(pos, &iter);
797 ret++;
799 while(ret < cnt) {
800 tmp = iter;
801 c = next_char(&tmp, &iter);
802 dompos_release(&tmp);
803 if(!c)
804 break;
806 ret++;
807 if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
808 *bounded = TRUE;
809 ret++;
813 *new_pos = iter;
814 return ret;
817 static long move_prev_chars(HTMLTxtRange *This, long cnt, const dompos_t *pos, BOOL end,
818 const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
820 dompos_t iter, tmp;
821 long ret = 0;
822 BOOL prev_eq = FALSE;
823 WCHAR c;
825 if(bounded)
826 *bounded = FALSE;
828 c = prev_char(This, pos, &iter);
829 if(c)
830 ret++;
832 while(c && ret < cnt) {
833 tmp = iter;
834 c = prev_char(This, &tmp, &iter);
835 dompos_release(&tmp);
836 if(!c) {
837 if(end)
838 ret++;
839 break;
842 ret++;
844 if(prev_eq) {
845 *bounded = TRUE;
846 ret++;
849 prev_eq = bound_pos && dompos_cmp(&iter, bound_pos);
852 *new_pos = iter;
853 return ret;
856 static long find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
858 dompos_t iter, tmp;
859 WCHAR c;
861 c = prev_char(This, pos, &iter);
862 if(!c || (first_space && isspaceW(c))) {
863 *ret = iter;
864 return FALSE;
867 while(1) {
868 tmp = iter;
869 c = prev_char(This, &tmp, &iter);
870 if(!c || isspaceW(c)) {
871 dompos_release(&iter);
872 break;
874 dompos_release(&tmp);
877 *ret = tmp;
878 return TRUE;
881 static int find_word_end(const dompos_t *pos, dompos_t *ret)
883 dompos_t iter, tmp;
884 int cnt = 1;
885 WCHAR c;
886 c = get_pos_char(pos);
887 if(isspaceW(c)) {
888 *ret = *pos;
889 dompos_addref(ret);
890 return 0;
893 c = next_char(pos, &iter);
894 if(!c) {
895 *ret = iter;
896 return 0;
898 if(c == '\n') {
899 *ret = *pos;
900 dompos_addref(ret);
901 return 0;
904 while(c && !isspaceW(c)) {
905 tmp = iter;
906 c = next_char(&tmp, &iter);
907 if(c == '\n') {
908 dompos_release(&iter);
909 iter = tmp;
910 }else {
911 cnt++;
912 dompos_release(&tmp);
916 *ret = iter;
917 return cnt;
920 static long move_next_words(long cnt, const dompos_t *pos, dompos_t *new_pos)
922 dompos_t iter, tmp;
923 long ret = 0;
924 WCHAR c;
926 c = get_pos_char(pos);
927 if(isspaceW(c)) {
928 end_space(pos, &iter);
929 ret++;
930 }else {
931 c = next_char(pos, &iter);
932 if(c && isspaceW(c))
933 ret++;
936 while(c && ret < cnt) {
937 tmp = iter;
938 c = next_char(&tmp, &iter);
939 dompos_release(&tmp);
940 if(isspaceW(c))
941 ret++;
944 *new_pos = iter;
945 return ret;
948 static long move_prev_words(HTMLTxtRange *This, long cnt, const dompos_t *pos, dompos_t *new_pos)
950 dompos_t iter, tmp;
951 long ret = 0;
953 iter = *pos;
954 dompos_addref(&iter);
956 while(ret < cnt) {
957 if(!find_prev_space(This, &iter, FALSE, &tmp))
958 break;
960 dompos_release(&iter);
961 iter = tmp;
962 ret++;
965 *new_pos = iter;
966 return ret;
969 #define HTMLTXTRANGE_THIS(iface) DEFINE_THIS(HTMLTxtRange, HTMLTxtRange, iface)
971 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
973 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
975 *ppv = NULL;
977 if(IsEqualGUID(&IID_IUnknown, riid)) {
978 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
979 *ppv = HTMLTXTRANGE(This);
980 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
981 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
982 *ppv = HTMLTXTRANGE(This);
983 }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
984 TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
985 *ppv = HTMLTXTRANGE(This);
986 }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
987 TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
988 *ppv = CMDTARGET(This);
991 if(*ppv) {
992 IUnknown_AddRef((IUnknown*)*ppv);
993 return S_OK;
996 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
997 return E_NOINTERFACE;
1000 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
1002 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1003 LONG ref = InterlockedIncrement(&This->ref);
1005 TRACE("(%p) ref=%d\n", This, ref);
1007 return ref;
1010 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1012 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1013 LONG ref = InterlockedDecrement(&This->ref);
1015 TRACE("(%p) ref=%d\n", This, ref);
1017 if(!ref) {
1018 if(This->nsrange)
1019 nsISelection_Release(This->nsrange);
1020 if(This->doc)
1021 list_remove(&This->entry);
1022 heap_free(This);
1025 return ref;
1028 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1030 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1031 FIXME("(%p)->(%p)\n", This, pctinfo);
1032 return E_NOTIMPL;
1035 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1036 LCID lcid, ITypeInfo **ppTInfo)
1038 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1039 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1040 return E_NOTIMPL;
1043 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1044 LPOLESTR *rgszNames, UINT cNames,
1045 LCID lcid, DISPID *rgDispId)
1047 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1048 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1049 lcid, rgDispId);
1050 return E_NOTIMPL;
1053 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1054 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1055 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1057 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1058 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1059 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1060 return E_NOTIMPL;
1063 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1065 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1067 TRACE("(%p)->(%p)\n", This, p);
1069 *p = NULL;
1071 if(This->nsrange) {
1072 nsIDOMDocumentFragment *fragment;
1073 nsresult nsres;
1075 nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1076 if(NS_SUCCEEDED(nsres)) {
1077 const PRUnichar *nstext;
1078 nsAString nsstr;
1080 nsAString_Init(&nsstr, NULL);
1081 nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1082 nsIDOMDocumentFragment_Release(fragment);
1084 nsAString_GetData(&nsstr, &nstext);
1085 *p = SysAllocString(nstext);
1087 nsAString_Finish(&nsstr);
1091 if(!*p) {
1092 const WCHAR emptyW[] = {0};
1093 *p = SysAllocString(emptyW);
1096 TRACE("return %s\n", debugstr_w(*p));
1097 return S_OK;
1100 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1102 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1103 nsIDOMDocument *nsdoc;
1104 nsIDOMText *text_node;
1105 nsAString text_str;
1106 nsresult nsres;
1108 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1110 if(!This->doc)
1111 return MSHTML_E_NODOC;
1113 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1114 if(NS_FAILED(nsres)) {
1115 ERR("GetDocument failed: %08x\n", nsres);
1116 return S_OK;
1119 nsAString_Init(&text_str, v);
1120 nsres = nsIDOMDocument_CreateTextNode(nsdoc, &text_str, &text_node);
1121 nsIDOMDocument_Release(nsdoc);
1122 nsAString_Finish(&text_str);
1123 if(NS_FAILED(nsres)) {
1124 ERR("CreateTextNode failed: %08x\n", nsres);
1125 return S_OK;
1127 nsres = nsIDOMRange_DeleteContents(This->nsrange);
1128 if(NS_FAILED(nsres))
1129 ERR("DeleteContents failed: %08x\n", nsres);
1131 nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1132 if(NS_FAILED(nsres))
1133 ERR("InsertNode failed: %08x\n", nsres);
1135 nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1136 if(NS_FAILED(nsres))
1137 ERR("SetEndAfter failed: %08x\n", nsres);
1139 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), VARIANT_FALSE);
1142 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1144 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1145 wstrbuf_t buf;
1147 TRACE("(%p)->(%p)\n", This, p);
1149 *p = NULL;
1150 if(!This->nsrange)
1151 return S_OK;
1153 wstrbuf_init(&buf);
1154 range_to_string(This, &buf);
1155 if(buf.buf)
1156 *p = SysAllocString(buf.buf);
1157 wstrbuf_finish(&buf);
1159 TRACE("ret %s\n", debugstr_w(*p));
1160 return S_OK;
1163 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1165 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1166 nsIDOMNode *nsnode, *tmp;
1167 HTMLDOMNode *node;
1169 TRACE("(%p)->(%p)\n", This, parent);
1171 nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1172 while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1173 nsIDOMNode_GetParentNode(nsnode, &tmp);
1174 nsIDOMNode_Release(nsnode);
1175 nsnode = tmp;
1178 if(!nsnode) {
1179 *parent = NULL;
1180 return S_OK;
1183 node = get_node(This->doc, nsnode, TRUE);
1184 nsIDOMNode_Release(nsnode);
1186 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)parent);
1189 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1191 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1192 nsIDOMRange *nsrange = NULL;
1194 TRACE("(%p)->(%p)\n", This, Duplicate);
1196 nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1197 *Duplicate = HTMLTxtRange_Create(This->doc, nsrange);
1198 nsIDOMRange_Release(nsrange);
1200 return S_OK;
1203 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1204 VARIANT_BOOL *InRange)
1206 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1207 HTMLTxtRange *src_range;
1208 PRInt16 nsret = 0;
1209 nsresult nsres;
1211 TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1213 *InRange = VARIANT_FALSE;
1215 src_range = get_range_object(This->doc, Range);
1216 if(!src_range)
1217 return E_FAIL;
1219 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1220 src_range->nsrange, &nsret);
1221 if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1222 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1223 src_range->nsrange, &nsret);
1224 if(NS_SUCCEEDED(nsres) && nsret >= 0)
1225 *InRange = VARIANT_TRUE;
1228 if(NS_FAILED(nsres))
1229 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1231 return S_OK;
1234 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1235 VARIANT_BOOL *IsEqual)
1237 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1238 HTMLTxtRange *src_range;
1239 PRInt16 nsret = 0;
1240 nsresult nsres;
1242 TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1244 *IsEqual = VARIANT_FALSE;
1246 src_range = get_range_object(This->doc, Range);
1247 if(!src_range)
1248 return E_FAIL;
1250 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1251 src_range->nsrange, &nsret);
1252 if(NS_SUCCEEDED(nsres) && !nsret) {
1253 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1254 src_range->nsrange, &nsret);
1255 if(NS_SUCCEEDED(nsres) && !nsret)
1256 *IsEqual = VARIANT_TRUE;
1259 if(NS_FAILED(nsres))
1260 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1262 return S_OK;
1265 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1267 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1268 FIXME("(%p)->(%x)\n", This, fStart);
1269 return E_NOTIMPL;
1272 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1274 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1276 TRACE("(%p)->(%x)\n", This, Start);
1278 nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1279 return S_OK;
1282 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1284 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1285 range_unit_t unit;
1287 TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1289 unit = string_to_unit(Unit);
1290 if(unit == RU_UNKNOWN)
1291 return E_INVALIDARG;
1293 *Success = VARIANT_FALSE;
1295 switch(unit) {
1296 case RU_WORD: {
1297 dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1298 PRBool collapsed;
1300 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1302 get_cur_pos(This, TRUE, &start_pos);
1303 get_cur_pos(This, FALSE, &end_pos);
1305 if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1306 set_range_pos(This, FALSE, &new_end_pos);
1307 *Success = VARIANT_TRUE;
1310 if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1311 if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1312 set_range_pos(This, TRUE, &new_start_pos);
1313 *Success = VARIANT_TRUE;
1315 dompos_release(&new_start_pos);
1318 dompos_release(&new_end_pos);
1319 dompos_release(&end_pos);
1320 dompos_release(&start_pos);
1322 break;
1325 case RU_TEXTEDIT: {
1326 nsIDOMDocument *nsdoc;
1327 nsIDOMHTMLDocument *nshtmldoc;
1328 nsIDOMHTMLElement *nsbody = NULL;
1329 nsresult nsres;
1331 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1332 if(NS_FAILED(nsres) || !nsdoc) {
1333 ERR("GetDocument failed: %08x\n", nsres);
1334 break;
1337 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
1338 nsIDOMDocument_Release(nsdoc);
1340 nsres = nsIDOMHTMLDocument_GetBody(nshtmldoc, &nsbody);
1341 nsIDOMHTMLDocument_Release(nshtmldoc);
1342 if(NS_FAILED(nsres) || !nsbody) {
1343 ERR("Could not get body: %08x\n", nsres);
1344 break;
1347 nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1348 nsIDOMHTMLElement_Release(nsbody);
1349 if(NS_FAILED(nsres)) {
1350 ERR("Collapse failed: %08x\n", nsres);
1351 break;
1354 *Success = VARIANT_TRUE;
1355 break;
1358 default:
1359 FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1362 return S_OK;
1365 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1366 long Count, long *ActualCount)
1368 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1369 range_unit_t unit;
1371 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1373 unit = string_to_unit(Unit);
1374 if(unit == RU_UNKNOWN)
1375 return E_INVALIDARG;
1377 if(!Count) {
1378 *ActualCount = 0;
1379 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1382 switch(unit) {
1383 case RU_CHAR: {
1384 dompos_t cur_pos, new_pos;
1386 get_cur_pos(This, TRUE, &cur_pos);
1388 if(Count > 0) {
1389 *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1390 set_range_pos(This, FALSE, &new_pos);
1391 dompos_release(&new_pos);
1393 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1394 }else {
1395 *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1396 set_range_pos(This, TRUE, &new_pos);
1397 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1398 dompos_release(&new_pos);
1401 dompos_release(&cur_pos);
1402 break;
1405 case RU_WORD: {
1406 dompos_t cur_pos, new_pos;
1408 get_cur_pos(This, TRUE, &cur_pos);
1410 if(Count > 0) {
1411 *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1412 set_range_pos(This, FALSE, &new_pos);
1413 dompos_release(&new_pos);
1414 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1415 }else {
1416 *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1417 set_range_pos(This, TRUE, &new_pos);
1418 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1419 dompos_release(&new_pos);
1422 dompos_release(&cur_pos);
1423 break;
1426 default:
1427 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1430 TRACE("ret %ld\n", *ActualCount);
1431 return S_OK;
1434 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1435 long Count, long *ActualCount)
1437 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1438 range_unit_t unit;
1440 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1442 unit = string_to_unit(Unit);
1443 if(unit == RU_UNKNOWN)
1444 return E_INVALIDARG;
1446 if(!Count) {
1447 *ActualCount = 0;
1448 return S_OK;
1451 switch(unit) {
1452 case RU_CHAR: {
1453 dompos_t start_pos, end_pos, new_pos;
1454 PRBool collapsed;
1456 get_cur_pos(This, TRUE, &start_pos);
1457 get_cur_pos(This, FALSE, &end_pos);
1458 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1460 if(Count > 0) {
1461 BOOL bounded;
1463 *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1464 set_range_pos(This, !bounded, &new_pos);
1465 if(bounded)
1466 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1467 }else {
1468 *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1469 set_range_pos(This, TRUE, &new_pos);
1472 dompos_release(&start_pos);
1473 dompos_release(&end_pos);
1474 dompos_release(&new_pos);
1475 break;
1478 default:
1479 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1482 return S_OK;
1485 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1486 long Count, long *ActualCount)
1488 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1489 range_unit_t unit;
1491 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1493 unit = string_to_unit(Unit);
1494 if(unit == RU_UNKNOWN)
1495 return E_INVALIDARG;
1497 if(!Count) {
1498 *ActualCount = 0;
1499 return S_OK;
1502 switch(unit) {
1503 case RU_CHAR: {
1504 dompos_t start_pos, end_pos, new_pos;
1505 PRBool collapsed;
1507 get_cur_pos(This, TRUE, &start_pos);
1508 get_cur_pos(This, FALSE, &end_pos);
1509 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1511 if(Count > 0) {
1512 *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1513 set_range_pos(This, FALSE, &new_pos);
1514 }else {
1515 BOOL bounded;
1517 *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1518 set_range_pos(This, bounded, &new_pos);
1519 if(bounded)
1520 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1523 dompos_release(&start_pos);
1524 dompos_release(&end_pos);
1525 dompos_release(&new_pos);
1526 break;
1529 default:
1530 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1533 return S_OK;
1536 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1538 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1540 TRACE("(%p)\n", This);
1542 if(This->doc->nscontainer) {
1543 nsIDOMWindow *dom_window = NULL;
1544 nsISelection *nsselection;
1546 nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &dom_window);
1547 nsIDOMWindow_GetSelection(dom_window, &nsselection);
1548 nsIDOMWindow_Release(dom_window);
1550 nsISelection_RemoveAllRanges(nsselection);
1551 nsISelection_AddRange(nsselection, This->nsrange);
1553 nsISelection_Release(nsselection);
1556 return S_OK;
1559 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1561 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1562 FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1563 return E_NOTIMPL;
1566 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1568 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1569 FIXME("(%p)->(%p)\n", This, element);
1570 return E_NOTIMPL;
1573 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1574 IHTMLTxtRange *SourceRange)
1576 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1577 FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1578 return E_NOTIMPL;
1581 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1582 IHTMLTxtRange *SourceRange, long *ret)
1584 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1585 HTMLTxtRange *src_range;
1586 PRInt16 nsret = 0;
1587 int nscmpt;
1588 nsresult nsres;
1590 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1592 nscmpt = string_to_nscmptype(how);
1593 if(nscmpt == -1)
1594 return E_INVALIDARG;
1596 src_range = get_range_object(This->doc, SourceRange);
1597 if(!src_range)
1598 return E_FAIL;
1600 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1601 if(NS_FAILED(nsres))
1602 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1604 *ret = nsret;
1605 return S_OK;
1608 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1609 long count, long Flags, VARIANT_BOOL *Success)
1611 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1612 FIXME("(%p)->(%s %ld %08lx %p)\n", This, debugstr_w(String), count, Flags, Success);
1613 return E_NOTIMPL;
1616 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, long x, long y)
1618 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1619 FIXME("(%p)->(%ld %ld)\n", This, x, y);
1620 return E_NOTIMPL;
1623 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1625 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1626 FIXME("(%p)->(%p)\n", This, Bookmark);
1627 return E_NOTIMPL;
1630 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1631 VARIANT_BOOL *Success)
1633 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1634 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1635 return E_NOTIMPL;
1638 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1639 VARIANT_BOOL *pfRet)
1641 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1642 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1643 return E_NOTIMPL;
1646 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1647 VARIANT_BOOL *pfRet)
1649 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1650 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1651 return E_NOTIMPL;
1654 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1655 VARIANT_BOOL *pfRet)
1657 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1658 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1659 return E_NOTIMPL;
1662 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1663 VARIANT_BOOL *pfRet)
1665 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1666 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1667 return E_NOTIMPL;
1670 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1671 BSTR *pcmdText)
1673 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1674 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1675 return E_NOTIMPL;
1678 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1679 VARIANT *pcmdValue)
1681 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1682 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1683 return E_NOTIMPL;
1686 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1687 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1689 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1690 FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1691 return E_NOTIMPL;
1694 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1695 VARIANT_BOOL *pfRet)
1697 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1698 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1699 return E_NOTIMPL;
1702 #undef HTMLTXTRANGE_THIS
1704 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1705 HTMLTxtRange_QueryInterface,
1706 HTMLTxtRange_AddRef,
1707 HTMLTxtRange_Release,
1708 HTMLTxtRange_GetTypeInfoCount,
1709 HTMLTxtRange_GetTypeInfo,
1710 HTMLTxtRange_GetIDsOfNames,
1711 HTMLTxtRange_Invoke,
1712 HTMLTxtRange_get_htmlText,
1713 HTMLTxtRange_put_text,
1714 HTMLTxtRange_get_text,
1715 HTMLTxtRange_parentElement,
1716 HTMLTxtRange_duplicate,
1717 HTMLTxtRange_inRange,
1718 HTMLTxtRange_isEqual,
1719 HTMLTxtRange_scrollIntoView,
1720 HTMLTxtRange_collapse,
1721 HTMLTxtRange_expand,
1722 HTMLTxtRange_move,
1723 HTMLTxtRange_moveStart,
1724 HTMLTxtRange_moveEnd,
1725 HTMLTxtRange_select,
1726 HTMLTxtRange_pasteHTML,
1727 HTMLTxtRange_moveToElementText,
1728 HTMLTxtRange_setEndPoint,
1729 HTMLTxtRange_compareEndPoints,
1730 HTMLTxtRange_findText,
1731 HTMLTxtRange_moveToPoint,
1732 HTMLTxtRange_getBookmark,
1733 HTMLTxtRange_moveToBookmark,
1734 HTMLTxtRange_queryCommandSupported,
1735 HTMLTxtRange_queryCommandEnabled,
1736 HTMLTxtRange_queryCommandState,
1737 HTMLTxtRange_queryCommandIndeterm,
1738 HTMLTxtRange_queryCommandText,
1739 HTMLTxtRange_queryCommandValue,
1740 HTMLTxtRange_execCommand,
1741 HTMLTxtRange_execCommandShowHelp
1744 #define OLECMDTRG_THIS(iface) DEFINE_THIS(HTMLTxtRange, OleCommandTarget, iface)
1746 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1748 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1749 return IHTMLTxtRange_QueryInterface(HTMLTXTRANGE(This), riid, ppv);
1752 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1754 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1755 return IHTMLTxtRange_AddRef(HTMLTXTRANGE(This));
1758 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1760 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1761 return IHTMLTxtRange_Release(HTMLTXTRANGE(This));
1764 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1765 ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1767 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1768 FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1769 return E_NOTIMPL;
1772 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1774 nsIDOMDocumentFragment *fragment;
1775 nsIDOMElement *blockquote_elem, *p_elem;
1776 nsIDOMDocument *nsdoc;
1777 nsIDOMNode *tmp;
1778 nsAString tag_str;
1780 static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1781 static const PRUnichar pW[] = {'P',0};
1783 TRACE("(%p)->(%p %p)\n", This, in, out);
1785 nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1787 nsAString_Init(&tag_str, blockquoteW);
1788 nsIDOMDocument_CreateElement(nsdoc, &tag_str, &blockquote_elem);
1789 nsAString_Finish(&tag_str);
1791 nsAString_Init(&tag_str, pW);
1792 nsIDOMDocument_CreateElement(nsdoc, &tag_str, &p_elem);
1793 nsAString_Finish(&tag_str);
1795 nsIDOMDocument_Release(nsdoc);
1797 nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1798 nsIDOMElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1799 nsIDOMDocumentFragment_Release(fragment);
1800 nsIDOMNode_Release(tmp);
1802 nsIDOMElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1803 nsIDOMElement_Release(p_elem);
1804 nsIDOMNode_Release(tmp);
1806 nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1807 nsIDOMElement_Release(blockquote_elem);
1809 return S_OK;
1812 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1813 DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1815 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1817 TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1818 nCmdexecopt, pvaIn, pvaOut);
1820 if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1821 switch(nCmdID) {
1822 case IDM_INDENT:
1823 return exec_indent(This, pvaIn, pvaOut);
1824 default:
1825 FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1827 }else {
1828 FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1831 return E_NOTIMPL;
1834 #undef OLECMDTRG_THIS
1836 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1837 RangeCommandTarget_QueryInterface,
1838 RangeCommandTarget_AddRef,
1839 RangeCommandTarget_Release,
1840 RangeCommandTarget_QueryStatus,
1841 RangeCommandTarget_Exec
1844 IHTMLTxtRange *HTMLTxtRange_Create(HTMLDocument *doc, nsIDOMRange *nsrange)
1846 HTMLTxtRange *ret = heap_alloc(sizeof(HTMLTxtRange));
1848 ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
1849 ret->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
1850 ret->ref = 1;
1852 if(nsrange)
1853 nsIDOMRange_AddRef(nsrange);
1854 ret->nsrange = nsrange;
1856 ret->doc = doc;
1857 list_add_head(&doc->range_list, &ret->entry);
1859 return HTMLTXTRANGE(ret);
1862 void detach_ranges(HTMLDocument *This)
1864 HTMLTxtRange *iter;
1866 LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1867 iter->doc = NULL;