mshtml: Added document keydown, keyup, dragstart and selectstart event implementation.
[wine/multimedia.git] / dlls / mshtml / txtrange.c
blob3f383d11ae730166a82608682add6b1c32c9464e
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 nsIDOMHTMLElement *nsbody = NULL;
1312 nsresult nsres;
1314 nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
1315 if(NS_FAILED(nsres) || !nsbody) {
1316 ERR("Could not get body: %08x\n", nsres);
1317 break;
1320 nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1321 nsIDOMHTMLElement_Release(nsbody);
1322 if(NS_FAILED(nsres)) {
1323 ERR("Collapse failed: %08x\n", nsres);
1324 break;
1327 *Success = VARIANT_TRUE;
1328 break;
1331 default:
1332 FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1335 return S_OK;
1338 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1339 long Count, long *ActualCount)
1341 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1342 range_unit_t unit;
1344 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1346 unit = string_to_unit(Unit);
1347 if(unit == RU_UNKNOWN)
1348 return E_INVALIDARG;
1350 if(!Count) {
1351 *ActualCount = 0;
1352 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1355 switch(unit) {
1356 case RU_CHAR: {
1357 dompos_t cur_pos, new_pos;
1359 get_cur_pos(This, TRUE, &cur_pos);
1361 if(Count > 0) {
1362 *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1363 set_range_pos(This, FALSE, &new_pos);
1364 dompos_release(&new_pos);
1366 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1367 }else {
1368 *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1369 set_range_pos(This, TRUE, &new_pos);
1370 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1371 dompos_release(&new_pos);
1374 dompos_release(&cur_pos);
1375 break;
1378 case RU_WORD: {
1379 dompos_t cur_pos, new_pos;
1381 get_cur_pos(This, TRUE, &cur_pos);
1383 if(Count > 0) {
1384 *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1385 set_range_pos(This, FALSE, &new_pos);
1386 dompos_release(&new_pos);
1387 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1388 }else {
1389 *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1390 set_range_pos(This, TRUE, &new_pos);
1391 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1392 dompos_release(&new_pos);
1395 dompos_release(&cur_pos);
1396 break;
1399 default:
1400 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1403 TRACE("ret %ld\n", *ActualCount);
1404 return S_OK;
1407 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1408 long Count, long *ActualCount)
1410 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1411 range_unit_t unit;
1413 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1415 unit = string_to_unit(Unit);
1416 if(unit == RU_UNKNOWN)
1417 return E_INVALIDARG;
1419 if(!Count) {
1420 *ActualCount = 0;
1421 return S_OK;
1424 switch(unit) {
1425 case RU_CHAR: {
1426 dompos_t start_pos, end_pos, new_pos;
1427 PRBool collapsed;
1429 get_cur_pos(This, TRUE, &start_pos);
1430 get_cur_pos(This, FALSE, &end_pos);
1431 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1433 if(Count > 0) {
1434 BOOL bounded;
1436 *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1437 set_range_pos(This, !bounded, &new_pos);
1438 if(bounded)
1439 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1440 }else {
1441 *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1442 set_range_pos(This, TRUE, &new_pos);
1445 dompos_release(&start_pos);
1446 dompos_release(&end_pos);
1447 dompos_release(&new_pos);
1448 break;
1451 default:
1452 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1455 return S_OK;
1458 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1459 long Count, long *ActualCount)
1461 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1462 range_unit_t unit;
1464 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1466 unit = string_to_unit(Unit);
1467 if(unit == RU_UNKNOWN)
1468 return E_INVALIDARG;
1470 if(!Count) {
1471 *ActualCount = 0;
1472 return S_OK;
1475 switch(unit) {
1476 case RU_CHAR: {
1477 dompos_t start_pos, end_pos, new_pos;
1478 PRBool collapsed;
1480 get_cur_pos(This, TRUE, &start_pos);
1481 get_cur_pos(This, FALSE, &end_pos);
1482 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1484 if(Count > 0) {
1485 *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1486 set_range_pos(This, FALSE, &new_pos);
1487 }else {
1488 BOOL bounded;
1490 *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1491 set_range_pos(This, bounded, &new_pos);
1492 if(bounded)
1493 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1496 dompos_release(&start_pos);
1497 dompos_release(&end_pos);
1498 dompos_release(&new_pos);
1499 break;
1502 default:
1503 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1506 return S_OK;
1509 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1511 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1513 TRACE("(%p)\n", This);
1515 if(This->doc->nscontainer) {
1516 nsIDOMWindow *dom_window = NULL;
1517 nsISelection *nsselection;
1519 nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &dom_window);
1520 nsIDOMWindow_GetSelection(dom_window, &nsselection);
1521 nsIDOMWindow_Release(dom_window);
1523 nsISelection_RemoveAllRanges(nsselection);
1524 nsISelection_AddRange(nsselection, This->nsrange);
1526 nsISelection_Release(nsselection);
1529 return S_OK;
1532 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1534 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1535 FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1536 return E_NOTIMPL;
1539 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1541 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1542 FIXME("(%p)->(%p)\n", This, element);
1543 return E_NOTIMPL;
1546 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1547 IHTMLTxtRange *SourceRange)
1549 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1550 FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1551 return E_NOTIMPL;
1554 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1555 IHTMLTxtRange *SourceRange, long *ret)
1557 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1558 HTMLTxtRange *src_range;
1559 PRInt16 nsret = 0;
1560 int nscmpt;
1561 nsresult nsres;
1563 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1565 nscmpt = string_to_nscmptype(how);
1566 if(nscmpt == -1)
1567 return E_INVALIDARG;
1569 src_range = get_range_object(This->doc, SourceRange);
1570 if(!src_range)
1571 return E_FAIL;
1573 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1574 if(NS_FAILED(nsres))
1575 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1577 *ret = nsret;
1578 return S_OK;
1581 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1582 long count, long Flags, VARIANT_BOOL *Success)
1584 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1585 FIXME("(%p)->(%s %ld %08lx %p)\n", This, debugstr_w(String), count, Flags, Success);
1586 return E_NOTIMPL;
1589 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, long x, long y)
1591 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1592 FIXME("(%p)->(%ld %ld)\n", This, x, y);
1593 return E_NOTIMPL;
1596 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1598 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1599 FIXME("(%p)->(%p)\n", This, Bookmark);
1600 return E_NOTIMPL;
1603 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1604 VARIANT_BOOL *Success)
1606 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1607 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1608 return E_NOTIMPL;
1611 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1612 VARIANT_BOOL *pfRet)
1614 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1615 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1616 return E_NOTIMPL;
1619 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1620 VARIANT_BOOL *pfRet)
1622 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1623 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1624 return E_NOTIMPL;
1627 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1628 VARIANT_BOOL *pfRet)
1630 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1631 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1632 return E_NOTIMPL;
1635 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1636 VARIANT_BOOL *pfRet)
1638 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1639 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1640 return E_NOTIMPL;
1643 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1644 BSTR *pcmdText)
1646 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1647 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1648 return E_NOTIMPL;
1651 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1652 VARIANT *pcmdValue)
1654 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1655 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1656 return E_NOTIMPL;
1659 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1660 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1662 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1663 FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1664 return E_NOTIMPL;
1667 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1668 VARIANT_BOOL *pfRet)
1670 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1671 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1672 return E_NOTIMPL;
1675 #undef HTMLTXTRANGE_THIS
1677 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1678 HTMLTxtRange_QueryInterface,
1679 HTMLTxtRange_AddRef,
1680 HTMLTxtRange_Release,
1681 HTMLTxtRange_GetTypeInfoCount,
1682 HTMLTxtRange_GetTypeInfo,
1683 HTMLTxtRange_GetIDsOfNames,
1684 HTMLTxtRange_Invoke,
1685 HTMLTxtRange_get_htmlText,
1686 HTMLTxtRange_put_text,
1687 HTMLTxtRange_get_text,
1688 HTMLTxtRange_parentElement,
1689 HTMLTxtRange_duplicate,
1690 HTMLTxtRange_inRange,
1691 HTMLTxtRange_isEqual,
1692 HTMLTxtRange_scrollIntoView,
1693 HTMLTxtRange_collapse,
1694 HTMLTxtRange_expand,
1695 HTMLTxtRange_move,
1696 HTMLTxtRange_moveStart,
1697 HTMLTxtRange_moveEnd,
1698 HTMLTxtRange_select,
1699 HTMLTxtRange_pasteHTML,
1700 HTMLTxtRange_moveToElementText,
1701 HTMLTxtRange_setEndPoint,
1702 HTMLTxtRange_compareEndPoints,
1703 HTMLTxtRange_findText,
1704 HTMLTxtRange_moveToPoint,
1705 HTMLTxtRange_getBookmark,
1706 HTMLTxtRange_moveToBookmark,
1707 HTMLTxtRange_queryCommandSupported,
1708 HTMLTxtRange_queryCommandEnabled,
1709 HTMLTxtRange_queryCommandState,
1710 HTMLTxtRange_queryCommandIndeterm,
1711 HTMLTxtRange_queryCommandText,
1712 HTMLTxtRange_queryCommandValue,
1713 HTMLTxtRange_execCommand,
1714 HTMLTxtRange_execCommandShowHelp
1717 #define OLECMDTRG_THIS(iface) DEFINE_THIS(HTMLTxtRange, OleCommandTarget, iface)
1719 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1721 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1722 return IHTMLTxtRange_QueryInterface(HTMLTXTRANGE(This), riid, ppv);
1725 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1727 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1728 return IHTMLTxtRange_AddRef(HTMLTXTRANGE(This));
1731 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1733 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1734 return IHTMLTxtRange_Release(HTMLTXTRANGE(This));
1737 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1738 ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1740 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1741 FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1742 return E_NOTIMPL;
1745 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1747 nsIDOMDocumentFragment *fragment;
1748 nsIDOMElement *blockquote_elem, *p_elem;
1749 nsIDOMNode *tmp;
1750 nsAString tag_str;
1752 static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1753 static const PRUnichar pW[] = {'P',0};
1755 TRACE("(%p)->(%p %p)\n", This, in, out);
1757 if(!This->doc->nsdoc) {
1758 WARN("NULL nsdoc\n");
1759 return E_NOTIMPL;
1762 nsAString_Init(&tag_str, blockquoteW);
1763 nsIDOMHTMLDocument_CreateElement(This->doc->nsdoc, &tag_str, &blockquote_elem);
1764 nsAString_Finish(&tag_str);
1766 nsAString_Init(&tag_str, pW);
1767 nsIDOMDocument_CreateElement(This->doc->nsdoc, &tag_str, &p_elem);
1768 nsAString_Finish(&tag_str);
1770 nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1771 nsIDOMElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1772 nsIDOMDocumentFragment_Release(fragment);
1773 nsIDOMNode_Release(tmp);
1775 nsIDOMElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1776 nsIDOMElement_Release(p_elem);
1777 nsIDOMNode_Release(tmp);
1779 nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1780 nsIDOMElement_Release(blockquote_elem);
1782 return S_OK;
1785 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1786 DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1788 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1790 TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1791 nCmdexecopt, pvaIn, pvaOut);
1793 if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1794 switch(nCmdID) {
1795 case IDM_INDENT:
1796 return exec_indent(This, pvaIn, pvaOut);
1797 default:
1798 FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1800 }else {
1801 FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1804 return E_NOTIMPL;
1807 #undef OLECMDTRG_THIS
1809 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1810 RangeCommandTarget_QueryInterface,
1811 RangeCommandTarget_AddRef,
1812 RangeCommandTarget_Release,
1813 RangeCommandTarget_QueryStatus,
1814 RangeCommandTarget_Exec
1817 IHTMLTxtRange *HTMLTxtRange_Create(HTMLDocument *doc, nsIDOMRange *nsrange)
1819 HTMLTxtRange *ret = heap_alloc(sizeof(HTMLTxtRange));
1821 ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
1822 ret->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
1823 ret->ref = 1;
1825 if(nsrange)
1826 nsIDOMRange_AddRef(nsrange);
1827 ret->nsrange = nsrange;
1829 ret->doc = doc;
1830 list_add_head(&doc->range_list, &ret->entry);
1832 return HTMLTXTRANGE(ret);
1835 void detach_ranges(HTMLDocument *This)
1837 HTMLTxtRange *iter;
1839 LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1840 iter->doc = NULL;