cdata.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *    DOM CDATA node implementation
 *
 * Copyright 2007 Alistair Leslie-Hughes
 *
 * 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
 */

#define COBJMACROS

#include "config.h"

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

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

#include "msxml_private.h"

#include "wine/debug.h"

#ifdef HAVE_LIBXML2

43 44
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

45
typedef struct
46
{
47
    xmlnode node;
48
    IXMLDOMCDATASection IXMLDOMCDATASection_iface;
49 50 51
    LONG ref;
} domcdata;

52 53 54
static const tid_t domcdata_se_tids[] = {
    IXMLDOMNode_tid,
    IXMLDOMCDATASection_tid,
55
    NULL_tid
56 57
};

58 59
static inline domcdata *impl_from_IXMLDOMCDATASection( IXMLDOMCDATASection *iface )
{
60
    return CONTAINING_RECORD(iface, domcdata, IXMLDOMCDATASection_iface);
61 62 63 64 65 66 67 68
}

static HRESULT WINAPI domcdata_QueryInterface(
    IXMLDOMCDATASection *iface,
    REFIID riid,
    void** ppvObject )
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
69
    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
70 71

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

94
    IXMLDOMCDATASection_AddRef(iface);
95 96 97 98 99 100 101
    return S_OK;
}

static ULONG WINAPI domcdata_AddRef(
    IXMLDOMCDATASection *iface )
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
102 103 104
    ULONG ref = InterlockedIncrement( &This->ref );
    TRACE("(%p)->(%d)\n", This, ref);
    return ref;
105 106 107 108 109 110
}

static ULONG WINAPI domcdata_Release(
    IXMLDOMCDATASection *iface )
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
111
    ULONG ref = InterlockedDecrement( &This->ref );
112

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

    return ref;
}

static HRESULT WINAPI domcdata_GetTypeInfoCount(
    IXMLDOMCDATASection *iface,
    UINT* pctinfo )
{
127
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
128
    return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
129 130 131 132 133 134 135
}

static HRESULT WINAPI domcdata_GetTypeInfo(
    IXMLDOMCDATASection *iface,
    UINT iTInfo, LCID lcid,
    ITypeInfo** ppTInfo )
{
136
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
137 138
    return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface,
        iTInfo, lcid, ppTInfo);
139 140 141 142 143 144 145
}

static HRESULT WINAPI domcdata_GetIDsOfNames(
    IXMLDOMCDATASection *iface,
    REFIID riid, LPOLESTR* rgszNames,
    UINT cNames, LCID lcid, DISPID* rgDispId )
{
146
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
147 148
    return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface,
        riid, rgszNames, cNames, lcid, rgDispId);
149 150 151 152 153 154 155 156
}

static HRESULT WINAPI domcdata_Invoke(
    IXMLDOMCDATASection *iface,
    DISPID dispIdMember, REFIID riid, LCID lcid,
    WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo, UINT* puArgErr )
{
157
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
158 159
    return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface,
        dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
160 161 162 163 164 165 166
}

static HRESULT WINAPI domcdata_get_nodeName(
    IXMLDOMCDATASection *iface,
    BSTR* p )
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
167 168 169 170 171 172 173

    static const WCHAR cdata_sectionW[] =
        {'#','c','d','a','t','a','-','s','e','c','t','i','o','n',0};

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

    return return_bstr(cdata_sectionW, p);
174 175 176 177
}

static HRESULT WINAPI domcdata_get_nodeValue(
    IXMLDOMCDATASection *iface,
178
    VARIANT* value)
179 180
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
181 182 183 184

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

    return node_get_content(&This->node, value);
185 186 187 188
}

static HRESULT WINAPI domcdata_put_nodeValue(
    IXMLDOMCDATASection *iface,
189
    VARIANT value)
190 191
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
192

193
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
194 195

    return node_put_value(&This->node, &value);
196 197 198 199 200 201 202
}

static HRESULT WINAPI domcdata_get_nodeType(
    IXMLDOMCDATASection *iface,
    DOMNodeType* domNodeType )
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
203 204 205 206 207

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

    *domNodeType = NODE_CDATA_SECTION;
    return S_OK;
208 209 210 211 212 213 214
}

static HRESULT WINAPI domcdata_get_parentNode(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode** parent )
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
215 216 217 218

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

    return node_get_parent(&This->node, parent);
219 220 221 222 223 224 225
}

static HRESULT WINAPI domcdata_get_childNodes(
    IXMLDOMCDATASection *iface,
    IXMLDOMNodeList** outList)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
226 227 228 229

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

    return node_get_child_nodes(&This->node, outList);
230 231 232 233 234 235 236
}

