Revert r66580 and r66579.
[reactos.git] / reactos / dll / win32 / mshtml / txtrange.c
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "mshtml_private.h"
20
21 static const WCHAR brW[] = {'b','r',0};
22 static const WCHAR hrW[] = {'h','r',0};
23
24 typedef struct {
25 IHTMLTxtRange IHTMLTxtRange_iface;
26 IOleCommandTarget IOleCommandTarget_iface;
27
28 LONG ref;
29
30 nsIDOMRange *nsrange;
31 HTMLDocumentNode *doc;
32
33 struct list entry;
34 } HTMLTxtRange;
35
36 typedef struct {
37 WCHAR *buf;
38 DWORD len;
39 DWORD size;
40 } wstrbuf_t;
41
42 typedef struct {
43 UINT16 type;
44 nsIDOMNode *node;
45 UINT32 off;
46 nsAString str;
47 const PRUnichar *p;
48 } dompos_t;
49
50 typedef enum {
51 RU_UNKNOWN,
52 RU_CHAR,
53 RU_WORD,
54 RU_SENTENCE,
55 RU_TEXTEDIT
56 } range_unit_t;
57
58 static HTMLTxtRange *get_range_object(HTMLDocumentNode *doc, IHTMLTxtRange *iface)
59 {
60 HTMLTxtRange *iter;
61
62 LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
63 if(&iter->IHTMLTxtRange_iface == iface)
64 return iter;
65 }
66
67 ERR("Could not find range in document\n");
68 return NULL;
69 }
70
71 static range_unit_t string_to_unit(LPCWSTR str)
72 {
73 static const WCHAR characterW[] =
74 {'c','h','a','r','a','c','t','e','r',0};
75 static const WCHAR wordW[] =
76 {'w','o','r','d',0};
77 static const WCHAR sentenceW[] =
78 {'s','e','n','t','e','n','c','e',0};
79 static const WCHAR texteditW[] =
80 {'t','e','x','t','e','d','i','t',0};
81
82 if(!strcmpiW(str, characterW)) return RU_CHAR;
83 if(!strcmpiW(str, wordW)) return RU_WORD;
84 if(!strcmpiW(str, sentenceW)) return RU_SENTENCE;
85 if(!strcmpiW(str, texteditW)) return RU_TEXTEDIT;
86
87 return RU_UNKNOWN;
88 }
89
90 static int string_to_nscmptype(LPCWSTR str)
91 {
92 static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
93 static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
94 static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
95 static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
96
97 if(!strcmpiW(str, seW)) return NS_START_TO_END;
98 if(!strcmpiW(str, ssW)) return NS_START_TO_START;
99 if(!strcmpiW(str, esW)) return NS_END_TO_START;
100 if(!strcmpiW(str, eeW)) return NS_END_TO_END;
101
102 return -1;
103 }
104
105 static UINT16 get_node_type(nsIDOMNode *node)
106 {
107 UINT16 type = 0;
108
109 if(node)
110 nsIDOMNode_GetNodeType(node, &type);
111
112 return type;
113 }
114
115 static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
116 {
117 nsIDOMElement *elem;
118 nsAString tag_str;
119 const PRUnichar *tag;
120 BOOL ret = FALSE;
121 nsresult nsres;
122
123 nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
124 if(NS_FAILED(nsres))
125 return FALSE;
126
127 nsAString_Init(&tag_str, NULL);
128 nsIDOMElement_GetTagName(elem, &tag_str);
129 nsIDOMElement_Release(elem);
130 nsAString_GetData(&tag_str, &tag);
131
132 ret = !strcmpiW(tag, istag);
133
134 nsAString_Finish(&tag_str);
135
136 return ret;
137 }
138
139 static BOOL is_space_elem(nsIDOMNode *node)
140 {
141 nsIDOMElement *elem;
142 nsAString tag_str;
143 const PRUnichar *tag;
144 BOOL ret = FALSE;
145 nsresult nsres;
146
147 nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
148 if(NS_FAILED(nsres))
149 return FALSE;
150
151 nsAString_Init(&tag_str, NULL);
152 nsIDOMElement_GetTagName(elem, &tag_str);
153 nsIDOMElement_Release(elem);
154 nsAString_GetData(&tag_str, &tag);
155
156 ret = !strcmpiW(tag, brW) || !strcmpiW(tag, hrW);
157
158 nsAString_Finish(&tag_str);
159
160 return ret;
161 }
162
163 static inline BOOL wstrbuf_init(wstrbuf_t *buf)
164 {
165 buf->len = 0;
166 buf->size = 16;
167 buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
168 if (!buf->buf) return FALSE;
169 *buf->buf = 0;
170 return TRUE;
171 }
172
173 static inline void wstrbuf_finish(wstrbuf_t *buf)
174 {
175 heap_free(buf->buf);
176 }
177
178 static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
179 {
180 if(buf->len+len >= buf->size) {
181 buf->size = 2*buf->size+len;
182 buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
183 }
184
185 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
186 buf->len += len;
187 buf->buf[buf->len] = 0;
188 }
189
190 static void wstrbuf_append_nodetxt(wstrbuf_t *buf, LPCWSTR str, int len)
191 {
192 const WCHAR *s = str;
193 WCHAR *d;
194
195 TRACE("%s\n", debugstr_wn(str, len));
196
197 if(buf->len+len >= buf->size) {
198 buf->size = 2*buf->size+len;
199 buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
200 }
201
202 if(buf->len && isspaceW(buf->buf[buf->len-1])) {
203 while(s < str+len && isspaceW(*s))
204 s++;
205 }
206
207 d = buf->buf+buf->len;
208 while(s < str+len) {
209 if(isspaceW(*s)) {
210 *d++ = ' ';
211 s++;
212 while(s < str+len && isspaceW(*s))
213 s++;
214 }else {
215 *d++ = *s++;
216 }
217 }
218
219 buf->len = d - buf->buf;
220 *d = 0;
221 }
222
223 static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node)
224 {
225
226 switch(get_node_type(node)) {
227 case TEXT_NODE: {
228 nsIDOMText *nstext;
229 nsAString data_str;
230 const PRUnichar *data;
231
232 nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
233
234 nsAString_Init(&data_str, NULL);
235 nsIDOMText_GetData(nstext, &data_str);
236 nsAString_GetData(&data_str, &data);
237 wstrbuf_append_nodetxt(buf, data, strlenW(data));
238 nsAString_Finish(&data_str);
239
240 nsIDOMText_Release(nstext);
241
242 break;
243 }
244 case ELEMENT_NODE:
245 if(is_elem_tag(node, brW)) {
246 static const WCHAR endlW[] = {'\r','\n'};
247 wstrbuf_append_len(buf, endlW, 2);
248 }else if(is_elem_tag(node, hrW)) {
249 static const WCHAR endl2W[] = {'\r','\n','\r','\n'};
250 wstrbuf_append_len(buf, endl2W, 4);
251 }
252 }
253 }
254
255 static void wstrbuf_append_node_rec(wstrbuf_t *buf, nsIDOMNode *node)
256 {
257 nsIDOMNode *iter, *tmp;
258
259 wstrbuf_append_node(buf, node);
260
261 nsIDOMNode_GetFirstChild(node, &iter);
262 while(iter) {
263 wstrbuf_append_node_rec(buf, iter);
264 nsIDOMNode_GetNextSibling(iter, &tmp);
265 nsIDOMNode_Release(iter);
266 iter = tmp;
267 }
268 }
269
270 static BOOL fill_nodestr(dompos_t *pos)
271 {
272 nsIDOMText *text;
273 nsresult nsres;
274
275 if(pos->type != TEXT_NODE)
276 return FALSE;
277
278 nsres = nsIDOMNode_QueryInterface(pos->node, &IID_nsIDOMText, (void**)&text);
279 if(NS_FAILED(nsres))
280 return FALSE;
281
282 nsAString_Init(&pos->str, NULL);
283 nsIDOMText_GetData(text, &pos->str);
284 nsIDOMText_Release(text);
285 nsAString_GetData(&pos->str, &pos->p);
286
287 if(pos->off == -1)
288 pos->off = *pos->p ? strlenW(pos->p)-1 : 0;
289
290 return TRUE;
291 }
292
293 static nsIDOMNode *next_node(nsIDOMNode *iter)
294 {
295 nsIDOMNode *ret, *tmp;
296 nsresult nsres;
297
298 if(!iter)
299 return NULL;
300
301 nsres = nsIDOMNode_GetFirstChild(iter, &ret);
302 if(NS_SUCCEEDED(nsres) && ret)
303 return ret;
304
305 nsIDOMNode_AddRef(iter);
306
307 do {
308 nsres = nsIDOMNode_GetNextSibling(iter, &ret);
309 if(NS_SUCCEEDED(nsres) && ret) {
310 nsIDOMNode_Release(iter);
311 return ret;
312 }
313
314 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
315 nsIDOMNode_Release(iter);
316 iter = tmp;
317 }while(NS_SUCCEEDED(nsres) && iter);
318
319 return NULL;
320 }
321
322 static nsIDOMNode *prev_node(HTMLTxtRange *This, nsIDOMNode *iter)
323 {
324 nsIDOMNode *ret, *tmp;
325 nsresult nsres;
326
327 if(!iter) {
328 nsIDOMHTMLElement *nselem;
329
330 nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nselem);
331 nsIDOMHTMLElement_GetLastChild(nselem, &tmp);
332 if(!tmp)
333 return (nsIDOMNode*)nselem;
334
335 while(tmp) {
336 ret = tmp;
337 nsIDOMNode_GetLastChild(ret, &tmp);
338 }
339
340 nsIDOMHTMLElement_Release(nselem);
341
342 return ret;
343 }
344
345 nsres = nsIDOMNode_GetLastChild(iter, &ret);
346 if(NS_SUCCEEDED(nsres) && ret)
347 return ret;
348
349 nsIDOMNode_AddRef(iter);
350
351 do {
352 nsres = nsIDOMNode_GetPreviousSibling(iter, &ret);
353 if(NS_SUCCEEDED(nsres) && ret) {
354 nsIDOMNode_Release(iter);
355 return ret;
356 }
357
358 nsres = nsIDOMNode_GetParentNode(iter, &tmp);
359 nsIDOMNode_Release(iter);
360 iter = tmp;
361 }while(NS_SUCCEEDED(nsres) && iter);
362
363 return NULL;
364 }
365
366 static nsIDOMNode *get_child_node(nsIDOMNode *node, UINT32 off)
367 {
368 nsIDOMNodeList *node_list;
369 nsIDOMNode *ret = NULL;
370
371 nsIDOMNode_GetChildNodes(node, &node_list);
372 nsIDOMNodeList_Item(node_list, off, &ret);
373 nsIDOMNodeList_Release(node_list);
374
375 return ret;
376 }
377
378 static void get_cur_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
379 {
380 nsIDOMNode *node;
381 LONG off;
382
383 pos->p = NULL;
384
385 if(!start) {
386 cpp_bool collapsed;
387 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
388 start = collapsed;
389 }
390
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);
397 }
398
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;
407 }
408
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);
421 }
422
423 if(pos->type == TEXT_NODE)
424 fill_nodestr(pos);
425 }
426
427 static void set_range_pos(HTMLTxtRange *This, BOOL start, dompos_t *pos)
428 {
429 nsresult nsres;
430
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);
441 }
442
443 if(NS_FAILED(nsres))
444 ERR("failed: %p %08x\n", pos->node, nsres);
445 }
446
447 static inline void dompos_release(dompos_t *pos)
448 {
449 if(pos->node)
450 nsIDOMNode_Release(pos->node);
451
452 if(pos->p)
453 nsAString_Finish(&pos->str);
454 }
455
456 static inline void dompos_addref(dompos_t *pos)
457 {
458 if(pos->node)
459 nsIDOMNode_AddRef(pos->node);
460
461 if(pos->type == TEXT_NODE)
462 fill_nodestr(pos);
463 }
464
465 static inline BOOL dompos_cmp(const dompos_t *pos1, const dompos_t *pos2)
466 {
467 return pos1->node == pos2->node && pos1->off == pos2->off;
468 }
469
470 static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
471 {
472 nsIDOMNode *iter, *tmp;
473 dompos_t start_pos, end_pos;
474 cpp_bool collapsed;
475
476 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
477 if(collapsed) {
478 wstrbuf_finish(buf);
479 buf->buf = NULL;
480 buf->size = 0;
481 return;
482 }
483
484 get_cur_pos(This, FALSE, &end_pos);
485 get_cur_pos(This, TRUE, &start_pos);
486
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);
495 }
496 }else {
497 iter = start_pos.node;
498 nsIDOMNode_AddRef(iter);
499 }
500
501 while(iter != end_pos.node) {
502 wstrbuf_append_node(buf, iter);
503 tmp = next_node(iter);
504 nsIDOMNode_Release(iter);
505 iter = tmp;
506 }
507
508 nsIDOMNode_AddRef(end_pos.node);
509
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);
515 }
516
517 nsIDOMNode_Release(iter);
518 dompos_release(&start_pos);
519 dompos_release(&end_pos);
520
521 if(buf->len) {
522 WCHAR *p;
523
524 for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);
525
526 p = strchrW(p, '\r');
527 if(p)
528 *p = 0;
529 }
530 }
531
532 HRESULT get_node_text(HTMLDOMNode *node, BSTR *ret)
533 {
534 wstrbuf_t buf;
535 HRESULT hres = S_OK;
536
537 if (!wstrbuf_init(&buf))
538 return E_OUTOFMEMORY;
539 wstrbuf_append_node_rec(&buf, node->nsnode);
540 if(buf.buf) {
541 *ret = SysAllocString(buf.buf);
542 if(!*ret)
543 hres = E_OUTOFMEMORY;
544 } else {
545 *ret = NULL;
546 }
547 wstrbuf_finish(&buf);
548
549 if(SUCCEEDED(hres))
550 TRACE("ret %s\n", debugstr_w(*ret));
551 return hres;
552 }
553
554 static WCHAR get_pos_char(const dompos_t *pos)
555 {
556 switch(pos->type) {
557 case TEXT_NODE:
558 return pos->p[pos->off];
559 case ELEMENT_NODE:
560 if(is_space_elem(pos->node))
561 return '\n';
562 }
563
564 return 0;
565 }
566
567 static void end_space(const dompos_t *pos, dompos_t *new_pos)
568 {
569 const WCHAR *p;
570
571 *new_pos = *pos;
572 dompos_addref(new_pos);
573
574 if(pos->type != TEXT_NODE)
575 return;
576
577 p = new_pos->p+new_pos->off;
578
579 if(!*p || !isspace(*p))
580 return;
581
582 while(p[1] && isspace(p[1]))
583 p++;
584
585 new_pos->off = p - new_pos->p;
586 }
587
588 static WCHAR next_char(const dompos_t *pos, dompos_t *new_pos)
589 {
590 nsIDOMNode *iter, *tmp;
591 dompos_t last_space, tmp_pos;
592 const WCHAR *p;
593 WCHAR cspace = 0;
594
595 if(pos->type == TEXT_NODE && pos->off != -1 && pos->p[pos->off]) {
596 p = pos->p+pos->off;
597
598 if(isspace(*p))
599 while(isspaceW(*++p));
600 else
601 p++;
602
603 if(*p && isspaceW(*p)) {
604 cspace = ' ';
605 while(p[1] && isspaceW(p[1]))
606 p++;
607 }
608
609 if(*p) {
610 *new_pos = *pos;
611 new_pos->off = p - pos->p;
612 dompos_addref(new_pos);
613
614 return cspace ? cspace : *p;
615 }else {
616 last_space = *pos;
617 last_space.off = p - pos->p;
618 dompos_addref(&last_space);
619 }
620 }
621
622 iter = next_node(pos->node);
623
624 while(iter) {
625 switch(get_node_type(iter)) {
626 case TEXT_NODE:
627 tmp_pos.node = iter;
628 tmp_pos.type = TEXT_NODE;
629 tmp_pos.off = 0;
630 dompos_addref(&tmp_pos);
631
632 p = tmp_pos.p;
633
634 if(!*p) {
635 dompos_release(&tmp_pos);
636 break;
637 }else if(isspaceW(*p)) {
638 if(cspace)
639 dompos_release(&last_space);
640 else
641 cspace = ' ';
642
643 while(p[1] && isspaceW(p[1]))
644 p++;
645
646 tmp_pos.off = p-tmp_pos.p;
647
648 if(!p[1]) {
649 last_space = tmp_pos;
650 break;
651 }
652
653 *new_pos = tmp_pos;
654 nsIDOMNode_Release(iter);
655 return cspace;
656 }else if(cspace) {
657 *new_pos = last_space;
658 dompos_release(&tmp_pos);
659 nsIDOMNode_Release(iter);
660
661 return cspace;
662 }else if(*p) {
663 tmp_pos.off = 0;
664 *new_pos = tmp_pos;
665 }
666
667 nsIDOMNode_Release(iter);
668 return *p;
669
670 case ELEMENT_NODE:
671 if(is_elem_tag(iter, brW)) {
672 if(cspace)
673 dompos_release(&last_space);
674 cspace = '\n';
675
676 nsIDOMNode_AddRef(iter);
677 last_space.node = iter;
678 last_space.type = ELEMENT_NODE;
679 last_space.off = 0;
680 last_space.p = NULL;
681 }else if(is_elem_tag(iter, hrW)) {
682 if(cspace) {
683 *new_pos = last_space;
684 nsIDOMNode_Release(iter);
685 return cspace;
686 }
687
688 new_pos->node = iter;
689 new_pos->type = ELEMENT_NODE;
690 new_pos->off = 0;
691 new_pos->p = NULL;
692 return '\n';
693 }
694 }
695
696 tmp = iter;
697 iter = next_node(iter);
698 nsIDOMNode_Release(tmp);
699 }
700
701 if(cspace) {
702 *new_pos = last_space;
703 }else {
704 *new_pos = *pos;
705 dompos_addref(new_pos);
706 }
707
708 return cspace;
709 }
710
711 static WCHAR prev_char(HTMLTxtRange *This, const dompos_t *pos, dompos_t *new_pos)
712 {
713 nsIDOMNode *iter, *tmp;
714 const WCHAR *p;
715 BOOL skip_space = FALSE;
716
717 if(pos->type == TEXT_NODE && isspaceW(pos->p[pos->off]))
718 skip_space = TRUE;
719
720 if(pos->type == TEXT_NODE && pos->off) {
721 p = pos->p+pos->off-1;
722
723 if(skip_space) {
724 while(p >= pos->p && isspace(*p))
725 p--;
726 }
727
728 if(p >= pos->p) {
729 *new_pos = *pos;
730 new_pos->off = p-pos->p;
731 dompos_addref(new_pos);
732 return new_pos->p[new_pos->off];
733 }
734 }
735
736 iter = prev_node(This, pos->node);
737
738 while(iter) {
739 switch(get_node_type(iter)) {
740 case TEXT_NODE: {
741 dompos_t tmp_pos;
742
743 tmp_pos.node = iter;
744 tmp_pos.type = TEXT_NODE;
745 tmp_pos.off = 0;
746 dompos_addref(&tmp_pos);
747
748 p = tmp_pos.p + strlenW(tmp_pos.p)-1;
749
750 if(skip_space) {
751 while(p >= tmp_pos.p && isspaceW(*p))
752 p--;
753 }
754
755 if(p < tmp_pos.p) {
756 dompos_release(&tmp_pos);
757 break;
758 }
759
760 tmp_pos.off = p-tmp_pos.p;
761 *new_pos = tmp_pos;
762 nsIDOMNode_Release(iter);
763 return *p;
764 }
765
766 case ELEMENT_NODE:
767 if(is_elem_tag(iter, brW)) {
768 if(skip_space) {
769 skip_space = FALSE;
770 break;
771 }
772 }else if(!is_elem_tag(iter, hrW)) {
773 break;
774 }
775
776 new_pos->node = iter;
777 new_pos->type = ELEMENT_NODE;
778 new_pos->off = 0;
779 new_pos->p = NULL;
780 return '\n';
781 }
782
783 tmp = iter;
784 iter = prev_node(This, iter);
785 nsIDOMNode_Release(tmp);
786 }
787
788 *new_pos = *pos;
789 dompos_addref(new_pos);
790 return 0;
791 }
792
793 static LONG move_next_chars(LONG cnt, const dompos_t *pos, BOOL col, const dompos_t *bound_pos,
794 BOOL *bounded, dompos_t *new_pos)
795 {
796 dompos_t iter, tmp;
797 LONG ret = 0;
798 WCHAR c;
799
800 if(bounded)
801 *bounded = FALSE;
802
803 if(col)
804 ret++;
805
806 if(ret >= cnt) {
807 end_space(pos, new_pos);
808 return ret;
809 }
810
811 c = next_char(pos, &iter);
812 ret++;
813
814 while(ret < cnt) {
815 tmp = iter;
816 c = next_char(&tmp, &iter);
817 dompos_release(&tmp);
818 if(!c)
819 break;
820
821 ret++;
822 if(bound_pos && dompos_cmp(&tmp, bound_pos)) {
823 *bounded = TRUE;
824 ret++;
825 }
826 }
827
828 *new_pos = iter;
829 return ret;
830 }
831
832 static LONG move_prev_chars(HTMLTxtRange *This, LONG cnt, const dompos_t *pos, BOOL end,
833 const dompos_t *bound_pos, BOOL *bounded, dompos_t *new_pos)
834 {
835 dompos_t iter, tmp;
836 LONG ret = 0;
837 BOOL prev_eq = FALSE;
838 WCHAR c;
839
840 if(bounded)
841 *bounded = FALSE;
842
843 c = prev_char(This, pos, &iter);
844 if(c)
845 ret++;
846
847 while(c && ret < cnt) {
848 tmp = iter;
849 c = prev_char(This, &tmp, &iter);
850 dompos_release(&tmp);
851 if(!c) {
852 if(end)
853 ret++;
854 break;
855 }
856
857 ret++;
858
859 if(prev_eq) {
860 *bounded = TRUE;
861 ret++;
862 }
863
864 prev_eq = bound_pos && dompos_cmp(&iter, bound_pos);
865 }
866
867 *new_pos = iter;
868 return ret;
869 }
870
871 static LONG find_prev_space(HTMLTxtRange *This, const dompos_t *pos, BOOL first_space, dompos_t *ret)
872 {
873 dompos_t iter, tmp;
874 WCHAR c;
875
876 c = prev_char(This, pos, &iter);
877 if(!c || (first_space && isspaceW(c))) {
878 *ret = iter;
879 return FALSE;
880 }
881
882 while(1) {
883 tmp = iter;
884 c = prev_char(This, &tmp, &iter);
885 if(!c || isspaceW(c)) {
886 dompos_release(&iter);
887 break;
888 }
889 dompos_release(&tmp);
890 }
891
892 *ret = tmp;
893 return TRUE;
894 }
895
896 static int find_word_end(const dompos_t *pos, dompos_t *ret)
897 {
898 dompos_t iter, tmp;
899 int cnt = 1;
900 WCHAR c;
901 c = get_pos_char(pos);
902 if(isspaceW(c)) {
903 *ret = *pos;
904 dompos_addref(ret);
905 return 0;
906 }
907
908 c = next_char(pos, &iter);
909 if(!c) {
910 *ret = iter;
911 return 0;
912 }
913 if(c == '\n') {
914 *ret = *pos;
915 dompos_addref(ret);
916 return 0;
917 }
918
919 while(c && !isspaceW(c)) {
920 tmp = iter;
921 c = next_char(&tmp, &iter);
922 if(c == '\n') {
923 dompos_release(&iter);
924 iter = tmp;
925 }else {
926 cnt++;
927 dompos_release(&tmp);
928 }
929 }
930
931 *ret = iter;
932 return cnt;
933 }
934
935 static LONG move_next_words(LONG cnt, const dompos_t *pos, dompos_t *new_pos)
936 {
937 dompos_t iter, tmp;
938 LONG ret = 0;
939 WCHAR c;
940
941 c = get_pos_char(pos);
942 if(isspaceW(c)) {
943 end_space(pos, &iter);
944 ret++;
945 }else {
946 c = next_char(pos, &iter);
947 if(c && isspaceW(c))
948 ret++;
949 }
950
951 while(c && ret < cnt) {
952 tmp = iter;
953 c = next_char(&tmp, &iter);
954 dompos_release(&tmp);
955 if(isspaceW(c))
956 ret++;
957 }
958
959 *new_pos = iter;
960 return ret;
961 }
962
963 static LONG move_prev_words(HTMLTxtRange *This, LONG cnt, const dompos_t *pos, dompos_t *new_pos)
964 {
965 dompos_t iter, tmp;
966 LONG ret = 0;
967
968 iter = *pos;
969 dompos_addref(&iter);
970
971 while(ret < cnt) {
972 if(!find_prev_space(This, &iter, FALSE, &tmp))
973 break;
974
975 dompos_release(&iter);
976 iter = tmp;
977 ret++;
978 }
979
980 *new_pos = iter;
981 return ret;
982 }
983
984 static inline HTMLTxtRange *impl_from_IHTMLTxtRange(IHTMLTxtRange *iface)
985 {
986 return CONTAINING_RECORD(iface, HTMLTxtRange, IHTMLTxtRange_iface);
987 }
988
989 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
990 {
991 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
992
993 *ppv = NULL;
994
995 if(IsEqualGUID(&IID_IUnknown, riid)) {
996 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
997 *ppv = &This->IHTMLTxtRange_iface;
998 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
999 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1000 *ppv = &This->IHTMLTxtRange_iface;
1001 }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
1002 TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
1003 *ppv = &This->IHTMLTxtRange_iface;
1004 }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1005 TRACE("(%p)->(IID_IOleCommandTarget %p)\n", This, ppv);
1006 *ppv = &This->IOleCommandTarget_iface;
1007 }
1008
1009 if(*ppv) {
1010 IUnknown_AddRef((IUnknown*)*ppv);
1011 return S_OK;
1012 }
1013
1014 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1015 return E_NOINTERFACE;
1016 }
1017
1018 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
1019 {
1020 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1021 LONG ref = InterlockedIncrement(&This->ref);
1022
1023 TRACE("(%p) ref=%d\n", This, ref);
1024
1025 return ref;
1026 }
1027
1028 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
1029 {
1030 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1031 LONG ref = InterlockedDecrement(&This->ref);
1032
1033 TRACE("(%p) ref=%d\n", This, ref);
1034
1035 if(!ref) {
1036 if(This->nsrange)
1037 nsIDOMRange_Release(This->nsrange);
1038 if(This->doc)
1039 list_remove(&This->entry);
1040 heap_free(This);
1041 }
1042
1043 return ref;
1044 }
1045
1046 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
1047 {
1048 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1049 FIXME("(%p)->(%p)\n", This, pctinfo);
1050 return E_NOTIMPL;
1051 }
1052
1053 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
1054 LCID lcid, ITypeInfo **ppTInfo)
1055 {
1056 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1057 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1058 return E_NOTIMPL;
1059 }
1060
1061 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
1062 LPOLESTR *rgszNames, UINT cNames,
1063 LCID lcid, DISPID *rgDispId)
1064 {
1065 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1066 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1067 lcid, rgDispId);
1068 return E_NOTIMPL;
1069 }
1070
1071 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
1072 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1073 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1074 {
1075 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1076 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1077 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1078 return E_NOTIMPL;
1079 }
1080
1081 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
1082 {
1083 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1084
1085 TRACE("(%p)->(%p)\n", This, p);
1086
1087 *p = NULL;
1088
1089 if(This->nsrange) {
1090 nsIDOMDocumentFragment *fragment;
1091 nsresult nsres;
1092
1093 nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
1094 if(NS_SUCCEEDED(nsres)) {
1095 const PRUnichar *nstext;
1096 nsAString nsstr;
1097
1098 nsAString_Init(&nsstr, NULL);
1099 nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
1100 nsIDOMDocumentFragment_Release(fragment);
1101
1102 nsAString_GetData(&nsstr, &nstext);
1103 *p = SysAllocString(nstext);
1104
1105 nsAString_Finish(&nsstr);
1106 }
1107 }
1108
1109 if(!*p) {
1110 const WCHAR emptyW[] = {0};
1111 *p = SysAllocString(emptyW);
1112 }
1113
1114 TRACE("return %s\n", debugstr_w(*p));
1115 return S_OK;
1116 }
1117
1118 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
1119 {
1120 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1121 nsIDOMText *text_node;
1122 nsAString text_str;
1123 nsresult nsres;
1124
1125 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1126
1127 if(!This->doc)
1128 return MSHTML_E_NODOC;
1129
1130 nsAString_InitDepend(&text_str, v);
1131 nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc->nsdoc, &text_str, &text_node);
1132 nsAString_Finish(&text_str);
1133 if(NS_FAILED(nsres)) {
1134 ERR("CreateTextNode failed: %08x\n", nsres);
1135 return S_OK;
1136 }
1137 nsres = nsIDOMRange_DeleteContents(This->nsrange);
1138 if(NS_FAILED(nsres))
1139 ERR("DeleteContents failed: %08x\n", nsres);
1140
1141 nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
1142 if(NS_FAILED(nsres))
1143 ERR("InsertNode failed: %08x\n", nsres);
1144
1145 nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
1146 if(NS_FAILED(nsres))
1147 ERR("SetEndAfter failed: %08x\n", nsres);
1148
1149 return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_FALSE);
1150 }
1151
1152 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
1153 {
1154 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1155 wstrbuf_t buf;
1156
1157 TRACE("(%p)->(%p)\n", This, p);
1158
1159 *p = NULL;
1160 if(!This->nsrange)
1161 return S_OK;
1162
1163 if (!wstrbuf_init(&buf))
1164 return E_OUTOFMEMORY;
1165 range_to_string(This, &buf);
1166 if (buf.buf)
1167 *p = SysAllocString(buf.buf);
1168 wstrbuf_finish(&buf);
1169
1170 TRACE("ret %s\n", debugstr_w(*p));
1171 return S_OK;
1172 }
1173
1174 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
1175 {
1176 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1177 nsIDOMNode *nsnode, *tmp;
1178 HTMLDOMNode *node;
1179 HRESULT hres;
1180
1181 TRACE("(%p)->(%p)\n", This, parent);
1182
1183 nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
1184 while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
1185 nsIDOMNode_GetParentNode(nsnode, &tmp);
1186 nsIDOMNode_Release(nsnode);
1187 nsnode = tmp;
1188 }
1189
1190 if(!nsnode) {
1191 *parent = NULL;
1192 return S_OK;
1193 }
1194
1195 hres = get_node(This->doc, nsnode, TRUE, &node);
1196 nsIDOMNode_Release(nsnode);
1197 if(FAILED(hres))
1198 return hres;
1199
1200 hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)parent);
1201 node_release(node);
1202 return hres;
1203 }
1204
1205 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
1206 {
1207 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1208 nsIDOMRange *nsrange = NULL;
1209 HRESULT hres;
1210
1211 TRACE("(%p)->(%p)\n", This, Duplicate);
1212
1213 nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1214 hres = HTMLTxtRange_Create(This->doc, nsrange, Duplicate);
1215 nsIDOMRange_Release(nsrange);
1216
1217 return hres;
1218 }
1219
1220 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1221 VARIANT_BOOL *InRange)
1222 {
1223 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1224 HTMLTxtRange *src_range;
1225 short nsret = 0;
1226 nsresult nsres;
1227
1228 TRACE("(%p)->(%p %p)\n", This, Range, InRange);
1229
1230 *InRange = VARIANT_FALSE;
1231
1232 src_range = get_range_object(This->doc, Range);
1233 if(!src_range)
1234 return E_FAIL;
1235
1236 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1237 src_range->nsrange, &nsret);
1238 if(NS_SUCCEEDED(nsres) && nsret <= 0) {
1239 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1240 src_range->nsrange, &nsret);
1241 if(NS_SUCCEEDED(nsres) && nsret >= 0)
1242 *InRange = VARIANT_TRUE;
1243 }
1244
1245 if(NS_FAILED(nsres))
1246 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1247
1248 return S_OK;
1249 }
1250
1251 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
1252 VARIANT_BOOL *IsEqual)
1253 {
1254 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1255 HTMLTxtRange *src_range;
1256 short nsret = 0;
1257 nsresult nsres;
1258
1259 TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
1260
1261 *IsEqual = VARIANT_FALSE;
1262
1263 src_range = get_range_object(This->doc, Range);
1264 if(!src_range)
1265 return E_FAIL;
1266
1267 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
1268 src_range->nsrange, &nsret);
1269 if(NS_SUCCEEDED(nsres) && !nsret) {
1270 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
1271 src_range->nsrange, &nsret);
1272 if(NS_SUCCEEDED(nsres) && !nsret)
1273 *IsEqual = VARIANT_TRUE;
1274 }
1275
1276 if(NS_FAILED(nsres))
1277 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1278
1279 return S_OK;
1280 }
1281
1282 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
1283 {
1284 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1285 FIXME("(%p)->(%x)\n", This, fStart);
1286 return E_NOTIMPL;
1287 }
1288
1289 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
1290 {
1291 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1292
1293 TRACE("(%p)->(%x)\n", This, Start);
1294
1295 nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
1296 return S_OK;
1297 }
1298
1299 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
1300 {
1301 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1302 range_unit_t unit;
1303
1304 TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
1305
1306 unit = string_to_unit(Unit);
1307 if(unit == RU_UNKNOWN)
1308 return E_INVALIDARG;
1309
1310 *Success = VARIANT_FALSE;
1311
1312 switch(unit) {
1313 case RU_WORD: {
1314 dompos_t end_pos, start_pos, new_start_pos, new_end_pos;
1315 cpp_bool collapsed;
1316
1317 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1318
1319 get_cur_pos(This, TRUE, &start_pos);
1320 get_cur_pos(This, FALSE, &end_pos);
1321
1322 if(find_word_end(&end_pos, &new_end_pos) || collapsed) {
1323 set_range_pos(This, FALSE, &new_end_pos);
1324 *Success = VARIANT_TRUE;
1325 }
1326
1327 if(start_pos.type && (get_pos_char(&end_pos) || !dompos_cmp(&new_end_pos, &end_pos))) {
1328 if(find_prev_space(This, &start_pos, TRUE, &new_start_pos)) {
1329 set_range_pos(This, TRUE, &new_start_pos);
1330 *Success = VARIANT_TRUE;
1331 }
1332 dompos_release(&new_start_pos);
1333 }
1334
1335 dompos_release(&new_end_pos);
1336 dompos_release(&end_pos);
1337 dompos_release(&start_pos);
1338
1339 break;
1340 }
1341
1342 case RU_TEXTEDIT: {
1343 nsIDOMHTMLElement *nsbody = NULL;
1344 nsresult nsres;
1345
1346 nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
1347 if(NS_FAILED(nsres) || !nsbody) {
1348 ERR("Could not get body: %08x\n", nsres);
1349 break;
1350 }
1351
1352 nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
1353 nsIDOMHTMLElement_Release(nsbody);
1354 if(NS_FAILED(nsres)) {
1355 ERR("Collapse failed: %08x\n", nsres);
1356 break;
1357 }
1358
1359 *Success = VARIANT_TRUE;
1360 break;
1361 }
1362
1363 default:
1364 FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
1365 }
1366
1367 return S_OK;
1368 }
1369
1370 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1371 LONG Count, LONG *ActualCount)
1372 {
1373 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1374 range_unit_t unit;
1375
1376 TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1377
1378 unit = string_to_unit(Unit);
1379 if(unit == RU_UNKNOWN)
1380 return E_INVALIDARG;
1381
1382 if(!Count) {
1383 *ActualCount = 0;
1384 return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1385 }
1386
1387 switch(unit) {
1388 case RU_CHAR: {
1389 dompos_t cur_pos, new_pos;
1390
1391 get_cur_pos(This, TRUE, &cur_pos);
1392
1393 if(Count > 0) {
1394 *ActualCount = move_next_chars(Count, &cur_pos, TRUE, NULL, NULL, &new_pos);
1395 set_range_pos(This, FALSE, &new_pos);
1396 dompos_release(&new_pos);
1397
1398 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1399 }else {
1400 *ActualCount = -move_prev_chars(This, -Count, &cur_pos, FALSE, NULL, NULL, &new_pos);
1401 set_range_pos(This, TRUE, &new_pos);
1402 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1403 dompos_release(&new_pos);
1404 }
1405
1406 dompos_release(&cur_pos);
1407 break;
1408 }
1409
1410 case RU_WORD: {
1411 dompos_t cur_pos, new_pos;
1412
1413 get_cur_pos(This, TRUE, &cur_pos);
1414
1415 if(Count > 0) {
1416 *ActualCount = move_next_words(Count, &cur_pos, &new_pos);
1417 set_range_pos(This, FALSE, &new_pos);
1418 dompos_release(&new_pos);
1419 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1420 }else {
1421 *ActualCount = -move_prev_words(This, -Count, &cur_pos, &new_pos);
1422 set_range_pos(This, TRUE, &new_pos);
1423 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1424 dompos_release(&new_pos);
1425 }
1426
1427 dompos_release(&cur_pos);
1428 break;
1429 }
1430
1431 default:
1432 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1433 }
1434
1435 TRACE("ret %d\n", *ActualCount);
1436 return S_OK;
1437 }
1438
1439 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1440 LONG Count, LONG *ActualCount)
1441 {
1442 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1443 range_unit_t unit;
1444
1445 TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1446
1447 unit = string_to_unit(Unit);
1448 if(unit == RU_UNKNOWN)
1449 return E_INVALIDARG;
1450
1451 if(!Count) {
1452 *ActualCount = 0;
1453 return S_OK;
1454 }
1455
1456 switch(unit) {
1457 case RU_CHAR: {
1458 dompos_t start_pos, end_pos, new_pos;
1459 cpp_bool collapsed;
1460
1461 get_cur_pos(This, TRUE, &start_pos);
1462 get_cur_pos(This, FALSE, &end_pos);
1463 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1464
1465 if(Count > 0) {
1466 BOOL bounded;
1467
1468 *ActualCount = move_next_chars(Count, &start_pos, collapsed, &end_pos, &bounded, &new_pos);
1469 set_range_pos(This, !bounded, &new_pos);
1470 if(bounded)
1471 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, FALSE);
1472 }else {
1473 *ActualCount = -move_prev_chars(This, -Count, &start_pos, FALSE, NULL, NULL, &new_pos);
1474 set_range_pos(This, TRUE, &new_pos);
1475 }
1476
1477 dompos_release(&start_pos);
1478 dompos_release(&end_pos);
1479 dompos_release(&new_pos);
1480 break;
1481 }
1482
1483 default:
1484 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1485 }
1486
1487 return S_OK;
1488 }
1489
1490 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1491 LONG Count, LONG *ActualCount)
1492 {
1493 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1494 range_unit_t unit;
1495
1496 TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1497
1498 unit = string_to_unit(Unit);
1499 if(unit == RU_UNKNOWN)
1500 return E_INVALIDARG;
1501
1502 if(!Count) {
1503 *ActualCount = 0;
1504 return S_OK;
1505 }
1506
1507 switch(unit) {
1508 case RU_CHAR: {
1509 dompos_t start_pos, end_pos, new_pos;
1510 cpp_bool collapsed;
1511
1512 get_cur_pos(This, TRUE, &start_pos);
1513 get_cur_pos(This, FALSE, &end_pos);
1514 nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
1515
1516 if(Count > 0) {
1517 *ActualCount = move_next_chars(Count, &end_pos, collapsed, NULL, NULL, &new_pos);
1518 set_range_pos(This, FALSE, &new_pos);
1519 }else {
1520 BOOL bounded;
1521
1522 *ActualCount = -move_prev_chars(This, -Count, &end_pos, TRUE, &start_pos, &bounded, &new_pos);
1523 set_range_pos(This, bounded, &new_pos);
1524 if(bounded)
1525 IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, TRUE);
1526 }
1527
1528 dompos_release(&start_pos);
1529 dompos_release(&end_pos);
1530 dompos_release(&new_pos);
1531 break;
1532 }
1533
1534 default:
1535 FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1536 }
1537
1538 return S_OK;
1539 }
1540
1541 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
1542 {
1543 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1544 nsISelection *nsselection;
1545 nsresult nsres;
1546
1547 TRACE("(%p)\n", This);
1548
1549 nsres = nsIDOMWindow_GetSelection(This->doc->basedoc.window->nswindow, &nsselection);
1550 if(NS_FAILED(nsres)) {
1551 ERR("GetSelection failed: %08x\n", nsres);
1552 return E_FAIL;
1553 }
1554
1555 nsISelection_RemoveAllRanges(nsselection);
1556 nsISelection_AddRange(nsselection, This->nsrange);
1557 nsISelection_Release(nsselection);
1558 return S_OK;
1559 }
1560
1561 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
1562 {
1563 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1564 FIXME("(%p)->(%s)\n", This, debugstr_w(html));
1565 return E_NOTIMPL;
1566 }
1567
1568 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
1569 {
1570 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1571 FIXME("(%p)->(%p)\n", This, element);
1572 return E_NOTIMPL;
1573 }
1574
1575 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
1576 IHTMLTxtRange *SourceRange)
1577 {
1578 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1579 FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
1580 return E_NOTIMPL;
1581 }
1582
1583 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1584 IHTMLTxtRange *SourceRange, LONG *ret)
1585 {
1586 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1587 HTMLTxtRange *src_range;
1588 short nsret = 0;
1589 int nscmpt;
1590 nsresult nsres;
1591
1592 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
1593
1594 nscmpt = string_to_nscmptype(how);
1595 if(nscmpt == -1)
1596 return E_INVALIDARG;
1597
1598 src_range = get_range_object(This->doc, SourceRange);
1599 if(!src_range)
1600 return E_FAIL;
1601
1602 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
1603 if(NS_FAILED(nsres))
1604 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
1605
1606 *ret = nsret;
1607 return S_OK;
1608 }
1609
1610 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1611 LONG count, LONG Flags, VARIANT_BOOL *Success)
1612 {
1613 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1614 FIXME("(%p)->(%s %d %08x %p)\n", This, debugstr_w(String), count, Flags, Success);
1615 return E_NOTIMPL;
1616 }
1617
1618 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, LONG x, LONG y)
1619 {
1620 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1621 FIXME("(%p)->(%d %d)\n", This, x, y);
1622 return E_NOTIMPL;
1623 }
1624
1625 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
1626 {
1627 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1628 FIXME("(%p)->(%p)\n", This, Bookmark);
1629 return E_NOTIMPL;
1630 }
1631
1632 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
1633 VARIANT_BOOL *Success)
1634 {
1635 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1636 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
1637 return E_NOTIMPL;
1638 }
1639
1640 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
1641 VARIANT_BOOL *pfRet)
1642 {
1643 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1644 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1645 return E_NOTIMPL;
1646 }
1647
1648 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
1649 VARIANT_BOOL *pfRet)
1650 {
1651 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1652 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1653 return E_NOTIMPL;
1654 }
1655
1656 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
1657 VARIANT_BOOL *pfRet)
1658 {
1659 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1660 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1661 return E_NOTIMPL;
1662 }
1663
1664 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
1665 VARIANT_BOOL *pfRet)
1666 {
1667 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1668 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1669 return E_NOTIMPL;
1670 }
1671
1672 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
1673 BSTR *pcmdText)
1674 {
1675 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1676 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
1677 return E_NOTIMPL;
1678 }
1679
1680 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
1681 VARIANT *pcmdValue)
1682 {
1683 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1684 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
1685 return E_NOTIMPL;
1686 }
1687
1688 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
1689 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1690 {
1691 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1692 FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
1693 return E_NOTIMPL;
1694 }
1695
1696 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
1697 VARIANT_BOOL *pfRet)
1698 {
1699 HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1700 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1701 return E_NOTIMPL;
1702 }
1703
1704 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
1705 HTMLTxtRange_QueryInterface,
1706 HTMLTxtRange_AddRef,
1707 HTMLTxtRange_Release,
1708 HTMLTxtRange_GetTypeInfoCount,
1709 HTMLTxtRange_GetTypeInfo,
1710 HTMLTxtRange_GetIDsOfNames,
1711 HTMLTxtRange_Invoke,
1712 HTMLTxtRange_get_htmlText,
1713 HTMLTxtRange_put_text,
1714 HTMLTxtRange_get_text,
1715 HTMLTxtRange_parentElement,
1716 HTMLTxtRange_duplicate,
1717 HTMLTxtRange_inRange,
1718 HTMLTxtRange_isEqual,
1719 HTMLTxtRange_scrollIntoView,
1720 HTMLTxtRange_collapse,
1721 HTMLTxtRange_expand,
1722 HTMLTxtRange_move,
1723 HTMLTxtRange_moveStart,
1724 HTMLTxtRange_moveEnd,
1725 HTMLTxtRange_select,
1726 HTMLTxtRange_pasteHTML,
1727 HTMLTxtRange_moveToElementText,
1728 HTMLTxtRange_setEndPoint,
1729 HTMLTxtRange_compareEndPoints,
1730 HTMLTxtRange_findText,
1731 HTMLTxtRange_moveToPoint,
1732 HTMLTxtRange_getBookmark,
1733 HTMLTxtRange_moveToBookmark,
1734 HTMLTxtRange_queryCommandSupported,
1735 HTMLTxtRange_queryCommandEnabled,
1736 HTMLTxtRange_queryCommandState,
1737 HTMLTxtRange_queryCommandIndeterm,
1738 HTMLTxtRange_queryCommandText,
1739 HTMLTxtRange_queryCommandValue,
1740 HTMLTxtRange_execCommand,
1741 HTMLTxtRange_execCommandShowHelp
1742 };
1743
1744 static inline HTMLTxtRange *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
1745 {
1746 return CONTAINING_RECORD(iface, HTMLTxtRange, IOleCommandTarget_iface);
1747 }
1748
1749 static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
1750 {
1751 HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1752 return IHTMLTxtRange_QueryInterface(&This->IHTMLTxtRange_iface, riid, ppv);
1753 }
1754
1755 static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
1756 {
1757 HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1758 return IHTMLTxtRange_AddRef(&This->IHTMLTxtRange_iface);
1759 }
1760
1761 static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
1762 {
1763 HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1764 return IHTMLTxtRange_Release(&This->IHTMLTxtRange_iface);
1765 }
1766
1767 static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1768 ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
1769 {
1770 HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1771 FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
1772 return E_NOTIMPL;
1773 }
1774
1775 static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
1776 {
1777 nsIDOMHTMLElement *blockquote_elem, *p_elem;
1778 nsIDOMDocumentFragment *fragment;
1779 nsIDOMNode *tmp;
1780
1781 static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
1782 static const PRUnichar pW[] = {'P',0};
1783
1784 TRACE("(%p)->(%p %p)\n", This, in, out);
1785
1786 if(!This->doc->nsdoc) {
1787 WARN("NULL nsdoc\n");
1788 return E_NOTIMPL;
1789 }
1790
1791 create_nselem(This->doc, blockquoteW, &blockquote_elem);
1792 create_nselem(This->doc, pW, &p_elem);
1793
1794 nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1795 nsIDOMHTMLElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1796 nsIDOMDocumentFragment_Release(fragment);
1797 nsIDOMNode_Release(tmp);
1798
1799 nsIDOMHTMLElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
1800 nsIDOMHTMLElement_Release(p_elem);
1801 nsIDOMNode_Release(tmp);
1802
1803 nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1804 nsIDOMHTMLElement_Release(blockquote_elem);
1805
1806 return S_OK;
1807 }
1808
1809 static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
1810 DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
1811 {
1812 HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1813
1814 TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
1815 nCmdexecopt, pvaIn, pvaOut);
1816
1817 if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1818 switch(nCmdID) {
1819 case IDM_INDENT:
1820 return exec_indent(This, pvaIn, pvaOut);
1821 default:
1822 FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
1823 }
1824 }else {
1825 FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
1826 }
1827
1828 return E_NOTIMPL;
1829 }
1830
1831 static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
1832 RangeCommandTarget_QueryInterface,
1833 RangeCommandTarget_AddRef,
1834 RangeCommandTarget_Release,
1835 RangeCommandTarget_QueryStatus,
1836 RangeCommandTarget_Exec
1837 };
1838
1839 HRESULT HTMLTxtRange_Create(HTMLDocumentNode *doc, nsIDOMRange *nsrange, IHTMLTxtRange **p)
1840 {
1841 HTMLTxtRange *ret;
1842
1843 ret = heap_alloc(sizeof(HTMLTxtRange));
1844 if(!ret)
1845 return E_OUTOFMEMORY;
1846
1847 ret->IHTMLTxtRange_iface.lpVtbl = &HTMLTxtRangeVtbl;
1848 ret->IOleCommandTarget_iface.lpVtbl = &OleCommandTargetVtbl;
1849 ret->ref = 1;
1850
1851 if(nsrange)
1852 nsIDOMRange_AddRef(nsrange);
1853 ret->nsrange = nsrange;
1854
1855 ret->doc = doc;
1856 list_add_head(&doc->range_list, &ret->entry);
1857
1858 *p = &ret->IHTMLTxtRange_iface;
1859 return S_OK;
1860 }
1861
1862 void detach_ranges(HTMLDocumentNode *This)
1863 {
1864 HTMLTxtRange *iter;
1865
1866 LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
1867 iter->doc = NULL;
1868 }
1869 }