text.c 22.6 KB
Newer Older
1 2 3 4
/*
 *    DOM text node implementation
 *
 * Copyright 2006 Huw Davies
5
 * Copyright 2007-2008 Alistair Leslie-Hughes
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26
 */

#define COBJMACROS

#include "config.h"

#include <stdarg.h>
27 28
#ifdef HAVE_LIBXML2
# include <libxml/parser.h>
29
# include <libxml/parserInternals.h>
30 31 32
# include <libxml/xmlerror.h>
#endif

33 34 35 36
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"
37
#include "msxml6.h"
38 39 40 41 42 43 44

#include "msxml_private.h"

#include "wine/debug.h"

#ifdef HAVE_LIBXML2

45 46
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

47 48
typedef struct _domtext
{
49
    xmlnode node;
50
    IXMLDOMText IXMLDOMText_iface;
51 52 53 54 55
    LONG ref;
} domtext;

static inline domtext *impl_from_IXMLDOMText( IXMLDOMText *iface )
{
56
    return CONTAINING_RECORD(iface, domtext, IXMLDOMText_iface);
57 58
}

59 60 61 62 63
static void domtext_reset_noenc(domtext *This)
{
    This->node.node->name = NULL;
}

64 65 66 67 68 69
static HRESULT WINAPI domtext_QueryInterface(
    IXMLDOMText *iface,
    REFIID riid,
    void** ppvObject )
{
    domtext *This = impl_from_IXMLDOMText( iface );
70
    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
71 72

    if ( IsEqualGUID( riid, &IID_IXMLDOMText ) ||
73
         IsEqualGUID( riid, &IID_IXMLDOMCharacterData) ||
74
         IsEqualGUID( riid, &IID_IXMLDOMNode ) ||
75
         IsEqualGUID( riid, &IID_IDispatch ) ||
76 77 78 79
         IsEqualGUID( riid, &IID_IUnknown ) )
    {
        *ppvObject = iface;
    }
80 81 82 83
    else if(node_query_interface(&This->node, riid, ppvObject))
    {
        return *ppvObject ? S_OK : E_NOINTERFACE;
    }
84 85
    else
    {
86
        TRACE("Unsupported interface %s\n", debugstr_guid(riid));
87
        *ppvObject = NULL;
88 89 90
        return E_NOINTERFACE;
    }

91
    IXMLDOMText_AddRef(iface);
92 93 94 95 96 97 98
    return S_OK;
}

static ULONG WINAPI domtext_AddRef(
    IXMLDOMText *iface )
{
    domtext *This = impl_from_IXMLDOMText( iface );
99 100 101
    ULONG ref = InterlockedIncrement( &This->ref );
    TRACE("(%p)->(%d)\n", This, ref);
    return ref;
102 103 104 105 106 107
}

static ULONG WINAPI domtext_Release(
    IXMLDOMText *iface )
{
    domtext *This = impl_from_IXMLDOMText( iface );
108
    ULONG ref = InterlockedDecrement( &This->ref );
109

110
    TRACE("(%p)->(%d)\n", This, ref);
111 112
    if ( ref == 0 )
    {
113
        destroy_xmlnode(&This->node);
114
        heap_free( This );
115 116 117 118 119 120 121 122 123
    }

    return ref;
}

static HRESULT WINAPI domtext_GetTypeInfoCount(
    IXMLDOMText *iface,
    UINT* pctinfo )
{
124
    domtext *This = impl_from_IXMLDOMText( iface );
125
    return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
126 127 128 129 130 131 132
}

static HRESULT WINAPI domtext_GetTypeInfo(
    IXMLDOMText *iface,
    UINT iTInfo, LCID lcid,
    ITypeInfo** ppTInfo )
{
133
    domtext *This = impl_from_IXMLDOMText( iface );
134 135
    return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface,
        iTInfo, lcid, ppTInfo);
136 137 138 139 140 141 142
}

static HRESULT WINAPI domtext_GetIDsOfNames(
    IXMLDOMText *iface,
    REFIID riid, LPOLESTR* rgszNames,
    UINT cNames, LCID lcid, DISPID* rgDispId )
{
143
    domtext *This = impl_from_IXMLDOMText( iface );
144 145
    return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface,
        riid, rgszNames, cNames, lcid, rgDispId);
