element.c 46.7 KB
Newer Older
1 2 3 4 5
/*
 *    DOM Document 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
 */

#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
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
34
#include "winnls.h"
35
#include "ole2.h"
36
#include "msxml6.h"
37 38 39 40 41

#include "msxml_private.h"

#include "wine/debug.h"

42 43
#ifdef HAVE_LIBXML2

44 45
WINE_DEFAULT_DEBUG_CHANNEL(msxml);

46
static const xmlChar DT_prefix[] = "dt";
47 48
static const xmlChar DT_nsURI[] = "urn:schemas-microsoft-com:datatypes";

49 50
typedef struct _domelem
{
51
    xmlnode node;
52
    IXMLDOMElement IXMLDOMElement_iface;
53 54 55
    LONG ref;
} domelem;

56 57
static const struct nodemap_funcs domelem_attr_map;

58 59 60
static const tid_t domelem_se_tids[] = {
    IXMLDOMNode_tid,
    IXMLDOMElement_tid,
61
    NULL_tid
62 63
};

64 65
static inline domelem *impl_from_IXMLDOMElement( IXMLDOMElement *iface )
{
66
    return CONTAINING_RECORD(iface, domelem, IXMLDOMElement_iface);
67 68
}

69
static inline xmlNodePtr get_element( const domelem *This )
70
{
71
    return This->node.node;
72 73
}

74 75 76 77 78
static HRESULT WINAPI domelem_QueryInterface(
    IXMLDOMElement *iface,
    REFIID riid,
    void** ppvObject )
{
79
    domelem *This = impl_from_IXMLDOMElement( iface );
80

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

83
    if ( IsEqualGUID( riid, &IID_IXMLDOMElement ) ||
84
         IsEqualGUID( riid, &IID_IXMLDOMNode ) ||
85 86 87
         IsEqualGUID( riid, &IID_IDispatch ) ||
         IsEqualGUID( riid, &IID_IUnknown ) )
    {
88
        *ppvObject = &This->IXMLDOMElement_iface;
89
    }
90
    else if(node_query_interface(&This->node, riid, ppvObject))
91 92 93
    {
        return *ppvObject ? S_OK : E_NOINTERFACE;
    }
94 95 96 97
    else if(IsEqualGUID( riid, &IID_ISupportErrorInfo ))
    {
        return node_create_supporterrorinfo(domelem_se_tids, ppvObject);
    }
98 99
    else
    {
100
        TRACE("interface %s not implemented\n", debugstr_guid(riid));
101
        *ppvObject = NULL;
102 103 104 105 106
        return E_NOINTERFACE;
    }

    IUnknown_AddRef( (IUnknown*)*ppvObject );
    return S_OK;
107 108 109 110 111 112
}

static ULONG WINAPI domelem_AddRef(
    IXMLDOMElement *iface )
{
    domelem *This = impl_from_IXMLDOMElement( iface );
113 114
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
118 119 120 121 122 123
}

static ULONG WINAPI domelem_Release(
    IXMLDOMElement *iface )
{
    domelem *This = impl_from_IXMLDOMElement( iface );
124 125
    ULONG ref = InterlockedDecrement(&This->ref);

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

    if(!ref) {
129
        destroy_xmlnode(&This->node);
130 131 132 133
        heap_free(This);
    }

    return ref;
134 135 136 137 138 139
}

static HRESULT WINAPI domelem_GetTypeInfoCount(
    IXMLDOMElement *iface,
    UINT* pctinfo )
{
140
    domelem *This = impl_from_IXMLDOMElement( iface );
141
    return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
142 143 144 145 146 147 148
}

static HRESULT WINAPI domelem_GetTypeInfo(
    IXMLDOMElement *iface,
    UINT iTInfo, LCID lcid,
    ITypeInfo** ppTInfo )
{
149
    domelem *This = impl_from_IXMLDOMElement( iface );
150 151
    return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface,
        iTInfo, lcid, ppTInfo);
152 153 154 155 156 157 158
}

static HRESULT WINAPI domelem_GetIDsOfNames(
    IXMLDOMElement *iface,
    REFIID riid, LPOLESTR* rgszNames,
    UINT cNames, LCID lcid, DISPID* rgDispId )
{
159
    domelem *This = impl_from_IXMLDOMElement( iface );
160 161
    return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface,
        riid, rgszNames, cNames, lcid, rgDispId);
162 163 164 165 166 167 168 169
}

static HRESULT WINAPI domelem_Invoke(
    IXMLDOMElement *iface,
    DISPID dispIdMember, REFIID riid, LCID lcid,
    WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
    EXCEPINFO* pExcepInfo, UINT* puArgErr )
{
170
    domelem *This = impl_from_IXMLDOMElement( iface );
171 172
    return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface,
        dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
173 174 175 176 177 178
}

static HRESULT WINAPI domelem_get_nodeName(
    IXMLDOMElement *iface,
    BSTR* p )
{
179
    domelem *This = impl_from_IXMLDOMElement( iface );
180 181 182 183

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

    return node_get_nodeName(&This->node, p);
184 185 186 187
}

static HRESULT WINAPI domelem_get_nodeValue(
    IXMLDOMElement *iface,
188
    VARIANT* value)
189
{
190
    domelem *This = impl_from_IXMLDOMElement( iface );
191 192 193 194 195 196 197 198 199

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

    if(!value)
        return E_INVALIDARG;

    V_VT(value) = VT_NULL;
    V_BSTR(value) = NULL; /* tests show that we should do this */
    return S_FALSE;
200 201 202 203
}

static HRESULT WINAPI domelem_put_nodeValue(
    IXMLDOMElement *iface,
204
    VARIANT value)
205
{
206
    domelem *This = impl_from_IXMLDOMElement( iface );
207
    TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
208
    return E_FAIL;
209 210 211 212 213 214
}

static HRESULT WINAPI domelem_get_nodeType(
    IXMLDOMElement *iface,
    DOMNodeType* domNodeType )
{
215
    domelem *This = impl_from_IXMLDOMElement( iface );
216 217 218 219 220

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

    *domNodeType = NODE_ELEMENT;
    return S_OK;
221 222 223 224 225 226
}

static HRESULT WINAPI domelem_get_parentNode(
    IXMLDOMElement *iface,
    IXMLDOMNode** parent )
{
227
    domelem *This = impl_from_IXMLDOMElement( iface );
228 229 230 231

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

    return node_get_parent(&This->node, parent);
232 233 234 235 236 237
}

static HRESULT WINAPI domelem_get_childNodes(
    IXMLDOMElement *iface,
    IXMLDOMNodeList** outList)
{
238
    domelem *This = impl_from_IXMLDOMElement( iface );
239 240 241 242

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

    return node_get_child_nodes(&This->node, outList);
243 244 245 246 247 248
}

static HRESULT WINAPI domelem_get_firstChild(
    IXMLDOMElement *iface,
    IXMLDOMNode** domNode)
{
249
    domelem *This = impl_from_IXMLDOMElement( iface );
250 251 252 253

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

    return node_get_first_child(&This->node, domNode);
254 255 256 257 258 259
}