static HRESULT WINAPI domcdata_get_firstChild(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode** domNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
237 238 239 240

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

    return return_null_node(domNode);
241 242 243 244 245 246 247
}

static HRESULT WINAPI domcdata_get_lastChild(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode** domNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
248 249 250 251

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

    return return_null_node(domNode);
252 253 254 255 256 257 258
}

static HRESULT WINAPI domcdata_get_previousSibling(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode** domNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
259 260 261 262

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

    return node_get_previous_sibling(&This->node, domNode);
263 264 265 266 267 268 269
}

static HRESULT WINAPI domcdata_get_nextSibling(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode** domNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
270 271 272 273

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

    return node_get_next_sibling(&This->node, domNode);
274 275 276 277 278 279
}

static HRESULT WINAPI domcdata_get_attributes(
    IXMLDOMCDATASection *iface,
    IXMLDOMNamedNodeMap** attributeMap)
{
280 281 282 283 284
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );

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

    return return_null_ptr((void**)attributeMap);
285 286 287 288
}

static HRESULT WINAPI domcdata_insertBefore(
    IXMLDOMCDATASection *iface,
289
    IXMLDOMNode* newNode, VARIANT refChild,
290 291 292
    IXMLDOMNode** outOldNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
293 294 295
    TRACE("(%p)->(%p %s %p)\n", This, newNode, debugstr_variant(&refChild), outOldNode);
    if (outOldNode) *outOldNode = NULL;
    return E_FAIL;
296 297 298 299 300 301 302 303 304
}

static HRESULT WINAPI domcdata_replaceChild(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode* newNode,
    IXMLDOMNode* oldNode,
    IXMLDOMNode** outOldNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
305 306 307
    TRACE("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
    if (outOldNode) *outOldNode = NULL;
    return E_FAIL;
308 309 310 311
}

static HRESULT WINAPI domcdata_removeChild(
    IXMLDOMCDATASection *iface,
312
    IXMLDOMNode *child, IXMLDOMNode **oldChild)
313 314
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
315
    TRACE("(%p)->(%p %p)\n", This, child, oldChild);
316 317
    if (oldChild) *oldChild = NULL;
    return E_FAIL;
318 319 320 321
}

static HRESULT WINAPI domcdata_appendChild(
    IXMLDOMCDATASection *iface,
322
    IXMLDOMNode *child, IXMLDOMNode **outChild)
323 324
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
325
    TRACE("(%p)->(%p %p)\n", This, child, outChild);
326 327
    if (outChild) *outChild = NULL;
    return E_FAIL;
328 329 330 331
}

static HRESULT WINAPI domcdata_hasChildNodes(
    IXMLDOMCDATASection *iface,
332
    VARIANT_BOOL *ret)
333 334
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
335
    TRACE("(%p)->(%p)\n", This, ret);
336
    return return_var_false(ret);
337 338 339 340
}

static HRESULT WINAPI domcdata_get_ownerDocument(
    IXMLDOMCDATASection *iface,
341
    IXMLDOMDocument    **doc)
342 343
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
344 345
    TRACE("(%p)->(%p)\n", This, doc);
    return node_get_owner_doc(&This->node, doc);
346 347 348 349
}

static HRESULT WINAPI domcdata_cloneNode(
    IXMLDOMCDATASection *iface,
350
    VARIANT_BOOL deep, IXMLDOMNode** outNode)
351 352
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
353 354
    TRACE("(%p)->(%d %p)\n", This, deep, outNode);
    return node_clone( &This->node, deep, outNode );
355 356 357 358 359 360 361
}

static HRESULT WINAPI domcdata_get_nodeTypeString(
    IXMLDOMCDATASection *iface,
    BSTR* p)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
362 363 364 365 366
    static const WCHAR cdatasectionW[] = {'c','d','a','t','a','s','e','c','t','i','o','n',0};

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

    return return_bstr(cdatasectionW, p);
367 368 369 370 371 372 373
}

static HRESULT WINAPI domcdata_get_text(
    IXMLDOMCDATASection *iface,
    BSTR* p)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
374 375
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_text(&This->node, p);
376 377 378 379 380 381 382
}

static HRESULT WINAPI domcdata_put_text(
    IXMLDOMCDATASection *iface,
    BSTR p)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
383
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
384
    return node_put_text( &This->node, p );
385 386 387 388
}

static HRESULT WINAPI domcdata_get_specified(
    IXMLDOMCDATASection *iface,
389
    VARIANT_BOOL* isSpecified)
390 391
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
392 393 394
    FIXME("(%p)->(%p) stub!\n", This, isSpecified);
    *isSpecified = VARIANT_TRUE;
    return S_OK;
