node.c 58.4 KB
Newer Older
1 2 3 4 5
/*
 *    Node implementation
 *
 * Copyright 2005 Mike McCormack
 *
6
 * This library is free software; you can redistribute it and/or
7 8 9 10
 * 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.
 *
11
 * This library is distributed in the hope that it will be useful,
12 13 14 15 16 17
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25
 */

#include "config.h"

#define COBJMACROS

#include <stdarg.h>
26 27 28

#ifdef HAVE_LIBXML2
# include <libxml/parser.h>
29
# include <libxml/parserInternals.h>
30 31 32 33 34 35 36 37 38
# include <libxml/xmlerror.h>
# include <libxml/HTMLtree.h>
# ifdef SONAME_LIBXSLT
#  ifdef HAVE_LIBXSLT_PATTERN_H
#   include <libxslt/pattern.h>
#  endif
#  ifdef HAVE_LIBXSLT_TRANSFORM_H
#   include <libxslt/transform.h>
#  endif
39
#  include <libxslt/imports.h>
40
#  include <libxslt/variables.h>
41 42
#  include <libxslt/xsltutils.h>
#  include <libxslt/xsltInternals.h>
43
#  include <libxslt/documents.h>
44 45 46
# endif
#endif

47 48 49 50 51
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winnls.h"
#include "ole2.h"
52
#include "msxml6.h"
53 54 55 56 57 58 59

#include "msxml_private.h"

#include "wine/debug.h"

#ifdef HAVE_LIBXML2

60 61
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

62 63 64 65
#ifdef SONAME_LIBXSLT
extern void* libxslt_handle;
# define MAKE_FUNCPTR(f) extern typeof(f) * p##f
MAKE_FUNCPTR(xsltApplyStylesheet);
66
MAKE_FUNCPTR(xsltApplyStylesheetUser);
67 68
MAKE_FUNCPTR(xsltCleanupGlobals);
MAKE_FUNCPTR(xsltFreeStylesheet);
69 70
MAKE_FUNCPTR(xsltFreeTransformContext);
MAKE_FUNCPTR(xsltNewTransformContext);
71
MAKE_FUNCPTR(xsltNextImport);
72
MAKE_FUNCPTR(xsltParseStylesheetDoc);
73
MAKE_FUNCPTR(xsltQuoteUserParams);
74
MAKE_FUNCPTR(xsltSaveResultTo);
75
# undef MAKE_FUNCPTR
76 77
#else
WINE_DECLARE_DEBUG_CHANNEL(winediag);
78 79
#endif

80 81
static const IID IID_xmlnode = {0x4f2f4ba2,0xb822,0x11df,{0x8b,0x8a,0x68,0x50,0xdf,0xd7,0x20,0x85}};

82
xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
83 84 85 86 87
{
    xmlnode *This;

    if ( !iface )
        return NULL;
88 89
    This = get_node_obj( iface );
    if ( !This || !This->node )
90
        return NULL;
91
    if ( type && This->node->type != type )
92
        return NULL;
93
    return This->node;
94 95
}

96 97
BOOL node_query_interface(xmlnode *This, REFIID riid, void **ppv)
{
98 99 100 101 102 103
    if(IsEqualGUID(&IID_xmlnode, riid)) {
        TRACE("(%p)->(IID_xmlnode %p)\n", This, ppv);
        *ppv = This;
        return TRUE;
    }

104
    return dispex_query_interface(&This->dispex, riid, ppv);
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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
/* common ISupportErrorInfo implementation */
typedef struct {
   ISupportErrorInfo ISupportErrorInfo_iface;
   LONG ref;

   const tid_t* iids;
} SupportErrorInfo;

static inline SupportErrorInfo *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
{
    return CONTAINING_RECORD(iface, SupportErrorInfo, ISupportErrorInfo_iface);
}

static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **obj)
{
    SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);

    *obj = NULL;

    if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISupportErrorInfo)) {
        *obj = iface;
        ISupportErrorInfo_AddRef(iface);
        return S_OK;
    }

    return E_NOINTERFACE;
}

static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
{
    SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
    ULONG ref = InterlockedIncrement(&This->ref);
    TRACE("(%p)->(%d)\n", This, ref );
    return ref;
}

static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
{
    SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if (ref == 0)
        heap_free(This);

    return ref;
}

static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
{
    SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
    enum tid_t const *tid;

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

    tid = This->iids;
165
    while (*tid != NULL_tid)
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
    {
        if (IsEqualGUID(riid, get_riid_from_tid(*tid)))
            return S_OK;
        tid++;
    }

    return S_FALSE;
}

static const struct ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
    SupportErrorInfo_QueryInterface,
    SupportErrorInfo_AddRef,
    SupportErrorInfo_Release,
    SupportErrorInfo_InterfaceSupportsErrorInfo
};

HRESULT node_create_supporterrorinfo(enum tid_t const *iids, void **obj)
{
    SupportErrorInfo *This;

    This = heap_alloc(sizeof(*This));
    if (!This) return E_OUTOFMEMORY;

    This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
    This->ref = 1;
    This->iids = iids;

    *obj = &This->ISupportErrorInfo_iface;

    return S_OK;
}

198 199
xmlnode *get_node_obj(IXMLDOMNode *node)
{
200
    xmlnode *obj = NULL;
201 202 203
    HRESULT hres;

    hres = IXMLDOMNode_QueryInterface(node, &IID_xmlnode, (void**)&obj);
204
    if (!obj) WARN("node is not our IXMLDOMNode implementation\n");
205 206 207
    return SUCCEEDED(hres) ? obj : NULL;
}

208
HRESULT node_get_nodeName(xmlnode *This, BSTR *name)
209
{
210 211 212
    BSTR prefix, base;
    HRESULT hr;

213 214 215
    if (!name)
        return E_INVALIDARG;

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    hr = node_get_base_name(This, &base);
    if (hr != S_OK) return hr;

    hr = node_get_prefix(This, &prefix);
    if (hr == S_OK)
    {
        static const WCHAR colW = ':';
        WCHAR *ptr;

        /* +1 for ':' */
        ptr = *name = SysAllocStringLen(NULL, SysStringLen(base) + SysStringLen(prefix) + 1);
        memcpy(ptr, prefix, SysStringByteLen(prefix));
        ptr += SysStringLen(prefix);
        memcpy(ptr++, &colW, sizeof(WCHAR));
        memcpy(ptr, base, SysStringByteLen(base));

        SysFreeString(base);
        SysFreeString(prefix);
    }
    else
        *name = base;
237 238

    return S_OK;
239 240
}

241
HRESULT node_get_content(xmlnode *This, VARIANT *value)
242
{
243
    xmlChar *content;
244

245 246 247
    if(!value)
        return E_INVALIDARG;

248 249 250 251
    content = xmlNodeGetContent(This->node);
    V_VT(value) = VT_BSTR;
    V_BSTR(value) = bstr_from_xmlChar( content );
    xmlFree(content);
252

253 254 255
    TRACE("%p returned %s\n", This, debugstr_w(V_BSTR(value)));
    return S_OK;
}
256

257 258 259 260
HRESULT node_set_content(xmlnode *This, LPCWSTR value)
{
    xmlChar *str;

261
    TRACE("(%p)->(%s)\n", This, debugstr_w(value));
262
    str = xmlchar_from_wchar(value);
263 264 265 266 267 268 269 270
    if(!str)
        return E_OUTOFMEMORY;

    xmlNodeSetContent(This->node, str);
    heap_free(str);
    return S_OK;
}

271 272 273 274
static HRESULT node_set_content_escaped(xmlnode *This, LPCWSTR value)
{
    xmlChar *str, *escaped;

275
    TRACE("(%p)->(%s)\n", This, debugstr_w(value));
276
    str = xmlchar_from_wchar(value);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    if(!str)
        return E_OUTOFMEMORY;

    escaped = xmlEncodeSpecialChars(NULL, str);
    if(!escaped)
    {
        heap_free(str);
        return E_OUTOFMEMORY;
    }

    xmlNodeSetContent(This->node, escaped);

    heap_free(str);
    xmlFree(escaped);

    return S_OK;
}

295
HRESULT node_put_value(xmlnode *This, VARIANT *value)
296
{
297
    HRESULT hr;
298

299 300 301 302 303 304 305 306 307 308
    if (V_VT(value) != VT_BSTR)
    {
        VARIANT string_value;

        VariantInit(&string_value);
        hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
        if(FAILED(hr)) {
            WARN("Couldn't convert to VT_BSTR\n");
            return hr;
        }
309

310 311 312 313 314
        hr = node_set_content(This, V_BSTR(&string_value));
        VariantClear(&string_value);
    }
    else
        hr = node_set_content(This, V_BSTR(value));
315

316
    return hr;
317
}
318

319 320 321 322
HRESULT node_put_value_escaped(xmlnode *This, VARIANT *value)
{
    HRESULT hr;

323 324 325 326 327 328 329 330 331 332
    if (V_VT(value) != VT_BSTR)
    {
       VARIANT string_value;

        VariantInit(&string_value);
        hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
        if(FAILED(hr)) {
            WARN("Couldn't convert to VT_BSTR\n");
            return hr;
        }
333

334 335 336 337 338
        hr = node_set_content_escaped(This, V_BSTR(&string_value));
        VariantClear(&string_value);
    }
    else
        hr = node_set_content_escaped(This, V_BSTR(value));
339

340
    return hr;
341 342
}

