txtrange.c 46.5 KB
Newer Older
1
/*
2
 * Copyright 2006-2007 Jacek Caban for CodeWeavers
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
20
#include <assert.h>
21 22 23 24 25 26 27

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
28
#include "mshtmcid.h"
29 30 31 32 33 34 35

#include "wine/debug.h"

#include "mshtml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

36
static const WCHAR brW[] = {'b','r',0};
37
static const WCHAR hrW[] = {'h','r',0};
38

39
typedef struct {
40
    DispatchEx dispex;
41
    IHTMLTxtRange     IHTMLTxtRange_iface;
42
    IOleCommandTarget IOleCommandTarget_iface;
43 44 45

    LONG ref;

46
    nsIDOMRange *nsrange;
47
    HTMLDocumentNode *doc;
48 49

    struct list entry;
50 51
} HTMLTxtRange;

52 53 54 55 56 57
typedef struct {
    WCHAR *buf;
    DWORD len;
    DWORD size;
} wstrbuf_t;

58 59 60 61 62 63
typedef struct {
    UINT16 type;
    nsIDOMNode *node;
    UINT32 off;
} rangepoint_t;

64 65 66 67 68 69 70 71
typedef enum {
    RU_UNKNOWN,
    RU_CHAR,
    RU_WORD,
    RU_SENTENCE,
    RU_TEXTEDIT
} range_unit_t;

72
static HTMLTxtRange *get_range_object(HTMLDocumentNode *doc, IHTMLTxtRange *iface)
73 74 75 76
{
    HTMLTxtRange *iter;

    LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
77
        if(&iter->IHTMLTxtRange_iface == iface)
78 79 80 81 82 83 84
            return iter;
    }

    ERR("Could not find range in document\n");
    return NULL;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static range_unit_t string_to_unit(LPCWSTR str)
{
    static const WCHAR characterW[] =
        {'c','h','a','r','a','c','t','e','r',0};
    static const WCHAR wordW[] =
        {'w','o','r','d',0};
    static const WCHAR sentenceW[] =
        {'s','e','n','t','e','n','c','e',0};
    static const WCHAR texteditW[] =
        {'t','e','x','t','e','d','i','t',0};

    if(!strcmpiW(str, characterW))  return RU_CHAR;
    if(!strcmpiW(str, wordW))       return RU_WORD;
    if(!strcmpiW(str, sentenceW))   return RU_SENTENCE;
    if(!strcmpiW(str, texteditW))   return RU_TEXTEDIT;

    return RU_UNKNOWN;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static int string_to_nscmptype(LPCWSTR str)
{
    static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
    static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
    static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
    static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};

    if(!strcmpiW(str, seW))  return NS_START_TO_END;
    if(!strcmpiW(str, ssW))  return NS_START_TO_START;
    if(!strcmpiW(str, esW))  return NS_END_TO_START;
    if(!strcmpiW(str, eeW))  return NS_END_TO_END;

    return -1;
}

119
static UINT16 get_node_type(nsIDOMNode *node)
120
{
121
    UINT16 type = 0;
122 123 124 125 126 127 128

    if(node)
        nsIDOMNode_GetNodeType(node, &type);

    return type;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
static void get_text_node_data(nsIDOMNode *node, nsAString *nsstr, const PRUnichar **str)
{
    nsIDOMText *nstext;
    nsresult nsres;

    nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
    assert(nsres == NS_OK);

    nsAString_Init(nsstr, NULL);
    nsres = nsIDOMText_GetData(nstext, nsstr);
    nsIDOMText_Release(nstext);
    if(NS_FAILED(nsres))
        ERR("GetData failed: %08x\n", nsres);

    nsAString_GetData(nsstr, str);
}

static nsIDOMNode *get_child_node(nsIDOMNode *node, UINT32 off)
{
    nsIDOMNodeList *node_list;
    nsIDOMNode *ret = NULL;

    nsIDOMNode_GetChildNodes(node, &node_list);
    nsIDOMNodeList_Item(node_list, off, &ret);
    nsIDOMNodeList_Release(node_list);

    return ret;
}

/* This is very inefficient, but there is no faster way to compute index in
 * child node list using public API. Gecko has internal nsINode::IndexOf
 * function that we could consider exporting and use instead. */
static int get_child_index(nsIDOMNode *parent, nsIDOMNode *child)
{
    nsIDOMNodeList *node_list;
    nsIDOMNode *node;
    int ret = 0;
    nsresult nsres;

    nsres = nsIDOMNode_GetChildNodes(parent, &node_list);
    assert(nsres == NS_OK);

    while(1) {
        nsres = nsIDOMNodeList_Item(node_list, ret, &node);
        assert(nsres == NS_OK && node);
        if(node == child) {
            nsIDOMNode_Release(node);
            break;
        }
        nsIDOMNode_Release(node);
        ret++;
    }

    nsIDOMNodeList_Release(node_list);
    return ret;
}

static void init_rangepoint(rangepoint_t *rangepoint, nsIDOMNode *node, UINT32 off)
{
    nsIDOMNode_AddRef(node);

    rangepoint->type = get_node_type(node);
    rangepoint->node = node;
    rangepoint->off = off;
}

static inline void free_rangepoint(rangepoint_t *rangepoint)
{
    nsIDOMNode_Release(rangepoint->node);
}

static inline BOOL rangepoint_cmp(const rangepoint_t *point1, const rangepoint_t *point2)
{
    return point1->node == point2->node && point1->off == point2->off;
}

static BOOL rangepoint_next_node(rangepoint_t *iter)
{
    nsIDOMNode *node;
    UINT32 off;
    nsresult nsres;

    /* Try to move to the child node. */
    node = get_child_node(iter->node, iter->off);
    if(node) {
        free_rangepoint(iter);
        init_rangepoint(iter, node, 0);
        nsIDOMNode_Release(node);
        return TRUE;
    }

    /* There are no more children in the node. Move to parent. */
    nsres = nsIDOMNode_GetParentNode(iter->node, &node);
    assert(nsres == NS_OK);
    if(!node)
        return FALSE;

    off = get_child_index(node, iter->node)+1;
    free_rangepoint(iter);
    init_rangepoint(iter, node, off);
    nsIDOMNode_Release(node);
    return TRUE;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static UINT32 get_child_count(nsIDOMNode *node)
{
    nsIDOMNodeList *node_list;
    UINT32 ret;
    nsresult nsres;

    nsres = nsIDOMNode_GetChildNodes(node, &node_list);
    assert(nsres == NS_OK);

    nsres = nsIDOMNodeList_GetLength(node_list, &ret);
    nsIDOMNodeList_Release(node_list);
    assert(nsres == NS_OK);

    return ret;
}

static UINT32 get_text_length(nsIDOMNode *node)
{
    nsIDOMText *nstext;
    UINT32 ret;
    nsresult nsres;

    nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);
    assert(nsres == NS_OK);

    nsres = nsIDOMText_GetLength(nstext, &ret);
    nsIDOMText_Release(nstext);
    assert(nsres == NS_OK);

    return ret;
}