static HRESULT WINAPI domelem_get_lastChild(
    IXMLDOMElement *iface,
    IXMLDOMNode** domNode)
{
260
    domelem *This = impl_from_IXMLDOMElement( iface );
261 262 263

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

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

static HRESULT WINAPI domelem_get_previousSibling(
    IXMLDOMElement *iface,
    IXMLDOMNode** domNode)
{
271
    domelem *This = impl_from_IXMLDOMElement( iface );
272 273 274 275

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

    return node_get_previous_sibling(&This->node, domNode);
276 277 278 279 280 281
}

static HRESULT WINAPI domelem_get_nextSibling(
    IXMLDOMElement *iface,
    IXMLDOMNode** domNode)
{
282
    domelem *This = impl_from_IXMLDOMElement( iface );
283 284 285 286

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

    return node_get_next_sibling(&This->node, domNode);
287 288 289 290
}

static HRESULT WINAPI domelem_get_attributes(
    IXMLDOMElement *iface,
291
    IXMLDOMNamedNodeMap** map)
292
{
293
    domelem *This = impl_from_IXMLDOMElement( iface );
294

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

297
    *map = create_nodemap(This->node.node, &domelem_attr_map);
298
    return S_OK;
299 300 301 302
}

static HRESULT WINAPI domelem_insertBefore(
    IXMLDOMElement *iface,
303
    IXMLDOMNode* newNode, VARIANT refChild,
304
    IXMLDOMNode** old_node)
305
{
306
    domelem *This = impl_from_IXMLDOMElement( iface );
307 308 309 310
    DOMNodeType type;
    HRESULT hr;

    TRACE("(%p)->(%p %s %p)\n", This, newNode, debugstr_variant(&refChild), old_node);
311

312 313
    hr = IXMLDOMNode_get_nodeType(newNode, &type);
    if (hr != S_OK) return hr;
314

315 316 317 318 319 320 321 322 323 324 325 326
    TRACE("new node type %d\n", type);
    switch (type)
    {
        case NODE_DOCUMENT:
        case NODE_DOCUMENT_TYPE:
        case NODE_ENTITY:
        case NODE_NOTATION:
            if (old_node) *old_node = NULL;
            return E_FAIL;
        default:
            return node_insert_before(&This->node, newNode, &refChild, old_node);
    }
327 328 329 330 331 332 333 334
}

static HRESULT WINAPI domelem_replaceChild(
    IXMLDOMElement *iface,
    IXMLDOMNode* newNode,
    IXMLDOMNode* oldNode,
    IXMLDOMNode** outOldNode)
{
335
    domelem *This = impl_from_IXMLDOMElement( iface );
336 337 338 339

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

    return node_replace_child(&This->node, newNode, oldNode, outOldNode);
340 341 342 343
}

static HRESULT WINAPI domelem_removeChild(
    IXMLDOMElement *iface,
344
    IXMLDOMNode *child, IXMLDOMNode **oldChild)
345
{
346
    domelem *This = impl_from_IXMLDOMElement( iface );
347 348
    TRACE("(%p)->(%p %p)\n", This, child, oldChild);
    return node_remove_child(&This->node, child, oldChild);
349 350 351 352
}

static HRESULT WINAPI domelem_appendChild(
    IXMLDOMElement *iface,
353
    IXMLDOMNode *child, IXMLDOMNode **outChild)
354
{
355
    domelem *This = impl_from_IXMLDOMElement( iface );
356 357
    TRACE("(%p)->(%p %p)\n", This, child, outChild);
    return node_append_child(&This->node, child, outChild);
358 359 360 361
}

static HRESULT WINAPI domelem_hasChildNodes(
    IXMLDOMElement *iface,
362
    VARIANT_BOOL *ret)
363
{
364
    domelem *This = impl_from_IXMLDOMElement( iface );
365 366
    TRACE("(%p)->(%p)\n", This, ret);
    return node_has_childnodes(&This->node, ret);
367 368 369
}

static HRESULT WINAPI domelem_get_ownerDocument(
370 371
    IXMLDOMElement   *iface,
    IXMLDOMDocument **doc)
372
{
373
    domelem *This = impl_from_IXMLDOMElement( iface );
374 375
    TRACE("(%p)->(%p)\n", This, doc);
    return node_get_owner_doc(&This->node, doc);
376 377 378 379
}

static HRESULT WINAPI domelem_cloneNode(
    IXMLDOMElement *iface,
380
    VARIANT_BOOL deep, IXMLDOMNode** outNode)
381
{
382
    domelem *This = impl_from_IXMLDOMElement( iface );
383 384
    TRACE("(%p)->(%d %p)\n", This, deep, outNode);
    return node_clone( &This->node, deep, outNode );
385 386 387 388 389 390
}

static HRESULT WINAPI domelem_get_nodeTypeString(
    IXMLDOMElement *iface,
    BSTR* p)
{
391
    domelem *This = impl_from_IXMLDOMElement( iface );
392 393 394 395 396
    static const WCHAR elementW[] = {'e','l','e','m','e','n','t',0};

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

    return return_bstr(elementW, p);
397 398 399 400 401 402
}

static HRESULT WINAPI domelem_get_text(
    IXMLDOMElement *iface,
    BSTR* p)
{
403
    domelem *This = impl_from_IXMLDOMElement( iface );
404 405
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_text(&This->node, p);
406 407 408 409 410 411
}

static HRESULT WINAPI domelem_put_text(
    IXMLDOMElement *iface,
    BSTR p)
{
412
    domelem *This = impl_from_IXMLDOMElement( iface );
413 414
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
    return node_put_text( &This->node, p );
415 416 417 418
}

static HRESULT WINAPI domelem_get_specified(
    IXMLDOMElement *iface,
419
    VARIANT_BOOL* isSpecified)
420
{
421
    domelem *This = impl_from_IXMLDOMElement( iface );
422 423 424
    FIXME("(%p)->(%p) stub!\n", This, isSpecified);
    *isSpecified = VARIANT_TRUE;
    return S_OK;
425 426 427 428
}

static HRESULT WINAPI domelem_get_definition(
    IXMLDOMElement *iface,
429
    IXMLDOMNode** definitionNode)
430
{
431
    domelem *This = impl_from_IXMLDOMElement( iface );
432 433
    FIXME("(%p)->(%p)\n", This, definitionNode);
    return E_NOTIMPL;
434 435
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
static inline BYTE hex_to_byte(xmlChar c)
{
    if(c <= '9') return c-'0';
    if(c <= 'F') return c-'A'+10;
    return c-'a'+10;
}

static inline BYTE base64_to_byte(xmlChar c)
{
    if(c == '+') return 62;
    if(c == '/') return 63;
    if(c <= '9') return c-'0'+52;
    if(c <= 'Z') return c-'A';
    return c-'a'+26;
}

452
static inline HRESULT variant_from_dt(XDR_DT dt, xmlChar* str, VARIANT* v)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
    VARIANT src;
    HRESULT hr = S_OK;
    BOOL handled = FALSE;

    VariantInit(&src);

    switch (dt)
    {
    case DT_INVALID:
    case DT_STRING:
    case DT_NMTOKEN:
    case DT_NMTOKENS:
    case DT_NUMBER:
    case DT_URI:
    case DT_UUID:
        {
            V_VT(v) = VT_BSTR;
            V_BSTR(v) = bstr_from_xmlChar(str);

            if(!V_BSTR(v))
                return E_OUTOFMEMORY;
            handled = TRUE;
        }
        break;
    case DT_DATE:
    case DT_DATE_TZ:
    case DT_DATETIME:
    case DT_DATETIME_TZ:
    case DT_TIME:
    case DT_TIME_TZ:
        {
            WCHAR *p, *e;
            SYSTEMTIME st;
            DOUBLE date = 0.0;

            st.wYear = 1899;
            st.wMonth = 12;
            st.wDay = 30;
            st.wDayOfWeek = st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;

            V_VT(&src) = VT_BSTR;
            V_BSTR(&src) = bstr_from_xmlChar(str);

            if(!V_BSTR(&src))
                return E_OUTOFMEMORY;

            p = V_BSTR(&src);
            e = p + SysStringLen(V_BSTR(&src));

            if(p+4<e && *(p+4)=='-') /* parse date (yyyy-mm-dd) */
            {
                st.wYear = atoiW(p);
                st.wMonth = atoiW(p+5);
                st.wDay = atoiW(p+8);
                p += 10;

                if(*p == 'T') p++;
            }

            if(p+2<e && *(p+2)==':') /* parse time (hh:mm:ss.?) */
            {
                st.wHour = atoiW(p);
                st.wMinute = atoiW(p+3);
                st.wSecond = atoiW(p+6);
                p += 8;

                if(*p == '.')
                {
                    p++;
                    while(isdigitW(*p)) p++;
                }
            }

            SystemTimeToVariantTime(&st, &date);
            V_VT(v) = VT_DATE;
            V_DATE(v) = date;

            if(*p == '+') /* parse timezone offset (+hh:mm) */
                V_DATE(v) += (DOUBLE)atoiW(p+1)/24 + (DOUBLE)atoiW(p+4)/1440;
            else if(*p == '-') /* parse timezone offset (-hh:mm) */
                V_DATE(v) -= (DOUBLE)atoiW(p+1)/24 + (DOUBLE)atoiW(p+4)/1440;

            VariantClear(&src);
            handled = TRUE;
        }
        break;
    case DT_BIN_HEX:
        {
            SAFEARRAYBOUND sab;
            int i, len;

            len = xmlStrlen(str)/2;
            sab.lLbound = 0;
            sab.cElements = len;

            V_VT(v) = (VT_ARRAY|VT_UI1);
            V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);

            if(!V_ARRAY(v))
                return E_OUTOFMEMORY;

            for(i=0; i<len; i++)
                ((BYTE*)V_ARRAY(v)->pvData)[i] = (hex_to_byte(str[2*i])<<4)
                    + hex_to_byte(str[2*i+1]);
            handled = TRUE;
        }
        break;
    case DT_BIN_BASE64:
        {
            SAFEARRAYBOUND sab;
564
            xmlChar *c1, *c2;
565 566
            int i, len;

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
            /* remove all formatting chars */
            c1 = c2 = str;
            len = 0;
            while (*c2)
            {
                if ( *c2 == ' '  || *c2 == '\t' ||
                     *c2 == '\n' || *c2 == '\r' )
                {
                    c2++;
                    continue;
                }
                *c1++ = *c2++;
                len++;
            }

            /* skip padding */
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
            if(str[len-2] == '=') i = 2;
            else if(str[len-1] == '=') i = 1;
            else i = 0;

            sab.lLbound = 0;
            sab.cElements = len/4*3-i;

            V_VT(v) = (VT_ARRAY|VT_UI1);
            V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);

            if(!V_ARRAY(v))
                return E_OUTOFMEMORY;

            for(i=0; i<len/4; i++)
            {
                ((BYTE*)V_ARRAY(v)->pvData)[3*i] = (base64_to_byte(str[4*i])<<2)
                    + (base64_to_byte(str[4*i+1])>>4);
                if(3*i+1 < sab.cElements)
                    ((BYTE*)V_ARRAY(v)->pvData)[3*i+1] = (base64_to_byte(str[4*i+1])<<4)
                        + (base64_to_byte(str[4*i+2])>>2);
                if(3*i+2 < sab.cElements)
                    ((BYTE*)V_ARRAY(v)->pvData)[3*i+2] = (base64_to_byte(str[4*i+2])<<6)
                        + base64_to_byte(str[4*i+3]);
            }
            handled = TRUE;
        }
        break;
    case DT_BOOLEAN:
        V_VT(v) = VT_BOOL;
        break;
    case DT_FIXED_14_4:
        V_VT(v) = VT_CY;
        break;
    case DT_I1:
        V_VT(v) = VT_I1;
        break;
    case DT_I2:
        V_VT(v) = VT_I2;
        break;
    case DT_I4:
    case DT_INT:
        V_VT(v) = VT_I4;
        break;
    case DT_I8:
        V_VT(v) = VT_I8;
        break;
    case DT_R4:
        V_VT(v) = VT_R4;
        break;
    case DT_FLOAT:
    case DT_R8:
        V_VT(v) = VT_R8;
        break;
    case DT_UI1:
        V_VT(v) = VT_UI1;
        break;
    case DT_UI2:
        V_VT(v) = VT_UI2;
        break;
    case DT_UI4:
        V_VT(v) = VT_UI4;
        break;
    case DT_UI8:
        V_VT(v) = VT_UI8;
        break;
    case DT_CHAR:
    case DT_ENTITY:
    case DT_ENTITIES:
    case DT_ENUMERATION:
    case DT_ID:
    case DT_IDREF:
    case DT_IDREFS:
    case DT_NOTATION:
656
        FIXME("need to handle dt:%s\n", debugstr_dt(dt));
657 658 659 660 661 662
        V_VT(v) = VT_BSTR;
        V_BSTR(v) = bstr_from_xmlChar(str);
        if (!V_BSTR(v))
            return E_OUTOFMEMORY;
        handled = TRUE;
        break;
663 664
    default:
        WARN("unknown type %d\n", dt);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    }

    if (!handled)
    {
        V_VT(&src) = VT_BSTR;
        V_BSTR(&src) = bstr_from_xmlChar(str);

        if(!V_BSTR(&src))
            return E_OUTOFMEMORY;

        hr = VariantChangeTypeEx(v, &src,
                MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),0, V_VT(v));
        VariantClear(&src);
    }
    return hr;
}

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
static XDR_DT element_get_dt(xmlNodePtr node)
{
    XDR_DT dt = DT_INVALID;

    TRACE("(%p)\n", node);
    if(node->type != XML_ELEMENT_NODE)
    {
        FIXME("invalid element node\n");
        return dt;
    }

    if (node->ns && xmlStrEqual(node->ns->href, DT_nsURI))
    {
        dt = str_to_dt(node->name, -1);
    }
    else
    {
        xmlChar* pVal = xmlGetNsProp(node, BAD_CAST "dt", DT_nsURI);
        if (pVal)
        {
            dt = str_to_dt(pVal, -1);
            xmlFree(pVal);
        }
        else if (node->doc)
        {
            IXMLDOMDocument3* doc = (IXMLDOMDocument3*)create_domdoc((xmlNodePtr)node->doc);
            if (doc)
            {
                VARIANT v;
                VariantInit(&v);

                if (IXMLDOMDocument3_get_schemas(doc, &v) == S_OK &&
                    V_VT(&v) == VT_DISPATCH)
                {
                    dt = SchemaCache_get_node_dt((IXMLDOMSchemaCollection2*)V_DISPATCH(&v), node);
                }
                VariantClear(&v);
                IXMLDOMDocument3_Release(doc);
            }
        }
    }

724
    TRACE("=> dt:%s\n", debugstr_dt(dt));
725 726 727
    return dt;
}