395 396 397 398
}

static HRESULT WINAPI domcdata_get_definition(
    IXMLDOMCDATASection *iface,
399
    IXMLDOMNode** definitionNode)
400 401
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
402 403
    FIXME("(%p)->(%p)\n", This, definitionNode);
    return E_NOTIMPL;
404 405 406 407
}

static HRESULT WINAPI domcdata_get_nodeTypedValue(
    IXMLDOMCDATASection *iface,
408
    VARIANT* v)
409 410
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
411 412
    TRACE("(%p)->(%p)\n", This, v);
    return node_get_content(&This->node, v);
413 414 415 416
}

static HRESULT WINAPI domcdata_put_nodeTypedValue(
    IXMLDOMCDATASection *iface,
417
    VARIANT typedValue)
418 419
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
420 421
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
    return E_NOTIMPL;
422 423 424 425
}

static HRESULT WINAPI domcdata_get_dataType(
    IXMLDOMCDATASection *iface,
426
    VARIANT* typename)
427 428
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
429 430
    TRACE("(%p)->(%p)\n", This, typename);
    return return_null_var( typename );
431 432 433 434 435 436 437
}

static HRESULT WINAPI domcdata_put_dataType(
    IXMLDOMCDATASection *iface,
    BSTR p)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
438

439
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
440 441 442 443 444

    if(!p)
        return E_INVALIDARG;

    return E_FAIL;
445 446 447 448 449 450 451
}

static HRESULT WINAPI domcdata_get_xml(
    IXMLDOMCDATASection *iface,
    BSTR* p)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
452 453 454

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

455
    return node_get_xml(&This->node, FALSE, p);
456 457 458 459
}

static HRESULT WINAPI domcdata_transformNode(
    IXMLDOMCDATASection *iface,
460
    IXMLDOMNode *node, BSTR *p)
461 462
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
463 464
    TRACE("(%p)->(%p %p)\n", This, node, p);
    return node_transform_node(&This->node, node, p);
465 466 467 468 469 470 471
}

static HRESULT WINAPI domcdata_selectNodes(
    IXMLDOMCDATASection *iface,
    BSTR p, IXMLDOMNodeList** outList)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
472 473
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outList);
    return node_select_nodes(&This->node, p, outList);
474 475 476 477 478 479 480
}

static HRESULT WINAPI domcdata_selectSingleNode(
    IXMLDOMCDATASection *iface,
    BSTR p, IXMLDOMNode** outNode)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
481 482
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outNode);
    return node_select_singlenode(&This->node, p, outNode);
483 484 485 486
}

static HRESULT WINAPI domcdata_get_parsed(
    IXMLDOMCDATASection *iface,
487
    VARIANT_BOOL* isParsed)
488 489
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
490 491 492
    FIXME("(%p)->(%p) stub!\n", This, isParsed);
    *isParsed = VARIANT_TRUE;
    return S_OK;
493 494 495 496 497 498 499
}

static HRESULT WINAPI domcdata_get_namespaceURI(
    IXMLDOMCDATASection *iface,
    BSTR* p)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
500 501
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_namespaceURI(&This->node, p);
502 503 504 505
}

static HRESULT WINAPI domcdata_get_prefix(
    IXMLDOMCDATASection *iface,
506
    BSTR* prefix)
507 508
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
509 510
    TRACE("(%p)->(%p)\n", This, prefix);
    return return_null_bstr( prefix );
511 512 513 514
}

static HRESULT WINAPI domcdata_get_baseName(
    IXMLDOMCDATASection *iface,
515
    BSTR* name)
516 517
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
518 519
    FIXME("(%p)->(%p): needs test\n", This, name);
    return return_null_bstr( name );
520 521 522 523 524 525 526
}

static HRESULT WINAPI domcdata_transformNodeToObject(
    IXMLDOMCDATASection *iface,
    IXMLDOMNode* domNode, VARIANT var1)
{
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
527 528
    FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
    return E_NOTIMPL;
529 530 531 532 533 534
}

static HRESULT WINAPI domcdata_get_data(
    IXMLDOMCDATASection *iface,
    BSTR *p)
{
535
    HRESULT hr;
536 537 538 539 540
    VARIANT vRet;

    if(!p)
        return E_INVALIDARG;

541
    hr = IXMLDOMCDATASection_get_nodeValue( iface, &vRet );
542 543 544 545 546 547
    if(hr == S_OK)
    {
        *p = V_BSTR(&vRet);
    }

    return hr;
548 549 550 551 552 553
}