146 147 148 149 150 151 152 153
}

static HRESULT WINAPI domtext_Invoke(
    IXMLDOMText *iface,
    DISPID dispIdMember, REFIID riid, LCID lcid,
    WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo, UINT* puArgErr )
{
154
    domtext *This = impl_from_IXMLDOMText( iface );
155 156
    return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface,
        dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
157 158 159 160 161 162 163
}

static HRESULT WINAPI domtext_get_nodeName(
    IXMLDOMText *iface,
    BSTR* p )
{
    domtext *This = impl_from_IXMLDOMText( iface );
164 165 166 167 168 169

    static const WCHAR textW[] = {'#','t','e','x','t',0};

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

    return return_bstr(textW, p);
170 171 172 173
}

static HRESULT WINAPI domtext_get_nodeValue(
    IXMLDOMText *iface,
174
    VARIANT* value )
175 176
{
    domtext *This = impl_from_IXMLDOMText( iface );
177 178 179

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

180
    return node_get_content(&This->node, value);
181 182 183 184
}

static HRESULT WINAPI domtext_put_nodeValue(
    IXMLDOMText *iface,
185
    VARIANT value)
186 187
{
    domtext *This = impl_from_IXMLDOMText( iface );
188

189
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
190

191
    domtext_reset_noenc(This);
192
    return node_put_value(&This->node, &value);
193 194 195 196 197 198 199
}

static HRESULT WINAPI domtext_get_nodeType(
    IXMLDOMText *iface,
    DOMNodeType* domNodeType )
{
    domtext *This = impl_from_IXMLDOMText( iface );
200 201 202 203 204

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

    *domNodeType = NODE_TEXT;
    return S_OK;
205 206 207 208 209 210 211
}

static HRESULT WINAPI domtext_get_parentNode(
    IXMLDOMText *iface,
    IXMLDOMNode** parent )
{
    domtext *This = impl_from_IXMLDOMText( iface );
212 213 214 215

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

    return node_get_parent(&This->node, parent);
216 217 218 219 220 221 222
}

static HRESULT WINAPI domtext_get_childNodes(
    IXMLDOMText *iface,
    IXMLDOMNodeList** outList)
{
    domtext *This = impl_from_IXMLDOMText( iface );
223 224 225 226

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

    return node_get_child_nodes(&This->node, outList);
227 228 229 230 231 232 233
}

static HRESULT WINAPI domtext_get_firstChild(
    IXMLDOMText *iface,
    IXMLDOMNode** domNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
234 235 236 237

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

    return return_null_node(domNode);
238 239 240 241 242 243 244
}

static HRESULT WINAPI domtext_get_lastChild(
    IXMLDOMText *iface,
    IXMLDOMNode** domNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
245 246 247 248

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

    return return_null_node(domNode);
249 250 251 252 253 254 255
}

static HRESULT WINAPI domtext_get_previousSibling(
    IXMLDOMText *iface,
    IXMLDOMNode** domNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
256 257 258 259

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

    return node_get_previous_sibling(&This->node, domNode);
260 261 262 263 264 265 266
}

static HRESULT WINAPI domtext_get_nextSibling(
    IXMLDOMText *iface,
    IXMLDOMNode** domNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
267 268 269 270

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

    return node_get_next_sibling(&This->node, domNode);
271 272 273 274 275 276 277
}

static HRESULT WINAPI domtext_get_attributes(
    IXMLDOMText *iface,
    IXMLDOMNamedNodeMap** attributeMap)
{
    domtext *This = impl_from_IXMLDOMText( iface );
278 279 280 281

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

    return return_null_ptr((void**)attributeMap);
282 283 284 285
}

static HRESULT WINAPI domtext_insertBefore(
    IXMLDOMText *iface,
286
    IXMLDOMNode* newNode, VARIANT refChild,
287 288 289
    IXMLDOMNode** outOldNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
290

291
    FIXME("(%p)->(%p %s %p) needs test\n", This, newNode, debugstr_variant(&refChild), outOldNode);
292 293

    return node_insert_before(&This->node, newNode, &refChild, outOldNode);
294 295 296 297 298 299 300 301 302
}