728 729
static HRESULT WINAPI domelem_get_nodeTypedValue(
    IXMLDOMElement *iface,
730
    VARIANT* v)
731
{
732
    domelem *This = impl_from_IXMLDOMElement( iface );
733 734 735 736
    XDR_DT dt;
    xmlChar* content;
    HRESULT hr;

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

739
    if(!v) return E_INVALIDARG;
740

741
    V_VT(v) = VT_NULL;
742 743 744

    dt = element_get_dt(get_element(This));
    content = xmlNodeGetContent(get_element(This));
745
    hr = variant_from_dt(dt, content, v);
746 747 748
    xmlFree(content);

    return hr;
749 750
}

751 752 753 754
static HRESULT encode_base64(const BYTE *buf, int len, BSTR *ret)
{
    static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    const BYTE *d = buf;
755
    int bytes, pad_bytes, div;
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    DWORD needed;
    WCHAR *ptr;

    bytes = (len*8 + 5)/6;
    pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;

    TRACE("%d, bytes is %d, pad bytes is %d\n", len, bytes, pad_bytes);
    needed = bytes + pad_bytes + 1;

    *ret = SysAllocStringLen(NULL, needed);
    if (!*ret) return E_OUTOFMEMORY;

    /* Three bytes of input give 4 chars of output */
    div = len / 3;

    ptr = *ret;
    while (div > 0)
    {
        /* first char is the first 6 bits of the first byte*/
        *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
        /* second char is the last 2 bits of the first byte and the first 4
         * bits of the second byte */
        *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
        /* third char is the last 4 bits of the second byte and the first 2
         * bits of the third byte */
        *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
        /* fourth char is the remaining 6 bits of the third byte */
        *ptr++ = b64[   d[2]       & 0x3f];
        d += 3;
        div--;
    }

    switch (pad_bytes)
    {
        case 1:
            /* first char is the first 6 bits of the first byte*/
            *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
            /* second char is the last 2 bits of the first byte and the first 4
             * bits of the second byte */
            *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
            /* third char is the last 4 bits of the second byte padded with
             * two zeroes */
            *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
            /* fourth char is a = to indicate one byte of padding */
            *ptr++ = '=';
            break;
        case 2:
            /* first char is the first 6 bits of the first byte*/
            *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
            /* second char is the last 2 bits of the first byte padded with
             * four zeroes*/
            *ptr++ = b64[ ((d[0] << 4) & 0x30)];
            /* third char is = to indicate padding */
            *ptr++ = '=';
            /* fourth char is = to indicate padding */
            *ptr++ = '=';
            break;
    }

    return S_OK;
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static HRESULT encode_binhex(const BYTE *buf, int len, BSTR *ret)
{
    static const char byte_to_hex[16] = "0123456789abcdef";
    int i;

    *ret = SysAllocStringLen(NULL, len*2);
    if (!*ret) return E_OUTOFMEMORY;

    for (i = 0; i < len; i++)
    {
        (*ret)[2*i]   = byte_to_hex[buf[i] >> 4];
        (*ret)[2*i+1] = byte_to_hex[0x0f & buf[i]];
    }

    return S_OK;
}

835 836
static HRESULT WINAPI domelem_put_nodeTypedValue(
    IXMLDOMElement *iface,
837
    VARIANT value)
838
{
839
    domelem *This = impl_from_IXMLDOMElement( iface );
840
    XDR_DT dt;
841 842
    HRESULT hr;

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

845
    dt = element_get_dt(get_element(This));
846
    switch (dt)
847
    {
848 849
    /* for untyped node coerce to BSTR and set */
    case DT_INVALID:
850 851
        if (V_VT(&value) != VT_BSTR)
        {
852 853 854
            VARIANT content;
            VariantInit(&content);
            hr = VariantChangeType(&content, &value, 0, VT_BSTR);
855 856
            if (hr == S_OK)
            {
857 858
                hr = node_set_content(&This->node, V_BSTR(&content));
                VariantClear(&content);
859 860 861 862
            }
        }
        else
            hr = node_set_content(&This->node, V_BSTR(&value));
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
        break;
    case DT_BIN_BASE64:
        if (V_VT(&value) == VT_BSTR)
            hr = node_set_content(&This->node, V_BSTR(&value));
        else if (V_VT(&value) == (VT_UI1|VT_ARRAY))
        {
            UINT dim = SafeArrayGetDim(V_ARRAY(&value));
            LONG lbound, ubound;
            BSTR encoded;
            BYTE *ptr;
            int len;

            if (dim > 1)
                FIXME("unexpected array dimension count %u\n", dim);

            SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
            SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);

            len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));

            hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
            if (FAILED(hr)) return hr;

            hr = encode_base64(ptr, len, &encoded);
            SafeArrayUnaccessData(V_ARRAY(&value));
            if (FAILED(hr)) return hr;

            hr = node_set_content(&This->node, encoded);
            SysFreeString(encoded);
        }
        else
        {
            FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
            return E_NOTIMPL;
        }
        break;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
    case DT_BIN_HEX:
        if (V_VT(&value) == (VT_UI1|VT_ARRAY))
        {
            UINT dim = SafeArrayGetDim(V_ARRAY(&value));
            LONG lbound, ubound;
            BSTR encoded;
            BYTE *ptr;
            int len;

            if (dim > 1)
                FIXME("unexpected array dimension count %u\n", dim);

            SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
            SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);

            len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));

            hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
            if (FAILED(hr)) return hr;

            hr = encode_binhex(ptr, len, &encoded);
            SafeArrayUnaccessData(V_ARRAY(&value));
            if (FAILED(hr)) return hr;

            hr = node_set_content(&This->node, encoded);
            SysFreeString(encoded);
        }
        else
        {
            FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
            return E_NOTIMPL;
        }
        break;