static HRESULT WINAPI domcdata_put_data(
    IXMLDOMCDATASection *iface,
    BSTR data)
{
554
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
555 556
    TRACE("(%p)->(%s)\n", This, debugstr_w(data));
    return node_set_content(&This->node, data);
557 558 559 560
}

static HRESULT WINAPI domcdata_get_length(
    IXMLDOMCDATASection *iface,
561
    LONG *len)
562
{
563
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
564 565
    HRESULT hr;
    BSTR data;
566

567
    TRACE("(%p)->(%p)\n", This, len);
568 569 570 571

    if(!len)
        return E_INVALIDARG;

572 573
    hr = IXMLDOMCDATASection_get_data(iface, &data);
    if(hr == S_OK)
574
    {
575 576
        *len = SysStringLen(data);
        SysFreeString(data);
577 578 579
    }

    return S_OK;
580 581 582 583
}

static HRESULT WINAPI domcdata_substringData(
    IXMLDOMCDATASection *iface,
584
    LONG offset, LONG count, BSTR *p)
585
{
586
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
587 588
    HRESULT hr;
    BSTR data;
589

590
    TRACE("(%p)->(%d %d %p)\n", This, offset, count, p);
591 592 593 594 595 596 597 598 599

    if(!p)
        return E_INVALIDARG;

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

    if(count == 0)
600
        return S_FALSE;
601

602 603
    hr = IXMLDOMCDATASection_get_data(iface, &data);
    if(hr == S_OK)
604
    {
605
        LONG len = SysStringLen(data);
606

607
        if(offset < len)
608
        {
609 610
            if(offset + count > len)
                *p = SysAllocString(&data[offset]);
611
            else
612
                *p = SysAllocStringLen(&data[offset], count);
613
        }
614 615
        else
            hr = S_FALSE;
616

617
        SysFreeString(data);
618 619 620
    }

    return hr;
621 622 623 624 625 626
}

static HRESULT WINAPI domcdata_appendData(
    IXMLDOMCDATASection *iface,
    BSTR p)
{
627
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
628 629 630
    HRESULT hr;
    BSTR data;
    LONG p_len;
631

632
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
633 634

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

637 638
    hr = IXMLDOMCDATASection_get_data(iface, &data);
    if(hr == S_OK)
639
    {
640 641 642 643 644 645 646 647 648 649 650
        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 = IXMLDOMCDATASection_put_data(iface, str);

        SysFreeString(str);
        SysFreeString(data);
651 652 653
    }

    return hr;
654 655 656 657
}

static HRESULT WINAPI domcdata_insertData(
    IXMLDOMCDATASection *iface,
658
    LONG offset, BSTR p)
659
{
660
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
661 662 663
    HRESULT hr;
    BSTR data;
    LONG p_len;
664

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

667
    /* If have a NULL or empty string, don't do anything. */
668
    if((p_len = SysStringLen(p)) == 0)
669 670 671 672 673 674 675
        return S_OK;

    if(offset < 0)
    {
        return E_INVALIDARG;
    }

676 677
    hr = IXMLDOMCDATASection_get_data(iface, &data);
    if(hr == S_OK)
678
    {
679 680
        LONG len = SysStringLen(data);
        BSTR str;
681

682
        if(len < offset)
683
        {
684
            SysFreeString(data);
685 686 687
            return E_INVALIDARG;
        }

688 689 690 691 692 693
        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;
694

695
        hr = IXMLDOMCDATASection_put_data(iface, str);
696

697 698
        SysFreeString(str);
        SysFreeString(data);
699 700 701
    }

    return hr;
702 703 704 705
}

static HRESULT WINAPI domcdata_deleteData(
    IXMLDOMCDATASection *iface,
706
    LONG offset, LONG count)
707
{
708
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
709 710 711 712
    HRESULT hr;
    LONG len = -1;
    BSTR str;

713
    TRACE("(%p)->(%d %d)\n", This, offset, count);
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 746 747 748 749

    hr = IXMLDOMCDATASection_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)
            IXMLDOMCDATASection_substringData(iface, count, len - count, &str);
        else
            IXMLDOMCDATASection_substringData(iface, 0, offset, &str);
        hr = IXMLDOMCDATASection_put_data(iface, str);
    }
    else
    /* cutting from the inside */
    {
        BSTR str_end;

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

        hr = IXMLDOMCDATASection_put_data(iface, str);
        if(hr == S_OK)
            hr = IXMLDOMCDATASection_appendData(iface, str_end);

        SysFreeString(str_end);
    }

    SysFreeString(str);

    return hr;
750 751 752 753
}

static HRESULT WINAPI domcdata_replaceData(
    IXMLDOMCDATASection *iface,
754
    LONG offset, LONG count, BSTR p)