343 344 345 346 347 348
static HRESULT get_node(
    xmlnode *This,
    const char *name,
    xmlNodePtr node,
    IXMLDOMNode **out )
{
349
    TRACE("(%p)->(%s %p %p)\n", This, name, node, out );
350 351 352

    if ( !out )
        return E_INVALIDARG;
353

354
    /* if we don't have a doc, use our parent. */
355 356 357
    if(node && !node->doc && node->parent)
        node->doc = node->parent->doc;

358 359 360 361 362 363
    *out = create_node( node );
    if (!*out)
        return S_FALSE;
    return S_OK;
}

364 365 366 367 368
HRESULT node_get_parent(xmlnode *This, IXMLDOMNode **parent)
{
    return get_node( This, "parent", This->node->parent, parent );
}

369
HRESULT node_get_child_nodes(xmlnode *This, IXMLDOMNodeList **ret)
370
{
371
    if(!ret)
372
        return E_INVALIDARG;
373

374 375
    *ret = create_children_nodelist(This->node);
    if(!*ret)
376
        return E_OUTOFMEMORY;
377

378
    return S_OK;
379 380
}

381 382 383 384 385 386 387 388
HRESULT node_get_first_child(xmlnode *This, IXMLDOMNode **ret)
{
    return get_node(This, "firstChild", This->node->children, ret);
}

HRESULT node_get_last_child(xmlnode *This, IXMLDOMNode **ret)
{
    return get_node(This, "lastChild", This->node->last, ret);
389 390
}

391 392 393 394 395 396 397 398
HRESULT node_get_previous_sibling(xmlnode *This, IXMLDOMNode **ret)
{
    return get_node(This, "previous", This->node->prev, ret);
}

HRESULT node_get_next_sibling(xmlnode *This, IXMLDOMNode **ret)
{
    return get_node(This, "next", This->node->next, ret);
399 400
}

401 402
static int node_get_inst_cnt(xmlNodePtr node)
{
403
    int ret = *(LONG *)&node->_private & NODE_PRIV_REFCOUNT_MASK;
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    xmlNodePtr child;

    /* add attribute counts */
    if (node->type == XML_ELEMENT_NODE)
    {
        xmlAttrPtr prop = node->properties;

        while (prop)
        {
            ret += node_get_inst_cnt((xmlNodePtr)prop);
            prop = prop->next;
        }
    }

    /* add children counts */
    child = node->children;
    while (child)
    {
        ret += node_get_inst_cnt(child);
        child = child->next;
    }

    return ret;
}

429
int xmlnode_get_inst_cnt(xmlnode *node)
430 431 432 433
{
    return node_get_inst_cnt(node->node);
}

434
/* _private field holds a number of COM instances spawned from this libxml2 node
435
 * most significant bits are used to store information about ignorable whitespace nodes */
436
void xmlnode_add_ref(xmlNodePtr node)
437 438 439 440 441
{
    if (node->type == XML_DOCUMENT_NODE) return;
    InterlockedIncrement((LONG*)&node->_private);
}

442
void xmlnode_release(xmlNodePtr node)
443 444 445 446 447
{
    if (node->type == XML_DOCUMENT_NODE) return;
    InterlockedDecrement((LONG*)&node->_private);
}

448 449
HRESULT node_insert_before(xmlnode *This, IXMLDOMNode *new_child, const VARIANT *ref_child,
        IXMLDOMNode **ret)
450
{
451 452
    IXMLDOMNode *before = NULL;
    xmlnode *node_obj;
453
    int refcount = 0;
454
    xmlDocPtr doc;
455 456
    HRESULT hr;

457
    if(!new_child)
458 459
        return E_INVALIDARG;

460
    node_obj = get_node_obj(new_child);
461
    if(!node_obj) return E_FAIL;
462 463

    switch(V_VT(ref_child))
464 465 466 467 468 469 470
    {
    case VT_EMPTY:
    case VT_NULL:
        break;

    case VT_UNKNOWN:
    case VT_DISPATCH:
471 472 473 474 475
        if (V_UNKNOWN(ref_child))
        {
            hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
            if(FAILED(hr)) return hr;
        }
476 477 478
        break;

    default:
479
        FIXME("refChild var type %x\n", V_VT(ref_child));
480 481 482
        return E_FAIL;
    }

483
    TRACE("new child %p, This->node %p\n", node_obj->node, This->node);
484

485 486 487
    if(!node_obj->node->parent)
        if(xmldoc_remove_orphan(node_obj->node->doc, node_obj->node) != S_OK)
            WARN("%p is not an orphan of %p\n", node_obj->node, node_obj->node->doc);
488

489 490
    refcount = xmlnode_get_inst_cnt(node_obj);

491 492
    if(before)
    {
493
        xmlnode *before_node_obj = get_node_obj(before);
494
        IXMLDOMNode_Release(before);
495
        if(!before_node_obj) return E_FAIL;
496
    }
497

498 499 500 501 502 503 504
    /* unlink from current parent first */
    if(node_obj->parent)
    {
        hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
        if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
    }
    doc = node_obj->node->doc;
505

506 507
    if(before)
    {
508
        xmlNodePtr new_node;
509
        xmlnode *before_node_obj = get_node_obj(before);
510 511 512 513 514 515

        /* refs count including subtree */
        if (doc != before_node_obj->node->doc)
            refcount = xmlnode_get_inst_cnt(node_obj);

        if (refcount) xmldoc_add_refs(before_node_obj->node->doc, refcount);
516 517 518 519 520 521 522 523
        new_node = xmlAddPrevSibling(before_node_obj->node, node_obj->node);
        if (new_node != node_obj->node)
        {
            if (refcount != 1)
                FIXME("referenced xmlNode was freed, expect crashes\n");
            xmlnode_add_ref(new_node);
            node_obj->node = new_node;
        }
524
        if (refcount) xmldoc_release_refs(doc, refcount);
525
        node_obj->parent = This->parent;
526 527 528
    }
    else
    {
529 530
        xmlNodePtr new_node;

531 532 533 534
        if (doc != This->node->doc)
            refcount = xmlnode_get_inst_cnt(node_obj);

        if (refcount) xmldoc_add_refs(This->node->doc, refcount);
535 536
        /* xmlAddChild doesn't unlink node from previous parent */
        xmlUnlinkNode(node_obj->node);
537 538 539 540 541 542 543 544
        new_node = xmlAddChild(This->node, node_obj->node);
        if (new_node != node_obj->node)
        {
            if (refcount != 1)
                FIXME("referenced xmlNode was freed, expect crashes\n");
            xmlnode_add_ref(new_node);
            node_obj->node = new_node;
        }
545
        if (refcount) xmldoc_release_refs(doc, refcount);
546
        node_obj->parent = This->iface;
547 548
    }

549 550
    if(ret)
    {
551 552 553
        IXMLDOMNode_AddRef(new_child);
        *ret = new_child;
    }
554

555 556
    TRACE("ret S_OK\n");
    return S_OK;
557 558
}

559 560
HRESULT node_replace_child(xmlnode *This, IXMLDOMNode *newChild, IXMLDOMNode *oldChild,
        IXMLDOMNode **ret)
561
{
562
    xmlnode *old_child, *new_child;
563 564
    xmlDocPtr leaving_doc;
    xmlNode *my_ancestor;
565
    int refcount = 0;
566 567 568 569 570 571

    /* Do not believe any documentation telling that newChild == NULL
       means removal. It does certainly *not* apply to msxml3! */
    if(!newChild || !oldChild)
        return E_INVALIDARG;

572 573
    if(ret)
        *ret = NULL;
574

575
    old_child = get_node_obj(oldChild);
576
    if(!old_child) return E_FAIL;
577

578
    if(old_child->node->parent != This->node)
579
    {
580
        WARN("childNode %p is not a child of %p\n", oldChild, This);
581 582 583
        return E_INVALIDARG;
    }

584
    new_child = get_node_obj(newChild);
585
    if(!new_child) return E_FAIL;
586

587 588 589
    my_ancestor = This->node;
    while(my_ancestor)
    {
590
        if(my_ancestor == new_child->node)
591 592 593 594 595 596 597
        {
            WARN("tried to create loop\n");
            return E_FAIL;
        }
        my_ancestor = my_ancestor->parent;
    }

598 599 600
    if(!new_child->node->parent)
        if(xmldoc_remove_orphan(new_child->node->doc, new_child->node) != S_OK)
            WARN("%p is not an orphan of %p\n", new_child->node, new_child->node->doc);
601

602
    leaving_doc = new_child->node->doc;
603 604 605 606 607

    if (leaving_doc != old_child->node->doc)
        refcount = xmlnode_get_inst_cnt(new_child);

    if (refcount) xmldoc_add_refs(old_child->node->doc, refcount);
608
    xmlReplaceNode(old_child->node, new_child->node);
609
    if (refcount) xmldoc_release_refs(leaving_doc, refcount);
610 611
    new_child->parent = old_child->parent;
    old_child->parent = NULL;
612

613
    xmldoc_add_orphan(old_child->node->doc, old_child->node);
614

615
    if(ret)
616 617
    {
        IXMLDOMNode_AddRef(oldChild);
618
        *ret = oldChild;
619 620 621
    }

    return S_OK;
622 623
}