static HRESULT WINAPI domtext_replaceChild(
    IXMLDOMText *iface,
    IXMLDOMNode* newNode,
    IXMLDOMNode* oldNode,
    IXMLDOMNode** outOldNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
303 304 305 306

    FIXME("(%p)->(%p %p %p) needs test\n", This, newNode, oldNode, outOldNode);

    return node_replace_child(&This->node, newNode, oldNode, outOldNode);
307 308 309 310
}

static HRESULT WINAPI domtext_removeChild(
    IXMLDOMText *iface,
311
    IXMLDOMNode *child, IXMLDOMNode **oldChild)
312 313
{
    domtext *This = impl_from_IXMLDOMText( iface );
314 315
    TRACE("(%p)->(%p %p)\n", This, child, oldChild);
    return node_remove_child(&This->node, child, oldChild);
316 317 318 319
}

static HRESULT WINAPI domtext_appendChild(
    IXMLDOMText *iface,
320
    IXMLDOMNode *child, IXMLDOMNode **outChild)
321 322
{
    domtext *This = impl_from_IXMLDOMText( iface );
323 324
    TRACE("(%p)->(%p %p)\n", This, child, outChild);
    return node_append_child(&This->node, child, outChild);
325 326 327 328
}

static HRESULT WINAPI domtext_hasChildNodes(
    IXMLDOMText *iface,
329
    VARIANT_BOOL *ret)
330 331
{
    domtext *This = impl_from_IXMLDOMText( iface );
332
    TRACE("(%p)->(%p)\n", This, ret);
333
    return return_var_false(ret);
334 335 336 337
}

static HRESULT WINAPI domtext_get_ownerDocument(
    IXMLDOMText *iface,
338
    IXMLDOMDocument **doc)
339 340
{
    domtext *This = impl_from_IXMLDOMText( iface );
341 342
    TRACE("(%p)->(%p)\n", This, doc);
    return node_get_owner_doc(&This->node, doc);
343 344 345 346
}

static HRESULT WINAPI domtext_cloneNode(
    IXMLDOMText *iface,
347
    VARIANT_BOOL deep, IXMLDOMNode** outNode)
348 349
{
    domtext *This = impl_from_IXMLDOMText( iface );
350 351
    TRACE("(%p)->(%d %p)\n", This, deep, outNode);
    return node_clone( &This->node, deep, outNode );
352 353 354 355 356 357 358
}

static HRESULT WINAPI domtext_get_nodeTypeString(
    IXMLDOMText *iface,
    BSTR* p)
{
    domtext *This = impl_from_IXMLDOMText( iface );
359 360 361 362 363
    static const WCHAR textW[] = {'t','e','x','t',0};

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

    return return_bstr(textW, p);
364 365 366 367 368 369 370
}

static HRESULT WINAPI domtext_get_text(
    IXMLDOMText *iface,
    BSTR* p)
{
    domtext *This = impl_from_IXMLDOMText( iface );
371 372
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_text(&This->node, p);
373 374 375 376 377 378 379
}

static HRESULT WINAPI domtext_put_text(
    IXMLDOMText *iface,
    BSTR p)
{
    domtext *This = impl_from_IXMLDOMText( iface );
380
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
381
    domtext_reset_noenc(This);
382
    return node_put_text( &This->node, p );
383 384 385 386
}

static HRESULT WINAPI domtext_get_specified(
    IXMLDOMText *iface,
387
    VARIANT_BOOL* isSpecified)
388 389
{
    domtext *This = impl_from_IXMLDOMText( iface );
390 391 392
    FIXME("(%p)->(%p) stub!\n", This, isSpecified);
    *isSpecified = VARIANT_TRUE;
    return S_OK;
393 394 395 396
}

static HRESULT WINAPI domtext_get_definition(
    IXMLDOMText *iface,
397
    IXMLDOMNode** definitionNode)
398 399
{
    domtext *This = impl_from_IXMLDOMText( iface );
400 401
    FIXME("(%p)->(%p)\n", This, definitionNode);
    return E_NOTIMPL;
402 403 404 405 406 407 408
}

