Create a branch for console restructuration work.
[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 #endif
56
57 static const IID IID_xmlnode = {0x4f2f4ba2,0xb822,0x11df,{0x8b,0x8a,0x68,0x50,0xdf,0xd7,0x20,0x85}};
58
59 xmlNodePtr xmlNodePtr_from_domnode( IXMLDOMNode *iface, xmlElementType type )
60 {
61 xmlnode *This;
62
63 if ( !iface )
64 return NULL;
65 This = get_node_obj( iface );
66 if ( !This || !This->node )
67 return NULL;
68 if ( type && This->node->type != type )
69 return NULL;
70 return This->node;
71 }
72
73 BOOL node_query_interface(xmlnode *This, REFIID riid, void **ppv)
74 {
75 if(IsEqualGUID(&IID_xmlnode, riid)) {
76 TRACE("(%p)->(IID_xmlnode %p)\n", This, ppv);
77 *ppv = This;
78 return TRUE;
79 }
80
81 return dispex_query_interface(&This->dispex, riid, ppv);
82 }
83
84 /* common ISupportErrorInfo implementation */
85 typedef struct {
86 ISupportErrorInfo ISupportErrorInfo_iface;
87 LONG ref;
88
89 const tid_t* iids;
90 } SupportErrorInfo;
91
92 static inline SupportErrorInfo *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
93 {
94 return CONTAINING_RECORD(iface, SupportErrorInfo, ISupportErrorInfo_iface);
95 }
96
97 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **obj)
98 {
99 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
100 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
101
102 *obj = NULL;
103
104 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISupportErrorInfo)) {
105 *obj = iface;
106 ISupportErrorInfo_AddRef(iface);
107 return S_OK;
108 }
109
110 return E_NOINTERFACE;
111 }
112
113 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
114 {
115 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
116 ULONG ref = InterlockedIncrement(&This->ref);
117 TRACE("(%p)->(%d)\n", This, ref );
118 return ref;
119 }
120
121 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
122 {
123 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
124 LONG ref = InterlockedDecrement(&This->ref);
125
126 TRACE("(%p)->(%d)\n", This, ref);
127
128 if (ref == 0)
129 heap_free(This);
130
131 return ref;
132 }
133
134 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
135 {
136 SupportErrorInfo *This = impl_from_ISupportErrorInfo(iface);
137 enum tid_t const *tid;
138
139 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
140
141 tid = This->iids;
142 while (*tid != NULL_tid)
143 {
144 if (IsEqualGUID(riid, get_riid_from_tid(*tid)))
145 return S_OK;
146 tid++;
147 }
148
149 return S_FALSE;
150 }
151
152 static const struct ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
153 SupportErrorInfo_QueryInterface,
154 SupportErrorInfo_AddRef,
155 SupportErrorInfo_Release,
156 SupportErrorInfo_InterfaceSupportsErrorInfo
157 };
158
159 HRESULT node_create_supporterrorinfo(enum tid_t const *iids, void **obj)
160 {
161 SupportErrorInfo *This;
162
163 This = heap_alloc(sizeof(*This));
164 if (!This) return E_OUTOFMEMORY;
165
166 This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
167 This->ref = 1;
168 This->iids = iids;
169
170 *obj = &This->ISupportErrorInfo_iface;
171
172 return S_OK;
173 }
174
175 xmlnode *get_node_obj(IXMLDOMNode *node)
176 {
177 xmlnode *obj = NULL;
178 HRESULT hres;
179
180 hres = IXMLDOMNode_QueryInterface(node, &IID_xmlnode, (void**)&obj);
181 if (!obj) WARN("node is not our IXMLDOMNode implementation\n");
182 return SUCCEEDED(hres) ? obj : NULL;
183 }
184
185 HRESULT node_get_nodeName(xmlnode *This, BSTR *name)
186 {
187 BSTR prefix, base;
188 HRESULT hr;
189
190 if (!name)
191 return E_INVALIDARG;
192
193 hr = node_get_base_name(This, &base);
194 if (hr != S_OK) return hr;
195
196 hr = node_get_prefix(This, &prefix);
197 if (hr == S_OK)
198 {
199 static const WCHAR colW = ':';
200 WCHAR *ptr;
201
202 /* +1 for ':' */
203 ptr = *name = SysAllocStringLen(NULL, SysStringLen(base) + SysStringLen(prefix) + 1);
204 memcpy(ptr, prefix, SysStringByteLen(prefix));
205 ptr += SysStringLen(prefix);
206 memcpy(ptr++, &colW, sizeof(WCHAR));
207 memcpy(ptr, base, SysStringByteLen(base));
208
209 SysFreeString(base);
210 SysFreeString(prefix);
211 }
212 else
213 *name = base;
214
215 return S_OK;
216 }
217
218 HRESULT node_get_content(xmlnode *This, VARIANT *value)
219 {
220 xmlChar *content;
221
222 if(!value)
223 return E_INVALIDARG;
224
225 content = xmlNodeGetContent(This->node);
226 V_VT(value) = VT_BSTR;
227 V_BSTR(value) = bstr_from_xmlChar( content );
228 xmlFree(content);
229
230 TRACE("%p returned %s\n", This, debugstr_w(V_BSTR(value)));
231 return S_OK;
232 }
233
234 HRESULT node_set_content(xmlnode *This, LPCWSTR value)
235 {
236 xmlChar *str;
237
238 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
239 str = xmlchar_from_wchar(value);
240 if(!str)
241 return E_OUTOFMEMORY;
242
243 xmlNodeSetContent(This->node, str);
244 heap_free(str);
245 return S_OK;
246 }
247
248 static HRESULT node_set_content_escaped(xmlnode *This, LPCWSTR value)
249 {
250 xmlChar *str, *escaped;
251
252 TRACE("(%p)->(%s)\n", This, debugstr_w(value));
253 str = xmlchar_from_wchar(value);
254 if(!str)
255 return E_OUTOFMEMORY;
256
257 escaped = xmlEncodeSpecialChars(NULL, str);
258 if(!escaped)
259 {
260 heap_free(str);
261 return E_OUTOFMEMORY;
262 }
263
264 xmlNodeSetContent(This->node, escaped);
265
266 heap_free(str);
267 xmlFree(escaped);
268
269 return S_OK;
270 }
271
272 HRESULT node_put_value(xmlnode *This, VARIANT *value)
273 {
274 HRESULT hr;
275
276 if (V_VT(value) != VT_BSTR)
277 {
278 VARIANT string_value;
279
280 VariantInit(&string_value);
281 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
282 if(FAILED(hr)) {
283 WARN("Couldn't convert to VT_BSTR\n");
284 return hr;
285 }
286
287 hr = node_set_content(This, V_BSTR(&string_value));
288 VariantClear(&string_value);
289 }
290 else
291 hr = node_set_content(This, V_BSTR(value));
292
293 return hr;
294 }
295
296 HRESULT node_put_value_escaped(xmlnode *This, VARIANT *value)
297 {
298 HRESULT hr;
299
300 if (V_VT(value) != VT_BSTR)
301 {
302 VARIANT string_value;
303
304 VariantInit(&string_value);
305 hr = VariantChangeType(&string_value, value, 0, VT_BSTR);
306 if(FAILED(hr)) {
307 WARN("Couldn't convert to VT_BSTR\n");
308 return hr;
309 }
310
311 hr = node_set_content_escaped(This, V_BSTR(&string_value));
312 VariantClear(&string_value);
313 }
314 else
315 hr = node_set_content_escaped(This, V_BSTR(value));
316
317 return hr;
318 }
319
320 static HRESULT get_node(
321 xmlnode *This,
322 const char *name,
323 xmlNodePtr node,
324 IXMLDOMNode **out )
325 {
326 TRACE("(%p)->(%s %p %p)\n", This, name, node, out );
327
328 if ( !out )
329 return E_INVALIDARG;
330
331 /* if we don't have a doc, use our parent. */
332 if(node && !node->doc && node->parent)
333 node->doc = node->parent->doc;
334
335 *out = create_node( node );
336 if (!*out)
337 return S_FALSE;
338 return S_OK;
339 }
340
341 HRESULT node_get_parent(xmlnode *This, IXMLDOMNode **parent)
342 {
343 return get_node( This, "parent", This->node->parent, parent );
344 }
345
346 HRESULT node_get_child_nodes(xmlnode *This, IXMLDOMNodeList **ret)
347 {
348 if(!ret)
349 return E_INVALIDARG;
350
351 *ret = create_children_nodelist(This->node);
352 if(!*ret)
353 return E_OUTOFMEMORY;
354
355 return S_OK;
356 }
357
358 HRESULT node_get_first_child(xmlnode *This, IXMLDOMNode **ret)
359 {
360 return get_node(This, "firstChild", This->node->children, ret);
361 }
362
363 HRESULT node_get_last_child(xmlnode *This, IXMLDOMNode **ret)
364 {
365 return get_node(This, "lastChild", This->node->last, ret);
366 }
367
368 HRESULT node_get_previous_sibling(xmlnode *This, IXMLDOMNode **ret)
369 {
370 return get_node(This, "previous", This->node->prev, ret);
371 }
372
373 HRESULT node_get_next_sibling(xmlnode *This, IXMLDOMNode **ret)
374 {
375 return get_node(This, "next", This->node->next, ret);
376 }
377
378 static int node_get_inst_cnt(xmlNodePtr node)
379 {
380 int ret = *(LONG *)&node->_private;
381 xmlNodePtr child;
382
383 /* add attribute counts */
384 if (node->type == XML_ELEMENT_NODE)
385 {
386 xmlAttrPtr prop = node->properties;
387
388 while (prop)
389 {
390 ret += node_get_inst_cnt((xmlNodePtr)prop);
391 prop = prop->next;
392 }
393 }
394
395 /* add children counts */
396 child = node->children;
397 while (child)
398 {
399 ret += node_get_inst_cnt(child);
400 child = child->next;
401 }
402
403 return ret;
404 }
405
406 int xmlnode_get_inst_cnt(xmlnode *node)
407 {
408 return node_get_inst_cnt(node->node);
409 }
410
411 HRESULT node_insert_before(xmlnode *This, IXMLDOMNode *new_child, const VARIANT *ref_child,
412 IXMLDOMNode **ret)
413 {
414 IXMLDOMNode *before = NULL;
415 xmlnode *node_obj;
416 int refcount = 0;
417 xmlDocPtr doc;
418 HRESULT hr;
419
420 if(!new_child)
421 return E_INVALIDARG;
422
423 node_obj = get_node_obj(new_child);
424 if(!node_obj) return E_FAIL;
425
426 switch(V_VT(ref_child))
427 {
428 case VT_EMPTY:
429 case VT_NULL:
430 break;
431
432 case VT_UNKNOWN:
433 case VT_DISPATCH:
434 if (V_UNKNOWN(ref_child))
435 {
436 hr = IUnknown_QueryInterface(V_UNKNOWN(ref_child), &IID_IXMLDOMNode, (void**)&before);
437 if(FAILED(hr)) return hr;
438 }
439 break;
440
441 default:
442 FIXME("refChild var type %x\n", V_VT(ref_child));
443 return E_FAIL;
444 }
445
446 TRACE("new child %p, This->node %p\n", node_obj->node, This->node);
447
448 if(!node_obj->node->parent)
449 if(xmldoc_remove_orphan(node_obj->node->doc, node_obj->node) != S_OK)
450 WARN("%p is not an orphan of %p\n", node_obj->node, node_obj->node->doc);
451
452 refcount = xmlnode_get_inst_cnt(node_obj);
453
454 if(before)
455 {
456 xmlnode *before_node_obj = get_node_obj(before);
457 IXMLDOMNode_Release(before);
458 if(!before_node_obj) return E_FAIL;
459
460 /* unlink from current parent first */
461 if(node_obj->parent)
462 {
463 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
464 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
465 }
466
467 doc = node_obj->node->doc;
468
469 /* refs count including subtree */
470 if (doc != before_node_obj->node->doc)
471 refcount = xmlnode_get_inst_cnt(node_obj);
472
473 if (refcount) xmldoc_add_refs(before_node_obj->node->doc, refcount);
474 xmlAddPrevSibling(before_node_obj->node, node_obj->node);
475 if (refcount) xmldoc_release_refs(doc, refcount);
476 node_obj->parent = This->parent;
477 }
478 else
479 {
480 /* unlink from current parent first */
481 if(node_obj->parent)
482 {
483 hr = IXMLDOMNode_removeChild(node_obj->parent, node_obj->iface, NULL);
484 if (hr == S_OK) xmldoc_remove_orphan(node_obj->node->doc, node_obj->node);
485 }
486 doc = node_obj->node->doc;
487
488 if (doc != This->node->doc)
489 refcount = xmlnode_get_inst_cnt(node_obj);
490
491 if (refcount) xmldoc_add_refs(This->node->doc, refcount);
492 /* xmlAddChild doesn't unlink node from previous parent */
493 xmlUnlinkNode(node_obj->node);
494 xmlAddChild(This->node, node_obj->node);
495 if (refcount) xmldoc_release_refs(doc, refcount);
496 node_obj->parent = This->iface;
497 }
498
499 if(ret)
500 {
501 IXMLDOMNode_AddRef(new_child);
502 *ret = new_child;
503 }
504
505 TRACE("ret S_OK\n");
506 return S_OK;
507 }
508
509 HRESULT node_replace_child(xmlnode *This, IXMLDOMNode *newChild, IXMLDOMNode *oldChild,
510 IXMLDOMNode **ret)
511 {
512 xmlnode *old_child, *new_child;
513 xmlDocPtr leaving_doc;
514 xmlNode *my_ancestor;
515 int refcount = 0;
516
517 /* Do not believe any documentation telling that newChild == NULL
518 means removal. It does certainly *not* apply to msxml3! */
519 if(!newChild || !oldChild)
520 return E_INVALIDARG;
521
522 if(ret)
523 *ret = NULL;
524
525 old_child = get_node_obj(oldChild);
526 if(!old_child) return E_FAIL;
527
528 if(old_child->node->parent != This->node)
529 {
530 WARN("childNode %p is not a child of %p\n", oldChild, This);
531 return E_INVALIDARG;
532 }
533
534 new_child = get_node_obj(newChild);
535 if(!new_child) return E_FAIL;
536
537 my_ancestor = This->node;
538 while(my_ancestor)
539 {
540 if(my_ancestor == new_child->node)
541 {
542 WARN("tried to create loop\n");
543 return E_FAIL;
544 }
545 my_ancestor = my_ancestor->parent;
546 }
547
548 if(!new_child->node->parent)
549 if(xmldoc_remove_orphan(new_child->node->doc, new_child->node) != S_OK)
550 WARN("%p is not an orphan of %p\n", new_child->node, new_child->node->doc);
551
552 leaving_doc = new_child->node->doc;
553
554 if (leaving_doc != old_child->node->doc)
555 refcount = xmlnode_get_inst_cnt(new_child);
556
557 if (refcount) xmldoc_add_refs(old_child->node->doc, refcount);
558 xmlReplaceNode(old_child->node, new_child->node);
559 if (refcount) xmldoc_release_refs(leaving_doc, refcount);
560 new_child->parent = old_child->parent;
561 old_child->parent = NULL;
562
563 xmldoc_add_orphan(old_child->node->doc, old_child->node);
564
565 if(ret)
566 {
567 IXMLDOMNode_AddRef(oldChild);
568 *ret = oldChild;
569 }
570
571 return S_OK;
572 }
573
574 HRESULT node_remove_child(xmlnode *This, IXMLDOMNode* child, IXMLDOMNode** oldChild)
575 {
576 xmlnode *child_node;
577
578 if(!child) return E_INVALIDARG;
579
580 if(oldChild)
581 *oldChild = NULL;
582
583 child_node = get_node_obj(child);
584 if(!child_node) return E_FAIL;
585
586 if(child_node->node->parent != This->node)
587 {
588 WARN("childNode %p is not a child of %p\n", child, This);
589 return E_INVALIDARG;
590 }
591
592 xmlUnlinkNode(child_node->node);
593 child_node->parent = NULL;
594 xmldoc_add_orphan(child_node->node->doc, child_node->node);
595
596 if(oldChild)
597 {
598 IXMLDOMNode_AddRef(child);
599 *oldChild = child;
600 }
601
602 return S_OK;
603 }
604
605 HRESULT node_append_child(xmlnode *This, IXMLDOMNode *child, IXMLDOMNode **outChild)
606 {
607 DOMNodeType type;
608 VARIANT var;
609 HRESULT hr;
610
611 if (!child)
612 return E_INVALIDARG;
613
614 hr = IXMLDOMNode_get_nodeType(child, &type);
615 if(FAILED(hr) || type == NODE_ATTRIBUTE) {
616 if (outChild) *outChild = NULL;
617 return E_FAIL;
618 }
619
620 VariantInit(&var);
621 return IXMLDOMNode_insertBefore(This->iface, child, var, outChild);
622 }
623
624 HRESULT node_has_childnodes(const xmlnode *This, VARIANT_BOOL *ret)
625 {
626 if (!ret) return E_INVALIDARG;
627
628 if (!This->node->children)
629 {
630 *ret = VARIANT_FALSE;
631 return S_FALSE;
632 }
633
634 *ret = VARIANT_TRUE;
635 return S_OK;
636 }
637
638 HRESULT node_get_owner_doc(const xmlnode *This, IXMLDOMDocument **doc)
639 {
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 FIXME("libxslt headers were not found at compile time\n");
1293 return E_NOTIMPL;
1294 #endif
1295 }
1296
1297 HRESULT node_transform_node(const xmlnode *node, IXMLDOMNode *stylesheet, BSTR *p)
1298 {
1299 return node_transform_node_params(node, stylesheet, p, NULL, NULL);
1300 }
1301
1302 HRESULT node_select_nodes(const xmlnode *This, BSTR query, IXMLDOMNodeList **nodes)
1303 {
1304 xmlChar* str;
1305 HRESULT hr;
1306
1307 if (!query || !nodes) return E_INVALIDARG;
1308
1309 str = xmlchar_from_wchar(query);
1310 hr = create_selection(This->node, str, nodes);
1311 heap_free(str);
1312
1313 return hr;
1314 }
1315
1316 HRESULT node_select_singlenode(const xmlnode *This, BSTR query, IXMLDOMNode **node)
1317 {
1318 IXMLDOMNodeList *list;
1319 HRESULT hr;
1320
1321 hr = node_select_nodes(This, query, &list);
1322 if (hr == S_OK)
1323 {
1324 hr = IXMLDOMNodeList_nextNode(list, node);
1325 IXMLDOMNodeList_Release(list);
1326 }
1327 return hr;
1328 }
1329
1330 HRESULT node_get_namespaceURI(xmlnode *This, BSTR *namespaceURI)
1331 {
1332 xmlNsPtr ns = This->node->ns;
1333
1334 if(!namespaceURI)
1335 return E_INVALIDARG;
1336
1337 *namespaceURI = NULL;
1338
1339 if (ns && ns->href)
1340 *namespaceURI = bstr_from_xmlChar(ns->href);
1341
1342 TRACE("uri: %s\n", debugstr_w(*namespaceURI));
1343
1344 return *namespaceURI ? S_OK : S_FALSE;
1345 }
1346
1347 HRESULT node_get_prefix(xmlnode *This, BSTR *prefix)
1348 {
1349 xmlNsPtr ns = This->node->ns;
1350
1351 if (!prefix) return E_INVALIDARG;
1352
1353 *prefix = NULL;
1354
1355 if (ns && ns->prefix)
1356 *prefix = bstr_from_xmlChar(ns->prefix);
1357
1358 TRACE("prefix: %s\n", debugstr_w(*prefix));
1359
1360 return *prefix ? S_OK : S_FALSE;
1361 }
1362
1363 HRESULT node_get_base_name(xmlnode *This, BSTR *name)
1364 {
1365 if (!name) return E_INVALIDARG;
1366
1367 *name = bstr_from_xmlChar(This->node->name);
1368 if (!*name) return E_OUTOFMEMORY;
1369
1370 TRACE("returning %s\n", debugstr_w(*name));
1371
1372 return S_OK;
1373 }
1374
1375 /* _private field holds a number of COM instances spawned from this libxml2 node */
1376 static void xmlnode_add_ref(xmlNodePtr node)
1377 {
1378 if (node->type == XML_DOCUMENT_NODE) return;
1379 InterlockedIncrement((LONG*)&node->_private);
1380 }
1381
1382 static void xmlnode_release(xmlNodePtr node)
1383 {
1384 if (node->type == XML_DOCUMENT_NODE) return;
1385 InterlockedDecrement((LONG*)&node->_private);
1386 }
1387
1388 void destroy_xmlnode(xmlnode *This)
1389 {
1390 if(This->node)
1391 {
1392 xmlnode_release(This->node);
1393 xmldoc_release(This->node->doc);
1394 }
1395 }
1396
1397 void init_xmlnode(xmlnode *This, xmlNodePtr node, IXMLDOMNode *node_iface, dispex_static_data_t *dispex_data)
1398 {
1399 if(node)
1400 {
1401 xmlnode_add_ref(node);
1402 xmldoc_add_ref(node->doc);
1403 }
1404
1405 This->node = node;
1406 This->iface = node_iface;
1407 This->parent = NULL;
1408
1409 init_dispex(&This->dispex, (IUnknown*)This->iface, dispex_data);
1410 }
1411
1412 typedef struct {
1413 xmlnode node;
1414 IXMLDOMNode IXMLDOMNode_iface;
1415 LONG ref;
1416 } unknode;
1417
1418 static inline unknode *unknode_from_IXMLDOMNode(IXMLDOMNode *iface)
1419 {
1420 return CONTAINING_RECORD(iface, unknode, IXMLDOMNode_iface);
1421 }
1422
1423 static HRESULT WINAPI unknode_QueryInterface(
1424 IXMLDOMNode *iface,
1425 REFIID riid,
1426 void** ppvObject )
1427 {
1428 unknode *This = unknode_from_IXMLDOMNode( iface );
1429
1430 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1431
1432 if (IsEqualGUID(riid, &IID_IUnknown)) {
1433 *ppvObject = iface;
1434 }else if (IsEqualGUID( riid, &IID_IDispatch) ||
1435 IsEqualGUID( riid, &IID_IXMLDOMNode)) {
1436 *ppvObject = &This->IXMLDOMNode_iface;
1437 }else if(node_query_interface(&This->node, riid, ppvObject)) {
1438 return *ppvObject ? S_OK : E_NOINTERFACE;
1439 }else {
1440 FIXME("interface %s not implemented\n", debugstr_guid(riid));
1441 *ppvObject = NULL;
1442 return E_NOINTERFACE;
1443 }
1444
1445 IUnknown_AddRef((IUnknown*)*ppvObject);
1446 return S_OK;
1447 }
1448
1449 static ULONG WINAPI unknode_AddRef(
1450 IXMLDOMNode *iface )
1451 {
1452 unknode *This = unknode_from_IXMLDOMNode( iface );
1453
1454 return InterlockedIncrement(&This->ref);
1455 }
1456
1457 static ULONG WINAPI unknode_Release(
1458 IXMLDOMNode *iface )
1459 {
1460 unknode *This = unknode_from_IXMLDOMNode( iface );
1461 LONG ref;
1462
1463 ref = InterlockedDecrement( &This->ref );
1464 if(!ref) {
1465 destroy_xmlnode(&This->node);
1466 heap_free(This);
1467 }
1468
1469 return ref;
1470 }
1471
1472 static HRESULT WINAPI unknode_GetTypeInfoCount(
1473 IXMLDOMNode *iface,
1474 UINT* pctinfo )
1475 {
1476 unknode *This = unknode_from_IXMLDOMNode( iface );
1477
1478 TRACE("(%p)->(%p)\n", This, pctinfo);
1479
1480 *pctinfo = 1;
1481
1482 return S_OK;
1483 }
1484
1485 static HRESULT WINAPI unknode_GetTypeInfo(
1486 IXMLDOMNode *iface,
1487 UINT iTInfo,
1488 LCID lcid,
1489 ITypeInfo** ppTInfo )
1490 {
1491 unknode *This = unknode_from_IXMLDOMNode( iface );
1492 HRESULT hr;
1493
1494 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1495
1496 hr = get_typeinfo(IXMLDOMNode_tid, ppTInfo);
1497
1498 return hr;
1499 }
1500
1501 static HRESULT WINAPI unknode_GetIDsOfNames(
1502 IXMLDOMNode *iface,
1503 REFIID riid,
1504 LPOLESTR* rgszNames,
1505 UINT cNames,
1506 LCID lcid,
1507 DISPID* rgDispId )
1508 {
1509 unknode *This = unknode_from_IXMLDOMNode( iface );
1510
1511 ITypeInfo *typeinfo;
1512 HRESULT hr;
1513
1514 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1515 lcid, rgDispId);
1516
1517 if(!rgszNames || cNames == 0 || !rgDispId)
1518 return E_INVALIDARG;
1519
1520 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1521 if(SUCCEEDED(hr))
1522 {
1523 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1524 ITypeInfo_Release(typeinfo);
1525 }
1526
1527 return hr;
1528 }
1529
1530 static HRESULT WINAPI unknode_Invoke(
1531 IXMLDOMNode *iface,
1532 DISPID dispIdMember,
1533 REFIID riid,
1534 LCID lcid,
1535 WORD wFlags,
1536 DISPPARAMS* pDispParams,
1537 VARIANT* pVarResult,
1538 EXCEPINFO* pExcepInfo,
1539 UINT* puArgErr )
1540 {
1541 unknode *This = unknode_from_IXMLDOMNode( iface );
1542 ITypeInfo *typeinfo;
1543 HRESULT hr;
1544
1545 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1546 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1547
1548 hr = get_typeinfo(IXMLDOMNode_tid, &typeinfo);
1549 if(SUCCEEDED(hr))
1550 {
1551 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLDOMNode_iface, dispIdMember, wFlags, pDispParams,
1552 pVarResult, pExcepInfo, puArgErr);
1553 ITypeInfo_Release(typeinfo);
1554 }
1555
1556 return hr;
1557 }
1558
1559 static HRESULT WINAPI unknode_get_nodeName(
1560 IXMLDOMNode *iface,
1561 BSTR* p )
1562 {
1563 unknode *This = unknode_from_IXMLDOMNode( iface );
1564
1565 FIXME("(%p)->(%p)\n", This, p);
1566
1567 return node_get_nodeName(&This->node, p);
1568 }
1569
1570 static HRESULT WINAPI unknode_get_nodeValue(
1571 IXMLDOMNode *iface,
1572 VARIANT* value)
1573 {
1574 unknode *This = unknode_from_IXMLDOMNode( iface );
1575
1576 FIXME("(%p)->(%p)\n", This, value);
1577
1578 if(!value)
1579 return E_INVALIDARG;
1580
1581 V_VT(value) = VT_NULL;
1582 return S_FALSE;
1583 }
1584
1585 static HRESULT WINAPI unknode_put_nodeValue(
1586 IXMLDOMNode *iface,
1587 VARIANT value)
1588 {
1589 unknode *This = unknode_from_IXMLDOMNode( iface );
1590 FIXME("(%p)->(v%d)\n", This, V_VT(&value));
1591 return E_FAIL;
1592 }
1593
1594 static HRESULT WINAPI unknode_get_nodeType(
1595 IXMLDOMNode *iface,
1596 DOMNodeType* domNodeType )
1597 {
1598 unknode *This = unknode_from_IXMLDOMNode( iface );
1599
1600 FIXME("(%p)->(%p)\n", This, domNodeType);
1601
1602 *domNodeType = This->node.node->type;
1603 return S_OK;
1604 }
1605
1606 static HRESULT WINAPI unknode_get_parentNode(
1607 IXMLDOMNode *iface,
1608 IXMLDOMNode** parent )
1609 {
1610 unknode *This = unknode_from_IXMLDOMNode( iface );
1611 FIXME("(%p)->(%p)\n", This, parent);
1612 if (!parent) return E_INVALIDARG;
1613 *parent = NULL;
1614 return S_FALSE;
1615 }
1616
1617 static HRESULT WINAPI unknode_get_childNodes(
1618 IXMLDOMNode *iface,
1619 IXMLDOMNodeList** outList)
1620 {
1621 unknode *This = unknode_from_IXMLDOMNode( iface );
1622
1623 TRACE("(%p)->(%p)\n", This, outList);
1624
1625 return node_get_child_nodes(&This->node, outList);
1626 }
1627
1628 static HRESULT WINAPI unknode_get_firstChild(
1629 IXMLDOMNode *iface,
1630 IXMLDOMNode** domNode)
1631 {
1632 unknode *This = unknode_from_IXMLDOMNode( iface );
1633
1634 TRACE("(%p)->(%p)\n", This, domNode);
1635
1636 return node_get_first_child(&This->node, domNode);
1637 }
1638
1639 static HRESULT WINAPI unknode_get_lastChild(
1640 IXMLDOMNode *iface,
1641 IXMLDOMNode** domNode)
1642 {
1643 unknode *This = unknode_from_IXMLDOMNode( iface );
1644
1645 TRACE("(%p)->(%p)\n", This, domNode);
1646
1647 return node_get_last_child(&This->node, domNode);
1648 }
1649
1650 static HRESULT WINAPI unknode_get_previousSibling(
1651 IXMLDOMNode *iface,
1652 IXMLDOMNode** domNode)
1653 {
1654 unknode *This = unknode_from_IXMLDOMNode( iface );
1655
1656 TRACE("(%p)->(%p)\n", This, domNode);
1657
1658 return node_get_previous_sibling(&This->node, domNode);
1659 }
1660
1661 static HRESULT WINAPI unknode_get_nextSibling(
1662 IXMLDOMNode *iface,
1663 IXMLDOMNode** domNode)
1664 {
1665 unknode *This = unknode_from_IXMLDOMNode( iface );
1666
1667 TRACE("(%p)->(%p)\n", This, domNode);
1668
1669 return node_get_next_sibling(&This->node, domNode);
1670 }
1671
1672 static HRESULT WINAPI unknode_get_attributes(
1673 IXMLDOMNode *iface,
1674 IXMLDOMNamedNodeMap** attributeMap)
1675 {
1676 unknode *This = unknode_from_IXMLDOMNode( iface );
1677
1678 FIXME("(%p)->(%p)\n", This, attributeMap);
1679
1680 return return_null_ptr((void**)attributeMap);
1681 }
1682
1683 static HRESULT WINAPI unknode_insertBefore(
1684 IXMLDOMNode *iface,
1685 IXMLDOMNode* newNode, VARIANT refChild,
1686 IXMLDOMNode** outOldNode)
1687 {
1688 unknode *This = unknode_from_IXMLDOMNode( iface );
1689
1690 FIXME("(%p)->(%p x%d %p)\n", This, newNode, V_VT(&refChild), outOldNode);
1691
1692 return node_insert_before(&This->node, newNode, &refChild, outOldNode);
1693 }
1694
1695 static HRESULT WINAPI unknode_replaceChild(
1696 IXMLDOMNode *iface,
1697 IXMLDOMNode* newNode,
1698 IXMLDOMNode* oldNode,
1699 IXMLDOMNode** outOldNode)
1700 {
1701 unknode *This = unknode_from_IXMLDOMNode( iface );
1702
1703 FIXME("(%p)->(%p %p %p)\n", This, newNode, oldNode, outOldNode);
1704
1705 return node_replace_child(&This->node, newNode, oldNode, outOldNode);
1706 }
1707
1708 static HRESULT WINAPI unknode_removeChild(
1709 IXMLDOMNode *iface,
1710 IXMLDOMNode* domNode, IXMLDOMNode** oldNode)
1711 {
1712 unknode *This = unknode_from_IXMLDOMNode( iface );
1713 return node_remove_child(&This->node, domNode, oldNode);
1714 }
1715
1716 static HRESULT WINAPI unknode_appendChild(
1717 IXMLDOMNode *iface,
1718 IXMLDOMNode* newNode, IXMLDOMNode** outNewNode)
1719 {
1720 unknode *This = unknode_from_IXMLDOMNode( iface );
1721 return node_append_child(&This->node, newNode, outNewNode);
1722 }
1723
1724 static HRESULT WINAPI unknode_hasChildNodes(
1725 IXMLDOMNode *iface,
1726 VARIANT_BOOL* pbool)
1727 {
1728 unknode *This = unknode_from_IXMLDOMNode( iface );
1729 return node_has_childnodes(&This->node, pbool);
1730 }
1731
1732 static HRESULT WINAPI unknode_get_ownerDocument(
1733 IXMLDOMNode *iface,
1734 IXMLDOMDocument** domDocument)
1735 {
1736 unknode *This = unknode_from_IXMLDOMNode( iface );
1737 return node_get_owner_doc(&This->node, domDocument);
1738 }
1739
1740 static HRESULT WINAPI unknode_cloneNode(
1741 IXMLDOMNode *iface,
1742 VARIANT_BOOL pbool, IXMLDOMNode** outNode)
1743 {
1744 unknode *This = unknode_from_IXMLDOMNode( iface );
1745 return node_clone(&This->node, pbool, outNode );
1746 }
1747
1748 static HRESULT WINAPI unknode_get_nodeTypeString(
1749 IXMLDOMNode *iface,
1750 BSTR* p)
1751 {
1752 unknode *This = unknode_from_IXMLDOMNode( iface );
1753
1754 FIXME("(%p)->(%p)\n", This, p);
1755
1756 return node_get_nodeName(&This->node, p);
1757 }
1758
1759 static HRESULT WINAPI unknode_get_text(
1760 IXMLDOMNode *iface,
1761 BSTR* p)
1762 {
1763 unknode *This = unknode_from_IXMLDOMNode( iface );
1764 return node_get_text(&This->node, p);
1765 }
1766
1767 static HRESULT WINAPI unknode_put_text(
1768 IXMLDOMNode *iface,
1769 BSTR p)
1770 {
1771 unknode *This = unknode_from_IXMLDOMNode( iface );
1772 return node_put_text(&This->node, p);
1773 }
1774
1775 static HRESULT WINAPI unknode_get_specified(
1776 IXMLDOMNode *iface,
1777 VARIANT_BOOL* isSpecified)
1778 {
1779 unknode *This = unknode_from_IXMLDOMNode( iface );
1780 FIXME("(%p)->(%p) stub!\n", This, isSpecified);
1781 *isSpecified = VARIANT_TRUE;
1782 return S_OK;
1783 }
1784
1785 static HRESULT WINAPI unknode_get_definition(
1786 IXMLDOMNode *iface,
1787 IXMLDOMNode** definitionNode)
1788 {
1789 unknode *This = unknode_from_IXMLDOMNode( iface );
1790 FIXME("(%p)->(%p)\n", This, definitionNode);
1791 return E_NOTIMPL;
1792 }
1793
1794 static HRESULT WINAPI unknode_get_nodeTypedValue(
1795 IXMLDOMNode *iface,
1796 VARIANT* var1)
1797 {
1798 unknode *This = unknode_from_IXMLDOMNode( iface );
1799 FIXME("(%p)->(%p)\n", This, var1);
1800 return return_null_var(var1);
1801 }
1802
1803 static HRESULT WINAPI unknode_put_nodeTypedValue(
1804 IXMLDOMNode *iface,
1805 VARIANT typedValue)
1806 {
1807 unknode *This = unknode_from_IXMLDOMNode( iface );
1808 FIXME("(%p)->(%s)\n", This, debugstr_variant(&typedValue));
1809 return E_NOTIMPL;
1810 }
1811
1812 static HRESULT WINAPI unknode_get_dataType(
1813 IXMLDOMNode *iface,
1814 VARIANT* var1)
1815 {
1816 unknode *This = unknode_from_IXMLDOMNode( iface );
1817 TRACE("(%p)->(%p)\n", This, var1);
1818 return return_null_var(var1);
1819 }
1820
1821 static HRESULT WINAPI unknode_put_dataType(
1822 IXMLDOMNode *iface,
1823 BSTR p)
1824 {
1825 unknode *This = unknode_from_IXMLDOMNode( iface );
1826
1827 FIXME("(%p)->(%s)\n", This, debugstr_w(p));
1828
1829 if(!p)
1830 return E_INVALIDARG;
1831
1832 return E_FAIL;
1833 }
1834
1835 static HRESULT WINAPI unknode_get_xml(
1836 IXMLDOMNode *iface,
1837 BSTR* p)
1838 {
1839 unknode *This = unknode_from_IXMLDOMNode( iface );
1840
1841 FIXME("(%p)->(%p)\n", This, p);
1842
1843 return node_get_xml(&This->node, FALSE, p);
1844 }
1845
1846 static HRESULT WINAPI unknode_transformNode(
1847 IXMLDOMNode *iface,
1848 IXMLDOMNode* domNode, BSTR* p)
1849 {
1850 unknode *This = unknode_from_IXMLDOMNode( iface );
1851 return node_transform_node(&This->node, domNode, p);
1852 }
1853
1854 static HRESULT WINAPI unknode_selectNodes(
1855 IXMLDOMNode *iface,
1856 BSTR p, IXMLDOMNodeList** outList)
1857 {
1858 unknode *This = unknode_from_IXMLDOMNode( iface );
1859 return node_select_nodes(&This->node, p, outList);
1860 }
1861
1862 static HRESULT WINAPI unknode_selectSingleNode(
1863 IXMLDOMNode *iface,
1864 BSTR p, IXMLDOMNode** outNode)
1865 {
1866 unknode *This = unknode_from_IXMLDOMNode( iface );
1867 return node_select_singlenode(&This->node, p, outNode);
1868 }
1869
1870 static HRESULT WINAPI unknode_get_parsed(
1871 IXMLDOMNode *iface,
1872 VARIANT_BOOL* isParsed)
1873 {
1874 unknode *This = unknode_from_IXMLDOMNode( iface );
1875 FIXME("(%p)->(%p) stub!\n", This, isParsed);
1876 *isParsed = VARIANT_TRUE;
1877 return S_OK;
1878 }
1879
1880 static HRESULT WINAPI unknode_get_namespaceURI(
1881 IXMLDOMNode *iface,
1882 BSTR* p)
1883 {
1884 unknode *This = unknode_from_IXMLDOMNode( iface );
1885 TRACE("(%p)->(%p)\n", This, p);
1886 return node_get_namespaceURI(&This->node, p);
1887 }
1888
1889 static HRESULT WINAPI unknode_get_prefix(
1890 IXMLDOMNode *iface,
1891 BSTR* p)
1892 {
1893 unknode *This = unknode_from_IXMLDOMNode( iface );
1894 return node_get_prefix(&This->node, p);
1895 }
1896
1897 static HRESULT WINAPI unknode_get_baseName(
1898 IXMLDOMNode *iface,
1899 BSTR* p)
1900 {
1901 unknode *This = unknode_from_IXMLDOMNode( iface );
1902 return node_get_base_name(&This->node, p);
1903 }
1904
1905 static HRESULT WINAPI unknode_transformNodeToObject(
1906 IXMLDOMNode *iface,
1907 IXMLDOMNode* domNode, VARIANT var1)
1908 {
1909 unknode *This = unknode_from_IXMLDOMNode( iface );
1910 FIXME("(%p)->(%p %s)\n", This, domNode, debugstr_variant(&var1));
1911 return E_NOTIMPL;
1912 }
1913
1914 static const struct IXMLDOMNodeVtbl unknode_vtbl =
1915 {
1916 unknode_QueryInterface,
1917 unknode_AddRef,
1918 unknode_Release,
1919 unknode_GetTypeInfoCount,
1920 unknode_GetTypeInfo,
1921 unknode_GetIDsOfNames,
1922 unknode_Invoke,
1923 unknode_get_nodeName,
1924 unknode_get_nodeValue,
1925 unknode_put_nodeValue,
1926 unknode_get_nodeType,
1927 unknode_get_parentNode,
1928 unknode_get_childNodes,
1929 unknode_get_firstChild,
1930 unknode_get_lastChild,
1931 unknode_get_previousSibling,
1932 unknode_get_nextSibling,
1933 unknode_get_attributes,
1934 unknode_insertBefore,
1935 unknode_replaceChild,
1936 unknode_removeChild,
1937 unknode_appendChild,
1938 unknode_hasChildNodes,
1939 unknode_get_ownerDocument,
1940 unknode_cloneNode,
1941 unknode_get_nodeTypeString,
1942 unknode_get_text,
1943 unknode_put_text,
1944 unknode_get_specified,
1945 unknode_get_definition,
1946 unknode_get_nodeTypedValue,
1947 unknode_put_nodeTypedValue,
1948 unknode_get_dataType,
1949 unknode_put_dataType,
1950 unknode_get_xml,
1951 unknode_transformNode,
1952 unknode_selectNodes,
1953 unknode_selectSingleNode,
1954 unknode_get_parsed,
1955 unknode_get_namespaceURI,
1956 unknode_get_prefix,
1957 unknode_get_baseName,
1958 unknode_transformNodeToObject
1959 };
1960
1961 IXMLDOMNode *create_node( xmlNodePtr node )
1962 {
1963 IUnknown *pUnk;
1964 IXMLDOMNode *ret;
1965 HRESULT hr;
1966
1967 if ( !node )
1968 return NULL;
1969
1970 TRACE("type %d\n", node->type);
1971 switch(node->type)
1972 {
1973 case XML_ELEMENT_NODE:
1974 pUnk = create_element( node );
1975 break;
1976 case XML_ATTRIBUTE_NODE:
1977 pUnk = create_attribute( node );
1978 break;
1979 case XML_TEXT_NODE:
1980 pUnk = create_text( node );
1981 break;
1982 case XML_CDATA_SECTION_NODE:
1983 pUnk = create_cdata( node );
1984 break;
1985 case XML_ENTITY_REF_NODE:
1986 pUnk = create_doc_entity_ref( node );
1987 break;
1988 case XML_PI_NODE:
1989 pUnk = create_pi( node );
1990 break;
1991 case XML_COMMENT_NODE:
1992 pUnk = create_comment( node );
1993 break;
1994 case XML_DOCUMENT_NODE:
1995 pUnk = create_domdoc( node );
1996 break;
1997 case XML_DOCUMENT_FRAG_NODE:
1998 pUnk = create_doc_fragment( node );
1999 break;
2000 case XML_DTD_NODE:
2001 pUnk = create_doc_type( node );
2002 break;
2003 default: {
2004 unknode *new_node;
2005
2006 FIXME("only creating basic node for type %d\n", node->type);
2007
2008 new_node = heap_alloc(sizeof(unknode));
2009 if(!new_node)
2010 return NULL;
2011
2012 new_node->IXMLDOMNode_iface.lpVtbl = &unknode_vtbl;
2013 new_node->ref = 1;
2014 init_xmlnode(&new_node->node, node, &new_node->IXMLDOMNode_iface, NULL);
2015 pUnk = (IUnknown*)&new_node->IXMLDOMNode_iface;
2016 }
2017 }
2018
2019 hr = IUnknown_QueryInterface(pUnk, &IID_IXMLDOMNode, (LPVOID*)&ret);
2020 IUnknown_Release(pUnk);
2021 if(FAILED(hr)) return NULL;
2022 return ret;
2023 }
2024 #endif