932
    default:
933
        FIXME("not implemented for dt:%s\n", debugstr_dt(dt));
934 935 936 937
        return E_NOTIMPL;
    }

    return hr;
938 939 940 941
}

static HRESULT WINAPI domelem_get_dataType(
    IXMLDOMElement *iface,
942
    VARIANT* typename)
943
{
944
    domelem *This = impl_from_IXMLDOMElement( iface );
945
    XDR_DT dt;
946 947 948

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

949 950
    if (!typename)
        return E_INVALIDARG;
951

952 953
    dt = element_get_dt(get_element(This));
    switch (dt)
954
    {
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
        case DT_BIN_BASE64:
        case DT_BIN_HEX:
        case DT_BOOLEAN:
        case DT_CHAR:
        case DT_DATE:
        case DT_DATE_TZ:
        case DT_DATETIME:
        case DT_DATETIME_TZ:
        case DT_FIXED_14_4:
        case DT_FLOAT:
        case DT_I1:
        case DT_I2:
        case DT_I4:
        case DT_I8:
        case DT_INT:
        case DT_NUMBER:
        case DT_R4:
        case DT_R8:
        case DT_TIME:
        case DT_TIME_TZ:
        case DT_UI1:
        case DT_UI2:
        case DT_UI4:
        case DT_UI8:
        case DT_URI:
        case DT_UUID:
            V_VT(typename) = VT_BSTR;
982
            V_BSTR(typename) = SysAllocString(dt_to_bstr(dt));
983 984 985 986 987 988 989 990 991 992

            if (!V_BSTR(typename))
                return E_OUTOFMEMORY;
            break;
        default:
            /* Other types (DTD equivalents) do not return anything here,
             * but the pointer part of the VARIANT is set to NULL */
            V_VT(typename) = VT_NULL;
            V_BSTR(typename) = NULL;
            break;
993 994
    }
    return (V_VT(typename) != VT_NULL) ? S_OK : S_FALSE;
995 996 997 998
}