624
HRESULT node_remove_child(xmlnode *This, IXMLDOMNode* child, IXMLDOMNode** oldChild)
625
{
626
    xmlnode *child_node;
627

628
    if(!child) return E_INVALIDARG;
629

630 631
    if(oldChild)
        *oldChild = NULL;
632

633
    child_node = get_node_obj(child);
634
    if(!child_node) return E_FAIL;
635

636
    if(child_node->node->parent != This->node)
637
    {
638
        WARN("childNode %p is not a child of %p\n", child, This);
639 640 641
        return E_INVALIDARG;
    }

642
    xmlUnlinkNode(child_node->node);
643
    child_node->parent = NULL;
644
    xmldoc_add_orphan(child_node->node->doc, child_node->node);
645

646
    if(oldChild)
647
    {
648 649
        IXMLDOMNode_AddRef(child);
        *oldChild = child;
650 651
    }

652
    return S_OK;
653 654
}

655
HRESULT node_append_child(xmlnode *This, IXMLDOMNode *child, IXMLDOMNode **outChild)
656
{
657
    DOMNodeType type;
658
    VARIANT var;
659
    HRESULT hr;
660

661 662 663
    if (!child)
        return E_INVALIDARG;

664
    hr = IXMLDOMNode_get_nodeType(child, &type);
665
    if(FAILED(hr) || type == NODE_ATTRIBUTE) {
666
        if (outChild) *outChild = NULL;
667
        return E_FAIL;
668 669
    }

670
    VariantInit(&var);
671
    return IXMLDOMNode_insertBefore(This->iface, child, var, outChild);
672 673
}

674
HRESULT node_has_childnodes(const xmlnode *This, VARIANT_BOOL *ret)
675
{
676
    if (!ret) return E_INVALIDARG;
677 678 679

    if (!This->node->children)
    {
680
        *ret = VARIANT_FALSE;
681 682 683
        return S_FALSE;
    }

684
    *ret = VARIANT_TRUE;
685
    return S_OK;
686 687
}

688
HRESULT node_get_owner_doc(const xmlnode *This, IXMLDOMDocument **doc)
689
{
690 691
    if(!doc)
        return E_INVALIDARG;
692
    return get_domdoc_from_xmldoc(This->node->doc, (IXMLDOMDocument3**)doc);
693 694
}

695
HRESULT node_clone(xmlnode *This, VARIANT_BOOL deep, IXMLDOMNode **cloneNode)
696
{
697 698
    IXMLDOMNode *node;
    xmlNodePtr clone;
699

700
    if(!cloneNode) return E_INVALIDARG;
701

702 703
    clone = xmlCopyNode(This->node, deep ? 1 : 2);
    if (clone)
704
    {
705
        xmlSetTreeDoc(clone, This->node->doc);
706
        xmldoc_add_orphan(clone->doc, clone);
707

708 709
        node = create_node(clone);
        if (!node)
710 711
        {
            ERR("Copy failed\n");
712 713
            xmldoc_remove_orphan(clone->doc, clone);
            xmlFreeNode(clone);
714 715 716
            return E_FAIL;
        }

717
        *cloneNode = node;
718 719 720 721 722 723 724 725
    }
    else
    {
        ERR("Copy failed\n");
        return E_FAIL;
    }

    return S_OK;
726 727
}

728
static xmlChar* do_get_text(xmlNodePtr node, BOOL trim, DWORD *first, DWORD *last, BOOL *trail_ig_ws)
729 730 731 732 733
{
    xmlNodePtr child;
    xmlChar* str;
    BOOL preserving = is_preserving_whitespace(node);

734 735 736
    *first = -1;
    *last = 0;

737 738 739
    if (!node->children)
    {
        str = xmlNodeGetContent(node);
740
        *trail_ig_ws = *(DWORD*)&node->_private & NODE_PRIV_CHILD_IGNORABLE_WS;
741 742 743
    }
    else
    {
744
        BOOL ig_ws = FALSE;
745
        xmlChar* tmp;
746
        DWORD pos = 0;
747
        str = xmlStrdup(BAD_CAST "");
748 749 750 751 752

        if (node->type != XML_DOCUMENT_NODE)
            ig_ws = *(DWORD*)&node->_private & NODE_PRIV_CHILD_IGNORABLE_WS;
        *trail_ig_ws = FALSE;

753 754 755 756
        for (child = node->children; child != NULL; child = child->next)
        {
            switch (child->type)
            {
757 758 759 760 761 762 763 764 765
            case XML_ELEMENT_NODE: {
                DWORD node_first, node_last;

                tmp = do_get_text(child, FALSE, &node_first, &node_last, trail_ig_ws);

                if (node_first!=-1 && pos+node_first<*first)
                    *first = pos+node_first;
                if (node_last && pos+node_last>*last)
                    *last = pos+node_last;
766
                break;
767
            }
768
            case XML_TEXT_NODE:
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
                tmp = xmlNodeGetContent(child);
                if (!preserving && tmp[0])
                {
                    xmlChar *beg;

                    for (beg = tmp; *beg; beg++)
                        if (!isspace(*beg)) break;

                    if (!*beg)
                    {
                        ig_ws = TRUE;
                        xmlFree(tmp);
                        tmp = NULL;
                    }
                }
784
                break;
785 786 787 788 789 790 791 792 793 794
            case XML_CDATA_SECTION_NODE:
            case XML_ENTITY_REF_NODE:
            case XML_ENTITY_NODE:
                tmp = xmlNodeGetContent(child);
                break;
            default:
                tmp = NULL;
                break;
            }

795
            if ((tmp && *tmp) || child->type==XML_CDATA_SECTION_NODE)
796
            {
797
                if (ig_ws && str[0])
798
                {
799 800
                    str = xmlStrcat(str, BAD_CAST " ");
                    pos++;
801
                }
802 803 804 805 806 807 808
                if (tmp && *tmp) str = xmlStrcat(str, tmp);
                if (child->type==XML_CDATA_SECTION_NODE && pos<*first)
                    *first = pos;
                if (tmp && *tmp) pos += xmlStrlen(tmp);
                if (child->type==XML_CDATA_SECTION_NODE && pos>*last)
                    *last = pos;
                ig_ws = FALSE;
809
            }
810
            if (tmp) xmlFree(tmp);
811 812 813 814 815 816 817 818

            if (!ig_ws)
            {
                ig_ws = *(DWORD*)&child->_private & NODE_PRIV_TRAILING_IGNORABLE_WS;
            }
            if (!ig_ws)
                ig_ws = *trail_ig_ws;
            *trail_ig_ws = FALSE;
819
        }
820 821

        *trail_ig_ws = ig_ws;
822 823 824 825 826 827 828 829 830 831
    }

    switch (node->type)
    {
    case XML_ELEMENT_NODE:
    case XML_TEXT_NODE:
    case XML_ENTITY_REF_NODE:
    case XML_ENTITY_NODE:
    case XML_DOCUMENT_NODE:
    case XML_DOCUMENT_FRAG_NODE:
832
        if (trim && !preserving)
833
        {
834
            xmlChar* ret;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
            int len;

            if (!str)
                break;

            for (ret = str; *ret && isspace(*ret) && (*first)--; ret++)
                if (*last) (*last)--;
            for (len = xmlStrlen(ret)-1; len >= 0 && len >= *last; len--)
                if(!isspace(ret[len])) break;

            ret = xmlStrndup(ret, len+1);
            xmlFree(str);
            str = ret;
            break;
        }
850 851 852 853 854 855 856 857
        break;
    default:
        break;
    }

    return str;
}

858
HRESULT node_get_text(const xmlnode *This, BSTR *text)
859
{
860
    BSTR str = NULL;
861
    xmlChar *content;
862
    DWORD first, last;
863
    BOOL tmp;
864

865
    if (!text) return E_INVALIDARG;
866

867
    content = do_get_text(This->node, TRUE, &first, &last, &tmp);
868
    if (content)
869
    {
870 871
        str = bstr_from_xmlChar(content);
        xmlFree(content);
872
    }
873 874 875 876

    /* Always return a string. */
    if (!str) str = SysAllocStringLen( NULL, 0 );

877 878 879 880
    TRACE("%p %s\n", This, debugstr_w(str) );
    *text = str;
 
    return S_OK;
881 882
}

883
HRESULT node_put_text(xmlnode *This, BSTR text)
884
{
885
    xmlChar *str, *str2;
886

887
    TRACE("(%p)->(%s)\n", This, debugstr_w(text));
888

889
    str = xmlchar_from_wchar(text);
890 891

    /* Escape the string. */
892
    str2 = xmlEncodeEntitiesReentrant(This->node->doc, str);
893
    heap_free(str);
894

895 896
    xmlNodeSetContent(This->node, str2);
    xmlFree(str2);
897 898

    return S_OK;
899 900
}