static BOOL rangepoint_prev_node(rangepoint_t *iter)
{
    nsIDOMNode *node;
    UINT32 off;
    nsresult nsres;

    /* Try to move to the child node. */
    if(iter->off) {
        node = get_child_node(iter->node, iter->off-1);
        assert(node != NULL);

        off = get_node_type(node) == TEXT_NODE ? get_text_length(node) : get_child_count(node);
        free_rangepoint(iter);
        init_rangepoint(iter, node, off);
        nsIDOMNode_Release(node);
        return TRUE;
    }

    /* There are no more children in the node. Move to parent. */
    nsres = nsIDOMNode_GetParentNode(iter->node, &node);
    assert(nsres == NS_OK);
    if(!node)
        return FALSE;

    off = get_child_index(node, iter->node);
    free_rangepoint(iter);
    init_rangepoint(iter, node, off);
    return TRUE;
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
static void get_start_point(HTMLTxtRange *This, rangepoint_t *ret)
{
    nsIDOMNode *node;
    LONG off;

    nsIDOMRange_GetStartContainer(This->nsrange, &node);
    nsIDOMRange_GetStartOffset(This->nsrange, &off);

    init_rangepoint(ret, node, off);

    nsIDOMNode_Release(node);
}

static void get_end_point(HTMLTxtRange *This, rangepoint_t *ret)
{
    nsIDOMNode *node;
    LONG off;

    nsIDOMRange_GetEndContainer(This->nsrange, &node);
    nsIDOMRange_GetEndOffset(This->nsrange, &off);

    init_rangepoint(ret, node, off);

    nsIDOMNode_Release(node);
}

321 322 323 324 325 326 327
static void set_start_point(HTMLTxtRange *This, const rangepoint_t *start)
{
    nsresult nsres = nsIDOMRange_SetStart(This->nsrange, start->node, start->off);
    if(NS_FAILED(nsres))
        ERR("failed: %08x\n", nsres);
}

328 329 330 331 332 333 334
static void set_end_point(HTMLTxtRange *This, const rangepoint_t *end)
{
    nsresult nsres = nsIDOMRange_SetEnd(This->nsrange, end->node, end->off);
    if(NS_FAILED(nsres))
        ERR("failed: %08x\n", nsres);
}

335
static BOOL is_elem_tag(nsIDOMNode *node, LPCWSTR istag)
336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
    nsIDOMElement *elem;
    nsAString tag_str;
    const PRUnichar *tag;
    BOOL ret = FALSE;
    nsresult nsres;

    nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMElement, (void**)&elem);
    if(NS_FAILED(nsres))
        return FALSE;

    nsAString_Init(&tag_str, NULL);
    nsIDOMElement_GetTagName(elem, &tag_str);
    nsIDOMElement_Release(elem);
350
    nsAString_GetData(&tag_str, &tag);
351

352 353 354 355 356 357 358
    ret = !strcmpiW(tag, istag);

    nsAString_Finish(&tag_str);

    return ret;
}

359
static inline BOOL wstrbuf_init(wstrbuf_t *buf)
360 361 362
{
    buf->len = 0;
    buf->size = 16;
363
    buf->buf = heap_alloc(buf->size * sizeof(WCHAR));
364
    if (!buf->buf) return FALSE;
365
    *buf->buf = 0;
366
    return TRUE;
367 368
}

369
static inline void wstrbuf_finish(wstrbuf_t *buf)
370
{
371
    heap_free(buf->buf);
372 373 374 375 376
}

static void wstrbuf_append_len(wstrbuf_t *buf, LPCWSTR str, int len)
{
    if(buf->len+len >= buf->size) {
377
        buf->size = 2*buf->size+len;
378
        buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
379 380 381 382 383 384 385
    }

    memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
    buf->len += len;
    buf->buf[buf->len] = 0;
}

386 387 388 389 390
static void wstrbuf_append_nodetxt(wstrbuf_t *buf, LPCWSTR str, int len)
{
    const WCHAR *s = str;
    WCHAR *d;

391 392
    TRACE("%s\n", debugstr_wn(str, len));

393
    if(buf->len+len >= buf->size) {
394
        buf->size = 2*buf->size+len;
395
        buf->buf = heap_realloc(buf->buf, buf->size * sizeof(WCHAR));
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    }

    if(buf->len && isspaceW(buf->buf[buf->len-1])) {
        while(s < str+len && isspaceW(*s))
            s++;
    }

    d = buf->buf+buf->len;
    while(s < str+len) {
        if(isspaceW(*s)) {
            *d++ = ' ';
            s++;
            while(s < str+len && isspaceW(*s))
                s++;
        }else {
            *d++ = *s++;
        }
    }

    buf->len = d - buf->buf;
    *d = 0;
}

419
static void wstrbuf_append_node(wstrbuf_t *buf, nsIDOMNode *node, BOOL ignore_text)
420 421 422 423 424 425 426 427
{

    switch(get_node_type(node)) {
    case TEXT_NODE: {
        nsIDOMText *nstext;
        nsAString data_str;
        const PRUnichar *data;

428 429 430
        if(ignore_text)
            break;

431 432 433 434
        nsIDOMNode_QueryInterface(node, &IID_nsIDOMText, (void**)&nstext);

        nsAString_Init(&data_str, NULL);
        nsIDOMText_GetData(nstext, &data_str);
435
        nsAString_GetData(&data_str, &data);
436
        wstrbuf_append_nodetxt(buf, data, strlenW(data));
437 438
        nsAString_Finish(&data_str);

439
        nsIDOMText_Release(nstext);
440 441 442 443

        break;
    }
    case ELEMENT_NODE:
444
        if(is_elem_tag(node, brW)) {
445 446
            static const WCHAR endlW[] = {'\r','\n'};
            wstrbuf_append_len(buf, endlW, 2);
447 448 449
        }else if(is_elem_tag(node, hrW)) {
            static const WCHAR endl2W[] = {'\r','\n','\r','\n'};
            wstrbuf_append_len(buf, endl2W, 4);
450 451 452 453
        }
    }
}

