2 * DOM Document implementation
4 * Copyright 2005 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * 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 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
38 #include "msxml_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
46 static const xmlChar DT_prefix[] = "dt";
47 static const xmlChar DT_nsURI[] = "urn:schemas-microsoft-com:datatypes";
49 typedef struct _domelem
52 IXMLDOMElement IXMLDOMElement_iface;
56 static const struct nodemap_funcs domelem_attr_map;
58 static const tid_t domelem_se_tids[] = {
64 static inline domelem *impl_from_IXMLDOMElement( IXMLDOMElement *iface )
66 return CONTAINING_RECORD(iface, domelem, IXMLDOMElement_iface);
69 static inline xmlNodePtr get_element( const domelem *This )
71 return This->node.node;
74 static HRESULT WINAPI domelem_QueryInterface(
75 IXMLDOMElement *iface,
79 domelem *This = impl_from_IXMLDOMElement( iface );
81 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
83 if ( IsEqualGUID( riid, &IID_IXMLDOMElement ) ||
84 IsEqualGUID( riid, &IID_IXMLDOMNode ) ||
85 IsEqualGUID( riid, &IID_IDispatch ) ||
86 IsEqualGUID( riid, &IID_IUnknown ) )
88 *ppvObject = &This->IXMLDOMElement_iface;
90 else if(node_query_interface(&This->node, riid, ppvObject))
92 return *ppvObject ? S_OK : E_NOINTERFACE;
94 else if(IsEqualGUID( riid, &IID_ISupportErrorInfo ))
96 return node_create_supporterrorinfo(domelem_se_tids, ppvObject);
100 TRACE("interface %s not implemented\n", debugstr_guid(riid));
102 return E_NOINTERFACE;
105 IUnknown_AddRef( (IUnknown*)*ppvObject );
109 static ULONG WINAPI domelem_AddRef(
110 IXMLDOMElement *iface )
112 domelem *This = impl_from_IXMLDOMElement( iface );
113 LONG ref = InterlockedIncrement(&This->ref);
115 TRACE("(%p)->(%d)\n", This, ref);
120 static ULONG WINAPI domelem_Release(
121 IXMLDOMElement *iface )
123 domelem *This = impl_from_IXMLDOMElement( iface );
124 ULONG ref = InterlockedDecrement(&This->ref);
126 TRACE("(%p)->(%d)\n", This, ref);
129 destroy_xmlnode(&This->node);
136 static HRESULT WINAPI domelem_GetTypeInfoCount(
137 IXMLDOMElement *iface,
140 domelem *This = impl_from_IXMLDOMElement( iface );
141 return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
144 static HRESULT WINAPI domelem_GetTypeInfo(
145 IXMLDOMElement *iface,
146 UINT iTInfo, LCID lcid,
147 ITypeInfo** ppTInfo )
149 domelem *This = impl_from_IXMLDOMElement( iface );
150 return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface,
151 iTInfo, lcid, ppTInfo);
154 static HRESULT WINAPI domelem_GetIDsOfNames(
155 IXMLDOMElement *iface,
156 REFIID riid, LPOLESTR* rgszNames,
157 UINT cNames, LCID lcid, DISPID* rgDispId )
159 domelem *This = impl_from_IXMLDOMElement( iface );
160 return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface,
161 riid, rgszNames, cNames, lcid, rgDispId);
164 static HRESULT WINAPI domelem_Invoke(
165 IXMLDOMElement *iface,
166 DISPID dispIdMember, REFIID riid, LCID lcid,
167 WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
168 EXCEPINFO* pExcepInfo, UINT* puArgErr )
170 domelem *This = impl_from_IXMLDOMElement( iface );
171 return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface,
172 dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
175 static HRESULT WINAPI domelem_get_nodeName(
176 IXMLDOMElement *iface,
179 domelem *This = impl_from_IXMLDOMElement( iface );
181 TRACE("(%p)->(%p)\n", This, p);
183 return node_get_nodeName(&This->node, p);
186 static HRESULT WINAPI domelem_get_nodeValue(
187 IXMLDOMElement *iface,
190 domelem *This = impl_from_IXMLDOMElement( iface );
192 TRACE("(%p)->(%p)\n", This, value);
197 V_VT(value) = VT_NULL;
198 V_BSTR(value) = NULL; /* tests show that we should do this */
202 static HRESULT WINAPI domelem_put_nodeValue(
203 IXMLDOMElement *iface,
206 domelem *This = impl_from_IXMLDOMElement( iface );
207 TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
211 static HRESULT WINAPI domelem_get_nodeType(
212 IXMLDOMElement *iface,
213 DOMNodeType* domNodeType )
215 domelem *This = impl_from_IXMLDOMElement( iface );
217 TRACE("(%p)->(%p)\n", This, domNodeType);
219 *domNodeType = NODE_ELEMENT;
223 static HRESULT WINAPI domelem_get_parentNode(
224 IXMLDOMElement *iface,
225 IXMLDOMNode** parent )
227 domelem *This = impl_from_IXMLDOMElement( iface );
229 TRACE("(%p)->(%p)\n", This, parent);
231 return node_get_parent(&This->node, parent);
234 static HRESULT WINAPI domelem_get_childNodes(
235 IXMLDOMElement *iface,
236 IXMLDOMNodeList** outList)
238 domelem *This = impl_from_IXMLDOMElement( iface );
240 TRACE("(%p)->(%p)\n", This, outList);
242 return node_get_child_nodes(&This->node, outList);
245 static HRESULT WINAPI domelem_get_firstChild(
246 IXMLDOMElement *iface,
247 IXMLDOMNode** domNode)
249 domelem *This = impl_from_IXMLDOMElement( iface );
251 TRACE("(%p)->(%p)\n", This, domNode);
253 return node_get_first_child(&This->node, domNode);
256 static HRESULT WINAPI domelem_get_lastChild(
257 IXMLDOMElement *iface,
258 IXMLDOMNode** domNode)
260 domelem *This = impl_from_IXMLDOMElement( iface );
262 TRACE("(%p)->(%p)\n", This, domNode);
264 return node_get_last_child(&This->node, domNode);
267 static HRESULT WINAPI domelem_get_previousSibling(
268 IXMLDOMElement *iface,
269 IXMLDOMNode** domNode)
271 domelem *This = impl_from_IXMLDOMElement( iface );
273 TRACE("(%p)->(%p)\n", This, domNode);
275 return node_get_previous_sibling(&This->node, domNode);
278 static HRESULT WINAPI domelem_get_nextSibling(
279 IXMLDOMElement *iface,
280 IXMLDOMNode** domNode)
282 domelem *This = impl_from_IXMLDOMElement( iface );
284 TRACE("(%p)->(%p)\n", This, domNode);
286 return node_get_next_sibling(&This->node, domNode);
289 static HRESULT WINAPI domelem_get_attributes(
290 IXMLDOMElement *iface,
291 IXMLDOMNamedNodeMap** map)
293 domelem *This = impl_from_IXMLDOMElement( iface );
295 TRACE("(%p)->(%p)\n", This, map);
297 *map = create_nodemap(This->node.node, &domelem_attr_map);
301 static HRESULT WINAPI domelem_insertBefore(
302 IXMLDOMElement *iface,
303 IXMLDOMNode* newNode, VARIANT refChild,
304 IXMLDOMNode** outOldNode)
306 domelem *This = impl_from_IXMLDOMElement( iface );
308 TRACE("(%p)->(%p %s %p)\n", This, newNode, debugstr_variant(&refChild), outOldNode);
310 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
313 static HRESULT WINAPI domelem_replaceChild(
314 IXMLDOMElement *iface,
315 IXMLDOMNode* newNode,
316 IXMLDOMNode* oldNode,
317 IXMLDOMNode** outOldNode)
319 domelem *This = impl_from_IXMLDOMElement( iface );
321 TRACE("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
323 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
326 static HRESULT WINAPI domelem_removeChild(
327 IXMLDOMElement *iface,
328 IXMLDOMNode *child, IXMLDOMNode **oldChild)
330 domelem *This = impl_from_IXMLDOMElement( iface );
331 TRACE("(%p)->(%p %p)\n", This, child, oldChild);
332 return node_remove_child(&This->node, child, oldChild);
335 static HRESULT WINAPI domelem_appendChild(
336 IXMLDOMElement *iface,
337 IXMLDOMNode *child, IXMLDOMNode **outChild)
339 domelem *This = impl_from_IXMLDOMElement( iface );
340 TRACE("(%p)->(%p %p)\n", This, child, outChild);
341 return node_append_child(&This->node, child, outChild);
344 static HRESULT WINAPI domelem_hasChildNodes(
345 IXMLDOMElement *iface,
348 domelem *This = impl_from_IXMLDOMElement( iface );
349 TRACE("(%p)->(%p)\n", This, ret);
350 return node_has_childnodes(&This->node, ret);
353 static HRESULT WINAPI domelem_get_ownerDocument(
354 IXMLDOMElement *iface,
355 IXMLDOMDocument **doc)
357 domelem *This = impl_from_IXMLDOMElement( iface );
358 TRACE("(%p)->(%p)\n", This, doc);
359 return node_get_owner_doc(&This->node, doc);
362 static HRESULT WINAPI domelem_cloneNode(
363 IXMLDOMElement *iface,
364 VARIANT_BOOL deep, IXMLDOMNode** outNode)
366 domelem *This = impl_from_IXMLDOMElement( iface );
367 TRACE("(%p)->(%d %p)\n", This, deep, outNode);
368 return node_clone( &This->node, deep, outNode );
371 static HRESULT WINAPI domelem_get_nodeTypeString(
372 IXMLDOMElement *iface,
375 domelem *This = impl_from_IXMLDOMElement( iface );
376 static const WCHAR elementW[] = {'e','l','e','m','e','n','t',0};
378 TRACE("(%p)->(%p)\n", This, p);
380 return return_bstr(elementW, p);
383 static HRESULT WINAPI domelem_get_text(
384 IXMLDOMElement *iface,
387 domelem *This = impl_from_IXMLDOMElement( iface );
388 TRACE("(%p)->(%p)\n", This, p);
389 return node_get_text(&This->node, p);
392 static HRESULT WINAPI domelem_put_text(
393 IXMLDOMElement *iface,
396 domelem *This = impl_from_IXMLDOMElement( iface );
397 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
398 return node_put_text( &This->node, p );
401 static HRESULT WINAPI domelem_get_specified(
402 IXMLDOMElement *iface,
403 VARIANT_BOOL* isSpecified)
405 domelem *This = impl_from_IXMLDOMElement( iface );
406 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
407 *isSpecified = VARIANT_TRUE;
411 static HRESULT WINAPI domelem_get_definition(
412 IXMLDOMElement *iface,
413 IXMLDOMNode** definitionNode)
415 domelem *This = impl_from_IXMLDOMElement( iface );
416 FIXME("(%p)->(%p)\n", This, definitionNode);
420 static inline BYTE hex_to_byte(xmlChar c)
422 if(c <= '9') return c-'0';
423 if(c <= 'F') return c-'A'+10;
427 static inline BYTE base64_to_byte(xmlChar c)
429 if(c == '+') return 62;
430 if(c == '/') return 63;
431 if(c <= '9') return c-'0'+52;
432 if(c <= 'Z') return c-'A';
436 static inline HRESULT variant_from_dt(XDR_DT dt, xmlChar* str, VARIANT* v)
440 BOOL handled = FALSE;
455 V_BSTR(v) = bstr_from_xmlChar(str);
458 return E_OUTOFMEMORY;
476 st.wDayOfWeek = st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
478 V_VT(&src) = VT_BSTR;
479 V_BSTR(&src) = bstr_from_xmlChar(str);
482 return E_OUTOFMEMORY;
485 e = p + SysStringLen(V_BSTR(&src));
487 if(p+4<e && *(p+4)=='-') /* parse date (yyyy-mm-dd) */
490 st.wMonth = atoiW(p+5);
491 st.wDay = atoiW(p+8);
497 if(p+2<e && *(p+2)==':') /* parse time (hh:mm:ss.?) */
500 st.wMinute = atoiW(p+3);
501 st.wSecond = atoiW(p+6);
507 while(isdigitW(*p)) p++;
511 SystemTimeToVariantTime(&st, &date);
515 if(*p == '+') /* parse timezone offset (+hh:mm) */
516 V_DATE(v) += (DOUBLE)atoiW(p+1)/24 + (DOUBLE)atoiW(p+4)/1440;
517 else if(*p == '-') /* parse timezone offset (-hh:mm) */
518 V_DATE(v) -= (DOUBLE)atoiW(p+1)/24 + (DOUBLE)atoiW(p+4)/1440;
529 len = xmlStrlen(str)/2;
533 V_VT(v) = (VT_ARRAY|VT_UI1);
534 V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);
537 return E_OUTOFMEMORY;
540 ((BYTE*)V_ARRAY(v)->pvData)[i] = (hex_to_byte(str[2*i])<<4)
541 + hex_to_byte(str[2*i+1]);
551 /* remove all formatting chars */
556 if ( *c2 == ' ' || *c2 == '\t' ||
557 *c2 == '\n' || *c2 == '\r' )
567 if(str[len-2] == '=') i = 2;
568 else if(str[len-1] == '=') i = 1;
572 sab.cElements = len/4*3-i;
574 V_VT(v) = (VT_ARRAY|VT_UI1);
575 V_ARRAY(v) = SafeArrayCreate(VT_UI1, 1, &sab);
578 return E_OUTOFMEMORY;
580 for(i=0; i<len/4; i++)
582 ((BYTE*)V_ARRAY(v)->pvData)[3*i] = (base64_to_byte(str[4*i])<<2)
583 + (base64_to_byte(str[4*i+1])>>4);
584 if(3*i+1 < sab.cElements)
585 ((BYTE*)V_ARRAY(v)->pvData)[3*i+1] = (base64_to_byte(str[4*i+1])<<4)
586 + (base64_to_byte(str[4*i+2])>>2);
587 if(3*i+2 < sab.cElements)
588 ((BYTE*)V_ARRAY(v)->pvData)[3*i+2] = (base64_to_byte(str[4*i+2])<<6)
589 + base64_to_byte(str[4*i+3]);
640 FIXME("need to handle dt:%s\n", debugstr_dt(dt));
642 V_BSTR(v) = bstr_from_xmlChar(str);
644 return E_OUTOFMEMORY;
648 WARN("unknown type %d\n", dt);
653 V_VT(&src) = VT_BSTR;
654 V_BSTR(&src) = bstr_from_xmlChar(str);
657 return E_OUTOFMEMORY;
659 hr = VariantChangeTypeEx(v, &src,
660 MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),0, V_VT(v));
666 static XDR_DT element_get_dt(xmlNodePtr node)
668 XDR_DT dt = DT_INVALID;
670 TRACE("(%p)\n", node);
671 if(node->type != XML_ELEMENT_NODE)
673 FIXME("invalid element node\n");
677 if (node->ns && xmlStrEqual(node->ns->href, DT_nsURI))
679 dt = str_to_dt(node->name, -1);
683 xmlChar* pVal = xmlGetNsProp(node, BAD_CAST "dt", DT_nsURI);
686 dt = str_to_dt(pVal, -1);
691 IXMLDOMDocument3* doc = (IXMLDOMDocument3*)create_domdoc((xmlNodePtr)node->doc);
697 if (IXMLDOMDocument3_get_schemas(doc, &v) == S_OK &&
698 V_VT(&v) == VT_DISPATCH)
700 dt = SchemaCache_get_node_dt((IXMLDOMSchemaCollection2*)V_DISPATCH(&v), node);
703 IXMLDOMDocument3_Release(doc);
708 TRACE("=> dt:%s\n", debugstr_dt(dt));
712 static HRESULT WINAPI domelem_get_nodeTypedValue(
713 IXMLDOMElement *iface,
716 domelem *This = impl_from_IXMLDOMElement( iface );
721 TRACE("(%p)->(%p)\n", This, v);
723 if(!v) return E_INVALIDARG;
727 dt = element_get_dt(get_element(This));
728 content = xmlNodeGetContent(get_element(This));
729 hr = variant_from_dt(dt, content, v);
735 static HRESULT encode_base64(const BYTE *buf, int len, BSTR *ret)
737 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
739 int bytes, pad_bytes, div, i;
743 bytes = (len*8 + 5)/6;
744 pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
746 TRACE("%d, bytes is %d, pad bytes is %d\n", len, bytes, pad_bytes);
747 needed = bytes + pad_bytes + 1;
749 *ret = SysAllocStringLen(NULL, needed);
750 if (!*ret) return E_OUTOFMEMORY;
752 /* Three bytes of input give 4 chars of output */
759 /* first char is the first 6 bits of the first byte*/
760 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
761 /* second char is the last 2 bits of the first byte and the first 4
762 * bits of the second byte */
763 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
764 /* third char is the last 4 bits of the second byte and the first 2
765 * bits of the third byte */
766 *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
767 /* fourth char is the remaining 6 bits of the third byte */
768 *ptr++ = b64[ d[2] & 0x3f];
777 /* first char is the first 6 bits of the first byte*/
778 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
779 /* second char is the last 2 bits of the first byte and the first 4
780 * bits of the second byte */
781 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
782 /* third char is the last 4 bits of the second byte padded with
784 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
785 /* fourth char is a = to indicate one byte of padding */
789 /* first char is the first 6 bits of the first byte*/
790 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
791 /* second char is the last 2 bits of the first byte padded with
793 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
794 /* third char is = to indicate padding */
796 /* fourth char is = to indicate padding */
804 static HRESULT encode_binhex(const BYTE *buf, int len, BSTR *ret)
806 static const char byte_to_hex[16] = "0123456789abcdef";
809 *ret = SysAllocStringLen(NULL, len*2);
810 if (!*ret) return E_OUTOFMEMORY;
812 for (i = 0; i < len; i++)
814 (*ret)[2*i] = byte_to_hex[buf[i] >> 4];
815 (*ret)[2*i+1] = byte_to_hex[0x0f & buf[i]];
821 static HRESULT WINAPI domelem_put_nodeTypedValue(
822 IXMLDOMElement *iface,
825 domelem *This = impl_from_IXMLDOMElement( iface );
829 TRACE("(%p)->(%s)\n", This, debugstr_variant(&value));
831 dt = element_get_dt(get_element(This));
834 /* for untyped node coerce to BSTR and set */
836 if (V_VT(&value) != VT_BSTR)
839 VariantInit(&content);
840 hr = VariantChangeType(&content, &value, 0, VT_BSTR);
843 hr = node_set_content(&This->node, V_BSTR(&content));
844 VariantClear(&content);
848 hr = node_set_content(&This->node, V_BSTR(&value));
851 if (V_VT(&value) == VT_BSTR)
852 hr = node_set_content(&This->node, V_BSTR(&value));
853 else if (V_VT(&value) == (VT_UI1|VT_ARRAY))
855 UINT dim = SafeArrayGetDim(V_ARRAY(&value));
862 FIXME("unexpected array dimension count %u\n", dim);
864 SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
865 SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);
867 len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));
869 hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
870 if (FAILED(hr)) return hr;
872 hr = encode_base64(ptr, len, &encoded);
873 SafeArrayUnaccessData(V_ARRAY(&value));
874 if (FAILED(hr)) return hr;
876 hr = node_set_content(&This->node, encoded);
877 SysFreeString(encoded);
881 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
886 if (V_VT(&value) == (VT_UI1|VT_ARRAY))
888 UINT dim = SafeArrayGetDim(V_ARRAY(&value));
895 FIXME("unexpected array dimension count %u\n", dim);
897 SafeArrayGetUBound(V_ARRAY(&value), 1, &ubound);
898 SafeArrayGetLBound(V_ARRAY(&value), 1, &lbound);
900 len = (ubound - lbound + 1)*SafeArrayGetElemsize(V_ARRAY(&value));
902 hr = SafeArrayAccessData(V_ARRAY(&value), (void*)&ptr);
903 if (FAILED(hr)) return hr;
905 hr = encode_binhex(ptr, len, &encoded);
906 SafeArrayUnaccessData(V_ARRAY(&value));
907 if (FAILED(hr)) return hr;
909 hr = node_set_content(&This->node, encoded);
910 SysFreeString(encoded);
914 FIXME("unhandled variant type %d for dt:%s\n", V_VT(&value), debugstr_dt(dt));
919 FIXME("not implemented for dt:%s\n", debugstr_dt(dt));
926 static HRESULT WINAPI domelem_get_dataType(
927 IXMLDOMElement *iface,
930 domelem *This = impl_from_IXMLDOMElement( iface );
933 TRACE("(%p)->(%p)\n", This, typename);
938 dt = element_get_dt(get_element(This));
967 V_VT(typename) = VT_BSTR;
968 V_BSTR(typename) = SysAllocString(dt_to_bstr(dt));
970 if (!V_BSTR(typename))
971 return E_OUTOFMEMORY;
974 /* Other types (DTD equivalents) do not return anything here,
975 * but the pointer part of the VARIANT is set to NULL */
976 V_VT(typename) = VT_NULL;
977 V_BSTR(typename) = NULL;
980 return (V_VT(typename) != VT_NULL) ? S_OK : S_FALSE;
983 static HRESULT WINAPI domelem_put_dataType(
984 IXMLDOMElement *iface,
987 domelem *This = impl_from_IXMLDOMElement( iface );
992 TRACE("(%p)->(%s)\n", This, debugstr_w(dtName));
997 dt = bstr_to_dt(dtName, -1);
999 /* An example of this is. The Text in the node needs to be a 0 or 1 for a boolean type.
1000 This applies to changing types (string->bool) or setting a new one
1002 str = xmlNodeGetContent(get_element(This));
1003 hr = dt_validate(dt, str);
1006 /* Check all supported types. */
1018 case DT_DATETIME_TZ:
1041 xmlAttrPtr attr = xmlHasNsProp(get_element(This), DT_prefix, DT_nsURI);
1044 attr = xmlSetNsProp(get_element(This), attr->ns, DT_prefix, dt_to_str(dt));
1049 xmlNsPtr ns = xmlNewNs(get_element(This), DT_nsURI, DT_prefix);
1052 attr = xmlNewNsProp(get_element(This), ns, DT_prefix, dt_to_str(dt));
1055 xmlAddChild(get_element(This), (xmlNodePtr)attr);
1059 ERR("Failed to create Attribute\n");
1062 ERR("Failed to create Namespace\n");
1067 FIXME("need to handle dt:%s\n", debugstr_dt(dt));
1075 static HRESULT WINAPI domelem_get_xml(
1076 IXMLDOMElement *iface,
1079 domelem *This = impl_from_IXMLDOMElement( iface );
1081 TRACE("(%p)->(%p)\n", This, p);
1083 return node_get_xml(&This->node, TRUE, p);
1086 static HRESULT WINAPI domelem_transformNode(
1087 IXMLDOMElement *iface,
1088 IXMLDOMNode *node, BSTR *p)
1090 domelem *This = impl_from_IXMLDOMElement( iface );
1091 TRACE("(%p)->(%p %p)\n", This, node, p);
1092 return node_transform_node(&This->node, node, p);
1095 static HRESULT WINAPI domelem_selectNodes(
1096 IXMLDOMElement *iface,
1097 BSTR p, IXMLDOMNodeList** outList)
1099 domelem *This = impl_from_IXMLDOMElement( iface );
1100 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outList);
1101 return node_select_nodes(&This->node, p, outList);
1104 static HRESULT WINAPI domelem_selectSingleNode(
1105 IXMLDOMElement *iface,
1106 BSTR p, IXMLDOMNode** outNode)
1108 domelem *This = impl_from_IXMLDOMElement( iface );
1109 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), outNode);
1110 return node_select_singlenode(&This->node, p, outNode);
1113 static HRESULT WINAPI domelem_get_parsed(
1114 IXMLDOMElement *iface,
1115 VARIANT_BOOL* isParsed)
1117 domelem *This = impl_from_IXMLDOMElement( iface );
1118 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1119 *isParsed = VARIANT_TRUE;
1123 static HRESULT WINAPI domelem_get_namespaceURI(
1124 IXMLDOMElement *iface,
1127 domelem *This = impl_from_IXMLDOMElement( iface );
1128 TRACE("(%p)->(%p)\n", This, p);
1129 return node_get_namespaceURI(&This->node, p);
1132 static HRESULT WINAPI domelem_get_prefix(
1133 IXMLDOMElement *iface,
1136 domelem *This = impl_from_IXMLDOMElement( iface );
1137 TRACE("(%p)->(%p)\n", This, prefix);
1138 return node_get_prefix( &This->node, prefix );
1141 static HRESULT WINAPI domelem_get_baseName(
1142 IXMLDOMElement *iface,
1145 domelem *This = impl_from_IXMLDOMElement( iface );
1146 TRACE("(%p)->(%p)\n", This, name);
1147 return node_get_base_name( &This->node, name );
1150 static HRESULT WINAPI domelem_transformNodeToObject(
1151 IXMLDOMElement *iface,
1152 IXMLDOMNode* domNode, VARIANT var1)
1154 domelem *This = impl_from_IXMLDOMElement( iface );
1155 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
1159 static HRESULT WINAPI domelem_get_tagName(
1160 IXMLDOMElement *iface,
1163 domelem *This = impl_from_IXMLDOMElement( iface );
1165 const xmlChar *prefix;
1168 TRACE("(%p)->(%p)\n", This, p );
1170 if (!p) return E_INVALIDARG;
1172 element = get_element( This );
1176 prefix = element->ns ? element->ns->prefix : NULL;
1177 qname = xmlBuildQName(element->name, prefix, NULL, 0);
1179 *p = bstr_from_xmlChar(qname);
1180 if (qname != element->name) xmlFree(qname);
1182 return *p ? S_OK : E_OUTOFMEMORY;
1185 static HRESULT WINAPI domelem_getAttribute(
1186 IXMLDOMElement *iface,
1187 BSTR name, VARIANT* value)
1189 domelem *This = impl_from_IXMLDOMElement( iface );
1191 xmlChar *xml_name, *xml_value = NULL;
1192 HRESULT hr = S_FALSE;
1194 TRACE("(%p)->(%s %p)\n", This, debugstr_w(name), value);
1197 return E_INVALIDARG;
1199 element = get_element( This );
1203 V_BSTR(value) = NULL;
1204 V_VT(value) = VT_NULL;
1206 xml_name = xmlchar_from_wchar( name );
1208 if(!xmlValidateNameValue(xml_name))
1211 xml_value = xmlGetNsProp(element, xml_name, NULL);
1213 heap_free(xml_name);
1216 V_VT(value) = VT_BSTR;
1217 V_BSTR(value) = bstr_from_xmlChar( xml_value );
1225 static HRESULT WINAPI domelem_setAttribute(
1226 IXMLDOMElement *iface,
1227 BSTR name, VARIANT value)
1229 domelem *This = impl_from_IXMLDOMElement( iface );
1230 xmlChar *xml_name, *xml_value, *local, *prefix;
1235 TRACE("(%p)->(%s %s)\n", This, debugstr_w(name), debugstr_variant(&value));
1237 element = get_element( This );
1242 hr = VariantChangeType(&var, &value, 0, VT_BSTR);
1245 FIXME("VariantChangeType failed\n");
1249 xml_name = xmlchar_from_wchar( name );
1250 xml_value = xmlchar_from_wchar( V_BSTR(&var) );
1252 if ((local = xmlSplitQName2(xml_name, &prefix)))
1254 static const xmlChar* xmlnsA = (const xmlChar*)"xmlns";
1257 /* it's not allowed to modify existing namespace definition */
1258 if (xmlStrEqual(prefix, xmlnsA))
1259 ns = xmlSearchNs(element->doc, element, local);
1265 return xmlStrEqual(ns->href, xml_value) ? S_OK : E_INVALIDARG;
1268 if (!xmlSetNsProp(element, NULL, xml_name, xml_value))
1271 heap_free(xml_value);
1272 heap_free(xml_name);
1278 static HRESULT WINAPI domelem_removeAttribute(
1279 IXMLDOMElement *iface,
1282 domelem *This = impl_from_IXMLDOMElement( iface );
1283 IXMLDOMNamedNodeMap *attr;
1286 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
1288 hr = IXMLDOMElement_get_attributes(iface, &attr);
1289 if (hr != S_OK) return hr;
1291 hr = IXMLDOMNamedNodeMap_removeNamedItem(attr, p, NULL);
1292 IXMLDOMNamedNodeMap_Release(attr);
1297 static HRESULT WINAPI domelem_getAttributeNode(
1298 IXMLDOMElement *iface,
1299 BSTR p, IXMLDOMAttribute** attributeNode )
1301 domelem *This = impl_from_IXMLDOMElement( iface );
1302 xmlChar *local, *prefix, *nameA;
1303 HRESULT hr = S_FALSE;
1307 TRACE("(%p)->(%s %p)\n", This, debugstr_w(p), attributeNode);
1309 element = get_element( This );
1310 if (!element) return E_FAIL;
1312 if (attributeNode) *attributeNode = NULL;
1314 nameA = xmlchar_from_wchar(p);
1315 if (!xmlValidateNameValue(nameA))
1327 *attributeNode = NULL;
1329 local = xmlSplitQName2(nameA, &prefix);
1333 /* try to get namespace for supplied qualified name */
1334 xmlNsPtr ns = xmlSearchNs(element->doc, element, prefix);
1337 attr = xmlHasNsProp(element, local, ns ? ns->href : NULL);
1342 attr = xmlHasProp(element, nameA);
1343 /* attribute has attached namespace and we requested non-qualified
1344 name - it's a failure case */
1345 if (attr && attr->ns) attr = NULL;
1352 IUnknown *unk = create_attribute((xmlNodePtr)attr);
1353 hr = IUnknown_QueryInterface(unk, &IID_IXMLDOMAttribute, (void**)attributeNode);
1354 IUnknown_Release(unk);
1360 static HRESULT WINAPI domelem_setAttributeNode(
1361 IXMLDOMElement *iface,
1362 IXMLDOMAttribute* attribute,
1363 IXMLDOMAttribute** old)
1365 domelem *This = impl_from_IXMLDOMElement( iface );
1366 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
1367 xmlChar *name, *value;
1374 FIXME("(%p)->(%p %p): semi-stub\n", This, attribute, old);
1376 if (!attribute) return E_INVALIDARG;
1378 attr_node = get_node_obj((IXMLDOMNode*)attribute);
1379 if (!attr_node) return E_FAIL;
1381 if (attr_node->parent)
1383 WARN("attempt to add already used attribute\n");
1387 hr = IXMLDOMAttribute_get_nodeName(attribute, &nameW);
1388 if (hr != S_OK) return hr;
1390 /* adding xmlns attribute doesn't change a tree or existing namespace definition */
1391 if (!strcmpW(nameW, xmlnsW))
1393 SysFreeString(nameW);
1394 return DISP_E_UNKNOWNNAME;
1397 hr = IXMLDOMAttribute_get_nodeValue(attribute, &valueW);
1400 SysFreeString(nameW);
1404 if (old) *old = NULL;
1406 TRACE("attribute: %s=%s\n", debugstr_w(nameW), debugstr_w(V_BSTR(&valueW)));
1408 hr = IXMLDOMAttribute_get_prefix(attribute, &prefix);
1411 FIXME("namespaces not supported: %s\n", debugstr_w(prefix));
1412 SysFreeString(prefix);
1415 name = xmlchar_from_wchar(nameW);
1416 value = xmlchar_from_wchar(V_BSTR(&valueW));
1418 if (!name || !value)
1420 SysFreeString(nameW);
1421 VariantClear(&valueW);
1424 return E_OUTOFMEMORY;
1427 attr = xmlSetNsProp(get_element(This), NULL, name, value);
1429 attr_node->parent = (IXMLDOMNode*)iface;
1431 SysFreeString(nameW);
1432 VariantClear(&valueW);
1436 return attr ? S_OK : E_FAIL;
1439 static HRESULT WINAPI domelem_removeAttributeNode(
1440 IXMLDOMElement *iface,
1441 IXMLDOMAttribute* domAttribute,
1442 IXMLDOMAttribute** attributeNode)
1444 domelem *This = impl_from_IXMLDOMElement( iface );
1445 FIXME("(%p)->(%p %p)\n", This, domAttribute, attributeNode);
1449 static HRESULT WINAPI domelem_getElementsByTagName(
1450 IXMLDOMElement *iface,
1451 BSTR tagName, IXMLDOMNodeList** resultList)
1453 domelem *This = impl_from_IXMLDOMElement( iface );
1458 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(tagName), resultList);
1460 if (!tagName || !resultList) return E_INVALIDARG;
1462 XPath = is_xpathmode(get_element(This)->doc);
1463 set_xpathmode(get_element(This)->doc, TRUE);
1464 query = tagName_to_XPath(tagName);
1465 hr = create_selection(get_element(This), query, resultList);
1467 set_xpathmode(get_element(This)->doc, XPath);
1472 static HRESULT WINAPI domelem_normalize(
1473 IXMLDOMElement *iface )
1475 domelem *This = impl_from_IXMLDOMElement( iface );
1476 FIXME("%p\n", This);
1480 static const struct IXMLDOMElementVtbl domelem_vtbl =
1482 domelem_QueryInterface,
1485 domelem_GetTypeInfoCount,
1486 domelem_GetTypeInfo,
1487 domelem_GetIDsOfNames,
1489 domelem_get_nodeName,
1490 domelem_get_nodeValue,
1491 domelem_put_nodeValue,
1492 domelem_get_nodeType,
1493 domelem_get_parentNode,
1494 domelem_get_childNodes,
1495 domelem_get_firstChild,
1496 domelem_get_lastChild,
1497 domelem_get_previousSibling,
1498 domelem_get_nextSibling,
1499 domelem_get_attributes,
1500 domelem_insertBefore,
1501 domelem_replaceChild,
1502 domelem_removeChild,
1503 domelem_appendChild,
1504 domelem_hasChildNodes,
1505 domelem_get_ownerDocument,
1507 domelem_get_nodeTypeString,
1510 domelem_get_specified,
1511 domelem_get_definition,
1512 domelem_get_nodeTypedValue,
1513 domelem_put_nodeTypedValue,
1514 domelem_get_dataType,
1515 domelem_put_dataType,
1517 domelem_transformNode,
1518 domelem_selectNodes,
1519 domelem_selectSingleNode,
1521 domelem_get_namespaceURI,
1523 domelem_get_baseName,
1524 domelem_transformNodeToObject,
1525 domelem_get_tagName,
1526 domelem_getAttribute,
1527 domelem_setAttribute,
1528 domelem_removeAttribute,
1529 domelem_getAttributeNode,
1530 domelem_setAttributeNode,
1531 domelem_removeAttributeNode,
1532 domelem_getElementsByTagName,
1536 static HRESULT domelem_get_qualified_item(const xmlNodePtr node, BSTR name, BSTR uri,
1543 TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);
1545 if (!name || !item) return E_INVALIDARG;
1549 href = xmlchar_from_wchar(uri);
1550 if (!href) return E_OUTOFMEMORY;
1555 nameA = xmlchar_from_wchar(name);
1559 return E_OUTOFMEMORY;
1562 attr = xmlHasNsProp(node, nameA, href);
1573 *item = create_node((xmlNodePtr)attr);
1578 static HRESULT domelem_get_named_item(const xmlNodePtr node, BSTR name, IXMLDOMNode **item)
1580 xmlChar *nameA, *local, *prefix;
1585 TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item );
1587 nameA = xmlchar_from_wchar(name);
1588 local = xmlSplitQName2(nameA, &prefix);
1592 return domelem_get_qualified_item(node, name, NULL, item);
1594 /* try to get namespace uri for supplied qualified name */
1595 ns = xmlSearchNs(node->doc, node, prefix);
1602 if (item) *item = NULL;
1603 return item ? S_FALSE : E_INVALIDARG;
1606 uriW = bstr_from_xmlChar(ns->href);
1607 localW = bstr_from_xmlChar(local);
1610 TRACE("got qualified node %s, uri=%s\n", debugstr_w(localW), debugstr_w(uriW));
1612 hr = domelem_get_qualified_item(node, localW, uriW, item);
1614 SysFreeString(localW);
1615 SysFreeString(uriW);
1620 static HRESULT domelem_set_named_item(xmlNodePtr node, IXMLDOMNode *newItem, IXMLDOMNode **namedItem)
1625 TRACE("(%p)->(%p %p)\n", node, newItem, namedItem );
1628 return E_INVALIDARG;
1630 if(namedItem) *namedItem = NULL;
1632 /* Must be an Attribute */
1633 ThisNew = get_node_obj( newItem );
1634 if(!ThisNew) return E_FAIL;
1636 if(ThisNew->node->type != XML_ATTRIBUTE_NODE)
1639 if(!ThisNew->node->parent)
1640 if(xmldoc_remove_orphan(ThisNew->node->doc, ThisNew->node) != S_OK)
1641 WARN("%p is not an orphan of %p\n", ThisNew->node, ThisNew->node->doc);
1643 nodeNew = xmlAddChild(node, ThisNew->node);
1646 *namedItem = create_node( nodeNew );
1650 static HRESULT domelem_remove_qualified_item(xmlNodePtr node, BSTR name, BSTR uri, IXMLDOMNode **item)
1652 xmlChar *nameA, *href;
1655 TRACE("(%p)->(%s %s %p)\n", node, debugstr_w(name), debugstr_w(uri), item);
1657 if (!name) return E_INVALIDARG;
1661 href = xmlchar_from_wchar(uri);
1662 if (!href) return E_OUTOFMEMORY;
1667 nameA = xmlchar_from_wchar(name);
1671 return E_OUTOFMEMORY;
1674 attr = xmlHasNsProp(node, nameA, href);
1681 if (item) *item = NULL;
1687 xmlUnlinkNode( (xmlNodePtr) attr );
1688 xmldoc_add_orphan( attr->doc, (xmlNodePtr) attr );
1689 *item = create_node( (xmlNodePtr) attr );
1693 if (xmlRemoveProp(attr) == -1)
1694 ERR("xmlRemoveProp failed\n");
1700 static HRESULT domelem_remove_named_item(xmlNodePtr node, BSTR name, IXMLDOMNode **item)
1702 TRACE("(%p)->(%s %p)\n", node, debugstr_w(name), item);
1703 return domelem_remove_qualified_item(node, name, NULL, item);
1706 static HRESULT domelem_get_item(const xmlNodePtr node, LONG index, IXMLDOMNode **item)
1711 TRACE("(%p)->(%d %p)\n", node, index, item);
1718 curr = node->properties;
1720 for (attrIndex = 0; attrIndex < index; attrIndex++) {
1721 if (curr->next == NULL)
1727 *item = create_node( (xmlNodePtr) curr );
1732 static HRESULT domelem_get_length(const xmlNodePtr node, LONG *length)
1738 TRACE("(%p)->(%p)\n", node, length);
1741 return E_INVALIDARG;
1743 first = node->properties;
1744 if (first == NULL) {
1751 while (curr->next) {
1755 *length = attrCount;
1760 static HRESULT domelem_next_node(const xmlNodePtr node, LONG *iter, IXMLDOMNode **nextNode)
1765 TRACE("(%p)->(%d: %p)\n", node, *iter, nextNode);
1769 curr = node->properties;
1771 for (i = 0; i < *iter; i++) {
1772 if (curr->next == NULL)
1779 *nextNode = create_node((xmlNodePtr)curr);
1784 static const struct nodemap_funcs domelem_attr_map = {
1785 domelem_get_named_item,
1786 domelem_set_named_item,
1787 domelem_remove_named_item,
1790 domelem_get_qualified_item,
1791 domelem_remove_qualified_item,
1795 static const tid_t domelem_iface_tids[] = {
1800 static dispex_static_data_t domelem_dispex = {
1807 IUnknown* create_element( xmlNodePtr element )
1811 This = heap_alloc( sizeof *This );
1815 This->IXMLDOMElement_iface.lpVtbl = &domelem_vtbl;
1818 init_xmlnode(&This->node, element, (IXMLDOMNode*)&This->IXMLDOMElement_iface, &domelem_dispex);
1820 return (IUnknown*)&This->IXMLDOMElement_iface;