755
{
756 757 758
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
    HRESULT hr;

759
    TRACE("(%p)->(%d %d %s)\n", This, offset, count, debugstr_w(p));
760 761 762 763 764 765 766

    hr = IXMLDOMCDATASection_deleteData(iface, offset, count);

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

    return hr;
767 768 769 770
}

static HRESULT WINAPI domcdata_splitText(
    IXMLDOMCDATASection *iface,
771
    LONG offset, IXMLDOMText **txtNode)
772
{
773
    domcdata *This = impl_from_IXMLDOMCDATASection( iface );
774 775 776 777 778 779 780 781 782
    IXMLDOMDocument *doc;
    LONG length = 0;
    HRESULT hr;

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

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

    *txtNode = NULL;
783

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    IXMLDOMCDATASection_get_length(iface, &length);

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

    hr = IXMLDOMCDATASection_get_ownerDocument(iface, &doc);
    if (hr == S_OK)
    {
        BSTR data;

        hr = IXMLDOMCDATASection_substringData(iface, offset, length - offset, &data);
        if (hr == S_OK)
        {
            hr = IXMLDOMDocument_createTextNode(doc, data, txtNode);
            if (hr == S_OK)
            {
                IXMLDOMNode *parent;

                hr = IXMLDOMCDATASection_get_parentNode(iface, &parent);
                if (hr == S_OK)
                {
                    IXMLDOMCDATASection_deleteData(iface, 0, offset);
                    hr = IXMLDOMNode_appendChild(parent, (IXMLDOMNode*)*txtNode, NULL);
807
                    IXMLDOMNode_Release(parent);
808 809 810 811 812 813 814 815 816
                }
            }
            SysFreeString(data);
        }
        IXMLDOMDocument_Release(doc);
    }

    return hr;
}
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

static const struct IXMLDOMCDATASectionVtbl domcdata_vtbl =
{
    domcdata_QueryInterface,
    domcdata_AddRef,
    domcdata_Release,
    domcdata_GetTypeInfoCount,
    domcdata_GetTypeInfo,
    domcdata_GetIDsOfNames,
    domcdata_Invoke,
    domcdata_get_nodeName,
    domcdata_get_nodeValue,
    domcdata_put_nodeValue,
    domcdata_get_nodeType,
    domcdata_get_parentNode,
    domcdata_get_childNodes,
    domcdata_get_firstChild,
    domcdata_get_lastChild,
    domcdata_get_previousSibling,
    domcdata_get_nextSibling,
    domcdata_get_attributes,
    domcdata_insertBefore,
    domcdata_replaceChild,
    domcdata_removeChild,
    domcdata_appendChild,
    domcdata_hasChildNodes,
    domcdata_get_ownerDocument,
    domcdata_cloneNode,
    domcdata_get_nodeTypeString,
    domcdata_get_text,
    domcdata_put_text,
    domcdata_get_specified,
    domcdata_get_definition,
    domcdata_get_nodeTypedValue,
    domcdata_put_nodeTypedValue,
    domcdata_get_dataType,
    domcdata_put_dataType,
    domcdata_get_xml,
    domcdata_transformNode,
    domcdata_selectNodes,
    domcdata_selectSingleNode,
    domcdata_get_parsed,
    domcdata_get_namespaceURI,
    domcdata_get_prefix,
    domcdata_get_baseName,
    domcdata_transformNodeToObject,
    domcdata_get_data,
    domcdata_put_data,
    domcdata_get_length,
    domcdata_substringData,
    domcdata_appendData,
    domcdata_insertData,
    domcdata_deleteData,
    domcdata_replaceData,
    domcdata_splitText
};

874 875 876 877 878 879 880 881 882 883 884 885
static const tid_t domcdata_iface_tids[] = {
    IXMLDOMCDATASection_tid,
    0
};

static dispex_static_data_t domcdata_dispex = {
    NULL,
    IXMLDOMCDATASection_tid,
    NULL,
    domcdata_iface_tids
};

886 887 888 889
IUnknown* create_cdata( xmlNodePtr text )
{
    domcdata *This;

890
    This = heap_alloc( sizeof *This );
891 892 893
    if ( !This )
        return NULL;

894
    This->IXMLDOMCDATASection_iface.lpVtbl = &domcdata_vtbl;
895 896
    This->ref = 1;

897
    init_xmlnode(&This->node, text, (IXMLDOMNode*)&This->IXMLDOMCDATASection_iface, &domcdata_dispex);
898

899
    return (IUnknown*)&This->IXMLDOMCDATASection_iface;
900 901 902
}

#endif