454 455 456 457
static void wstrbuf_append_node_rec(wstrbuf_t *buf, nsIDOMNode *node)
{
    nsIDOMNode *iter, *tmp;

458
    wstrbuf_append_node(buf, node, FALSE);
459 460 461 462 463 464 465 466 467 468

    nsIDOMNode_GetFirstChild(node, &iter);
    while(iter) {
        wstrbuf_append_node_rec(buf, iter);
        nsIDOMNode_GetNextSibling(iter, &tmp);
        nsIDOMNode_Release(iter);
        iter = tmp;
    }
}

469 470
static void range_to_string(HTMLTxtRange *This, wstrbuf_t *buf)
{
471
    rangepoint_t end_pos, iter;
472
    cpp_bool collapsed;
473 474 475 476 477 478 479 480 481

    nsIDOMRange_GetCollapsed(This->nsrange, &collapsed);
    if(collapsed) {
        wstrbuf_finish(buf);
        buf->buf = NULL;
        buf->size = 0;
        return;
    }

482 483
    get_end_point(This, &end_pos);
    get_start_point(This, &iter);
484

485 486 487 488
    do {
        if(iter.type == TEXT_NODE) {
            const PRUnichar *str;
            nsAString nsstr;
489

490
            get_text_node_data(iter.node, &nsstr, &str);
491

492 493 494 495 496
            if(iter.node == end_pos.node) {
                wstrbuf_append_nodetxt(buf, str+iter.off, end_pos.off-iter.off);
                nsAString_Finish(&nsstr);
                break;
            }
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
            wstrbuf_append_nodetxt(buf, str+iter.off, strlenW(str+iter.off));
            nsAString_Finish(&nsstr);
        }else {
            nsIDOMNode *node;

            node = get_child_node(iter.node, iter.off);
            if(node) {
                wstrbuf_append_node(buf, node, TRUE);
                nsIDOMNode_Release(node);
            }
        }

        if(!rangepoint_next_node(&iter)) {
            ERR("End of document?\n");
            break;
        }
    }while(!rangepoint_cmp(&iter, &end_pos));
515

516 517
    free_rangepoint(&iter);
    free_rangepoint(&end_pos);
518 519 520 521 522 523 524 525 526 527

    if(buf->len) {
        WCHAR *p;

        for(p = buf->buf+buf->len-1; p >= buf->buf && isspaceW(*p); p--);

        p = strchrW(p, '\r');
        if(p)
            *p = 0;
    }
528 529
}

530 531 532 533 534
HRESULT get_node_text(HTMLDOMNode *node, BSTR *ret)
{
    wstrbuf_t buf;
    HRESULT hres = S_OK;

535 536
    if (!wstrbuf_init(&buf))
        return E_OUTOFMEMORY;
537
    wstrbuf_append_node_rec(&buf, node->nsnode);
538
    if(buf.buf && *buf.buf) {
539 540 541
        *ret = SysAllocString(buf.buf);
        if(!*ret)
            hres = E_OUTOFMEMORY;
542
    } else {
543 544 545 546 547 548 549 550 551
        *ret = NULL;
    }
    wstrbuf_finish(&buf);

    if(SUCCEEDED(hres))
        TRACE("ret %s\n", debugstr_w(*ret));
    return hres;
}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
static WCHAR move_next_char(rangepoint_t *iter)
{
    rangepoint_t last_space;
    nsIDOMNode *node;
    WCHAR cspace = 0;
    const WCHAR *p;

    do {
        switch(iter->type) {
        case TEXT_NODE: {
            const PRUnichar *str;
            nsAString nsstr;
            WCHAR c;

            get_text_node_data(iter->node, &nsstr, &str);
            p = str+iter->off;
            if(!*p) {
                nsAString_Finish(&nsstr);
                break;
            }

            c = *p;
            if(isspaceW(c)) {
                while(isspaceW(*p))
                    p++;

                if(cspace)
                    free_rangepoint(&last_space);
                else
                    cspace = ' ';

                iter->off = p-str;
                c = *p;
                nsAString_Finish(&nsstr);
                if(!c) { /* continue to skip spaces */
                    init_rangepoint(&last_space, iter->node, iter->off);
                    break;
                }

                return cspace;
            }else {
                nsAString_Finish(&nsstr);
            }

            /* If we have a non-space char and we're skipping spaces, stop and return the last found space. */
            if(cspace) {
                free_rangepoint(iter);
                *iter = last_space;
                return cspace;
            }

            iter->off++;
            return c;
        }
        case ELEMENT_NODE:
            node = get_child_node(iter->node, iter->off);
            if(!node)
                break;

            if(is_elem_tag(node, brW)) {
                if(cspace) {
                    nsIDOMNode_Release(node);
                    free_rangepoint(iter);
                    *iter = last_space;
                    return cspace;
                }

                cspace = '\n';
                init_rangepoint(&last_space, iter->node, iter->off+1);
            }else if(is_elem_tag(node, hrW)) {
                nsIDOMNode_Release(node);
                if(cspace) {
                    free_rangepoint(iter);
                    *iter = last_space;
                    return cspace;
                }

                iter->off++;
                return '\n';
            }

            nsIDOMNode_Release(node);
        }
    }while(rangepoint_next_node(iter));

    return cspace;
}

