mshtml: hr element is not a space element for moving functions.
[wine/wine64.git] / dlls / mshtml / txtrange.c
blob7e505df16e31a6c30a38f68abe451fd65cf86d88
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 "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31 #include "mshtmcid.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "mshtml_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 static const WCHAR brW[] = {'b','r',0};
41 static const WCHAR hrW[] = {'h','r',0};
43 typedef struct {
44 const IHTMLTxtRangeVtbl *lpHTMLTxtRangeVtbl;
45 const IOleCommandTargetVtbl *lpOleCommandTargetVtbl;
47 LONG ref;
49 nsIDOMRange *nsrange;
50 HTMLDocument *doc;
52 struct list entry;
53 } HTMLTxtRange;
55 #define HTMLTXTRANGE(x) ((IHTMLTxtRange*) &(x)->lpHTMLTxtRangeVtbl)
57 typedef struct {
58 WCHAR *buf;
59 DWORD len;
60 DWORD size;
61 } wstrbuf_t;
63 typedef struct {
64 PRUint16 type;
65 nsIDOMNode *node;
66 PRUint32 off;
67 nsAString str;
68 const PRUnichar *p;
69 } dompos_t;
71 typedef enum {
72 RU_UNKNOWN,
73 RU_CHAR,
74 RU_WORD,
75 RU_SENTENCE,
76 RU_TEXTEDIT
77 } range_unit_t;
79 static HTMLTxtRange *get_range_object(HTMLDocument *doc, IHTMLTxtRange *iface)
81 HTMLTxtRange *iter;
83 LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
84 if(HTMLTXTRANGE(iter) == iface)
85 return iter;
88 ERR("Could not find range in document\n");
89 return NULL;
92 static range_unit_t string_to_unit(LPCWSTR str)
94 static const WCHAR characterW[] =
95 {'c','h','a','r','a','c','t','e','r',0};
96 static const WCHAR wordW[] =
97 {'w','o','r','d',0};
98 static const WCHAR sentenceW[] =
99 {'s','e','n','t','e','n','c','e',0};
100 static const WCHAR texteditW[] =
101 {'t','e','x','t','e','d','i','t',0};
103 if(!strcmpiW(str, characterW)) return RU_CHAR;
104 if(!strcmpiW(str, wordW)) return RU_WORD;
105 if(!strcmpiW(str, sentenceW)) return RU_SENTENCE;
106 if(!strcmpiW(str, texteditW)) return RU_TEXTEDIT;
108 return RU_UNKNOWN;
111 static int string_to_nscmptype(LPCWSTR str)
113 static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
114 static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
115 static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
116 static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
118 if(!strcmpiW(str, seW)) return NS_START_TO_END;
119 if(!strcmpiW(str, ssW)) return NS_START_TO_START;
120 if(!strcmpiW(str, esW)) return NS_END_TO_START;
121 if(!strcmpiW(str, eeW)) return NS_END_TO_END;
123 return -1;
126 static PRUint16 get_node_type(nsIDOMNode *node)
128 PRUint16 type = 0;
130 if(node)
131 nsIDOMNode_GetNodeType(node, &type);
133 return type;
136 static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
138 nsIDOMElement *elem;
139 nsAString tag_str;
140 const PRUnichar *tag;
141 BOOL ret = FALSE;
142 nsresult nsres;
144 nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
145 if(NS_FAILED(nsres))
146 return FALSE;
148 nsAString_Init(&tag_str, NULL);
149 nsIDOMElement_GetTagName(elem, &tag_str);
150 nsIDOMElement_Release(elem);
151 nsAString_GetData(&tag_str, &tag);
153 ret = !strcmpiW(tag, istag);
155 nsAString_Finish(&tag_str);
157 return ret;
160 static BOOL is_space_elem(nsIDOMNode *node)
162 nsIDOMElement *elem;
163 nsAString tag_str;
164 const PRUnichar *tag;
165 BOOL ret = FALSE;
166 nsresult nsres;
168 nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
169 if(NS_FAILED(nsres))
170 return FALSE;
172 nsAString_Init(&tag_str, NULL);
173 nsIDOMElement_GetTagName(elem, &tag_str);
174 nsIDOMElement_Release(elem);
175 nsAString_GetData(&tag_str, &tag);
177 ret = !strcmpiW(tag, brW) || !strcmpiW(tag, hrW);
179 nsAString_Finish(&tag_str);
181 return ret;
184 static inline void wstrbuf_init(wstrbuf_t *buf)
186 buf->len = 0;
187 buf->size = 16;
188 buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
189 *buf->buf = 0;
192 static inline void wstrbuf_finish(wstrbuf_t *buf)
194 heap_free(buf->buf);
197 static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
199 if(buf->len+len >= buf->size) {
200 buf->size = 2*buf->len+len;
201 buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
204 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
205 buf->len += len;
206 buf->buf[buf->len] = 0;
209 static inline void wstrbuf_append(wstrbuf_t *buf, LPCWSTR str)
211 wstrbuf_append_len(buf, str, strlenW(str));
214 static void wstrbuf_append_nodetxt(wstrbuf_t *buf, LPCWSTR str, int len)
216 const WCHAR *s = str;
217 WCHAR *d;
219 TRACE("%s\n", debugstr_wn(str, len));
221 if(buf->len+len >= buf->size) {
222 buf->size = 2*buf->len+len;
223 buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
226 if(buf->len && isspaceW(buf->buf[buf->len-1])) {
227 while(s < str+len && isspaceW(*s))
228 s++;
231 d = buf->buf+buf->len;
232 while(s < str+len) {
233 if(isspaceW(*s)) {
234 *d++ = ' ';
235 s++;
236 while(s < str+len && isspaceW(*s))
237 s++;
238 }else {
239 *d++ = *s++;
243 buf->len = d - buf->buf;
244 *d = 0;
247 static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node)
250 switch(get_node_type(node)) {
251 case TEXT_NODE: {
252 nsIDOMText *nstext;
253 nsAString data_str;
254 const PRUnichar *data;
256 nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
258 nsAString_Init(&data_str, NULL);
259 nsIDOMText_GetData(nstext, &data_str);
260 nsAString_GetData(&data_str, &data);
261 wstrbuf_append_nodetxt(buf, data, strlenW(data));
262 nsAString_Finish(&data_str);
264 nsIDOMText_Release(nstext);
266 break;
268 case ELEMENT_NODE:
269 if(is_elem_tag(node, brW)) {
270 static const WCHAR endlW[] = {'\r','\n'};
271 wstrbuf_append_len(buf, endlW, 2);
272 }else if(is_elem_tag(node, hrW)) {
273 static const WCHAR endl2W[] = {'\r','\n','\r','\n'};
274 wstrbuf_append_len(buf, endl2W, 4);
279 static BOOL fill_nodestr(dompos_t *pos)
281 nsIDOMText *text;
282 nsresult nsres;
284 if(pos->type != TEXT_NODE)
285 return FALSE;
287 nsres = nsIDOMNode_QueryInterface(pos->node, &IID_nsIDOMText, (void**)&text);
288 if(NS_FAILED(nsres))
289 return FALSE;
291 nsAString_Init(&pos->str, NULL);
292 nsIDOMText_GetData(text, &pos->str);
293 nsIDOMText_Release(text);
294 nsAString_GetData(&pos->str, &pos->p);
296 if(pos->off == -1)
297 pos->off = *pos->p ? strlenW(pos->p)-1 : 0;
299 return TRUE;
302 static nsIDOMNode *next_node(nsIDOMNode *iter)
304 nsIDOMNode *ret, *tmp;
305 nsresult nsres;
307 if(!iter)
308 return NULL;
310 nsres = nsIDOMNode_GetFirstChild(iter, &ret);
311 if(NS_SUCCEEDED(nsres) && ret)
312 return ret;
314 nsIDOMNode_AddRef(iter);
316 do {
317 nsres = nsIDOMNode_GetNextSibling(iter, &ret);
318 if(NS_SUCCEEDED(nsres) && ret) {
319 nsIDOMNode_Release(iter);
320 return ret;
323 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
324 nsIDOMNode_Release(iter);
325 iter = tmp;
326 }while(NS_SUCCEEDED(nsres) && iter);
328 return NULL;
331 static nsIDOMNode *prev_node(HTMLTxtRange *This, nsIDOMNode *iter)
333 nsIDOMNode *ret, *tmp;
334 nsresult nsres;
336 if(!iter) {
337 nsIDOMHTMLDocument *nshtmldoc;
338 nsIDOMHTMLElement *nselem;
339 nsIDOMDocument *nsdoc;
341 nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
342 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
343 nsIDOMDocument_Release(nsdoc);
344 nsIDOMHTMLDocument_GetBody(nshtmldoc, &nselem);
345 nsIDOMHTMLDocument_Release(nshtmldoc);
347 nsIDOMElement_GetLastChild(nselem, &tmp);
348 if(!tmp)
349 return (nsIDOMNode*)nselem;
351 while(tmp) {
352 ret = tmp;
353 nsIDOMNode_GetLastChild(ret, &tmp);
356 nsIDOMElement_Release(nselem);
358 return ret;
361 nsres = nsIDOMNode_GetLastChild(iter, &ret);
362 if(NS_SUCCEEDED(nsres) && ret)
363 return ret;
365 nsIDOMNode_AddRef(iter);
367 do {
368 nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
369 if(NS_SUCCEEDED(nsres) && ret) {
370 nsIDOMNode_Release(iter);
371 return ret;
374 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
375 nsIDOMNode_Release(iter);
376 iter = tmp;
377 }while(NS_SUCCEEDED(nsres) && iter);
379 return NULL;
382 static nsIDOMNode *get_child_node(nsIDOMNode *node, PRUint32 off)
384 nsIDOMNodeList *node_list;
385 nsIDOMNode *ret = NULL;
387 nsIDOMNode_GetChildNodes(node, &node_list);
388 nsIDOMNodeList_Item(node_list, off, &ret);
389 nsIDOMNodeList_Release(node_list);
391 return ret;
394 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
396 nsIDOMNode *node;
397 PRInt32 off;
399 pos->p = NULL;
401 if(!start) {
402 PRBool collapsed;
403 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
404 start = collapsed;
407 if(start) {
408 nsIDOMRange_GetStartContainer(This->nsrange, &node);
409 nsIDOMRange_GetStartOffset(This->nsrange, &off);
410 }else {
411 nsIDOMRange_GetEndContainer(This->nsrange, &node);
412 nsIDOMRange_GetEndOffset(This->nsrange, &off);
415 pos->type = get_node_type(node);
416 if(pos->type == ELEMENT_NODE) {
417 if(start) {
418 pos->node = get_child_node(node, off);
419 pos->off = 0;
420 }else {
421 pos->node = off ? get_child_node(node, off-1) : prev_node(This, node);
422 pos->off = -1;
425 pos->type = get_node_type(pos->node);
426 nsIDOMNode_Release(node);
427 }else if(start) {
428 pos->node = node;
429 pos->off = off;
430 }else if(off) {
431 pos->node = node;
432 pos->off = off-1;
433 }else {
434 pos->node = prev_node(This, node);
435 pos->off = -1;
436 nsIDOMNode_Release(node);
439 if(pos->type == TEXT_NODE)
440 fill_nodestr(pos);
443 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
445 nsresult nsres;
447 if(start) {
448 if(pos->type == TEXT_NODE)
449 nsres = nsIDOMRange_SetStart(This->nsrange, pos->node, pos->off);
450 else
451 nsres = nsIDOMRange_SetStartBefore(This->nsrange, pos->node);
452 }else {
453 if(pos->type == TEXT_NODE && pos->p[pos->off+1])
454 nsres = nsIDOMRange_SetEnd(This->nsrange, pos->node, pos->off+1);
455 else
456 nsres = nsIDOMRange_SetEndAfter(This->nsrange, pos->node);
459 if(NS_FAILED(nsres))
460 ERR("failed: %p %08x\n", pos->node, nsres);
463 static inline void dompos_release(dompos_t *pos)
465 if(pos->node)
466 nsIDOMNode_Release(pos->node);
468 if(pos->p)
469 nsAString_Finish(&pos->str);
472 static inline void dompos_addref(dompos_t *pos)
474 if(pos->node)
475 nsIDOMNode_AddRef(pos->node);
477 if(pos->type == TEXT_NODE)
478 fill_nodestr(pos);
481 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
483 return pos1->node == pos2->node && pos1->off == pos2->off;
486 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
488 nsIDOMNode *iter, *tmp;
489 dompos_t start_pos, end_pos;
490 PRBool collapsed;
492 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
493 if(collapsed) {
494 wstrbuf_finish(buf);
495 buf->buf = NULL;
496 buf->size = 0;
497 return;
500 get_cur_pos(This, FALSE, &end_pos);
501 get_cur_pos(This, TRUE, &start_pos);
503 if(start_pos.type == TEXT_NODE) {
504 if(start_pos.node == end_pos.node) {
505 wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, end_pos.off-start_pos.off+1);
506 iter = start_pos.node;
507 nsIDOMNode_AddRef(iter);
508 }else {
509 wstrbuf_append_nodetxt(buf, start_pos.p+start_pos.off, strlenW(start_pos.p+start_pos.off));
510 iter = next_node(start_pos.node);
512 }else {
513 iter = start_pos.node;
514 nsIDOMNode_AddRef(iter);
517 while(iter != end_pos.node) {
518 wstrbuf_append_node(buf, iter);
519 tmp = next_node(iter);
520 nsIDOMNode_Release(iter);
521 iter = tmp;
524 nsIDOMNode_AddRef(end_pos.node);
526 if(start_pos.node != end_pos.node) {
527 if(end_pos.type == TEXT_NODE)
528 wstrbuf_append_nodetxt(buf, end_pos.p, end_pos.off+1);
529 else
530 wstrbuf_append_node(buf, end_pos.node);
533 nsIDOMNode_Release(iter);
534 dompos_release(&start_pos);
535 dompos_release(&end_pos);
537 if(buf->len) {
538 WCHAR *p;
540 for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);
542 p = strchrW(p, '\r');
543 if(p)
544 *p = 0;
548 static WCHAR get_pos_char(const dompos_t *pos)
550 switch(pos->type) {
551 case TEXT_NODE:
552 return pos->p[pos->off];
553 case ELEMENT_NODE:
554 if(is_space_elem(pos->node))
555 return '\n';
558 return 0;
561 static void end_space(const dompos_t *pos, dompos_t *new_pos)
563 const WCHAR *p;
565 *new_pos = *pos;
566 dompos_addref(new_pos);
568 if(pos->type != TEXT_NODE)
569 return;
571 p = new_pos->p+new_pos->off;
573 if(!*p || !isspace(*p))
574 return;
576 while(p[1] && isspace(p[1]))
577 p++;
579 new_pos->off = p - new_pos->p;
582 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
584 nsIDOMNode *iter, *tmp;
585 dompos_t last_space, tmp_pos;
586 const WCHAR *p;
587 WCHAR cspace = 0;
589 if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off]) {
590 p = pos->p+pos->off;
592 if(isspace(*p))
593 while(isspaceW(*++p));
594 else
595 p++;
597 if(*p && isspaceW(*p)) {
598 cspace = ' ';
599 while(p[1] && isspaceW(p[1]))
600 p++;
603 if(*p) {
604 *new_pos = *pos;
605 new_pos->off = p - pos->p;
606 dompos_addref(new_pos);
608 return cspace ? cspace : *p;
609 }else {
610 last_space = *pos;
611 last_space.off = p - pos->p;
612 dompos_addref(&last_space);
616 iter = next_node(pos->node);
618 while(iter) {
619 switch(get_node_type(iter)) {
620 case TEXT_NODE:
621 tmp_pos.node = iter;
622 tmp_pos.type = TEXT_NODE;
623 dompos_addref(&tmp_pos);
625 p = tmp_pos.p;
627 if(!*p) {
628 dompos_release(&tmp_pos);
629 break;
630 }else if(isspaceW(*p)) {
631 if(cspace)
632 dompos_release(&last_space);
633 else
634 cspace = ' ';
636 while(p[1] && isspaceW(p[1]))
637 p++;
639 tmp_pos.off = p-tmp_pos.p;
641 if(!p[1]) {
642 last_space = tmp_pos;
643 break;
646 *new_pos = tmp_pos;
647 nsIDOMNode_Release(iter);
648 return cspace;
649 }else if(cspace) {
650 *new_pos = last_space;
651 dompos_release(&tmp_pos);
652 nsIDOMNode_Release(iter);
654 return cspace;
655 }else if(*p) {
656 tmp_pos.off = 0;
657 *new_pos = tmp_pos;
660 nsIDOMNode_Release(iter);
661 return *p;
663 case ELEMENT_NODE:
664 if(is_elem_tag(iter, brW)) {
665 if(cspace)
666 dompos_release(&last_space);
667 cspace = '\n';
669 nsIDOMNode_AddRef(iter);
670 last_space.node = iter;
671 last_space.type = ELEMENT_NODE;
672 last_space.off = 0;
673 last_space.p = NULL;
674 }else if(is_elem_tag(iter, hrW)) {
675 if(cspace) {
676 *new_pos = last_space;
677 nsIDOMNode_Release(iter);
678 return cspace;
681 new_pos->node = iter;
682 new_pos->type = ELEMENT_NODE;
683 new_pos->off = 0;
684 new_pos->p = NULL;
685 return '\n';
689 tmp = iter;
690 iter = next_node(iter);
691 nsIDOMNode_Release(tmp);
694 if(cspace) {
695 *new_pos = last_space;
696 }else {
697 *new_pos = *pos;
698 dompos_addref(new_pos);
701 return cspace;
704 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
706 nsIDOMNode *iter, *tmp;
707 const WCHAR *p;
708 BOOL skip_space = FALSE;
710 if(pos->type == TEXT_NODE && isspaceW(pos->p[pos->off]))
711 skip_space = TRUE;
713 if(pos->type == TEXT_NODE && pos->off) {
714 p = pos->p+pos->off-1;
716 if(skip_space) {
717 while(p >= pos->p && isspace(*p))
718 p--;
721 if(p >= pos->p) {
722 *new_pos = *pos;
723 new_pos->off = p-pos->p;
724 dompos_addref(new_pos);
725 return new_pos->p[new_pos->off];
729 iter = prev_node(This, pos->node);
731 while(iter) {
732 switch(get_node_type(iter)) {
733 case TEXT_NODE: {
734 dompos_t tmp_pos;
736 tmp_pos.node = iter;
737 tmp_pos.type = TEXT_NODE;
738 dompos_addref(&tmp_pos);
740 p = tmp_pos.p + strlenW(tmp_pos.p)-1;
742 if(skip_space) {
743 while(p >= tmp_pos.p && isspaceW(*p))
744 p--;
747 if(p < tmp_pos.p) {
748 dompos_release(&tmp_pos);
749 break;
752 tmp_pos.off = p-tmp_pos.p;
753 *new_pos = tmp_pos;
754 nsIDOMNode_Release(iter);
755 return *p;
758 case ELEMENT_NODE:
759 if(is_elem_tag(iter, brW)) {
760 if(skip_space) {
761 skip_space = FALSE;
762 break;
764 }else if(!is_elem_tag(iter, hrW)) {
765 break;
768 new_pos->node = iter;
769 new_pos->type = ELEMENT_NODE;
770 new_pos->off = 0;
771 new_pos->p = NULL;
772 return '\n';
775 tmp = iter;
776 iter = prev_node(This, iter);
777 nsIDOMNode_Release(tmp);
780 *new_pos = *pos;
781 dompos_addref(new_pos);
782 return 0;
785 static long move_next_chars(long cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
786 BOOL *bounded, dompos_t *new_pos)
788 dompos_t iter, tmp;
789 long ret = 0;
790 WCHAR c;
792 if(bounded)
793 *bounded = FALSE;
795 if(col)
796 ret++;
798 if(ret >= cnt) {
799 end_space(pos, new_pos);
800 return ret;
803 c = next_char(pos, &iter);
804 ret++;
806 while(ret < cnt) {
807 tmp = iter;
808 c = next_char(&tmp, &iter);
809 dompos_release(&tmp);
810 if(!c)
811 break;
813 ret++;
814 if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
815 *bounded = TRUE;
816 ret++;
820 *new_pos = iter;
821 return ret;
824 static long move_prev_chars(HTMLTxtRange *This, long cnt, const dompos_t *pos, BOOL end,
825 const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
827 dompos_t iter, tmp;
828 long ret = 0;
829 WCHAR c;
831 if(bounded)
832 *bounded = FALSE;
834 c = prev_char(This, pos, &iter);
835 if(c)
836 ret++;
838 while(c && ret < cnt) {
839 tmp = iter;
840 c = prev_char(This, &tmp, &iter);
841 dompos_release(&tmp);
842 if(!c) {
843 if(end)
844 ret++;
845 break;
848 ret++;
850 if(bound_pos && dompos_cmp(&iter, bound_pos)) {
851 *bounded = TRUE;
852 cnt--;
856 *new_pos = iter;
857 return bounded && *bounded ? ret+1 : ret;
860 static long find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
862 dompos_t iter, tmp;
863 WCHAR c;
865 c = prev_char(This, pos, &iter);
866 if(!c || (first_space && isspaceW(c))) {
867 *ret = iter;
868 return FALSE;
871 while(1) {
872 tmp = iter;
873 c = prev_char(This, &tmp, &iter);
874 if(!c || isspaceW(c)) {
875 dompos_release(&iter);
876 break;
878 dompos_release(&tmp);
881 *ret = tmp;
882 return TRUE;
885 static int find_word_end(const dompos_t *pos, dompos_t *ret)
887 dompos_t iter, tmp;
888 int cnt = 1;
889 WCHAR c;
890 c = get_pos_char(pos);
891 if(isspaceW(c)) {
892 *ret = *pos;
893 dompos_addref(ret);
894 return 0;
897 c = next_char(pos, &iter);
898 if(!c) {
899 *ret = iter;
900 return 0;
902 if(c == '\n') {
903 *ret = *pos;
904 dompos_addref(ret);
905 return 0;
908 while(c && !isspaceW(c)) {
909 tmp = iter;
910 c = next_char(&tmp, &iter);
911 if(c == '\n') {
912 dompos_release(&iter);
913 iter = tmp;
914 }else {
915 cnt++;
916 dompos_release(&tmp);
920 *ret = iter;
921 return cnt;
924 static long move_next_words(long cnt, const dompos_t *pos, dompos_t *new_pos)
926 dompos_t iter, tmp;
927 long ret = 0;
928 WCHAR c;
930 c = get_pos_char(pos);
931 if(isspaceW(c)) {
932 end_space(pos, &iter);
933 ret++;
934 }else {
935 c = next_char(pos, &iter);
936 if(c && isspaceW(c))
937 ret++;
940 while(c && ret < cnt) {
941 tmp = iter;
942 c = next_char(&tmp, &iter);
943 dompos_release(&tmp);
944 if(isspaceW(c))
945 ret++;
948 *new_pos = iter;
949 return ret;
952 static long move_prev_words(HTMLTxtRange *This, long cnt, const dompos_t *pos, dompos_t *new_pos)
954 dompos_t iter, tmp;
955 long ret = 0;
957 iter = *pos;
958 dompos_addref(&iter);
960 while(ret < cnt) {
961 if(!find_prev_space(This, &iter, FALSE, &tmp))
962 break;
964 dompos_release(&iter);
965 iter = tmp;
966 ret++;
969 *new_pos = iter;
970 return ret;
973 #define HTMLTXTRANGE_THIS(iface) DEFINE_THIS(HTMLTxtRange, HTMLTxtRange, iface)
975 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
977 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
979 *ppv = NULL;
981 if(IsEqualGUID(&IID_IUnknown, riid)) {
982 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
983 *ppv = HTMLTXTRANGE(This);
984 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
985 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
986 *ppv = HTMLTXTRANGE(This);
987 }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
988 TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
989 *ppv = HTMLTXTRANGE(This);
990 }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
991 TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
992 *ppv = CMDTARGET(This);
995 if(*ppv) {
996 IUnknown_AddRef((IUnknown*)*ppv);
997 return S_OK;
1000 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1001 return E_NOINTERFACE;
1004 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
1006 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1007 LONG ref = InterlockedIncrement(&This->ref);
1009 TRACE("(%p) ref=%d\n", This, ref);
1011 return ref;
1014 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1016 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1017 LONG ref = InterlockedDecrement(&This->ref);
1019 TRACE("(%p) ref=%d\n", This, ref);
1021 if(!ref) {
1022 if(This->nsrange)
1023 nsISelection_Release(This->nsrange);
1024 if(This->doc)
1025 list_remove(&This->entry);
1026 heap_free(This);
1029 return ref;
1032 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1034 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1035 FIXME("(%p)->(%p)\n", This, pctinfo);
1036 return E_NOTIMPL;
1039 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1040 LCID lcid, ITypeInfo **ppTInfo)
1042 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1043 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1044 return E_NOTIMPL;
1047 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1048 LPOLESTR *rgszNames, UINT cNames,
1049 LCID lcid, DISPID *rgDispId)
1051 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1052 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1053 lcid, rgDispId);
1054 return E_NOTIMPL;
1057 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1058 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1059 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1061 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1062 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1063 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1064 return E_NOTIMPL;
1067 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1069 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1071 TRACE("(%p)->(%p)\n", This, p);
1073 *p = NULL;
1075 if(This->nsrange) {
1076 nsIDOMDocumentFragment *fragment;
1077 nsresult nsres;
1079 nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1080 if(NS_SUCCEEDED(nsres)) {
1081 const PRUnichar *nstext;
1082 nsAString nsstr;
1084 nsAString_Init(&nsstr, NULL);
1085 nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1086 nsIDOMDocumentFragment_Release(fragment);
1088 nsAString_GetData(&nsstr, &nstext);
1089 *p = SysAllocString(nstext);
1091 nsAString_Finish(&nsstr);
1095 if(!*p) {
1096 const WCHAR emptyW[] = {0};
1097 *p = SysAllocString(emptyW);
1100 TRACE("return %s\n", debugstr_w(*p));
1101 return S_OK;
1104 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1106 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1107 nsIDOMDocument *nsdoc;
1108 nsIDOMText *text_node;
1109 nsAString text_str;
1110 nsresult nsres;
1112 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1114 if(!This->doc)
1115 return MSHTML_E_NODOC;
1117 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1118 if(NS_FAILED(nsres)) {
1119 ERR("GetDocument failed: %08x\n", nsres);
1120 return S_OK;
1123 nsAString_Init(&text_str, v);
1124 nsres = nsIDOMDocument_CreateTextNode(nsdoc, &text_str, &text_node);
1125 nsIDOMDocument_Release(nsdoc);
1126 nsAString_Finish(&text_str);
1127 if(NS_FAILED(nsres)) {
1128 ERR("CreateTextNode failed: %08x\n", nsres);
1129 return S_OK;
1131 nsres = nsIDOMRange_DeleteContents(This->nsrange);
1132 if(NS_FAILED(nsres))
1133 ERR("DeleteContents failed: %08x\n", nsres);
1135 nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1136 if(NS_FAILED(nsres))
1137 ERR("InsertNode failed: %08x\n", nsres);
1139 nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1140 if(NS_FAILED(nsres))
1141 ERR("SetEndAfter failed: %08x\n", nsres);
1143 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), VARIANT_FALSE);
1146 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1148 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1149 wstrbuf_t buf;
1151 TRACE("(%p)->(%p)\n", This, p);
1153 *p = NULL;
1154 if(!This->nsrange)
1155 return S_OK;
1157 wstrbuf_init(&buf);
1158 range_to_string(This, &buf);
1159 if(buf.buf)
1160 *p = SysAllocString(buf.buf);
1161 wstrbuf_finish(&buf);
1163 TRACE("ret %s\n", debugstr_w(*p));
1164 return S_OK;
1167 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1169 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1170 nsIDOMNode *nsnode, *tmp;
1171 HTMLDOMNode *node;
1173 TRACE("(%p)->(%p)\n", This, parent);
1175 nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1176 while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1177 nsIDOMNode_GetParentNode(nsnode, &tmp);
1178 nsIDOMNode_Release(nsnode);
1179 nsnode = tmp;
1182 if(!nsnode) {
1183 *parent = NULL;
1184 return S_OK;
1187 node = get_node(This->doc, nsnode);
1188 nsIDOMNode_Release(nsnode);
1190 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)parent);
1193 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1195 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1196 nsIDOMRange *nsrange = NULL;
1198 TRACE("(%p)->(%p)\n", This, Duplicate);
1200 nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1201 *Duplicate = HTMLTxtRange_Create(This->doc, nsrange);
1202 nsIDOMRange_Release(nsrange);
1204 return S_OK;
1207 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1208 VARIANT_BOOL *InRange)
1210 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1211 HTMLTxtRange *src_range;
1212 PRInt16 nsret = 0;
1213 nsresult nsres;
1215 TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1217 *InRange = VARIANT_FALSE;
1219 src_range = get_range_object(This->doc, Range);
1220 if(!src_range)
1221 return E_FAIL;
1223 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1224 src_range->nsrange, &nsret);
1225 if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1226 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1227 src_range->nsrange, &nsret);
1228 if(NS_SUCCEEDED(nsres) && nsret >= 0)
1229 *InRange = VARIANT_TRUE;
1232 if(NS_FAILED(nsres))
1233 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1235 return S_OK;
1238 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1239 VARIANT_BOOL *IsEqual)
1241 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1242 HTMLTxtRange *src_range;
1243 PRInt16 nsret = 0;
1244 nsresult nsres;
1246 TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1248 *IsEqual = VARIANT_FALSE;
1250 src_range = get_range_object(This->doc, Range);
1251 if(!src_range)
1252 return E_FAIL;
1254 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1255 src_range->nsrange, &nsret);
1256 if(NS_SUCCEEDED(nsres) && !nsret) {
1257 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1258 src_range->nsrange, &nsret);
1259 if(NS_SUCCEEDED(nsres) && !nsret)
1260 *IsEqual = VARIANT_TRUE;
1263 if(NS_FAILED(nsres))
1264 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1266 return S_OK;
1269 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1271 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1272 FIXME("(%p)->(%x)\n", This, fStart);
1273 return E_NOTIMPL;
1276 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1278 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1280 TRACE("(%p)->(%x)\n", This, Start);
1282 nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1283 return S_OK;
1286 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1288 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1289 range_unit_t unit;
1291 TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1293 unit = string_to_unit(Unit);
1294 if(unit == RU_UNKNOWN)
1295 return E_INVALIDARG;
1297 *Success = VARIANT_FALSE;
1299 switch(unit) {
1300 case RU_WORD: {
1301 dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1302 PRBool collapsed;
1304 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1306 get_cur_pos(This, TRUE, &start_pos);
1307 get_cur_pos(This, FALSE, &end_pos);
1309 if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1310 set_range_pos(This, FALSE, &new_end_pos);
1311 *Success = VARIANT_TRUE;
1314 if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1315 if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1316 set_range_pos(This, TRUE, &new_start_pos);
1317 *Success = VARIANT_TRUE;
1319 dompos_release(&new_start_pos);
1322 dompos_release(&new_end_pos);
1323 dompos_release(&end_pos);
1324 dompos_release(&start_pos);
1326 break;
1329 case RU_TEXTEDIT: {
1330 nsIDOMDocument *nsdoc;
1331 nsIDOMHTMLDocument *nshtmldoc;
1332 nsIDOMHTMLElement *nsbody = NULL;
1333 nsresult nsres;
1335 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1336 if(NS_FAILED(nsres) || !nsdoc) {
1337 ERR("GetDocument failed: %08x\n", nsres);
1338 break;
1341 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
1342 nsIDOMDocument_Release(nsdoc);
1344 nsres = nsIDOMHTMLDocument_GetBody(nshtmldoc, &nsbody);
1345 nsIDOMHTMLDocument_Release(nshtmldoc);
1346 if(NS_FAILED(nsres) || !nsbody) {
1347 ERR("Could not get body: %08x\n", nsres);
1348 break;
1351 nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1352 nsIDOMHTMLElement_Release(nsbody);
1353 if(NS_FAILED(nsres)) {
1354 ERR("Collapse failed: %08x\n", nsres);
1355 break;
1358 *Success = VARIANT_TRUE;
1359 break;
1362 default:
1363 FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1366 return S_OK;
1369 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1370 long Count, long *ActualCount)
1372 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1373 range_unit_t unit;
1375 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1377 unit = string_to_unit(Unit);
1378 if(unit == RU_UNKNOWN)
1379 return E_INVALIDARG;
1381 if(!Count) {
1382 *ActualCount = 0;
1383 return IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1386 switch(unit) {
1387 case RU_CHAR: {
1388 dompos_t cur_pos, new_pos;
1390 get_cur_pos(This, TRUE, &cur_pos);
1392 if(Count > 0) {
1393 *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1394 set_range_pos(This, FALSE, &new_pos);
1395 dompos_release(&new_pos);
1397 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1398 }else {
1399 *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1400 set_range_pos(This, TRUE, &new_pos);
1401 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1402 dompos_release(&new_pos);
1405 dompos_release(&cur_pos);
1406 break;
1409 case RU_WORD: {
1410 dompos_t cur_pos, new_pos;
1412 get_cur_pos(This, TRUE, &cur_pos);
1414 if(Count > 0) {
1415 *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1416 set_range_pos(This, FALSE, &new_pos);
1417 dompos_release(&new_pos);
1418 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1419 }else {
1420 *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1421 set_range_pos(This, TRUE, &new_pos);
1422 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1423 dompos_release(&new_pos);
1426 dompos_release(&cur_pos);
1427 break;
1430 default:
1431 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1434 TRACE("ret %ld\n", *ActualCount);
1435 return S_OK;
1438 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1439 long Count, long *ActualCount)
1441 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1442 range_unit_t unit;
1444 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1446 unit = string_to_unit(Unit);
1447 if(unit == RU_UNKNOWN)
1448 return E_INVALIDARG;
1450 if(!Count) {
1451 *ActualCount = 0;
1452 return S_OK;
1455 switch(unit) {
1456 case RU_CHAR: {
1457 dompos_t start_pos, end_pos, new_pos;
1458 PRBool collapsed;
1460 get_cur_pos(This, TRUE, &start_pos);
1461 get_cur_pos(This, FALSE, &end_pos);
1462 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1464 if(Count > 0) {
1465 BOOL bounded;
1467 *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1468 set_range_pos(This, !bounded, &new_pos);
1469 if(bounded)
1470 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), FALSE);
1471 }else {
1472 *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1473 set_range_pos(This, TRUE, &new_pos);
1476 dompos_release(&start_pos);
1477 dompos_release(&end_pos);
1478 dompos_release(&new_pos);
1479 break;
1482 default:
1483 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1486 return S_OK;
1489 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1490 long Count, long *ActualCount)
1492 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1493 range_unit_t unit;
1495 TRACE("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1497 unit = string_to_unit(Unit);
1498 if(unit == RU_UNKNOWN)
1499 return E_INVALIDARG;
1501 if(!Count) {
1502 *ActualCount = 0;
1503 return S_OK;
1506 switch(unit) {
1507 case RU_CHAR: {
1508 dompos_t start_pos, end_pos, new_pos;
1509 PRBool collapsed;
1511 get_cur_pos(This, TRUE, &start_pos);
1512 get_cur_pos(This, FALSE, &end_pos);
1513 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1515 if(Count > 0) {
1516 *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1517 set_range_pos(This, FALSE, &new_pos);
1518 }else {
1519 BOOL bounded;
1521 *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1522 set_range_pos(This, bounded, &new_pos);
1523 if(bounded)
1524 IHTMLTxtRange_collapse(HTMLTXTRANGE(This), TRUE);
1527 dompos_release(&start_pos);
1528 dompos_release(&end_pos);
1529 dompos_release(&new_pos);
1530 break;
1533 default:
1534 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1537 return S_OK;
1540 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1542 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1544 TRACE("(%p)\n", This);
1546 if(This->doc->nscontainer) {
1547 nsIDOMWindow *dom_window = NULL;
1548 nsISelection *nsselection;
1550 nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &dom_window);
1551 nsIDOMWindow_GetSelection(dom_window, &nsselection);
1552 nsIDOMWindow_Release(dom_window);
1554 nsISelection_RemoveAllRanges(nsselection);
1555 nsISelection_AddRange(nsselection, This->nsrange);
1557 nsISelection_Release(nsselection);
1560 return S_OK;
1563 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1565 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1566 FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1567 return E_NOTIMPL;
1570 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1572 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1573 FIXME("(%p)->(%p)\n", This, element);
1574 return E_NOTIMPL;
1577 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1578 IHTMLTxtRange *SourceRange)
1580 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1581 FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1582 return E_NOTIMPL;
1585 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1586 IHTMLTxtRange *SourceRange, long *ret)
1588 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1589 HTMLTxtRange *src_range;
1590 PRInt16 nsret = 0;
1591 int nscmpt;
1592 nsresult nsres;
1594 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1596 nscmpt = string_to_nscmptype(how);
1597 if(nscmpt == -1)
1598 return E_INVALIDARG;
1600 src_range = get_range_object(This->doc, SourceRange);
1601 if(!src_range)
1602 return E_FAIL;
1604 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1605 if(NS_FAILED(nsres))
1606 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1608 *ret = nsret;
1609 return S_OK;
1612 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1613 long count, long Flags, VARIANT_BOOL *Success)
1615 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1616 FIXME("(%p)->(%s %ld %08lx %p)\n", This, debugstr_w(String), count, Flags, Success);
1617 return E_NOTIMPL;
1620 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, long x, long y)
1622 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1623 FIXME("(%p)->(%ld %ld)\n", This, x, y);
1624 return E_NOTIMPL;
1627 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1629 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1630 FIXME("(%p)->(%p)\n", This, Bookmark);
1631 return E_NOTIMPL;
1634 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1635 VARIANT_BOOL *Success)
1637 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1638 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1639 return E_NOTIMPL;
1642 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1643 VARIANT_BOOL *pfRet)
1645 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1646 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1647 return E_NOTIMPL;
1650 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1651 VARIANT_BOOL *pfRet)
1653 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1654 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1655 return E_NOTIMPL;
1658 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1659 VARIANT_BOOL *pfRet)
1661 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1662 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1663 return E_NOTIMPL;
1666 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1667 VARIANT_BOOL *pfRet)
1669 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1670 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1671 return E_NOTIMPL;
1674 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1675 BSTR *pcmdText)
1677 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1678 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1679 return E_NOTIMPL;
1682 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1683 VARIANT *pcmdValue)
1685 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1686 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1687 return E_NOTIMPL;
1690 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1691 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1693 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1694 FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1695 return E_NOTIMPL;
1698 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1699 VARIANT_BOOL *pfRet)
1701 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
1702 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1703 return E_NOTIMPL;
1706 #undef HTMLTXTRANGE_THIS
1708 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1709 HTMLTxtRange_QueryInterface,
1710 HTMLTxtRange_AddRef,
1711 HTMLTxtRange_Release,
1712 HTMLTxtRange_GetTypeInfoCount,
1713 HTMLTxtRange_GetTypeInfo,
1714 HTMLTxtRange_GetIDsOfNames,
1715 HTMLTxtRange_Invoke,
1716 HTMLTxtRange_get_htmlText,
1717 HTMLTxtRange_put_text,
1718 HTMLTxtRange_get_text,
1719 HTMLTxtRange_parentElement,
1720 HTMLTxtRange_duplicate,
1721 HTMLTxtRange_inRange,
1722 HTMLTxtRange_isEqual,
1723 HTMLTxtRange_scrollIntoView,
1724 HTMLTxtRange_collapse,
1725 HTMLTxtRange_expand,
1726 HTMLTxtRange_move,
1727 HTMLTxtRange_moveStart,
1728 HTMLTxtRange_moveEnd,
1729 HTMLTxtRange_select,
1730 HTMLTxtRange_pasteHTML,
1731 HTMLTxtRange_moveToElementText,
1732 HTMLTxtRange_setEndPoint,
1733 HTMLTxtRange_compareEndPoints,
1734 HTMLTxtRange_findText,
1735 HTMLTxtRange_moveToPoint,
1736 HTMLTxtRange_getBookmark,
1737 HTMLTxtRange_moveToBookmark,
1738 HTMLTxtRange_queryCommandSupported,
1739 HTMLTxtRange_queryCommandEnabled,
1740 HTMLTxtRange_queryCommandState,
1741 HTMLTxtRange_queryCommandIndeterm,
1742 HTMLTxtRange_queryCommandText,
1743 HTMLTxtRange_queryCommandValue,
1744 HTMLTxtRange_execCommand,
1745 HTMLTxtRange_execCommandShowHelp
1748 #define OLECMDTRG_THIS(iface) DEFINE_THIS(HTMLTxtRange, OleCommandTarget, iface)
1750 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1752 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1753 return IHTMLTxtRange_QueryInterface(HTMLTXTRANGE(This), riid, ppv);
1756 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1758 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1759 return IHTMLTxtRange_AddRef(HTMLTXTRANGE(This));
1762 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1764 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1765 return IHTMLTxtRange_Release(HTMLTXTRANGE(This));
1768 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1769 ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1771 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1772 FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1773 return E_NOTIMPL;
1776 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1778 nsIDOMDocumentFragment *fragment;
1779 nsIDOMElement *blockquote_elem, *p_elem;
1780 nsIDOMDocument *nsdoc;
1781 nsIDOMNode *tmp;
1782 nsAString tag_str;
1784 static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1785 static const PRUnichar pW[] = {'P',0};
1787 TRACE("(%p)->(%p %p)\n", This, in, out);
1789 nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
1791 nsAString_Init(&tag_str, blockquoteW);
1792 nsIDOMDocument_CreateElement(nsdoc, &tag_str, &blockquote_elem);
1793 nsAString_Finish(&tag_str);
1795 nsAString_Init(&tag_str, pW);
1796 nsIDOMDocument_CreateElement(nsdoc, &tag_str, &p_elem);
1797 nsAString_Finish(&tag_str);
1799 nsIDOMDocument_Release(nsdoc);
1801 nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1802 nsIDOMElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1803 nsIDOMDocumentFragment_Release(fragment);
1804 nsIDOMNode_Release(tmp);
1806 nsIDOMElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1807 nsIDOMElement_Release(p_elem);
1808 nsIDOMNode_Release(tmp);
1810 nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1811 nsIDOMElement_Release(blockquote_elem);
1813 return S_OK;
1816 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1817 DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1819 HTMLTxtRange *This = OLECMDTRG_THIS(iface);
1821 TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1822 nCmdexecopt, pvaIn, pvaOut);
1824 if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1825 switch(nCmdID) {
1826 case IDM_INDENT:
1827 return exec_indent(This, pvaIn, pvaOut);
1828 default:
1829 FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1831 }else {
1832 FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1835 return E_NOTIMPL;
1838 #undef OLECMDTRG_THIS
1840 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1841 RangeCommandTarget_QueryInterface,
1842 RangeCommandTarget_AddRef,
1843 RangeCommandTarget_Release,
1844 RangeCommandTarget_QueryStatus,
1845 RangeCommandTarget_Exec
1848 IHTMLTxtRange *HTMLTxtRange_Create(HTMLDocument *doc, nsIDOMRange *nsrange)
1850 HTMLTxtRange *ret = heap_alloc(sizeof(HTMLTxtRange));
1852 ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
1853 ret->lpOleCommandTargetVtbl = &OleCommandTargetVtbl;
1854 ret->ref = 1;
1856 if(nsrange)
1857 nsIDOMRange_AddRef(nsrange);
1858 ret->nsrange = nsrange;
1860 ret->doc = doc;
1861 list_add_head(&doc->range_list, &ret->entry);
1863 return HTMLTXTRANGE(ret);
1866 void detach_ranges(HTMLDocument *This)
1868 HTMLTxtRange *iter;
1870 LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1871 iter->doc = NULL;