static HRESULT WINAPI domtext_get_nodeTypedValue(
    IXMLDOMText *iface,
    VARIANT* var1)
{
    domtext *This = impl_from_IXMLDOMText( iface );
409 410 411 412 413 414 415 416
    IXMLDOMNode* parent = NULL;
    HRESULT hr;

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

    if (!var1)
        return E_INVALIDARG;

417
    hr = IXMLDOMText_get_parentNode(iface, &parent);
418 419 420 421 422 423 424 425 426 427 428 429 430 431

    if (hr == S_OK)
    {
        hr = IXMLDOMNode_get_nodeTypedValue(parent, var1);
        IXMLDOMNode_Release(parent);
    }
    else
    {
        V_VT(var1) = VT_NULL;
        V_BSTR(var1) = NULL;
        hr = S_FALSE;
    }

    return hr;
432 433 434 435
}

static HRESULT WINAPI domtext_put_nodeTypedValue(
    IXMLDOMText *iface,
436
    VARIANT value)
437 438
{
    domtext *This = impl_from_IXMLDOMText( iface );
439 440 441
    IXMLDOMNode* parent = NULL;
    HRESULT hr;

442
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
443

444
    hr = IXMLDOMText_get_parentNode(iface, &parent);
445 446 447

    if (hr == S_OK)
    {
448
        hr = IXMLDOMNode_put_nodeTypedValue(parent, value);
449 450 451 452 453 454 455 456
        IXMLDOMNode_Release(parent);
    }
    else
    {
        hr = S_FALSE;
    }

    return hr;
457 458 459 460
}

static HRESULT WINAPI domtext_get_dataType(
    IXMLDOMText *iface,
461
    VARIANT* dtName)
462 463
{
    domtext *This = impl_from_IXMLDOMText( iface );
464 465 466 467 468 469 470 471
    IXMLDOMNode* parent = NULL;
    HRESULT hr;

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

    if (!dtName)
        return E_INVALIDARG;

472
    hr = IXMLDOMText_get_parentNode(iface, &parent);
473 474 475 476 477 478 479 480 481 482 483 484 485 486

    if (hr == S_OK)
    {
        hr = IXMLDOMNode_get_dataType(parent, dtName);
        IXMLDOMNode_Release(parent);
    }
    else
    {
        V_VT(dtName) = VT_NULL;
        V_BSTR(dtName) = NULL;
        hr = S_FALSE;
    }

    return hr;
487 488 489 490
}

static HRESULT WINAPI domtext_put_dataType(
    IXMLDOMText *iface,
491
    BSTR dtName)
492 493
{
    domtext *This = impl_from_IXMLDOMText( iface );
494 495 496 497 498 499 500 501
    IXMLDOMNode* parent = NULL;
    HRESULT hr;

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

    if (!dtName)
        return E_INVALIDARG;

502
    hr = IXMLDOMText_get_parentNode(iface, &parent);
503 504 505 506 507 508 509 510 511 512 513 514

    if (hr == S_OK)
    {
        hr = IXMLDOMNode_put_dataType(parent, dtName);
        IXMLDOMNode_Release(parent);
    }
    else
    {
        hr = S_FALSE;
    }

    return hr;
515 516 517 518 519 520 521
}

static HRESULT WINAPI domtext_get_xml(
    IXMLDOMText *iface,
    BSTR* p)
{
    domtext *This = impl_from_IXMLDOMText( iface );
522 523 524

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

525
    return node_get_xml(&This->node, TRUE, p);
526 527 528 529
}

static HRESULT WINAPI domtext_transformNode(
    IXMLDOMText *iface,
530
    IXMLDOMNode *node, BSTR *p)
531 532
{
    domtext *This = impl_from_IXMLDOMText( iface );
533 534
    TRACE("(%p)->(%p %p)\n", This, node, p);
    return node_transform_node(&This->node, node, p);
535 536 537 538 539 540 541
}

static HRESULT WINAPI domtext_selectNodes(
    IXMLDOMText *iface,
    BSTR p, IXMLDOMNodeList** outList)
{
    domtext *This = impl_from_IXMLDOMText( iface );
542 543
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outList);
    return node_select_nodes(&This->node, p, outList);
544 545 546 547 548 549 550
}

static HRESULT WINAPI domtext_selectSingleNode(
    IXMLDOMText *iface,
    BSTR p, IXMLDOMNode** outNode)
{
    domtext *This = impl_from_IXMLDOMText( iface );
551 552
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outNode);
    return node_select_singlenode(&This->node, p, outNode);