static WCHAR move_prev_char(rangepoint_t *iter)
{
    rangepoint_t last_space;
    nsIDOMNode *node;
    WCHAR cspace = 0;
    const WCHAR *p;

    do {
        switch(iter->type) {
        case TEXT_NODE: {
            const PRUnichar *str;
            nsAString nsstr;
            WCHAR c;

            if(!iter->off)
                break;

            get_text_node_data(iter->node, &nsstr, &str);

            p = str+iter->off-1;
            c = *p;

            if(isspaceW(c)) {
                while(p > str && isspaceW(*(p-1)))
                    p--;

                if(cspace)
                    free_rangepoint(&last_space);
                else
                    cspace = ' ';

                iter->off = p-str;
                nsAString_Finish(&nsstr);
                if(p == str) { /* continue to skip spaces */
                    init_rangepoint(&last_space, iter->node, iter->off);
                    break;
                }

                return cspace;
            }else {
                nsAString_Finish(&nsstr);
            }

            /* If we have a non-space char and we're skipping spaces, stop and return the last found space. */
            if(cspace) {
                free_rangepoint(iter);
                *iter = last_space;
                return cspace;
            }

            iter->off--;
            return c;
        }
        case ELEMENT_NODE:
            if(!iter->off)
                break;

            node = get_child_node(iter->node, iter->off-1);
            if(!node)
                break;

            if(is_elem_tag(node, brW)) {
                if(cspace)
                    free_rangepoint(&last_space);
                cspace = '\n';
                init_rangepoint(&last_space, iter->node, iter->off-1);
            }else if(is_elem_tag(node, hrW)) {
                nsIDOMNode_Release(node);
                if(cspace) {
                    free_rangepoint(iter);
                    *iter = last_space;
                    return cspace;
                }

                iter->off--;
                return '\n';
            }

            nsIDOMNode_Release(node);
        }
    }while(rangepoint_prev_node(iter));

    if(cspace) {
        free_rangepoint(iter);
        *iter = last_space;
        return cspace;
    }

    return 0;
}

static LONG move_by_chars(rangepoint_t *iter, LONG cnt)
{
    LONG ret = 0;

    if(cnt >= 0) {
        while(ret < cnt && move_next_char(iter))
            ret++;
    }else {
        while(ret > cnt && move_prev_char(iter))
            ret--;
    }

    return ret;
}

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
static LONG find_prev_space(rangepoint_t *iter, BOOL first_space)
{
    rangepoint_t prev;
    WCHAR c;

    init_rangepoint(&prev, iter->node, iter->off);
    c = move_prev_char(&prev);
    if(!c || (first_space && isspaceW(c)))
        return FALSE;

    do {
        free_rangepoint(iter);
        init_rangepoint(iter, prev.node, prev.off);
        c = move_prev_char(&prev);
    }while(c && !isspaceW(c));

    free_rangepoint(&prev);
    return TRUE;
}

static BOOL find_word_end(rangepoint_t *iter, BOOL is_collapsed)
{
    rangepoint_t prev_iter;
    WCHAR c;
    BOOL ret = FALSE;

    if(!is_collapsed) {
        init_rangepoint(&prev_iter, iter->node, iter->off);
        c = move_prev_char(&prev_iter);
        free_rangepoint(&prev_iter);
        if(isspaceW(c))
            return FALSE;
    }

    do {
        init_rangepoint(&prev_iter, iter->node, iter->off);
        c = move_next_char(iter);
        if(c == '\n') {
            free_rangepoint(iter);
            *iter = prev_iter;
            return ret;
        }
        if(!c) {
            if(!ret)
                ret = !rangepoint_cmp(iter, &prev_iter);
        }else {
            ret = TRUE;
        }
        free_rangepoint(&prev_iter);
    }while(c && !isspaceW(c));

    return ret;
}

800
static LONG move_by_words(rangepoint_t *iter, LONG cnt)
801
{
802
    LONG ret = 0;
803

804 805
    if(cnt >= 0) {
        WCHAR c;
806

807 808
        while(ret < cnt && (c = move_next_char(iter))) {
            if(isspaceW(c))
809
                ret++;
810 811
        }
    }else {
812 813
        while(ret > cnt && find_prev_space(iter, FALSE))
            ret--;
814 815 816 817 818
    }

    return ret;
}

819 820 821 822
static inline HTMLTxtRange *impl_from_IHTMLTxtRange(IHTMLTxtRange *iface)
{
    return CONTAINING_RECORD(iface, HTMLTxtRange, IHTMLTxtRange_iface);
}
823 824 825

static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
{
826
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
827

828
    TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
829 830

    if(IsEqualGUID(&IID_IUnknown, riid)) {
831
        *ppv = &This->IHTMLTxtRange_iface;
832
    }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
833
        *ppv = &This->IHTMLTxtRange_iface;
834
    }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
835
        *ppv = &This->IOleCommandTarget_iface;
836 837 838 839 840 841
    }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
        return *ppv ? S_OK : E_NOINTERFACE;
    }else {
        *ppv = NULL;
        WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        return E_NOINTERFACE;
842 843
    }

844 845
    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
846 847 848 849
}

static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
{
850
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
851 852
    LONG ref = InterlockedIncrement(&This->ref);

853
    TRACE("(%p) ref=%d\n", This, ref);
854 855 856 857 858 859

    return ref;
}

static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
{
860
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
861 862
    LONG ref = InterlockedDecrement(&This->ref);

863
    TRACE("(%p) ref=%d\n", This, ref);
864 865

    if(!ref) {
866
        if(This->nsrange)
867
            nsIDOMRange_Release(This->nsrange);
868 869
        if(This->doc)
            list_remove(&This->entry);
870
        release_dispex(&This->dispex);
871
        heap_free(This);
872 873 874 875 876 877 878
    }

    return ref;
}

static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
{
879
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
880 881

    return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
882 883 884 885 886
}

static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
                                               LCID lcid, ITypeInfo **ppTInfo)
{
887
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
888 889

    return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
890 891 892 893 894 895
}

static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
                                                 LPOLESTR *rgszNames, UINT cNames,
                                                 LCID lcid, DISPID *rgDispId)
{
896
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
897 898 899

    return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames,
            cNames, lcid, rgDispId);
900 901 902 903 904 905
}

static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
                            REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                            VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
906
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
907 908 909

    return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid,
            lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
910 911 912 913
}

static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
{
914
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

    TRACE("(%p)->(%p)\n", This, p);

    *p = NULL;

    if(This->nsrange) {
        nsIDOMDocumentFragment *fragment;
        nsresult nsres;

        nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
        if(NS_SUCCEEDED(nsres)) {
            const PRUnichar *nstext;
            nsAString nsstr;

            nsAString_Init(&nsstr, NULL);
            nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
            nsIDOMDocumentFragment_Release(fragment);

933
            nsAString_GetData(&nsstr, &nstext);
934 935 936 937 938 939 940 941 942 943 944 945 946
            *p = SysAllocString(nstext);

            nsAString_Finish(&nsstr);
        }
    }

    if(!*p) {
        const WCHAR emptyW[] = {0};
        *p = SysAllocString(emptyW);
    }

    TRACE("return %s\n", debugstr_w(*p));
    return S_OK;
947 948 949 950
}

