Sync to trunk r65566.
[reactos.git] / dll / win32 / msxml3 / node.c
1 /*
2 * Node implementation
3 *
4 * Copyright 2005 Mike McCormack
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "precomp.h"
22
23 #ifdef HAVE_LIBXML2
24 # include <libxml/HTMLtree.h>
25 # ifdef SONAME_LIBXSLT
26 # ifdef HAVE_LIBXSLT_PATTERN_H
27 # include <libxslt/pattern.h>
28 # endif
29 # ifdef HAVE_LIBXSLT_TRANSFORM_H
30 # include <libxslt/transform.h>
31 # endif
32 # include <libxslt/imports.h>
33 # include <libxslt/variables.h>
34 # include <libxslt/xsltutils.h>
35 # include <libxslt/xsltInternals.h>
36 # endif
37 #endif
38
39 #ifdef HAVE_LIBXML2
40
41 #ifdef SONAME_LIBXSLT
42 extern void* libxslt_handle;
43 # define MAKE_FUNCPTR(f) extern typeof(f) * p##f
44 MAKE_FUNCPTR(xsltApplyStylesheet);
45 MAKE_FUNCPTR(xsltApplyStylesheetUser);
46 MAKE_FUNCPTR(xsltCleanupGlobals);
47 MAKE_FUNCPTR(xsltFreeStylesheet);
48 MAKE_FUNCPTR(xsltFreeTransformContext);
49 MAKE_FUNCPTR(xsltNewTransformContext);
50 MAKE_FUNCPTR(xsltNextImport);
51 MAKE_FUNCPTR(xsltParseStylesheetDoc);
52 MAKE_FUNCPTR(xsltQuoteUserParams);
53 MAKE_FUNCPTR(xsltSaveResultTo);
54 # undef MAKE_FUNCPTR
55 #else
56 WINE_DECLARE_DEBUG_CHANNEL(winediag);
57 #endif
58
59 static const IID IID_xmlnode = {0x4f2f4ba2,0xb822,0x11df,{0x8b,0x8a,0x68,0x50,0xdf,0xd7,0x20,0x85}};
60
61 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
62 {
63 xmlnode *This;
64
65 if ( !iface )
66 return NULL;
67 This = get_node_obj( iface );
68 if ( !This || !This->node )
69 return NULL;
70 if ( type && This->node->type != type )
71 return NULL;
72 return This->node;
73 }
74
75 BOOL node_query_interface(xmlnode *This, REFIID riid, void **ppv)
76 {
77 if(IsEqualGUID(&IID_xmlnode, riid)) {
78 TRACE("(%p)->(IID_xmlnode %p)\n", This, ppv);
79 *ppv = This;
80 return TRUE;
81 }
82
83 return dispex_query_interface(&This->dispex, riid, ppv);
84 }
85
86 /* common ISupportErrorInfo implementation */
87 typedef struct {
88 ISupportErrorInfo ISupportErrorInfo_iface;
89 LONG ref;
90
91 const tid_t* iids;
92 } SupportErrorInfo;
93
94 static inline SupportErrorInfo *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
95 {
96 return CONTAINING_RECORD(iface, SupportErrorInfo, ISupportErrorInfo_iface);
97 }
98
99 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **obj)
100 {
101 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
102 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
103
104 *obj = NULL;
105
106 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISupportErrorInfo)) {
107 *obj = iface;
108 ISupportErrorInfo_AddRef(iface);
109 return S_OK;
110 }
111
112 return E_NOINTERFACE;
113 }
114
115 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
116 {
117 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
118 ULONG ref = InterlockedIncrement(&This->ref);
119 TRACE("(%p)->(%d)\n", This, ref );
120 return ref;
121 }
122
123 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
124 {
125 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
126 LONG ref = InterlockedDecrement(&This->ref);
127
128 TRACE("(%p)->(%d)\n", This, ref);
129
130 if (ref == 0)
131 heap_free(This);
132
133 return ref;
134 }
135
136 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
137 {
138 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
139 enum tid_t const *tid;
140
141 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
142
143 tid = This->iids;
144 while (*tid != NULL_tid)
145 {
146 if (IsEqualGUID(riid, get_riid_from_tid(*tid)))
147 return S_OK;
148 tid++;
149 }
150
151 return S_FALSE;
152 }
153
154 static const struct ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
155 SupportErrorInfo_QueryInterface,
156 SupportErrorInfo_AddRef,
157 SupportErrorInfo_Release,
158 SupportErrorInfo_InterfaceSupportsErrorInfo
159 };
160
161 HRESULT node_create_supporterrorinfo(enum tid_t const *iids, void **obj)
162 {
163 SupportErrorInfo *This;
164
165 This = heap_alloc(sizeof(*This));
166 if (!This) return E_OUTOFMEMORY;
167
168 This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
169 This->ref = 1;
170 This->iids = iids;
171
172 *obj = &This->ISupportErrorInfo_iface;
173
174 return S_OK;
175 }
176
177 xmlnode *get_node_obj(IXMLDOMNode *node)
178 {
179 xmlnode *obj = NULL;
180 HRESULT hres;
181
182 hres = IXMLDOMNode_QueryInterface(node, &IID_xmlnode, (void**)&obj);
183 if (!obj) WARN("node is not our IXMLDOMNode implementation\n");
184 return SUCCEEDED(hres) ? obj : NULL;
185 }
186
187 HRESULT node_get_nodeName(xmlnode *This, BSTR *name)
188 {
189 BSTR prefix, base;
190 HRESULT hr;
191
192 if (!name)
193 return E_INVALIDARG;
194
195 hr = node_get_base_name(This, &base);
196 if (hr != S_OK) return hr;
197
198 hr = node_get_prefix(This, &prefix);
199 if (hr == S_OK)
200 {
201 static const WCHAR colW = ':';
202 WCHAR *ptr;
203
204 /* +1 for ':' */
205 ptr = *name = SysAllocStringLen(NULL, SysStringLen(base) + SysStringLen(prefix) + 1);
206 memcpy(ptr, prefix, SysStringByteLen(prefix));
207 ptr += SysStringLen(prefix);
208 memcpy(ptr++, &colW, sizeof(WCHAR));
209 memcpy(ptr, base, SysStringByteLen(base));
210
211 SysFreeString(base);
212 SysFreeString(prefix);
213 }
214 else
215 *name = base;
216
217 return S_OK;
218 }
219
220 HRESULT node_get_content(xmlnode *This, VARIANT *value)
221 {
222 xmlChar *content;
223
224 if(!value)
225 return E_INVALIDARG;
226
227 content = xmlNodeGetContent(This->node);
228 V_VT(value) = VT_BSTR;
229 V_BSTR(value) = bstr_from_xmlChar( content );
230 xmlFree(content);
231
232 TRACE("%p returned %s\n", This, debugstr_w(V_BSTR(value)));
233 return S_OK;
234 }
235
236 HRESULT node_set_content(xmlnode *This, LPCWSTR value)
237 {
238 xmlChar *str;
239
240 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
241 str = xmlchar_from_wchar(value);
242 if(!str)
243 return E_OUTOFMEMORY;
244
245 xmlNodeSetContent(This->node, str);
246 heap_free(str);
247 return S_OK;
248 }
249
250 static HRESULT node_set_content_escaped(xmlnode *This, LPCWSTR value)
251 {
252 xmlChar *str, *escaped;
253
254 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
255 str = xmlchar_from_wchar(value);
256 if(!str)
257 return E_OUTOFMEMORY;
258
259 escaped = xmlEncodeSpecialChars(NULL, str);
260 if(!escaped)
261 {
262 heap_free(str);
263 return E_OUTOFMEMORY;
264 }
265
266 xmlNodeSetContent(This->node, escaped);
267
268 heap_free(str);
269 xmlFree(escaped);
270
271 return S_OK;
272 }
273
274 HRESULT node_put_value(xmlnode *This, VARIANT *value)
275 {
276 HRESULT hr;
277
278 if (V_VT(value) != VT_BSTR)
279 {
280 VARIANT string_value;
281
282 VariantInit(&string_value);
283 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
284 if(FAILED(hr)) {
285 WARN("Couldn't convert to VT_BSTR\n");
286 return hr;
287 }
288
289 hr = node_set_content(This, V_BSTR(&string_value));
290 VariantClear(&string_value);
291 }
292 else
293 hr = node_set_content(This, V_BSTR(value));
294
295 return hr;
296 }
297
298 HRESULT node_put_value_escaped(xmlnode *This, VARIANT *value)
299 {
300 HRESULT hr;
301
302 if (V_VT(value) != VT_BSTR)
303 {
304 VARIANT string_value;
305
306 VariantInit(&string_value);
307 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
308 if(FAILED(hr)) {
309 WARN("Couldn't convert to VT_BSTR\n");
310 return hr;
311 }
312
313 hr = node_set_content_escaped(This, V_BSTR(&string_value));
314 VariantClear(&string_value);
315 }
316 else
317 hr = node_set_content_escaped(This, V_BSTR(value));
318
319 return hr;
320 }
321
322 static HRESULT get_node(
323 xmlnode *This,
324 const char *name,
325 xmlNodePtr node,
326 IXMLDOMNode **out )
327 {
328 TRACE("(%p)->(%s %p %p)\n", This, name, node, out );
329
330 if ( !out )
331 return E_INVALIDARG;
332
333 /* if we don't have a doc, use our parent. */
334 if(node && !node->doc && node->parent)
335 node->doc = node->parent->doc;
336
337 *out = create_node( node );
338 if (!*out)
339 return S_FALSE;
340 return S_OK;
341 }
342
343 HRESULT node_get_parent(xmlnode *This, IXMLDOMNode **parent)
344 {
345 return get_node( This, "parent", This->node->parent, parent );
346 }
347
348 HRESULT node_get_child_nodes(xmlnode *This, IXMLDOMNodeList **ret)
349 {
350 if(!ret)
351 return E_INVALIDARG;
352
353 *ret = create_children_nodelist(This->node);
354 if(!*ret)
355 return E_OUTOFMEMORY;
356
357 return S_OK;
358 }
359
360 HRESULT node_get_first_child(xmlnode *This, IXMLDOMNode **ret)
361 {
362 return get_node(This, "firstChild", This->node->children, ret);
363 }
364
365 HRESULT node_get_last_child(xmlnode *This, IXMLDOMNode **ret)
366 {
367 return get_node(This, "lastChild", This->node->last, ret);
368 }
369
370 HRESULT node_get_previous_sibling(xmlnode *This, IXMLDOMNode **ret)
371 {
372 return get_node(This, "previous", This->node->prev, ret);
373 }
374
375 HRESULT node_get_next_sibling(xmlnode *This, IXMLDOMNode **ret)
376 {
377 return get_node(This, "next", This->node->next, ret);
378 }
379
380 static int node_get_inst_cnt(xmlNodePtr node)
381 {
382 int ret = *(LONG *)&node->_private;
383 xmlNodePtr child;
384
385 /* add attribute counts */
386 if (node->type == XML_ELEMENT_NODE)
387 {
388 xmlAttrPtr prop = node->properties;
389
390 while (prop)
391 {
392 ret += node_get_inst_cnt((xmlNodePtr)prop);
393 prop = prop->next;
394 }
395 }
396
397 /* add children counts */
398 child = node->children;
399 while (child)
400 {
401 ret += node_get_inst_cnt(child);
402 child = child->next;
403 }
404
405 return ret;
406 }
407
408 int xmlnode_get_inst_cnt(xmlnode *node)
409 {
410 return node_get_inst_cnt(node->node);
411 }
412
413 HRESULT node_insert_before(xmlnode *This, IXMLDOMNode *new_child, const VARIANT *ref_child,
414 IXMLDOMNode **ret)
415 {
416 IXMLDOMNode *before = NULL;
417 xmlnode *node_obj;
418 int refcount = 0;
419 xmlDocPtr doc;
420 HRESULT hr;
421
422 if(!new_child)
423 return E_INVALIDARG;
424
425 node_obj = get_node_obj(new_child);
426 if(!node_obj) return E_FAIL;
427
428 switch(V_VT(ref_child))
429 {
430 case VT_EMPTY:
431 case VT_NULL:
432 break;
433
434 case VT_UNKNOWN:
435 case VT_DISPATCH:
436 if (V_UNKNOWN(ref_child))
437 {
438 hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
439 if(FAILED(hr)) return hr;
440 }
441 break;
442
443 default:
444 FIXME("refChild var type %x\n", V_VT(ref_child));
445 return E_FAIL;
446 }
447
448 TRACE("new child %p, This->node %p\n", node_obj->node, This->node);
449
450 if(!node_obj->node->parent)
451 if(xmldoc_remove_orphan(node_obj->node->doc, node_obj->node) != S_OK)
452 WARN("%p is not an orphan of %p\n", node_obj->node, node_obj->node->doc);
453
454 refcount = xmlnode_get_inst_cnt(node_obj);
455
456 if(before)
457 {
458 xmlnode *before_node_obj = get_node_obj(before);
459 IXMLDOMNode_Release(before);
460 if(!before_node_obj) return E_FAIL;
461
462 /* unlink from current parent first */
463 if(node_obj->parent)
464 {
465 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
466 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
467 }
468
469 doc = node_obj->node->doc;
470
471 /* refs count including subtree */
472 if (doc != before_node_obj->node->doc)
473 refcount = xmlnode_get_inst_cnt(node_obj);
474
475 if (refcount) xmldoc_add_refs(before_node_obj->node->doc, refcount);
476 xmlAddPrevSibling(before_node_obj->node, node_obj->node);
477 if (refcount) xmldoc_release_refs(doc, refcount);
478 node_obj->parent = This->parent;
479 }
480 else
481 {
482 /* unlink from current parent first */
483 if(node_obj->parent)
484 {
485 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
486 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
487 }
488 doc = node_obj->node->doc;
489
490 if (doc != This->node->doc)
491 refcount = xmlnode_get_inst_cnt(node_obj);
492
493 if (refcount) xmldoc_add_refs(This->node->doc, refcount);
494 /* xmlAddChild doesn't unlink node from previous parent */
495 xmlUnlinkNode(node_obj->node);
496 xmlAddChild(This->node, node_obj->node);
497 if (refcount) xmldoc_release_refs(doc, refcount);
498 node_obj->parent = This->iface;
499 }
500
501 if(ret)
502 {
503 IXMLDOMNode_AddRef(new_child);
504 *ret = new_child;
505 }
506
507 TRACE("ret S_OK\n");
508 return S_OK;
509 }
510
511 HRESULT node_replace_child(xmlnode *This, IXMLDOMNode *newChild, IXMLDOMNode *oldChild,
512 IXMLDOMNode **ret)
513 {
514 xmlnode *old_child, *new_child;
515 xmlDocPtr leaving_doc;
516 xmlNode *my_ancestor;
517 int refcount = 0;
518
519 /* Do not believe any documentation telling that newChild == NULL
520 means removal. It does certainly *not* apply to msxml3! */
521 if(!newChild || !oldChild)
522 return E_INVALIDARG;
523
524 if(ret)
525 *ret = NULL;
526
527 old_child = get_node_obj(oldChild);
528 if(!old_child) return E_FAIL;
529
530 if(old_child->node->parent != This->node)
531 {
532 WARN("childNode %p is not a child of %p\n", oldChild, This);
533 return E_INVALIDARG;
534 }
535
536 new_child = get_node_obj(newChild);
537 if(!new_child) return E_FAIL;
538
539 my_ancestor = This->node;
540 while(my_ancestor)
541 {
542 if(my_ancestor == new_child->node)
543 {
544 WARN("tried to create loop\n");
545 return E_FAIL;
546 }
547 my_ancestor = my_ancestor->parent;
548 }
549
550 if(!new_child->node->parent)
551 if(xmldoc_remove_orphan(new_child->node->doc, new_child->node) != S_OK)
552 WARN("%p is not an orphan of %p\n", new_child->node, new_child->node->doc);
553
554 leaving_doc = new_child->node->doc;
555
556 if (leaving_doc != old_child->node->doc)
557 refcount = xmlnode_get_inst_cnt(new_child);
558
559 if (refcount) xmldoc_add_refs(old_child->node->doc, refcount);
560 xmlReplaceNode(old_child->node, new_child->node);
561 if (refcount) xmldoc_release_refs(leaving_doc, refcount);
562 new_child->parent = old_child->parent;
563 old_child->parent = NULL;
564
565 xmldoc_add_orphan(old_child->node->doc, old_child->node);
566
567 if(ret)
568 {
569 IXMLDOMNode_AddRef(oldChild);
570 *ret = oldChild;
571 }
572
573 return S_OK;
574 }
575
576 HRESULT node_remove_child(xmlnode *This, IXMLDOMNode* child, IXMLDOMNode** oldChild)
577 {
578 xmlnode *child_node;
579
580 if(!child) return E_INVALIDARG;
581
582 if(oldChild)
583 *oldChild = NULL;
584
585 child_node = get_node_obj(child);
586 if(!child_node) return E_FAIL;
587
588 if(child_node->node->parent != This->node)
589 {
590 WARN("childNode %p is not a child of %p\n", child, This);
591 return E_INVALIDARG;
592 }
593
594 xmlUnlinkNode(child_node->node);
595 child_node->parent = NULL;
596 xmldoc_add_orphan(child_node->node->doc, child_node->node);
597
598 if(oldChild)
599 {
600 IXMLDOMNode_AddRef(child);
601 *oldChild = child;
602 }
603
604 return S_OK;
605 }
606
607 HRESULT node_append_child(xmlnode *This, IXMLDOMNode *child, IXMLDOMNode **outChild)
608 {
609 DOMNodeType type;
610 VARIANT var;
611 HRESULT hr;
612
613 if (!child)
614 return E_INVALIDARG;
615
616 hr = IXMLDOMNode_get_nodeType(child, &type);
617 if(FAILED(hr) || type == NODE_ATTRIBUTE) {
618 if (outChild) *outChild = NULL;
619 return E_FAIL;
620 }
621
622 VariantInit(&var);
623 return IXMLDOMNode_insertBefore(This->iface, child, var, outChild);
624 }
625
626 HRESULT node_has_childnodes(const xmlnode *This, VARIANT_BOOL *ret)
627 {
628 if (!ret) return E_INVALIDARG;
629
630 if (!This->node->children)
631 {
632 *ret = VARIANT_FALSE;
633 return S_FALSE;
634 }
635
636 *ret = VARIANT_TRUE;
637 return S_OK;
638 }
639
640 HRESULT node_get_owner_doc(const xmlnode *This, IXMLDOMDocument **doc)
641 {
642 return get_domdoc_from_xmldoc(This->node->doc, (IXMLDOMDocument3**)doc);
643 }
644
645 HRESULT node_clone(xmlnode *This, VARIANT_BOOL deep, IXMLDOMNode **cloneNode)
646 {
647 IXMLDOMNode *node;
648 xmlNodePtr clone;
649
650 if(!cloneNode) return E_INVALIDARG;
651
652 clone = xmlCopyNode(This->node, deep ? 1 : 2);
653 if (clone)
654 {
655 xmlSetTreeDoc(clone, This->node->doc);
656 xmldoc_add_orphan(clone->doc, clone);
657
658 node = create_node(clone);
659 if (!node)
660 {
661 ERR("Copy failed\n");
662 xmldoc_remove_orphan(clone->doc, clone);
663 xmlFreeNode(clone);
664 return E_FAIL;
665 }
666
667 *cloneNode = node;
668 }
669 else
670 {
671 ERR("Copy failed\n");
672 return E_FAIL;
673 }
674
675 return S_OK;
676 }
677
678 static inline xmlChar* trim_whitespace(xmlChar* str)
679 {
680 xmlChar* ret = str;
681 int len;
682
683 if (!str)
684 return NULL;
685
686 while (*ret && isspace(*ret))
687 ++ret;
688 len = xmlStrlen(ret);
689 if (len)
690 while (isspace(ret[len-1])) --len;
691
692 ret = xmlStrndup(ret, len);
693 xmlFree(str);
694 return ret;
695 }
696
697 static xmlChar* do_get_text(xmlNodePtr node)
698 {
699 xmlNodePtr child;
700 xmlChar* str;
701 BOOL preserving = is_preserving_whitespace(node);
702
703 if (!node->children)
704 {
705 str = xmlNodeGetContent(node);
706 }
707 else
708 {
709 xmlElementType prev_type = XML_TEXT_NODE;
710 xmlChar* tmp;
711 str = xmlStrdup(BAD_CAST "");
712 for (child = node->children; child != NULL; child = child->next)
713 {
714 switch (child->type)
715 {
716 case XML_ELEMENT_NODE:
717 tmp = do_get_text(child);
718 break;
719 case XML_TEXT_NODE:
720 case XML_CDATA_SECTION_NODE:
721 case XML_ENTITY_REF_NODE:
722 case XML_ENTITY_NODE:
723 tmp = xmlNodeGetContent(child);
724 break;
725 default:
726 tmp = NULL;
727 break;
728 }
729
730 if (tmp)
731 {
732 if (*tmp)
733 {
734 if (prev_type == XML_ELEMENT_NODE && child->type == XML_ELEMENT_NODE)
735 str = xmlStrcat(str, BAD_CAST " ");
736 str = xmlStrcat(str, tmp);
737 prev_type = child->type;
738 }
739 xmlFree(tmp);
740 }
741 }
742 }
743
744 switch (node->type)
745 {
746 case XML_ELEMENT_NODE:
747 case XML_TEXT_NODE:
748 case XML_ENTITY_REF_NODE:
749 case XML_ENTITY_NODE:
750 case XML_DOCUMENT_NODE:
751 case XML_DOCUMENT_FRAG_NODE:
752 if (!preserving)
753 str = trim_whitespace(str);
754 break;
755 default:
756 break;
757 }
758
759 return str;
760 }
761
762 HRESULT node_get_text(const xmlnode *This, BSTR *text)
763 {
764 BSTR str = NULL;
765 xmlChar *content;
766
767 if (!text) return E_INVALIDARG;
768
769 content = do_get_text(This->node);
770 if (content)
771 {
772 str = bstr_from_xmlChar(content);
773 xmlFree(content);
774 }
775
776 /* Always return a string. */
777 if (!str) str = SysAllocStringLen( NULL, 0 );
778
779 TRACE("%p %s\n", This, debugstr_w(str) );
780 *text = str;
781
782 return S_OK;
783 }
784
785 HRESULT node_put_text(xmlnode *This, BSTR text)
786 {
787 xmlChar *str, *str2;
788
789 TRACE("(%p)->(%s)\n", This, debugstr_w(text));
790
791 str = xmlchar_from_wchar(text);
792
793 /* Escape the string. */
794 str2 = xmlEncodeEntitiesReentrant(This->node->doc, str);
795 heap_free(str);
796
797 xmlNodeSetContent(This->node, str2);
798 xmlFree(str2);
799
800 return S_OK;
801 }
802
803 BSTR EnsureCorrectEOL(BSTR sInput)
804 {
805 int nNum = 0;
806 BSTR sNew;
807 int nLen;
808 int i;
809
810 nLen = SysStringLen(sInput);
811 /* Count line endings */
812 for(i=0; i < nLen; i++)
813 {
814 if(sInput[i] == '\n')
815 nNum++;
816 }
817
818 TRACE("len=%d, num=%d\n", nLen, nNum);
819
820 /* Add linefeed as needed */
821 if(nNum > 0)
822 {
823 int nPlace = 0;
824 sNew = SysAllocStringLen(NULL, nLen + nNum);
825 for(i=0; i < nLen; i++)
826 {
827 if(sInput[i] == '\n')
828 {
829 sNew[i+nPlace] = '\r';
830 nPlace++;
831 }
832 sNew[i+nPlace] = sInput[i];
833 }
834
835 SysFreeString(sInput);
836 }
837 else
838 {
839 sNew = sInput;
840 }
841
842 TRACE("len %d\n", SysStringLen(sNew));
843
844 return sNew;
845 }
846
847 /*
848 * We are trying to replicate the same behaviour as msxml by converting
849 * line endings to \r\n and using indents as \t. The problem is that msxml
850 * only formats nodes that have a line ending. Using libxml we cannot
851 * reproduce behaviour exactly.
852 *
853 */
854 HRESULT node_get_xml(xmlnode *This, BOOL ensure_eol, BSTR *ret)
855 {
856 xmlBufferPtr xml_buf;
857 xmlNodePtr xmldecl;
858 int size;
859
860 if(!ret)
861 return E_INVALIDARG;
862
863 *ret = NULL;
864
865 xml_buf = xmlBufferCreate();
866 if(!xml_buf)
867 return E_OUTOFMEMORY;
868
869 xmldecl = xmldoc_unlink_xmldecl( This->node->doc );
870
871 size = xmlNodeDump(xml_buf, This->node->doc, This->node, 0, 1);
872 if(size > 0) {
873 const xmlChar *buf_content;
874 BSTR content;
875
876 /* Attribute Nodes return a space in front of their name */
877 buf_content = xmlBufferContent(xml_buf);
878
879 content = bstr_from_xmlChar(buf_content + (buf_content[0] == ' ' ? 1 : 0));
880 if(ensure_eol)
881 content = EnsureCorrectEOL(content);
882
883 *ret = content;
884 }else {
885 *ret = SysAllocStringLen(NULL, 0);
886 }
887
888 xmlBufferFree(xml_buf);
889 xmldoc_link_xmldecl( This->node->doc, xmldecl );
890 return *ret ? S_OK : E_OUTOFMEMORY;
891 }
892
893 #ifdef SONAME_LIBXSLT
894
895 /* duplicates xmlBufferWriteQuotedString() logic */
896 static void xml_write_quotedstring(xmlOutputBufferPtr buf, const xmlChar *string)
897 {
898 const xmlChar *cur, *base;
899
900 if (xmlStrchr(string, '\"'))
901 {
902 if (xmlStrchr(string, '\''))
903 {
904 xmlOutputBufferWrite(buf, 1, "\"");
905 base = cur = string;
906
907 while (*cur)
908 {
909 if (*cur == '"')
910 {
911 if (base != cur)
912 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
913 xmlOutputBufferWrite(buf, 6, "&quot;");
914 cur++;
915 base = cur;
916 }
917 else
918 cur++;
919 }
920 if (base != cur)
921 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
922 xmlOutputBufferWrite(buf, 1, "\"");
923 }
924 else
925 {
926 xmlOutputBufferWrite(buf, 1, "\'");
927 xmlOutputBufferWriteString(buf, (const char*)string);
928 xmlOutputBufferWrite(buf, 1, "\'");
929 }
930 }
931 else
932 {
933 xmlOutputBufferWrite(buf, 1, "\"");
934 xmlOutputBufferWriteString(buf, (const char*)string);
935 xmlOutputBufferWrite(buf, 1, "\"");
936 }
937 }
938
939 static int XMLCALL transform_to_stream_write(void *context, const char *buffer, int len)
940 {
941 DWORD written;
942 HRESULT hr = IStream_Write((IStream*)context, buffer, len, &written);
943 return hr == S_OK ? written : -1;
944 }
945
946 /* Output for method "text" */
947 static void transform_write_text(xmlDocPtr result, xsltStylesheetPtr style, xmlOutputBufferPtr output)
948 {
949 xmlNodePtr cur = result->children;
950 while (cur)
951 {
952 if (cur->type == XML_TEXT_NODE)
953 xmlOutputBufferWriteString(output, (const char*)cur->content);
954
955 /* skip to next node */
956 if (cur->children)
957 {
958 if ((cur->children->type != XML_ENTITY_DECL) &&
959 (cur->children->type != XML_ENTITY_REF_NODE) &&
960 (cur->children->type != XML_ENTITY_NODE))
961 {
962 cur = cur->children;
963 continue;
964 }
965 }
966
967 if (cur->next) {
968 cur = cur->next;
969 continue;
970 }
971
972 do
973 {
974 cur = cur->parent;
975 if (cur == NULL)
976 break;
977 if (cur == (xmlNodePtr) style->doc) {
978 cur = NULL;
979 break;
980 }
981 if (cur->next) {
982 cur = cur->next;
983 break;
984 }
985 } while (cur);
986 }
987 }
988
989 #undef XSLT_GET_IMPORT_PTR
990 #define XSLT_GET_IMPORT_PTR(res, style, name) { \
991 xsltStylesheetPtr st = style; \
992 res = NULL; \
993 while (st != NULL) { \
994 if (st->name != NULL) { res = st->name; break; } \
995 st = pxsltNextImport(st); \
996 }}
997
998 #undef XSLT_GET_IMPORT_INT
999 #define XSLT_GET_IMPORT_INT(res, style, name) { \
1000 xsltStylesheetPtr st = style; \
1001 res = -1; \
1002 while (st != NULL) { \
1003 if (st->name != -1) { res = st->name; break; } \
1004 st = pxsltNextImport(st); \
1005 }}
1006
1007 static void transform_write_xmldecl(xmlDocPtr result, xsltStylesheetPtr style, BOOL omit_encoding, xmlOutputBufferPtr output)
1008 {
1009 int omit_xmldecl, standalone;
1010
1011 XSLT_GET_IMPORT_INT(omit_xmldecl, style, omitXmlDeclaration);
1012 if (omit_xmldecl == 1) return;
1013
1014 XSLT_GET_IMPORT_INT(standalone, style, standalone);
1015
1016 xmlOutputBufferWriteString(output, "<?xml version=");
1017 if (result->version)
1018 {
1019 xmlOutputBufferWriteString(output, "\"");
1020 xmlOutputBufferWriteString(output, (const char *)result->version);
1021 xmlOutputBufferWriteString(output, "\"");
1022 }
1023 else
1024 xmlOutputBufferWriteString(output, "\"1.0\"");
1025
1026 if (!omit_encoding)
1027 {
1028 const xmlChar *encoding;
1029
1030 /* default encoding is UTF-16 */
1031 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1032 xmlOutputBufferWriteString(output, " encoding=");
1033 xmlOutputBufferWriteString(output, "\"");
1034 xmlOutputBufferWriteString(output, encoding ? (const char *)encoding : "UTF-16");
1035 xmlOutputBufferWriteString(output, "\"");
1036 }
1037
1038 /* standalone attribute */
1039 if (standalone != -1)
1040 xmlOutputBufferWriteString(output, standalone == 0 ? " standalone=\"no\"" : " standalone=\"yes\"");
1041
1042 xmlOutputBufferWriteString(output, "?>");
1043 }
1044
1045 static void htmldtd_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc)
1046 {
1047 xmlDtdPtr cur = doc->intSubset;
1048
1049 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
1050 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1051 if (cur->ExternalID)
1052 {
1053 xmlOutputBufferWriteString(buf, " PUBLIC ");
1054 xml_write_quotedstring(buf, cur->ExternalID);
1055 if (cur->SystemID)
1056 {
1057 xmlOutputBufferWriteString(buf, " ");
1058 xml_write_quotedstring(buf, cur->SystemID);
1059 }
1060 }
1061 else if (cur->SystemID)
1062 {
1063 xmlOutputBufferWriteString(buf, " SYSTEM ");
1064 xml_write_quotedstring(buf, cur->SystemID);
1065 }
1066 xmlOutputBufferWriteString(buf, ">\n");
1067 }
1068
1069 /* Duplicates htmlDocContentDumpFormatOutput() the way we need it - doesn't add trailing newline. */
1070 static void htmldoc_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding, int format)
1071 {
1072 xmlElementType type;
1073
1074 /* force HTML output */
1075 type = doc->type;
1076 doc->type = XML_HTML_DOCUMENT_NODE;
1077 if (doc->intSubset)
1078 htmldtd_dumpcontent(buf, doc);
1079 if (doc->children) {
1080 xmlNodePtr cur = doc->children;
1081 while (cur) {
1082 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
1083 cur = cur->next;
1084 }
1085 }
1086 doc->type = type;
1087 }
1088
1089 static inline BOOL transform_is_empty_resultdoc(xmlDocPtr result)
1090 {
1091 return !result->children || ((result->children->type == XML_DTD_NODE) && !result->children->next);
1092 }
1093
1094 static inline BOOL transform_is_valid_method(xsltStylesheetPtr style)
1095 {
1096 return !style->methodURI || !(style->method && xmlStrEqual(style->method, (const xmlChar *)"xhtml"));
1097 }
1098
1099 /* Helper to write transformation result to specified output buffer. */
1100 static HRESULT node_transform_write(xsltStylesheetPtr style, xmlDocPtr result, BOOL omit_encoding, const char *encoding, xmlOutputBufferPtr output)
1101 {
1102 const xmlChar *method;
1103 int indent;
1104
1105 if (!transform_is_valid_method(style))
1106 {
1107 ERR("unknown output method\n");
1108 return E_FAIL;
1109 }
1110
1111 XSLT_GET_IMPORT_PTR(method, style, method)
1112 XSLT_GET_IMPORT_INT(indent, style, indent);
1113
1114 if (!method && (result->type == XML_HTML_DOCUMENT_NODE))
1115 method = (const xmlChar *) "html";
1116
1117 if (method && xmlStrEqual(method, (const xmlChar *)"html"))
1118 {
1119 htmlSetMetaEncoding(result, (const xmlChar *)encoding);
1120 if (indent == -1)
1121 indent = 1;
1122 htmldoc_dumpcontent(output, result, (const char*)encoding, indent);
1123 }
1124 else if (method && xmlStrEqual(method, (const xmlChar *)"xhtml"))
1125 {
1126 htmlSetMetaEncoding(result, (const xmlChar *) encoding);
1127 htmlDocContentDumpOutput(output, result, encoding);
1128 }
1129 else if (method && xmlStrEqual(method, (const xmlChar *)"text"))
1130 transform_write_text(result, style, output);
1131 else
1132 {
1133 transform_write_xmldecl(result, style, omit_encoding, output);
1134
1135 if (result->children)
1136 {
1137 xmlNodePtr child = result->children;
1138
1139 while (child)
1140 {
1141 xmlNodeDumpOutput(output, result, child, 0, indent == 1, encoding);
1142 if (indent && ((child->type == XML_DTD_NODE) || ((child->type == XML_COMMENT_NODE) && child->next)))
1143 xmlOutputBufferWriteString(output, "\r\n");
1144 child = child->next;
1145 }
1146 }
1147 }
1148
1149 xmlOutputBufferFlush(output);
1150 return S_OK;
1151 }
1152
1153 /* For BSTR output is always UTF-16, without 'encoding' attribute */
1154 static HRESULT node_transform_write_to_bstr(xsltStylesheetPtr style, xmlDocPtr result, BSTR *str)
1155 {
1156 HRESULT hr = S_OK;
1157
1158 if (transform_is_empty_resultdoc(result))
1159 *str = SysAllocStringLen(NULL, 0);
1160 else
1161 {
1162 xmlOutputBufferPtr output = xmlAllocOutputBuffer(xmlFindCharEncodingHandler("UTF-16"));
1163 const xmlChar *content;
1164 size_t len;
1165
1166 *str = NULL;
1167 if (!output)
1168 return E_OUTOFMEMORY;
1169
1170 hr = node_transform_write(style, result, TRUE, "UTF-16", output);
1171 #ifdef LIBXML2_NEW_BUFFER
1172 content = xmlBufContent(output->conv);
1173 len = xmlBufUse(output->conv);
1174 #else
1175 content = xmlBufferContent(output->conv);
1176 len = xmlBufferLength(output->conv);
1177 #endif
1178 /* UTF-16 encoder places UTF-16 bom, we don't need it for BSTR */
1179 content += sizeof(WCHAR);
1180 *str = SysAllocStringLen((WCHAR*)content, len/sizeof(WCHAR) - 1);
1181 xmlOutputBufferClose(output);
1182 }
1183
1184 return *str ? hr : E_OUTOFMEMORY;
1185 }
1186
1187 static HRESULT node_transform_write_to_stream(xsltStylesheetPtr style, xmlDocPtr result, IStream *stream)
1188 {
1189 static const xmlChar *utf16 = (const xmlChar*)"UTF-16";
1190 xmlOutputBufferPtr output;
1191 const xmlChar *encoding;
1192 HRESULT hr;
1193
1194 if (transform_is_empty_resultdoc(result))
1195 {
1196 WARN("empty result document\n");
1197 return S_OK;
1198 }
1199
1200 if (style->methodURI && (!style->method || !xmlStrEqual(style->method, (const xmlChar *) "xhtml")))
1201 {
1202 ERR("unknown output method\n");
1203 return E_FAIL;
1204 }
1205
1206 /* default encoding is UTF-16 */
1207 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1208 if (!encoding)
1209 encoding = utf16;
1210
1211 output = xmlOutputBufferCreateIO(transform_to_stream_write, NULL, stream, xmlFindCharEncodingHandler((const char*)encoding));
1212 if (!output)
1213 return E_OUTOFMEMORY;
1214
1215 hr = node_transform_write(style, result, FALSE, (const char*)encoding, output);
1216 xmlOutputBufferClose(output);
1217 return hr;
1218 }
1219
1220 #endif
1221
1222 HRESULT node_transform_node_params(const xmlnode *This, IXMLDOMNode *stylesheet, BSTR *p,
1223 IStream *stream, const struct xslprocessor_params *params)
1224 {
1225 #ifdef SONAME_LIBXSLT
1226 xsltStylesheetPtr xsltSS;
1227 HRESULT hr = S_OK;
1228 xmlnode *sheet;
1229
1230 if (!libxslt_handle) return E_NOTIMPL;
1231 if (!stylesheet || !p) return E_INVALIDARG;
1232
1233 *p = NULL;
1234
1235 sheet = get_node_obj(stylesheet);
1236 if(!sheet) return E_FAIL;
1237
1238 xsltSS = pxsltParseStylesheetDoc(sheet->node->doc);
1239 if(xsltSS)
1240 {
1241 const char **xslparams = NULL;
1242 xmlDocPtr result;
1243 unsigned int i;
1244
1245 /* convert our parameter list to libxml2 format */
1246 if (params && params->count)
1247 {
1248 struct xslprocessor_par *par;
1249
1250 i = 0;
1251 xslparams = heap_alloc((params->count*2 + 1)*sizeof(char*));
1252 LIST_FOR_EACH_ENTRY(par, &params->list, struct xslprocessor_par, entry)
1253 {
1254 xslparams[i++] = (char*)xmlchar_from_wchar(par->name);
1255 xslparams[i++] = (char*)xmlchar_from_wchar(par->value);
1256 }
1257 xslparams[i] = NULL;
1258 }
1259
1260 if (xslparams)
1261 {
1262 xsltTransformContextPtr ctxt = pxsltNewTransformContext(xsltSS, This->node->doc);
1263
1264 /* push parameters to user context */
1265 pxsltQuoteUserParams(ctxt, xslparams);
1266 result = pxsltApplyStylesheetUser(xsltSS, This->node->doc, NULL, NULL, NULL, ctxt);
1267 pxsltFreeTransformContext(ctxt);
1268
1269 for (i = 0; i < params->count*2; i++)
1270 heap_free((char*)xslparams[i]);
1271 heap_free(xslparams);
1272 }
1273 else
1274 result = pxsltApplyStylesheet(xsltSS, This->node->doc, NULL);
1275
1276 if (result)
1277 {
1278 if (stream)
1279 hr = node_transform_write_to_stream(xsltSS, result, stream);
1280 else
1281 hr = node_transform_write_to_bstr(xsltSS, result, p);
1282 xmlFreeDoc(result);
1283 }
1284 /* libxslt "helpfully" frees the XML document the stylesheet was
1285 generated from, too */
1286 xsltSS->doc = NULL;
1287 pxsltFreeStylesheet(xsltSS);
1288 }
1289
1290 if(!*p) *p = SysAllocStringLen(NULL, 0);
1291
1292 return hr;
1293 #else
1294 ERR_(winediag)("libxslt headers were not found at compile time. Expect problems.\n");
1295
1296 return E_NOTIMPL;
1297 #endif
1298 }
1299
1300 HRESULT node_transform_node(const xmlnode *node, IXMLDOMNode *stylesheet, BSTR *p)
1301 {
1302 return node_transform_node_params(node, stylesheet, p, NULL, NULL);
1303 }
1304
1305 HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
1306 {
1307 xmlChar* str;
1308 HRESULT hr;
1309
1310 if (!query || !nodes) return E_INVALIDARG;
1311
1312 str = xmlchar_from_wchar(query);
1313 hr = create_selection(This->node, str, nodes);
1314 heap_free(str);
1315
1316 return hr;
1317 }
1318
1319 HRESULT node_select_singlenode(const xmlnode *This, BSTR query, IXMLDOMNode **node)
1320 {
1321 IXMLDOMNodeList *list;
1322 HRESULT hr;
1323
1324 hr = node_select_nodes(This, query, &list);
1325 if (hr == S_OK)
1326 {
1327 hr = IXMLDOMNodeList_nextNode(list, node);
1328 IXMLDOMNodeList_Release(list);
1329 }
1330 return hr;
1331 }
1332
1333 HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1334 {
1335 xmlNsPtr ns = This->node->ns;
1336
1337 if(!namespaceURI)
1338 return E_INVALIDARG;
1339
1340 *namespaceURI = NULL;
1341
1342 if (ns && ns->href)
1343 *namespaceURI = bstr_from_xmlChar(ns->href);
1344
1345 TRACE("uri: %s\n", debugstr_w(*namespaceURI));
1346
1347 return *namespaceURI ? S_OK : S_FALSE;
1348 }
1349
1350 HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1351 {
1352 xmlNsPtr ns = This->node->ns;
1353
1354 if (!prefix) return E_INVALIDARG;
1355
1356 *prefix = NULL;
1357
1358 if (ns && ns->prefix)
1359 *prefix = bstr_from_xmlChar(ns->prefix);
1360
1361 TRACE("prefix: %s\n", debugstr_w(*prefix));
1362
1363 return *prefix ? S_OK : S_FALSE;
1364 }
1365
1366 HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1367 {
1368 if (!name) return E_INVALIDARG;
1369
1370 *name = bstr_from_xmlChar(This->node->name);
1371 if (!*name) return E_OUTOFMEMORY;
1372
1373 TRACE("returning %s\n", debugstr_w(*name));
1374
1375 return S_OK;
1376 }
1377
1378 /* _private field holds a number of COM instances spawned from this libxml2 node */
1379 static void xmlnode_add_ref(xmlNodePtr node)
1380 {
1381 if (node->type == XML_DOCUMENT_NODE) return;
1382 InterlockedIncrement((LONG*)&node->_private);
1383 }
1384
1385 static void xmlnode_release(xmlNodePtr node)
1386 {
1387 if (node->type == XML_DOCUMENT_NODE) return;
1388 InterlockedDecrement((LONG*)&node->_private);
1389 }
1390
1391 void destroy_xmlnode(xmlnode *This)
1392 {
1393 if(This->node)
1394 {
1395 xmlnode_release(This->node);
1396 xmldoc_release(This->node->doc);
1397 }
1398 }
1399
1400 void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1401 {
1402 if(node)
1403 {
1404 xmlnode_add_ref(node);
1405 xmldoc_add_ref(node->doc);
1406 }
1407
1408 This->node = node;
1409 This->iface = node_iface;
1410 This->parent = NULL;
1411
1412 init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1413 }
1414
1415 typedef struct {
1416 xmlnode node;
1417 IXMLDOMNode IXMLDOMNode_iface;
1418 LONG ref;
1419 } unknode;
1420
1421 static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1422 {
1423 return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1424 }
1425
1426 static HRESULT WINAPI unknode_QueryInterface(
1427 IXMLDOMNode *iface,
1428 REFIID riid,
1429 void** ppvObject )
1430 {
1431 unknode *This = unknode_from_IXMLDOMNode( iface );
1432
1433 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1434
1435 if (IsEqualGUID(riid, &IID_IUnknown)) {
1436 *ppvObject = iface;
1437 }else if (IsEqualGUID( riid, &IID_IDispatch) ||
1438 IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1439 *ppvObject = &This->IXMLDOMNode_iface;
1440 }else if(node_query_interface(&This->node, riid, ppvObject)) {
1441 return *ppvObject ? S_OK : E_NOINTERFACE;
1442 }else {
1443 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1444 *ppvObject = NULL;
1445 return E_NOINTERFACE;
1446 }
1447
1448 IUnknown_AddRef((IUnknown*)*ppvObject);
1449 return S_OK;
1450 }
1451
1452 static ULONG WINAPI unknode_AddRef(
1453 IXMLDOMNode *iface )
1454 {
1455 unknode *This = unknode_from_IXMLDOMNode( iface );
1456
1457 return InterlockedIncrement(&This->ref);
1458 }
1459
1460 static ULONG WINAPI unknode_Release(
1461 IXMLDOMNode *iface )
1462 {
1463 unknode *This = unknode_from_IXMLDOMNode( iface );
1464 LONG ref;
1465
1466 ref = InterlockedDecrement( &This->ref );
1467 if(!ref) {
1468 destroy_xmlnode(&This->node);
1469 heap_free(This);
1470 }
1471
1472 return ref;
1473 }
1474
1475 static HRESULT WINAPI unknode_GetTypeInfoCount(
1476 IXMLDOMNode *iface,
1477 UINT* pctinfo )
1478 {
1479 unknode *This = unknode_from_IXMLDOMNode( iface );
1480
1481 TRACE("(%p)->(%p)\n", This, pctinfo);
1482
1483 *pctinfo = 1;
1484
1485 return S_OK;
1486 }
1487
1488 static HRESULT WINAPI unknode_GetTypeInfo(
1489 IXMLDOMNode *iface,
1490 UINT iTInfo,
1491 LCID lcid,
1492 ITypeInfo** ppTInfo )
1493 {
1494 unknode *This = unknode_from_IXMLDOMNode( iface );
1495 HRESULT hr;
1496
1497 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1498
1499 hr = get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1500
1501 return hr;
1502 }
1503
1504 static HRESULT WINAPI unknode_GetIDsOfNames(
1505 IXMLDOMNode *iface,
1506 REFIID riid,
1507 LPOLESTR* rgszNames,
1508 UINT cNames,
1509 LCID lcid,
1510 DISPID* rgDispId )
1511 {
1512 unknode *This = unknode_from_IXMLDOMNode( iface );
1513
1514 ITypeInfo *typeinfo;
1515 HRESULT hr;
1516
1517 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1518 lcid, rgDispId);
1519
1520 if(!rgszNames || cNames == 0 || !rgDispId)
1521 return E_INVALIDARG;
1522
1523 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1524 if(SUCCEEDED(hr))
1525 {
1526 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1527 ITypeInfo_Release(typeinfo);
1528 }
1529
1530 return hr;
1531 }
1532
1533 static HRESULT WINAPI unknode_Invoke(
1534 IXMLDOMNode *iface,
1535 DISPID dispIdMember,
1536 REFIID riid,
1537 LCID lcid,
1538 WORD wFlags,
1539 DISPPARAMS* pDispParams,
1540 VARIANT* pVarResult,
1541 EXCEPINFO* pExcepInfo,
1542 UINT* puArgErr )
1543 {
1544 unknode *This = unknode_from_IXMLDOMNode( iface );
1545 ITypeInfo *typeinfo;
1546 HRESULT hr;
1547
1548 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1549 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1550
1551 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1552 if(SUCCEEDED(hr))
1553 {
1554 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDOMNode_iface, dispIdMember, wFlags, pDispParams,
1555 pVarResult, pExcepInfo, puArgErr);
1556 ITypeInfo_Release(typeinfo);
1557 }
1558
1559 return hr;
1560 }
1561
1562 static HRESULT WINAPI unknode_get_nodeName(
1563 IXMLDOMNode *iface,
1564 BSTR* p )
1565 {
1566 unknode *This = unknode_from_IXMLDOMNode( iface );
1567
1568 FIXME("(%p)->(%p)\n", This, p);
1569
1570 return node_get_nodeName(&This->node, p);
1571 }
1572
1573 static HRESULT WINAPI unknode_get_nodeValue(
1574 IXMLDOMNode *iface,
1575 VARIANT* value)
1576 {
1577 unknode *This = unknode_from_IXMLDOMNode( iface );
1578
1579 FIXME("(%p)->(%p)\n", This, value);
1580
1581 if(!value)
1582 return E_INVALIDARG;
1583
1584 V_VT(value) = VT_NULL;
1585 return S_FALSE;
1586 }
1587
1588 static HRESULT WINAPI unknode_put_nodeValue(
1589 IXMLDOMNode *iface,
1590 VARIANT value)
1591 {
1592 unknode *This = unknode_from_IXMLDOMNode( iface );
1593 FIXME("(%p)->(v%d)\n", This, V_VT(&value));
1594 return E_FAIL;
1595 }
1596
1597 static HRESULT WINAPI unknode_get_nodeType(
1598 IXMLDOMNode *iface,
1599 DOMNodeType* domNodeType )
1600 {
1601 unknode *This = unknode_from_IXMLDOMNode( iface );
1602
1603 FIXME("(%p)->(%p)\n", This, domNodeType);
1604
1605 *domNodeType = This->node.node->type;
1606 return S_OK;
1607 }
1608
1609 static HRESULT WINAPI unknode_get_parentNode(
1610 IXMLDOMNode *iface,
1611 IXMLDOMNode** parent )
1612 {
1613 unknode *This = unknode_from_IXMLDOMNode( iface );
1614 FIXME("(%p)->(%p)\n", This, parent);
1615 if (!parent) return E_INVALIDARG;
1616 *parent = NULL;
1617 return S_FALSE;
1618 }
1619
1620 static HRESULT WINAPI unknode_get_childNodes(
1621 IXMLDOMNode *iface,
1622 IXMLDOMNodeList** outList)
1623 {
1624 unknode *This = unknode_from_IXMLDOMNode( iface );
1625
1626 TRACE("(%p)->(%p)\n", This, outList);
1627
1628 return node_get_child_nodes(&This->node, outList);
1629 }
1630
1631 static HRESULT WINAPI unknode_get_firstChild(
1632 IXMLDOMNode *iface,
1633 IXMLDOMNode** domNode)
1634 {
1635 unknode *This = unknode_from_IXMLDOMNode( iface );
1636
1637 TRACE("(%p)->(%p)\n", This, domNode);
1638
1639 return node_get_first_child(&This->node, domNode);
1640 }
1641
1642 static HRESULT WINAPI unknode_get_lastChild(
1643 IXMLDOMNode *iface,
1644 IXMLDOMNode** domNode)
1645 {
1646 unknode *This = unknode_from_IXMLDOMNode( iface );
1647
1648 TRACE("(%p)->(%p)\n", This, domNode);
1649
1650 return node_get_last_child(&This->node, domNode);
1651 }
1652
1653 static HRESULT WINAPI unknode_get_previousSibling(
1654 IXMLDOMNode *iface,
1655 IXMLDOMNode** domNode)
1656 {
1657 unknode *This = unknode_from_IXMLDOMNode( iface );
1658
1659 TRACE("(%p)->(%p)\n", This, domNode);
1660
1661 return node_get_previous_sibling(&This->node, domNode);
1662 }
1663
1664 static HRESULT WINAPI unknode_get_nextSibling(
1665 IXMLDOMNode *iface,
1666 IXMLDOMNode** domNode)
1667 {
1668 unknode *This = unknode_from_IXMLDOMNode( iface );
1669
1670 TRACE("(%p)->(%p)\n", This, domNode);
1671
1672 return node_get_next_sibling(&This->node, domNode);
1673 }
1674
1675 static HRESULT WINAPI unknode_get_attributes(
1676 IXMLDOMNode *iface,
1677 IXMLDOMNamedNodeMap** attributeMap)
1678 {
1679 unknode *This = unknode_from_IXMLDOMNode( iface );
1680
1681 FIXME("(%p)->(%p)\n", This, attributeMap);
1682
1683 return return_null_ptr((void**)attributeMap);
1684 }
1685
1686 static HRESULT WINAPI unknode_insertBefore(
1687 IXMLDOMNode *iface,
1688 IXMLDOMNode* newNode, VARIANT refChild,
1689 IXMLDOMNode** outOldNode)
1690 {
1691 unknode *This = unknode_from_IXMLDOMNode( iface );
1692
1693 FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);
1694
1695 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1696 }
1697
1698 static HRESULT WINAPI unknode_replaceChild(
1699 IXMLDOMNode *iface,
1700 IXMLDOMNode* newNode,
1701 IXMLDOMNode* oldNode,
1702 IXMLDOMNode** outOldNode)
1703 {
1704 unknode *This = unknode_from_IXMLDOMNode( iface );
1705
1706 FIXME("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
1707
1708 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
1709 }
1710
1711 static HRESULT WINAPI unknode_removeChild(
1712 IXMLDOMNode *iface,
1713 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
1714 {
1715 unknode *This = unknode_from_IXMLDOMNode( iface );
1716 return node_remove_child(&This->node, domNode, oldNode);
1717 }
1718
1719 static HRESULT WINAPI unknode_appendChild(
1720 IXMLDOMNode *iface,
1721 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
1722 {
1723 unknode *This = unknode_from_IXMLDOMNode( iface );
1724 return node_append_child(&This->node, newNode, outNewNode);
1725 }
1726
1727 static HRESULT WINAPI unknode_hasChildNodes(
1728 IXMLDOMNode *iface,
1729 VARIANT_BOOL* pbool)
1730 {
1731 unknode *This = unknode_from_IXMLDOMNode( iface );
1732 return node_has_childnodes(&This->node, pbool);
1733 }
1734
1735 static HRESULT WINAPI unknode_get_ownerDocument(
1736 IXMLDOMNode *iface,
1737 IXMLDOMDocument** domDocument)
1738 {
1739 unknode *This = unknode_from_IXMLDOMNode( iface );
1740 return node_get_owner_doc(&This->node, domDocument);
1741 }
1742
1743 static HRESULT WINAPI unknode_cloneNode(
1744 IXMLDOMNode *iface,
1745 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
1746 {
1747 unknode *This = unknode_from_IXMLDOMNode( iface );
1748 return node_clone(&This->node, pbool, outNode );
1749 }
1750
1751 static HRESULT WINAPI unknode_get_nodeTypeString(
1752 IXMLDOMNode *iface,
1753 BSTR* p)
1754 {
1755 unknode *This = unknode_from_IXMLDOMNode( iface );
1756
1757 FIXME("(%p)->(%p)\n", This, p);
1758
1759 return node_get_nodeName(&This->node, p);
1760 }
1761
1762 static HRESULT WINAPI unknode_get_text(
1763 IXMLDOMNode *iface,
1764 BSTR* p)
1765 {
1766 unknode *This = unknode_from_IXMLDOMNode( iface );
1767 return node_get_text(&This->node, p);
1768 }
1769
1770 static HRESULT WINAPI unknode_put_text(
1771 IXMLDOMNode *iface,
1772 BSTR p)
1773 {
1774 unknode *This = unknode_from_IXMLDOMNode( iface );
1775 return node_put_text(&This->node, p);
1776 }
1777
1778 static HRESULT WINAPI unknode_get_specified(
1779 IXMLDOMNode *iface,
1780 VARIANT_BOOL* isSpecified)
1781 {
1782 unknode *This = unknode_from_IXMLDOMNode( iface );
1783 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
1784 *isSpecified = VARIANT_TRUE;
1785 return S_OK;
1786 }
1787
1788 static HRESULT WINAPI unknode_get_definition(
1789 IXMLDOMNode *iface,
1790 IXMLDOMNode** definitionNode)
1791 {
1792 unknode *This = unknode_from_IXMLDOMNode( iface );
1793 FIXME("(%p)->(%p)\n", This, definitionNode);
1794 return E_NOTIMPL;
1795 }
1796
1797 static HRESULT WINAPI unknode_get_nodeTypedValue(
1798 IXMLDOMNode *iface,
1799 VARIANT* var1)
1800 {
1801 unknode *This = unknode_from_IXMLDOMNode( iface );
1802 FIXME("(%p)->(%p)\n", This, var1);
1803 return return_null_var(var1);
1804 }
1805
1806 static HRESULT WINAPI unknode_put_nodeTypedValue(
1807 IXMLDOMNode *iface,
1808 VARIANT typedValue)
1809 {
1810 unknode *This = unknode_from_IXMLDOMNode( iface );
1811 FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
1812 return E_NOTIMPL;
1813 }
1814
1815 static HRESULT WINAPI unknode_get_dataType(
1816 IXMLDOMNode *iface,
1817 VARIANT* var1)
1818 {
1819 unknode *This = unknode_from_IXMLDOMNode( iface );
1820 TRACE("(%p)->(%p)\n", This, var1);
1821 return return_null_var(var1);
1822 }
1823
1824 static HRESULT WINAPI unknode_put_dataType(
1825 IXMLDOMNode *iface,
1826 BSTR p)
1827 {
1828 unknode *This = unknode_from_IXMLDOMNode( iface );
1829
1830 FIXME("(%p)->(%s)\n", This, debugstr_w(p));
1831
1832 if(!p)
1833 return E_INVALIDARG;
1834
1835 return E_FAIL;
1836 }
1837
1838 static HRESULT WINAPI unknode_get_xml(
1839 IXMLDOMNode *iface,
1840 BSTR* p)
1841 {
1842 unknode *This = unknode_from_IXMLDOMNode( iface );
1843
1844 FIXME("(%p)->(%p)\n", This, p);
1845
1846 return node_get_xml(&This->node, FALSE, p);
1847 }
1848
1849 static HRESULT WINAPI unknode_transformNode(
1850 IXMLDOMNode *iface,
1851 IXMLDOMNode* domNode, BSTR* p)
1852 {
1853 unknode *This = unknode_from_IXMLDOMNode( iface );
1854 return node_transform_node(&This->node, domNode, p);
1855 }
1856
1857 static HRESULT WINAPI unknode_selectNodes(
1858 IXMLDOMNode *iface,
1859 BSTR p, IXMLDOMNodeList** outList)
1860 {
1861 unknode *This = unknode_from_IXMLDOMNode( iface );
1862 return node_select_nodes(&This->node, p, outList);
1863 }
1864
1865 static HRESULT WINAPI unknode_selectSingleNode(
1866 IXMLDOMNode *iface,
1867 BSTR p, IXMLDOMNode** outNode)
1868 {
1869 unknode *This = unknode_from_IXMLDOMNode( iface );
1870 return node_select_singlenode(&This->node, p, outNode);
1871 }
1872
1873 static HRESULT WINAPI unknode_get_parsed(
1874 IXMLDOMNode *iface,
1875 VARIANT_BOOL* isParsed)
1876 {
1877 unknode *This = unknode_from_IXMLDOMNode( iface );
1878 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1879 *isParsed = VARIANT_TRUE;
1880 return S_OK;
1881 }
1882
1883 static HRESULT WINAPI unknode_get_namespaceURI(
1884 IXMLDOMNode *iface,
1885 BSTR* p)
1886 {
1887 unknode *This = unknode_from_IXMLDOMNode( iface );
1888 TRACE("(%p)->(%p)\n", This, p);
1889 return node_get_namespaceURI(&This->node, p);
1890 }
1891
1892 static HRESULT WINAPI unknode_get_prefix(
1893 IXMLDOMNode *iface,
1894 BSTR* p)
1895 {
1896 unknode *This = unknode_from_IXMLDOMNode( iface );
1897 return node_get_prefix(&This->node, p);
1898 }
1899
1900 static HRESULT WINAPI unknode_get_baseName(
1901 IXMLDOMNode *iface,
1902 BSTR* p)
1903 {
1904 unknode *This = unknode_from_IXMLDOMNode( iface );
1905 return node_get_base_name(&This->node, p);
1906 }
1907
1908 static HRESULT WINAPI unknode_transformNodeToObject(
1909 IXMLDOMNode *iface,
1910 IXMLDOMNode* domNode, VARIANT var1)
1911 {
1912 unknode *This = unknode_from_IXMLDOMNode( iface );
1913 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
1914 return E_NOTIMPL;
1915 }
1916
1917 static const struct IXMLDOMNodeVtbl unknode_vtbl =
1918 {
1919 unknode_QueryInterface,
1920 unknode_AddRef,
1921 unknode_Release,
1922 unknode_GetTypeInfoCount,
1923 unknode_GetTypeInfo,
1924 unknode_GetIDsOfNames,
1925 unknode_Invoke,
1926 unknode_get_nodeName,
1927 unknode_get_nodeValue,
1928 unknode_put_nodeValue,
1929 unknode_get_nodeType,
1930 unknode_get_parentNode,
1931 unknode_get_childNodes,
1932 unknode_get_firstChild,
1933 unknode_get_lastChild,
1934 unknode_get_previousSibling,
1935 unknode_get_nextSibling,
1936 unknode_get_attributes,
1937 unknode_insertBefore,
1938 unknode_replaceChild,
1939 unknode_removeChild,
1940 unknode_appendChild,
1941 unknode_hasChildNodes,
1942 unknode_get_ownerDocument,
1943 unknode_cloneNode,
1944 unknode_get_nodeTypeString,
1945 unknode_get_text,
1946 unknode_put_text,
1947 unknode_get_specified,
1948 unknode_get_definition,
1949 unknode_get_nodeTypedValue,
1950 unknode_put_nodeTypedValue,
1951 unknode_get_dataType,
1952 unknode_put_dataType,
1953 unknode_get_xml,
1954 unknode_transformNode,
1955 unknode_selectNodes,
1956 unknode_selectSingleNode,
1957 unknode_get_parsed,
1958 unknode_get_namespaceURI,
1959 unknode_get_prefix,
1960 unknode_get_baseName,
1961 unknode_transformNodeToObject
1962 };
1963
1964 IXMLDOMNode *create_node( xmlNodePtr node )
1965 {
1966 IUnknown *pUnk;
1967 IXMLDOMNode *ret;
1968 HRESULT hr;
1969
1970 if ( !node )
1971 return NULL;
1972
1973 TRACE("type %d\n", node->type);
1974 switch(node->type)
1975 {
1976 case XML_ELEMENT_NODE:
1977 pUnk = create_element( node );
1978 break;
1979 case XML_ATTRIBUTE_NODE:
1980 pUnk = create_attribute( node );
1981 break;
1982 case XML_TEXT_NODE:
1983 pUnk = create_text( node );
1984 break;
1985 case XML_CDATA_SECTION_NODE:
1986 pUnk = create_cdata( node );
1987 break;
1988 case XML_ENTITY_REF_NODE:
1989 pUnk = create_doc_entity_ref( node );
1990 break;
1991 case XML_PI_NODE:
1992 pUnk = create_pi( node );
1993 break;
1994 case XML_COMMENT_NODE:
1995 pUnk = create_comment( node );
1996 break;
1997 case XML_DOCUMENT_NODE:
1998 pUnk = create_domdoc( node );
1999 break;
2000 case XML_DOCUMENT_FRAG_NODE:
2001 pUnk = create_doc_fragment( node );
2002 break;
2003 case XML_DTD_NODE:
2004 pUnk = create_doc_type( node );
2005 break;
2006 default: {
2007 unknode *new_node;
2008
2009 FIXME("only creating basic node for type %d\n", node->type);
2010
2011 new_node = heap_alloc(sizeof(unknode));
2012 if(!new_node)
2013 return NULL;
2014
2015 new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2016 new_node->ref = 1;
2017 init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
2018 pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2019 }
2020 }
2021
2022 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
2023 IUnknown_Release(pUnk);
2024 if(FAILED(hr)) return NULL;
2025 return ret;
2026 }
2027 #endif