553 554 555 556
}

static HRESULT WINAPI domtext_get_parsed(
    IXMLDOMText *iface,
557
    VARIANT_BOOL* isParsed)
558 559
{
    domtext *This = impl_from_IXMLDOMText( iface );
560 561 562
    FIXME("(%p)->(%p) stub!\n", This, isParsed);
    *isParsed = VARIANT_TRUE;
    return S_OK;
563 564 565 566 567 568 569
}

static HRESULT WINAPI domtext_get_namespaceURI(
    IXMLDOMText *iface,
    BSTR* p)
{
    domtext *This = impl_from_IXMLDOMText( iface );
570 571
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_namespaceURI(&This->node, p);
572 573 574 575
}

static HRESULT WINAPI domtext_get_prefix(
    IXMLDOMText *iface,
576
    BSTR* prefix)
577 578
{
    domtext *This = impl_from_IXMLDOMText( iface );
579 580
    TRACE("(%p)->(%p)\n", This, prefix);
    return return_null_bstr( prefix );
581 582 583 584
}

static HRESULT WINAPI domtext_get_baseName(
    IXMLDOMText *iface,
585
    BSTR* name)
586 587
{
    domtext *This = impl_from_IXMLDOMText( iface );
588 589
    TRACE("(%p)->(%p)\n", This, name);
    return return_null_bstr( name );
590 591 592 593 594 595 596
}

static HRESULT WINAPI domtext_transformNodeToObject(
    IXMLDOMText *iface,
    IXMLDOMNode* domNode, VARIANT var1)
{
    domtext *This = impl_from_IXMLDOMText( iface );
597 598
    FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
    return E_NOTIMPL;
599 600 601 602 603 604
}

static HRESULT WINAPI domtext_get_data(
    IXMLDOMText *iface,
    BSTR *p)
{
605
    domtext *This = impl_from_IXMLDOMText( iface );
606 607 608 609

    if(!p)
        return E_INVALIDARG;

610 611
    *p = bstr_from_xmlChar(This->node.node->content);
    return S_OK;
612 613 614 615 616 617
}

static HRESULT WINAPI domtext_put_data(
    IXMLDOMText *iface,
    BSTR data)
{
618
    domtext *This = impl_from_IXMLDOMText( iface );
619 620 621
    BSTR normalized_data = NULL;
    HRESULT hr;
    size_t i, j;
622

623
    TRACE("(%p)->(%s)\n", This, debugstr_w(data));
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    if (data)
    {
        /* normalize line endings */
        normalized_data = SysAllocStringLen(NULL, SysStringLen(data));
        if (!normalized_data) return E_OUTOFMEMORY;
        for (i = 0, j = 0; data[i]; i++)
        {
            if (data[i] == '\r')
            {
                if (data[i + 1] == '\n') i++; /* change \r\n to just \n */
                normalized_data[j++] = '\n'; /* change \r by itself to \n */
            }
            else
                normalized_data[j++] = data[i];
        }
        normalized_data[j] = 0;
    }

    domtext_reset_noenc(This);
    hr = node_set_content(&This->node, normalized_data);

    SysFreeString(normalized_data);
    return hr;
648 649 650 651
}

static HRESULT WINAPI domtext_get_length(
    IXMLDOMText *iface,
652
    LONG *len)
653
{
654
    domtext *This = impl_from_IXMLDOMText( iface );
655 656
    HRESULT hr;
    BSTR data;
657

658
    TRACE("(%p)->(%p)\n", This, len);
659 660 661 662

    if(!len)
        return E_INVALIDARG;

663 664
    hr = IXMLDOMText_get_data(iface, &data);
    if(hr == S_OK)
665
    {
666 667
        *len = SysStringLen(data);
        SysFreeString(data);
668 669
    }

670
    return hr;
671 672 673 674
}

static HRESULT WINAPI domtext_substringData(
    IXMLDOMText *iface,
675
    LONG offset, LONG count, BSTR *p)