static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
{
951
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
952 953 954 955 956 957 958 959 960
    nsIDOMText *text_node;
    nsAString text_str;
    nsresult nsres;

    TRACE("(%p)->(%s)\n", This, debugstr_w(v));

    if(!This->doc)
        return MSHTML_E_NODOC;

961
    nsAString_InitDepend(&text_str, v);
962
    nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc->nsdoc, &text_str, &text_node);
963 964 965 966 967 968 969 970 971 972 973 974 975
    nsAString_Finish(&text_str);
    if(NS_FAILED(nsres)) {
        ERR("CreateTextNode failed: %08x\n", nsres);
        return S_OK;
    }
    nsres = nsIDOMRange_DeleteContents(This->nsrange);
    if(NS_FAILED(nsres))
        ERR("DeleteContents failed: %08x\n", nsres);

    nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
    if(NS_FAILED(nsres))
        ERR("InsertNode failed: %08x\n", nsres);

976 977 978 979
    nsres = nsIDOMRange_SetEndAfter(This->nsrange, (nsIDOMNode*)text_node);
    if(NS_FAILED(nsres))
        ERR("SetEndAfter failed: %08x\n", nsres);

980
    return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_FALSE);
981 982 983 984
}

static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
{
985
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
986
    wstrbuf_t buf;
987 988 989

    TRACE("(%p)->(%p)\n", This, p);

990 991 992 993
    *p = NULL;
    if(!This->nsrange)
        return S_OK;

994 995
    if (!wstrbuf_init(&buf))
        return E_OUTOFMEMORY;
996
    range_to_string(This, &buf);
997
    if (buf.buf)
998 999
        *p = SysAllocString(buf.buf);
    wstrbuf_finish(&buf);
1000

1001
    TRACE("ret %s\n", debugstr_w(*p));
1002
    return S_OK;
1003 1004 1005 1006
}

static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
{
1007
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1008 1009
    nsIDOMNode *nsnode, *tmp;
    HTMLDOMNode *node;
1010
    HRESULT hres;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

    TRACE("(%p)->(%p)\n", This, parent);

    nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
    while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
        nsIDOMNode_GetParentNode(nsnode, &tmp);
        nsIDOMNode_Release(nsnode);
        nsnode = tmp;
    }

    if(!nsnode) {
        *parent = NULL;
        return S_OK;
    }

1026
    hres = get_node(This->doc, nsnode, TRUE, &node);
1027
    nsIDOMNode_Release(nsnode);
1028 1029
    if(FAILED(hres))
        return hres;
1030

1031 1032 1033
    hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)parent);
    node_release(node);
    return hres;
1034 1035 1036 1037
}

static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
{
1038
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1039
    nsIDOMRange *nsrange = NULL;
1040
    HRESULT hres;
1041 1042 1043 1044

    TRACE("(%p)->(%p)\n", This, Duplicate);

    nsIDOMRange_CloneRange(This->nsrange, &nsrange);
1045
    hres = HTMLTxtRange_Create(This->doc, nsrange, Duplicate);
1046 1047
    nsIDOMRange_Release(nsrange);

1048
    return hres;
1049 1050 1051 1052 1053
}

static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
        VARIANT_BOOL *InRange)
{
1054
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1055
    HTMLTxtRange *src_range;
1056
    short nsret = 0;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    nsresult nsres;

    TRACE("(%p)->(%p %p)\n", This, Range, InRange);

    *InRange = VARIANT_FALSE;

    src_range = get_range_object(This->doc, Range);
    if(!src_range)
        return E_FAIL;

    nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
            src_range->nsrange, &nsret);
    if(NS_SUCCEEDED(nsres) && nsret <= 0) {
        nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
                src_range->nsrange, &nsret);
        if(NS_SUCCEEDED(nsres) && nsret >= 0)
            *InRange = VARIANT_TRUE;
    }

    if(NS_FAILED(nsres))
        ERR("CompareBoundaryPoints failed: %08x\n", nsres);

    return S_OK;
1080 1081 1082 1083 1084
}

static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
        VARIANT_BOOL *IsEqual)
{
1085
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1086
    HTMLTxtRange *src_range;
1087
    short nsret = 0;
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    nsresult nsres;

    TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);

    *IsEqual = VARIANT_FALSE;

    src_range = get_range_object(This->doc, Range);
    if(!src_range)
        return E_FAIL;

    nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
            src_range->nsrange, &nsret);
    if(NS_SUCCEEDED(nsres) && !nsret) {
        nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
                src_range->nsrange, &nsret);
        if(NS_SUCCEEDED(nsres) && !nsret)
            *IsEqual = VARIANT_TRUE;
    }

    if(NS_FAILED(nsres))
        ERR("CompareBoundaryPoints failed: %08x\n", nsres);

    return S_OK;
1111 1112 1113 1114
}