901
BSTR EnsureCorrectEOL(BSTR sInput)
902 903 904 905 906 907
{
    int nNum = 0;
    BSTR sNew;
    int nLen;
    int i;

908
    nLen = SysStringLen(sInput);
909 910 911
    /* Count line endings */
    for(i=0; i < nLen; i++)
    {
912
        if(sInput[i] == '\n')
913 914 915 916 917 918 919 920 921
            nNum++;
    }

    TRACE("len=%d, num=%d\n", nLen, nNum);

    /* Add linefeed as needed */
    if(nNum > 0)
    {
        int nPlace = 0;
922
        sNew = SysAllocStringLen(NULL, nLen + nNum);
923 924
        for(i=0; i < nLen; i++)
        {
925
            if(sInput[i] == '\n')
926
            {
927
                sNew[i+nPlace] = '\r';
928 929 930 931 932 933 934 935 936 937 938 939
                nPlace++;
            }
            sNew[i+nPlace] = sInput[i];
        }

        SysFreeString(sInput);
    }
    else
    {
        sNew = sInput;
    }

940
    TRACE("len %d\n", SysStringLen(sNew));
941 942 943 944 945 946

    return sNew;
}

/*
 * We are trying to replicate the same behaviour as msxml by converting
947
 * line endings to \r\n and using indents as \t. The problem is that msxml
948 949 950 951
 * only formats nodes that have a line ending. Using libxml we cannot
 * reproduce behaviour exactly.
 *
 */
952
HRESULT node_get_xml(xmlnode *This, BOOL ensure_eol, BSTR *ret)
953
{
954
    xmlBufferPtr xml_buf;
955
    xmlNodePtr xmldecl;
956
    int size;
957

958
    if(!ret)
959 960
        return E_INVALIDARG;

961 962 963 964 965
    *ret = NULL;

    xml_buf = xmlBufferCreate();
    if(!xml_buf)
        return E_OUTOFMEMORY;
966

967 968
    xmldecl = xmldoc_unlink_xmldecl( This->node->doc );

969 970 971 972
    size = xmlNodeDump(xml_buf, This->node->doc, This->node, 0, 1);
    if(size > 0) {
        const xmlChar *buf_content;
        BSTR content;
973

974 975
        /* Attribute Nodes return a space in front of their name */
        buf_content = xmlBufferContent(xml_buf);
976

977 978 979
        content = bstr_from_xmlChar(buf_content + (buf_content[0] == ' ' ? 1 : 0));
        if(ensure_eol)
            content = EnsureCorrectEOL(content);
980

981 982 983
        *ret = content;
    }else {
        *ret = SysAllocStringLen(NULL, 0);
984 985
    }

986
    xmlBufferFree(xml_buf);
987
    xmldoc_link_xmldecl( This->node->doc, xmldecl );
988
    return *ret ? S_OK : E_OUTOFMEMORY;
989 990
}

991 992
#ifdef SONAME_LIBXSLT

993 994 995 996 997 998 999 1000 1001
/* duplicates xmlBufferWriteQuotedString() logic */
static void xml_write_quotedstring(xmlOutputBufferPtr buf, const xmlChar *string)
{
    const xmlChar *cur, *base;

    if (xmlStrchr(string, '\"'))
    {
        if (xmlStrchr(string, '\''))
        {
1002
            xmlOutputBufferWrite(buf, 1, "\"");
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
            base = cur = string;

            while (*cur)
            {
                if (*cur == '"')
                {
                    if (base != cur)
                        xmlOutputBufferWrite(buf, cur-base, (const char*)base);
                    xmlOutputBufferWrite(buf, 6, "&quot;");
                    cur++;
                    base = cur;
                }
                else
                    cur++;
            }
            if (base != cur)
                xmlOutputBufferWrite(buf, cur-base, (const char*)base);
1020 1021
            xmlOutputBufferWrite(buf, 1, "\"");
        }
1022 1023
        else
        {
1024
            xmlOutputBufferWrite(buf, 1, "\'");
1025
            xmlOutputBufferWriteString(buf, (const char*)string);
1026
            xmlOutputBufferWrite(buf, 1, "\'");
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
        }
    }
    else
    {
        xmlOutputBufferWrite(buf, 1, "\"");
        xmlOutputBufferWriteString(buf, (const char*)string);
        xmlOutputBufferWrite(buf, 1, "\"");
    }
}

1037 1038 1039
static int XMLCALL transform_to_stream_write(void *context, const char *buffer, int len)
{
    DWORD written;
1040
    HRESULT hr = ISequentialStream_Write((ISequentialStream *)context, buffer, len, &written);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    return hr == S_OK ? written : -1;
}

/* Output for method "text" */
static void transform_write_text(xmlDocPtr result, xsltStylesheetPtr style, xmlOutputBufferPtr output)
{
    xmlNodePtr cur = result->children;
    while (cur)
    {
        if (cur->type == XML_TEXT_NODE)
            xmlOutputBufferWriteString(output, (const char*)cur->content);

        /* skip to next node */
        if (cur->children)
        {
            if ((cur->children->type != XML_ENTITY_DECL) &&
                (cur->children->type != XML_ENTITY_REF_NODE) &&
                (cur->children->type != XML_ENTITY_NODE))
            {
                cur = cur->children;
                continue;
            }
        }

        if (cur->next) {
            cur = cur->next;
            continue;
        }

        do
        {
            cur = cur->parent;
            if (cur == NULL)
                break;
            if (cur == (xmlNodePtr) style->doc) {
                cur = NULL;
                break;
            }
            if (cur->next) {
                cur = cur->next;
                break;
            }
        } while (cur);
    }
}

#undef XSLT_GET_IMPORT_PTR
#define XSLT_GET_IMPORT_PTR(res, style, name) {          \
    xsltStylesheetPtr st = style;                        \
    res = NULL;                                          \
    while (st != NULL) {                                 \
        if (st->name != NULL) { res = st->name; break; } \
        st = pxsltNextImport(st);                        \
    }}

#undef XSLT_GET_IMPORT_INT
#define XSLT_GET_IMPORT_INT(res, style, name) {         \
    xsltStylesheetPtr st = style;                       \
    res = -1;                                           \
    while (st != NULL) {                                \
        if (st->name != -1) { res = st->name; break; }  \
        st = pxsltNextImport(st);                       \
    }}

static void transform_write_xmldecl(xmlDocPtr result, xsltStylesheetPtr style, BOOL omit_encoding, xmlOutputBufferPtr output)
{
    int omit_xmldecl, standalone;

    XSLT_GET_IMPORT_INT(omit_xmldecl, style, omitXmlDeclaration);
    if (omit_xmldecl == 1) return;

    XSLT_GET_IMPORT_INT(standalone, style, standalone);

    xmlOutputBufferWriteString(output, "<?xml version=");
    if (result->version)
    {
        xmlOutputBufferWriteString(output, "\"");
        xmlOutputBufferWriteString(output, (const char *)result->version);
        xmlOutputBufferWriteString(output, "\"");
    }
    else
        xmlOutputBufferWriteString(output, "\"1.0\"");

    if (!omit_encoding)
    {
        const xmlChar *encoding;

        /* default encoding is UTF-16 */
        XSLT_GET_IMPORT_PTR(encoding, style, encoding);
        xmlOutputBufferWriteString(output, " encoding=");
        xmlOutputBufferWriteString(output, "\"");
        xmlOutputBufferWriteString(output, encoding ? (const char *)encoding : "UTF-16");
        xmlOutputBufferWriteString(output, "\"");
    }

    /* standalone attribute */
    if (standalone != -1)
        xmlOutputBufferWriteString(output, standalone == 0 ? " standalone=\"no\"" : " standalone=\"yes\"");

    xmlOutputBufferWriteString(output, "?>");
}

1143 1144 1145 1146 1147 1148 1149 1150 1151
static void htmldtd_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc)
{
    xmlDtdPtr cur = doc->intSubset;

    xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
    if (cur->ExternalID)
    {
        xmlOutputBufferWriteString(buf, " PUBLIC ");
1152
        xml_write_quotedstring(buf, cur->ExternalID);
1153 1154 1155
        if (cur->SystemID)
        {
            xmlOutputBufferWriteString(buf, " ");
1156
            xml_write_quotedstring(buf, cur->SystemID);
1157
        }
1158 1159 1160 1161
    }
    else if (cur->SystemID)
    {
        xmlOutputBufferWriteString(buf, " SYSTEM ");
1162
        xml_write_quotedstring(buf, cur->SystemID);
1163 1164 1165 1166
    }
    xmlOutputBufferWriteString(buf, ">\n");
}

