2 * DOM text node implementation
4 * Copyright 2006 Huw Davies
5 * Copyright 2007-2008 Alistair Leslie-Hughes
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <libxml/parser.h>
26 #include <libxml/parserInternals.h>
27 #include <libxml/xmlerror.h>
35 #include "msxml_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
41 typedef struct _domtext
44 IXMLDOMText IXMLDOMText_iface
;
48 static inline domtext
*impl_from_IXMLDOMText( IXMLDOMText
*iface
)
50 return CONTAINING_RECORD(iface
, domtext
, IXMLDOMText_iface
);
53 static void domtext_reset_noenc(domtext
*This
)
55 This
->node
.node
->name
= NULL
;
58 static HRESULT WINAPI
domtext_QueryInterface(
63 domtext
*This
= impl_from_IXMLDOMText( iface
);
64 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
66 if ( IsEqualGUID( riid
, &IID_IXMLDOMText
) ||
67 IsEqualGUID( riid
, &IID_IXMLDOMCharacterData
) ||
68 IsEqualGUID( riid
, &IID_IXMLDOMNode
) ||
69 IsEqualGUID( riid
, &IID_IDispatch
) ||
70 IsEqualGUID( riid
, &IID_IUnknown
) )
74 else if(node_query_interface(&This
->node
, riid
, ppvObject
))
76 return *ppvObject
? S_OK
: E_NOINTERFACE
;
80 TRACE("Unsupported interface %s\n", debugstr_guid(riid
));
85 IXMLDOMText_AddRef(iface
);
89 static ULONG WINAPI
domtext_AddRef(
92 domtext
*This
= impl_from_IXMLDOMText( iface
);
93 ULONG ref
= InterlockedIncrement( &This
->ref
);
94 TRACE("%p, refcount %lu.\n", iface
, ref
);
98 static ULONG WINAPI
domtext_Release(
101 domtext
*This
= impl_from_IXMLDOMText( iface
);
102 ULONG ref
= InterlockedDecrement( &This
->ref
);
104 TRACE("%p, refcount %lu.\n", iface
, ref
);
107 destroy_xmlnode(&This
->node
);
114 static HRESULT WINAPI
domtext_GetTypeInfoCount(
118 domtext
*This
= impl_from_IXMLDOMText( iface
);
119 return IDispatchEx_GetTypeInfoCount(&This
->node
.dispex
.IDispatchEx_iface
, pctinfo
);
122 static HRESULT WINAPI
domtext_GetTypeInfo(
124 UINT iTInfo
, LCID lcid
,
125 ITypeInfo
** ppTInfo
)
127 domtext
*This
= impl_from_IXMLDOMText( iface
);
128 return IDispatchEx_GetTypeInfo(&This
->node
.dispex
.IDispatchEx_iface
,
129 iTInfo
, lcid
, ppTInfo
);
132 static HRESULT WINAPI
domtext_GetIDsOfNames(
134 REFIID riid
, LPOLESTR
* rgszNames
,
135 UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
137 domtext
*This
= impl_from_IXMLDOMText( iface
);
138 return IDispatchEx_GetIDsOfNames(&This
->node
.dispex
.IDispatchEx_iface
,
139 riid
, rgszNames
, cNames
, lcid
, rgDispId
);
142 static HRESULT WINAPI
domtext_Invoke(
144 DISPID dispIdMember
, REFIID riid
, LCID lcid
,
145 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
146 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
148 domtext
*This
= impl_from_IXMLDOMText( iface
);
149 return IDispatchEx_Invoke(&This
->node
.dispex
.IDispatchEx_iface
,
150 dispIdMember
, riid
, lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
153 static HRESULT WINAPI
domtext_get_nodeName(
157 domtext
*This
= impl_from_IXMLDOMText( iface
);
159 static const WCHAR textW
[] = {'#','t','e','x','t',0};
161 TRACE("(%p)->(%p)\n", This
, p
);
163 return return_bstr(textW
, p
);
166 static HRESULT WINAPI
domtext_get_nodeValue(
170 domtext
*This
= impl_from_IXMLDOMText( iface
);
172 TRACE("(%p)->(%p)\n", This
, value
);
174 return node_get_content(&This
->node
, value
);
177 static HRESULT WINAPI
domtext_put_nodeValue(
181 domtext
*This
= impl_from_IXMLDOMText( iface
);
183 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&value
));
185 domtext_reset_noenc(This
);
186 return node_put_value(&This
->node
, &value
);
189 static HRESULT WINAPI
domtext_get_nodeType(
191 DOMNodeType
* domNodeType
)
193 domtext
*This
= impl_from_IXMLDOMText( iface
);
195 TRACE("(%p)->(%p)\n", This
, domNodeType
);
197 *domNodeType
= NODE_TEXT
;
201 static HRESULT WINAPI
domtext_get_parentNode(
203 IXMLDOMNode
** parent
)
205 domtext
*This
= impl_from_IXMLDOMText( iface
);
207 TRACE("(%p)->(%p)\n", This
, parent
);
209 return node_get_parent(&This
->node
, parent
);
212 static HRESULT WINAPI
domtext_get_childNodes(
214 IXMLDOMNodeList
** outList
)
216 domtext
*This
= impl_from_IXMLDOMText( iface
);
218 TRACE("(%p)->(%p)\n", This
, outList
);
220 return node_get_child_nodes(&This
->node
, outList
);
223 static HRESULT WINAPI
domtext_get_firstChild(
225 IXMLDOMNode
** domNode
)
227 domtext
*This
= impl_from_IXMLDOMText( iface
);
229 TRACE("(%p)->(%p)\n", This
, domNode
);
231 return return_null_node(domNode
);
234 static HRESULT WINAPI
domtext_get_lastChild(
236 IXMLDOMNode
** domNode
)
238 domtext
*This
= impl_from_IXMLDOMText( iface
);
240 TRACE("(%p)->(%p)\n", This
, domNode
);
242 return return_null_node(domNode
);
245 static HRESULT WINAPI
domtext_get_previousSibling(
247 IXMLDOMNode
** domNode
)
249 domtext
*This
= impl_from_IXMLDOMText( iface
);
251 TRACE("(%p)->(%p)\n", This
, domNode
);
253 return node_get_previous_sibling(&This
->node
, domNode
);
256 static HRESULT WINAPI
domtext_get_nextSibling(
258 IXMLDOMNode
** domNode
)
260 domtext
*This
= impl_from_IXMLDOMText( iface
);
262 TRACE("(%p)->(%p)\n", This
, domNode
);
264 return node_get_next_sibling(&This
->node
, domNode
);
267 static HRESULT WINAPI
domtext_get_attributes(
269 IXMLDOMNamedNodeMap
** attributeMap
)
271 domtext
*This
= impl_from_IXMLDOMText( iface
);
273 TRACE("(%p)->(%p)\n", This
, attributeMap
);
275 return return_null_ptr((void**)attributeMap
);
278 static HRESULT WINAPI
domtext_insertBefore(
280 IXMLDOMNode
* newNode
, VARIANT refChild
,
281 IXMLDOMNode
** outOldNode
)
283 domtext
*This
= impl_from_IXMLDOMText( iface
);
285 FIXME("(%p)->(%p %s %p) needs test\n", This
, newNode
, debugstr_variant(&refChild
), outOldNode
);
287 return node_insert_before(&This
->node
, newNode
, &refChild
, outOldNode
);
290 static HRESULT WINAPI
domtext_replaceChild(
292 IXMLDOMNode
* newNode
,
293 IXMLDOMNode
* oldNode
,
294 IXMLDOMNode
** outOldNode
)
296 domtext
*This
= impl_from_IXMLDOMText( iface
);
298 FIXME("(%p)->(%p %p %p) needs test\n", This
, newNode
, oldNode
, outOldNode
);
300 return node_replace_child(&This
->node
, newNode
, oldNode
, outOldNode
);
303 static HRESULT WINAPI
domtext_removeChild(
305 IXMLDOMNode
*child
, IXMLDOMNode
**oldChild
)
307 domtext
*This
= impl_from_IXMLDOMText( iface
);
308 TRACE("(%p)->(%p %p)\n", This
, child
, oldChild
);
309 return node_remove_child(&This
->node
, child
, oldChild
);
312 static HRESULT WINAPI
domtext_appendChild(
314 IXMLDOMNode
*child
, IXMLDOMNode
**outChild
)
316 domtext
*This
= impl_from_IXMLDOMText( iface
);
317 TRACE("(%p)->(%p %p)\n", This
, child
, outChild
);
318 return node_append_child(&This
->node
, child
, outChild
);
321 static HRESULT WINAPI
domtext_hasChildNodes(
325 domtext
*This
= impl_from_IXMLDOMText( iface
);
326 TRACE("(%p)->(%p)\n", This
, ret
);
327 return return_var_false(ret
);
330 static HRESULT WINAPI
domtext_get_ownerDocument(
332 IXMLDOMDocument
**doc
)
334 domtext
*This
= impl_from_IXMLDOMText( iface
);
335 TRACE("(%p)->(%p)\n", This
, doc
);
336 return node_get_owner_doc(&This
->node
, doc
);
339 static HRESULT WINAPI
domtext_cloneNode(
341 VARIANT_BOOL deep
, IXMLDOMNode
** outNode
)
343 domtext
*This
= impl_from_IXMLDOMText( iface
);
344 TRACE("(%p)->(%d %p)\n", This
, deep
, outNode
);
345 return node_clone( &This
->node
, deep
, outNode
);
348 static HRESULT WINAPI
domtext_get_nodeTypeString(
352 domtext
*This
= impl_from_IXMLDOMText( iface
);
353 static const WCHAR textW
[] = {'t','e','x','t',0};
355 TRACE("(%p)->(%p)\n", This
, p
);
357 return return_bstr(textW
, p
);
360 static HRESULT WINAPI
domtext_get_text(
364 domtext
*This
= impl_from_IXMLDOMText( iface
);
365 TRACE("(%p)->(%p)\n", This
, p
);
366 return node_get_text(&This
->node
, p
);
369 static HRESULT WINAPI
domtext_put_text(
373 domtext
*This
= impl_from_IXMLDOMText( iface
);
374 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
375 domtext_reset_noenc(This
);
376 return node_put_text( &This
->node
, p
);
379 static HRESULT WINAPI
domtext_get_specified(
381 VARIANT_BOOL
* isSpecified
)
383 domtext
*This
= impl_from_IXMLDOMText( iface
);
384 FIXME("(%p)->(%p) stub!\n", This
, isSpecified
);
385 *isSpecified
= VARIANT_TRUE
;
389 static HRESULT WINAPI
domtext_get_definition(
391 IXMLDOMNode
** definitionNode
)
393 domtext
*This
= impl_from_IXMLDOMText( iface
);
394 FIXME("(%p)->(%p)\n", This
, definitionNode
);
398 static HRESULT WINAPI
domtext_get_nodeTypedValue(
402 domtext
*This
= impl_from_IXMLDOMText( iface
);
403 IXMLDOMNode
* parent
= NULL
;
406 TRACE("(%p)->(%p)\n", This
, var1
);
411 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
415 hr
= IXMLDOMNode_get_nodeTypedValue(parent
, var1
);
416 IXMLDOMNode_Release(parent
);
420 V_VT(var1
) = VT_NULL
;
428 static HRESULT WINAPI
domtext_put_nodeTypedValue(
432 domtext
*This
= impl_from_IXMLDOMText( iface
);
433 IXMLDOMNode
* parent
= NULL
;
436 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&value
));
438 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
442 hr
= IXMLDOMNode_put_nodeTypedValue(parent
, value
);
443 IXMLDOMNode_Release(parent
);
453 static HRESULT WINAPI
domtext_get_dataType(
457 domtext
*This
= impl_from_IXMLDOMText( iface
);
458 IXMLDOMNode
* parent
= NULL
;
461 TRACE("(%p)->(%p)\n", This
, dtName
);
466 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
470 hr
= IXMLDOMNode_get_dataType(parent
, dtName
);
471 IXMLDOMNode_Release(parent
);
475 V_VT(dtName
) = VT_NULL
;
476 V_BSTR(dtName
) = NULL
;
483 static HRESULT WINAPI
domtext_put_dataType(
487 domtext
*This
= impl_from_IXMLDOMText( iface
);
488 IXMLDOMNode
* parent
= NULL
;
491 TRACE("(%p)->(%p)\n", This
, dtName
);
496 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
500 hr
= IXMLDOMNode_put_dataType(parent
, dtName
);
501 IXMLDOMNode_Release(parent
);
511 static HRESULT WINAPI
domtext_get_xml(
515 domtext
*This
= impl_from_IXMLDOMText( iface
);
517 TRACE("(%p)->(%p)\n", This
, p
);
519 return node_get_xml(&This
->node
, TRUE
, p
);
522 static HRESULT WINAPI
domtext_transformNode(
524 IXMLDOMNode
*node
, BSTR
*p
)
526 domtext
*This
= impl_from_IXMLDOMText( iface
);
527 TRACE("(%p)->(%p %p)\n", This
, node
, p
);
528 return node_transform_node(&This
->node
, node
, p
);
531 static HRESULT WINAPI
domtext_selectNodes(
533 BSTR p
, IXMLDOMNodeList
** outList
)
535 domtext
*This
= impl_from_IXMLDOMText( iface
);
536 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), outList
);
537 return node_select_nodes(&This
->node
, p
, outList
);
540 static HRESULT WINAPI
domtext_selectSingleNode(
542 BSTR p
, IXMLDOMNode
** outNode
)
544 domtext
*This
= impl_from_IXMLDOMText( iface
);
545 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), outNode
);
546 return node_select_singlenode(&This
->node
, p
, outNode
);
549 static HRESULT WINAPI
domtext_get_parsed(
551 VARIANT_BOOL
* isParsed
)
553 domtext
*This
= impl_from_IXMLDOMText( iface
);
554 FIXME("(%p)->(%p) stub!\n", This
, isParsed
);
555 *isParsed
= VARIANT_TRUE
;
559 static HRESULT WINAPI
domtext_get_namespaceURI(
563 domtext
*This
= impl_from_IXMLDOMText( iface
);
564 TRACE("(%p)->(%p)\n", This
, p
);
565 return node_get_namespaceURI(&This
->node
, p
);
568 static HRESULT WINAPI
domtext_get_prefix(
572 domtext
*This
= impl_from_IXMLDOMText( iface
);
573 TRACE("(%p)->(%p)\n", This
, prefix
);
574 return return_null_bstr( prefix
);
577 static HRESULT WINAPI
domtext_get_baseName(
581 domtext
*This
= impl_from_IXMLDOMText( iface
);
582 TRACE("(%p)->(%p)\n", This
, name
);
583 return return_null_bstr( name
);
586 static HRESULT WINAPI
domtext_transformNodeToObject(
588 IXMLDOMNode
* domNode
, VARIANT var1
)
590 domtext
*This
= impl_from_IXMLDOMText( iface
);
591 FIXME("(%p)->(%p %s)\n", This
, domNode
, debugstr_variant(&var1
));
595 static HRESULT WINAPI
domtext_get_data(
599 domtext
*This
= impl_from_IXMLDOMText( iface
);
604 *p
= bstr_from_xmlChar(This
->node
.node
->content
);
608 static HRESULT WINAPI
domtext_put_data(
612 domtext
*This
= impl_from_IXMLDOMText( iface
);
613 BSTR normalized_data
= NULL
;
617 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
621 /* normalize line endings */
622 normalized_data
= SysAllocStringLen(NULL
, SysStringLen(data
));
623 if (!normalized_data
) return E_OUTOFMEMORY
;
624 for (i
= 0, j
= 0; data
[i
]; i
++)
628 if (data
[i
+ 1] == '\n') i
++; /* change \r\n to just \n */
629 normalized_data
[j
++] = '\n'; /* change \r by itself to \n */
632 normalized_data
[j
++] = data
[i
];
634 normalized_data
[j
] = 0;
637 domtext_reset_noenc(This
);
638 hr
= node_set_content(&This
->node
, normalized_data
);
640 SysFreeString(normalized_data
);
644 static HRESULT WINAPI
domtext_get_length(
648 domtext
*This
= impl_from_IXMLDOMText( iface
);
652 TRACE("(%p)->(%p)\n", This
, len
);
657 hr
= IXMLDOMText_get_data(iface
, &data
);
660 *len
= SysStringLen(data
);
667 static HRESULT WINAPI
domtext_substringData(
669 LONG offset
, LONG count
, BSTR
*p
)
674 TRACE("%p, %ld, %ld, %p.\n", iface
, offset
, count
, p
);
680 if(offset
< 0 || count
< 0)
686 hr
= IXMLDOMText_get_data(iface
, &data
);
689 LONG len
= SysStringLen(data
);
693 if(offset
+ count
> len
)
694 *p
= SysAllocString(&data
[offset
]);
696 *p
= SysAllocStringLen(&data
[offset
], count
);
707 static HRESULT WINAPI
domtext_appendData(
711 domtext
*This
= impl_from_IXMLDOMText( iface
);
716 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
718 /* Nothing to do if NULL or an Empty string passed in. */
719 if((p_len
= SysStringLen(p
)) == 0) return S_OK
;
721 hr
= IXMLDOMText_get_data(iface
, &data
);
724 LONG len
= SysStringLen(data
);
725 BSTR str
= SysAllocStringLen(NULL
, p_len
+ len
);
727 memcpy(str
, data
, len
*sizeof(WCHAR
));
728 memcpy(&str
[len
], p
, p_len
*sizeof(WCHAR
));
731 hr
= IXMLDOMText_put_data(iface
, str
);
740 static HRESULT WINAPI
domtext_insertData(
748 TRACE("%p, %ld, %s.\n", iface
, offset
, debugstr_w(p
));
750 /* If have a NULL or empty string, don't do anything. */
751 if((p_len
= SysStringLen(p
)) == 0)
759 hr
= IXMLDOMText_get_data(iface
, &data
);
762 LONG len
= SysStringLen(data
);
771 str
= SysAllocStringLen(NULL
, len
+ p_len
);
772 /* start part, supplied string and end part */
773 memcpy(str
, data
, offset
*sizeof(WCHAR
));
774 memcpy(&str
[offset
], p
, p_len
*sizeof(WCHAR
));
775 memcpy(&str
[offset
+p_len
], &data
[offset
], (len
-offset
)*sizeof(WCHAR
));
778 hr
= IXMLDOMText_put_data(iface
, str
);
787 static HRESULT WINAPI
domtext_deleteData(
789 LONG offset
, LONG count
)
795 TRACE("%p, %ld, %ld.\n", iface
, offset
, count
);
797 hr
= IXMLDOMText_get_length(iface
, &len
);
798 if(hr
!= S_OK
) return hr
;
800 if((offset
< 0) || (offset
> len
) || (count
< 0))
803 if(len
== 0) return S_OK
;
805 /* cutting start or end */
806 if((offset
== 0) || ((count
+ offset
) >= len
))
809 IXMLDOMText_substringData(iface
, count
, len
- count
, &str
);
811 IXMLDOMText_substringData(iface
, 0, offset
, &str
);
812 hr
= IXMLDOMText_put_data(iface
, str
);
815 /* cutting from the inside */
819 IXMLDOMText_substringData(iface
, 0, offset
, &str
);
820 IXMLDOMText_substringData(iface
, offset
+ count
, len
- count
, &str_end
);
822 hr
= IXMLDOMText_put_data(iface
, str
);
824 hr
= IXMLDOMText_appendData(iface
, str_end
);
826 SysFreeString(str_end
);
834 static HRESULT WINAPI
domtext_replaceData(
836 LONG offset
, LONG count
, BSTR p
)
840 TRACE("%p, %ld, %ld, %s.\n", iface
, offset
, count
, debugstr_w(p
));
842 hr
= IXMLDOMText_deleteData(iface
, offset
, count
);
845 hr
= IXMLDOMText_insertData(iface
, offset
, p
);
850 static HRESULT WINAPI
domtext_splitText(
852 LONG offset
, IXMLDOMText
**txtNode
)
856 TRACE("%p, %ld, %p.\n", iface
, offset
, txtNode
);
858 if (!txtNode
|| offset
< 0) return E_INVALIDARG
;
862 IXMLDOMText_get_length(iface
, &length
);
864 if (offset
> length
) return E_INVALIDARG
;
865 if (offset
== length
) return S_FALSE
;
867 FIXME("adjacent text nodes are not supported\n");
872 static const struct IXMLDOMTextVtbl domtext_vtbl
=
874 domtext_QueryInterface
,
877 domtext_GetTypeInfoCount
,
879 domtext_GetIDsOfNames
,
881 domtext_get_nodeName
,
882 domtext_get_nodeValue
,
883 domtext_put_nodeValue
,
884 domtext_get_nodeType
,
885 domtext_get_parentNode
,
886 domtext_get_childNodes
,
887 domtext_get_firstChild
,
888 domtext_get_lastChild
,
889 domtext_get_previousSibling
,
890 domtext_get_nextSibling
,
891 domtext_get_attributes
,
892 domtext_insertBefore
,
893 domtext_replaceChild
,
896 domtext_hasChildNodes
,
897 domtext_get_ownerDocument
,
899 domtext_get_nodeTypeString
,
902 domtext_get_specified
,
903 domtext_get_definition
,
904 domtext_get_nodeTypedValue
,
905 domtext_put_nodeTypedValue
,
906 domtext_get_dataType
,
907 domtext_put_dataType
,
909 domtext_transformNode
,
911 domtext_selectSingleNode
,
913 domtext_get_namespaceURI
,
915 domtext_get_baseName
,
916 domtext_transformNodeToObject
,
920 domtext_substringData
,
928 static const tid_t domtext_iface_tids
[] = {
933 static dispex_static_data_t domtext_dispex
= {
940 IUnknown
* create_text( xmlNodePtr text
)
944 This
= heap_alloc( sizeof *This
);
948 This
->IXMLDOMText_iface
.lpVtbl
= &domtext_vtbl
;
951 init_xmlnode(&This
->node
, text
, (IXMLDOMNode
*)&This
->IXMLDOMText_iface
, &domtext_dispex
);
953 return (IUnknown
*)&This
->IXMLDOMText_iface
;