static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
{
1115
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1116 1117 1118 1119 1120 1121
    FIXME("(%p)->(%x)\n", This, fStart);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
{
1122
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1123 1124 1125 1126 1127

    TRACE("(%p)->(%x)\n", This, Start);

    nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
    return S_OK;
1128 1129 1130 1131
}

static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
{
1132
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1133 1134 1135 1136 1137 1138 1139 1140
    range_unit_t unit;

    TRACE("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);

    unit = string_to_unit(Unit);
    if(unit == RU_UNKNOWN)
        return E_INVALIDARG;

1141 1142
    *Success = VARIANT_FALSE;

1143 1144
    switch(unit) {
    case RU_WORD: {
1145 1146
        rangepoint_t end, start;
        cpp_bool is_collapsed;
1147

1148 1149
        get_start_point(This, &start);
        get_end_point(This, &end);
1150

1151
        nsIDOMRange_GetCollapsed(This->nsrange, &is_collapsed);
1152

1153 1154
        if(find_word_end(&end, is_collapsed)) {
            set_end_point(This, &end);
1155
            *Success = VARIANT_TRUE;
1156 1157
        }

1158 1159 1160
        if(find_prev_space(&start, TRUE)) {
            set_start_point(This, &start);
            *Success = VARIANT_TRUE;
1161 1162
        }

1163 1164
        free_rangepoint(&end);
        free_rangepoint(&start);
1165 1166
        break;
    }
1167 1168 1169 1170 1171

    case RU_TEXTEDIT: {
        nsIDOMHTMLElement *nsbody = NULL;
        nsresult nsres;

1172
        nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
        if(NS_FAILED(nsres) || !nsbody) {
            ERR("Could not get body: %08x\n", nsres);
            break;
        }

        nsres = nsIDOMRange_SelectNodeContents(This->nsrange, (nsIDOMNode*)nsbody);
        nsIDOMHTMLElement_Release(nsbody);
        if(NS_FAILED(nsres)) {
            ERR("Collapse failed: %08x\n", nsres);
            break;
        }

        *Success = VARIANT_TRUE;
        break;
    }

1189 1190 1191 1192 1193
    default:
        FIXME("Unimplemented unit %s\n", debugstr_w(Unit));
    }

    return S_OK;
1194 1195 1196
}

static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
1197
        LONG Count, LONG *ActualCount)
1198
{
1199
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1200 1201
    range_unit_t unit;

1202
    TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1203 1204 1205 1206 1207 1208 1209

    unit = string_to_unit(Unit);
    if(unit == RU_UNKNOWN)
        return E_INVALIDARG;

    if(!Count) {
        *ActualCount = 0;
1210
        return IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_TRUE);
1211 1212 1213 1214
    }

    switch(unit) {
    case RU_CHAR: {
1215
        rangepoint_t start;
1216

1217
        get_start_point(This, &start);
1218

1219
        *ActualCount = move_by_chars(&start, Count);
1220

1221
        set_start_point(This, &start);
1222
        IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_TRUE);
1223
        free_rangepoint(&start);
1224 1225 1226
        break;
    }

1227
    case RU_WORD: {
1228
        rangepoint_t start;
1229

1230
        get_start_point(This, &start);
1231

1232
        *ActualCount = move_by_words(&start, Count);
1233

1234
        set_start_point(This, &start);
1235
        IHTMLTxtRange_collapse(&This->IHTMLTxtRange_iface, VARIANT_TRUE);
1236
        free_rangepoint(&start);
1237 1238 1239
        break;
    }

1240 1241 1242 1243
    default:
        FIXME("unimplemented unit %s\n", debugstr_w(Unit));
    }

1244
    TRACE("ret %d\n", *ActualCount);
1245
    return S_OK;
1246 1247 1248
}

static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
1249
        LONG Count, LONG *ActualCount)
1250
{
1251
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1252 1253
    range_unit_t unit;

1254
    TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

    unit = string_to_unit(Unit);
    if(unit == RU_UNKNOWN)
        return E_INVALIDARG;

    if(!Count) {
        *ActualCount = 0;
        return S_OK;
    }

    switch(unit) {
    case RU_CHAR: {
1267
        rangepoint_t start;
1268

1269
        get_start_point(This, &start);
1270

1271
        *ActualCount = move_by_chars(&start, Count);
1272

1273 1274
        set_start_point(This, &start);
        free_rangepoint(&start);
1275 1276 1277 1278 1279
        break;
    }

    default:
        FIXME("unimplemented unit %s\n", debugstr_w(Unit));
1280
        return E_NOTIMPL;
1281 1282 1283
    }

    return S_OK;
1284 1285 1286
}

static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
1287
        LONG Count, LONG *ActualCount)
1288
{
1289
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1290 1291
    range_unit_t unit;

1292
    TRACE("(%p)->(%s %d %p)\n", This, debugstr_w(Unit), Count, ActualCount);
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

    unit = string_to_unit(Unit);
    if(unit == RU_UNKNOWN)
        return E_INVALIDARG;

    if(!Count) {
        *ActualCount = 0;
        return S_OK;
    }

    switch(unit) {
    case RU_CHAR: {
1305
        rangepoint_t end;
1306

1307
        get_end_point(This, &end);
1308

1309
        *ActualCount = move_by_chars(&end, Count);
1310

1311 1312
        set_end_point(This, &end);
        free_rangepoint(&end);
1313 1314 1315 1316 1317 1318 1319 1320
        break;
    }

    default:
        FIXME("unimplemented unit %s\n", debugstr_w(Unit));
    }

    return S_OK;
1321 1322 1323 1324
}

static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
{
1325
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1326 1327
    nsISelection *nsselection;
    nsresult nsres;
1328 1329 1330

    TRACE("(%p)\n", This);

1331
    nsres = nsIDOMWindow_GetSelection(This->doc->basedoc.window->nswindow, &nsselection);
1332 1333 1334
    if(NS_FAILED(nsres)) {
        ERR("GetSelection failed: %08x\n", nsres);
        return E_FAIL;
1335 1336
    }

1337 1338 1339
    nsISelection_RemoveAllRanges(nsselection);
    nsISelection_AddRange(nsselection, This->nsrange);
    nsISelection_Release(nsselection);
1340
    return S_OK;
1341 1342 1343 1344
}

