msxml4/tests: Copy namespaces as attributes tests.
[wine.git] / dlls / msxml3 / node.c
blob4e4a9cfd1ef137ab52290858e051d16d315991df
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 if (node)
1564 *node = NULL;
1566 hr = node_select_nodes(This, query, &list);
1567 if (hr == S_OK)
1569 hr = IXMLDOMNodeList_nextNode(list, node);
1570 IXMLDOMNodeList_Release(list);
1572 return hr;
1575 HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1577 xmlNsPtr ns = This->node->ns;
1579 if(!namespaceURI)
1580 return E_INVALIDARG;
1582 *namespaceURI = NULL;
1584 if (ns && ns->href)
1585 *namespaceURI = bstr_from_xmlChar(ns->href);
1587 TRACE("uri: %s\n", debugstr_w(*namespaceURI));
1589 return *namespaceURI ? S_OK : S_FALSE;
1592 HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1594 xmlNsPtr ns = This->node->ns;
1596 if (!prefix) return E_INVALIDARG;
1598 *prefix = NULL;
1600 if (ns && ns->prefix)
1601 *prefix = bstr_from_xmlChar(ns->prefix);
1603 TRACE("prefix: %s\n", debugstr_w(*prefix));
1605 return *prefix ? S_OK : S_FALSE;
1608 HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1610 if (!name) return E_INVALIDARG;
1612 *name = bstr_from_xmlChar(This->node->name);
1613 if (!*name) return E_OUTOFMEMORY;
1615 TRACE("returning %s\n", debugstr_w(*name));
1617 return S_OK;
1620 void destroy_xmlnode(xmlnode *This)
1622 if(This->node)
1624 xmlnode_release(This->node);
1625 xmldoc_release(This->node->doc);
1629 void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1631 if(node)
1633 xmlnode_add_ref(node);
1634 xmldoc_add_ref(node->doc);
1637 This->node = node;
1638 This->iface = node_iface;
1639 This->parent = NULL;
1641 init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1644 typedef struct {
1645 xmlnode node;
1646 IXMLDOMNode IXMLDOMNode_iface;
1647 LONG ref;
1648 } unknode;
1650 static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1652 return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1655 static HRESULT WINAPI unknode_QueryInterface(
1656 IXMLDOMNode *iface,
1657 REFIID riid,
1658 void** ppvObject )
1660 unknode *This = unknode_from_IXMLDOMNode( iface );
1662 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1664 if (IsEqualGUID(riid, &IID_IUnknown)) {
1665 *ppvObject = iface;
1666 }else if (IsEqualGUID( riid, &IID_IDispatch) ||
1667 IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1668 *ppvObject = &This->IXMLDOMNode_iface;
1669 }else if(node_query_interface(&This->node, riid, ppvObject)) {
1670 return *ppvObject ? S_OK : E_NOINTERFACE;
1671 }else {
1672 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1673 *ppvObject = NULL;
1674 return E_NOINTERFACE;
1677 IUnknown_AddRef((IUnknown*)*ppvObject);
1678 return S_OK;
1681 static ULONG WINAPI unknode_AddRef(
1682 IXMLDOMNode *iface )
1684 unknode *This = unknode_from_IXMLDOMNode( iface );
1686 return InterlockedIncrement(&This->ref);
1689 static ULONG WINAPI unknode_Release(
1690 IXMLDOMNode *iface )
1692 unknode *This = unknode_from_IXMLDOMNode( iface );
1693 LONG ref;
1695 ref = InterlockedDecrement( &This->ref );
1696 if(!ref) {
1697 destroy_xmlnode(&This->node);
1698 heap_free(This);
1701 return ref;
1704 static HRESULT WINAPI unknode_GetTypeInfoCount(
1705 IXMLDOMNode *iface,
1706 UINT* pctinfo )
1708 unknode *This = unknode_from_IXMLDOMNode( iface );
1710 TRACE("(%p)->(%p)\n", This, pctinfo);
1712 *pctinfo = 1;
1714 return S_OK;
1717 static HRESULT WINAPI unknode_GetTypeInfo(
1718 IXMLDOMNode *iface,
1719 UINT iTInfo,
1720 LCID lcid,
1721 ITypeInfo** ppTInfo )
1723 TRACE("%p, %u, %lx, %p.\n", iface, iTInfo, lcid, ppTInfo);
1725 return get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1728 static HRESULT WINAPI unknode_GetIDsOfNames(
1729 IXMLDOMNode *iface,
1730 REFIID riid,
1731 LPOLESTR* rgszNames,
1732 UINT cNames,
1733 LCID lcid,
1734 DISPID* rgDispId )
1736 ITypeInfo *typeinfo;
1737 HRESULT hr;
1739 TRACE("%p, %s, %p, %u, %lx, %p.\n", iface, debugstr_guid(riid), rgszNames, cNames,
1740 lcid, rgDispId);
1742 if(!rgszNames || cNames == 0 || !rgDispId)
1743 return E_INVALIDARG;
1745 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1746 if(SUCCEEDED(hr))
1748 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1749 ITypeInfo_Release(typeinfo);
1752 return hr;
1755 static HRESULT WINAPI unknode_Invoke(
1756 IXMLDOMNode *iface,
1757 DISPID dispIdMember,
1758 REFIID riid,
1759 LCID lcid,
1760 WORD wFlags,
1761 DISPPARAMS* pDispParams,
1762 VARIANT* pVarResult,
1763 EXCEPINFO* pExcepInfo,
1764 UINT* puArgErr )
1766 ITypeInfo *typeinfo;
1767 HRESULT hr;
1769 TRACE("%p, %ld, %s, %lx, %d, %p, %p, %p, %p.\n", iface, dispIdMember, debugstr_guid(riid),
1770 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1772 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1773 if(SUCCEEDED(hr))
1775 hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags, pDispParams,
1776 pVarResult, pExcepInfo, puArgErr);
1777 ITypeInfo_Release(typeinfo);
1780 return hr;
1783 static HRESULT WINAPI unknode_get_nodeName(
1784 IXMLDOMNode *iface,
1785 BSTR* p )
1787 unknode *This = unknode_from_IXMLDOMNode( iface );
1789 FIXME("(%p)->(%p)\n", This, p);
1791 return node_get_nodeName(&This->node, p);
1794 static HRESULT WINAPI unknode_get_nodeValue(
1795 IXMLDOMNode *iface,
1796 VARIANT* value)
1798 unknode *This = unknode_from_IXMLDOMNode( iface );
1800 FIXME("(%p)->(%p)\n", This, value);
1802 if(!value)
1803 return E_INVALIDARG;
1805 V_VT(value) = VT_NULL;
1806 return S_FALSE;
1809 static HRESULT WINAPI unknode_put_nodeValue(
1810 IXMLDOMNode *iface,
1811 VARIANT value)
1813 unknode *This = unknode_from_IXMLDOMNode( iface );
1814 FIXME("(%p)->(v%d)\n", This, V_VT(&value));
1815 return E_FAIL;
1818 static HRESULT WINAPI unknode_get_nodeType(
1819 IXMLDOMNode *iface,
1820 DOMNodeType* domNodeType )
1822 unknode *This = unknode_from_IXMLDOMNode( iface );
1824 FIXME("(%p)->(%p)\n", This, domNodeType);
1826 switch (This->node.node->type)
1828 case XML_ELEMENT_NODE:
1829 case XML_ATTRIBUTE_NODE:
1830 case XML_TEXT_NODE:
1831 case XML_CDATA_SECTION_NODE:
1832 case XML_ENTITY_REF_NODE:
1833 case XML_ENTITY_NODE:
1834 case XML_PI_NODE:
1835 case XML_COMMENT_NODE:
1836 case XML_DOCUMENT_NODE:
1837 case XML_DOCUMENT_TYPE_NODE:
1838 case XML_DOCUMENT_FRAG_NODE:
1839 case XML_NOTATION_NODE:
1840 /* we only care about this set of types, libxml2 type values are
1841 exactly what we need */
1842 *domNodeType = (DOMNodeType)This->node.node->type;
1843 break;
1844 default:
1845 *domNodeType = NODE_INVALID;
1846 break;
1849 return S_OK;
1852 static HRESULT WINAPI unknode_get_parentNode(
1853 IXMLDOMNode *iface,
1854 IXMLDOMNode** parent )
1856 unknode *This = unknode_from_IXMLDOMNode( iface );
1857 FIXME("(%p)->(%p)\n", This, parent);
1858 if (!parent) return E_INVALIDARG;
1859 *parent = NULL;
1860 return S_FALSE;
1863 static HRESULT WINAPI unknode_get_childNodes(
1864 IXMLDOMNode *iface,
1865 IXMLDOMNodeList** outList)
1867 unknode *This = unknode_from_IXMLDOMNode( iface );
1869 TRACE("(%p)->(%p)\n", This, outList);
1871 return node_get_child_nodes(&This->node, outList);
1874 static HRESULT WINAPI unknode_get_firstChild(
1875 IXMLDOMNode *iface,
1876 IXMLDOMNode** domNode)
1878 unknode *This = unknode_from_IXMLDOMNode( iface );
1880 TRACE("(%p)->(%p)\n", This, domNode);
1882 return node_get_first_child(&This->node, domNode);
1885 static HRESULT WINAPI unknode_get_lastChild(
1886 IXMLDOMNode *iface,
1887 IXMLDOMNode** domNode)
1889 unknode *This = unknode_from_IXMLDOMNode( iface );
1891 TRACE("(%p)->(%p)\n", This, domNode);
1893 return node_get_last_child(&This->node, domNode);
1896 static HRESULT WINAPI unknode_get_previousSibling(
1897 IXMLDOMNode *iface,
1898 IXMLDOMNode** domNode)
1900 unknode *This = unknode_from_IXMLDOMNode( iface );
1902 TRACE("(%p)->(%p)\n", This, domNode);
1904 return node_get_previous_sibling(&This->node, domNode);
1907 static HRESULT WINAPI unknode_get_nextSibling(
1908 IXMLDOMNode *iface,
1909 IXMLDOMNode** domNode)
1911 unknode *This = unknode_from_IXMLDOMNode( iface );
1913 TRACE("(%p)->(%p)\n", This, domNode);
1915 return node_get_next_sibling(&This->node, domNode);
1918 static HRESULT WINAPI unknode_get_attributes(
1919 IXMLDOMNode *iface,
1920 IXMLDOMNamedNodeMap** attributeMap)
1922 unknode *This = unknode_from_IXMLDOMNode( iface );
1924 FIXME("(%p)->(%p)\n", This, attributeMap);
1926 return return_null_ptr((void**)attributeMap);
1929 static HRESULT WINAPI unknode_insertBefore(
1930 IXMLDOMNode *iface,
1931 IXMLDOMNode* newNode, VARIANT refChild,
1932 IXMLDOMNode** outOldNode)
1934 unknode *This = unknode_from_IXMLDOMNode( iface );
1936 FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);
1938 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1941 static HRESULT WINAPI unknode_replaceChild(
1942 IXMLDOMNode *iface,
1943 IXMLDOMNode* newNode,
1944 IXMLDOMNode* oldNode,
1945 IXMLDOMNode** outOldNode)
1947 unknode *This = unknode_from_IXMLDOMNode( iface );
1949 FIXME("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
1951 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
1954 static HRESULT WINAPI unknode_removeChild(
1955 IXMLDOMNode *iface,
1956 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
1958 unknode *This = unknode_from_IXMLDOMNode( iface );
1959 return node_remove_child(&This->node, domNode, oldNode);
1962 static HRESULT WINAPI unknode_appendChild(
1963 IXMLDOMNode *iface,
1964 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
1966 unknode *This = unknode_from_IXMLDOMNode( iface );
1967 return node_append_child(&This->node, newNode, outNewNode);
1970 static HRESULT WINAPI unknode_hasChildNodes(
1971 IXMLDOMNode *iface,
1972 VARIANT_BOOL* pbool)
1974 unknode *This = unknode_from_IXMLDOMNode( iface );
1975 return node_has_childnodes(&This->node, pbool);
1978 static HRESULT WINAPI unknode_get_ownerDocument(
1979 IXMLDOMNode *iface,
1980 IXMLDOMDocument** domDocument)
1982 unknode *This = unknode_from_IXMLDOMNode( iface );
1983 return node_get_owner_doc(&This->node, domDocument);
1986 static HRESULT WINAPI unknode_cloneNode(
1987 IXMLDOMNode *iface,
1988 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
1990 unknode *This = unknode_from_IXMLDOMNode( iface );
1991 return node_clone(&This->node, pbool, outNode );
1994 static HRESULT WINAPI unknode_get_nodeTypeString(
1995 IXMLDOMNode *iface,
1996 BSTR* p)
1998 unknode *This = unknode_from_IXMLDOMNode( iface );
2000 FIXME("(%p)->(%p)\n", This, p);
2002 return node_get_nodeName(&This->node, p);
2005 static HRESULT WINAPI unknode_get_text(
2006 IXMLDOMNode *iface,
2007 BSTR* p)
2009 unknode *This = unknode_from_IXMLDOMNode( iface );
2010 return node_get_text(&This->node, p);
2013 static HRESULT WINAPI unknode_put_text(
2014 IXMLDOMNode *iface,
2015 BSTR p)
2017 unknode *This = unknode_from_IXMLDOMNode( iface );
2018 return node_put_text(&This->node, p);
2021 static HRESULT WINAPI unknode_get_specified(
2022 IXMLDOMNode *iface,
2023 VARIANT_BOOL* isSpecified)
2025 unknode *This = unknode_from_IXMLDOMNode( iface );
2026 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
2027 *isSpecified = VARIANT_TRUE;
2028 return S_OK;
2031 static HRESULT WINAPI unknode_get_definition(
2032 IXMLDOMNode *iface,
2033 IXMLDOMNode** definitionNode)
2035 unknode *This = unknode_from_IXMLDOMNode( iface );
2036 FIXME("(%p)->(%p)\n", This, definitionNode);
2037 return E_NOTIMPL;
2040 static HRESULT WINAPI unknode_get_nodeTypedValue(
2041 IXMLDOMNode *iface,
2042 VARIANT* var1)
2044 unknode *This = unknode_from_IXMLDOMNode( iface );
2045 FIXME("(%p)->(%p)\n", This, var1);
2046 return return_null_var(var1);
2049 static HRESULT WINAPI unknode_put_nodeTypedValue(
2050 IXMLDOMNode *iface,
2051 VARIANT typedValue)
2053 unknode *This = unknode_from_IXMLDOMNode( iface );
2054 FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
2055 return E_NOTIMPL;
2058 static HRESULT WINAPI unknode_get_dataType(
2059 IXMLDOMNode *iface,
2060 VARIANT* var1)
2062 unknode *This = unknode_from_IXMLDOMNode( iface );
2063 TRACE("(%p)->(%p)\n", This, var1);
2064 return return_null_var(var1);
2067 static HRESULT WINAPI unknode_put_dataType(
2068 IXMLDOMNode *iface,
2069 BSTR p)
2071 unknode *This = unknode_from_IXMLDOMNode( iface );
2073 FIXME("(%p)->(%s)\n", This, debugstr_w(p));
2075 if(!p)
2076 return E_INVALIDARG;
2078 return E_FAIL;
2081 static HRESULT WINAPI unknode_get_xml(
2082 IXMLDOMNode *iface,
2083 BSTR* p)
2085 unknode *This = unknode_from_IXMLDOMNode( iface );
2087 FIXME("(%p)->(%p)\n", This, p);
2089 return node_get_xml(&This->node, FALSE, p);
2092 static HRESULT WINAPI unknode_transformNode(
2093 IXMLDOMNode *iface,
2094 IXMLDOMNode* domNode, BSTR* p)
2096 unknode *This = unknode_from_IXMLDOMNode( iface );
2097 return node_transform_node(&This->node, domNode, p);
2100 static HRESULT WINAPI unknode_selectNodes(
2101 IXMLDOMNode *iface,
2102 BSTR p, IXMLDOMNodeList** outList)
2104 unknode *This = unknode_from_IXMLDOMNode( iface );
2105 return node_select_nodes(&This->node, p, outList);
2108 static HRESULT WINAPI unknode_selectSingleNode(
2109 IXMLDOMNode *iface,
2110 BSTR p, IXMLDOMNode** outNode)
2112 unknode *This = unknode_from_IXMLDOMNode( iface );
2113 return node_select_singlenode(&This->node, p, outNode);
2116 static HRESULT WINAPI unknode_get_parsed(
2117 IXMLDOMNode *iface,
2118 VARIANT_BOOL* isParsed)
2120 unknode *This = unknode_from_IXMLDOMNode( iface );
2121 FIXME("(%p)->(%p) stub!\n", This, isParsed);
2122 *isParsed = VARIANT_TRUE;
2123 return S_OK;
2126 static HRESULT WINAPI unknode_get_namespaceURI(
2127 IXMLDOMNode *iface,
2128 BSTR* p)
2130 unknode *This = unknode_from_IXMLDOMNode( iface );
2131 TRACE("(%p)->(%p)\n", This, p);
2132 return node_get_namespaceURI(&This->node, p);
2135 static HRESULT WINAPI unknode_get_prefix(
2136 IXMLDOMNode *iface,
2137 BSTR* p)
2139 unknode *This = unknode_from_IXMLDOMNode( iface );
2140 return node_get_prefix(&This->node, p);
2143 static HRESULT WINAPI unknode_get_baseName(
2144 IXMLDOMNode *iface,
2145 BSTR* p)
2147 unknode *This = unknode_from_IXMLDOMNode( iface );
2148 return node_get_base_name(&This->node, p);
2151 static HRESULT WINAPI unknode_transformNodeToObject(
2152 IXMLDOMNode *iface,
2153 IXMLDOMNode* domNode, VARIANT var1)
2155 unknode *This = unknode_from_IXMLDOMNode( iface );
2156 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
2157 return E_NOTIMPL;
2160 static const struct IXMLDOMNodeVtbl unknode_vtbl =
2162 unknode_QueryInterface,
2163 unknode_AddRef,
2164 unknode_Release,
2165 unknode_GetTypeInfoCount,
2166 unknode_GetTypeInfo,
2167 unknode_GetIDsOfNames,
2168 unknode_Invoke,
2169 unknode_get_nodeName,
2170 unknode_get_nodeValue,
2171 unknode_put_nodeValue,
2172 unknode_get_nodeType,
2173 unknode_get_parentNode,
2174 unknode_get_childNodes,
2175 unknode_get_firstChild,
2176 unknode_get_lastChild,
2177 unknode_get_previousSibling,
2178 unknode_get_nextSibling,
2179 unknode_get_attributes,
2180 unknode_insertBefore,
2181 unknode_replaceChild,
2182 unknode_removeChild,
2183 unknode_appendChild,
2184 unknode_hasChildNodes,
2185 unknode_get_ownerDocument,
2186 unknode_cloneNode,
2187 unknode_get_nodeTypeString,
2188 unknode_get_text,
2189 unknode_put_text,
2190 unknode_get_specified,
2191 unknode_get_definition,
2192 unknode_get_nodeTypedValue,
2193 unknode_put_nodeTypedValue,
2194 unknode_get_dataType,
2195 unknode_put_dataType,
2196 unknode_get_xml,
2197 unknode_transformNode,
2198 unknode_selectNodes,
2199 unknode_selectSingleNode,
2200 unknode_get_parsed,
2201 unknode_get_namespaceURI,
2202 unknode_get_prefix,
2203 unknode_get_baseName,
2204 unknode_transformNodeToObject
2207 IXMLDOMNode *create_node( xmlNodePtr node )
2209 IUnknown *pUnk;
2210 IXMLDOMNode *ret;
2211 HRESULT hr;
2213 if ( !node )
2214 return NULL;
2216 TRACE("type %d\n", node->type);
2217 switch(node->type)
2219 case XML_ELEMENT_NODE:
2220 pUnk = create_element( node );
2221 break;
2222 case XML_ATTRIBUTE_NODE:
2223 pUnk = create_attribute( node, FALSE );
2224 break;
2225 case XML_TEXT_NODE:
2226 pUnk = create_text( node );
2227 break;
2228 case XML_CDATA_SECTION_NODE:
2229 pUnk = create_cdata( node );
2230 break;
2231 case XML_ENTITY_REF_NODE:
2232 pUnk = create_doc_entity_ref( node );
2233 break;
2234 case XML_PI_NODE:
2235 pUnk = create_pi( node );
2236 break;
2237 case XML_COMMENT_NODE:
2238 pUnk = create_comment( node );
2239 break;
2240 case XML_DOCUMENT_NODE:
2241 pUnk = create_domdoc( node );
2242 break;
2243 case XML_DOCUMENT_FRAG_NODE:
2244 pUnk = create_doc_fragment( node );
2245 break;
2246 case XML_DTD_NODE:
2247 case XML_DOCUMENT_TYPE_NODE:
2248 pUnk = create_doc_type( node );
2249 break;
2250 case XML_ENTITY_NODE:
2251 case XML_NOTATION_NODE: {
2252 unknode *new_node;
2254 FIXME("only creating basic node for type %d\n", node->type);
2256 new_node = heap_alloc(sizeof(unknode));
2257 if(!new_node)
2258 return NULL;
2260 new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2261 new_node->ref = 1;
2262 init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
2263 pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2264 break;
2266 default:
2267 ERR("Called for unsupported node type %d\n", node->type);
2268 return NULL;
2271 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
2272 IUnknown_Release(pUnk);
2273 if(FAILED(hr)) return NULL;
2274 return ret;