push cea036dc4aae50c232dc1391dcc459ff0b060bcf
[wine/hacks.git] / dlls / mshtml / txtrange.c
blobc35834fde35ac849ca9efbab0d72ca8d81cac2da
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 nsIDOMHTMLElement *nselem;
330 nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nselem);
331 nsIDOMElement_GetLastChild(nselem, &tmp);
332 if(!tmp)
333 return (nsIDOMNode*)nselem;
335 while(tmp) {
336 ret = tmp;
337 nsIDOMNode_GetLastChild(ret, &tmp);
340 nsIDOMElement_Release(nselem);
342 return ret;
345 nsres = nsIDOMNode_GetLastChild(iter, &ret);
346 if(NS_SUCCEEDED(nsres) && ret)
347 return ret;
349 nsIDOMNode_AddRef(iter);
351 do {
352 nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
353 if(NS_SUCCEEDED(nsres) && ret) {
354 nsIDOMNode_Release(iter);
355 return ret;
358 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
359 nsIDOMNode_Release(iter);
360 iter = tmp;
361 }while(NS_SUCCEEDED(nsres) && iter);
363 return NULL;
366 static nsIDOMNode *get_child_node(nsIDOMNode *node, PRUint32 off)
368 nsIDOMNodeList *node_list;
369 nsIDOMNode *ret = NULL;
371 nsIDOMNode_GetChildNodes(node, &node_list);
372 nsIDOMNodeList_Item(node_list, off, &ret);
373 nsIDOMNodeList_Release(node_list);
375 return ret;
378 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
380 nsIDOMNode *node;
381 PRInt32 off;
383 pos->p = NULL;
385 if(!start) {
386 PRBool collapsed;
387 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
388 start = collapsed;
391 if(start) {
392 nsIDOMRange_GetStartContainer(This->nsrange, &node);
393 nsIDOMRange_GetStartOffset(This->nsrange, &off);
394 }else {
395 nsIDOMRange_GetEndContainer(This->nsrange, &node);
396 nsIDOMRange_GetEndOffset(This->nsrange, &off);
399 pos->type = get_node_type(node);
400 if(pos->type == ELEMENT_NODE) {
401 if(start) {
402 pos->node = get_child_node(node, off);
403 pos->off = 0;
404 }else {
405 pos->node = off ? get_child_node(node, off-1) : prev_node(This, node);
406 pos->off = -1;
409 pos->type = get_node_type(pos->node);
410 nsIDOMNode_Release(node);
411 }else if(start) {
412 pos->node = node;
413 pos->off = off;
414 }else if(off) {
415 pos->node = node;
416 pos->off = off-1;
417 }else {
418 pos->node = prev_node(This, node);
419 pos->off = -1;
420 nsIDOMNode_Release(node);
423 if(pos->type == TEXT_NODE)
424 fill_nodestr(pos);
427 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
429 nsresult nsres;
431 if(start) {
432 if(pos->type == TEXT_NODE)
433 nsres = nsIDOMRange_SetStart(This->nsrange, pos->node, pos->off);
434 else
435 nsres = nsIDOMRange_SetStartBefore(This->nsrange, pos->node);
436 }else {
437 if(pos->type == TEXT_NODE && pos->p[pos->off+1])
438 nsres = nsIDOMRange_SetEnd(This->nsrange, pos->node, pos->off+1);
439 else
440 nsres = nsIDOMRange_SetEndAfter(This->nsrange, pos->node);
443 if(NS_FAILED(nsres))
444 ERR("failed: %p %08x\n", pos->node, nsres);
447 static inline void dompos_release(dompos_t *pos)
449 if(pos->node)
450 nsIDOMNode_Release(pos->node);
452 if(pos->p)
453 nsAString_Finish(&pos->str);
456 static inline void dompos_addref(dompos_t *pos)
458 if(pos->node)
459 nsIDOMNode_AddRef(pos->node);
461 if(pos->type == TEXT_NODE)
462 fill_nodestr(pos);
465 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
467 return pos1->node == pos2->node && pos1->off == pos2->off;
470 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
472 nsIDOMNode *iter, *tmp;
473 dompos_t start_pos, end_pos;
474 PRBool collapsed;
476 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
477 if(collapsed) {
478 wstrbuf_finish(buf);
479 buf->buf = NULL;
480 buf->size = 0;
481 return;
484 get_cur_pos(This, FALSE, &end_pos);
485 get_cur_pos(This, TRUE, &start_pos);
487 if(start_pos.type == TEXT_NODE) {
488 if(start_pos.node == end_pos.node) {
489 wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, end_pos.off-start_pos.off+1);
490 iter = start_pos.node;
491 nsIDOMNode_AddRef(iter);
492 }else {
493 wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, strlenW(start_pos.p+start_pos.off));
494 iter = next_node(start_pos.node);
496 }else {
497 iter = start_pos.node;
498 nsIDOMNode_AddRef(iter);
501 while(iter != end_pos.node) {
502 wstrbuf_append_node(buf, iter);
503 tmp = next_node(iter);
504 nsIDOMNode_Release(iter);
505 iter = tmp;
508 nsIDOMNode_AddRef(end_pos.node);
510 if(start_pos.node != end_pos.node) {
511 if(end_pos.type == TEXT_NODE)
512 wstrbuf_append_nodetxt(buf, end_pos.p, end_pos.off+1);
513 else
514 wstrbuf_append_node(buf, end_pos.node);
517 nsIDOMNode_Release(iter);
518 dompos_release(&start_pos);
519 dompos_release(&end_pos);
521 if(buf->len) {
522 WCHAR *p;
524 for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);
526 p = strchrW(p, '\r');
527 if(p)
528 *p = 0;
532 static WCHAR get_pos_char(const dompos_t *pos)
534 switch(pos->type) {
535 case TEXT_NODE:
536 return pos->p[pos->off];
537 case ELEMENT_NODE:
538 if(is_space_elem(pos->node))
539 return '\n';
542 return 0;
545 static void end_space(const dompos_t *pos, dompos_t *new_pos)
547 const WCHAR *p;
549 *new_pos = *pos;
550 dompos_addref(new_pos);
552 if(pos->type != TEXT_NODE)
553 return;
555 p = new_pos->p+new_pos->off;
557 if(!*p || !isspace(*p))
558 return;
560 while(p[1] && isspace(p[1]))
561 p++;
563 new_pos->off = p - new_pos->p;
566 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
568 nsIDOMNode *iter, *tmp;
569 dompos_t last_space, tmp_pos;
570 const WCHAR *p;
571 WCHAR cspace = 0;
573 if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off]) {
574 p = pos->p+pos->off;
576 if(isspace(*p))
577 while(isspaceW(*++p));
578 else
579 p++;
581 if(*p && isspaceW(*p)) {
582 cspace = ' ';
583 while(p[1] && isspaceW(p[1]))
584 p++;
587 if(*p) {
588 *new_pos = *pos;
589 new_pos->off = p - pos->p;
590 dompos_addref(new_pos);
592 return cspace ? cspace : *p;
593 }else {
594 last_space = *pos;
595 last_space.off = p - pos->p;
596 dompos_addref(&last_space);
600 iter = next_node(pos->node);
602 while(iter) {
603 switch(get_node_type(iter)) {
604 case TEXT_NODE:
605 tmp_pos.node = iter;
606 tmp_pos.type = TEXT_NODE;
607 tmp_pos.off = 0;
608 dompos_addref(&tmp_pos);
610 p = tmp_pos.p;
612 if(!*p) {
613 dompos_release(&tmp_pos);
614 break;
615 }else if(isspaceW(*p)) {
616 if(cspace)
617 dompos_release(&last_space);
618 else
619 cspace = ' ';
621 while(p[1] && isspaceW(p[1]))
622 p++;
624 tmp_pos.off = p-tmp_pos.p;
626 if(!p[1]) {
627 last_space = tmp_pos;
628 break;
631 *new_pos = tmp_pos;
632 nsIDOMNode_Release(iter);
633 return cspace;
634 }else if(cspace) {
635 *new_pos = last_space;
636 dompos_release(&tmp_pos);
637 nsIDOMNode_Release(iter);
639 return cspace;
640 }else if(*p) {
641 tmp_pos.off = 0;
642 *new_pos = tmp_pos;
645 nsIDOMNode_Release(iter);
646 return *p;
648 case ELEMENT_NODE:
649 if(is_elem_tag(iter, brW)) {
650 if(cspace)
651 dompos_release(&last_space);
652 cspace = '\n';
654 nsIDOMNode_AddRef(iter);
655 last_space.node = iter;
656 last_space.type = ELEMENT_NODE;
657 last_space.off = 0;
658 last_space.p = NULL;
659 }else if(is_elem_tag(iter, hrW)) {
660 if(cspace) {
661 *new_pos = last_space;
662 nsIDOMNode_Release(iter);
663 return cspace;
666 new_pos->node = iter;
667 new_pos->type = ELEMENT_NODE;
668 new_pos->off = 0;
669 new_pos->p = NULL;
670 return '\n';
674 tmp = iter;
675 iter = next_node(iter);
676 nsIDOMNode_Release(tmp);
679 if(cspace) {
680 *new_pos = last_space;
681 }else {
682 *new_pos = *pos;
683 dompos_addref(new_pos);
686 return cspace;
689 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
691 nsIDOMNode *iter, *tmp;
692 const WCHAR *p;
693 BOOL skip_space = FALSE;
695 if(pos->type == TEXT_NODE && isspaceW(pos->p[pos->off]))
696 skip_space = TRUE;
698 if(pos->type == TEXT_NODE && pos->off) {
699 p = pos->p+pos->off-1;
701 if(skip_space) {
702 while(p >= pos->p && isspace(*p))
703 p--;
706 if(p >= pos->p) {
707 *new_pos = *pos;
708 new_pos->off = p-pos->p;
709 dompos_addref(new_pos);
710 return new_pos->p[new_pos->off];
714 iter = prev_node(This, pos->node);
716 while(iter) {
717 switch(get_node_type(iter)) {
718 case TEXT_NODE: {
719 dompos_t tmp_pos;
721 tmp_pos.node = iter;
722 tmp_pos.type = TEXT_NODE;
723 tmp_pos.off = 0;
724 dompos_addref(&tmp_pos);
726 p = tmp_pos.p + strlenW(tmp_pos.p)-1;
728 if(skip_space) {
729 while(p >= tmp_pos.p && isspaceW(*p))
730 p--;
733 if(p < tmp_pos.p) {
734 dompos_release(&tmp_pos);
735 break;
738 tmp_pos.off = p-tmp_pos.p;
739 *new_pos = tmp_pos;
740 nsIDOMNode_Release(iter);
741 return *p;
744 case ELEMENT_NODE:
745 if(is_elem_tag(iter, brW)) {
746 if(skip_space) {
747 skip_space = FALSE;
748 break;
750 }else if(!is_elem_tag(iter, hrW)) {
751 break;
754 new_pos->node = iter;
755 new_pos->type = ELEMENT_NODE;
756 new_pos->off = 0;
757 new_pos->p = NULL;
758 return '\n';
761 tmp = iter;
762 iter = prev_node(This, iter);
763 nsIDOMNode_Release(tmp);
766 *new_pos = *pos;
767 dompos_addref(new_pos);
768 return 0;
771 static long move_next_chars(long cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
772 BOOL *bounded, dompos_t *new_pos)
774 dompos_t iter, tmp;
775 long ret = 0;
776 WCHAR c;
778 if(bounded)
779 *bounded = FALSE;
781 if(col)
782 ret++;
784 if(ret >= cnt) {
785 end_space(pos, new_pos);
786 return ret;
789 c = next_char(pos, &iter);
790 ret++;
792 while(ret < cnt) {
793 tmp = iter;
794 c = next_char(&tmp, &iter);
795 dompos_release(&tmp);
796 if(!c)
797 break;
799 ret++;
800 if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
801 *bounded = TRUE;
802 ret++;
806 *new_pos = iter;
807 return ret;
810 static long move_prev_chars(HTMLTxtRange *This, long cnt, const dompos_t *pos, BOOL end,
811 const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
813 dompos_t iter, tmp;
814 long ret = 0;
815 BOOL prev_eq = FALSE;
816 WCHAR c;
818 if(bounded)
819 *bounded = FALSE;
821 c = prev_char(This, pos, &iter);
822 if(c)
823 ret++;
825 while(c && ret < cnt) {
826 tmp = iter;
827 c = prev_char(This, &tmp, &iter);
828 dompos_release(&tmp);
829 if(!c) {
830 if(end)
831 ret++;
832 break;
835 ret++;
837 if(prev_eq) {
838 *bounded = TRUE;
839 ret++;
842 prev_eq = bound_pos && dompos_cmp(&iter, bound_pos);
845 *new_pos = iter;
846 return ret;
849 static long find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
851 dompos_t iter, tmp;
852 WCHAR c;
854 c = prev_char(This, pos, &iter);
855 if(!c || (first_space && isspaceW(c))) {
856 *ret = iter;
857 return FALSE;
860 while(1) {
861 tmp = iter;
862 c = prev_char(This, &tmp, &iter);
863 if(!c || isspaceW(c)) {
864 dompos_release(&iter);
865 break;
867 dompos_release(&tmp);
870 *ret = tmp;
871 return TRUE;
874 static int find_word_end(const dompos_t *pos, dompos_t *ret)
876 dompos_t iter, tmp;
877 int cnt = 1;
878 WCHAR c;
879 c = get_pos_char(pos);
880 if(isspaceW(c)) {
881 *ret = *pos;
882 dompos_addref(ret);
883 return 0;
886 c = next_char(pos, &iter);
887 if(!c) {
888 *ret = iter;
889 return 0;
891 if(c == '\n') {
892 *ret = *pos;
893 dompos_addref(ret);
894 return 0;
897 while(c && !isspaceW(c)) {
898 tmp = iter;
899 c = next_char(&tmp, &iter);
900 if(c == '\n') {
901 dompos_release(&iter);
902 iter = tmp;
903 }else {
904 cnt++;
905 dompos_release(&tmp);
909 *ret = iter;
910 return cnt;
913 static long move_next_words(long cnt, const dompos_t *pos, dompos_t *new_pos)
915 dompos_t iter, tmp;
916 long ret = 0;
917 WCHAR c;
919 c = get_pos_char(pos);
920 if(isspaceW(c)) {
921 end_space(pos, &iter);
922 ret++;
923 }else {
924 c = next_char(pos, &iter);
925 if(c && isspaceW(c))
926 ret++;
929 while(c && ret < cnt) {
930 tmp = iter;
931 c = next_char(&tmp, &iter);
932 dompos_release(&tmp);
933 if(isspaceW(c))
934 ret++;
937 *new_pos = iter;
938 return ret;
941 static long move_prev_words(HTMLTxtRange *This, long cnt, const dompos_t *pos, dompos_t *new_pos)
943 dompos_t iter, tmp;
944 long ret = 0;
946 iter = *pos;
947 dompos_addref(&iter);
949 while(ret < cnt) {
950 if(!find_prev_space(This, &iter, FALSE, &tmp))
951 break;
953 dompos_release(&iter);
954 iter = tmp;
955 ret++;
958 *new_pos = iter;
959 return ret;
962 #define HTMLTXTRANGE_THIS(iface) DEFINE_THIS(HTMLTxtRange, HTMLTxtRange, iface)
964 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
966 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
968 *ppv = NULL;
970 if(IsEqualGUID(&IID_IUnknown, riid)) {
971 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
972 *ppv = HTMLTXTRANGE(This);
973 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
974 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
975 *ppv = HTMLTXTRANGE(This);
976 }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
977 TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
978 *ppv = HTMLTXTRANGE(This);
979 }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
980 TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
981 *ppv = CMDTARGET(This);
984 if(*ppv) {
985 IUnknown_AddRef((IUnknown*)*ppv);
986 return S_OK;
989 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
990 return E_NOINTERFACE;
993 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
995 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
996 LONG ref = InterlockedIncrement(&This->ref);
998 TRACE("(%p) ref=%d\n", This, ref);
1000 return ref;
1003 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1005 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1006 LONG ref = InterlockedDecrement(&This->ref);
1008 TRACE("(%p) ref=%d\n", This, ref);
1010 if(!ref) {
1011 if(This->nsrange)
1012 nsISelection_Release(This->nsrange);
1013 if(This->doc)
1014 list_remove(&This->entry);
1015 heap_free(This);
1018 return ref;
1021 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1023 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1024 FIXME("(%p)->(%p)\n", This, pctinfo);
1025 return E_NOTIMPL;
1028 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1029 LCID lcid, ITypeInfo **ppTInfo)
1031 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1032 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1033 return E_NOTIMPL;
1036 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1037 LPOLESTR *rgszNames, UINT cNames,
1038 LCID lcid, DISPID *rgDispId)
1040 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1041 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1042 lcid, rgDispId);
1043 return E_NOTIMPL;
1046 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1047 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1048 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1050 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1051 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1052 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1053 return E_NOTIMPL;
1056 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1058 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1060 TRACE("(%p)->(%p)\n", This, p);
1062 *p = NULL;
1064 if(This->nsrange) {
1065 nsIDOMDocumentFragment *fragment;
1066 nsresult nsres;
1068 nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1069 if(NS_SUCCEEDED(nsres)) {
1070 const PRUnichar *nstext;
1071 nsAString nsstr;
1073 nsAString_Init(&nsstr, NULL);
1074 nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1075 nsIDOMDocumentFragment_Release(fragment);
1077 nsAString_GetData(&nsstr, &nstext);
1078 *p = SysAllocString(nstext);
1080 nsAString_Finish(&nsstr);
1084 if(!*p) {
1085 const WCHAR emptyW[] = {0};
1086 *p = SysAllocString(emptyW);
1089 TRACE("return %s\n", debugstr_w(*p));
1090 return S_OK;
1093 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1095 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1096 nsIDOMText *text_node;
1097 nsAString text_str;
1098 nsresult nsres;
1100 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1102 if(!This->doc)
1103 return MSHTML_E_NODOC;
1105 nsAString_Init(&text_str, v);
1106 nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc->nsdoc, &text_str, &text_node);
1107 nsAString_Finish(&text_str);
1108 if(NS_FAILED(nsres)) {
1109 ERR("CreateTextNode failed: %08x\n", nsres);
1110 return S_OK;
1112 nsres = nsIDOMRange_DeleteContents(This->nsrange);
1113 if(NS_FAILED(nsres))
1114 ERR("DeleteContents failed: %08x\n", nsres);
1116 nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1117 if(NS_FAILED(nsres))
1118 ERR("InsertNode failed: %08x\n", nsres);
1120 nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1121 if(NS_FAILED(nsres))
1122 ERR("SetEndAfter failed: %08x\n", nsres);
1124 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), VARIANT_FALSE);
1127 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1129 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1130 wstrbuf_t buf;
1132 TRACE("(%p)->(%p)\n", This, p);
1134 *p = NULL;
1135 if(!This->nsrange)
1136 return S_OK;
1138 wstrbuf_init(&buf);
1139 range_to_string(This, &buf);
1140 if(buf.buf)
1141 *p = SysAllocString(buf.buf);
1142 wstrbuf_finish(&buf);
1144 TRACE("ret %s\n", debugstr_w(*p));
1145 return S_OK;
1148 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1150 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1151 nsIDOMNode *nsnode, *tmp;
1152 HTMLDOMNode *node;
1154 TRACE("(%p)->(%p)\n", This, parent);
1156 nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1157 while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1158 nsIDOMNode_GetParentNode(nsnode, &tmp);
1159 nsIDOMNode_Release(nsnode);
1160 nsnode = tmp;
1163 if(!nsnode) {
1164 *parent = NULL;
1165 return S_OK;
1168 node = get_node(This->doc, nsnode, TRUE);
1169 nsIDOMNode_Release(nsnode);
1171 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)parent);
1174 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1176 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1177 nsIDOMRange *nsrange = NULL;
1179 TRACE("(%p)->(%p)\n", This, Duplicate);
1181 nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1182 *Duplicate = HTMLTxtRange_Create(This->doc, nsrange);
1183 nsIDOMRange_Release(nsrange);
1185 return S_OK;
1188 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1189 VARIANT_BOOL *InRange)
1191 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1192 HTMLTxtRange *src_range;
1193 PRInt16 nsret = 0;
1194 nsresult nsres;
1196 TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1198 *InRange = VARIANT_FALSE;
1200 src_range = get_range_object(This->doc, Range);
1201 if(!src_range)
1202 return E_FAIL;
1204 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1205 src_range->nsrange, &nsret);
1206 if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1207 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1208 src_range->nsrange, &nsret);
1209 if(NS_SUCCEEDED(nsres) && nsret >= 0)
1210 *InRange = VARIANT_TRUE;
1213 if(NS_FAILED(nsres))
1214 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1216 return S_OK;
1219 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1220 VARIANT_BOOL *IsEqual)
1222 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1223 HTMLTxtRange *src_range;
1224 PRInt16 nsret = 0;
1225 nsresult nsres;
1227 TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1229 *IsEqual = VARIANT_FALSE;
1231 src_range = get_range_object(This->doc, Range);
1232 if(!src_range)
1233 return E_FAIL;
1235 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1236 src_range->nsrange, &nsret);
1237 if(NS_SUCCEEDED(nsres) && !nsret) {
1238 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1239 src_range->nsrange, &nsret);
1240 if(NS_SUCCEEDED(nsres) && !nsret)
1241 *IsEqual = VARIANT_TRUE;
1244 if(NS_FAILED(nsres))
1245 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1247 return S_OK;
1250 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1252 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1253 FIXME("(%p)->(%x)\n", This, fStart);
1254 return E_NOTIMPL;
1257 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1259 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1261 TRACE("(%p)->(%x)\n", This, Start);
1263 nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1264 return S_OK;
1267 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1269 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1270 range_unit_t unit;
1272 TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1274 unit = string_to_unit(Unit);
1275 if(unit == RU_UNKNOWN)
1276 return E_INVALIDARG;
1278 *Success = VARIANT_FALSE;
1280 switch(unit) {
1281 case RU_WORD: {
1282 dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1283 PRBool collapsed;
1285 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1287 get_cur_pos(This, TRUE, &start_pos);
1288 get_cur_pos(This, FALSE, &end_pos);
1290 if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1291 set_range_pos(This, FALSE, &new_end_pos);
1292 *Success = VARIANT_TRUE;
1295 if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1296 if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1297 set_range_pos(This, TRUE, &new_start_pos);
1298 *Success = VARIANT_TRUE;
1300 dompos_release(&new_start_pos);
1303 dompos_release(&new_end_pos);
1304 dompos_release(&end_pos);
1305 dompos_release(&start_pos);
1307 break;
1310 case RU_TEXTEDIT: {
1311 nsIDOMDocument *nsdoc;
1312 nsIDOMHTMLDocument *nshtmldoc;
1313 nsIDOMHTMLElement *nsbody = NULL;
1314 nsresult nsres;
1316 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1317 if(NS_FAILED(nsres) || !nsdoc) {
1318 ERR("GetDocument failed: %08x\n", nsres);
1319 break;
1322 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
1323 nsIDOMDocument_Release(nsdoc);
1325 nsres = nsIDOMHTMLDocument_GetBody(nshtmldoc, &nsbody);
1326 nsIDOMHTMLDocument_Release(nshtmldoc);
1327 if(NS_FAILED(nsres) || !nsbody) {
1328 ERR("Could not get body: %08x\n", nsres);
1329 break;
1332 nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1333 nsIDOMHTMLElement_Release(nsbody);
1334 if(NS_FAILED(nsres)) {
1335 ERR("Collapse failed: %08x\n", nsres);
1336 break;
1339 *Success = VARIANT_TRUE;
1340 break;
1343 default:
1344 FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1347 return S_OK;
1350 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1351 long Count, long *ActualCount)
1353 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1354 range_unit_t unit;
1356 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1358 unit = string_to_unit(Unit);
1359 if(unit == RU_UNKNOWN)
1360 return E_INVALIDARG;
1362 if(!Count) {
1363 *ActualCount = 0;
1364 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1367 switch(unit) {
1368 case RU_CHAR: {
1369 dompos_t cur_pos, new_pos;
1371 get_cur_pos(This, TRUE, &cur_pos);
1373 if(Count > 0) {
1374 *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1375 set_range_pos(This, FALSE, &new_pos);
1376 dompos_release(&new_pos);
1378 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1379 }else {
1380 *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1381 set_range_pos(This, TRUE, &new_pos);
1382 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1383 dompos_release(&new_pos);
1386 dompos_release(&cur_pos);
1387 break;
1390 case RU_WORD: {
1391 dompos_t cur_pos, new_pos;
1393 get_cur_pos(This, TRUE, &cur_pos);
1395 if(Count > 0) {
1396 *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1397 set_range_pos(This, FALSE, &new_pos);
1398 dompos_release(&new_pos);
1399 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1400 }else {
1401 *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1402 set_range_pos(This, TRUE, &new_pos);
1403 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1404 dompos_release(&new_pos);
1407 dompos_release(&cur_pos);
1408 break;
1411 default:
1412 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1415 TRACE("ret %ld\n", *ActualCount);
1416 return S_OK;
1419 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1420 long Count, long *ActualCount)
1422 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1423 range_unit_t unit;
1425 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1427 unit = string_to_unit(Unit);
1428 if(unit == RU_UNKNOWN)
1429 return E_INVALIDARG;
1431 if(!Count) {
1432 *ActualCount = 0;
1433 return S_OK;
1436 switch(unit) {
1437 case RU_CHAR: {
1438 dompos_t start_pos, end_pos, new_pos;
1439 PRBool collapsed;
1441 get_cur_pos(This, TRUE, &start_pos);
1442 get_cur_pos(This, FALSE, &end_pos);
1443 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1445 if(Count > 0) {
1446 BOOL bounded;
1448 *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1449 set_range_pos(This, !bounded, &new_pos);
1450 if(bounded)
1451 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1452 }else {
1453 *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1454 set_range_pos(This, TRUE, &new_pos);
1457 dompos_release(&start_pos);
1458 dompos_release(&end_pos);
1459 dompos_release(&new_pos);
1460 break;
1463 default:
1464 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1467 return S_OK;
1470 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1471 long Count, long *ActualCount)
1473 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1474 range_unit_t unit;
1476 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1478 unit = string_to_unit(Unit);
1479 if(unit == RU_UNKNOWN)
1480 return E_INVALIDARG;
1482 if(!Count) {
1483 *ActualCount = 0;
1484 return S_OK;
1487 switch(unit) {
1488 case RU_CHAR: {
1489 dompos_t start_pos, end_pos, new_pos;
1490 PRBool collapsed;
1492 get_cur_pos(This, TRUE, &start_pos);
1493 get_cur_pos(This, FALSE, &end_pos);
1494 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1496 if(Count > 0) {
1497 *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1498 set_range_pos(This, FALSE, &new_pos);
1499 }else {
1500 BOOL bounded;
1502 *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1503 set_range_pos(This, bounded, &new_pos);
1504 if(bounded)
1505 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1508 dompos_release(&start_pos);
1509 dompos_release(&end_pos);
1510 dompos_release(&new_pos);
1511 break;
1514 default:
1515 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1518 return S_OK;
1521 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1523 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1525 TRACE("(%p)\n", This);
1527 if(This->doc->nscontainer) {
1528 nsIDOMWindow *dom_window = NULL;
1529 nsISelection *nsselection;
1531 nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &dom_window);
1532 nsIDOMWindow_GetSelection(dom_window, &nsselection);
1533 nsIDOMWindow_Release(dom_window);
1535 nsISelection_RemoveAllRanges(nsselection);
1536 nsISelection_AddRange(nsselection, This->nsrange);
1538 nsISelection_Release(nsselection);
1541 return S_OK;
1544 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1546 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1547 FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1548 return E_NOTIMPL;
1551 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1553 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1554 FIXME("(%p)->(%p)\n", This, element);
1555 return E_NOTIMPL;
1558 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1559 IHTMLTxtRange *SourceRange)
1561 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1562 FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1563 return E_NOTIMPL;
1566 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1567 IHTMLTxtRange *SourceRange, long *ret)
1569 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1570 HTMLTxtRange *src_range;
1571 PRInt16 nsret = 0;
1572 int nscmpt;
1573 nsresult nsres;
1575 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1577 nscmpt = string_to_nscmptype(how);
1578 if(nscmpt == -1)
1579 return E_INVALIDARG;
1581 src_range = get_range_object(This->doc, SourceRange);
1582 if(!src_range)
1583 return E_FAIL;
1585 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1586 if(NS_FAILED(nsres))
1587 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1589 *ret = nsret;
1590 return S_OK;
1593 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1594 long count, long Flags, VARIANT_BOOL *Success)
1596 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1597 FIXME("(%p)->(%s %ld %08lx %p)\n", This, debugstr_w(String), count, Flags, Success);
1598 return E_NOTIMPL;
1601 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, long x, long y)
1603 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1604 FIXME("(%p)->(%ld %ld)\n", This, x, y);
1605 return E_NOTIMPL;
1608 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1610 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1611 FIXME("(%p)->(%p)\n", This, Bookmark);
1612 return E_NOTIMPL;
1615 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1616 VARIANT_BOOL *Success)
1618 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1619 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1620 return E_NOTIMPL;
1623 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1624 VARIANT_BOOL *pfRet)
1626 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1627 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1628 return E_NOTIMPL;
1631 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1632 VARIANT_BOOL *pfRet)
1634 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1635 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1636 return E_NOTIMPL;
1639 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1640 VARIANT_BOOL *pfRet)
1642 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1643 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1644 return E_NOTIMPL;
1647 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1648 VARIANT_BOOL *pfRet)
1650 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1651 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1652 return E_NOTIMPL;
1655 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1656 BSTR *pcmdText)
1658 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1659 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1660 return E_NOTIMPL;
1663 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1664 VARIANT *pcmdValue)
1666 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1667 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1668 return E_NOTIMPL;
1671 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1672 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1674 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1675 FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1676 return E_NOTIMPL;
1679 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1680 VARIANT_BOOL *pfRet)
1682 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1683 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1684 return E_NOTIMPL;
1687 #undef HTMLTXTRANGE_THIS
1689 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1690 HTMLTxtRange_QueryInterface,
1691 HTMLTxtRange_AddRef,
1692 HTMLTxtRange_Release,
1693 HTMLTxtRange_GetTypeInfoCount,
1694 HTMLTxtRange_GetTypeInfo,
1695 HTMLTxtRange_GetIDsOfNames,
1696 HTMLTxtRange_Invoke,
1697 HTMLTxtRange_get_htmlText,
1698 HTMLTxtRange_put_text,
1699 HTMLTxtRange_get_text,
1700 HTMLTxtRange_parentElement,
1701 HTMLTxtRange_duplicate,
1702 HTMLTxtRange_inRange,
1703 HTMLTxtRange_isEqual,
1704 HTMLTxtRange_scrollIntoView,
1705 HTMLTxtRange_collapse,
1706 HTMLTxtRange_expand,
1707 HTMLTxtRange_move,
1708 HTMLTxtRange_moveStart,
1709 HTMLTxtRange_moveEnd,
1710 HTMLTxtRange_select,
1711 HTMLTxtRange_pasteHTML,
1712 HTMLTxtRange_moveToElementText,
1713 HTMLTxtRange_setEndPoint,
1714 HTMLTxtRange_compareEndPoints,
1715 HTMLTxtRange_findText,
1716 HTMLTxtRange_moveToPoint,
1717 HTMLTxtRange_getBookmark,
1718 HTMLTxtRange_moveToBookmark,
1719 HTMLTxtRange_queryCommandSupported,
1720 HTMLTxtRange_queryCommandEnabled,
1721 HTMLTxtRange_queryCommandState,
1722 HTMLTxtRange_queryCommandIndeterm,
1723 HTMLTxtRange_queryCommandText,
1724 HTMLTxtRange_queryCommandValue,
1725 HTMLTxtRange_execCommand,
1726 HTMLTxtRange_execCommandShowHelp
1729 #define OLECMDTRG_THIS(iface) DEFINE_THIS(HTMLTxtRange, OleCommandTarget, iface)
1731 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1733 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1734 return IHTMLTxtRange_QueryInterface(HTMLTXTRANGE(This), riid, ppv);
1737 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1739 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1740 return IHTMLTxtRange_AddRef(HTMLTXTRANGE(This));
1743 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1745 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1746 return IHTMLTxtRange_Release(HTMLTXTRANGE(This));
1749 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1750 ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1752 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1753 FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1754 return E_NOTIMPL;
1757 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1759 nsIDOMDocumentFragment *fragment;
1760 nsIDOMElement *blockquote_elem, *p_elem;
1761 nsIDOMDocument *nsdoc;
1762 nsIDOMNode *tmp;
1763 nsAString tag_str;
1765 static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1766 static const PRUnichar pW[] = {'P',0};
1768 TRACE("(%p)->(%p %p)\n", This, in, out);
1770 nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1772 nsAString_Init(&tag_str, blockquoteW);
1773 nsIDOMDocument_CreateElement(nsdoc, &tag_str, &blockquote_elem);
1774 nsAString_Finish(&tag_str);
1776 nsAString_Init(&tag_str, pW);
1777 nsIDOMDocument_CreateElement(nsdoc, &tag_str, &p_elem);
1778 nsAString_Finish(&tag_str);
1780 nsIDOMDocument_Release(nsdoc);
1782 nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1783 nsIDOMElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1784 nsIDOMDocumentFragment_Release(fragment);
1785 nsIDOMNode_Release(tmp);
1787 nsIDOMElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1788 nsIDOMElement_Release(p_elem);
1789 nsIDOMNode_Release(tmp);
1791 nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1792 nsIDOMElement_Release(blockquote_elem);
1794 return S_OK;
1797 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1798 DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1800 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1802 TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1803 nCmdexecopt, pvaIn, pvaOut);
1805 if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1806 switch(nCmdID) {
1807 case IDM_INDENT:
1808 return exec_indent(This, pvaIn, pvaOut);
1809 default:
1810 FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1812 }else {
1813 FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1816 return E_NOTIMPL;
1819 #undef OLECMDTRG_THIS
1821 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1822 RangeCommandTarget_QueryInterface,
1823 RangeCommandTarget_AddRef,
1824 RangeCommandTarget_Release,
1825 RangeCommandTarget_QueryStatus,
1826 RangeCommandTarget_Exec
1829 IHTMLTxtRange *HTMLTxtRange_Create(HTMLDocument *doc, nsIDOMRange *nsrange)
1831 HTMLTxtRange *ret = heap_alloc(sizeof(HTMLTxtRange));
1833 ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
1834 ret->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
1835 ret->ref = 1;
1837 if(nsrange)
1838 nsIDOMRange_AddRef(nsrange);
1839 ret->nsrange = nsrange;
1841 ret->doc = doc;
1842 list_add_head(&doc->range_list, &ret->entry);
1844 return HTMLTXTRANGE(ret);
1847 void detach_ranges(HTMLDocument *This)
1849 HTMLTxtRange *iter;
1851 LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1852 iter->doc = NULL;