static HRESULT WINAPI domelem_put_dataType(
    IXMLDOMElement *iface,
999
    BSTR dtName)
1000
{
1001
    domelem *This = impl_from_IXMLDOMElement( iface );
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 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
    HRESULT hr = E_FAIL;
    xmlChar *str;
    XDR_DT dt;

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

    if(dtName == NULL)
        return E_INVALIDARG;

    dt = bstr_to_dt(dtName, -1);

    /* An example of this is. The Text in the node needs to be a 0 or 1 for a boolean type.
       This applies to changing types (string->bool) or setting a new one
     */
    str = xmlNodeGetContent(get_element(This));
    hr = dt_validate(dt, str);
    xmlFree(str);

    /* Check all supported types. */
    if (hr == S_OK)
    {
        switch (dt)
        {
        case DT_BIN_BASE64:
        case DT_BIN_HEX:
        case DT_BOOLEAN:
        case DT_CHAR:
        case DT_DATE:
        case DT_DATE_TZ:
        case DT_DATETIME:
        case DT_DATETIME_TZ:
        case DT_FIXED_14_4:
        case DT_FLOAT:
        case DT_I1:
        case DT_I2:
        case DT_I4:
        case DT_I8:
        case DT_INT:
        case DT_NMTOKEN:
        case DT_NMTOKENS:
        case DT_NUMBER:
        case DT_R4:
        case DT_R8:
        case DT_STRING:
        case DT_TIME:
        case DT_TIME_TZ:
        case DT_UI1:
        case DT_UI2:
        case DT_UI4:
        case DT_UI8:
        case DT_URI:
        case DT_UUID:
            {
                xmlAttrPtr attr = xmlHasNsProp(get_element(This), DT_prefix, DT_nsURI);
                if (attr)
                {
                    attr = xmlSetNsProp(get_element(This), attr->ns, DT_prefix, dt_to_str(dt));
                    hr = S_OK;
                }
                else
                {
                    xmlNsPtr ns = xmlNewNs(get_element(This), DT_nsURI, DT_prefix);
                    if (ns)
                    {
                        attr = xmlNewNsProp(get_element(This), ns, DT_prefix, dt_to_str(dt));
                        if (attr)
                        {
                            xmlAddChild(get_element(This), (xmlNodePtr)attr);
                            hr = S_OK;
                        }
                        else
                            ERR("Failed to create Attribute\n");
                    }
                    else
                        ERR("Failed to create Namespace\n");
                }
            }
            break;
        default:
1081
            FIXME("need to handle dt:%s\n", debugstr_dt(dt));
1082 1083 1084 1085 1086
            break;
        }
    }

    return hr;
1087 1088 1089 1090 1091 1092
}

static HRESULT WINAPI domelem_get_xml(
    IXMLDOMElement *iface,
    BSTR* p)
{
1093
    domelem *This = impl_from_IXMLDOMElement( iface );
1094 1095 1096

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

1097
    return node_get_xml(&This->node, TRUE, p);
1098 1099 1100 1101
}

static HRESULT WINAPI domelem_transformNode(
    IXMLDOMElement *iface,
1102
    IXMLDOMNode *node, BSTR *p)
1103
{
1104
    domelem *This = impl_from_IXMLDOMElement( iface );
1105 1106
    TRACE("(%p)->(%p %p)\n", This, node, p);
    return node_transform_node(&This->node, node, p);
1107 1108 1109 1110 1111 1112
}

static HRESULT WINAPI domelem_selectNodes(
    IXMLDOMElement *iface,
    BSTR p, IXMLDOMNodeList** outList)
{
1113
    domelem *This = impl_from_IXMLDOMElement( iface );
1114 1115
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outList);
    return node_select_nodes(&This->node, p, outList);
1116 1117 1118 1119 1120 1121
}

static HRESULT WINAPI domelem_selectSingleNode(
    IXMLDOMElement *iface,
    BSTR p, IXMLDOMNode** outNode)
{
1122
    domelem *This = impl_from_IXMLDOMElement( iface );
1123 1124
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outNode);
    return node_select_singlenode(&This->node, p, outNode);
1125 1126 1127 1128
}

static HRESULT WINAPI domelem_get_parsed(
    IXMLDOMElement *iface,
1129
    VARIANT_BOOL* isParsed)
1130
{
1131
    domelem *This = impl_from_IXMLDOMElement( iface );
1132 1133 1134
    FIXME("(%p)->(%p) stub!\n", This, isParsed);
    *isParsed = VARIANT_TRUE;
    return S_OK;
1135 1136 1137 1138 1139 1140
}

static HRESULT WINAPI domelem_get_namespaceURI(
    IXMLDOMElement *iface,
    BSTR* p)
{
1141
    domelem *This = impl_from_IXMLDOMElement( iface );
1142 1143
    TRACE("(%p)->(%p)\n", This, p);
    return node_get_namespaceURI(&This->node, p);
1144 1145 1146 1147
}

static HRESULT WINAPI domelem_get_prefix(
    IXMLDOMElement *iface,
1148
    BSTR* prefix)
1149
{
1150
    domelem *This = impl_from_IXMLDOMElement( iface );
1151 1152
    TRACE("(%p)->(%p)\n", This, prefix);
    return node_get_prefix( &This->node, prefix );
1153 1154 1155 1156
}

static HRESULT WINAPI domelem_get_baseName(
    IXMLDOMElement *iface,
1157
    BSTR* name)
1158
{
1159
    domelem *This = impl_from_IXMLDOMElement( iface );
1160 1161
    TRACE("(%p)->(%p)\n", This, name);
    return node_get_base_name( &This->node, name );
1162 1163 1164 1165 1166 1167
}

static HRESULT WINAPI domelem_transformNodeToObject(
    IXMLDOMElement *iface,
    IXMLDOMNode* domNode, VARIANT var1)
{
1168
    domelem *This = impl_from_IXMLDOMElement( iface );
1169 1170
    FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
    return E_NOTIMPL;
1171 1172 1173 1174 1175 1176
}