676
{
677
    domtext *This = impl_from_IXMLDOMText( iface );
678 679
    HRESULT hr;
    BSTR data;
680

681
    TRACE("(%p)->(%d %d %p)\n", This, offset, count, p);
682 683 684 685 686 687 688 689 690

    if(!p)
        return E_INVALIDARG;

    *p = NULL;
    if(offset < 0 || count < 0)
        return E_INVALIDARG;

    if(count == 0)
691
        return S_FALSE;
692

693 694
    hr = IXMLDOMText_get_data(iface, &data);
    if(hr == S_OK)
695
    {
696
        LONG len = SysStringLen(data);
697

698
        if(offset < len)
699
        {
700 701
            if(offset + count > len)
                *p = SysAllocString(&data[offset]);
702
            else
703
                *p = SysAllocStringLen(&data[offset], count);
704
        }
705 706
        else
            hr = S_FALSE;
707

708
        SysFreeString(data);
709 710 711
    }

    return hr;
712 713 714 715 716 717
}

static HRESULT WINAPI domtext_appendData(
    IXMLDOMText *iface,
    BSTR p)
{
718
    domtext *This = impl_from_IXMLDOMText( iface );
719 720 721
    HRESULT hr;
    BSTR data;
    LONG p_len;
722

723
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
724 725

    /* Nothing to do if NULL or an Empty string passed in. */
726
    if((p_len = SysStringLen(p)) == 0) return S_OK;
727

728 729
    hr = IXMLDOMText_get_data(iface, &data);
    if(hr == S_OK)
730
    {
731 732 733 734 735 736 737 738 739 740 741
        LONG len = SysStringLen(data);
        BSTR str = SysAllocStringLen(NULL, p_len + len);

        memcpy(str, data, len*sizeof(WCHAR));
        memcpy(&str[len], p, p_len*sizeof(WCHAR));
        str[len+p_len] = 0;

        hr = IXMLDOMText_put_data(iface, str);

        SysFreeString(str);
        SysFreeString(data);
742 743 744
    }

    return hr;
745 746 747 748
}

static HRESULT WINAPI domtext_insertData(
    IXMLDOMText *iface,
749
    LONG offset, BSTR p)
750
{
751
    domtext *This = impl_from_IXMLDOMText( iface );
752 753 754
    HRESULT hr;
    BSTR data;
    LONG p_len;
755

756
    TRACE("(%p)->(%d %s)\n", This, offset, debugstr_w(p));
757

758
    /* If have a NULL or empty string, don't do anything. */
759
    if((p_len = SysStringLen(p)) == 0)
760 761 762 763 764 765 766
        return S_OK;

    if(offset < 0)
    {
        return E_INVALIDARG;
    }

767 768
    hr = IXMLDOMText_get_data(iface, &data);
    if(hr == S_OK)
769
    {
770 771
        LONG len = SysStringLen(data);
        BSTR str;
772

773
        if(len < offset)
774
        {
775
            SysFreeString(data);
776 777 778
            return E_INVALIDARG;
        }

779 780 781 782 783 784
        str = SysAllocStringLen(NULL, len + p_len);
        /* start part, supplied string and end part */
        memcpy(str, data, offset*sizeof(WCHAR));
        memcpy(&str[offset], p, p_len*sizeof(WCHAR));
        memcpy(&str[offset+p_len], &data[offset], (len-offset)*sizeof(WCHAR));
        str[len+p_len] = 0;
785

786
        hr = IXMLDOMText_put_data(iface, str);
787

788 789
        SysFreeString(str);
        SysFreeString(data);
790 791 792
    }

    return hr;
793 794 795 796
}

static HRESULT WINAPI domtext_deleteData(
    IXMLDOMText *iface,
797
    LONG offset, LONG count)
798
{
799 800 801 802
    HRESULT hr;
    LONG len = -1;
    BSTR str;

803
    TRACE("(%p)->(%d %d)\n", iface, offset, count);
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

    hr = IXMLDOMText_get_length(iface, &len);
    if(hr != S_OK) return hr;

    if((offset < 0) || (offset > len) || (count < 0))
        return E_INVALIDARG;

    if(len == 0) return S_OK;

    /* cutting start or end */
    if((offset == 0) || ((count + offset) >= len))
    {
        if(offset == 0)
            IXMLDOMText_substringData(iface, count, len - count, &str);
        else
            IXMLDOMText_substringData(iface, 0, offset, &str);
        hr = IXMLDOMText_put_data(iface, str);
    }
    else
    /* cutting from the inside */
    {
        BSTR str_end;

        IXMLDOMText_substringData(iface, 0, offset, &str);
        IXMLDOMText_substringData(iface, offset + count, len - count, &str_end);

        hr = IXMLDOMText_put_data(iface, str);
        if(hr == S_OK)
            hr = IXMLDOMText_appendData(iface, str_end);

        SysFreeString(str_end);
    }

    SysFreeString(str);

    return hr;
840 841 842 843
}