static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
{
1345
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
    nsIDOMDocumentFragment *doc_frag;
    nsAString nsstr;
    nsresult nsres;

    TRACE("(%p)->(%s)\n", This, debugstr_w(html));

    nsres = nsIDOMRange_Collapse(This->nsrange, TRUE);
    assert(nsres == NS_OK);

    nsAString_InitDepend(&nsstr, html);
    nsres = nsIDOMRange_CreateContextualFragment(This->nsrange, &nsstr, &doc_frag);
    nsAString_Finish(&nsstr);
    if(NS_FAILED(nsres)) {
        ERR("CreateContextualFragment failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)doc_frag);
    nsIDOMDocumentFragment_Release(doc_frag);
    if(NS_FAILED(nsres)) {
        ERR("InsertNode failed: %08x\n", nsres);
        return E_FAIL;
    }

    nsres = nsIDOMRange_Collapse(This->nsrange, FALSE);
    assert(nsres == NS_OK);
    return S_OK;
1373 1374 1375 1376
}

static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
{
1377
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    HTMLElement *elem;
    nsresult nsres;

    TRACE("(%p)->(%p)\n", This, element);

    elem = unsafe_impl_from_IHTMLElement(element);
    if(!elem)
        return E_INVALIDARG;

    nsres = nsIDOMRange_SelectNodeContents(This->nsrange, elem->node.nsnode);
    if(NS_FAILED(nsres)) {
        ERR("SelectNodeContents failed: %08x\n", nsres);
        return E_FAIL;
    }

    return S_OK;
1394 1395 1396 1397 1398
}

static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
        IHTMLTxtRange *SourceRange)
{
1399
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
    HTMLTxtRange *src_range;
    nsIDOMNode *ref_node;
    INT32 ref_offset;
    BOOL set_start;
    int how_type;
    INT16 cmp;
    nsresult nsres;

    TRACE("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);

    how_type = string_to_nscmptype(how);
    if(how_type == -1)
        return E_INVALIDARG;

    src_range = get_range_object(This->doc, SourceRange);
    if(!src_range)
        return E_FAIL;

    switch(how_type) {
    case NS_START_TO_START:
    case NS_END_TO_START:
        nsres = nsIDOMRange_GetStartContainer(src_range->nsrange, &ref_node);
        assert(nsres == NS_OK);

        nsres = nsIDOMRange_GetStartOffset(src_range->nsrange, &ref_offset);
        assert(nsres == NS_OK);

        set_start = how_type == NS_START_TO_START;
        break;
    case NS_END_TO_END:
    case NS_START_TO_END:
        nsres = nsIDOMRange_GetEndContainer(src_range->nsrange, &ref_node);
        assert(nsres == NS_OK);

        nsres = nsIDOMRange_GetEndOffset(src_range->nsrange, &ref_offset);
        assert(nsres == NS_OK);

        set_start = how_type == NS_START_TO_END;
        break;
    DEFAULT_UNREACHABLE;
    }

    nsres = nsIDOMRange_ComparePoint(This->nsrange, ref_node, ref_offset, &cmp);
    assert(nsres == NS_OK);

    if(set_start) {
        if(cmp <= 0) {
            nsres = nsIDOMRange_SetStart(This->nsrange, ref_node, ref_offset);
        }else {
            nsres = nsIDOMRange_Collapse(This->nsrange, FALSE);
            assert(nsres == NS_OK);

            nsres = nsIDOMRange_SetEnd(This->nsrange, ref_node, ref_offset);
        }
    }else {
        if(cmp >= 0) {
            nsres = nsIDOMRange_SetEnd(This->nsrange, ref_node, ref_offset);
        }else {
            nsres = nsIDOMRange_Collapse(This->nsrange, TRUE);
            assert(nsres == NS_OK);

            nsres = nsIDOMRange_SetStart(This->nsrange, ref_node, ref_offset);
        }
    }
    assert(nsres == NS_OK);

    nsIDOMNode_Release(ref_node);
    return S_OK;
1468 1469 1470
}

static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
1471
        IHTMLTxtRange *SourceRange, LONG *ret)
1472
{
1473
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1474
    HTMLTxtRange *src_range;
1475
    short nsret = 0;
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
    int nscmpt;
    nsresult nsres;

    TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);

    nscmpt = string_to_nscmptype(how);
    if(nscmpt == -1)
        return E_INVALIDARG;

    src_range = get_range_object(This->doc, SourceRange);
    if(!src_range)
        return E_FAIL;

    nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
    if(NS_FAILED(nsres))
        ERR("CompareBoundaryPoints failed: %08x\n", nsres);

    *ret = nsret;
    return S_OK;
1495 1496 1497
}

static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
1498
        LONG count, LONG Flags, VARIANT_BOOL *Success)
1499
{
1500
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1501
    FIXME("(%p)->(%s %d %08x %p)\n", This, debugstr_w(String), count, Flags, Success);
1502 1503 1504
    return E_NOTIMPL;
}

1505
static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, LONG x, LONG y)
1506
{
1507
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1508
    FIXME("(%p)->(%d %d)\n", This, x, y);
1509 1510 1511 1512 1513
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
{
1514
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1515 1516 1517 1518 1519 1520 1521
    FIXME("(%p)->(%p)\n", This, Bookmark);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
        VARIANT_BOOL *Success)
{
1522
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1523 1524 1525 1526 1527 1528 1529
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT_BOOL *pfRet)
{
1530
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1531 1532 1533 1534 1535 1536 1537
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT_BOOL *pfRet)
{
1538
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1539 1540 1541 1542 1543 1544 1545
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT_BOOL *pfRet)
{
1546
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1547 1548 1549 1550 1551 1552 1553
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT_BOOL *pfRet)
{
1554
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1555 1556 1557 1558 1559 1560 1561
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
        BSTR *pcmdText)
{
1562
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1563 1564 1565 1566 1567 1568 1569
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT *pcmdValue)
{
1570
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1571 1572 1573 1574 1575 1576 1577
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
{
1578
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1579 1580 1581 1582 1583 1584 1585
    FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
    return E_NOTIMPL;
}

static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
        VARIANT_BOOL *pfRet)
{
1586
    HTMLTxtRange *This = impl_from_IHTMLTxtRange(iface);
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
    FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
    return E_NOTIMPL;
}

static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
    HTMLTxtRange_QueryInterface,
    HTMLTxtRange_AddRef,
    HTMLTxtRange_Release,
    HTMLTxtRange_GetTypeInfoCount,
    HTMLTxtRange_GetTypeInfo,
    HTMLTxtRange_GetIDsOfNames,
    HTMLTxtRange_Invoke,
    HTMLTxtRange_get_htmlText,
    HTMLTxtRange_put_text,
    HTMLTxtRange_get_text,
    HTMLTxtRange_parentElement,
    HTMLTxtRange_duplicate,
    HTMLTxtRange_inRange,
    HTMLTxtRange_isEqual,
    HTMLTxtRange_scrollIntoView,
    HTMLTxtRange_collapse,
    HTMLTxtRange_expand,
    HTMLTxtRange_move,
    HTMLTxtRange_moveStart,
    HTMLTxtRange_moveEnd,
    HTMLTxtRange_select,
    HTMLTxtRange_pasteHTML,
    HTMLTxtRange_moveToElementText,
    HTMLTxtRange_setEndPoint,
    HTMLTxtRange_compareEndPoints,
    HTMLTxtRange_findText,
    HTMLTxtRange_moveToPoint,
    HTMLTxtRange_getBookmark,
    HTMLTxtRange_moveToBookmark,
    HTMLTxtRange_queryCommandSupported,
    HTMLTxtRange_queryCommandEnabled,
    HTMLTxtRange_queryCommandState,
    HTMLTxtRange_queryCommandIndeterm,
    HTMLTxtRange_queryCommandText,
    HTMLTxtRange_queryCommandValue,
    HTMLTxtRange_execCommand,
    HTMLTxtRange_execCommandShowHelp
};