static HRESULT WINAPI domelem_get_tagName(
    IXMLDOMElement *iface,
    BSTR* p)
{
1177 1178
    domelem *This = impl_from_IXMLDOMElement( iface );
    xmlNodePtr element;
1179 1180
    const xmlChar *prefix;
    xmlChar *qname;
1181

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

1184 1185
    if (!p) return E_INVALIDARG;

1186
    element = get_element( This );
1187 1188 1189
    if ( !element )
        return E_FAIL;

1190 1191
    prefix = element->ns ? element->ns->prefix : NULL;
    qname = xmlBuildQName(element->name, prefix, NULL, 0);
1192

1193 1194 1195 1196
    *p = bstr_from_xmlChar(qname);
    if (qname != element->name) xmlFree(qname);

    return *p ? S_OK : E_OUTOFMEMORY;
1197 1198 1199 1200
}

static HRESULT WINAPI domelem_getAttribute(
    IXMLDOMElement *iface,
1201
    BSTR name, VARIANT* value)
1202
{
1203 1204
    domelem *This = impl_from_IXMLDOMElement( iface );
    xmlNodePtr element;
1205
    xmlChar *xml_name, *xml_value = NULL;
1206
    xmlChar *local, *prefix;
1207
    HRESULT hr = S_FALSE;
1208
    xmlNsPtr ns;
1209

1210
    TRACE("(%p)->(%s %p)\n", This, debugstr_w(name), value);
1211

1212 1213 1214
    if(!value || !name)
        return E_INVALIDARG;

1215 1216 1217 1218
    element = get_element( This );
    if ( !element )
        return E_FAIL;

1219 1220 1221
    V_BSTR(value) = NULL;
    V_VT(value) = VT_NULL;

1222
    xml_name = xmlchar_from_wchar( name );
1223 1224 1225 1226

    if(!xmlValidateNameValue(xml_name))
        hr = E_FAIL;
    else
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    {
        if ((local = xmlSplitQName2(xml_name, &prefix)))
        {
            if (xmlStrEqual(prefix, BAD_CAST "xmlns"))
            {
                ns = xmlSearchNs(element->doc, element, local);
                if (ns)
                    xml_value = xmlStrdup(ns->href);
            }
            else
            {
                ns = xmlSearchNs(element->doc, element, prefix);
                if (ns)
                    xml_value = xmlGetNsProp(element, local, ns->href);
            }

            xmlFree(prefix);
            xmlFree(local);
        }
        else
            xml_value = xmlGetNsProp(element, xml_name, NULL);
    }
1249

1250
    heap_free(xml_name);
1251 1252 1253 1254 1255 1256 1257 1258 1259
    if(xml_value)
    {
        V_VT(value) = VT_BSTR;
        V_BSTR(value) = bstr_from_xmlChar( xml_value );
        xmlFree(xml_value);
        hr = S_OK;
    }

    return hr;
1260 1261 1262 1263
}

static HRESULT WINAPI domelem_setAttribute(
    IXMLDOMElement *iface,
1264
    BSTR name, VARIANT value)
1265
{
1266
    domelem *This = impl_from_IXMLDOMElement( iface );
1267
    xmlChar *xml_name, *xml_value, *local, *prefix;
1268
    xmlNodePtr element;
1269
    HRESULT hr = S_OK;
1270

1271
    TRACE("(%p)->(%s %s)\n", This, debugstr_w(name), debugstr_variant(&value));
1272 1273 1274 1275 1276

    element = get_element( This );
    if ( !element )
        return E_FAIL;

1277
    if (V_VT(&value) != VT_BSTR)
1278
    {
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
        VARIANT var;

        VariantInit(&var);
        hr = VariantChangeType(&var, &value, 0, VT_BSTR);
        if (hr != S_OK)
        {
            FIXME("VariantChangeType failed\n");
            return hr;
        }

        xml_value = xmlchar_from_wchar(V_BSTR(&var));
        VariantClear(&var);
1291
    }
1292 1293
    else
        xml_value = xmlchar_from_wchar(V_BSTR(&value));
1294

1295
    xml_name = xmlchar_from_wchar( name );
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    if ((local = xmlSplitQName2(xml_name, &prefix)))
    {
        static const xmlChar* xmlnsA = (const xmlChar*)"xmlns";
        xmlNsPtr ns = NULL;

        /* it's not allowed to modify existing namespace definition */
        if (xmlStrEqual(prefix, xmlnsA))
            ns = xmlSearchNs(element->doc, element, local);

        xmlFree(prefix);
        xmlFree(local);

        if (ns)
1310 1311 1312
        {
            int cmp = xmlStrEqual(ns->href, xml_value);
            heap_free(xml_value);
1313
            heap_free(xml_name);
1314 1315
            return cmp ? S_OK : E_INVALIDARG;
        }
1316 1317 1318
    }

    if (!xmlSetNsProp(element, NULL, xml_name, xml_value))
1319 1320
        hr = E_FAIL;

1321 1322
    heap_free(xml_value);
    heap_free(xml_name);
1323 1324

    return hr;
1325 1326 1327 1328 1329 1330
}

static HRESULT WINAPI domelem_removeAttribute(
    IXMLDOMElement *iface,
    BSTR p)
{
1331 1332 1333 1334
    domelem *This = impl_from_IXMLDOMElement( iface );
    IXMLDOMNamedNodeMap *attr;
    HRESULT hr;

1335
    TRACE("(%p)->(%s)\n", This, debugstr_w(p));
1336 1337 1338 1339 1340 1341 1342 1343

    hr = IXMLDOMElement_get_attributes(iface, &attr);
    if (hr != S_OK) return hr;

    hr = IXMLDOMNamedNodeMap_removeNamedItem(attr, p, NULL);
    IXMLDOMNamedNodeMap_Release(attr);

    return hr;
1344 1345 1346 1347 1348 1349
}

static HRESULT WINAPI domelem_getAttributeNode(
    IXMLDOMElement *iface,
    BSTR p, IXMLDOMAttribute** attributeNode )
{
1350
    domelem *This = impl_from_IXMLDOMElement( iface );
1351 1352
    xmlChar *local, *prefix, *nameA;
    HRESULT hr = S_FALSE;
1353 1354 1355 1356 1357
    xmlNodePtr element;
    xmlAttrPtr attr;

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

1358 1359 1360 1361 1362 1363 1364 1365 1366
    element = get_element( This );
    if (!element) return E_FAIL;

    if (attributeNode) *attributeNode = NULL;

    nameA = xmlchar_from_wchar(p);
    if (!xmlValidateNameValue(nameA))
    {
        heap_free(nameA);
1367
        return E_FAIL;
1368 1369 1370 1371 1372 1373 1374
    }

    if (!attributeNode)
    {
        heap_free(nameA);
        return S_FALSE;
    }
1375 1376 1377

    *attributeNode = NULL;

1378
    local = xmlSplitQName2(nameA, &prefix);
1379

1380 1381 1382 1383 1384
    if (local)
    {
        /* try to get namespace for supplied qualified name */
        xmlNsPtr ns = xmlSearchNs(element->doc, element, prefix);
        xmlFree(prefix);
1385

1386 1387 1388 1389
        attr = xmlHasNsProp(element, local, ns ? ns->href : NULL);
        xmlFree(local);
    }
    else
1390
    {
1391 1392 1393 1394
        attr = xmlHasProp(element, nameA);
        /* attribute has attached namespace and we requested non-qualified
           name - it's a failure case */
        if (attr && attr->ns) attr = NULL;
1395 1396
    }

1397 1398 1399 1400 1401
    heap_free(nameA);

    if (attr)
    {
        IUnknown *unk = create_attribute((xmlNodePtr)attr);
1402 1403 1404 1405 1406
        hr = IUnknown_QueryInterface(unk, &IID_IXMLDOMAttribute, (void**)attributeNode);
        IUnknown_Release(unk);
    }

    return hr;
1407 1408 1409 1410
}