static HRESULT WINAPI domtext_replaceData(
    IXMLDOMText *iface,
844
    LONG offset, LONG count, BSTR p)
845
{
846 847 848
    domtext *This = impl_from_IXMLDOMText( iface );
    HRESULT hr;

849
    TRACE("(%p)->(%d %d %s)\n", This, offset, count, debugstr_w(p));
850 851 852 853 854 855 856

    hr = IXMLDOMText_deleteData(iface, offset, count);

    if (hr == S_OK)
       hr = IXMLDOMText_insertData(iface, offset, p);

    return hr;
857 858 859 860
}

static HRESULT WINAPI domtext_splitText(
    IXMLDOMText *iface,
861
    LONG offset, IXMLDOMText **txtNode)
862
{
863
    domtext *This = impl_from_IXMLDOMText( iface );
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    LONG length = 0;

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

    if (!txtNode || offset < 0) return E_INVALIDARG;

    *txtNode = NULL;

    IXMLDOMText_get_length(iface, &length);

    if (offset > length) return E_INVALIDARG;
    if (offset == length) return S_FALSE;

    FIXME("adjacent text nodes are not supported\n");

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    return E_NOTIMPL;
}

static const struct IXMLDOMTextVtbl domtext_vtbl =
{
    domtext_QueryInterface,
    domtext_AddRef,
    domtext_Release,
    domtext_GetTypeInfoCount,
    domtext_GetTypeInfo,
    domtext_GetIDsOfNames,
    domtext_Invoke,
    domtext_get_nodeName,
    domtext_get_nodeValue,
    domtext_put_nodeValue,
    domtext_get_nodeType,
    domtext_get_parentNode,
    domtext_get_childNodes,
    domtext_get_firstChild,
    domtext_get_lastChild,
    domtext_get_previousSibling,
    domtext_get_nextSibling,
    domtext_get_attributes,
    domtext_insertBefore,
    domtext_replaceChild,
    domtext_removeChild,
    domtext_appendChild,
    domtext_hasChildNodes,
    domtext_get_ownerDocument,
    domtext_cloneNode,
    domtext_get_nodeTypeString,
    domtext_get_text,
    domtext_put_text,
    domtext_get_specified,
    domtext_get_definition,
    domtext_get_nodeTypedValue,
    domtext_put_nodeTypedValue,
    domtext_get_dataType,
    domtext_put_dataType,
    domtext_get_xml,
    domtext_transformNode,
    domtext_selectNodes,
    domtext_selectSingleNode,
    domtext_get_parsed,
    domtext_get_namespaceURI,
    domtext_get_prefix,
    domtext_get_baseName,
    domtext_transformNodeToObject,
    domtext_get_data,
    domtext_put_data,
    domtext_get_length,
    domtext_substringData,
    domtext_appendData,
    domtext_insertData,
    domtext_deleteData,
    domtext_replaceData,
    domtext_splitText
};

938 939 940 941 942 943 944 945 946 947 948 949
static const tid_t domtext_iface_tids[] = {
    IXMLDOMText_tid,
    0
};

static dispex_static_data_t domtext_dispex = {
    NULL,
    IXMLDOMText_tid,
    NULL,
    domtext_iface_tids
};

950 951 952 953
IUnknown* create_text( xmlNodePtr text )
{
    domtext *This;

954
    This = heap_alloc( sizeof *This );
955 956 957
    if ( !This )
        return NULL;

958
    This->IXMLDOMText_iface.lpVtbl = &domtext_vtbl;
959 960
    This->ref = 1;

961
    init_xmlnode(&This->node, text, (IXMLDOMNode*)&This->IXMLDOMText_iface, &domtext_dispex);
962

963
    return (IUnknown*)&This->IXMLDOMText_iface;
964 965 966
}

#endif