1167 1168
/* Duplicates htmlDocContentDumpFormatOutput() the way we need it - doesn't add trailing newline. */
static void htmldoc_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding, int format)
1169 1170 1171 1172 1173 1174 1175 1176
{
    xmlElementType type;

    /* force HTML output */
    type = doc->type;
    doc->type = XML_HTML_DOCUMENT_NODE;
    if (doc->intSubset)
        htmldtd_dumpcontent(buf, doc);
1177
    if (doc->children) {
1178
        xmlNodePtr cur = doc->children;
1179 1180
        while (cur) {
            htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
1181 1182 1183 1184 1185 1186
            cur = cur->next;
        }
    }
    doc->type = type;
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
static inline BOOL transform_is_empty_resultdoc(xmlDocPtr result)
{
    return !result->children || ((result->children->type == XML_DTD_NODE) && !result->children->next);
}

static inline BOOL transform_is_valid_method(xsltStylesheetPtr style)
{
    return !style->methodURI || !(style->method && xmlStrEqual(style->method, (const xmlChar *)"xhtml"));
}

/* Helper to write transformation result to specified output buffer. */
static HRESULT node_transform_write(xsltStylesheetPtr style, xmlDocPtr result, BOOL omit_encoding, const char *encoding, xmlOutputBufferPtr output)
{
    const xmlChar *method;
    int indent;

    if (!transform_is_valid_method(style))
    {
        ERR("unknown output method\n");
        return E_FAIL;
    }

    XSLT_GET_IMPORT_PTR(method, style, method)
    XSLT_GET_IMPORT_INT(indent, style, indent);

    if (!method && (result->type == XML_HTML_DOCUMENT_NODE))
        method = (const xmlChar *) "html";

    if (method && xmlStrEqual(method, (const xmlChar *)"html"))
    {
        htmlSetMetaEncoding(result, (const xmlChar *)encoding);
        if (indent == -1)
            indent = 1;
1220
        htmldoc_dumpcontent(output, result, encoding, indent);
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    }
    else if (method && xmlStrEqual(method, (const xmlChar *)"xhtml"))
    {
        htmlSetMetaEncoding(result, (const xmlChar *) encoding);
        htmlDocContentDumpOutput(output, result, encoding);
    }
    else if (method && xmlStrEqual(method, (const xmlChar *)"text"))
        transform_write_text(result, style, output);
    else
    {
        transform_write_xmldecl(result, style, omit_encoding, output);

        if (result->children)
        {
            xmlNodePtr child = result->children;

            while (child)
            {
                xmlNodeDumpOutput(output, result, child, 0, indent == 1, encoding);
                if (indent && ((child->type == XML_DTD_NODE) || ((child->type == XML_COMMENT_NODE) && child->next)))
                    xmlOutputBufferWriteString(output, "\r\n");
                child = child->next;
            }
        }
    }

    xmlOutputBufferFlush(output);
    return S_OK;
}

/* For BSTR output is always UTF-16, without 'encoding' attribute */
static HRESULT node_transform_write_to_bstr(xsltStylesheetPtr style, xmlDocPtr result, BSTR *str)
1253
{
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    HRESULT hr = S_OK;

    if (transform_is_empty_resultdoc(result))
        *str = SysAllocStringLen(NULL, 0);
    else
    {
        xmlOutputBufferPtr output = xmlAllocOutputBuffer(xmlFindCharEncodingHandler("UTF-16"));
        const xmlChar *content;
        size_t len;

        *str = NULL;
        if (!output)
            return E_OUTOFMEMORY;

        hr = node_transform_write(style, result, TRUE, "UTF-16", output);
1269
#ifdef LIBXML2_NEW_BUFFER
1270 1271
        content = xmlBufContent(output->conv);
        len = xmlBufUse(output->conv);
1272
#else
1273 1274
        content = xmlBufferContent(output->conv);
        len = xmlBufferLength(output->conv);
1275
#endif
1276 1277 1278 1279 1280 1281 1282
        /* UTF-16 encoder places UTF-16 bom, we don't need it for BSTR */
        content += sizeof(WCHAR);
        *str = SysAllocStringLen((WCHAR*)content, len/sizeof(WCHAR) - 1);
        xmlOutputBufferClose(output);
    }

    return *str ? hr : E_OUTOFMEMORY;
1283 1284
}

1285
static HRESULT node_transform_write_to_stream(xsltStylesheetPtr style, xmlDocPtr result, ISequentialStream *stream)
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
{
    static const xmlChar *utf16 = (const xmlChar*)"UTF-16";
    xmlOutputBufferPtr output;
    const xmlChar *encoding;
    HRESULT hr;

    if (transform_is_empty_resultdoc(result))
    {
        WARN("empty result document\n");
        return S_OK;
    }

    if (style->methodURI && (!style->method || !xmlStrEqual(style->method, (const xmlChar *) "xhtml")))
    {
        ERR("unknown output method\n");
        return E_FAIL;
    }

    /* default encoding is UTF-16 */
    XSLT_GET_IMPORT_PTR(encoding, style, encoding);
    if (!encoding)
        encoding = utf16;

    output = xmlOutputBufferCreateIO(transform_to_stream_write, NULL, stream, xmlFindCharEncodingHandler((const char*)encoding));
    if (!output)
        return E_OUTOFMEMORY;

    hr = node_transform_write(style, result, FALSE, (const char*)encoding, output);
    xmlOutputBufferClose(output);
    return hr;
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 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 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 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 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
struct import_buffer
{
    char *data;
    int cur;
    int len;
};

static int XMLCALL import_loader_io_read(void *context, char *out, int len)
{
    struct import_buffer *buffer = (struct import_buffer *)context;

    TRACE("%p, %p, %d\n", context, out, len);

    if (buffer->cur == buffer->len)
        return 0;

    len = min(len, buffer->len - buffer->cur);
    memcpy(out, &buffer->data[buffer->cur], len);
    buffer->cur += len;

    TRACE("read %d\n", len);

    return len;
}

static int XMLCALL import_loader_io_close(void * context)
{
    struct import_buffer *buffer = (struct import_buffer *)context;

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

    heap_free(buffer->data);
    heap_free(buffer);
    return 0;
}

static HRESULT import_loader_onDataAvailable(void *ctxt, char *ptr, DWORD len)
{
    xmlParserInputPtr *input = (xmlParserInputPtr *)ctxt;
    xmlParserInputBufferPtr inputbuffer;
    struct import_buffer *buffer;

    buffer = heap_alloc(sizeof(*buffer));

    buffer->data = heap_alloc(len);
    memcpy(buffer->data, ptr, len);
    buffer->cur = 0;
    buffer->len = len;

    inputbuffer = xmlParserInputBufferCreateIO(import_loader_io_read, import_loader_io_close, buffer,
            XML_CHAR_ENCODING_NONE);
    *input = xmlNewIOInputStream(ctxt, inputbuffer, XML_CHAR_ENCODING_NONE);
    if (!*input)
        xmlFreeParserInputBuffer(inputbuffer);

    return *input ? S_OK : E_FAIL;
}

static HRESULT xslt_doc_get_uri(const xmlChar *uri, void *_ctxt, xsltLoadType type, IUri **doc_uri)
{
    xsltStylesheetPtr style = (xsltStylesheetPtr)_ctxt;
    IUri *href_uri;
    HRESULT hr;
    BSTR uriW;

    *doc_uri = NULL;

    uriW = bstr_from_xmlChar(uri);
    hr = CreateUri(uriW, Uri_CREATE_ALLOW_RELATIVE | Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &href_uri);
    SysFreeString(uriW);
    if (FAILED(hr))
    {
        WARN("Failed to create href uri, %#x.\n", hr);
        return hr;
    }

    if (type == XSLT_LOAD_STYLESHEET && style->doc && style->doc->name)
    {
        IUri *base_uri;
        BSTR baseuriW;

        baseuriW = bstr_from_xmlChar((xmlChar *)style->doc->name);
        hr = CreateUri(baseuriW, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &base_uri);
        SysFreeString(baseuriW);
        if (FAILED(hr))
        {
            WARN("Failed to create base uri, %#x.\n", hr);
            return hr;
        }

        hr = CoInternetCombineIUri(base_uri, href_uri, 0, doc_uri, 0);
        IUri_Release(base_uri);
        if (FAILED(hr))
            WARN("Failed to combine uris, %#x.\n", hr);
    }
    else
    {
        *doc_uri = href_uri;
        IUri_AddRef(*doc_uri);
    }

    IUri_Release(href_uri);

    return hr;
}

xmlDocPtr xslt_doc_default_loader(const xmlChar *uri, xmlDictPtr dict, int options,
    void *_ctxt, xsltLoadType type)
{
    IUri *import_uri = NULL;
    xmlParserInputPtr input;
    xmlParserCtxtPtr pctxt;
    xmlDocPtr doc = NULL;
    IMoniker *moniker;
    HRESULT hr;
    bsc_t *bsc;
    BSTR uriW;

    TRACE("%s, %p, %#x, %p, %d\n", debugstr_a((const char *)uri), dict, options, _ctxt, type);

    pctxt = xmlNewParserCtxt();
    if (!pctxt)
        return NULL;

    if (dict && pctxt->dict)
    {
        xmlDictFree(pctxt->dict);
        pctxt->dict = NULL;
    }

    if (dict)
    {
        pctxt->dict = dict;
        xmlDictReference(pctxt->dict);
    }

    xmlCtxtUseOptions(pctxt, options);

    hr = xslt_doc_get_uri(uri, _ctxt, type, &import_uri);
    if (FAILED(hr))
        goto failed;

    hr = CreateURLMonikerEx2(NULL, import_uri, &moniker, 0);
    if (FAILED(hr))
        goto failed;

    hr = bind_url(moniker, import_loader_onDataAvailable, &input, &bsc);
    IMoniker_Release(moniker);
    if (FAILED(hr))
        goto failed;

    if (FAILED(detach_bsc(bsc)))
        goto failed;

    if (!input)
        goto failed;

    inputPush(pctxt, input);
    xmlParseDocument(pctxt);

    if (pctxt->wellFormed)
    {
        doc = pctxt->myDoc;
        /* Set imported uri, to give nested imports a chance. */
        if (IUri_GetPropertyBSTR(import_uri, Uri_PROPERTY_ABSOLUTE_URI, &uriW, 0) == S_OK)
        {
            doc->name = (char *)xmlchar_from_wcharn(uriW, SysStringLen(uriW), TRUE);
            SysFreeString(uriW);
        }
    }
    else
    {
        doc = NULL;
        xmlFreeDoc(pctxt->myDoc);
        pctxt->myDoc = NULL;
    }

failed:
    xmlFreeParserCtxt(pctxt);
    if (import_uri)
        IUri_Release(import_uri);

    return doc;
}

#endif /* SONAME_LIBXSLT */
1504

1505
HRESULT node_transform_node_params(const xmlnode *This, IXMLDOMNode *stylesheet, BSTR *p,
1506
    ISequentialStream *stream, const struct xslprocessor_params *params)
1507
{
1508
#ifdef SONAME_LIBXSLT
1509
    xsltStylesheetPtr xsltSS;
1510
    xmlDocPtr sheet_doc;
1511
    HRESULT hr = S_OK;
1512
    xmlnode *sheet;
1513

1514
    if (!libxslt_handle) return E_NOTIMPL;
1515
    if (!stylesheet || (!p && !stream)) return E_INVALIDARG;
1516

1517
    if (p) *p = NULL;
1518

1519
    sheet = get_node_obj(stylesheet);
1520
    if(!sheet) return E_FAIL;
1521

1522 1523 1524
    sheet_doc = xmlCopyDoc(sheet->node->doc, 1);
    xsltSS = pxsltParseStylesheetDoc(sheet_doc);
    if (xsltSS)
1525
    {
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        const char **xslparams = NULL;
        xmlDocPtr result;
        unsigned int i;

        /* convert our parameter list to libxml2 format */
        if (params && params->count)
        {
            struct xslprocessor_par *par;

            i = 0;
            xslparams = heap_alloc((params->count*2 + 1)*sizeof(char*));
            LIST_FOR_EACH_ENTRY(par, &params->list, struct xslprocessor_par, entry)
            {
                xslparams[i++] = (char*)xmlchar_from_wchar(par->name);
                xslparams[i++] = (char*)xmlchar_from_wchar(par->value);
            }
            xslparams[i] = NULL;
        }

        if (xslparams)
        {
            xsltTransformContextPtr ctxt = pxsltNewTransformContext(xsltSS, This->node->doc);

            /* push parameters to user context */
            pxsltQuoteUserParams(ctxt, xslparams);
            result = pxsltApplyStylesheetUser(xsltSS, This->node->doc, NULL, NULL, NULL, ctxt);
            pxsltFreeTransformContext(ctxt);

            for (i = 0; i < params->count*2; i++)
                heap_free((char*)xslparams[i]);
            heap_free(xslparams);
        }
        else
            result = pxsltApplyStylesheet(xsltSS, This->node->doc, NULL);

1561
        if (result)
1562
        {
1563 1564 1565 1566
            if (stream)
                hr = node_transform_write_to_stream(xsltSS, result, stream);
            else
                hr = node_transform_write_to_bstr(xsltSS, result, p);
1567
            xmlFreeDoc(result);
1568
        }
1569

1570
        pxsltFreeStylesheet(xsltSS);
1571
    }
1572 1573
    else
        xmlFreeDoc(sheet_doc);
1574

1575
    if (p && !*p) *p = SysAllocStringLen(NULL, 0);
1576

1577
    return hr;
1578
#else
1579 1580
    ERR_(winediag)("libxslt headers were not found at compile time. Expect problems.\n");

1581
    return E_NOTIMPL;
1582
#endif
1583 1584
}

1585 1586
HRESULT node_transform_node(const xmlnode *node, IXMLDOMNode *stylesheet, BSTR *p)
{
1587
    return node_transform_node_params(node, stylesheet, p, NULL, NULL);
1588 1589
}

1590
HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
1591
{
1592 1593
    xmlChar* str;
    HRESULT hr;
1594

1595
    if (!query || !nodes) return E_INVALIDARG;
1596

1597
    str = xmlchar_from_wchar(query);
1598
    hr = create_selection(This->node, str, nodes);
1599 1600 1601
    heap_free(str);

    return hr;
1602 1603
}

1604
HRESULT node_select_singlenode(const xmlnode *This, BSTR query, IXMLDOMNode **node)
1605
{
Huw Davies's avatar
Huw Davies committed
1606
    IXMLDOMNodeList *list;
1607
    HRESULT hr;
Huw Davies's avatar
Huw Davies committed
1608

1609 1610
    hr = node_select_nodes(This, query, &list);
    if (hr == S_OK)
Huw Davies's avatar
Huw Davies committed
1611
    {
1612
        hr = IXMLDOMNodeList_nextNode(list, node);
Huw Davies's avatar
Huw Davies committed
1613 1614
        IXMLDOMNodeList_Release(list);
    }
1615
    return hr;
1616 1617
}

1618
HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1619
{
1620
    xmlNsPtr ns = This->node->ns;
1621 1622 1623 1624 1625 1626

    if(!namespaceURI)
        return E_INVALIDARG;

    *namespaceURI = NULL;

1627 1628
    if (ns && ns->href)
        *namespaceURI = bstr_from_xmlChar(ns->href);
1629

1630 1631 1632
    TRACE("uri: %s\n", debugstr_w(*namespaceURI));

    return *namespaceURI ? S_OK : S_FALSE;
1633 1634
}

1635
HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1636
{
1637
    xmlNsPtr ns = This->node->ns;
1638

1639
    if (!prefix) return E_INVALIDARG;
1640

1641
    *prefix = NULL;
1642

1643 1644
    if (ns && ns->prefix)
        *prefix = bstr_from_xmlChar(ns->prefix);
1645

1646
    TRACE("prefix: %s\n", debugstr_w(*prefix));
1647

1648
    return *prefix ? S_OK : S_FALSE;
1649 1650
}

1651
HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1652
{
1653
    if (!name) return E_INVALIDARG;
1654

1655 1656
    *name = bstr_from_xmlChar(This->node->name);
    if (!*name) return E_OUTOFMEMORY;
1657

1658
    TRACE("returning %s\n", debugstr_w(*name));
1659

1660
    return S_OK;
1661 1662
}

1663
void destroy_xmlnode(xmlnode *This)
1664
{
1665
    if(This->node)
1666 1667
    {
        xmlnode_release(This->node);
1668
        xmldoc_release(This->node->doc);
1669
    }
1670 1671
}

1672
void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1673 1674
{
    if(node)
1675 1676 1677 1678
    {
        xmlnode_add_ref(node);
        xmldoc_add_ref(node->doc);
    }
1679

1680
    This->node = node;
1681
    This->iface = node_iface;
1682
    This->parent = NULL;
1683

1684
    init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1685 1686
}

1687 1688
typedef struct {
    xmlnode node;
1689
    IXMLDOMNode IXMLDOMNode_iface;
1690 1691 1692
    LONG ref;
} unknode;

1693
static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1694
{
1695
    return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1696 1697 1698 1699 1700 1701 1702
}

static HRESULT WINAPI unknode_QueryInterface(
    IXMLDOMNode *iface,
    REFIID riid,
    void** ppvObject )
{
1703
    unknode *This = unknode_from_IXMLDOMNode( iface );
1704 1705 1706 1707 1708 1709 1710

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);

    if (IsEqualGUID(riid, &IID_IUnknown)) {
        *ppvObject = iface;
    }else if (IsEqualGUID( riid, &IID_IDispatch) ||
              IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1711
        *ppvObject = &This->IXMLDOMNode_iface;
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
    }else if(node_query_interface(&This->node, riid, ppvObject)) {
        return *ppvObject ? S_OK : E_NOINTERFACE;
    }else  {
        FIXME("interface %s not implemented\n", debugstr_guid(riid));
        *ppvObject = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppvObject);
    return S_OK;
}

static ULONG WINAPI unknode_AddRef(
    IXMLDOMNode *iface )
{
1727
    unknode *This = unknode_from_IXMLDOMNode( iface );
1728 1729 1730 1731 1732 1733 1734

    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI unknode_Release(
    IXMLDOMNode *iface )
{
1735
    unknode *This = unknode_from_IXMLDOMNode( iface );
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
    LONG ref;

    ref = InterlockedDecrement( &This->ref );
    if(!ref) {
        destroy_xmlnode(&This->node);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI unknode_GetTypeInfoCount(
    IXMLDOMNode *iface,
    UINT* pctinfo )
{
1751
    unknode *This = unknode_from_IXMLDOMNode( iface );
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

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

    *pctinfo = 1;

    return S_OK;
}

static HRESULT WINAPI unknode_GetTypeInfo(
    IXMLDOMNode *iface,
    UINT iTInfo,
    LCID lcid,
1764
    ITypeInfo** ppTInfo )
1765
{
1766
    unknode *This = unknode_from_IXMLDOMNode( iface );
1767 1768
    HRESULT hr;

1769 1770 1771
    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783

    return hr;
}

static HRESULT WINAPI unknode_GetIDsOfNames(
    IXMLDOMNode *iface,
    REFIID riid,
    LPOLESTR* rgszNames,
    UINT cNames,
    LCID lcid,
    DISPID* rgDispId )
{
1784
    unknode *This = unknode_from_IXMLDOMNode( iface );
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
          lcid, rgDispId);

    if(!rgszNames || cNames == 0 || !rgDispId)
        return E_INVALIDARG;

    hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
    if(SUCCEEDED(hr))
1797
    {
1798
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1799 1800
        ITypeInfo_Release(typeinfo);
    }
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815

    return hr;
}

static HRESULT WINAPI unknode_Invoke(
    IXMLDOMNode *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS* pDispParams,
    VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo,
    UINT* puArgErr )
{
1816
    unknode *This = unknode_from_IXMLDOMNode( iface );
1817 1818 1819 1820 1821 1822 1823 1824
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
          lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
    if(SUCCEEDED(hr))
1825
    {
1826
        hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDOMNode_iface, dispIdMember, wFlags, pDispParams,
1827
                pVarResult, pExcepInfo, puArgErr);
1828 1829
        ITypeInfo_Release(typeinfo);
    }
1830 1831 1832 1833 1834 1835 1836 1837

    return hr;
}

static HRESULT WINAPI unknode_get_nodeName(
    IXMLDOMNode *iface,
    BSTR* p )
{
1838
    unknode *This = unknode_from_IXMLDOMNode( iface );
1839 1840 1841 1842

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

    return node_get_nodeName(&This->node, p);
1843 1844 1845 1846
}

static HRESULT WINAPI unknode_get_nodeValue(
    IXMLDOMNode *iface,
1847
    VARIANT* value)
1848
{
1849
    unknode *This = unknode_from_IXMLDOMNode( iface );
1850 1851 1852 1853 1854 1855 1856 1857

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

    if(!value)
        return E_INVALIDARG;

    V_VT(value) = VT_NULL;
    return S_FALSE;
1858 1859 1860 1861
}

static HRESULT WINAPI unknode_put_nodeValue(
    IXMLDOMNode *iface,
1862
    VARIANT value)
1863
{
1864
    unknode *This = unknode_from_IXMLDOMNode( iface );
1865 1866
    FIXME("(%p)->(v%d)\n", This, V_VT(&value));
    return E_FAIL;
1867 1868 1869 1870 1871 1872
}

static HRESULT WINAPI unknode_get_nodeType(
    IXMLDOMNode *iface,
    DOMNodeType* domNodeType )
{
1873
    unknode *This = unknode_from_IXMLDOMNode( iface );
1874 1875 1876

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

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
    switch (This->node.node->type)
    {
    case XML_ELEMENT_NODE:
    case XML_ATTRIBUTE_NODE:
    case XML_TEXT_NODE:
    case XML_CDATA_SECTION_NODE:
    case XML_ENTITY_REF_NODE:
    case XML_ENTITY_NODE:
    case XML_PI_NODE:
    case XML_COMMENT_NODE:
    case XML_DOCUMENT_NODE:
    case XML_DOCUMENT_TYPE_NODE:
    case XML_DOCUMENT_FRAG_NODE:
    case XML_NOTATION_NODE:
        /* we only care about this set of types, libxml2 type values are
           exactly what we need */
        *domNodeType = (DOMNodeType)This->node.node->type;
        break;
    default:
        *domNodeType = NODE_INVALID;
        break;
    }

1900
    return S_OK;
1901 1902 1903 1904 1905 1906
}

static HRESULT WINAPI unknode_get_parentNode(
    IXMLDOMNode *iface,
    IXMLDOMNode** parent )
{
1907
    unknode *This = unknode_from_IXMLDOMNode( iface );
1908
    FIXME("(%p)->(%p)\n", This, parent);
1909 1910 1911 1912 1913 1914 1915 1916 1917
    if (!parent) return E_INVALIDARG;
    *parent = NULL;
    return S_FALSE;
}

static HRESULT WINAPI unknode_get_childNodes(
    IXMLDOMNode *iface,
    IXMLDOMNodeList** outList)
{
1918
    unknode *This = unknode_from_IXMLDOMNode( iface );
1919 1920 1921 1922

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

    return node_get_child_nodes(&This->node, outList);
1923 1924 1925 1926 1927 1928
}

static HRESULT WINAPI unknode_get_firstChild(
    IXMLDOMNode *iface,
    IXMLDOMNode** domNode)
{
1929
    unknode *This = unknode_from_IXMLDOMNode( iface );
1930 1931 1932 1933

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

    return node_get_first_child(&This->node, domNode);
1934 1935 1936 1937 1938 1939
}

static HRESULT WINAPI unknode_get_lastChild(
    IXMLDOMNode *iface,
    IXMLDOMNode** domNode)
{
1940
    unknode *This = unknode_from_IXMLDOMNode( iface );
1941 1942 1943

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

1944
    return node_get_last_child(&This->node, domNode);
1945 1946 1947 1948 1949 1950
}

static HRESULT WINAPI unknode_get_previousSibling(
    IXMLDOMNode *iface,
    IXMLDOMNode** domNode)
{
1951
    unknode *This = unknode_from_IXMLDOMNode( iface );
1952 1953 1954 1955

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

    return node_get_previous_sibling(&This->node, domNode);
1956 1957 1958 1959 1960 1961
}

static HRESULT WINAPI unknode_get_nextSibling(
    IXMLDOMNode *iface,
    IXMLDOMNode** domNode)
{
1962
    unknode *This = unknode_from_IXMLDOMNode( iface );
1963 1964 1965 1966

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

    return node_get_next_sibling(&This->node, domNode);
1967 1968 1969 1970 1971 1972
}

static HRESULT WINAPI unknode_get_attributes(
    IXMLDOMNode *iface,
    IXMLDOMNamedNodeMap** attributeMap)
{
1973
    unknode *This = unknode_from_IXMLDOMNode( iface );
1974 1975 1976 1977

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

    return return_null_ptr((void**)attributeMap);
1978 1979 1980 1981
}

static HRESULT WINAPI unknode_insertBefore(
    IXMLDOMNode *iface,
1982
    IXMLDOMNode* newNode, VARIANT refChild,
1983 1984
    IXMLDOMNode** outOldNode)
{
1985
    unknode *This = unknode_from_IXMLDOMNode( iface );
1986 1987 1988 1989

    FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);

    return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1990 1991 1992 1993 1994 1995 1996 1997
}

static HRESULT WINAPI unknode_replaceChild(
    IXMLDOMNode *iface,
    IXMLDOMNode* newNode,
    IXMLDOMNode* oldNode,
    IXMLDOMNode** outOldNode)
{
1998
    unknode *This = unknode_from_IXMLDOMNode( iface );
1999 2000 2001 2002

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

    return node_replace_child(&This->node, newNode, oldNode, outOldNode);
2003 2004 2005 2006 2007 2008
}

static HRESULT WINAPI unknode_removeChild(
    IXMLDOMNode *iface,
    IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
{
2009
    unknode *This = unknode_from_IXMLDOMNode( iface );
2010
    return node_remove_child(&This->node, domNode, oldNode);
2011 2012 2013 2014 2015 2016
}

static HRESULT WINAPI unknode_appendChild(
    IXMLDOMNode *iface,
    IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
{
2017
    unknode *This = unknode_from_IXMLDOMNode( iface );
2018
    return node_append_child(&This->node, newNode, outNewNode);
2019 2020 2021 2022 2023 2024
}

static HRESULT WINAPI unknode_hasChildNodes(
    IXMLDOMNode *iface,
    VARIANT_BOOL* pbool)
{
2025
    unknode *This = unknode_from_IXMLDOMNode( iface );
2026
    return node_has_childnodes(&This->node, pbool);
2027 2028 2029 2030 2031 2032
}

static HRESULT WINAPI unknode_get_ownerDocument(
    IXMLDOMNode *iface,
    IXMLDOMDocument** domDocument)
{
2033
    unknode *This = unknode_from_IXMLDOMNode( iface );
2034
    return node_get_owner_doc(&This->node, domDocument);
2035 2036 2037 2038 2039 2040
}

static HRESULT WINAPI unknode_cloneNode(
    IXMLDOMNode *iface,
    VARIANT_BOOL pbool, IXMLDOMNode** outNode)
{
2041
    unknode *This = unknode_from_IXMLDOMNode( iface );
2042
    return node_clone(&This->node, pbool, outNode );
2043 2044 2045 2046 2047 2048
}

static HRESULT WINAPI unknode_get_nodeTypeString(
    IXMLDOMNode *iface,
    BSTR* p)
{
2049
    unknode *This = unknode_from_IXMLDOMNode( iface );
2050 2051 2052 2053

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

    return node_get_nodeName(&This->node, p);
2054 2055 2056 2057 2058 2059
}

static HRESULT WINAPI unknode_get_text(
    IXMLDOMNode *iface,
    BSTR* p)
{
2060
    unknode *This = unknode_from_IXMLDOMNode( iface );
2061
    return node_get_text(&This->node, p);
2062 2063 2064 2065 2066 2067
}

static HRESULT WINAPI unknode_put_text(
    IXMLDOMNode *iface,
    BSTR p)
{
2068
    unknode *This = unknode_from_IXMLDOMNode( iface );
2069
    return node_put_text(&This->node, p);
2070 2071 2072 2073
}

static HRESULT WINAPI unknode_get_specified(
    IXMLDOMNode *iface,
2074
    VARIANT_BOOL* isSpecified)
2075
{
2076
    unknode *This = unknode_from_IXMLDOMNode( iface );
2077 2078 2079
    FIXME("(%p)->(%p) stub!\n", This, isSpecified);
    *isSpecified = VARIANT_TRUE;
    return S_OK;
2080 2081 2082 2083
}

static HRESULT WINAPI unknode_get_definition(
    IXMLDOMNode *iface,
2084
    IXMLDOMNode** definitionNode)
2085
{
2086
    unknode *This = unknode_from_IXMLDOMNode( iface );
2087 2088
    FIXME("(%p)->(%p)\n", This, definitionNode);
    return E_NOTIMPL;
2089 2090 2091 2092 2093 2094
}

static HRESULT WINAPI unknode_get_nodeTypedValue(
    IXMLDOMNode *iface,
    VARIANT* var1)
{
2095
    unknode *This = unknode_from_IXMLDOMNode( iface );
2096 2097
    FIXME("(%p)->(%p)\n", This, var1);
    return return_null_var(var1);
2098 2099 2100 2101
}

static HRESULT WINAPI unknode_put_nodeTypedValue(
    IXMLDOMNode *iface,
2102
    VARIANT typedValue)
2103
{
2104
    unknode *This = unknode_from_IXMLDOMNode( iface );
2105 2106
    FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
    return E_NOTIMPL;
2107 2108 2109 2110 2111 2112
}

static HRESULT WINAPI unknode_get_dataType(
    IXMLDOMNode *iface,
    VARIANT* var1)
{
2113
    unknode *This = unknode_from_IXMLDOMNode( iface );
2114 2115
    TRACE("(%p)->(%p)\n", This, var1);
    return return_null_var(var1);
2116 2117 2118 2119 2120 2121
}

static HRESULT WINAPI unknode_put_dataType(
    IXMLDOMNode *iface,
    BSTR p)
{
2122
    unknode *This = unknode_from_IXMLDOMNode( iface );
2123 2124 2125 2126 2127 2128 2129

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

    if(!p)
        return E_INVALIDARG;

    return E_FAIL;
2130 2131 2132 2133 2134 2135
}

static HRESULT WINAPI unknode_get_xml(
    IXMLDOMNode *iface,
    BSTR* p)
{
2136
    unknode *This = unknode_from_IXMLDOMNode( iface );
2137 2138 2139

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

2140
    return node_get_xml(&This->node, FALSE, p);
2141 2142 2143 2144 2145 2146
}

static HRESULT WINAPI unknode_transformNode(
    IXMLDOMNode *iface,
    IXMLDOMNode* domNode, BSTR* p)
{
2147
    unknode *This = unknode_from_IXMLDOMNode( iface );
2148
    return node_transform_node(&This->node, domNode, p);
2149 2150 2151 2152 2153 2154
}

static HRESULT WINAPI unknode_selectNodes(
    IXMLDOMNode *iface,
    BSTR p, IXMLDOMNodeList** outList)
{
2155
    unknode *This = unknode_from_IXMLDOMNode( iface );
2156
    return node_select_nodes(&This->node, p, outList);
2157 2158 2159 2160 2161 2162
}

static HRESULT WINAPI unknode_selectSingleNode(
    IXMLDOMNode *iface,
    BSTR p, IXMLDOMNode** outNode)
{
2163
    unknode *This = unknode_from_IXMLDOMNode( iface );
2164
    return node_select_singlenode(&This->node, p, outNode);
2165 2166 2167 2168
}

static HRESULT WINAPI unknode_get_parsed(
    IXMLDOMNode *iface,
2169
    VARIANT_BOOL* isParsed)
2170
{
2171
    unknode *This = unknode_from_IXMLDOMNode( iface );
2172 2173 2174
    FIXME("(%p)->(%p) stub!\n", This, isParsed);
    *isParsed = VARIANT_TRUE;
    return S_OK;
2175 2176 2177 2178 2179 2180
}

static HRESULT WINAPI unknode_get_namespaceURI(
    IXMLDOMNode *iface,
    BSTR* p)
{
2181
    unknode *This = unknode_from_IXMLDOMNode( iface );
2182 2183
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_namespaceURI(&This->node, p);
2184 2185 2186 2187 2188 2189
}

static HRESULT WINAPI unknode_get_prefix(
    IXMLDOMNode *iface,
    BSTR* p)
{
2190
    unknode *This = unknode_from_IXMLDOMNode( iface );
2191
    return node_get_prefix(&This->node, p);
2192 2193 2194 2195 2196 2197
}

static HRESULT WINAPI unknode_get_baseName(
    IXMLDOMNode *iface,
    BSTR* p)
{
2198
    unknode *This = unknode_from_IXMLDOMNode( iface );
2199
    return node_get_base_name(&This->node, p);
2200 2201 2202 2203 2204 2205
}

static HRESULT WINAPI unknode_transformNodeToObject(
    IXMLDOMNode *iface,
    IXMLDOMNode* domNode, VARIANT var1)
{
2206
    unknode *This = unknode_from_IXMLDOMNode( iface );
2207 2208
    FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
    return E_NOTIMPL;
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
}

static const struct IXMLDOMNodeVtbl unknode_vtbl =
{
    unknode_QueryInterface,
    unknode_AddRef,
    unknode_Release,
    unknode_GetTypeInfoCount,
    unknode_GetTypeInfo,
    unknode_GetIDsOfNames,
    unknode_Invoke,
    unknode_get_nodeName,
    unknode_get_nodeValue,
    unknode_put_nodeValue,
    unknode_get_nodeType,
    unknode_get_parentNode,
    unknode_get_childNodes,
    unknode_get_firstChild,
    unknode_get_lastChild,
    unknode_get_previousSibling,
    unknode_get_nextSibling,
    unknode_get_attributes,
    unknode_insertBefore,
    unknode_replaceChild,
    unknode_removeChild,
    unknode_appendChild,
    unknode_hasChildNodes,
    unknode_get_ownerDocument,
    unknode_cloneNode,
    unknode_get_nodeTypeString,
    unknode_get_text,
    unknode_put_text,
    unknode_get_specified,
    unknode_get_definition,
    unknode_get_nodeTypedValue,
    unknode_put_nodeTypedValue,
    unknode_get_dataType,
    unknode_put_dataType,
    unknode_get_xml,
    unknode_transformNode,
    unknode_selectNodes,
    unknode_selectSingleNode,
    unknode_get_parsed,
    unknode_get_namespaceURI,
    unknode_get_prefix,
    unknode_get_baseName,
    unknode_transformNodeToObject
};

2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
IXMLDOMNode *create_node( xmlNodePtr node )
{
    IUnknown *pUnk;
    IXMLDOMNode *ret;
    HRESULT hr;

    if ( !node )
        return NULL;

    TRACE("type %d\n", node->type);
    switch(node->type)
    {
    case XML_ELEMENT_NODE:
2271
        pUnk = create_element( node );
2272
        break;
2273
    case XML_ATTRIBUTE_NODE:
2274
        pUnk = create_attribute( node, FALSE );
2275
        break;
2276 2277 2278
    case XML_TEXT_NODE:
        pUnk = create_text( node );
        break;
2279 2280 2281
    case XML_CDATA_SECTION_NODE:
        pUnk = create_cdata( node );
        break;
2282 2283 2284
    case XML_ENTITY_REF_NODE:
        pUnk = create_doc_entity_ref( node );
        break;
2285 2286 2287
    case XML_PI_NODE:
        pUnk = create_pi( node );
        break;
2288 2289 2290
    case XML_COMMENT_NODE:
        pUnk = create_comment( node );
        break;
2291
    case XML_DOCUMENT_NODE:
2292 2293
        pUnk = create_domdoc( node );
        break;
2294 2295 2296
    case XML_DOCUMENT_FRAG_NODE:
        pUnk = create_doc_fragment( node );
        break;
2297
    case XML_DTD_NODE:
2298
    case XML_DOCUMENT_TYPE_NODE:
2299 2300
        pUnk = create_doc_type( node );
        break;
2301 2302
    case XML_ENTITY_NODE:
    case XML_NOTATION_NODE: {
2303
        unknode *new_node;
2304

2305
        FIXME("only creating basic node for type %d\n", node->type);
2306

2307
        new_node = heap_alloc(sizeof(unknode));
2308 2309 2310
        if(!new_node)
            return NULL;

2311
        new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2312
        new_node->ref = 1;
2313 2314
        init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
        pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2315
        break;
2316
    }
2317 2318 2319
    default:
        ERR("Called for unsupported node type %d\n", node->type);
        return NULL;
2320 2321 2322 2323 2324 2325 2326
    }

    hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
    IUnknown_Release(pUnk);
    if(FAILED(hr)) return NULL;
    return ret;
}
2327
#endif