static HRESULT WINAPI domelem_setAttributeNode(
    IXMLDOMElement *iface,
1411 1412
    IXMLDOMAttribute* attribute,
    IXMLDOMAttribute** old)
1413
{
1414
    domelem *This = impl_from_IXMLDOMElement( iface );
1415
    static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
1416 1417
    xmlChar *name, *value;
    BSTR nameW, prefix;
1418
    xmlnode *attr_node;
1419 1420 1421
    xmlAttrPtr attr;
    VARIANT valueW;
    HRESULT hr;
1422

1423
    FIXME("(%p)->(%p %p): semi-stub\n", This, attribute, old);
1424

1425
    if (!attribute) return E_INVALIDARG;
1426

1427
    attr_node = get_node_obj((IXMLDOMNode*)attribute);
1428
    if (!attr_node) return E_FAIL;
1429 1430 1431 1432 1433 1434 1435

    if (attr_node->parent)
    {
        WARN("attempt to add already used attribute\n");
        return E_FAIL;
    }

1436 1437 1438
    hr = IXMLDOMAttribute_get_nodeName(attribute, &nameW);
    if (hr != S_OK) return hr;

1439 1440 1441 1442 1443 1444 1445
    /* adding xmlns attribute doesn't change a tree or existing namespace definition */
    if (!strcmpW(nameW, xmlnsW))
    {
        SysFreeString(nameW);
        return DISP_E_UNKNOWNNAME;
    }

1446 1447 1448 1449 1450 1451 1452
    hr = IXMLDOMAttribute_get_nodeValue(attribute, &valueW);
    if (hr != S_OK)
    {
        SysFreeString(nameW);
        return hr;
    }

1453 1454
    if (old) *old = NULL;

1455 1456 1457 1458 1459 1460 1461 1462 1463
    TRACE("attribute: %s=%s\n", debugstr_w(nameW), debugstr_w(V_BSTR(&valueW)));

    hr = IXMLDOMAttribute_get_prefix(attribute, &prefix);
    if (hr == S_OK)
    {
        FIXME("namespaces not supported: %s\n", debugstr_w(prefix));
        SysFreeString(prefix);
    }

1464 1465
    name = xmlchar_from_wchar(nameW);
    value = xmlchar_from_wchar(V_BSTR(&valueW));
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476

    if (!name || !value)
    {
        SysFreeString(nameW);
        VariantClear(&valueW);
        heap_free(name);
        heap_free(value);
        return E_OUTOFMEMORY;
    }

    attr = xmlSetNsProp(get_element(This), NULL, name, value);
1477 1478
    if (attr)
        attr_node->parent = (IXMLDOMNode*)iface;
1479 1480 1481 1482 1483 1484 1485

    SysFreeString(nameW);
    VariantClear(&valueW);
    heap_free(name);
    heap_free(value);

    return attr ? S_OK : E_FAIL;
1486 1487 1488 1489 1490 1491 1492
}

static HRESULT WINAPI domelem_removeAttributeNode(
    IXMLDOMElement *iface,
    IXMLDOMAttribute* domAttribute,
    IXMLDOMAttribute** attributeNode)
{
1493 1494
    domelem *This = impl_from_IXMLDOMElement( iface );
    FIXME("(%p)->(%p %p)\n", This, domAttribute, attributeNode);
1495 1496 1497 1498 1499
    return E_NOTIMPL;
}

static HRESULT WINAPI domelem_getElementsByTagName(
    IXMLDOMElement *iface,
1500
    BSTR tagName, IXMLDOMNodeList** resultList)
1501
{
1502
    domelem *This = impl_from_IXMLDOMElement( iface );
1503
    xmlChar *query;
1504
    HRESULT hr;
1505
    BOOL XPath;
1506

1507
    TRACE("(%p)->(%s, %p)\n", This, debugstr_w(tagName), resultList);
1508

1509
    if (!tagName || !resultList) return E_INVALIDARG;
1510

1511 1512 1513
    XPath = is_xpathmode(get_element(This)->doc);
    set_xpathmode(get_element(This)->doc, TRUE);
    query = tagName_to_XPath(tagName);
1514
    hr = create_selection(get_element(This), query, resultList);
1515 1516
    xmlFree(query);
    set_xpathmode(get_element(This)->doc, XPath);
1517 1518

    return hr;
1519 1520 1521 1522 1523
}

static HRESULT WINAPI domelem_normalize(
    IXMLDOMElement *iface )
{
1524 1525
    domelem *This = impl_from_IXMLDOMElement( iface );
    FIXME("%p\n", This);
1526 1527 1528
    return E_NOTIMPL;
}

1529
static const struct IXMLDOMElementVtbl domelem_vtbl =
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 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
{
    domelem_QueryInterface,
    domelem_AddRef,
    domelem_Release,
    domelem_GetTypeInfoCount,
    domelem_GetTypeInfo,
    domelem_GetIDsOfNames,
    domelem_Invoke,
    domelem_get_nodeName,
    domelem_get_nodeValue,
    domelem_put_nodeValue,
    domelem_get_nodeType,
    domelem_get_parentNode,
    domelem_get_childNodes,
    domelem_get_firstChild,
    domelem_get_lastChild,
    domelem_get_previousSibling,
    domelem_get_nextSibling,
    domelem_get_attributes,
    domelem_insertBefore,
    domelem_replaceChild,
    domelem_removeChild,
    domelem_appendChild,
    domelem_hasChildNodes,
    domelem_get_ownerDocument,
    domelem_cloneNode,
    domelem_get_nodeTypeString,
    domelem_get_text,
    domelem_put_text,
    domelem_get_specified,
    domelem_get_definition,
    domelem_get_nodeTypedValue,
    domelem_put_nodeTypedValue,
    domelem_get_dataType,
    domelem_put_dataType,
    domelem_get_xml,
    domelem_transformNode,
    domelem_selectNodes,
    domelem_selectSingleNode,
    domelem_get_parsed,
    domelem_get_namespaceURI,
    domelem_get_prefix,
    domelem_get_baseName,
    domelem_transformNodeToObject,
    domelem_get_tagName,
    domelem_getAttribute,
    domelem_setAttribute,
    domelem_removeAttribute,
    domelem_getAttributeNode,
    domelem_setAttributeNode,
    domelem_removeAttributeNode,
    domelem_getElementsByTagName,
    domelem_normalize,
};

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
static HRESULT domelem_get_qualified_item(const xmlNodePtr node, BSTR name, BSTR uri,
    IXMLDOMNode **item)
{
    xmlAttrPtr attr;
    xmlChar *nameA;
    xmlChar *href;

    TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);

    if (!name || !item) return E_INVALIDARG;

    if (uri && *uri)
    {
        href = xmlchar_from_wchar(uri);
        if (!href) return E_OUTOFMEMORY;
    }
    else
        href = NULL;

    nameA = xmlchar_from_wchar(name);
    if (!nameA)
    {
        heap_free(href);
        return E_OUTOFMEMORY;
    }

    attr = xmlHasNsProp(node, nameA, href);

    heap_free(nameA);
    heap_free(href);

    if (!attr)
    {
        *item = NULL;
        return S_FALSE;
    }

    *item = create_node((xmlNodePtr)attr);

    return S_OK;
}

