uiautomationcore: Implement UiaGetPropertyValue.
[wine.git] / dlls / msxml3 / node.c
blob339504477acc7968917f72228c953e3e05345e45
1 /*
2 * Node 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
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include <libxml/parser.h>
26 #include <libxml/parserInternals.h>
27 #include <libxml/xmlerror.h>
28 #include <libxml/HTMLtree.h>
29 #include <libxslt/pattern.h>
30 #include <libxslt/transform.h>
31 #include <libxslt/imports.h>
32 #include <libxslt/variables.h>
33 #include <libxslt/xsltutils.h>
34 #include <libxslt/xsltInternals.h>
35 #include <libxslt/documents.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "ole2.h"
42 #include "msxml6.h"
44 #include "msxml_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
50 static const IID IID_xmlnode = {0x4f2f4ba2,0xb822,0x11df,{0x8b,0x8a,0x68,0x50,0xdf,0xd7,0x20,0x85}};
52 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
54 xmlnode *This;
56 if ( !iface )
57 return NULL;
58 This = get_node_obj( iface );
59 if ( !This || !This->node )
60 return NULL;
61 if ( type && This->node->type != type )
62 return NULL;
63 return This->node;
66 BOOL node_query_interface(xmlnode *This, REFIID riid, void **ppv)
68 if(IsEqualGUID(&IID_xmlnode, riid)) {
69 TRACE("(%p)->(IID_xmlnode %p)\n", This, ppv);
70 *ppv = This;
71 return TRUE;
74 return dispex_query_interface(&This->dispex, riid, ppv);
77 /* common ISupportErrorInfo implementation */
78 typedef struct {
79 ISupportErrorInfo ISupportErrorInfo_iface;
80 LONG ref;
82 const tid_t* iids;
83 } SupportErrorInfo;
85 static inline SupportErrorInfo *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
87 return CONTAINING_RECORD(iface, SupportErrorInfo, ISupportErrorInfo_iface);
90 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **obj)
92 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
93 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
95 *obj = NULL;
97 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISupportErrorInfo)) {
98 *obj = iface;
99 ISupportErrorInfo_AddRef(iface);
100 return S_OK;
103 return E_NOINTERFACE;
106 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
108 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
109 ULONG ref = InterlockedIncrement(&This->ref);
110 TRACE("%p, refcount %ld.\n", iface, ref );
111 return ref;
114 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
116 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
117 LONG ref = InterlockedDecrement(&This->ref);
119 TRACE("%p, refcount %ld.\n", iface, ref);
121 if (!ref)
122 heap_free(This);
124 return ref;
127 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
129 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
130 enum tid_t const *tid;
132 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
134 tid = This->iids;
135 while (*tid != NULL_tid)
137 if (IsEqualGUID(riid, get_riid_from_tid(*tid)))
138 return S_OK;
139 tid++;
142 return S_FALSE;
145 static const struct ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
146 SupportErrorInfo_QueryInterface,
147 SupportErrorInfo_AddRef,
148 SupportErrorInfo_Release,
149 SupportErrorInfo_InterfaceSupportsErrorInfo
152 HRESULT node_create_supporterrorinfo(enum tid_t const *iids, void **obj)
154 SupportErrorInfo *This;
156 This = heap_alloc(sizeof(*This));
157 if (!This) return E_OUTOFMEMORY;
159 This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
160 This->ref = 1;
161 This->iids = iids;
163 *obj = &This->ISupportErrorInfo_iface;
165 return S_OK;
168 xmlnode *get_node_obj(IXMLDOMNode *node)
170 xmlnode *obj = NULL;
171 HRESULT hres;
173 hres = IXMLDOMNode_QueryInterface(node, &IID_xmlnode, (void**)&obj);
174 if (!obj) WARN("node is not our IXMLDOMNode implementation\n");
175 return SUCCEEDED(hres) ? obj : NULL;
178 HRESULT node_get_nodeName(xmlnode *This, BSTR *name)
180 BSTR prefix, base;
181 HRESULT hr;
183 if (!name)
184 return E_INVALIDARG;
186 hr = node_get_base_name(This, &base);
187 if (hr != S_OK) return hr;
189 hr = node_get_prefix(This, &prefix);
190 if (hr == S_OK)
192 static const WCHAR colW = ':';
193 WCHAR *ptr;
195 /* +1 for ':' */
196 ptr = *name = SysAllocStringLen(NULL, SysStringLen(base) + SysStringLen(prefix) + 1);
197 memcpy(ptr, prefix, SysStringByteLen(prefix));
198 ptr += SysStringLen(prefix);
199 memcpy(ptr++, &colW, sizeof(WCHAR));
200 memcpy(ptr, base, SysStringByteLen(base));
202 SysFreeString(base);
203 SysFreeString(prefix);
205 else
206 *name = base;
208 return S_OK;
211 HRESULT node_get_content(xmlnode *This, VARIANT *value)
213 xmlChar *content;
215 if(!value)
216 return E_INVALIDARG;
218 content = xmlNodeGetContent(This->node);
219 V_VT(value) = VT_BSTR;
220 V_BSTR(value) = bstr_from_xmlChar( content );
221 xmlFree(content);
223 TRACE("%p returned %s\n", This, debugstr_w(V_BSTR(value)));
224 return S_OK;
227 HRESULT node_set_content(xmlnode *This, LPCWSTR value)
229 xmlChar *str;
231 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
232 str = xmlchar_from_wchar(value);
233 if(!str)
234 return E_OUTOFMEMORY;
236 xmlNodeSetContent(This->node, str);
237 heap_free(str);
238 return S_OK;
241 static HRESULT node_set_content_escaped(xmlnode *This, LPCWSTR value)
243 xmlChar *str, *escaped;
245 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
246 str = xmlchar_from_wchar(value);
247 if(!str)
248 return E_OUTOFMEMORY;
250 escaped = xmlEncodeSpecialChars(NULL, str);
251 if(!escaped)
253 heap_free(str);
254 return E_OUTOFMEMORY;
257 xmlNodeSetContent(This->node, escaped);
259 heap_free(str);
260 xmlFree(escaped);
262 return S_OK;
265 HRESULT node_put_value(xmlnode *This, VARIANT *value)
267 HRESULT hr;
269 if (V_VT(value) != VT_BSTR)
271 VARIANT string_value;
273 VariantInit(&string_value);
274 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
275 if(FAILED(hr)) {
276 WARN("Couldn't convert to VT_BSTR\n");
277 return hr;
280 hr = node_set_content(This, V_BSTR(&string_value));
281 VariantClear(&string_value);
283 else
284 hr = node_set_content(This, V_BSTR(value));
286 return hr;
289 HRESULT node_put_value_escaped(xmlnode *This, VARIANT *value)
291 HRESULT hr;
293 if (V_VT(value) != VT_BSTR)
295 VARIANT string_value;
297 VariantInit(&string_value);
298 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
299 if(FAILED(hr)) {
300 WARN("Couldn't convert to VT_BSTR\n");
301 return hr;
304 hr = node_set_content_escaped(This, V_BSTR(&string_value));
305 VariantClear(&string_value);
307 else
308 hr = node_set_content_escaped(This, V_BSTR(value));
310 return hr;
313 static HRESULT get_node(
314 xmlnode *This,
315 const char *name,
316 xmlNodePtr node,
317 IXMLDOMNode **out )
319 TRACE("(%p)->(%s %p %p)\n", This, name, node, out );
321 if ( !out )
322 return E_INVALIDARG;
324 /* if we don't have a doc, use our parent. */
325 if(node && !node->doc && node->parent)
326 node->doc = node->parent->doc;
328 *out = create_node( node );
329 if (!*out)
330 return S_FALSE;
331 return S_OK;
334 HRESULT node_get_parent(xmlnode *This, IXMLDOMNode **parent)
336 return get_node( This, "parent", This->node->parent, parent );
339 HRESULT node_get_child_nodes(xmlnode *This, IXMLDOMNodeList **ret)
341 if(!ret)
342 return E_INVALIDARG;
344 *ret = create_children_nodelist(This->node);
345 if(!*ret)
346 return E_OUTOFMEMORY;
348 return S_OK;
351 HRESULT node_get_first_child(xmlnode *This, IXMLDOMNode **ret)
353 return get_node(This, "firstChild", This->node->children, ret);
356 HRESULT node_get_last_child(xmlnode *This, IXMLDOMNode **ret)
358 return get_node(This, "lastChild", This->node->last, ret);
361 HRESULT node_get_previous_sibling(xmlnode *This, IXMLDOMNode **ret)
363 return get_node(This, "previous", This->node->prev, ret);
366 HRESULT node_get_next_sibling(xmlnode *This, IXMLDOMNode **ret)
368 return get_node(This, "next", This->node->next, ret);
371 static int node_get_inst_cnt(xmlNodePtr node)
373 int ret = *(LONG *)&node->_private & NODE_PRIV_REFCOUNT_MASK;
374 xmlNodePtr child;
376 /* add attribute counts */
377 if (node->type == XML_ELEMENT_NODE)
379 xmlAttrPtr prop = node->properties;
381 while (prop)
383 ret += node_get_inst_cnt((xmlNodePtr)prop);
384 prop = prop->next;
388 /* add children counts */
389 child = node->children;
390 while (child)
392 ret += node_get_inst_cnt(child);
393 child = child->next;
396 return ret;
399 int xmlnode_get_inst_cnt(xmlnode *node)
401 return node_get_inst_cnt(node->node);
404 /* _private field holds a number of COM instances spawned from this libxml2 node
405 * most significant bits are used to store information about ignorable whitespace nodes */
406 void xmlnode_add_ref(xmlNodePtr node)
408 if (node->type == XML_DOCUMENT_NODE) return;
409 InterlockedIncrement((LONG*)&node->_private);
412 void xmlnode_release(xmlNodePtr node)
414 if (node->type == XML_DOCUMENT_NODE) return;
415 InterlockedDecrement((LONG*)&node->_private);
418 HRESULT node_insert_before(xmlnode *This, IXMLDOMNode *new_child, const VARIANT *ref_child,
419 IXMLDOMNode **ret)
421 IXMLDOMNode *before = NULL;
422 xmlnode *node_obj;
423 int refcount = 0;
424 xmlDocPtr doc;
425 HRESULT hr;
427 if(!new_child)
428 return E_INVALIDARG;
430 node_obj = get_node_obj(new_child);
431 if(!node_obj) return E_FAIL;
433 switch(V_VT(ref_child))
435 case VT_EMPTY:
436 case VT_NULL:
437 break;
439 case VT_UNKNOWN:
440 case VT_DISPATCH:
441 if (V_UNKNOWN(ref_child))
443 hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
444 if(FAILED(hr)) return hr;
446 break;
448 default:
449 FIXME("refChild var type %x\n", V_VT(ref_child));
450 return E_FAIL;
453 TRACE("new child %p, This->node %p\n", node_obj->node, This->node);
455 if(!node_obj->node->parent)
456 if(xmldoc_remove_orphan(node_obj->node->doc, node_obj->node) != S_OK)
457 WARN("%p is not an orphan of %p\n", node_obj->node, node_obj->node->doc);
459 refcount = xmlnode_get_inst_cnt(node_obj);
461 if(before)
463 xmlnode *before_node_obj = get_node_obj(before);
464 IXMLDOMNode_Release(before);
465 if(!before_node_obj) return E_FAIL;
468 /* unlink from current parent first */
469 if(node_obj->parent)
471 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
472 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
474 doc = node_obj->node->doc;
476 if(before)
478 xmlNodePtr new_node;
479 xmlnode *before_node_obj = get_node_obj(before);
481 /* refs count including subtree */
482 if (doc != before_node_obj->node->doc)
483 refcount = xmlnode_get_inst_cnt(node_obj);
485 if (refcount) xmldoc_add_refs(before_node_obj->node->doc, refcount);
486 new_node = xmlAddPrevSibling(before_node_obj->node, node_obj->node);
487 if (new_node != node_obj->node)
489 if (refcount != 1)
490 FIXME("referenced xmlNode was freed, expect crashes\n");
491 xmlnode_add_ref(new_node);
492 node_obj->node = new_node;
494 if (refcount) xmldoc_release_refs(doc, refcount);
495 node_obj->parent = This->parent;
497 else
499 xmlNodePtr new_node;
501 if (doc != This->node->doc)
502 refcount = xmlnode_get_inst_cnt(node_obj);
504 if (refcount) xmldoc_add_refs(This->node->doc, refcount);
505 /* xmlAddChild doesn't unlink node from previous parent */
506 xmlUnlinkNode(node_obj->node);
507 new_node = xmlAddChild(This->node, node_obj->node);
508 if (new_node != node_obj->node)
510 if (refcount != 1)
511 FIXME("referenced xmlNode was freed, expect crashes\n");
512 xmlnode_add_ref(new_node);
513 node_obj->node = new_node;
515 if (refcount) xmldoc_release_refs(doc, refcount);
516 node_obj->parent = This->iface;
519 if(ret)
521 IXMLDOMNode_AddRef(new_child);
522 *ret = new_child;
525 TRACE("ret S_OK\n");
526 return S_OK;
529 HRESULT node_replace_child(xmlnode *This, IXMLDOMNode *newChild, IXMLDOMNode *oldChild,
530 IXMLDOMNode **ret)
532 xmlnode *old_child, *new_child;
533 xmlDocPtr leaving_doc;
534 xmlNode *my_ancestor;
535 int refcount = 0;
537 /* Do not believe any documentation telling that newChild == NULL
538 means removal. It does certainly *not* apply to msxml3! */
539 if(!newChild || !oldChild)
540 return E_INVALIDARG;
542 if(ret)
543 *ret = NULL;
545 old_child = get_node_obj(oldChild);
546 if(!old_child) return E_FAIL;
548 if(old_child->node->parent != This->node)
550 WARN("childNode %p is not a child of %p\n", oldChild, This);
551 return E_INVALIDARG;
554 new_child = get_node_obj(newChild);
555 if(!new_child) return E_FAIL;
557 my_ancestor = This->node;
558 while(my_ancestor)
560 if(my_ancestor == new_child->node)
562 WARN("tried to create loop\n");
563 return E_FAIL;
565 my_ancestor = my_ancestor->parent;
568 if(!new_child->node->parent)
569 if(xmldoc_remove_orphan(new_child->node->doc, new_child->node) != S_OK)
570 WARN("%p is not an orphan of %p\n", new_child->node, new_child->node->doc);
572 leaving_doc = new_child->node->doc;
574 if (leaving_doc != old_child->node->doc)
575 refcount = xmlnode_get_inst_cnt(new_child);
577 if (refcount) xmldoc_add_refs(old_child->node->doc, refcount);
578 xmlReplaceNode(old_child->node, new_child->node);
579 if (refcount) xmldoc_release_refs(leaving_doc, refcount);
580 new_child->parent = old_child->parent;
581 old_child->parent = NULL;
583 xmldoc_add_orphan(old_child->node->doc, old_child->node);
585 if(ret)
587 IXMLDOMNode_AddRef(oldChild);
588 *ret = oldChild;
591 return S_OK;
594 HRESULT node_remove_child(xmlnode *This, IXMLDOMNode* child, IXMLDOMNode** oldChild)
596 xmlnode *child_node;
598 if(!child) return E_INVALIDARG;
600 if(oldChild)
601 *oldChild = NULL;
603 child_node = get_node_obj(child);
604 if(!child_node) return E_FAIL;
606 if(child_node->node->parent != This->node)
608 WARN("childNode %p is not a child of %p\n", child, This);
609 return E_INVALIDARG;
612 xmlUnlinkNode(child_node->node);
613 child_node->parent = NULL;
614 xmldoc_add_orphan(child_node->node->doc, child_node->node);
616 if(oldChild)
618 IXMLDOMNode_AddRef(child);
619 *oldChild = child;
622 return S_OK;
625 HRESULT node_append_child(xmlnode *This, IXMLDOMNode *child, IXMLDOMNode **outChild)
627 DOMNodeType type;
628 VARIANT var;
629 HRESULT hr;
631 if (!child)
632 return E_INVALIDARG;
634 hr = IXMLDOMNode_get_nodeType(child, &type);
635 if(FAILED(hr) || type == NODE_ATTRIBUTE) {
636 if (outChild) *outChild = NULL;
637 return E_FAIL;
640 VariantInit(&var);
641 return IXMLDOMNode_insertBefore(This->iface, child, var, outChild);
644 HRESULT node_has_childnodes(const xmlnode *This, VARIANT_BOOL *ret)
646 if (!ret) return E_INVALIDARG;
648 if (!This->node->children)
650 *ret = VARIANT_FALSE;
651 return S_FALSE;
654 *ret = VARIANT_TRUE;
655 return S_OK;
658 HRESULT node_get_owner_doc(const xmlnode *This, IXMLDOMDocument **doc)
660 if(!doc)
661 return E_INVALIDARG;
662 return get_domdoc_from_xmldoc(This->node->doc, (IXMLDOMDocument3**)doc);
665 HRESULT node_clone(xmlnode *This, VARIANT_BOOL deep, IXMLDOMNode **cloneNode)
667 IXMLDOMNode *node;
668 xmlNodePtr clone;
670 if(!cloneNode) return E_INVALIDARG;
672 clone = xmlCopyNode(This->node, deep ? 1 : 2);
673 if (clone)
675 xmlSetTreeDoc(clone, This->node->doc);
676 xmldoc_add_orphan(clone->doc, clone);
678 node = create_node(clone);
679 if (!node)
681 ERR("Copy failed\n");
682 xmldoc_remove_orphan(clone->doc, clone);
683 xmlFreeNode(clone);
684 return E_FAIL;
687 *cloneNode = node;
689 else
691 ERR("Copy failed\n");
692 return E_FAIL;
695 return S_OK;
698 static xmlChar* do_get_text(xmlNodePtr node, BOOL trim, DWORD *first, DWORD *last, BOOL *trail_ig_ws)
700 xmlNodePtr child;
701 xmlChar* str;
702 BOOL preserving = is_preserving_whitespace(node);
704 *first = -1;
705 *last = 0;
707 if (!node->children)
709 str = xmlNodeGetContent(node);
710 *trail_ig_ws = *(DWORD*)&node->_private & NODE_PRIV_CHILD_IGNORABLE_WS;
712 else
714 BOOL ig_ws = FALSE;
715 xmlChar* tmp;
716 DWORD pos = 0;
717 str = xmlStrdup(BAD_CAST "");
719 if (node->type != XML_DOCUMENT_NODE)
720 ig_ws = *(DWORD*)&node->_private & NODE_PRIV_CHILD_IGNORABLE_WS;
721 *trail_ig_ws = FALSE;
723 for (child = node->children; child != NULL; child = child->next)
725 switch (child->type)
727 case XML_ELEMENT_NODE: {
728 DWORD node_first, node_last;
730 tmp = do_get_text(child, FALSE, &node_first, &node_last, trail_ig_ws);
732 if (node_first!=-1 && pos+node_first<*first)
733 *first = pos+node_first;
734 if (node_last && pos+node_last>*last)
735 *last = pos+node_last;
736 break;
738 case XML_TEXT_NODE:
739 tmp = xmlNodeGetContent(child);
740 if (!preserving && tmp[0])
742 xmlChar *beg;
744 for (beg = tmp; *beg; beg++)
745 if (!isspace(*beg)) break;
747 if (!*beg)
749 ig_ws = TRUE;
750 xmlFree(tmp);
751 tmp = NULL;
754 break;
755 case XML_CDATA_SECTION_NODE:
756 case XML_ENTITY_REF_NODE:
757 case XML_ENTITY_NODE:
758 tmp = xmlNodeGetContent(child);
759 break;
760 default:
761 tmp = NULL;
762 break;
765 if ((tmp && *tmp) || child->type==XML_CDATA_SECTION_NODE)
767 if (ig_ws && str[0])
769 str = xmlStrcat(str, BAD_CAST " ");
770 pos++;
772 if (tmp && *tmp) str = xmlStrcat(str, tmp);
773 if (child->type==XML_CDATA_SECTION_NODE && pos<*first)
774 *first = pos;
775 if (tmp && *tmp) pos += xmlStrlen(tmp);
776 if (child->type==XML_CDATA_SECTION_NODE && pos>*last)
777 *last = pos;
778 ig_ws = FALSE;
780 if (tmp) xmlFree(tmp);
782 if (!ig_ws)
784 ig_ws = *(DWORD*)&child->_private & NODE_PRIV_TRAILING_IGNORABLE_WS;
786 if (!ig_ws)
787 ig_ws = *trail_ig_ws;
788 *trail_ig_ws = FALSE;
791 *trail_ig_ws = ig_ws;
794 switch (node->type)
796 case XML_ELEMENT_NODE:
797 case XML_TEXT_NODE:
798 case XML_ENTITY_REF_NODE:
799 case XML_ENTITY_NODE:
800 case XML_DOCUMENT_NODE:
801 case XML_DOCUMENT_FRAG_NODE:
802 if (trim && !preserving)
804 xmlChar* ret;
805 int len;
807 if (!str)
808 break;
810 for (ret = str; *ret && isspace(*ret) && (*first)--; ret++)
811 if (*last) (*last)--;
812 for (len = xmlStrlen(ret)-1; len >= 0 && len >= *last; len--)
813 if(!isspace(ret[len])) break;
815 ret = xmlStrndup(ret, len+1);
816 xmlFree(str);
817 str = ret;
818 break;
820 break;
821 default:
822 break;
825 return str;
828 HRESULT node_get_text(const xmlnode *This, BSTR *text)
830 BSTR str = NULL;
831 xmlChar *content;
832 DWORD first, last;
833 BOOL tmp;
835 if (!text) return E_INVALIDARG;
837 content = do_get_text(This->node, TRUE, &first, &last, &tmp);
838 if (content)
840 str = bstr_from_xmlChar(content);
841 xmlFree(content);
844 /* Always return a string. */
845 if (!str) str = SysAllocStringLen( NULL, 0 );
847 TRACE("%p %s\n", This, debugstr_w(str) );
848 *text = str;
850 return S_OK;
853 HRESULT node_put_text(xmlnode *This, BSTR text)
855 xmlChar *str, *str2;
857 TRACE("(%p)->(%s)\n", This, debugstr_w(text));
859 str = xmlchar_from_wchar(text);
861 /* Escape the string. */
862 str2 = xmlEncodeEntitiesReentrant(This->node->doc, str);
863 heap_free(str);
865 xmlNodeSetContent(This->node, str2);
866 xmlFree(str2);
868 return S_OK;
871 BSTR EnsureCorrectEOL(BSTR sInput)
873 int nNum = 0;
874 BSTR sNew;
875 int nLen;
876 int i;
878 nLen = SysStringLen(sInput);
879 /* Count line endings */
880 for(i=0; i < nLen; i++)
882 if(sInput[i] == '\n')
883 nNum++;
886 TRACE("len=%d, num=%d\n", nLen, nNum);
888 /* Add linefeed as needed */
889 if(nNum > 0)
891 int nPlace = 0;
892 sNew = SysAllocStringLen(NULL, nLen + nNum);
893 for(i=0; i < nLen; i++)
895 if(sInput[i] == '\n')
897 sNew[i+nPlace] = '\r';
898 nPlace++;
900 sNew[i+nPlace] = sInput[i];
903 SysFreeString(sInput);
905 else
907 sNew = sInput;
910 TRACE("len %d\n", SysStringLen(sNew));
912 return sNew;
916 * We are trying to replicate the same behaviour as msxml by converting
917 * line endings to \r\n and using indents as \t. The problem is that msxml
918 * only formats nodes that have a line ending. Using libxml we cannot
919 * reproduce behaviour exactly.
922 HRESULT node_get_xml(xmlnode *This, BOOL ensure_eol, BSTR *ret)
924 xmlBufferPtr xml_buf;
925 xmlNodePtr xmldecl;
926 int size;
928 if(!ret)
929 return E_INVALIDARG;
931 *ret = NULL;
933 xml_buf = xmlBufferCreate();
934 if(!xml_buf)
935 return E_OUTOFMEMORY;
937 xmldecl = xmldoc_unlink_xmldecl( This->node->doc );
939 size = xmlNodeDump(xml_buf, This->node->doc, This->node, 0, 1);
940 if(size > 0) {
941 const xmlChar *buf_content;
942 BSTR content;
944 /* Attribute Nodes return a space in front of their name */
945 buf_content = xmlBufferContent(xml_buf);
947 content = bstr_from_xmlChar(buf_content + (buf_content[0] == ' ' ? 1 : 0));
948 if(ensure_eol)
949 content = EnsureCorrectEOL(content);
951 *ret = content;
952 }else {
953 *ret = SysAllocStringLen(NULL, 0);
956 xmlBufferFree(xml_buf);
957 xmldoc_link_xmldecl( This->node->doc, xmldecl );
958 return *ret ? S_OK : E_OUTOFMEMORY;
961 /* duplicates xmlBufferWriteQuotedString() logic */
962 static void xml_write_quotedstring(xmlOutputBufferPtr buf, const xmlChar *string)
964 const xmlChar *cur, *base;
966 if (xmlStrchr(string, '\"'))
968 if (xmlStrchr(string, '\''))
970 xmlOutputBufferWrite(buf, 1, "\"");
971 base = cur = string;
973 while (*cur)
975 if (*cur == '"')
977 if (base != cur)
978 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
979 xmlOutputBufferWrite(buf, 6, "&quot;");
980 cur++;
981 base = cur;
983 else
984 cur++;
986 if (base != cur)
987 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
988 xmlOutputBufferWrite(buf, 1, "\"");
990 else
992 xmlOutputBufferWrite(buf, 1, "\'");
993 xmlOutputBufferWriteString(buf, (const char*)string);
994 xmlOutputBufferWrite(buf, 1, "\'");
997 else
999 xmlOutputBufferWrite(buf, 1, "\"");
1000 xmlOutputBufferWriteString(buf, (const char*)string);
1001 xmlOutputBufferWrite(buf, 1, "\"");
1005 static int XMLCALL transform_to_stream_write(void *context, const char *buffer, int len)
1007 DWORD written;
1008 HRESULT hr = ISequentialStream_Write((ISequentialStream *)context, buffer, len, &written);
1009 return hr == S_OK ? written : -1;
1012 /* Output for method "text" */
1013 static void transform_write_text(xmlDocPtr result, xsltStylesheetPtr style, xmlOutputBufferPtr output)
1015 xmlNodePtr cur = result->children;
1016 while (cur)
1018 if (cur->type == XML_TEXT_NODE)
1019 xmlOutputBufferWriteString(output, (const char*)cur->content);
1021 /* skip to next node */
1022 if (cur->children)
1024 if ((cur->children->type != XML_ENTITY_DECL) &&
1025 (cur->children->type != XML_ENTITY_REF_NODE) &&
1026 (cur->children->type != XML_ENTITY_NODE))
1028 cur = cur->children;
1029 continue;
1033 if (cur->next) {
1034 cur = cur->next;
1035 continue;
1040 cur = cur->parent;
1041 if (cur == NULL)
1042 break;
1043 if (cur == (xmlNodePtr) style->doc) {
1044 cur = NULL;
1045 break;
1047 if (cur->next) {
1048 cur = cur->next;
1049 break;
1051 } while (cur);
1055 #undef XSLT_GET_IMPORT_PTR
1056 #define XSLT_GET_IMPORT_PTR(res, style, name) { \
1057 xsltStylesheetPtr st = style; \
1058 res = NULL; \
1059 while (st != NULL) { \
1060 if (st->name != NULL) { res = st->name; break; } \
1061 st = xsltNextImport(st); \
1064 #undef XSLT_GET_IMPORT_INT
1065 #define XSLT_GET_IMPORT_INT(res, style, name) { \
1066 xsltStylesheetPtr st = style; \
1067 res = -1; \
1068 while (st != NULL) { \
1069 if (st->name != -1) { res = st->name; break; } \
1070 st = xsltNextImport(st); \
1073 static void transform_write_xmldecl(xmlDocPtr result, xsltStylesheetPtr style, BOOL omit_encoding, xmlOutputBufferPtr output)
1075 int omit_xmldecl, standalone;
1077 XSLT_GET_IMPORT_INT(omit_xmldecl, style, omitXmlDeclaration);
1078 if (omit_xmldecl == 1) return;
1080 XSLT_GET_IMPORT_INT(standalone, style, standalone);
1082 xmlOutputBufferWriteString(output, "<?xml version=");
1083 if (result->version)
1085 xmlOutputBufferWriteString(output, "\"");
1086 xmlOutputBufferWriteString(output, (const char *)result->version);
1087 xmlOutputBufferWriteString(output, "\"");
1089 else
1090 xmlOutputBufferWriteString(output, "\"1.0\"");
1092 if (!omit_encoding)
1094 const xmlChar *encoding;
1096 /* default encoding is UTF-16 */
1097 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1098 xmlOutputBufferWriteString(output, " encoding=");
1099 xmlOutputBufferWriteString(output, "\"");
1100 xmlOutputBufferWriteString(output, encoding ? (const char *)encoding : "UTF-16");
1101 xmlOutputBufferWriteString(output, "\"");
1104 /* standalone attribute */
1105 if (standalone != -1)
1106 xmlOutputBufferWriteString(output, standalone == 0 ? " standalone=\"no\"" : " standalone=\"yes\"");
1108 xmlOutputBufferWriteString(output, "?>");
1111 static void htmldtd_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc)
1113 xmlDtdPtr cur = doc->intSubset;
1115 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
1116 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1117 if (cur->ExternalID)
1119 xmlOutputBufferWriteString(buf, " PUBLIC ");
1120 xml_write_quotedstring(buf, cur->ExternalID);
1121 if (cur->SystemID)
1123 xmlOutputBufferWriteString(buf, " ");
1124 xml_write_quotedstring(buf, cur->SystemID);
1127 else if (cur->SystemID)
1129 xmlOutputBufferWriteString(buf, " SYSTEM ");
1130 xml_write_quotedstring(buf, cur->SystemID);
1132 xmlOutputBufferWriteString(buf, ">\n");
1135 /* Duplicates htmlDocContentDumpFormatOutput() the way we need it - doesn't add trailing newline. */
1136 static void htmldoc_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding, int format)
1138 xmlElementType type;
1140 /* force HTML output */
1141 type = doc->type;
1142 doc->type = XML_HTML_DOCUMENT_NODE;
1143 if (doc->intSubset)
1144 htmldtd_dumpcontent(buf, doc);
1145 if (doc->children) {
1146 xmlNodePtr cur = doc->children;
1147 while (cur) {
1148 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
1149 cur = cur->next;
1152 doc->type = type;
1155 static inline BOOL transform_is_empty_resultdoc(xmlDocPtr result)
1157 return !result->children || ((result->children->type == XML_DTD_NODE) && !result->children->next);
1160 static inline BOOL transform_is_valid_method(xsltStylesheetPtr style)
1162 return !style->methodURI || !(style->method && xmlStrEqual(style->method, (const xmlChar *)"xhtml"));
1165 /* Helper to write transformation result to specified output buffer. */
1166 static HRESULT node_transform_write(xsltStylesheetPtr style, xmlDocPtr result, BOOL omit_encoding, const char *encoding, xmlOutputBufferPtr output)
1168 const xmlChar *method;
1169 int indent;
1171 if (!transform_is_valid_method(style))
1173 ERR("unknown output method\n");
1174 return E_FAIL;
1177 XSLT_GET_IMPORT_PTR(method, style, method)
1178 XSLT_GET_IMPORT_INT(indent, style, indent);
1180 if (!method && (result->type == XML_HTML_DOCUMENT_NODE))
1181 method = (const xmlChar *) "html";
1183 if (method && xmlStrEqual(method, (const xmlChar *)"html"))
1185 htmlSetMetaEncoding(result, (const xmlChar *)encoding);
1186 if (indent == -1)
1187 indent = 1;
1188 htmldoc_dumpcontent(output, result, encoding, indent);
1190 else if (method && xmlStrEqual(method, (const xmlChar *)"xhtml"))
1192 htmlSetMetaEncoding(result, (const xmlChar *) encoding);
1193 htmlDocContentDumpOutput(output, result, encoding);
1195 else if (method && xmlStrEqual(method, (const xmlChar *)"text"))
1196 transform_write_text(result, style, output);
1197 else
1199 transform_write_xmldecl(result, style, omit_encoding, output);
1201 if (result->children)
1203 xmlNodePtr child = result->children;
1205 while (child)
1207 xmlNodeDumpOutput(output, result, child, 0, indent == 1, encoding);
1208 if (indent && ((child->type == XML_DTD_NODE) || ((child->type == XML_COMMENT_NODE) && child->next)))
1209 xmlOutputBufferWriteString(output, "\r\n");
1210 child = child->next;
1215 xmlOutputBufferFlush(output);
1216 return S_OK;
1219 /* For BSTR output is always UTF-16, without 'encoding' attribute */
1220 static HRESULT node_transform_write_to_bstr(xsltStylesheetPtr style, xmlDocPtr result, BSTR *str)
1222 HRESULT hr = S_OK;
1224 if (transform_is_empty_resultdoc(result))
1225 *str = SysAllocStringLen(NULL, 0);
1226 else
1228 xmlOutputBufferPtr output = xmlAllocOutputBuffer(xmlFindCharEncodingHandler("UTF-16"));
1229 const xmlChar *content;
1230 size_t len;
1232 *str = NULL;
1233 if (!output)
1234 return E_OUTOFMEMORY;
1236 hr = node_transform_write(style, result, TRUE, "UTF-16", output);
1237 content = xmlBufContent(output->conv);
1238 len = xmlBufUse(output->conv);
1239 /* UTF-16 encoder places UTF-16 bom, we don't need it for BSTR */
1240 content += sizeof(WCHAR);
1241 *str = SysAllocStringLen((WCHAR*)content, len/sizeof(WCHAR) - 1);
1242 xmlOutputBufferClose(output);
1245 return *str ? hr : E_OUTOFMEMORY;
1248 static HRESULT node_transform_write_to_stream(xsltStylesheetPtr style, xmlDocPtr result, ISequentialStream *stream)
1250 static const xmlChar *utf16 = (const xmlChar*)"UTF-16";
1251 xmlOutputBufferPtr output;
1252 const xmlChar *encoding;
1253 HRESULT hr;
1255 if (transform_is_empty_resultdoc(result))
1257 WARN("empty result document\n");
1258 return S_OK;
1261 if (style->methodURI && (!style->method || !xmlStrEqual(style->method, (const xmlChar *) "xhtml")))
1263 ERR("unknown output method\n");
1264 return E_FAIL;
1267 /* default encoding is UTF-16 */
1268 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1269 if (!encoding)
1270 encoding = utf16;
1272 output = xmlOutputBufferCreateIO(transform_to_stream_write, NULL, stream, xmlFindCharEncodingHandler((const char*)encoding));
1273 if (!output)
1274 return E_OUTOFMEMORY;
1276 hr = node_transform_write(style, result, FALSE, (const char*)encoding, output);
1277 xmlOutputBufferClose(output);
1278 return hr;
1281 struct import_buffer
1283 char *data;
1284 int cur;
1285 int len;
1288 static int XMLCALL import_loader_io_read(void *context, char *out, int len)
1290 struct import_buffer *buffer = (struct import_buffer *)context;
1292 TRACE("%p, %p, %d\n", context, out, len);
1294 if (buffer->cur == buffer->len)
1295 return 0;
1297 len = min(len, buffer->len - buffer->cur);
1298 memcpy(out, &buffer->data[buffer->cur], len);
1299 buffer->cur += len;
1301 TRACE("read %d\n", len);
1303 return len;
1306 static int XMLCALL import_loader_io_close(void * context)
1308 struct import_buffer *buffer = (struct import_buffer *)context;
1310 TRACE("%p\n", context);
1312 heap_free(buffer->data);
1313 heap_free(buffer);
1314 return 0;
1317 static HRESULT import_loader_onDataAvailable(void *ctxt, char *ptr, DWORD len)
1319 xmlParserInputPtr *input = (xmlParserInputPtr *)ctxt;
1320 xmlParserInputBufferPtr inputbuffer;
1321 struct import_buffer *buffer;
1323 buffer = heap_alloc(sizeof(*buffer));
1325 buffer->data = heap_alloc(len);
1326 memcpy(buffer->data, ptr, len);
1327 buffer->cur = 0;
1328 buffer->len = len;
1330 inputbuffer = xmlParserInputBufferCreateIO(import_loader_io_read, import_loader_io_close, buffer,
1331 XML_CHAR_ENCODING_NONE);
1332 *input = xmlNewIOInputStream(NULL, inputbuffer, XML_CHAR_ENCODING_NONE);
1333 if (!*input)
1334 xmlFreeParserInputBuffer(inputbuffer);
1336 return *input ? S_OK : E_FAIL;
1339 static HRESULT xslt_doc_get_uri(const xmlChar *uri, void *_ctxt, xsltLoadType type, IUri **doc_uri)
1341 xsltStylesheetPtr style = (xsltStylesheetPtr)_ctxt;
1342 IUri *href_uri;
1343 HRESULT hr;
1344 BSTR uriW;
1346 *doc_uri = NULL;
1348 uriW = bstr_from_xmlChar(uri);
1349 hr = CreateUri(uriW, Uri_CREATE_ALLOW_RELATIVE | Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &href_uri);
1350 SysFreeString(uriW);
1351 if (FAILED(hr))
1353 WARN("Failed to create href uri, %#lx.\n", hr);
1354 return hr;
1357 if (type == XSLT_LOAD_STYLESHEET && style->doc && style->doc->name)
1359 IUri *base_uri;
1360 BSTR baseuriW;
1362 baseuriW = bstr_from_xmlChar((xmlChar *)style->doc->name);
1363 hr = CreateUri(baseuriW, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &base_uri);
1364 SysFreeString(baseuriW);
1365 if (FAILED(hr))
1367 WARN("Failed to create base uri, %#lx.\n", hr);
1368 return hr;
1371 hr = CoInternetCombineIUri(base_uri, href_uri, 0, doc_uri, 0);
1372 IUri_Release(base_uri);
1373 if (FAILED(hr))
1374 WARN("Failed to combine uris, hr %#lx.\n", hr);
1376 else
1378 *doc_uri = href_uri;
1379 IUri_AddRef(*doc_uri);
1382 IUri_Release(href_uri);
1384 return hr;
1387 xmlDocPtr xslt_doc_default_loader(const xmlChar *uri, xmlDictPtr dict, int options,
1388 void *_ctxt, xsltLoadType type)
1390 IUri *import_uri = NULL;
1391 xmlParserInputPtr input;
1392 xmlParserCtxtPtr pctxt;
1393 xmlDocPtr doc = NULL;
1394 IMoniker *moniker;
1395 HRESULT hr;
1396 bsc_t *bsc;
1397 BSTR uriW;
1399 TRACE("%s, %p, %#x, %p, %d\n", debugstr_a((const char *)uri), dict, options, _ctxt, type);
1401 pctxt = xmlNewParserCtxt();
1402 if (!pctxt)
1403 return NULL;
1405 if (dict && pctxt->dict)
1407 xmlDictFree(pctxt->dict);
1408 pctxt->dict = NULL;
1411 if (dict)
1413 pctxt->dict = dict;
1414 xmlDictReference(pctxt->dict);
1417 xmlCtxtUseOptions(pctxt, options);
1419 hr = xslt_doc_get_uri(uri, _ctxt, type, &import_uri);
1420 if (FAILED(hr))
1421 goto failed;
1423 hr = CreateURLMonikerEx2(NULL, import_uri, &moniker, 0);
1424 if (FAILED(hr))
1425 goto failed;
1427 hr = bind_url(moniker, import_loader_onDataAvailable, &input, &bsc);
1428 IMoniker_Release(moniker);
1429 if (FAILED(hr))
1430 goto failed;
1432 if (FAILED(detach_bsc(bsc)))
1433 goto failed;
1435 if (!input)
1436 goto failed;
1438 inputPush(pctxt, input);
1439 xmlParseDocument(pctxt);
1441 if (pctxt->wellFormed)
1443 doc = pctxt->myDoc;
1444 /* Set imported uri, to give nested imports a chance. */
1445 if (IUri_GetPropertyBSTR(import_uri, Uri_PROPERTY_ABSOLUTE_URI, &uriW, 0) == S_OK)
1447 doc->name = (char *)xmlchar_from_wcharn(uriW, SysStringLen(uriW), TRUE);
1448 SysFreeString(uriW);
1451 else
1453 doc = NULL;
1454 xmlFreeDoc(pctxt->myDoc);
1455 pctxt->myDoc = NULL;
1458 failed:
1459 xmlFreeParserCtxt(pctxt);
1460 if (import_uri)
1461 IUri_Release(import_uri);
1463 return doc;
1466 HRESULT node_transform_node_params(const xmlnode *This, IXMLDOMNode *stylesheet, BSTR *p,
1467 ISequentialStream *stream, const struct xslprocessor_params *params)
1469 xsltStylesheetPtr xsltSS;
1470 xmlDocPtr sheet_doc;
1471 HRESULT hr = S_OK;
1472 xmlnode *sheet;
1474 if (!stylesheet || (!p && !stream)) return E_INVALIDARG;
1476 if (p) *p = NULL;
1478 sheet = get_node_obj(stylesheet);
1479 if(!sheet) return E_FAIL;
1481 sheet_doc = xmlCopyDoc(sheet->node->doc, 1);
1482 xsltSS = xsltParseStylesheetDoc(sheet_doc);
1483 if (xsltSS)
1485 const char **xslparams = NULL;
1486 xmlDocPtr result;
1487 unsigned int i;
1489 /* convert our parameter list to libxml2 format */
1490 if (params && params->count)
1492 struct xslprocessor_par *par;
1494 i = 0;
1495 xslparams = heap_alloc((params->count*2 + 1)*sizeof(char*));
1496 LIST_FOR_EACH_ENTRY(par, &params->list, struct xslprocessor_par, entry)
1498 xslparams[i++] = (char*)xmlchar_from_wchar(par->name);
1499 xslparams[i++] = (char*)xmlchar_from_wchar(par->value);
1501 xslparams[i] = NULL;
1504 if (xslparams)
1506 xsltTransformContextPtr ctxt = xsltNewTransformContext(xsltSS, This->node->doc);
1508 /* push parameters to user context */
1509 xsltQuoteUserParams(ctxt, xslparams);
1510 result = xsltApplyStylesheetUser(xsltSS, This->node->doc, NULL, NULL, NULL, ctxt);
1511 xsltFreeTransformContext(ctxt);
1513 for (i = 0; i < params->count*2; i++)
1514 heap_free((char*)xslparams[i]);
1515 heap_free(xslparams);
1517 else
1518 result = xsltApplyStylesheet(xsltSS, This->node->doc, NULL);
1520 if (result)
1522 if (stream)
1523 hr = node_transform_write_to_stream(xsltSS, result, stream);
1524 else
1525 hr = node_transform_write_to_bstr(xsltSS, result, p);
1526 xmlFreeDoc(result);
1529 xsltFreeStylesheet(xsltSS);
1531 else
1532 xmlFreeDoc(sheet_doc);
1534 if (p && !*p) *p = SysAllocStringLen(NULL, 0);
1536 return hr;
1539 HRESULT node_transform_node(const xmlnode *node, IXMLDOMNode *stylesheet, BSTR *p)
1541 return node_transform_node_params(node, stylesheet, p, NULL, NULL);
1544 HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
1546 xmlChar* str;
1547 HRESULT hr;
1549 if (!query || !nodes) return E_INVALIDARG;
1551 str = xmlchar_from_wchar(query);
1552 hr = create_selection(This->node, str, nodes);
1553 heap_free(str);
1555 return hr;
1558 HRESULT node_select_singlenode(const xmlnode *This, BSTR query, IXMLDOMNode **node)
1560 IXMLDOMNodeList *list;
1561 HRESULT hr;
1563 hr = node_select_nodes(This, query, &list);
1564 if (hr == S_OK)
1566 hr = IXMLDOMNodeList_nextNode(list, node);
1567 IXMLDOMNodeList_Release(list);
1569 return hr;
1572 HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1574 xmlNsPtr ns = This->node->ns;
1576 if(!namespaceURI)
1577 return E_INVALIDARG;
1579 *namespaceURI = NULL;
1581 if (ns && ns->href)
1582 *namespaceURI = bstr_from_xmlChar(ns->href);
1584 TRACE("uri: %s\n", debugstr_w(*namespaceURI));
1586 return *namespaceURI ? S_OK : S_FALSE;
1589 HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1591 xmlNsPtr ns = This->node->ns;
1593 if (!prefix) return E_INVALIDARG;
1595 *prefix = NULL;
1597 if (ns && ns->prefix)
1598 *prefix = bstr_from_xmlChar(ns->prefix);
1600 TRACE("prefix: %s\n", debugstr_w(*prefix));
1602 return *prefix ? S_OK : S_FALSE;
1605 HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1607 if (!name) return E_INVALIDARG;
1609 *name = bstr_from_xmlChar(This->node->name);
1610 if (!*name) return E_OUTOFMEMORY;
1612 TRACE("returning %s\n", debugstr_w(*name));
1614 return S_OK;
1617 void destroy_xmlnode(xmlnode *This)
1619 if(This->node)
1621 xmlnode_release(This->node);
1622 xmldoc_release(This->node->doc);
1626 void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1628 if(node)
1630 xmlnode_add_ref(node);
1631 xmldoc_add_ref(node->doc);
1634 This->node = node;
1635 This->iface = node_iface;
1636 This->parent = NULL;
1638 init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1641 typedef struct {
1642 xmlnode node;
1643 IXMLDOMNode IXMLDOMNode_iface;
1644 LONG ref;
1645 } unknode;
1647 static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1649 return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1652 static HRESULT WINAPI unknode_QueryInterface(
1653 IXMLDOMNode *iface,
1654 REFIID riid,
1655 void** ppvObject )
1657 unknode *This = unknode_from_IXMLDOMNode( iface );
1659 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1661 if (IsEqualGUID(riid, &IID_IUnknown)) {
1662 *ppvObject = iface;
1663 }else if (IsEqualGUID( riid, &IID_IDispatch) ||
1664 IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1665 *ppvObject = &This->IXMLDOMNode_iface;
1666 }else if(node_query_interface(&This->node, riid, ppvObject)) {
1667 return *ppvObject ? S_OK : E_NOINTERFACE;
1668 }else {
1669 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1670 *ppvObject = NULL;
1671 return E_NOINTERFACE;
1674 IUnknown_AddRef((IUnknown*)*ppvObject);
1675 return S_OK;
1678 static ULONG WINAPI unknode_AddRef(
1679 IXMLDOMNode *iface )
1681 unknode *This = unknode_from_IXMLDOMNode( iface );
1683 return InterlockedIncrement(&This->ref);
1686 static ULONG WINAPI unknode_Release(
1687 IXMLDOMNode *iface )
1689 unknode *This = unknode_from_IXMLDOMNode( iface );
1690 LONG ref;
1692 ref = InterlockedDecrement( &This->ref );
1693 if(!ref) {
1694 destroy_xmlnode(&This->node);
1695 heap_free(This);
1698 return ref;
1701 static HRESULT WINAPI unknode_GetTypeInfoCount(
1702 IXMLDOMNode *iface,
1703 UINT* pctinfo )
1705 unknode *This = unknode_from_IXMLDOMNode( iface );
1707 TRACE("(%p)->(%p)\n", This, pctinfo);
1709 *pctinfo = 1;
1711 return S_OK;
1714 static HRESULT WINAPI unknode_GetTypeInfo(
1715 IXMLDOMNode *iface,
1716 UINT iTInfo,
1717 LCID lcid,
1718 ITypeInfo** ppTInfo )
1720 TRACE("%p, %u, %lx, %p.\n", iface, iTInfo, lcid, ppTInfo);
1722 return get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1725 static HRESULT WINAPI unknode_GetIDsOfNames(
1726 IXMLDOMNode *iface,
1727 REFIID riid,
1728 LPOLESTR* rgszNames,
1729 UINT cNames,
1730 LCID lcid,
1731 DISPID* rgDispId )
1733 ITypeInfo *typeinfo;
1734 HRESULT hr;
1736 TRACE("%p, %s, %p, %u, %lx, %p.\n", iface, debugstr_guid(riid), rgszNames, cNames,
1737 lcid, rgDispId);
1739 if(!rgszNames || cNames == 0 || !rgDispId)
1740 return E_INVALIDARG;
1742 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1743 if(SUCCEEDED(hr))
1745 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1746 ITypeInfo_Release(typeinfo);
1749 return hr;
1752 static HRESULT WINAPI unknode_Invoke(
1753 IXMLDOMNode *iface,
1754 DISPID dispIdMember,
1755 REFIID riid,
1756 LCID lcid,
1757 WORD wFlags,
1758 DISPPARAMS* pDispParams,
1759 VARIANT* pVarResult,
1760 EXCEPINFO* pExcepInfo,
1761 UINT* puArgErr )
1763 ITypeInfo *typeinfo;
1764 HRESULT hr;
1766 TRACE("%p, %ld, %s, %lx, %d, %p, %p, %p, %p.\n", iface, dispIdMember, debugstr_guid(riid),
1767 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1769 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1770 if(SUCCEEDED(hr))
1772 hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags, pDispParams,
1773 pVarResult, pExcepInfo, puArgErr);
1774 ITypeInfo_Release(typeinfo);
1777 return hr;
1780 static HRESULT WINAPI unknode_get_nodeName(
1781 IXMLDOMNode *iface,
1782 BSTR* p )
1784 unknode *This = unknode_from_IXMLDOMNode( iface );
1786 FIXME("(%p)->(%p)\n", This, p);
1788 return node_get_nodeName(&This->node, p);
1791 static HRESULT WINAPI unknode_get_nodeValue(
1792 IXMLDOMNode *iface,
1793 VARIANT* value)
1795 unknode *This = unknode_from_IXMLDOMNode( iface );
1797 FIXME("(%p)->(%p)\n", This, value);
1799 if(!value)
1800 return E_INVALIDARG;
1802 V_VT(value) = VT_NULL;
1803 return S_FALSE;
1806 static HRESULT WINAPI unknode_put_nodeValue(
1807 IXMLDOMNode *iface,
1808 VARIANT value)
1810 unknode *This = unknode_from_IXMLDOMNode( iface );
1811 FIXME("(%p)->(v%d)\n", This, V_VT(&value));
1812 return E_FAIL;
1815 static HRESULT WINAPI unknode_get_nodeType(
1816 IXMLDOMNode *iface,
1817 DOMNodeType* domNodeType )
1819 unknode *This = unknode_from_IXMLDOMNode( iface );
1821 FIXME("(%p)->(%p)\n", This, domNodeType);
1823 switch (This->node.node->type)
1825 case XML_ELEMENT_NODE:
1826 case XML_ATTRIBUTE_NODE:
1827 case XML_TEXT_NODE:
1828 case XML_CDATA_SECTION_NODE:
1829 case XML_ENTITY_REF_NODE:
1830 case XML_ENTITY_NODE:
1831 case XML_PI_NODE:
1832 case XML_COMMENT_NODE:
1833 case XML_DOCUMENT_NODE:
1834 case XML_DOCUMENT_TYPE_NODE:
1835 case XML_DOCUMENT_FRAG_NODE:
1836 case XML_NOTATION_NODE:
1837 /* we only care about this set of types, libxml2 type values are
1838 exactly what we need */
1839 *domNodeType = (DOMNodeType)This->node.node->type;
1840 break;
1841 default:
1842 *domNodeType = NODE_INVALID;
1843 break;
1846 return S_OK;
1849 static HRESULT WINAPI unknode_get_parentNode(
1850 IXMLDOMNode *iface,
1851 IXMLDOMNode** parent )
1853 unknode *This = unknode_from_IXMLDOMNode( iface );
1854 FIXME("(%p)->(%p)\n", This, parent);
1855 if (!parent) return E_INVALIDARG;
1856 *parent = NULL;
1857 return S_FALSE;
1860 static HRESULT WINAPI unknode_get_childNodes(
1861 IXMLDOMNode *iface,
1862 IXMLDOMNodeList** outList)
1864 unknode *This = unknode_from_IXMLDOMNode( iface );
1866 TRACE("(%p)->(%p)\n", This, outList);
1868 return node_get_child_nodes(&This->node, outList);
1871 static HRESULT WINAPI unknode_get_firstChild(
1872 IXMLDOMNode *iface,
1873 IXMLDOMNode** domNode)
1875 unknode *This = unknode_from_IXMLDOMNode( iface );
1877 TRACE("(%p)->(%p)\n", This, domNode);
1879 return node_get_first_child(&This->node, domNode);
1882 static HRESULT WINAPI unknode_get_lastChild(
1883 IXMLDOMNode *iface,
1884 IXMLDOMNode** domNode)
1886 unknode *This = unknode_from_IXMLDOMNode( iface );
1888 TRACE("(%p)->(%p)\n", This, domNode);
1890 return node_get_last_child(&This->node, domNode);
1893 static HRESULT WINAPI unknode_get_previousSibling(
1894 IXMLDOMNode *iface,
1895 IXMLDOMNode** domNode)
1897 unknode *This = unknode_from_IXMLDOMNode( iface );
1899 TRACE("(%p)->(%p)\n", This, domNode);
1901 return node_get_previous_sibling(&This->node, domNode);
1904 static HRESULT WINAPI unknode_get_nextSibling(
1905 IXMLDOMNode *iface,
1906 IXMLDOMNode** domNode)
1908 unknode *This = unknode_from_IXMLDOMNode( iface );
1910 TRACE("(%p)->(%p)\n", This, domNode);
1912 return node_get_next_sibling(&This->node, domNode);
1915 static HRESULT WINAPI unknode_get_attributes(
1916 IXMLDOMNode *iface,
1917 IXMLDOMNamedNodeMap** attributeMap)
1919 unknode *This = unknode_from_IXMLDOMNode( iface );
1921 FIXME("(%p)->(%p)\n", This, attributeMap);
1923 return return_null_ptr((void**)attributeMap);
1926 static HRESULT WINAPI unknode_insertBefore(
1927 IXMLDOMNode *iface,
1928 IXMLDOMNode* newNode, VARIANT refChild,
1929 IXMLDOMNode** outOldNode)
1931 unknode *This = unknode_from_IXMLDOMNode( iface );
1933 FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);
1935 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1938 static HRESULT WINAPI unknode_replaceChild(
1939 IXMLDOMNode *iface,
1940 IXMLDOMNode* newNode,
1941 IXMLDOMNode* oldNode,
1942 IXMLDOMNode** outOldNode)
1944 unknode *This = unknode_from_IXMLDOMNode( iface );
1946 FIXME("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
1948 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
1951 static HRESULT WINAPI unknode_removeChild(
1952 IXMLDOMNode *iface,
1953 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
1955 unknode *This = unknode_from_IXMLDOMNode( iface );
1956 return node_remove_child(&This->node, domNode, oldNode);
1959 static HRESULT WINAPI unknode_appendChild(
1960 IXMLDOMNode *iface,
1961 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
1963 unknode *This = unknode_from_IXMLDOMNode( iface );
1964 return node_append_child(&This->node, newNode, outNewNode);
1967 static HRESULT WINAPI unknode_hasChildNodes(
1968 IXMLDOMNode *iface,
1969 VARIANT_BOOL* pbool)
1971 unknode *This = unknode_from_IXMLDOMNode( iface );
1972 return node_has_childnodes(&This->node, pbool);
1975 static HRESULT WINAPI unknode_get_ownerDocument(
1976 IXMLDOMNode *iface,
1977 IXMLDOMDocument** domDocument)
1979 unknode *This = unknode_from_IXMLDOMNode( iface );
1980 return node_get_owner_doc(&This->node, domDocument);
1983 static HRESULT WINAPI unknode_cloneNode(
1984 IXMLDOMNode *iface,
1985 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
1987 unknode *This = unknode_from_IXMLDOMNode( iface );
1988 return node_clone(&This->node, pbool, outNode );
1991 static HRESULT WINAPI unknode_get_nodeTypeString(
1992 IXMLDOMNode *iface,
1993 BSTR* p)
1995 unknode *This = unknode_from_IXMLDOMNode( iface );
1997 FIXME("(%p)->(%p)\n", This, p);
1999 return node_get_nodeName(&This->node, p);
2002 static HRESULT WINAPI unknode_get_text(
2003 IXMLDOMNode *iface,
2004 BSTR* p)
2006 unknode *This = unknode_from_IXMLDOMNode( iface );
2007 return node_get_text(&This->node, p);
2010 static HRESULT WINAPI unknode_put_text(
2011 IXMLDOMNode *iface,
2012 BSTR p)
2014 unknode *This = unknode_from_IXMLDOMNode( iface );
2015 return node_put_text(&This->node, p);
2018 static HRESULT WINAPI unknode_get_specified(
2019 IXMLDOMNode *iface,
2020 VARIANT_BOOL* isSpecified)
2022 unknode *This = unknode_from_IXMLDOMNode( iface );
2023 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
2024 *isSpecified = VARIANT_TRUE;
2025 return S_OK;
2028 static HRESULT WINAPI unknode_get_definition(
2029 IXMLDOMNode *iface,
2030 IXMLDOMNode** definitionNode)
2032 unknode *This = unknode_from_IXMLDOMNode( iface );
2033 FIXME("(%p)->(%p)\n", This, definitionNode);
2034 return E_NOTIMPL;
2037 static HRESULT WINAPI unknode_get_nodeTypedValue(
2038 IXMLDOMNode *iface,
2039 VARIANT* var1)
2041 unknode *This = unknode_from_IXMLDOMNode( iface );
2042 FIXME("(%p)->(%p)\n", This, var1);
2043 return return_null_var(var1);
2046 static HRESULT WINAPI unknode_put_nodeTypedValue(
2047 IXMLDOMNode *iface,
2048 VARIANT typedValue)
2050 unknode *This = unknode_from_IXMLDOMNode( iface );
2051 FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
2052 return E_NOTIMPL;
2055 static HRESULT WINAPI unknode_get_dataType(
2056 IXMLDOMNode *iface,
2057 VARIANT* var1)
2059 unknode *This = unknode_from_IXMLDOMNode( iface );
2060 TRACE("(%p)->(%p)\n", This, var1);
2061 return return_null_var(var1);
2064 static HRESULT WINAPI unknode_put_dataType(
2065 IXMLDOMNode *iface,
2066 BSTR p)
2068 unknode *This = unknode_from_IXMLDOMNode( iface );
2070 FIXME("(%p)->(%s)\n", This, debugstr_w(p));
2072 if(!p)
2073 return E_INVALIDARG;
2075 return E_FAIL;
2078 static HRESULT WINAPI unknode_get_xml(
2079 IXMLDOMNode *iface,
2080 BSTR* p)
2082 unknode *This = unknode_from_IXMLDOMNode( iface );
2084 FIXME("(%p)->(%p)\n", This, p);
2086 return node_get_xml(&This->node, FALSE, p);
2089 static HRESULT WINAPI unknode_transformNode(
2090 IXMLDOMNode *iface,
2091 IXMLDOMNode* domNode, BSTR* p)
2093 unknode *This = unknode_from_IXMLDOMNode( iface );
2094 return node_transform_node(&This->node, domNode, p);
2097 static HRESULT WINAPI unknode_selectNodes(
2098 IXMLDOMNode *iface,
2099 BSTR p, IXMLDOMNodeList** outList)
2101 unknode *This = unknode_from_IXMLDOMNode( iface );
2102 return node_select_nodes(&This->node, p, outList);
2105 static HRESULT WINAPI unknode_selectSingleNode(
2106 IXMLDOMNode *iface,
2107 BSTR p, IXMLDOMNode** outNode)
2109 unknode *This = unknode_from_IXMLDOMNode( iface );
2110 return node_select_singlenode(&This->node, p, outNode);
2113 static HRESULT WINAPI unknode_get_parsed(
2114 IXMLDOMNode *iface,
2115 VARIANT_BOOL* isParsed)
2117 unknode *This = unknode_from_IXMLDOMNode( iface );
2118 FIXME("(%p)->(%p) stub!\n", This, isParsed);
2119 *isParsed = VARIANT_TRUE;
2120 return S_OK;
2123 static HRESULT WINAPI unknode_get_namespaceURI(
2124 IXMLDOMNode *iface,
2125 BSTR* p)
2127 unknode *This = unknode_from_IXMLDOMNode( iface );
2128 TRACE("(%p)->(%p)\n", This, p);
2129 return node_get_namespaceURI(&This->node, p);
2132 static HRESULT WINAPI unknode_get_prefix(
2133 IXMLDOMNode *iface,
2134 BSTR* p)
2136 unknode *This = unknode_from_IXMLDOMNode( iface );
2137 return node_get_prefix(&This->node, p);
2140 static HRESULT WINAPI unknode_get_baseName(
2141 IXMLDOMNode *iface,
2142 BSTR* p)
2144 unknode *This = unknode_from_IXMLDOMNode( iface );
2145 return node_get_base_name(&This->node, p);
2148 static HRESULT WINAPI unknode_transformNodeToObject(
2149 IXMLDOMNode *iface,
2150 IXMLDOMNode* domNode, VARIANT var1)
2152 unknode *This = unknode_from_IXMLDOMNode( iface );
2153 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
2154 return E_NOTIMPL;
2157 static const struct IXMLDOMNodeVtbl unknode_vtbl =
2159 unknode_QueryInterface,
2160 unknode_AddRef,
2161 unknode_Release,
2162 unknode_GetTypeInfoCount,
2163 unknode_GetTypeInfo,
2164 unknode_GetIDsOfNames,
2165 unknode_Invoke,
2166 unknode_get_nodeName,
2167 unknode_get_nodeValue,
2168 unknode_put_nodeValue,
2169 unknode_get_nodeType,
2170 unknode_get_parentNode,
2171 unknode_get_childNodes,
2172 unknode_get_firstChild,
2173 unknode_get_lastChild,
2174 unknode_get_previousSibling,
2175 unknode_get_nextSibling,
2176 unknode_get_attributes,
2177 unknode_insertBefore,
2178 unknode_replaceChild,
2179 unknode_removeChild,
2180 unknode_appendChild,
2181 unknode_hasChildNodes,
2182 unknode_get_ownerDocument,
2183 unknode_cloneNode,
2184 unknode_get_nodeTypeString,
2185 unknode_get_text,
2186 unknode_put_text,
2187 unknode_get_specified,
2188 unknode_get_definition,
2189 unknode_get_nodeTypedValue,
2190 unknode_put_nodeTypedValue,
2191 unknode_get_dataType,
2192 unknode_put_dataType,
2193 unknode_get_xml,
2194 unknode_transformNode,
2195 unknode_selectNodes,
2196 unknode_selectSingleNode,
2197 unknode_get_parsed,
2198 unknode_get_namespaceURI,
2199 unknode_get_prefix,
2200 unknode_get_baseName,
2201 unknode_transformNodeToObject
2204 IXMLDOMNode *create_node( xmlNodePtr node )
2206 IUnknown *pUnk;
2207 IXMLDOMNode *ret;
2208 HRESULT hr;
2210 if ( !node )
2211 return NULL;
2213 TRACE("type %d\n", node->type);
2214 switch(node->type)
2216 case XML_ELEMENT_NODE:
2217 pUnk = create_element( node );
2218 break;
2219 case XML_ATTRIBUTE_NODE:
2220 pUnk = create_attribute( node, FALSE );
2221 break;
2222 case XML_TEXT_NODE:
2223 pUnk = create_text( node );
2224 break;
2225 case XML_CDATA_SECTION_NODE:
2226 pUnk = create_cdata( node );
2227 break;
2228 case XML_ENTITY_REF_NODE:
2229 pUnk = create_doc_entity_ref( node );
2230 break;
2231 case XML_PI_NODE:
2232 pUnk = create_pi( node );
2233 break;
2234 case XML_COMMENT_NODE:
2235 pUnk = create_comment( node );
2236 break;
2237 case XML_DOCUMENT_NODE:
2238 pUnk = create_domdoc( node );
2239 break;
2240 case XML_DOCUMENT_FRAG_NODE:
2241 pUnk = create_doc_fragment( node );
2242 break;
2243 case XML_DTD_NODE:
2244 case XML_DOCUMENT_TYPE_NODE:
2245 pUnk = create_doc_type( node );
2246 break;
2247 case XML_ENTITY_NODE:
2248 case XML_NOTATION_NODE: {
2249 unknode *new_node;
2251 FIXME("only creating basic node for type %d\n", node->type);
2253 new_node = heap_alloc(sizeof(unknode));
2254 if(!new_node)
2255 return NULL;
2257 new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2258 new_node->ref = 1;
2259 init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
2260 pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2261 break;
2263 default:
2264 ERR("Called for unsupported node type %d\n", node->type);
2265 return NULL;
2268 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
2269 IUnknown_Release(pUnk);
2270 if(FAILED(hr)) return NULL;
2271 return ret;