[MSXML3] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / 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
463 /* unlink from current parent first */
464 if(node_obj->parent)
465 {
466 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
467 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
468 }
469 doc = node_obj->node->doc;
470
471 if(before)
472 {
473 xmlnode *before_node_obj = get_node_obj(before);
474
475 /* refs count including subtree */
476 if (doc != before_node_obj->node->doc)
477 refcount = xmlnode_get_inst_cnt(node_obj);
478
479 if (refcount) xmldoc_add_refs(before_node_obj->node->doc, refcount);
480 node_obj->node = xmlAddPrevSibling(before_node_obj->node, node_obj->node);
481 if (refcount) xmldoc_release_refs(doc, refcount);
482 node_obj->parent = This->parent;
483 }
484 else
485 {
486 if (doc != This->node->doc)
487 refcount = xmlnode_get_inst_cnt(node_obj);
488
489 if (refcount) xmldoc_add_refs(This->node->doc, refcount);
490 /* xmlAddChild doesn't unlink node from previous parent */
491 xmlUnlinkNode(node_obj->node);
492 node_obj->node = xmlAddChild(This->node, node_obj->node);
493 if (refcount) xmldoc_release_refs(doc, refcount);
494 node_obj->parent = This->iface;
495 }
496
497 if(ret)
498 {
499 IXMLDOMNode_AddRef(new_child);
500 *ret = new_child;
501 }
502
503 TRACE("ret S_OK\n");
504 return S_OK;
505 }
506
507 HRESULT node_replace_child(xmlnode *This, IXMLDOMNode *newChild, IXMLDOMNode *oldChild,
508 IXMLDOMNode **ret)
509 {
510 xmlnode *old_child, *new_child;
511 xmlDocPtr leaving_doc;
512 xmlNode *my_ancestor;
513 int refcount = 0;
514
515 /* Do not believe any documentation telling that newChild == NULL
516 means removal. It does certainly *not* apply to msxml3! */
517 if(!newChild || !oldChild)
518 return E_INVALIDARG;
519
520 if(ret)
521 *ret = NULL;
522
523 old_child = get_node_obj(oldChild);
524 if(!old_child) return E_FAIL;
525
526 if(old_child->node->parent != This->node)
527 {
528 WARN("childNode %p is not a child of %p\n", oldChild, This);
529 return E_INVALIDARG;
530 }
531
532 new_child = get_node_obj(newChild);
533 if(!new_child) return E_FAIL;
534
535 my_ancestor = This->node;
536 while(my_ancestor)
537 {
538 if(my_ancestor == new_child->node)
539 {
540 WARN("tried to create loop\n");
541 return E_FAIL;
542 }
543 my_ancestor = my_ancestor->parent;
544 }
545
546 if(!new_child->node->parent)
547 if(xmldoc_remove_orphan(new_child->node->doc, new_child->node) != S_OK)
548 WARN("%p is not an orphan of %p\n", new_child->node, new_child->node->doc);
549
550 leaving_doc = new_child->node->doc;
551
552 if (leaving_doc != old_child->node->doc)
553 refcount = xmlnode_get_inst_cnt(new_child);
554
555 if (refcount) xmldoc_add_refs(old_child->node->doc, refcount);
556 xmlReplaceNode(old_child->node, new_child->node);
557 if (refcount) xmldoc_release_refs(leaving_doc, refcount);
558 new_child->parent = old_child->parent;
559 old_child->parent = NULL;
560
561 xmldoc_add_orphan(old_child->node->doc, old_child->node);
562
563 if(ret)
564 {
565 IXMLDOMNode_AddRef(oldChild);
566 *ret = oldChild;
567 }
568
569 return S_OK;
570 }
571
572 HRESULT node_remove_child(xmlnode *This, IXMLDOMNode* child, IXMLDOMNode** oldChild)
573 {
574 xmlnode *child_node;
575
576 if(!child) return E_INVALIDARG;
577
578 if(oldChild)
579 *oldChild = NULL;
580
581 child_node = get_node_obj(child);
582 if(!child_node) return E_FAIL;
583
584 if(child_node->node->parent != This->node)
585 {
586 WARN("childNode %p is not a child of %p\n", child, This);
587 return E_INVALIDARG;
588 }
589
590 xmlUnlinkNode(child_node->node);
591 child_node->parent = NULL;
592 xmldoc_add_orphan(child_node->node->doc, child_node->node);
593
594 if(oldChild)
595 {
596 IXMLDOMNode_AddRef(child);
597 *oldChild = child;
598 }
599
600 return S_OK;
601 }
602
603 HRESULT node_append_child(xmlnode *This, IXMLDOMNode *child, IXMLDOMNode **outChild)
604 {
605 DOMNodeType type;
606 VARIANT var;
607 HRESULT hr;
608
609 if (!child)
610 return E_INVALIDARG;
611
612 hr = IXMLDOMNode_get_nodeType(child, &type);
613 if(FAILED(hr) || type == NODE_ATTRIBUTE) {
614 if (outChild) *outChild = NULL;
615 return E_FAIL;
616 }
617
618 VariantInit(&var);
619 return IXMLDOMNode_insertBefore(This->iface, child, var, outChild);
620 }
621
622 HRESULT node_has_childnodes(const xmlnode *This, VARIANT_BOOL *ret)
623 {
624 if (!ret) return E_INVALIDARG;
625
626 if (!This->node->children)
627 {
628 *ret = VARIANT_FALSE;
629 return S_FALSE;
630 }
631
632 *ret = VARIANT_TRUE;
633 return S_OK;
634 }
635
636 HRESULT node_get_owner_doc(const xmlnode *This, IXMLDOMDocument **doc)
637 {
638 if(!doc)
639 return E_INVALIDARG;
640 return get_domdoc_from_xmldoc(This->node->doc, (IXMLDOMDocument3**)doc);
641 }
642
643 HRESULT node_clone(xmlnode *This, VARIANT_BOOL deep, IXMLDOMNode **cloneNode)
644 {
645 IXMLDOMNode *node;
646 xmlNodePtr clone;
647
648 if(!cloneNode) return E_INVALIDARG;
649
650 clone = xmlCopyNode(This->node, deep ? 1 : 2);
651 if (clone)
652 {
653 xmlSetTreeDoc(clone, This->node->doc);
654 xmldoc_add_orphan(clone->doc, clone);
655
656 node = create_node(clone);
657 if (!node)
658 {
659 ERR("Copy failed\n");
660 xmldoc_remove_orphan(clone->doc, clone);
661 xmlFreeNode(clone);
662 return E_FAIL;
663 }
664
665 *cloneNode = node;
666 }
667 else
668 {
669 ERR("Copy failed\n");
670 return E_FAIL;
671 }
672
673 return S_OK;
674 }
675
676 static inline xmlChar* trim_whitespace(xmlChar* str)
677 {
678 xmlChar* ret = str;
679 int len;
680
681 if (!str)
682 return NULL;
683
684 while (*ret && isspace(*ret))
685 ++ret;
686 len = xmlStrlen(ret);
687 if (len)
688 while (isspace(ret[len-1])) --len;
689
690 ret = xmlStrndup(ret, len);
691 xmlFree(str);
692 return ret;
693 }
694
695 static xmlChar* do_get_text(xmlNodePtr node)
696 {
697 xmlNodePtr child;
698 xmlChar* str;
699 BOOL preserving = is_preserving_whitespace(node);
700
701 if (!node->children)
702 {
703 str = xmlNodeGetContent(node);
704 }
705 else
706 {
707 xmlElementType prev_type = XML_TEXT_NODE;
708 xmlChar* tmp;
709 str = xmlStrdup(BAD_CAST "");
710 for (child = node->children; child != NULL; child = child->next)
711 {
712 switch (child->type)
713 {
714 case XML_ELEMENT_NODE:
715 tmp = do_get_text(child);
716 break;
717 case XML_TEXT_NODE:
718 case XML_CDATA_SECTION_NODE:
719 case XML_ENTITY_REF_NODE:
720 case XML_ENTITY_NODE:
721 tmp = xmlNodeGetContent(child);
722 break;
723 default:
724 tmp = NULL;
725 break;
726 }
727
728 if (tmp)
729 {
730 if (*tmp)
731 {
732 if (prev_type == XML_ELEMENT_NODE && child->type == XML_ELEMENT_NODE)
733 str = xmlStrcat(str, BAD_CAST " ");
734 str = xmlStrcat(str, tmp);
735 prev_type = child->type;
736 }
737 xmlFree(tmp);
738 }
739 }
740 }
741
742 switch (node->type)
743 {
744 case XML_ELEMENT_NODE:
745 case XML_TEXT_NODE:
746 case XML_ENTITY_REF_NODE:
747 case XML_ENTITY_NODE:
748 case XML_DOCUMENT_NODE:
749 case XML_DOCUMENT_FRAG_NODE:
750 if (!preserving)
751 str = trim_whitespace(str);
752 break;
753 default:
754 break;
755 }
756
757 return str;
758 }
759
760 HRESULT node_get_text(const xmlnode *This, BSTR *text)
761 {
762 BSTR str = NULL;
763 xmlChar *content;
764
765 if (!text) return E_INVALIDARG;
766
767 content = do_get_text(This->node);
768 if (content)
769 {
770 str = bstr_from_xmlChar(content);
771 xmlFree(content);
772 }
773
774 /* Always return a string. */
775 if (!str) str = SysAllocStringLen( NULL, 0 );
776
777 TRACE("%p %s\n", This, debugstr_w(str) );
778 *text = str;
779
780 return S_OK;
781 }
782
783 HRESULT node_put_text(xmlnode *This, BSTR text)
784 {
785 xmlChar *str, *str2;
786
787 TRACE("(%p)->(%s)\n", This, debugstr_w(text));
788
789 str = xmlchar_from_wchar(text);
790
791 /* Escape the string. */
792 str2 = xmlEncodeEntitiesReentrant(This->node->doc, str);
793 heap_free(str);
794
795 xmlNodeSetContent(This->node, str2);
796 xmlFree(str2);
797
798 return S_OK;
799 }
800
801 BSTR EnsureCorrectEOL(BSTR sInput)
802 {
803 int nNum = 0;
804 BSTR sNew;
805 int nLen;
806 int i;
807
808 nLen = SysStringLen(sInput);
809 /* Count line endings */
810 for(i=0; i < nLen; i++)
811 {
812 if(sInput[i] == '\n')
813 nNum++;
814 }
815
816 TRACE("len=%d, num=%d\n", nLen, nNum);
817
818 /* Add linefeed as needed */
819 if(nNum > 0)
820 {
821 int nPlace = 0;
822 sNew = SysAllocStringLen(NULL, nLen + nNum);
823 for(i=0; i < nLen; i++)
824 {
825 if(sInput[i] == '\n')
826 {
827 sNew[i+nPlace] = '\r';
828 nPlace++;
829 }
830 sNew[i+nPlace] = sInput[i];
831 }
832
833 SysFreeString(sInput);
834 }
835 else
836 {
837 sNew = sInput;
838 }
839
840 TRACE("len %d\n", SysStringLen(sNew));
841
842 return sNew;
843 }
844
845 /*
846 * We are trying to replicate the same behaviour as msxml by converting
847 * line endings to \r\n and using indents as \t. The problem is that msxml
848 * only formats nodes that have a line ending. Using libxml we cannot
849 * reproduce behaviour exactly.
850 *
851 */
852 HRESULT node_get_xml(xmlnode *This, BOOL ensure_eol, BSTR *ret)
853 {
854 xmlBufferPtr xml_buf;
855 xmlNodePtr xmldecl;
856 int size;
857
858 if(!ret)
859 return E_INVALIDARG;
860
861 *ret = NULL;
862
863 xml_buf = xmlBufferCreate();
864 if(!xml_buf)
865 return E_OUTOFMEMORY;
866
867 xmldecl = xmldoc_unlink_xmldecl( This->node->doc );
868
869 size = xmlNodeDump(xml_buf, This->node->doc, This->node, 0, 1);
870 if(size > 0) {
871 const xmlChar *buf_content;
872 BSTR content;
873
874 /* Attribute Nodes return a space in front of their name */
875 buf_content = xmlBufferContent(xml_buf);
876
877 content = bstr_from_xmlChar(buf_content + (buf_content[0] == ' ' ? 1 : 0));
878 if(ensure_eol)
879 content = EnsureCorrectEOL(content);
880
881 *ret = content;
882 }else {
883 *ret = SysAllocStringLen(NULL, 0);
884 }
885
886 xmlBufferFree(xml_buf);
887 xmldoc_link_xmldecl( This->node->doc, xmldecl );
888 return *ret ? S_OK : E_OUTOFMEMORY;
889 }
890
891 #ifdef SONAME_LIBXSLT
892
893 /* duplicates xmlBufferWriteQuotedString() logic */
894 static void xml_write_quotedstring(xmlOutputBufferPtr buf, const xmlChar *string)
895 {
896 const xmlChar *cur, *base;
897
898 if (xmlStrchr(string, '\"'))
899 {
900 if (xmlStrchr(string, '\''))
901 {
902 xmlOutputBufferWrite(buf, 1, "\"");
903 base = cur = string;
904
905 while (*cur)
906 {
907 if (*cur == '"')
908 {
909 if (base != cur)
910 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
911 xmlOutputBufferWrite(buf, 6, "&quot;");
912 cur++;
913 base = cur;
914 }
915 else
916 cur++;
917 }
918 if (base != cur)
919 xmlOutputBufferWrite(buf, cur-base, (const char*)base);
920 xmlOutputBufferWrite(buf, 1, "\"");
921 }
922 else
923 {
924 xmlOutputBufferWrite(buf, 1, "\'");
925 xmlOutputBufferWriteString(buf, (const char*)string);
926 xmlOutputBufferWrite(buf, 1, "\'");
927 }
928 }
929 else
930 {
931 xmlOutputBufferWrite(buf, 1, "\"");
932 xmlOutputBufferWriteString(buf, (const char*)string);
933 xmlOutputBufferWrite(buf, 1, "\"");
934 }
935 }
936
937 static int XMLCALL transform_to_stream_write(void *context, const char *buffer, int len)
938 {
939 DWORD written;
940 HRESULT hr = IStream_Write((IStream*)context, buffer, len, &written);
941 return hr == S_OK ? written : -1;
942 }
943
944 /* Output for method "text" */
945 static void transform_write_text(xmlDocPtr result, xsltStylesheetPtr style, xmlOutputBufferPtr output)
946 {
947 xmlNodePtr cur = result->children;
948 while (cur)
949 {
950 if (cur->type == XML_TEXT_NODE)
951 xmlOutputBufferWriteString(output, (const char*)cur->content);
952
953 /* skip to next node */
954 if (cur->children)
955 {
956 if ((cur->children->type != XML_ENTITY_DECL) &&
957 (cur->children->type != XML_ENTITY_REF_NODE) &&
958 (cur->children->type != XML_ENTITY_NODE))
959 {
960 cur = cur->children;
961 continue;
962 }
963 }
964
965 if (cur->next) {
966 cur = cur->next;
967 continue;
968 }
969
970 do
971 {
972 cur = cur->parent;
973 if (cur == NULL)
974 break;
975 if (cur == (xmlNodePtr) style->doc) {
976 cur = NULL;
977 break;
978 }
979 if (cur->next) {
980 cur = cur->next;
981 break;
982 }
983 } while (cur);
984 }
985 }
986
987 #undef XSLT_GET_IMPORT_PTR
988 #define XSLT_GET_IMPORT_PTR(res, style, name) { \
989 xsltStylesheetPtr st = style; \
990 res = NULL; \
991 while (st != NULL) { \
992 if (st->name != NULL) { res = st->name; break; } \
993 st = pxsltNextImport(st); \
994 }}
995
996 #undef XSLT_GET_IMPORT_INT
997 #define XSLT_GET_IMPORT_INT(res, style, name) { \
998 xsltStylesheetPtr st = style; \
999 res = -1; \
1000 while (st != NULL) { \
1001 if (st->name != -1) { res = st->name; break; } \
1002 st = pxsltNextImport(st); \
1003 }}
1004
1005 static void transform_write_xmldecl(xmlDocPtr result, xsltStylesheetPtr style, BOOL omit_encoding, xmlOutputBufferPtr output)
1006 {
1007 int omit_xmldecl, standalone;
1008
1009 XSLT_GET_IMPORT_INT(omit_xmldecl, style, omitXmlDeclaration);
1010 if (omit_xmldecl == 1) return;
1011
1012 XSLT_GET_IMPORT_INT(standalone, style, standalone);
1013
1014 xmlOutputBufferWriteString(output, "<?xml version=");
1015 if (result->version)
1016 {
1017 xmlOutputBufferWriteString(output, "\"");
1018 xmlOutputBufferWriteString(output, (const char *)result->version);
1019 xmlOutputBufferWriteString(output, "\"");
1020 }
1021 else
1022 xmlOutputBufferWriteString(output, "\"1.0\"");
1023
1024 if (!omit_encoding)
1025 {
1026 const xmlChar *encoding;
1027
1028 /* default encoding is UTF-16 */
1029 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1030 xmlOutputBufferWriteString(output, " encoding=");
1031 xmlOutputBufferWriteString(output, "\"");
1032 xmlOutputBufferWriteString(output, encoding ? (const char *)encoding : "UTF-16");
1033 xmlOutputBufferWriteString(output, "\"");
1034 }
1035
1036 /* standalone attribute */
1037 if (standalone != -1)
1038 xmlOutputBufferWriteString(output, standalone == 0 ? " standalone=\"no\"" : " standalone=\"yes\"");
1039
1040 xmlOutputBufferWriteString(output, "?>");
1041 }
1042
1043 static void htmldtd_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc)
1044 {
1045 xmlDtdPtr cur = doc->intSubset;
1046
1047 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
1048 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1049 if (cur->ExternalID)
1050 {
1051 xmlOutputBufferWriteString(buf, " PUBLIC ");
1052 xml_write_quotedstring(buf, cur->ExternalID);
1053 if (cur->SystemID)
1054 {
1055 xmlOutputBufferWriteString(buf, " ");
1056 xml_write_quotedstring(buf, cur->SystemID);
1057 }
1058 }
1059 else if (cur->SystemID)
1060 {
1061 xmlOutputBufferWriteString(buf, " SYSTEM ");
1062 xml_write_quotedstring(buf, cur->SystemID);
1063 }
1064 xmlOutputBufferWriteString(buf, ">\n");
1065 }
1066
1067 /* Duplicates htmlDocContentDumpFormatOutput() the way we need it - doesn't add trailing newline. */
1068 static void htmldoc_dumpcontent(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding, int format)
1069 {
1070 xmlElementType type;
1071
1072 /* force HTML output */
1073 type = doc->type;
1074 doc->type = XML_HTML_DOCUMENT_NODE;
1075 if (doc->intSubset)
1076 htmldtd_dumpcontent(buf, doc);
1077 if (doc->children) {
1078 xmlNodePtr cur = doc->children;
1079 while (cur) {
1080 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
1081 cur = cur->next;
1082 }
1083 }
1084 doc->type = type;
1085 }
1086
1087 static inline BOOL transform_is_empty_resultdoc(xmlDocPtr result)
1088 {
1089 return !result->children || ((result->children->type == XML_DTD_NODE) && !result->children->next);
1090 }
1091
1092 static inline BOOL transform_is_valid_method(xsltStylesheetPtr style)
1093 {
1094 return !style->methodURI || !(style->method && xmlStrEqual(style->method, (const xmlChar *)"xhtml"));
1095 }
1096
1097 /* Helper to write transformation result to specified output buffer. */
1098 static HRESULT node_transform_write(xsltStylesheetPtr style, xmlDocPtr result, BOOL omit_encoding, const char *encoding, xmlOutputBufferPtr output)
1099 {
1100 const xmlChar *method;
1101 int indent;
1102
1103 if (!transform_is_valid_method(style))
1104 {
1105 ERR("unknown output method\n");
1106 return E_FAIL;
1107 }
1108
1109 XSLT_GET_IMPORT_PTR(method, style, method)
1110 XSLT_GET_IMPORT_INT(indent, style, indent);
1111
1112 if (!method && (result->type == XML_HTML_DOCUMENT_NODE))
1113 method = (const xmlChar *) "html";
1114
1115 if (method && xmlStrEqual(method, (const xmlChar *)"html"))
1116 {
1117 htmlSetMetaEncoding(result, (const xmlChar *)encoding);
1118 if (indent == -1)
1119 indent = 1;
1120 htmldoc_dumpcontent(output, result, (const char*)encoding, indent);
1121 }
1122 else if (method && xmlStrEqual(method, (const xmlChar *)"xhtml"))
1123 {
1124 htmlSetMetaEncoding(result, (const xmlChar *) encoding);
1125 htmlDocContentDumpOutput(output, result, encoding);
1126 }
1127 else if (method && xmlStrEqual(method, (const xmlChar *)"text"))
1128 transform_write_text(result, style, output);
1129 else
1130 {
1131 transform_write_xmldecl(result, style, omit_encoding, output);
1132
1133 if (result->children)
1134 {
1135 xmlNodePtr child = result->children;
1136
1137 while (child)
1138 {
1139 xmlNodeDumpOutput(output, result, child, 0, indent == 1, encoding);
1140 if (indent && ((child->type == XML_DTD_NODE) || ((child->type == XML_COMMENT_NODE) && child->next)))
1141 xmlOutputBufferWriteString(output, "\r\n");
1142 child = child->next;
1143 }
1144 }
1145 }
1146
1147 xmlOutputBufferFlush(output);
1148 return S_OK;
1149 }
1150
1151 /* For BSTR output is always UTF-16, without 'encoding' attribute */
1152 static HRESULT node_transform_write_to_bstr(xsltStylesheetPtr style, xmlDocPtr result, BSTR *str)
1153 {
1154 HRESULT hr = S_OK;
1155
1156 if (transform_is_empty_resultdoc(result))
1157 *str = SysAllocStringLen(NULL, 0);
1158 else
1159 {
1160 xmlOutputBufferPtr output = xmlAllocOutputBuffer(xmlFindCharEncodingHandler("UTF-16"));
1161 const xmlChar *content;
1162 size_t len;
1163
1164 *str = NULL;
1165 if (!output)
1166 return E_OUTOFMEMORY;
1167
1168 hr = node_transform_write(style, result, TRUE, "UTF-16", output);
1169 #ifdef LIBXML2_NEW_BUFFER
1170 content = xmlBufContent(output->conv);
1171 len = xmlBufUse(output->conv);
1172 #else
1173 content = xmlBufferContent(output->conv);
1174 len = xmlBufferLength(output->conv);
1175 #endif
1176 /* UTF-16 encoder places UTF-16 bom, we don't need it for BSTR */
1177 content += sizeof(WCHAR);
1178 *str = SysAllocStringLen((WCHAR*)content, len/sizeof(WCHAR) - 1);
1179 xmlOutputBufferClose(output);
1180 }
1181
1182 return *str ? hr : E_OUTOFMEMORY;
1183 }
1184
1185 static HRESULT node_transform_write_to_stream(xsltStylesheetPtr style, xmlDocPtr result, IStream *stream)
1186 {
1187 static const xmlChar *utf16 = (const xmlChar*)"UTF-16";
1188 xmlOutputBufferPtr output;
1189 const xmlChar *encoding;
1190 HRESULT hr;
1191
1192 if (transform_is_empty_resultdoc(result))
1193 {
1194 WARN("empty result document\n");
1195 return S_OK;
1196 }
1197
1198 if (style->methodURI && (!style->method || !xmlStrEqual(style->method, (const xmlChar *) "xhtml")))
1199 {
1200 ERR("unknown output method\n");
1201 return E_FAIL;
1202 }
1203
1204 /* default encoding is UTF-16 */
1205 XSLT_GET_IMPORT_PTR(encoding, style, encoding);
1206 if (!encoding)
1207 encoding = utf16;
1208
1209 output = xmlOutputBufferCreateIO(transform_to_stream_write, NULL, stream, xmlFindCharEncodingHandler((const char*)encoding));
1210 if (!output)
1211 return E_OUTOFMEMORY;
1212
1213 hr = node_transform_write(style, result, FALSE, (const char*)encoding, output);
1214 xmlOutputBufferClose(output);
1215 return hr;
1216 }
1217
1218 #endif
1219
1220 HRESULT node_transform_node_params(const xmlnode *This, IXMLDOMNode *stylesheet, BSTR *p,
1221 IStream *stream, const struct xslprocessor_params *params)
1222 {
1223 #ifdef SONAME_LIBXSLT
1224 xsltStylesheetPtr xsltSS;
1225 HRESULT hr = S_OK;
1226 xmlnode *sheet;
1227
1228 if (!libxslt_handle) return E_NOTIMPL;
1229 if (!stylesheet || !p) return E_INVALIDARG;
1230
1231 *p = NULL;
1232
1233 sheet = get_node_obj(stylesheet);
1234 if(!sheet) return E_FAIL;
1235
1236 xsltSS = pxsltParseStylesheetDoc(sheet->node->doc);
1237 if(xsltSS)
1238 {
1239 const char **xslparams = NULL;
1240 xmlDocPtr result;
1241 unsigned int i;
1242
1243 /* convert our parameter list to libxml2 format */
1244 if (params && params->count)
1245 {
1246 struct xslprocessor_par *par;
1247
1248 i = 0;
1249 xslparams = heap_alloc((params->count*2 + 1)*sizeof(char*));
1250 LIST_FOR_EACH_ENTRY(par, &params->list, struct xslprocessor_par, entry)
1251 {
1252 xslparams[i++] = (char*)xmlchar_from_wchar(par->name);
1253 xslparams[i++] = (char*)xmlchar_from_wchar(par->value);
1254 }
1255 xslparams[i] = NULL;
1256 }
1257
1258 if (xslparams)
1259 {
1260 xsltTransformContextPtr ctxt = pxsltNewTransformContext(xsltSS, This->node->doc);
1261
1262 /* push parameters to user context */
1263 pxsltQuoteUserParams(ctxt, xslparams);
1264 result = pxsltApplyStylesheetUser(xsltSS, This->node->doc, NULL, NULL, NULL, ctxt);
1265 pxsltFreeTransformContext(ctxt);
1266
1267 for (i = 0; i < params->count*2; i++)
1268 heap_free((char*)xslparams[i]);
1269 heap_free(xslparams);
1270 }
1271 else
1272 result = pxsltApplyStylesheet(xsltSS, This->node->doc, NULL);
1273
1274 if (result)
1275 {
1276 if (stream)
1277 hr = node_transform_write_to_stream(xsltSS, result, stream);
1278 else
1279 hr = node_transform_write_to_bstr(xsltSS, result, p);
1280 xmlFreeDoc(result);
1281 }
1282 /* libxslt "helpfully" frees the XML document the stylesheet was
1283 generated from, too */
1284 xsltSS->doc = NULL;
1285 pxsltFreeStylesheet(xsltSS);
1286 }
1287
1288 if(!*p) *p = SysAllocStringLen(NULL, 0);
1289
1290 return hr;
1291 #else
1292 ERR_(winediag)("libxslt headers were not found at compile time. Expect problems.\n");
1293
1294 return E_NOTIMPL;
1295 #endif
1296 }
1297
1298 HRESULT node_transform_node(const xmlnode *node, IXMLDOMNode *stylesheet, BSTR *p)
1299 {
1300 return node_transform_node_params(node, stylesheet, p, NULL, NULL);
1301 }
1302
1303 HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
1304 {
1305 xmlChar* str;
1306 HRESULT hr;
1307
1308 if (!query || !nodes) return E_INVALIDARG;
1309
1310 str = xmlchar_from_wchar(query);
1311 hr = create_selection(This->node, str, nodes);
1312 heap_free(str);
1313
1314 return hr;
1315 }
1316
1317 HRESULT node_select_singlenode(const xmlnode *This, BSTR query, IXMLDOMNode **node)
1318 {
1319 IXMLDOMNodeList *list;
1320 HRESULT hr;
1321
1322 hr = node_select_nodes(This, query, &list);
1323 if (hr == S_OK)
1324 {
1325 hr = IXMLDOMNodeList_nextNode(list, node);
1326 IXMLDOMNodeList_Release(list);
1327 }
1328 return hr;
1329 }
1330
1331 HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1332 {
1333 xmlNsPtr ns = This->node->ns;
1334
1335 if(!namespaceURI)
1336 return E_INVALIDARG;
1337
1338 *namespaceURI = NULL;
1339
1340 if (ns && ns->href)
1341 *namespaceURI = bstr_from_xmlChar(ns->href);
1342
1343 TRACE("uri: %s\n", debugstr_w(*namespaceURI));
1344
1345 return *namespaceURI ? S_OK : S_FALSE;
1346 }
1347
1348 HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1349 {
1350 xmlNsPtr ns = This->node->ns;
1351
1352 if (!prefix) return E_INVALIDARG;
1353
1354 *prefix = NULL;
1355
1356 if (ns && ns->prefix)
1357 *prefix = bstr_from_xmlChar(ns->prefix);
1358
1359 TRACE("prefix: %s\n", debugstr_w(*prefix));
1360
1361 return *prefix ? S_OK : S_FALSE;
1362 }
1363
1364 HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1365 {
1366 if (!name) return E_INVALIDARG;
1367
1368 *name = bstr_from_xmlChar(This->node->name);
1369 if (!*name) return E_OUTOFMEMORY;
1370
1371 TRACE("returning %s\n", debugstr_w(*name));
1372
1373 return S_OK;
1374 }
1375
1376 /* _private field holds a number of COM instances spawned from this libxml2 node */
1377 static void xmlnode_add_ref(xmlNodePtr node)
1378 {
1379 if (node->type == XML_DOCUMENT_NODE) return;
1380 InterlockedIncrement((LONG*)&node->_private);
1381 }
1382
1383 static void xmlnode_release(xmlNodePtr node)
1384 {
1385 if (node->type == XML_DOCUMENT_NODE) return;
1386 InterlockedDecrement((LONG*)&node->_private);
1387 }
1388
1389 void destroy_xmlnode(xmlnode *This)
1390 {
1391 if(This->node)
1392 {
1393 xmlnode_release(This->node);
1394 xmldoc_release(This->node->doc);
1395 }
1396 }
1397
1398 void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1399 {
1400 if(node)
1401 {
1402 xmlnode_add_ref(node);
1403 xmldoc_add_ref(node->doc);
1404 }
1405
1406 This->node = node;
1407 This->iface = node_iface;
1408 This->parent = NULL;
1409
1410 init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1411 }
1412
1413 typedef struct {
1414 xmlnode node;
1415 IXMLDOMNode IXMLDOMNode_iface;
1416 LONG ref;
1417 } unknode;
1418
1419 static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1420 {
1421 return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1422 }
1423
1424 static HRESULT WINAPI unknode_QueryInterface(
1425 IXMLDOMNode *iface,
1426 REFIID riid,
1427 void** ppvObject )
1428 {
1429 unknode *This = unknode_from_IXMLDOMNode( iface );
1430
1431 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1432
1433 if (IsEqualGUID(riid, &IID_IUnknown)) {
1434 *ppvObject = iface;
1435 }else if (IsEqualGUID( riid, &IID_IDispatch) ||
1436 IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1437 *ppvObject = &This->IXMLDOMNode_iface;
1438 }else if(node_query_interface(&This->node, riid, ppvObject)) {
1439 return *ppvObject ? S_OK : E_NOINTERFACE;
1440 }else {
1441 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1442 *ppvObject = NULL;
1443 return E_NOINTERFACE;
1444 }
1445
1446 IUnknown_AddRef((IUnknown*)*ppvObject);
1447 return S_OK;
1448 }
1449
1450 static ULONG WINAPI unknode_AddRef(
1451 IXMLDOMNode *iface )
1452 {
1453 unknode *This = unknode_from_IXMLDOMNode( iface );
1454
1455 return InterlockedIncrement(&This->ref);
1456 }
1457
1458 static ULONG WINAPI unknode_Release(
1459 IXMLDOMNode *iface )
1460 {
1461 unknode *This = unknode_from_IXMLDOMNode( iface );
1462 LONG ref;
1463
1464 ref = InterlockedDecrement( &This->ref );
1465 if(!ref) {
1466 destroy_xmlnode(&This->node);
1467 heap_free(This);
1468 }
1469
1470 return ref;
1471 }
1472
1473 static HRESULT WINAPI unknode_GetTypeInfoCount(
1474 IXMLDOMNode *iface,
1475 UINT* pctinfo )
1476 {
1477 unknode *This = unknode_from_IXMLDOMNode( iface );
1478
1479 TRACE("(%p)->(%p)\n", This, pctinfo);
1480
1481 *pctinfo = 1;
1482
1483 return S_OK;
1484 }
1485
1486 static HRESULT WINAPI unknode_GetTypeInfo(
1487 IXMLDOMNode *iface,
1488 UINT iTInfo,
1489 LCID lcid,
1490 ITypeInfo** ppTInfo )
1491 {
1492 unknode *This = unknode_from_IXMLDOMNode( iface );
1493 HRESULT hr;
1494
1495 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1496
1497 hr = get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1498
1499 return hr;
1500 }
1501
1502 static HRESULT WINAPI unknode_GetIDsOfNames(
1503 IXMLDOMNode *iface,
1504 REFIID riid,
1505 LPOLESTR* rgszNames,
1506 UINT cNames,
1507 LCID lcid,
1508 DISPID* rgDispId )
1509 {
1510 unknode *This = unknode_from_IXMLDOMNode( iface );
1511
1512 ITypeInfo *typeinfo;
1513 HRESULT hr;
1514
1515 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1516 lcid, rgDispId);
1517
1518 if(!rgszNames || cNames == 0 || !rgDispId)
1519 return E_INVALIDARG;
1520
1521 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1522 if(SUCCEEDED(hr))
1523 {
1524 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1525 ITypeInfo_Release(typeinfo);
1526 }
1527
1528 return hr;
1529 }
1530
1531 static HRESULT WINAPI unknode_Invoke(
1532 IXMLDOMNode *iface,
1533 DISPID dispIdMember,
1534 REFIID riid,
1535 LCID lcid,
1536 WORD wFlags,
1537 DISPPARAMS* pDispParams,
1538 VARIANT* pVarResult,
1539 EXCEPINFO* pExcepInfo,
1540 UINT* puArgErr )
1541 {
1542 unknode *This = unknode_from_IXMLDOMNode( iface );
1543 ITypeInfo *typeinfo;
1544 HRESULT hr;
1545
1546 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1547 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1548
1549 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1550 if(SUCCEEDED(hr))
1551 {
1552 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDOMNode_iface, dispIdMember, wFlags, pDispParams,
1553 pVarResult, pExcepInfo, puArgErr);
1554 ITypeInfo_Release(typeinfo);
1555 }
1556
1557 return hr;
1558 }
1559
1560 static HRESULT WINAPI unknode_get_nodeName(
1561 IXMLDOMNode *iface,
1562 BSTR* p )
1563 {
1564 unknode *This = unknode_from_IXMLDOMNode( iface );
1565
1566 FIXME("(%p)->(%p)\n", This, p);
1567
1568 return node_get_nodeName(&This->node, p);
1569 }
1570
1571 static HRESULT WINAPI unknode_get_nodeValue(
1572 IXMLDOMNode *iface,
1573 VARIANT* value)
1574 {
1575 unknode *This = unknode_from_IXMLDOMNode( iface );
1576
1577 FIXME("(%p)->(%p)\n", This, value);
1578
1579 if(!value)
1580 return E_INVALIDARG;
1581
1582 V_VT(value) = VT_NULL;
1583 return S_FALSE;
1584 }
1585
1586 static HRESULT WINAPI unknode_put_nodeValue(
1587 IXMLDOMNode *iface,
1588 VARIANT value)
1589 {
1590 unknode *This = unknode_from_IXMLDOMNode( iface );
1591 FIXME("(%p)->(v%d)\n", This, V_VT(&value));
1592 return E_FAIL;
1593 }
1594
1595 static HRESULT WINAPI unknode_get_nodeType(
1596 IXMLDOMNode *iface,
1597 DOMNodeType* domNodeType )
1598 {
1599 unknode *This = unknode_from_IXMLDOMNode( iface );
1600
1601 FIXME("(%p)->(%p)\n", This, domNodeType);
1602
1603 switch (This->node.node->type)
1604 {
1605 case XML_ELEMENT_NODE:
1606 case XML_ATTRIBUTE_NODE:
1607 case XML_TEXT_NODE:
1608 case XML_CDATA_SECTION_NODE:
1609 case XML_ENTITY_REF_NODE:
1610 case XML_ENTITY_NODE:
1611 case XML_PI_NODE:
1612 case XML_COMMENT_NODE:
1613 case XML_DOCUMENT_NODE:
1614 case XML_DOCUMENT_TYPE_NODE:
1615 case XML_DOCUMENT_FRAG_NODE:
1616 case XML_NOTATION_NODE:
1617 /* we only care about this set of types, libxml2 type values are
1618 exactly what we need */
1619 *domNodeType = (DOMNodeType)This->node.node->type;
1620 break;
1621 default:
1622 *domNodeType = NODE_INVALID;
1623 break;
1624 }
1625
1626 return S_OK;
1627 }
1628
1629 static HRESULT WINAPI unknode_get_parentNode(
1630 IXMLDOMNode *iface,
1631 IXMLDOMNode** parent )
1632 {
1633 unknode *This = unknode_from_IXMLDOMNode( iface );
1634 FIXME("(%p)->(%p)\n", This, parent);
1635 if (!parent) return E_INVALIDARG;
1636 *parent = NULL;
1637 return S_FALSE;
1638 }
1639
1640 static HRESULT WINAPI unknode_get_childNodes(
1641 IXMLDOMNode *iface,
1642 IXMLDOMNodeList** outList)
1643 {
1644 unknode *This = unknode_from_IXMLDOMNode( iface );
1645
1646 TRACE("(%p)->(%p)\n", This, outList);
1647
1648 return node_get_child_nodes(&This->node, outList);
1649 }
1650
1651 static HRESULT WINAPI unknode_get_firstChild(
1652 IXMLDOMNode *iface,
1653 IXMLDOMNode** domNode)
1654 {
1655 unknode *This = unknode_from_IXMLDOMNode( iface );
1656
1657 TRACE("(%p)->(%p)\n", This, domNode);
1658
1659 return node_get_first_child(&This->node, domNode);
1660 }
1661
1662 static HRESULT WINAPI unknode_get_lastChild(
1663 IXMLDOMNode *iface,
1664 IXMLDOMNode** domNode)
1665 {
1666 unknode *This = unknode_from_IXMLDOMNode( iface );
1667
1668 TRACE("(%p)->(%p)\n", This, domNode);
1669
1670 return node_get_last_child(&This->node, domNode);
1671 }
1672
1673 static HRESULT WINAPI unknode_get_previousSibling(
1674 IXMLDOMNode *iface,
1675 IXMLDOMNode** domNode)
1676 {
1677 unknode *This = unknode_from_IXMLDOMNode( iface );
1678
1679 TRACE("(%p)->(%p)\n", This, domNode);
1680
1681 return node_get_previous_sibling(&This->node, domNode);
1682 }
1683
1684 static HRESULT WINAPI unknode_get_nextSibling(
1685 IXMLDOMNode *iface,
1686 IXMLDOMNode** domNode)
1687 {
1688 unknode *This = unknode_from_IXMLDOMNode( iface );
1689
1690 TRACE("(%p)->(%p)\n", This, domNode);
1691
1692 return node_get_next_sibling(&This->node, domNode);
1693 }
1694
1695 static HRESULT WINAPI unknode_get_attributes(
1696 IXMLDOMNode *iface,
1697 IXMLDOMNamedNodeMap** attributeMap)
1698 {
1699 unknode *This = unknode_from_IXMLDOMNode( iface );
1700
1701 FIXME("(%p)->(%p)\n", This, attributeMap);
1702
1703 return return_null_ptr((void**)attributeMap);
1704 }
1705
1706 static HRESULT WINAPI unknode_insertBefore(
1707 IXMLDOMNode *iface,
1708 IXMLDOMNode* newNode, VARIANT refChild,
1709 IXMLDOMNode** outOldNode)
1710 {
1711 unknode *This = unknode_from_IXMLDOMNode( iface );
1712
1713 FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);
1714
1715 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1716 }
1717
1718 static HRESULT WINAPI unknode_replaceChild(
1719 IXMLDOMNode *iface,
1720 IXMLDOMNode* newNode,
1721 IXMLDOMNode* oldNode,
1722 IXMLDOMNode** outOldNode)
1723 {
1724 unknode *This = unknode_from_IXMLDOMNode( iface );
1725
1726 FIXME("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
1727
1728 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
1729 }
1730
1731 static HRESULT WINAPI unknode_removeChild(
1732 IXMLDOMNode *iface,
1733 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
1734 {
1735 unknode *This = unknode_from_IXMLDOMNode( iface );
1736 return node_remove_child(&This->node, domNode, oldNode);
1737 }
1738
1739 static HRESULT WINAPI unknode_appendChild(
1740 IXMLDOMNode *iface,
1741 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
1742 {
1743 unknode *This = unknode_from_IXMLDOMNode( iface );
1744 return node_append_child(&This->node, newNode, outNewNode);
1745 }
1746
1747 static HRESULT WINAPI unknode_hasChildNodes(
1748 IXMLDOMNode *iface,
1749 VARIANT_BOOL* pbool)
1750 {
1751 unknode *This = unknode_from_IXMLDOMNode( iface );
1752 return node_has_childnodes(&This->node, pbool);
1753 }
1754
1755 static HRESULT WINAPI unknode_get_ownerDocument(
1756 IXMLDOMNode *iface,
1757 IXMLDOMDocument** domDocument)
1758 {
1759 unknode *This = unknode_from_IXMLDOMNode( iface );
1760 return node_get_owner_doc(&This->node, domDocument);
1761 }
1762
1763 static HRESULT WINAPI unknode_cloneNode(
1764 IXMLDOMNode *iface,
1765 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
1766 {
1767 unknode *This = unknode_from_IXMLDOMNode( iface );
1768 return node_clone(&This->node, pbool, outNode );
1769 }
1770
1771 static HRESULT WINAPI unknode_get_nodeTypeString(
1772 IXMLDOMNode *iface,
1773 BSTR* p)
1774 {
1775 unknode *This = unknode_from_IXMLDOMNode( iface );
1776
1777 FIXME("(%p)->(%p)\n", This, p);
1778
1779 return node_get_nodeName(&This->node, p);
1780 }
1781
1782 static HRESULT WINAPI unknode_get_text(
1783 IXMLDOMNode *iface,
1784 BSTR* p)
1785 {
1786 unknode *This = unknode_from_IXMLDOMNode( iface );
1787 return node_get_text(&This->node, p);
1788 }
1789
1790 static HRESULT WINAPI unknode_put_text(
1791 IXMLDOMNode *iface,
1792 BSTR p)
1793 {
1794 unknode *This = unknode_from_IXMLDOMNode( iface );
1795 return node_put_text(&This->node, p);
1796 }
1797
1798 static HRESULT WINAPI unknode_get_specified(
1799 IXMLDOMNode *iface,
1800 VARIANT_BOOL* isSpecified)
1801 {
1802 unknode *This = unknode_from_IXMLDOMNode( iface );
1803 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
1804 *isSpecified = VARIANT_TRUE;
1805 return S_OK;
1806 }
1807
1808 static HRESULT WINAPI unknode_get_definition(
1809 IXMLDOMNode *iface,
1810 IXMLDOMNode** definitionNode)
1811 {
1812 unknode *This = unknode_from_IXMLDOMNode( iface );
1813 FIXME("(%p)->(%p)\n", This, definitionNode);
1814 return E_NOTIMPL;
1815 }
1816
1817 static HRESULT WINAPI unknode_get_nodeTypedValue(
1818 IXMLDOMNode *iface,
1819 VARIANT* var1)
1820 {
1821 unknode *This = unknode_from_IXMLDOMNode( iface );
1822 FIXME("(%p)->(%p)\n", This, var1);
1823 return return_null_var(var1);
1824 }
1825
1826 static HRESULT WINAPI unknode_put_nodeTypedValue(
1827 IXMLDOMNode *iface,
1828 VARIANT typedValue)
1829 {
1830 unknode *This = unknode_from_IXMLDOMNode( iface );
1831 FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
1832 return E_NOTIMPL;
1833 }
1834
1835 static HRESULT WINAPI unknode_get_dataType(
1836 IXMLDOMNode *iface,
1837 VARIANT* var1)
1838 {
1839 unknode *This = unknode_from_IXMLDOMNode( iface );
1840 TRACE("(%p)->(%p)\n", This, var1);
1841 return return_null_var(var1);
1842 }
1843
1844 static HRESULT WINAPI unknode_put_dataType(
1845 IXMLDOMNode *iface,
1846 BSTR p)
1847 {
1848 unknode *This = unknode_from_IXMLDOMNode( iface );
1849
1850 FIXME("(%p)->(%s)\n", This, debugstr_w(p));
1851
1852 if(!p)
1853 return E_INVALIDARG;
1854
1855 return E_FAIL;
1856 }
1857
1858 static HRESULT WINAPI unknode_get_xml(
1859 IXMLDOMNode *iface,
1860 BSTR* p)
1861 {
1862 unknode *This = unknode_from_IXMLDOMNode( iface );
1863
1864 FIXME("(%p)->(%p)\n", This, p);
1865
1866 return node_get_xml(&This->node, FALSE, p);
1867 }
1868
1869 static HRESULT WINAPI unknode_transformNode(
1870 IXMLDOMNode *iface,
1871 IXMLDOMNode* domNode, BSTR* p)
1872 {
1873 unknode *This = unknode_from_IXMLDOMNode( iface );
1874 return node_transform_node(&This->node, domNode, p);
1875 }
1876
1877 static HRESULT WINAPI unknode_selectNodes(
1878 IXMLDOMNode *iface,
1879 BSTR p, IXMLDOMNodeList** outList)
1880 {
1881 unknode *This = unknode_from_IXMLDOMNode( iface );
1882 return node_select_nodes(&This->node, p, outList);
1883 }
1884
1885 static HRESULT WINAPI unknode_selectSingleNode(
1886 IXMLDOMNode *iface,
1887 BSTR p, IXMLDOMNode** outNode)
1888 {
1889 unknode *This = unknode_from_IXMLDOMNode( iface );
1890 return node_select_singlenode(&This->node, p, outNode);
1891 }
1892
1893 static HRESULT WINAPI unknode_get_parsed(
1894 IXMLDOMNode *iface,
1895 VARIANT_BOOL* isParsed)
1896 {
1897 unknode *This = unknode_from_IXMLDOMNode( iface );
1898 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1899 *isParsed = VARIANT_TRUE;
1900 return S_OK;
1901 }
1902
1903 static HRESULT WINAPI unknode_get_namespaceURI(
1904 IXMLDOMNode *iface,
1905 BSTR* p)
1906 {
1907 unknode *This = unknode_from_IXMLDOMNode( iface );
1908 TRACE("(%p)->(%p)\n", This, p);
1909 return node_get_namespaceURI(&This->node, p);
1910 }
1911
1912 static HRESULT WINAPI unknode_get_prefix(
1913 IXMLDOMNode *iface,
1914 BSTR* p)
1915 {
1916 unknode *This = unknode_from_IXMLDOMNode( iface );
1917 return node_get_prefix(&This->node, p);
1918 }
1919
1920 static HRESULT WINAPI unknode_get_baseName(
1921 IXMLDOMNode *iface,
1922 BSTR* p)
1923 {
1924 unknode *This = unknode_from_IXMLDOMNode( iface );
1925 return node_get_base_name(&This->node, p);
1926 }
1927
1928 static HRESULT WINAPI unknode_transformNodeToObject(
1929 IXMLDOMNode *iface,
1930 IXMLDOMNode* domNode, VARIANT var1)
1931 {
1932 unknode *This = unknode_from_IXMLDOMNode( iface );
1933 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
1934 return E_NOTIMPL;
1935 }
1936
1937 static const struct IXMLDOMNodeVtbl unknode_vtbl =
1938 {
1939 unknode_QueryInterface,
1940 unknode_AddRef,
1941 unknode_Release,
1942 unknode_GetTypeInfoCount,
1943 unknode_GetTypeInfo,
1944 unknode_GetIDsOfNames,
1945 unknode_Invoke,
1946 unknode_get_nodeName,
1947 unknode_get_nodeValue,
1948 unknode_put_nodeValue,
1949 unknode_get_nodeType,
1950 unknode_get_parentNode,
1951 unknode_get_childNodes,
1952 unknode_get_firstChild,
1953 unknode_get_lastChild,
1954 unknode_get_previousSibling,
1955 unknode_get_nextSibling,
1956 unknode_get_attributes,
1957 unknode_insertBefore,
1958 unknode_replaceChild,
1959 unknode_removeChild,
1960 unknode_appendChild,
1961 unknode_hasChildNodes,
1962 unknode_get_ownerDocument,
1963 unknode_cloneNode,
1964 unknode_get_nodeTypeString,
1965 unknode_get_text,
1966 unknode_put_text,
1967 unknode_get_specified,
1968 unknode_get_definition,
1969 unknode_get_nodeTypedValue,
1970 unknode_put_nodeTypedValue,
1971 unknode_get_dataType,
1972 unknode_put_dataType,
1973 unknode_get_xml,
1974 unknode_transformNode,
1975 unknode_selectNodes,
1976 unknode_selectSingleNode,
1977 unknode_get_parsed,
1978 unknode_get_namespaceURI,
1979 unknode_get_prefix,
1980 unknode_get_baseName,
1981 unknode_transformNodeToObject
1982 };
1983
1984 IXMLDOMNode *create_node( xmlNodePtr node )
1985 {
1986 IUnknown *pUnk;
1987 IXMLDOMNode *ret;
1988 HRESULT hr;
1989
1990 if ( !node )
1991 return NULL;
1992
1993 TRACE("type %d\n", node->type);
1994 switch(node->type)
1995 {
1996 case XML_ELEMENT_NODE:
1997 pUnk = create_element( node );
1998 break;
1999 case XML_ATTRIBUTE_NODE:
2000 pUnk = create_attribute( node );
2001 break;
2002 case XML_TEXT_NODE:
2003 pUnk = create_text( node );
2004 break;
2005 case XML_CDATA_SECTION_NODE:
2006 pUnk = create_cdata( node );
2007 break;
2008 case XML_ENTITY_REF_NODE:
2009 pUnk = create_doc_entity_ref( node );
2010 break;
2011 case XML_PI_NODE:
2012 pUnk = create_pi( node );
2013 break;
2014 case XML_COMMENT_NODE:
2015 pUnk = create_comment( node );
2016 break;
2017 case XML_DOCUMENT_NODE:
2018 pUnk = create_domdoc( node );
2019 break;
2020 case XML_DOCUMENT_FRAG_NODE:
2021 pUnk = create_doc_fragment( node );
2022 break;
2023 case XML_DTD_NODE:
2024 case XML_DOCUMENT_TYPE_NODE:
2025 pUnk = create_doc_type( node );
2026 break;
2027 case XML_ENTITY_NODE:
2028 case XML_NOTATION_NODE: {
2029 unknode *new_node;
2030
2031 FIXME("only creating basic node for type %d\n", node->type);
2032
2033 new_node = heap_alloc(sizeof(unknode));
2034 if(!new_node)
2035 return NULL;
2036
2037 new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2038 new_node->ref = 1;
2039 init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
2040 pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2041 break;
2042 }
2043 default:
2044 ERR("Called for unsupported node type %d\n", node->type);
2045 return NULL;
2046 }
2047
2048 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
2049 IUnknown_Release(pUnk);
2050 if(FAILED(hr)) return NULL;
2051 return ret;
2052 }
2053 #endif