static HRESULT domelem_get_named_item(const xmlNodePtr node, BSTR name, IXMLDOMNode **item)
{
    xmlChar *nameA, *local, *prefix;
    BSTR uriW, localW;
    xmlNsPtr ns;
    HRESULT hr;

    TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item );

    nameA = xmlchar_from_wchar(name);
    local = xmlSplitQName2(nameA, &prefix);
    heap_free(nameA);

    if (!local)
        return domelem_get_qualified_item(node, name, NULL, item);

    /* try to get namespace uri for supplied qualified name */
    ns = xmlSearchNs(node->doc, node, prefix);

    xmlFree(prefix);

    if (!ns)
    {
        xmlFree(local);
        if (item) *item = NULL;
        return item ? S_FALSE : E_INVALIDARG;
    }

    uriW = bstr_from_xmlChar(ns->href);
    localW = bstr_from_xmlChar(local);
    xmlFree(local);

    TRACE("got qualified node %s, uri=%s\n", debugstr_w(localW), debugstr_w(uriW));

    hr = domelem_get_qualified_item(node, localW, uriW, item);

    SysFreeString(localW);
    SysFreeString(uriW);

    return hr;
}

static HRESULT domelem_set_named_item(xmlNodePtr node, IXMLDOMNode *newItem, IXMLDOMNode **namedItem)
{
    xmlNodePtr nodeNew;
    xmlnode *ThisNew;

    TRACE("(%p)->(%p %p)\n", node, newItem, namedItem );

    if(!newItem)
        return E_INVALIDARG;

    if(namedItem) *namedItem = NULL;

    /* Must be an Attribute */
    ThisNew = get_node_obj( newItem );
    if(!ThisNew) return E_FAIL;

    if(ThisNew->node->type != XML_ATTRIBUTE_NODE)
        return E_FAIL;

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

    nodeNew = xmlAddChild(node, ThisNew->node);

    if(namedItem)
        *namedItem = create_node( nodeNew );
    return S_OK;
}

static HRESULT domelem_remove_qualified_item(xmlNodePtr node, BSTR name, BSTR uri, IXMLDOMNode **item)
{
    xmlChar *nameA, *href;
    xmlAttrPtr attr;

    TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);

    if (!name) return E_INVALIDARG;

    if (uri && *uri)
    {
        href = xmlchar_from_wchar(uri);
        if (!href) return E_OUTOFMEMORY;
    }
    else
        href = NULL;

    nameA = xmlchar_from_wchar(name);
    if (!nameA)
    {
        heap_free(href);
        return E_OUTOFMEMORY;
    }

    attr = xmlHasNsProp(node, nameA, href);

    heap_free(nameA);
    heap_free(href);

    if (!attr)
    {
        if (item) *item = NULL;
        return S_FALSE;
    }

    if (item)
    {
        xmlUnlinkNode( (xmlNodePtr) attr );
        xmldoc_add_orphan( attr->doc, (xmlNodePtr) attr );
        *item = create_node( (xmlNodePtr) attr );
    }
    else
    {
        if (xmlRemoveProp(attr) == -1)
            ERR("xmlRemoveProp failed\n");
    }

    return S_OK;
}

static HRESULT domelem_remove_named_item(xmlNodePtr node, BSTR name, IXMLDOMNode **item)
{
    TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item);
    return domelem_remove_qualified_item(node, name, NULL, item);
}

static HRESULT domelem_get_item(const xmlNodePtr node, LONG index, IXMLDOMNode **item)
{
    xmlAttrPtr curr;
    LONG attrIndex;

    TRACE("(%p)->(%d %p)\n", node, index, item);

    *item = NULL;

    if (index < 0)
        return S_FALSE;

    curr = node->properties;

    for (attrIndex = 0; attrIndex < index; attrIndex++) {
        if (curr->next == NULL)
            return S_FALSE;
        else
            curr = curr->next;
    }

    *item = create_node( (xmlNodePtr) curr );

    return S_OK;
}

static HRESULT domelem_get_length(const xmlNodePtr node, LONG *length)
{
    xmlAttrPtr first;
    xmlAttrPtr curr;
    LONG attrCount;

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

    if( !length )
        return E_INVALIDARG;

    first = node->properties;
    if (first == NULL) {
	*length = 0;
	return S_OK;
    }

    curr = first;
    attrCount = 1;
    while (curr->next) {
        attrCount++;
        curr = curr->next;
    }
    *length = attrCount;

    return S_OK;
}

static HRESULT domelem_next_node(const xmlNodePtr node, LONG *iter, IXMLDOMNode **nextNode)
{
    xmlAttrPtr curr;
    LONG i;

    TRACE("(%p)->(%d: %p)\n", node, *iter, nextNode);

    *nextNode = NULL;

    curr = node->properties;

    for (i = 0; i < *iter; i++) {
        if (curr->next == NULL)
            return S_FALSE;
        else
            curr = curr->next;
    }

    (*iter)++;
    *nextNode = create_node((xmlNodePtr)curr);

    return S_OK;
}

static const struct nodemap_funcs domelem_attr_map = {
    domelem_get_named_item,
    domelem_set_named_item,
    domelem_remove_named_item,
    domelem_get_item,
    domelem_get_length,
    domelem_get_qualified_item,
    domelem_remove_qualified_item,
    domelem_next_node
};

1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
static const tid_t domelem_iface_tids[] = {
    IXMLDOMElement_tid,
    0
};

static dispex_static_data_t domelem_dispex = {
    NULL,
    IXMLDOMElement_tid,
    NULL,
    domelem_iface_tids
};

1856
IUnknown* create_element( xmlNodePtr element )
1857
{
1858
    domelem *This;
1859

1860
    This = heap_alloc( sizeof *This );
1861 1862
    if ( !This )
        return NULL;
1863

1864
    This->IXMLDOMElement_iface.lpVtbl = &domelem_vtbl;
1865
    This->ref = 1;
1866

1867
    init_xmlnode(&This->node, element, (IXMLDOMNode*)&This->IXMLDOMElement_iface, &domelem_dispex);
1868

1869
    return (IUnknown*)&This->IXMLDOMElement_iface;
1870
}
1871 1872

#endif