mshtml: Don't use fire_event to dispatch contextmenu event.
[wine.git] / dlls / msxml3 / node.c
blobece82efa10eb3cde89b3d6076372ad25687dc3a9
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 #include "config.h"
23 #define COBJMACROS
25 #include <stdarg.h>
27 #ifdef HAVE_LIBXML2
28 # include <libxml/parser.h>
29 # include <libxml/xmlerror.h>
30 # include <libxml/HTMLtree.h>
31 # ifdef SONAME_LIBXSLT
32 # ifdef HAVE_LIBXSLT_PATTERN_H
33 # include <libxslt/pattern.h>
34 # endif
35 # ifdef HAVE_LIBXSLT_TRANSFORM_H
36 # include <libxslt/transform.h>
37 # endif
38 # include <libxslt/imports.h>
39 # include <libxslt/variables.h>
40 # include <libxslt/xsltutils.h>
41 # include <libxslt/xsltInternals.h>
42 # endif
43 #endif
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "winnls.h"
49 #include "ole2.h"
50 #include "msxml6.h"
52 #include "msxml_private.h"
54 #include "wine/debug.h"
56 #ifdef HAVE_LIBXML2
58 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
60 #ifdef SONAME_LIBXSLT
61 extern void* libxslt_handle;
62 # define MAKE_FUNCPTR(f) extern typeof(f) * p##f
63 MAKE_FUNCPTR(xsltApplyStylesheet);
64 MAKE_FUNCPTR(xsltApplyStylesheetUser);
65 MAKE_FUNCPTR(xsltCleanupGlobals);
66 MAKE_FUNCPTR(xsltFreeStylesheet);
67 MAKE_FUNCPTR(xsltFreeTransformContext);
68 MAKE_FUNCPTR(xsltNewTransformContext);
69 MAKE_FUNCPTR(xsltNextImport);
70 MAKE_FUNCPTR(xsltParseStylesheetDoc);
71 MAKE_FUNCPTR(xsltQuoteUserParams);
72 MAKE_FUNCPTR(xsltSaveResultTo);
73 # undef MAKE_FUNCPTR
74 #else
75 WINE_DECLARE_DEBUG_CHANNEL(winediag);
76 #endif
78 static const IID IID_xmlnode = {0x4f2f4ba2,0xb822,0x11df,{0x8b,0x8a,0x68,0x50,0xdf,0xd7,0x20,0x85}};
80 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
82 xmlnode *This;
84 if ( !iface )
85 return NULL;
86 This = get_node_obj( iface );
87 if ( !This || !This->node )
88 return NULL;
89 if ( type && This->node->type != type )
90 return NULL;
91 return This->node;
94 BOOL node_query_interface(xmlnode *This, REFIID riid, void **ppv)
96 if(IsEqualGUID(&IID_xmlnode, riid)) {
97 TRACE("(%p)->(IID_xmlnode %p)\n", This, ppv);
98 *ppv = This;
99 return TRUE;
102 return dispex_query_interface(&This->dispex, riid, ppv);
105 /* common ISupportErrorInfo implementation */
106 typedef struct {
107 ISupportErrorInfo ISupportErrorInfo_iface;
108 LONG ref;
110 const tid_t* iids;
111 } SupportErrorInfo;
113 static inline SupportErrorInfo *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
115 return CONTAINING_RECORD(iface, SupportErrorInfo, ISupportErrorInfo_iface);
118 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **obj)
120 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
121 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
123 *obj = NULL;
125 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISupportErrorInfo)) {
126 *obj = iface;
127 ISupportErrorInfo_AddRef(iface);
128 return S_OK;
131 return E_NOINTERFACE;
134 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
136 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
137 ULONG ref = InterlockedIncrement(&This->ref);
138 TRACE("(%p)->(%d)\n", This, ref );
139 return ref;
142 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
144 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
145 LONG ref = InterlockedDecrement(&This->ref);
147 TRACE("(%p)->(%d)\n", This, ref);
149 if (ref == 0)
150 heap_free(This);
152 return ref;
155 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
157 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
158 enum tid_t const *tid;
160 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
162 tid = This->iids;
163 while (*tid != NULL_tid)
165 if (IsEqualGUID(riid, get_riid_from_tid(*tid)))
166 return S_OK;
167 tid++;
170 return S_FALSE;
173 static const struct ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
174 SupportErrorInfo_QueryInterface,
175 SupportErrorInfo_AddRef,
176 SupportErrorInfo_Release,
177 SupportErrorInfo_InterfaceSupportsErrorInfo
180 HRESULT node_create_supporterrorinfo(enum tid_t const *iids, void **obj)
182 SupportErrorInfo *This;
184 This = heap_alloc(sizeof(*This));
185 if (!This) return E_OUTOFMEMORY;
187 This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
188 This->ref = 1;
189 This->iids = iids;
191 *obj = &This->ISupportErrorInfo_iface;
193 return S_OK;
196 xmlnode *get_node_obj(IXMLDOMNode *node)
198 xmlnode *obj = NULL;
199 HRESULT hres;
201 hres = IXMLDOMNode_QueryInterface(node, &IID_xmlnode, (void**)&obj);
202 if (!obj) WARN("node is not our IXMLDOMNode implementation\n");
203 return SUCCEEDED(hres) ? obj : NULL;
206 HRESULT node_get_nodeName(xmlnode *This, BSTR *name)
208 BSTR prefix, base;
209 HRESULT hr;
211 if (!name)
212 return E_INVALIDARG;
214 hr = node_get_base_name(This, &base);
215 if (hr != S_OK) return hr;
217 hr = node_get_prefix(This, &prefix);
218 if (hr == S_OK)
220 static const WCHAR colW = ':';
221 WCHAR *ptr;
223 /* +1 for ':' */
224 ptr = *name = SysAllocStringLen(NULL, SysStringLen(base) + SysStringLen(prefix) + 1);
225 memcpy(ptr, prefix, SysStringByteLen(prefix));
226 ptr += SysStringLen(prefix);
227 memcpy(ptr++, &colW, sizeof(WCHAR));
228 memcpy(ptr, base, SysStringByteLen(base));
230 SysFreeString(base);
231 SysFreeString(prefix);
233 else
234 *name = base;
236 return S_OK;
239 HRESULT node_get_content(xmlnode *This, VARIANT *value)
241 xmlChar *content;
243 if(!value)
244 return E_INVALIDARG;
246 content = xmlNodeGetContent(This->node);
247 V_VT(value) = VT_BSTR;
248 V_BSTR(value) = bstr_from_xmlChar( content );
249 xmlFree(content);
251 TRACE("%p returned %s\n", This, debugstr_w(V_BSTR(value)));
252 return S_OK;
255 HRESULT node_set_content(xmlnode *This, LPCWSTR value)
257 xmlChar *str;
259 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
260 str = xmlchar_from_wchar(value);
261 if(!str)
262 return E_OUTOFMEMORY;
264 xmlNodeSetContent(This->node, str);
265 heap_free(str);
266 return S_OK;
269 static HRESULT node_set_content_escaped(xmlnode *This, LPCWSTR value)
271 xmlChar *str, *escaped;
273 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
274 str = xmlchar_from_wchar(value);
275 if(!str)
276 return E_OUTOFMEMORY;
278 escaped = xmlEncodeSpecialChars(NULL, str);
279 if(!escaped)
281 heap_free(str);
282 return E_OUTOFMEMORY;
285 xmlNodeSetContent(This->node, escaped);
287 heap_free(str);
288 xmlFree(escaped);
290 return S_OK;
293 HRESULT node_put_value(xmlnode *This, VARIANT *value)
295 HRESULT hr;
297 if (V_VT(value) != VT_BSTR)
299 VARIANT string_value;
301 VariantInit(&string_value);
302 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
303 if(FAILED(hr)) {
304 WARN("Couldn't convert to VT_BSTR\n");
305 return hr;
308 hr = node_set_content(This, V_BSTR(&string_value));
309 VariantClear(&string_value);
311 else
312 hr = node_set_content(This, V_BSTR(value));
314 return hr;
317 HRESULT node_put_value_escaped(xmlnode *This, VARIANT *value)
319 HRESULT hr;
321 if (V_VT(value) != VT_BSTR)
323 VARIANT string_value;
325 VariantInit(&string_value);
326 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
327 if(FAILED(hr)) {
328 WARN("Couldn't convert to VT_BSTR\n");
329 return hr;
332 hr = node_set_content_escaped(This, V_BSTR(&string_value));
333 VariantClear(&string_value);
335 else
336 hr = node_set_content_escaped(This, V_BSTR(value));
338 return hr;
341 static HRESULT get_node(
342 xmlnode *This,
343 const char *name,
344 xmlNodePtr node,
345 IXMLDOMNode **out )
347 TRACE("(%p)->(%s %p %p)\n", This, name, node, out );
349 if ( !out )
350 return E_INVALIDARG;
352 /* if we don't have a doc, use our parent. */
353 if(node && !node->doc && node->parent)
354 node->doc = node->parent->doc;
356 *out = create_node( node );
357 if (!*out)
358 return S_FALSE;
359 return S_OK;
362 HRESULT node_get_parent(xmlnode *This, IXMLDOMNode **parent)
364 return get_node( This, "parent", This->node->parent, parent );
367 HRESULT node_get_child_nodes(xmlnode *This, IXMLDOMNodeList **ret)
369 if(!ret)
370 return E_INVALIDARG;
372 *ret = create_children_nodelist(This->node);
373 if(!*ret)
374 return E_OUTOFMEMORY;
376 return S_OK;
379 HRESULT node_get_first_child(xmlnode *This, IXMLDOMNode **ret)
381 return get_node(This, "firstChild", This->node->children, ret);
384 HRESULT node_get_last_child(xmlnode *This, IXMLDOMNode **ret)
386 return get_node(This, "lastChild", This->node->last, ret);
389 HRESULT node_get_previous_sibling(xmlnode *This, IXMLDOMNode **ret)
391 return get_node(This, "previous", This->node->prev, ret);
394 HRESULT node_get_next_sibling(xmlnode *This, IXMLDOMNode **ret)
396 return get_node(This, "next", This->node->next, ret);
399 static int node_get_inst_cnt(xmlNodePtr node)
401 int ret = *(LONG *)&node->_private & NODE_PRIV_REFCOUNT_MASK;
402 xmlNodePtr child;
404 /* add attribute counts */
405 if (node->type == XML_ELEMENT_NODE)
407 xmlAttrPtr prop = node->properties;
409 while (prop)
411 ret += node_get_inst_cnt((xmlNodePtr)prop);
412 prop = prop->next;
416 /* add children counts */
417 child = node->children;
418 while (child)
420 ret += node_get_inst_cnt(child);
421 child = child->next;
424 return ret;
427 int xmlnode_get_inst_cnt(xmlnode *node)
429 return node_get_inst_cnt(node->node);
432 /* _private field holds a number of COM instances spawned from this libxml2 node
433 * most significant bits are used to store information about ignorrable whitespace nodes */
434 void xmlnode_add_ref(xmlNodePtr node)
436 if (node->type == XML_DOCUMENT_NODE) return;
437 InterlockedIncrement((LONG*)&node->_private);
440 void xmlnode_release(xmlNodePtr node)
442 if (node->type == XML_DOCUMENT_NODE) return;
443 InterlockedDecrement((LONG*)&node->_private);
446 HRESULT node_insert_before(xmlnode *This, IXMLDOMNode *new_child, const VARIANT *ref_child,
447 IXMLDOMNode **ret)
449 IXMLDOMNode *before = NULL;
450 xmlnode *node_obj;
451 int refcount = 0;
452 xmlDocPtr doc;
453 HRESULT hr;
455 if(!new_child)
456 return E_INVALIDARG;
458 node_obj = get_node_obj(new_child);
459 if(!node_obj) return E_FAIL;
461 switch(V_VT(ref_child))
463 case VT_EMPTY:
464 case VT_NULL:
465 break;
467 case VT_UNKNOWN:
468 case VT_DISPATCH:
469 if (V_UNKNOWN(ref_child))
471 hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
472 if(FAILED(hr)) return hr;
474 break;
476 default:
477 FIXME("refChild var type %x\n", V_VT(ref_child));
478 return E_FAIL;
481 TRACE("new child %p, This->node %p\n", node_obj->node, This->node);
483 if(!node_obj->node->parent)
484 if(xmldoc_remove_orphan(node_obj->node->doc, node_obj->node) != S_OK)
485 WARN("%p is not an orphan of %p\n", node_obj->node, node_obj->node->doc);
487 refcount = xmlnode_get_inst_cnt(node_obj);
489 if(before)
491 xmlnode *before_node_obj = get_node_obj(before);
492 IXMLDOMNode_Release(before);
493 if(!before_node_obj) return E_FAIL;
496 /* unlink from current parent first */
497 if(node_obj->parent)
499 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
500 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
502 doc = node_obj->node->doc;
504 if(before)
506 xmlNodePtr new_node;
507 xmlnode *before_node_obj = get_node_obj(before);
509 /* refs count including subtree */
510 if (doc != before_node_obj->node->doc)
511 refcount = xmlnode_get_inst_cnt(node_obj);
513 if (refcount) xmldoc_add_refs(before_node_obj->node->doc, refcount);
514 new_node = xmlAddPrevSibling(before_node_obj->node, node_obj->node);
515 if (new_node != node_obj->node)
517 if (refcount != 1)
518 FIXME("referenced xmlNode was freed, expect crashes\n");
519 xmlnode_add_ref(new_node);
520 node_obj->node = new_node;
522 if (refcount) xmldoc_release_refs(doc, refcount);
523 node_obj->parent = This->parent;
525 else
527 xmlNodePtr new_node;
529 if (doc != This->node->doc)
530 refcount = xmlnode_get_inst_cnt(node_obj);
532 if (refcount) xmldoc_add_refs(This->node->doc, refcount);
533 /* xmlAddChild doesn't unlink node from previous parent */
534 xmlUnlinkNode(node_obj->node);
535 new_node = xmlAddChild(This->node, node_obj->node);
536 if (new_node != node_obj->node)
538 if (refcount != 1)
539 FIXME("referenced xmlNode was freed, expect crashes\n");
540 xmlnode_add_ref(new_node);
541 node_obj->node = new_node;
543 if (refcount) xmldoc_release_refs(doc, refcount);
544 node_obj->parent = This->iface;
547 if(ret)
549 IXMLDOMNode_AddRef(new_child);
550 *ret = new_child;
553 TRACE("ret S_OK\n");
554 return S_OK;
557 HRESULT node_replace_child(xmlnode *This, IXMLDOMNode *newChild, IXMLDOMNode *oldChild,
558 IXMLDOMNode **ret)
560 xmlnode *old_child, *new_child;
561 xmlDocPtr leaving_doc;
562 xmlNode *my_ancestor;
563 int refcount = 0;
565 /* Do not believe any documentation telling that newChild == NULL
566 means removal. It does certainly *not* apply to msxml3! */
567 if(!newChild || !oldChild)
568 return E_INVALIDARG;
570 if(ret)
571 *ret = NULL;
573 old_child = get_node_obj(oldChild);
574 if(!old_child) return E_FAIL;
576 if(old_child->node->parent != This->node)
578 WARN("childNode %p is not a child of %p\n", oldChild, This);
579 return E_INVALIDARG;
582 new_child = get_node_obj(newChild);
583 if(!new_child) return E_FAIL;
585 my_ancestor = This->node;
586 while(my_ancestor)
588 if(my_ancestor == new_child->node)
590 WARN("tried to create loop\n");
591 return E_FAIL;
593 my_ancestor = my_ancestor->parent;
596 if(!new_child->node->parent)
597 if(xmldoc_remove_orphan(new_child->node->doc, new_child->node) != S_OK)
598 WARN("%p is not an orphan of %p\n", new_child->node, new_child->node->doc);
600 leaving_doc = new_child->node->doc;
602 if (leaving_doc != old_child->node->doc)
603 refcount = xmlnode_get_inst_cnt(new_child);
605 if (refcount) xmldoc_add_refs(old_child->node->doc, refcount);
606 xmlReplaceNode(old_child->node, new_child->node);
607 if (refcount) xmldoc_release_refs(leaving_doc, refcount);
608 new_child->parent = old_child->parent;
609 old_child->parent = NULL;
611 xmldoc_add_orphan(old_child->node->doc, old_child->node);
613 if(ret)
615 IXMLDOMNode_AddRef(oldChild);
616 *ret = oldChild;
619 return S_OK;
622 HRESULT node_remove_child(xmlnode *This, IXMLDOMNode* child, IXMLDOMNode** oldChild)
624 xmlnode *child_node;
626 if(!child) return E_INVALIDARG;
628 if(oldChild)
629 *oldChild = NULL;
631 child_node = get_node_obj(child);
632 if(!child_node) return E_FAIL;
634 if(child_node->node->parent != This->node)
636 WARN("childNode %p is not a child of %p\n", child, This);
637 return E_INVALIDARG;
640 xmlUnlinkNode(child_node->node);
641 child_node->parent = NULL;
642 xmldoc_add_orphan(child_node->node->doc, child_node->node);
644 if(oldChild)
646 IXMLDOMNode_AddRef(child);
647 *oldChild = child;
650 return S_OK;
653 HRESULT node_append_child(xmlnode *This, IXMLDOMNode *child, IXMLDOMNode **outChild)
655 DOMNodeType type;
656 VARIANT var;
657 HRESULT hr;
659 if (!child)
660 return E_INVALIDARG;
662 hr = IXMLDOMNode_get_nodeType(child, &type);
663 if(FAILED(hr) || type == NODE_ATTRIBUTE) {
664 if (outChild) *outChild = NULL;
665 return E_FAIL;
668 VariantInit(&var);
669 return IXMLDOMNode_insertBefore(This->iface, child, var, outChild);
672 HRESULT node_has_childnodes(const xmlnode *This, VARIANT_BOOL *ret)
674 if (!ret) return E_INVALIDARG;
676 if (!This->node->children)
678 *ret = VARIANT_FALSE;
679 return S_FALSE;
682 *ret = VARIANT_TRUE;
683 return S_OK;
686 HRESULT node_get_owner_doc(const xmlnode *This, IXMLDOMDocument **doc)
688 if(!doc)
689 return E_INVALIDARG;
690 return get_domdoc_from_xmldoc(This->node->doc, (IXMLDOMDocument3**)doc);
693 HRESULT node_clone(xmlnode *This, VARIANT_BOOL deep, IXMLDOMNode **cloneNode)
695 IXMLDOMNode *node;
696 xmlNodePtr clone;
698 if(!cloneNode) return E_INVALIDARG;
700 clone = xmlCopyNode(This->node, deep ? 1 : 2);
701 if (clone)
703 xmlSetTreeDoc(clone, This->node->doc);
704 xmldoc_add_orphan(clone->doc, clone);
706 node = create_node(clone);
707 if (!node)
709 ERR("Copy failed\n");
710 xmldoc_remove_orphan(clone->doc, clone);
711 xmlFreeNode(clone);
712 return E_FAIL;
715 *cloneNode = node;
717 else
719 ERR("Copy failed\n");
720 return E_FAIL;
723 return S_OK;
726 static xmlChar* do_get_text(xmlNodePtr node, BOOL trim, DWORD *first, DWORD *last, BOOL *trail_ig_ws)
728 xmlNodePtr child;
729 xmlChar* str;
730 BOOL preserving = is_preserving_whitespace(node);
732 *first = -1;
733 *last = 0;
735 if (!node->children)
737 str = xmlNodeGetContent(node);
738 *trail_ig_ws = *(DWORD*)&node->_private & NODE_PRIV_CHILD_IGNORABLE_WS;
740 else
742 BOOL ig_ws = FALSE;
743 xmlChar* tmp;
744 DWORD pos = 0;
745 str = xmlStrdup(BAD_CAST "");
747 if (node->type != XML_DOCUMENT_NODE)
748 ig_ws = *(DWORD*)&node->_private & NODE_PRIV_CHILD_IGNORABLE_WS;
749 *trail_ig_ws = FALSE;
751 for (child = node->children; child != NULL; child = child->next)
753 switch (child->type)
755 case XML_ELEMENT_NODE: {
756 DWORD node_first, node_last;
758 tmp = do_get_text(child, FALSE, &node_first, &node_last, trail_ig_ws);
760 if (node_first!=-1 && pos+node_first<*first)
761 *first = pos+node_first;
762 if (node_last && pos+node_last>*last)
763 *last = pos+node_last;
764 break;
766 case XML_TEXT_NODE:
767 tmp = xmlNodeGetContent(child);
768 if (!preserving && tmp[0])
770 xmlChar *beg;
772 for (beg = tmp; *beg; beg++)
773 if (!isspace(*beg)) break;
775 if (!*beg)
777 ig_ws = TRUE;
778 xmlFree(tmp);
779 tmp = NULL;
782 break;
783 case XML_CDATA_SECTION_NODE:
784 case XML_ENTITY_REF_NODE:
785 case XML_ENTITY_NODE:
786 tmp = xmlNodeGetContent(child);
787 break;
788 default:
789 tmp = NULL;
790 break;
793 if ((tmp && *tmp) || child->type==XML_CDATA_SECTION_NODE)
795 if (ig_ws && str[0])
797 str = xmlStrcat(str, BAD_CAST " ");
798 pos++;
800 if (tmp && *tmp) str = xmlStrcat(str, tmp);
801 if (child->type==XML_CDATA_SECTION_NODE && pos<*first)
802 *first = pos;
803 if (tmp && *tmp) pos += xmlStrlen(tmp);
804 if (child->type==XML_CDATA_SECTION_NODE && pos>*last)
805 *last = pos;
806 ig_ws = FALSE;
808 if (tmp) xmlFree(tmp);
810 if (!ig_ws)
812 ig_ws = *(DWORD*)&child->_private & NODE_PRIV_TRAILING_IGNORABLE_WS;
814 if (!ig_ws)
815 ig_ws = *trail_ig_ws;
816 *trail_ig_ws = FALSE;
819 *trail_ig_ws = ig_ws;
822 switch (node->type)
824 case XML_ELEMENT_NODE:
825 case XML_TEXT_NODE:
826 case XML_ENTITY_REF_NODE:
827 case XML_ENTITY_NODE:
828 case XML_DOCUMENT_NODE:
829 case XML_DOCUMENT_FRAG_NODE:
830 if (trim && !preserving)
832 xmlChar* ret;
833 int len;
835 if (!str)
836 break;
838 for (ret = str; *ret && isspace(*ret) && (*first)--; ret++)
839 if (*last) (*last)--;
840 for (len = xmlStrlen(ret)-1; len >= 0 && len >= *last; len--)
841 if(!isspace(ret[len])) break;
843 ret = xmlStrndup(ret, len+1);
844 xmlFree(str);
845 str = ret;
846 break;
848 break;
849 default:
850 break;
853 return str;
856 HRESULT node_get_text(const xmlnode *This, BSTR *text)
858 BSTR str = NULL;
859 xmlChar *content;
860 DWORD first, last;
861 BOOL tmp;
863 if (!text) return E_INVALIDARG;
865 content = do_get_text(This->node, TRUE, &first, &last, &tmp);
866 if (content)
868 str = bstr_from_xmlChar(content);
869 xmlFree(content);
872 /* Always return a string. */
873 if (!str) str = SysAllocStringLen( NULL, 0 );
875 TRACE("%p %s\n", This, debugstr_w(str) );
876 *text = str;
878 return S_OK;
881 HRESULT node_put_text(xmlnode *This, BSTR text)
883 xmlChar *str, *str2;
885 TRACE("(%p)->(%s)\n", This, debugstr_w(text));
887 str = xmlchar_from_wchar(text);
889 /* Escape the string. */
890 str2 = xmlEncodeEntitiesReentrant(This->node->doc, str);
891 heap_free(str);
893 xmlNodeSetContent(This->node, str2);
894 xmlFree(str2);
896 return S_OK;
899 BSTR EnsureCorrectEOL(BSTR sInput)
901 int nNum = 0;
902 BSTR sNew;
903 int nLen;
904 int i;
906 nLen = SysStringLen(sInput);
907 /* Count line endings */
908 for(i=0; i < nLen; i++)
910 if(sInput[i] == '\n')
911 nNum++;
914 TRACE("len=%d, num=%d\n", nLen, nNum);
916 /* Add linefeed as needed */
917 if(nNum > 0)
919 int nPlace = 0;
920 sNew = SysAllocStringLen(NULL, nLen + nNum);
921 for(i=0; i < nLen; i++)
923 if(sInput[i] == '\n')
925 sNew[i+nPlace] = '\r';
926 nPlace++;
928 sNew[i+nPlace] = sInput[i];
931 SysFreeString(sInput);
933 else
935 sNew = sInput;
938 TRACE("len %d\n", SysStringLen(sNew));
940 return sNew;
944 * We are trying to replicate the same behaviour as msxml by converting
945 * line endings to \r\n and using indents as \t. The problem is that msxml
946 * only formats nodes that have a line ending. Using libxml we cannot
947 * reproduce behaviour exactly.
950 HRESULT node_get_xml(xmlnode *This, BOOL ensure_eol, BSTR *ret)
952 xmlBufferPtr xml_buf;
953 xmlNodePtr xmldecl;
954 int size;
956 if(!ret)
957 return E_INVALIDARG;
959 *ret = NULL;
961 xml_buf = xmlBufferCreate();
962 if(!xml_buf)
963 return E_OUTOFMEMORY;
965 xmldecl = xmldoc_unlink_xmldecl( This->node->doc );
967 size = xmlNodeDump(xml_buf, This->node->doc, This->node, 0, 1);
968 if(size > 0) {
969 const xmlChar *buf_content;
970 BSTR content;
972 /* Attribute Nodes return a space in front of their name */
973 buf_content = xmlBufferContent(xml_buf);
975 content = bstr_from_xmlChar(buf_content + (buf_content[0] == ' ' ? 1 : 0));
976 if(ensure_eol)
977 content = EnsureCorrectEOL(content);
979 *ret = content;
980 }else {
981 *ret = SysAllocStringLen(NULL, 0);
984 xmlBufferFree(xml_buf);
985 xmldoc_link_xmldecl( This->node->doc, xmldecl );
986 return *ret ? S_OK : E_OUTOFMEMORY;
989 #ifdef SONAME_LIBXSLT
991 /* duplicates xmlBufferWriteQuotedString() logic */
992 static void xml_write_quotedstring(xmlOutputBufferPtr buf, const xmlChar *string)
994 const xmlChar *cur, *base;
996 if (xmlStrchr(string, '\"'))
998 if (xmlStrchr(string, '\''))
1000 xmlOutputBufferWrite(buf, 1, "\"");
1001 base = cur = string;
1003 while (*cur)
1005 if (*cur == '"')
1007 if (base != cur)
1008 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
1009 xmlOutputBufferWrite(buf, 6, "&quot;");
1010 cur++;
1011 base = cur;
1013 else
1014 cur++;
1016 if (base != cur)
1017 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
1018 xmlOutputBufferWrite(buf, 1, "\"");
1020 else
1022 xmlOutputBufferWrite(buf, 1, "\'");
1023 xmlOutputBufferWriteString(buf, (const char*)string);
1024 xmlOutputBufferWrite(buf, 1, "\'");
1027 else
1029 xmlOutputBufferWrite(buf, 1, "\"");
1030 xmlOutputBufferWriteString(buf, (const char*)string);
1031 xmlOutputBufferWrite(buf, 1, "\"");
1035 static int XMLCALL transform_to_stream_write(void *context, const char *buffer, int len)
1037 DWORD written;
1038 HRESULT hr = ISequentialStream_Write((ISequentialStream *)context, buffer, len, &written);
1039 return hr == S_OK ? written : -1;
1042 /* Output for method "text" */
1043 static void transform_write_text(xmlDocPtr result, xsltStylesheetPtr style, xmlOutputBufferPtr output)
1045 xmlNodePtr cur = result->children;
1046 while (cur)
1048 if (cur->type == XML_TEXT_NODE)
1049 xmlOutputBufferWriteString(output, (const char*)cur->content);
1051 /* skip to next node */
1052 if (cur->children)
1054 if ((cur->children->type != XML_ENTITY_DECL) &&
1055 (cur->children->type != XML_ENTITY_REF_NODE) &&
1056 (cur->children->type != XML_ENTITY_NODE))
1058 cur = cur->children;
1059 continue;
1063 if (cur->next) {
1064 cur = cur->next;
1065 continue;
1070 cur = cur->parent;
1071 if (cur == NULL)
1072 break;
1073 if (cur == (xmlNodePtr) style->doc) {
1074 cur = NULL;
1075 break;
1077 if (cur->next) {
1078 cur = cur->next;
1079 break;
1081 } while (cur);
1085 #undef XSLT_GET_IMPORT_PTR
1086 #define XSLT_GET_IMPORT_PTR(res, style, name) { \
1087 xsltStylesheetPtr st = style; \
1088 res = NULL; \
1089 while (st != NULL) { \
1090 if (st->name != NULL) { res = st->name; break; } \
1091 st = pxsltNextImport(st); \
1094 #undef XSLT_GET_IMPORT_INT
1095 #define XSLT_GET_IMPORT_INT(res, style, name) { \
1096 xsltStylesheetPtr st = style; \
1097 res = -1; \
1098 while (st != NULL) { \
1099 if (st->name != -1) { res = st->name; break; } \
1100 st = pxsltNextImport(st); \
1103 static void transform_write_xmldecl(xmlDocPtr result, xsltStylesheetPtr style, BOOL omit_encoding, xmlOutputBufferPtr output)
1105 int omit_xmldecl, standalone;
1107 XSLT_GET_IMPORT_INT(omit_xmldecl, style, omitXmlDeclaration);
1108 if (omit_xmldecl == 1) return;
1110 XSLT_GET_IMPORT_INT(standalone, style, standalone);
1112 xmlOutputBufferWriteString(output, "<?xml version=");
1113 if (result->version)
1115 xmlOutputBufferWriteString(output, "\"");
1116 xmlOutputBufferWriteString(output, (const char *)result->version);
1117 xmlOutputBufferWriteString(output, "\"");
1119 else
1120 xmlOutputBufferWriteString(output, "\"1.0\"");
1122 if (!omit_encoding)
1124 const xmlChar *encoding;
1126 /* default encoding is UTF-16 */
1127 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1128 xmlOutputBufferWriteString(output, " encoding=");
1129 xmlOutputBufferWriteString(output, "\"");
1130 xmlOutputBufferWriteString(output, encoding ? (const char *)encoding : "UTF-16");
1131 xmlOutputBufferWriteString(output, "\"");
1134 /* standalone attribute */
1135 if (standalone != -1)
1136 xmlOutputBufferWriteString(output, standalone == 0 ? " standalone=\"no\"" : " standalone=\"yes\"");
1138 xmlOutputBufferWriteString(output, "?>");
1141 static void htmldtd_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc)
1143 xmlDtdPtr cur = doc->intSubset;
1145 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
1146 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1147 if (cur->ExternalID)
1149 xmlOutputBufferWriteString(buf, " PUBLIC ");
1150 xml_write_quotedstring(buf, cur->ExternalID);
1151 if (cur->SystemID)
1153 xmlOutputBufferWriteString(buf, " ");
1154 xml_write_quotedstring(buf, cur->SystemID);
1157 else if (cur->SystemID)
1159 xmlOutputBufferWriteString(buf, " SYSTEM ");
1160 xml_write_quotedstring(buf, cur->SystemID);
1162 xmlOutputBufferWriteString(buf, ">\n");
1165 /* Duplicates htmlDocContentDumpFormatOutput() the way we need it - doesn't add trailing newline. */
1166 static void htmldoc_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding, int format)
1168 xmlElementType type;
1170 /* force HTML output */
1171 type = doc->type;
1172 doc->type = XML_HTML_DOCUMENT_NODE;
1173 if (doc->intSubset)
1174 htmldtd_dumpcontent(buf, doc);
1175 if (doc->children) {
1176 xmlNodePtr cur = doc->children;
1177 while (cur) {
1178 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
1179 cur = cur->next;
1182 doc->type = type;
1185 static inline BOOL transform_is_empty_resultdoc(xmlDocPtr result)
1187 return !result->children || ((result->children->type == XML_DTD_NODE) && !result->children->next);
1190 static inline BOOL transform_is_valid_method(xsltStylesheetPtr style)
1192 return !style->methodURI || !(style->method && xmlStrEqual(style->method, (const xmlChar *)"xhtml"));
1195 /* Helper to write transformation result to specified output buffer. */
1196 static HRESULT node_transform_write(xsltStylesheetPtr style, xmlDocPtr result, BOOL omit_encoding, const char *encoding, xmlOutputBufferPtr output)
1198 const xmlChar *method;
1199 int indent;
1201 if (!transform_is_valid_method(style))
1203 ERR("unknown output method\n");
1204 return E_FAIL;
1207 XSLT_GET_IMPORT_PTR(method, style, method)
1208 XSLT_GET_IMPORT_INT(indent, style, indent);
1210 if (!method && (result->type == XML_HTML_DOCUMENT_NODE))
1211 method = (const xmlChar *) "html";
1213 if (method && xmlStrEqual(method, (const xmlChar *)"html"))
1215 htmlSetMetaEncoding(result, (const xmlChar *)encoding);
1216 if (indent == -1)
1217 indent = 1;
1218 htmldoc_dumpcontent(output, result, (const char*)encoding, indent);
1220 else if (method && xmlStrEqual(method, (const xmlChar *)"xhtml"))
1222 htmlSetMetaEncoding(result, (const xmlChar *) encoding);
1223 htmlDocContentDumpOutput(output, result, encoding);
1225 else if (method && xmlStrEqual(method, (const xmlChar *)"text"))
1226 transform_write_text(result, style, output);
1227 else
1229 transform_write_xmldecl(result, style, omit_encoding, output);
1231 if (result->children)
1233 xmlNodePtr child = result->children;
1235 while (child)
1237 xmlNodeDumpOutput(output, result, child, 0, indent == 1, encoding);
1238 if (indent && ((child->type == XML_DTD_NODE) || ((child->type == XML_COMMENT_NODE) && child->next)))
1239 xmlOutputBufferWriteString(output, "\r\n");
1240 child = child->next;
1245 xmlOutputBufferFlush(output);
1246 return S_OK;
1249 /* For BSTR output is always UTF-16, without 'encoding' attribute */
1250 static HRESULT node_transform_write_to_bstr(xsltStylesheetPtr style, xmlDocPtr result, BSTR *str)
1252 HRESULT hr = S_OK;
1254 if (transform_is_empty_resultdoc(result))
1255 *str = SysAllocStringLen(NULL, 0);
1256 else
1258 xmlOutputBufferPtr output = xmlAllocOutputBuffer(xmlFindCharEncodingHandler("UTF-16"));
1259 const xmlChar *content;
1260 size_t len;
1262 *str = NULL;
1263 if (!output)
1264 return E_OUTOFMEMORY;
1266 hr = node_transform_write(style, result, TRUE, "UTF-16", output);
1267 #ifdef LIBXML2_NEW_BUFFER
1268 content = xmlBufContent(output->conv);
1269 len = xmlBufUse(output->conv);
1270 #else
1271 content = xmlBufferContent(output->conv);
1272 len = xmlBufferLength(output->conv);
1273 #endif
1274 /* UTF-16 encoder places UTF-16 bom, we don't need it for BSTR */
1275 content += sizeof(WCHAR);
1276 *str = SysAllocStringLen((WCHAR*)content, len/sizeof(WCHAR) - 1);
1277 xmlOutputBufferClose(output);
1280 return *str ? hr : E_OUTOFMEMORY;
1283 static HRESULT node_transform_write_to_stream(xsltStylesheetPtr style, xmlDocPtr result, ISequentialStream *stream)
1285 static const xmlChar *utf16 = (const xmlChar*)"UTF-16";
1286 xmlOutputBufferPtr output;
1287 const xmlChar *encoding;
1288 HRESULT hr;
1290 if (transform_is_empty_resultdoc(result))
1292 WARN("empty result document\n");
1293 return S_OK;
1296 if (style->methodURI && (!style->method || !xmlStrEqual(style->method, (const xmlChar *) "xhtml")))
1298 ERR("unknown output method\n");
1299 return E_FAIL;
1302 /* default encoding is UTF-16 */
1303 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1304 if (!encoding)
1305 encoding = utf16;
1307 output = xmlOutputBufferCreateIO(transform_to_stream_write, NULL, stream, xmlFindCharEncodingHandler((const char*)encoding));
1308 if (!output)
1309 return E_OUTOFMEMORY;
1311 hr = node_transform_write(style, result, FALSE, (const char*)encoding, output);
1312 xmlOutputBufferClose(output);
1313 return hr;
1316 #endif
1318 HRESULT node_transform_node_params(const xmlnode *This, IXMLDOMNode *stylesheet, BSTR *p,
1319 ISequentialStream *stream, const struct xslprocessor_params *params)
1321 #ifdef SONAME_LIBXSLT
1322 xsltStylesheetPtr xsltSS;
1323 xmlDocPtr sheet_doc;
1324 HRESULT hr = S_OK;
1325 xmlnode *sheet;
1327 if (!libxslt_handle) return E_NOTIMPL;
1328 if (!stylesheet || !p) return E_INVALIDARG;
1330 *p = NULL;
1332 sheet = get_node_obj(stylesheet);
1333 if(!sheet) return E_FAIL;
1335 sheet_doc = xmlCopyDoc(sheet->node->doc, 1);
1336 xsltSS = pxsltParseStylesheetDoc(sheet_doc);
1337 if (xsltSS)
1339 const char **xslparams = NULL;
1340 xmlDocPtr result;
1341 unsigned int i;
1343 /* convert our parameter list to libxml2 format */
1344 if (params && params->count)
1346 struct xslprocessor_par *par;
1348 i = 0;
1349 xslparams = heap_alloc((params->count*2 + 1)*sizeof(char*));
1350 LIST_FOR_EACH_ENTRY(par, &params->list, struct xslprocessor_par, entry)
1352 xslparams[i++] = (char*)xmlchar_from_wchar(par->name);
1353 xslparams[i++] = (char*)xmlchar_from_wchar(par->value);
1355 xslparams[i] = NULL;
1358 if (xslparams)
1360 xsltTransformContextPtr ctxt = pxsltNewTransformContext(xsltSS, This->node->doc);
1362 /* push parameters to user context */
1363 pxsltQuoteUserParams(ctxt, xslparams);
1364 result = pxsltApplyStylesheetUser(xsltSS, This->node->doc, NULL, NULL, NULL, ctxt);
1365 pxsltFreeTransformContext(ctxt);
1367 for (i = 0; i < params->count*2; i++)
1368 heap_free((char*)xslparams[i]);
1369 heap_free(xslparams);
1371 else
1372 result = pxsltApplyStylesheet(xsltSS, This->node->doc, NULL);
1374 if (result)
1376 if (stream)
1377 hr = node_transform_write_to_stream(xsltSS, result, stream);
1378 else
1379 hr = node_transform_write_to_bstr(xsltSS, result, p);
1380 xmlFreeDoc(result);
1383 pxsltFreeStylesheet(xsltSS);
1385 else
1386 xmlFreeDoc(sheet_doc);
1388 if(!*p) *p = SysAllocStringLen(NULL, 0);
1390 return hr;
1391 #else
1392 ERR_(winediag)("libxslt headers were not found at compile time. Expect problems.\n");
1394 return E_NOTIMPL;
1395 #endif
1398 HRESULT node_transform_node(const xmlnode *node, IXMLDOMNode *stylesheet, BSTR *p)
1400 return node_transform_node_params(node, stylesheet, p, NULL, NULL);
1403 HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
1405 xmlChar* str;
1406 HRESULT hr;
1408 if (!query || !nodes) return E_INVALIDARG;
1410 str = xmlchar_from_wchar(query);
1411 hr = create_selection(This->node, str, nodes);
1412 heap_free(str);
1414 return hr;
1417 HRESULT node_select_singlenode(const xmlnode *This, BSTR query, IXMLDOMNode **node)
1419 IXMLDOMNodeList *list;
1420 HRESULT hr;
1422 hr = node_select_nodes(This, query, &list);
1423 if (hr == S_OK)
1425 hr = IXMLDOMNodeList_nextNode(list, node);
1426 IXMLDOMNodeList_Release(list);
1428 return hr;
1431 HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1433 xmlNsPtr ns = This->node->ns;
1435 if(!namespaceURI)
1436 return E_INVALIDARG;
1438 *namespaceURI = NULL;
1440 if (ns && ns->href)
1441 *namespaceURI = bstr_from_xmlChar(ns->href);
1443 TRACE("uri: %s\n", debugstr_w(*namespaceURI));
1445 return *namespaceURI ? S_OK : S_FALSE;
1448 HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1450 xmlNsPtr ns = This->node->ns;
1452 if (!prefix) return E_INVALIDARG;
1454 *prefix = NULL;
1456 if (ns && ns->prefix)
1457 *prefix = bstr_from_xmlChar(ns->prefix);
1459 TRACE("prefix: %s\n", debugstr_w(*prefix));
1461 return *prefix ? S_OK : S_FALSE;
1464 HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1466 if (!name) return E_INVALIDARG;
1468 *name = bstr_from_xmlChar(This->node->name);
1469 if (!*name) return E_OUTOFMEMORY;
1471 TRACE("returning %s\n", debugstr_w(*name));
1473 return S_OK;
1476 void destroy_xmlnode(xmlnode *This)
1478 if(This->node)
1480 xmlnode_release(This->node);
1481 xmldoc_release(This->node->doc);
1485 void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1487 if(node)
1489 xmlnode_add_ref(node);
1490 xmldoc_add_ref(node->doc);
1493 This->node = node;
1494 This->iface = node_iface;
1495 This->parent = NULL;
1497 init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1500 typedef struct {
1501 xmlnode node;
1502 IXMLDOMNode IXMLDOMNode_iface;
1503 LONG ref;
1504 } unknode;
1506 static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1508 return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1511 static HRESULT WINAPI unknode_QueryInterface(
1512 IXMLDOMNode *iface,
1513 REFIID riid,
1514 void** ppvObject )
1516 unknode *This = unknode_from_IXMLDOMNode( iface );
1518 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1520 if (IsEqualGUID(riid, &IID_IUnknown)) {
1521 *ppvObject = iface;
1522 }else if (IsEqualGUID( riid, &IID_IDispatch) ||
1523 IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1524 *ppvObject = &This->IXMLDOMNode_iface;
1525 }else if(node_query_interface(&This->node, riid, ppvObject)) {
1526 return *ppvObject ? S_OK : E_NOINTERFACE;
1527 }else {
1528 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1529 *ppvObject = NULL;
1530 return E_NOINTERFACE;
1533 IUnknown_AddRef((IUnknown*)*ppvObject);
1534 return S_OK;
1537 static ULONG WINAPI unknode_AddRef(
1538 IXMLDOMNode *iface )
1540 unknode *This = unknode_from_IXMLDOMNode( iface );
1542 return InterlockedIncrement(&This->ref);
1545 static ULONG WINAPI unknode_Release(
1546 IXMLDOMNode *iface )
1548 unknode *This = unknode_from_IXMLDOMNode( iface );
1549 LONG ref;
1551 ref = InterlockedDecrement( &This->ref );
1552 if(!ref) {
1553 destroy_xmlnode(&This->node);
1554 heap_free(This);
1557 return ref;
1560 static HRESULT WINAPI unknode_GetTypeInfoCount(
1561 IXMLDOMNode *iface,
1562 UINT* pctinfo )
1564 unknode *This = unknode_from_IXMLDOMNode( iface );
1566 TRACE("(%p)->(%p)\n", This, pctinfo);
1568 *pctinfo = 1;
1570 return S_OK;
1573 static HRESULT WINAPI unknode_GetTypeInfo(
1574 IXMLDOMNode *iface,
1575 UINT iTInfo,
1576 LCID lcid,
1577 ITypeInfo** ppTInfo )
1579 unknode *This = unknode_from_IXMLDOMNode( iface );
1580 HRESULT hr;
1582 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1584 hr = get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1586 return hr;
1589 static HRESULT WINAPI unknode_GetIDsOfNames(
1590 IXMLDOMNode *iface,
1591 REFIID riid,
1592 LPOLESTR* rgszNames,
1593 UINT cNames,
1594 LCID lcid,
1595 DISPID* rgDispId )
1597 unknode *This = unknode_from_IXMLDOMNode( iface );
1599 ITypeInfo *typeinfo;
1600 HRESULT hr;
1602 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1603 lcid, rgDispId);
1605 if(!rgszNames || cNames == 0 || !rgDispId)
1606 return E_INVALIDARG;
1608 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1609 if(SUCCEEDED(hr))
1611 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1612 ITypeInfo_Release(typeinfo);
1615 return hr;
1618 static HRESULT WINAPI unknode_Invoke(
1619 IXMLDOMNode *iface,
1620 DISPID dispIdMember,
1621 REFIID riid,
1622 LCID lcid,
1623 WORD wFlags,
1624 DISPPARAMS* pDispParams,
1625 VARIANT* pVarResult,
1626 EXCEPINFO* pExcepInfo,
1627 UINT* puArgErr )
1629 unknode *This = unknode_from_IXMLDOMNode( iface );
1630 ITypeInfo *typeinfo;
1631 HRESULT hr;
1633 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1634 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1636 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1637 if(SUCCEEDED(hr))
1639 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDOMNode_iface, dispIdMember, wFlags, pDispParams,
1640 pVarResult, pExcepInfo, puArgErr);
1641 ITypeInfo_Release(typeinfo);
1644 return hr;
1647 static HRESULT WINAPI unknode_get_nodeName(
1648 IXMLDOMNode *iface,
1649 BSTR* p )
1651 unknode *This = unknode_from_IXMLDOMNode( iface );
1653 FIXME("(%p)->(%p)\n", This, p);
1655 return node_get_nodeName(&This->node, p);
1658 static HRESULT WINAPI unknode_get_nodeValue(
1659 IXMLDOMNode *iface,
1660 VARIANT* value)
1662 unknode *This = unknode_from_IXMLDOMNode( iface );
1664 FIXME("(%p)->(%p)\n", This, value);
1666 if(!value)
1667 return E_INVALIDARG;
1669 V_VT(value) = VT_NULL;
1670 return S_FALSE;
1673 static HRESULT WINAPI unknode_put_nodeValue(
1674 IXMLDOMNode *iface,
1675 VARIANT value)
1677 unknode *This = unknode_from_IXMLDOMNode( iface );
1678 FIXME("(%p)->(v%d)\n", This, V_VT(&value));
1679 return E_FAIL;
1682 static HRESULT WINAPI unknode_get_nodeType(
1683 IXMLDOMNode *iface,
1684 DOMNodeType* domNodeType )
1686 unknode *This = unknode_from_IXMLDOMNode( iface );
1688 FIXME("(%p)->(%p)\n", This, domNodeType);
1690 switch (This->node.node->type)
1692 case XML_ELEMENT_NODE:
1693 case XML_ATTRIBUTE_NODE:
1694 case XML_TEXT_NODE:
1695 case XML_CDATA_SECTION_NODE:
1696 case XML_ENTITY_REF_NODE:
1697 case XML_ENTITY_NODE:
1698 case XML_PI_NODE:
1699 case XML_COMMENT_NODE:
1700 case XML_DOCUMENT_NODE:
1701 case XML_DOCUMENT_TYPE_NODE:
1702 case XML_DOCUMENT_FRAG_NODE:
1703 case XML_NOTATION_NODE:
1704 /* we only care about this set of types, libxml2 type values are
1705 exactly what we need */
1706 *domNodeType = (DOMNodeType)This->node.node->type;
1707 break;
1708 default:
1709 *domNodeType = NODE_INVALID;
1710 break;
1713 return S_OK;
1716 static HRESULT WINAPI unknode_get_parentNode(
1717 IXMLDOMNode *iface,
1718 IXMLDOMNode** parent )
1720 unknode *This = unknode_from_IXMLDOMNode( iface );
1721 FIXME("(%p)->(%p)\n", This, parent);
1722 if (!parent) return E_INVALIDARG;
1723 *parent = NULL;
1724 return S_FALSE;
1727 static HRESULT WINAPI unknode_get_childNodes(
1728 IXMLDOMNode *iface,
1729 IXMLDOMNodeList** outList)
1731 unknode *This = unknode_from_IXMLDOMNode( iface );
1733 TRACE("(%p)->(%p)\n", This, outList);
1735 return node_get_child_nodes(&This->node, outList);
1738 static HRESULT WINAPI unknode_get_firstChild(
1739 IXMLDOMNode *iface,
1740 IXMLDOMNode** domNode)
1742 unknode *This = unknode_from_IXMLDOMNode( iface );
1744 TRACE("(%p)->(%p)\n", This, domNode);
1746 return node_get_first_child(&This->node, domNode);
1749 static HRESULT WINAPI unknode_get_lastChild(
1750 IXMLDOMNode *iface,
1751 IXMLDOMNode** domNode)
1753 unknode *This = unknode_from_IXMLDOMNode( iface );
1755 TRACE("(%p)->(%p)\n", This, domNode);
1757 return node_get_last_child(&This->node, domNode);
1760 static HRESULT WINAPI unknode_get_previousSibling(
1761 IXMLDOMNode *iface,
1762 IXMLDOMNode** domNode)
1764 unknode *This = unknode_from_IXMLDOMNode( iface );
1766 TRACE("(%p)->(%p)\n", This, domNode);
1768 return node_get_previous_sibling(&This->node, domNode);
1771 static HRESULT WINAPI unknode_get_nextSibling(
1772 IXMLDOMNode *iface,
1773 IXMLDOMNode** domNode)
1775 unknode *This = unknode_from_IXMLDOMNode( iface );
1777 TRACE("(%p)->(%p)\n", This, domNode);
1779 return node_get_next_sibling(&This->node, domNode);
1782 static HRESULT WINAPI unknode_get_attributes(
1783 IXMLDOMNode *iface,
1784 IXMLDOMNamedNodeMap** attributeMap)
1786 unknode *This = unknode_from_IXMLDOMNode( iface );
1788 FIXME("(%p)->(%p)\n", This, attributeMap);
1790 return return_null_ptr((void**)attributeMap);
1793 static HRESULT WINAPI unknode_insertBefore(
1794 IXMLDOMNode *iface,
1795 IXMLDOMNode* newNode, VARIANT refChild,
1796 IXMLDOMNode** outOldNode)
1798 unknode *This = unknode_from_IXMLDOMNode( iface );
1800 FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);
1802 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1805 static HRESULT WINAPI unknode_replaceChild(
1806 IXMLDOMNode *iface,
1807 IXMLDOMNode* newNode,
1808 IXMLDOMNode* oldNode,
1809 IXMLDOMNode** outOldNode)
1811 unknode *This = unknode_from_IXMLDOMNode( iface );
1813 FIXME("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
1815 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
1818 static HRESULT WINAPI unknode_removeChild(
1819 IXMLDOMNode *iface,
1820 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
1822 unknode *This = unknode_from_IXMLDOMNode( iface );
1823 return node_remove_child(&This->node, domNode, oldNode);
1826 static HRESULT WINAPI unknode_appendChild(
1827 IXMLDOMNode *iface,
1828 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
1830 unknode *This = unknode_from_IXMLDOMNode( iface );
1831 return node_append_child(&This->node, newNode, outNewNode);
1834 static HRESULT WINAPI unknode_hasChildNodes(
1835 IXMLDOMNode *iface,
1836 VARIANT_BOOL* pbool)
1838 unknode *This = unknode_from_IXMLDOMNode( iface );
1839 return node_has_childnodes(&This->node, pbool);
1842 static HRESULT WINAPI unknode_get_ownerDocument(
1843 IXMLDOMNode *iface,
1844 IXMLDOMDocument** domDocument)
1846 unknode *This = unknode_from_IXMLDOMNode( iface );
1847 return node_get_owner_doc(&This->node, domDocument);
1850 static HRESULT WINAPI unknode_cloneNode(
1851 IXMLDOMNode *iface,
1852 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
1854 unknode *This = unknode_from_IXMLDOMNode( iface );
1855 return node_clone(&This->node, pbool, outNode );
1858 static HRESULT WINAPI unknode_get_nodeTypeString(
1859 IXMLDOMNode *iface,
1860 BSTR* p)
1862 unknode *This = unknode_from_IXMLDOMNode( iface );
1864 FIXME("(%p)->(%p)\n", This, p);
1866 return node_get_nodeName(&This->node, p);
1869 static HRESULT WINAPI unknode_get_text(
1870 IXMLDOMNode *iface,
1871 BSTR* p)
1873 unknode *This = unknode_from_IXMLDOMNode( iface );
1874 return node_get_text(&This->node, p);
1877 static HRESULT WINAPI unknode_put_text(
1878 IXMLDOMNode *iface,
1879 BSTR p)
1881 unknode *This = unknode_from_IXMLDOMNode( iface );
1882 return node_put_text(&This->node, p);
1885 static HRESULT WINAPI unknode_get_specified(
1886 IXMLDOMNode *iface,
1887 VARIANT_BOOL* isSpecified)
1889 unknode *This = unknode_from_IXMLDOMNode( iface );
1890 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
1891 *isSpecified = VARIANT_TRUE;
1892 return S_OK;
1895 static HRESULT WINAPI unknode_get_definition(
1896 IXMLDOMNode *iface,
1897 IXMLDOMNode** definitionNode)
1899 unknode *This = unknode_from_IXMLDOMNode( iface );
1900 FIXME("(%p)->(%p)\n", This, definitionNode);
1901 return E_NOTIMPL;
1904 static HRESULT WINAPI unknode_get_nodeTypedValue(
1905 IXMLDOMNode *iface,
1906 VARIANT* var1)
1908 unknode *This = unknode_from_IXMLDOMNode( iface );
1909 FIXME("(%p)->(%p)\n", This, var1);
1910 return return_null_var(var1);
1913 static HRESULT WINAPI unknode_put_nodeTypedValue(
1914 IXMLDOMNode *iface,
1915 VARIANT typedValue)
1917 unknode *This = unknode_from_IXMLDOMNode( iface );
1918 FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
1919 return E_NOTIMPL;
1922 static HRESULT WINAPI unknode_get_dataType(
1923 IXMLDOMNode *iface,
1924 VARIANT* var1)
1926 unknode *This = unknode_from_IXMLDOMNode( iface );
1927 TRACE("(%p)->(%p)\n", This, var1);
1928 return return_null_var(var1);
1931 static HRESULT WINAPI unknode_put_dataType(
1932 IXMLDOMNode *iface,
1933 BSTR p)
1935 unknode *This = unknode_from_IXMLDOMNode( iface );
1937 FIXME("(%p)->(%s)\n", This, debugstr_w(p));
1939 if(!p)
1940 return E_INVALIDARG;
1942 return E_FAIL;
1945 static HRESULT WINAPI unknode_get_xml(
1946 IXMLDOMNode *iface,
1947 BSTR* p)
1949 unknode *This = unknode_from_IXMLDOMNode( iface );
1951 FIXME("(%p)->(%p)\n", This, p);
1953 return node_get_xml(&This->node, FALSE, p);
1956 static HRESULT WINAPI unknode_transformNode(
1957 IXMLDOMNode *iface,
1958 IXMLDOMNode* domNode, BSTR* p)
1960 unknode *This = unknode_from_IXMLDOMNode( iface );
1961 return node_transform_node(&This->node, domNode, p);
1964 static HRESULT WINAPI unknode_selectNodes(
1965 IXMLDOMNode *iface,
1966 BSTR p, IXMLDOMNodeList** outList)
1968 unknode *This = unknode_from_IXMLDOMNode( iface );
1969 return node_select_nodes(&This->node, p, outList);
1972 static HRESULT WINAPI unknode_selectSingleNode(
1973 IXMLDOMNode *iface,
1974 BSTR p, IXMLDOMNode** outNode)
1976 unknode *This = unknode_from_IXMLDOMNode( iface );
1977 return node_select_singlenode(&This->node, p, outNode);
1980 static HRESULT WINAPI unknode_get_parsed(
1981 IXMLDOMNode *iface,
1982 VARIANT_BOOL* isParsed)
1984 unknode *This = unknode_from_IXMLDOMNode( iface );
1985 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1986 *isParsed = VARIANT_TRUE;
1987 return S_OK;
1990 static HRESULT WINAPI unknode_get_namespaceURI(
1991 IXMLDOMNode *iface,
1992 BSTR* p)
1994 unknode *This = unknode_from_IXMLDOMNode( iface );
1995 TRACE("(%p)->(%p)\n", This, p);
1996 return node_get_namespaceURI(&This->node, p);
1999 static HRESULT WINAPI unknode_get_prefix(
2000 IXMLDOMNode *iface,
2001 BSTR* p)
2003 unknode *This = unknode_from_IXMLDOMNode( iface );
2004 return node_get_prefix(&This->node, p);
2007 static HRESULT WINAPI unknode_get_baseName(
2008 IXMLDOMNode *iface,
2009 BSTR* p)
2011 unknode *This = unknode_from_IXMLDOMNode( iface );
2012 return node_get_base_name(&This->node, p);
2015 static HRESULT WINAPI unknode_transformNodeToObject(
2016 IXMLDOMNode *iface,
2017 IXMLDOMNode* domNode, VARIANT var1)
2019 unknode *This = unknode_from_IXMLDOMNode( iface );
2020 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
2021 return E_NOTIMPL;
2024 static const struct IXMLDOMNodeVtbl unknode_vtbl =
2026 unknode_QueryInterface,
2027 unknode_AddRef,
2028 unknode_Release,
2029 unknode_GetTypeInfoCount,
2030 unknode_GetTypeInfo,
2031 unknode_GetIDsOfNames,
2032 unknode_Invoke,
2033 unknode_get_nodeName,
2034 unknode_get_nodeValue,
2035 unknode_put_nodeValue,
2036 unknode_get_nodeType,
2037 unknode_get_parentNode,
2038 unknode_get_childNodes,
2039 unknode_get_firstChild,
2040 unknode_get_lastChild,
2041 unknode_get_previousSibling,
2042 unknode_get_nextSibling,
2043 unknode_get_attributes,
2044 unknode_insertBefore,
2045 unknode_replaceChild,
2046 unknode_removeChild,
2047 unknode_appendChild,
2048 unknode_hasChildNodes,
2049 unknode_get_ownerDocument,
2050 unknode_cloneNode,
2051 unknode_get_nodeTypeString,
2052 unknode_get_text,
2053 unknode_put_text,
2054 unknode_get_specified,
2055 unknode_get_definition,
2056 unknode_get_nodeTypedValue,
2057 unknode_put_nodeTypedValue,
2058 unknode_get_dataType,
2059 unknode_put_dataType,
2060 unknode_get_xml,
2061 unknode_transformNode,
2062 unknode_selectNodes,
2063 unknode_selectSingleNode,
2064 unknode_get_parsed,
2065 unknode_get_namespaceURI,
2066 unknode_get_prefix,
2067 unknode_get_baseName,
2068 unknode_transformNodeToObject
2071 IXMLDOMNode *create_node( xmlNodePtr node )
2073 IUnknown *pUnk;
2074 IXMLDOMNode *ret;
2075 HRESULT hr;
2077 if ( !node )
2078 return NULL;
2080 TRACE("type %d\n", node->type);
2081 switch(node->type)
2083 case XML_ELEMENT_NODE:
2084 pUnk = create_element( node );
2085 break;
2086 case XML_ATTRIBUTE_NODE:
2087 pUnk = create_attribute( node );
2088 break;
2089 case XML_TEXT_NODE:
2090 pUnk = create_text( node );
2091 break;
2092 case XML_CDATA_SECTION_NODE:
2093 pUnk = create_cdata( node );
2094 break;
2095 case XML_ENTITY_REF_NODE:
2096 pUnk = create_doc_entity_ref( node );
2097 break;
2098 case XML_PI_NODE:
2099 pUnk = create_pi( node );
2100 break;
2101 case XML_COMMENT_NODE:
2102 pUnk = create_comment( node );
2103 break;
2104 case XML_DOCUMENT_NODE:
2105 pUnk = create_domdoc( node );
2106 break;
2107 case XML_DOCUMENT_FRAG_NODE:
2108 pUnk = create_doc_fragment( node );
2109 break;
2110 case XML_DTD_NODE:
2111 case XML_DOCUMENT_TYPE_NODE:
2112 pUnk = create_doc_type( node );
2113 break;
2114 case XML_ENTITY_NODE:
2115 case XML_NOTATION_NODE: {
2116 unknode *new_node;
2118 FIXME("only creating basic node for type %d\n", node->type);
2120 new_node = heap_alloc(sizeof(unknode));
2121 if(!new_node)
2122 return NULL;
2124 new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2125 new_node->ref = 1;
2126 init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
2127 pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2128 break;
2130 default:
2131 ERR("Called for unsupported node type %d\n", node->type);
2132 return NULL;
2135 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
2136 IUnknown_Release(pUnk);
2137 if(FAILED(hr)) return NULL;
2138 return ret;
2140 #endif