1631 1632 1633 1634
static inline HTMLTxtRange *impl_from_IOleCommandTarget(IOleCommandTarget *iface)
{
    return CONTAINING_RECORD(iface, HTMLTxtRange, IOleCommandTarget_iface);
}
1635 1636 1637

static HRESULT WINAPI RangeCommandTarget_QueryInterface(IOleCommandTarget *iface, REFIID riid, void **ppv)
{
1638
    HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1639
    return IHTMLTxtRange_QueryInterface(&This->IHTMLTxtRange_iface, riid, ppv);
1640 1641 1642 1643
}

static ULONG WINAPI RangeCommandTarget_AddRef(IOleCommandTarget *iface)
{
1644
    HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1645
    return IHTMLTxtRange_AddRef(&This->IHTMLTxtRange_iface);
1646 1647 1648 1649
}

static ULONG WINAPI RangeCommandTarget_Release(IOleCommandTarget *iface)
{
1650
    HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1651
    return IHTMLTxtRange_Release(&This->IHTMLTxtRange_iface);
1652 1653 1654 1655 1656
}

static HRESULT WINAPI RangeCommandTarget_QueryStatus(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
        ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText)
{
1657
    HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1658 1659 1660 1661
    FIXME("(%p)->(%s %d %p %p)\n", This, debugstr_guid(pguidCmdGroup), cCmds, prgCmds, pCmdText);
    return E_NOTIMPL;
}

1662 1663
static HRESULT exec_indent(HTMLTxtRange *This, VARIANT *in, VARIANT *out)
{
1664
    nsIDOMHTMLElement *blockquote_elem, *p_elem;
1665 1666 1667 1668 1669 1670 1671 1672
    nsIDOMDocumentFragment *fragment;
    nsIDOMNode *tmp;

    static const PRUnichar blockquoteW[] = {'B','L','O','C','K','Q','U','O','T','E',0};
    static const PRUnichar pW[] = {'P',0};

    TRACE("(%p)->(%p %p)\n", This, in, out);

1673
    if(!This->doc->nsdoc) {
1674 1675 1676
        WARN("NULL nsdoc\n");
        return E_NOTIMPL;
    }
1677

1678 1679
    create_nselem(This->doc, blockquoteW, &blockquote_elem);
    create_nselem(This->doc, pW, &p_elem);
1680 1681

    nsIDOMRange_ExtractContents(This->nsrange, &fragment);
1682
    nsIDOMHTMLElement_AppendChild(p_elem, (nsIDOMNode*)fragment, &tmp);
1683 1684 1685
    nsIDOMDocumentFragment_Release(fragment);
    nsIDOMNode_Release(tmp);

1686 1687
    nsIDOMHTMLElement_AppendChild(blockquote_elem, (nsIDOMNode*)p_elem, &tmp);
    nsIDOMHTMLElement_Release(p_elem);
1688 1689 1690
    nsIDOMNode_Release(tmp);

    nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)blockquote_elem);
1691
    nsIDOMHTMLElement_Release(blockquote_elem);
1692 1693 1694 1695

    return S_OK;
}

1696 1697 1698
static HRESULT WINAPI RangeCommandTarget_Exec(IOleCommandTarget *iface, const GUID *pguidCmdGroup,
        DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
{
1699
    HTMLTxtRange *This = impl_from_IOleCommandTarget(iface);
1700

1701 1702 1703 1704
    TRACE("(%p)->(%s %d %x %p %p)\n", This, debugstr_guid(pguidCmdGroup), nCmdID,
          nCmdexecopt, pvaIn, pvaOut);

    if(pguidCmdGroup && IsEqualGUID(&CGID_MSHTML, pguidCmdGroup)) {
1705 1706 1707 1708 1709 1710
        switch(nCmdID) {
        case IDM_INDENT:
            return exec_indent(This, pvaIn, pvaOut);
        default:
            FIXME("Unsupported cmdid %d of CGID_MSHTML\n", nCmdID);
        }
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    }else {
        FIXME("Unsupported cmd %d of group %s\n", nCmdID, debugstr_guid(pguidCmdGroup));
    }

    return E_NOTIMPL;
}

static const IOleCommandTargetVtbl OleCommandTargetVtbl = {
    RangeCommandTarget_QueryInterface,
    RangeCommandTarget_AddRef,
    RangeCommandTarget_Release,
    RangeCommandTarget_QueryStatus,
    RangeCommandTarget_Exec
};

1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
static const tid_t HTMLTxtRange_iface_tids[] = {
    IHTMLTxtRange_tid,
    0
};
static dispex_static_data_t HTMLTxtRange_dispex = {
    NULL,
    IHTMLTxtRange_tid,
    HTMLTxtRange_iface_tids
};

1736
HRESULT HTMLTxtRange_Create(HTMLDocumentNode *doc, nsIDOMRange *nsrange, IHTMLTxtRange **p)
1737
{
1738 1739 1740 1741 1742
    HTMLTxtRange *ret;

    ret = heap_alloc(sizeof(HTMLTxtRange));
    if(!ret)
        return E_OUTOFMEMORY;
1743

1744 1745
    init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLTxtRange_iface, &HTMLTxtRange_dispex);

1746
    ret->IHTMLTxtRange_iface.lpVtbl = &HTMLTxtRangeVtbl;
1747
    ret->IOleCommandTarget_iface.lpVtbl = &OleCommandTargetVtbl;
1748 1749
    ret->ref = 1;

1750 1751 1752
    if(nsrange)
        nsIDOMRange_AddRef(nsrange);
    ret->nsrange = nsrange;
1753

1754 1755 1756
    ret->doc = doc;
    list_add_head(&doc->range_list, &ret->entry);

1757
    *p = &ret->IHTMLTxtRange_iface;
1758
    return S_OK;
1759
}
1760

1761
void detach_ranges(HTMLDocumentNode *This)
1762 1763 1764 1765 1766 1767 1768
{
    HTMLTxtRange *iter;

    LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {
        iter->doc = NULL;
    }
}