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
28 # include <libxml/parser.h>
29 # include <libxml/parserInternals.h>
30 # include <libxml/xmlerror.h>
39 #include "msxml_private.h"
41 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
47 typedef struct _domtext
50 IXMLDOMText IXMLDOMText_iface
;
54 static inline domtext
*impl_from_IXMLDOMText( IXMLDOMText
*iface
)
56 return CONTAINING_RECORD(iface
, domtext
, IXMLDOMText_iface
);
59 static void domtext_reset_noenc(domtext
*This
)
61 This
->node
.node
->name
= NULL
;
64 static HRESULT WINAPI
domtext_QueryInterface(
69 domtext
*This
= impl_from_IXMLDOMText( iface
);
70 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppvObject
);
72 if ( IsEqualGUID( riid
, &IID_IXMLDOMText
) ||
73 IsEqualGUID( riid
, &IID_IXMLDOMCharacterData
) ||
74 IsEqualGUID( riid
, &IID_IXMLDOMNode
) ||
75 IsEqualGUID( riid
, &IID_IDispatch
) ||
76 IsEqualGUID( riid
, &IID_IUnknown
) )
80 else if(node_query_interface(&This
->node
, riid
, ppvObject
))
82 return *ppvObject
? S_OK
: E_NOINTERFACE
;
86 TRACE("Unsupported interface %s\n", debugstr_guid(riid
));
91 IXMLDOMText_AddRef(iface
);
95 static ULONG WINAPI
domtext_AddRef(
98 domtext
*This
= impl_from_IXMLDOMText( iface
);
99 ULONG ref
= InterlockedIncrement( &This
->ref
);
100 TRACE("(%p)->(%d)\n", This
, ref
);
104 static ULONG WINAPI
domtext_Release(
107 domtext
*This
= impl_from_IXMLDOMText( iface
);
108 ULONG ref
= InterlockedDecrement( &This
->ref
);
110 TRACE("(%p)->(%d)\n", This
, ref
);
113 destroy_xmlnode(&This
->node
);
120 static HRESULT WINAPI
domtext_GetTypeInfoCount(
124 domtext
*This
= impl_from_IXMLDOMText( iface
);
125 return IDispatchEx_GetTypeInfoCount(&This
->node
.dispex
.IDispatchEx_iface
, pctinfo
);
128 static HRESULT WINAPI
domtext_GetTypeInfo(
130 UINT iTInfo
, LCID lcid
,
131 ITypeInfo
** ppTInfo
)
133 domtext
*This
= impl_from_IXMLDOMText( iface
);
134 return IDispatchEx_GetTypeInfo(&This
->node
.dispex
.IDispatchEx_iface
,
135 iTInfo
, lcid
, ppTInfo
);
138 static HRESULT WINAPI
domtext_GetIDsOfNames(
140 REFIID riid
, LPOLESTR
* rgszNames
,
141 UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
143 domtext
*This
= impl_from_IXMLDOMText( iface
);
144 return IDispatchEx_GetIDsOfNames(&This
->node
.dispex
.IDispatchEx_iface
,
145 riid
, rgszNames
, cNames
, lcid
, rgDispId
);
148 static HRESULT WINAPI
domtext_Invoke(
150 DISPID dispIdMember
, REFIID riid
, LCID lcid
,
151 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
152 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
154 domtext
*This
= impl_from_IXMLDOMText( iface
);
155 return IDispatchEx_Invoke(&This
->node
.dispex
.IDispatchEx_iface
,
156 dispIdMember
, riid
, lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
159 static HRESULT WINAPI
domtext_get_nodeName(
163 domtext
*This
= impl_from_IXMLDOMText( iface
);
165 static const WCHAR textW
[] = {'#','t','e','x','t',0};
167 TRACE("(%p)->(%p)\n", This
, p
);
169 return return_bstr(textW
, p
);
172 static HRESULT WINAPI
domtext_get_nodeValue(
176 domtext
*This
= impl_from_IXMLDOMText( iface
);
178 TRACE("(%p)->(%p)\n", This
, value
);
180 return node_get_content(&This
->node
, value
);
183 static HRESULT WINAPI
domtext_put_nodeValue(
187 domtext
*This
= impl_from_IXMLDOMText( iface
);
189 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&value
));
191 domtext_reset_noenc(This
);
192 return node_put_value(&This
->node
, &value
);
195 static HRESULT WINAPI
domtext_get_nodeType(
197 DOMNodeType
* domNodeType
)
199 domtext
*This
= impl_from_IXMLDOMText( iface
);
201 TRACE("(%p)->(%p)\n", This
, domNodeType
);
203 *domNodeType
= NODE_TEXT
;
207 static HRESULT WINAPI
domtext_get_parentNode(
209 IXMLDOMNode
** parent
)
211 domtext
*This
= impl_from_IXMLDOMText( iface
);
213 TRACE("(%p)->(%p)\n", This
, parent
);
215 return node_get_parent(&This
->node
, parent
);
218 static HRESULT WINAPI
domtext_get_childNodes(
220 IXMLDOMNodeList
** outList
)
222 domtext
*This
= impl_from_IXMLDOMText( iface
);
224 TRACE("(%p)->(%p)\n", This
, outList
);
226 return node_get_child_nodes(&This
->node
, outList
);
229 static HRESULT WINAPI
domtext_get_firstChild(
231 IXMLDOMNode
** domNode
)
233 domtext
*This
= impl_from_IXMLDOMText( iface
);
235 TRACE("(%p)->(%p)\n", This
, domNode
);
237 return return_null_node(domNode
);
240 static HRESULT WINAPI
domtext_get_lastChild(
242 IXMLDOMNode
** domNode
)
244 domtext
*This
= impl_from_IXMLDOMText( iface
);
246 TRACE("(%p)->(%p)\n", This
, domNode
);
248 return return_null_node(domNode
);
251 static HRESULT WINAPI
domtext_get_previousSibling(
253 IXMLDOMNode
** domNode
)
255 domtext
*This
= impl_from_IXMLDOMText( iface
);
257 TRACE("(%p)->(%p)\n", This
, domNode
);
259 return node_get_previous_sibling(&This
->node
, domNode
);
262 static HRESULT WINAPI
domtext_get_nextSibling(
264 IXMLDOMNode
** domNode
)
266 domtext
*This
= impl_from_IXMLDOMText( iface
);
268 TRACE("(%p)->(%p)\n", This
, domNode
);
270 return node_get_next_sibling(&This
->node
, domNode
);
273 static HRESULT WINAPI
domtext_get_attributes(
275 IXMLDOMNamedNodeMap
** attributeMap
)
277 domtext
*This
= impl_from_IXMLDOMText( iface
);
279 TRACE("(%p)->(%p)\n", This
, attributeMap
);
281 return return_null_ptr((void**)attributeMap
);
284 static HRESULT WINAPI
domtext_insertBefore(
286 IXMLDOMNode
* newNode
, VARIANT refChild
,
287 IXMLDOMNode
** outOldNode
)
289 domtext
*This
= impl_from_IXMLDOMText( iface
);
291 FIXME("(%p)->(%p %s %p) needs test\n", This
, newNode
, debugstr_variant(&refChild
), outOldNode
);
293 return node_insert_before(&This
->node
, newNode
, &refChild
, outOldNode
);
296 static HRESULT WINAPI
domtext_replaceChild(
298 IXMLDOMNode
* newNode
,
299 IXMLDOMNode
* oldNode
,
300 IXMLDOMNode
** outOldNode
)
302 domtext
*This
= impl_from_IXMLDOMText( iface
);
304 FIXME("(%p)->(%p %p %p) needs test\n", This
, newNode
, oldNode
, outOldNode
);
306 return node_replace_child(&This
->node
, newNode
, oldNode
, outOldNode
);
309 static HRESULT WINAPI
domtext_removeChild(
311 IXMLDOMNode
*child
, IXMLDOMNode
**oldChild
)
313 domtext
*This
= impl_from_IXMLDOMText( iface
);
314 TRACE("(%p)->(%p %p)\n", This
, child
, oldChild
);
315 return node_remove_child(&This
->node
, child
, oldChild
);
318 static HRESULT WINAPI
domtext_appendChild(
320 IXMLDOMNode
*child
, IXMLDOMNode
**outChild
)
322 domtext
*This
= impl_from_IXMLDOMText( iface
);
323 TRACE("(%p)->(%p %p)\n", This
, child
, outChild
);
324 return node_append_child(&This
->node
, child
, outChild
);
327 static HRESULT WINAPI
domtext_hasChildNodes(
331 domtext
*This
= impl_from_IXMLDOMText( iface
);
332 TRACE("(%p)->(%p)\n", This
, ret
);
333 return return_var_false(ret
);
336 static HRESULT WINAPI
domtext_get_ownerDocument(
338 IXMLDOMDocument
**doc
)
340 domtext
*This
= impl_from_IXMLDOMText( iface
);
341 TRACE("(%p)->(%p)\n", This
, doc
);
342 return node_get_owner_doc(&This
->node
, doc
);
345 static HRESULT WINAPI
domtext_cloneNode(
347 VARIANT_BOOL deep
, IXMLDOMNode
** outNode
)
349 domtext
*This
= impl_from_IXMLDOMText( iface
);
350 TRACE("(%p)->(%d %p)\n", This
, deep
, outNode
);
351 return node_clone( &This
->node
, deep
, outNode
);
354 static HRESULT WINAPI
domtext_get_nodeTypeString(
358 domtext
*This
= impl_from_IXMLDOMText( iface
);
359 static const WCHAR textW
[] = {'t','e','x','t',0};
361 TRACE("(%p)->(%p)\n", This
, p
);
363 return return_bstr(textW
, p
);
366 static HRESULT WINAPI
domtext_get_text(
370 domtext
*This
= impl_from_IXMLDOMText( iface
);
371 TRACE("(%p)->(%p)\n", This
, p
);
372 return node_get_text(&This
->node
, p
);
375 static HRESULT WINAPI
domtext_put_text(
379 domtext
*This
= impl_from_IXMLDOMText( iface
);
380 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
381 domtext_reset_noenc(This
);
382 return node_put_text( &This
->node
, p
);
385 static HRESULT WINAPI
domtext_get_specified(
387 VARIANT_BOOL
* isSpecified
)
389 domtext
*This
= impl_from_IXMLDOMText( iface
);
390 FIXME("(%p)->(%p) stub!\n", This
, isSpecified
);
391 *isSpecified
= VARIANT_TRUE
;
395 static HRESULT WINAPI
domtext_get_definition(
397 IXMLDOMNode
** definitionNode
)
399 domtext
*This
= impl_from_IXMLDOMText( iface
);
400 FIXME("(%p)->(%p)\n", This
, definitionNode
);
404 static HRESULT WINAPI
domtext_get_nodeTypedValue(
408 domtext
*This
= impl_from_IXMLDOMText( iface
);
409 IXMLDOMNode
* parent
= NULL
;
412 TRACE("(%p)->(%p)\n", This
, var1
);
417 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
421 hr
= IXMLDOMNode_get_nodeTypedValue(parent
, var1
);
422 IXMLDOMNode_Release(parent
);
426 V_VT(var1
) = VT_NULL
;
434 static HRESULT WINAPI
domtext_put_nodeTypedValue(
438 domtext
*This
= impl_from_IXMLDOMText( iface
);
439 IXMLDOMNode
* parent
= NULL
;
442 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&value
));
444 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
448 hr
= IXMLDOMNode_put_nodeTypedValue(parent
, value
);
449 IXMLDOMNode_Release(parent
);
459 static HRESULT WINAPI
domtext_get_dataType(
463 domtext
*This
= impl_from_IXMLDOMText( iface
);
464 IXMLDOMNode
* parent
= NULL
;
467 TRACE("(%p)->(%p)\n", This
, dtName
);
472 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
476 hr
= IXMLDOMNode_get_dataType(parent
, dtName
);
477 IXMLDOMNode_Release(parent
);
481 V_VT(dtName
) = VT_NULL
;
482 V_BSTR(dtName
) = NULL
;
489 static HRESULT WINAPI
domtext_put_dataType(
493 domtext
*This
= impl_from_IXMLDOMText( iface
);
494 IXMLDOMNode
* parent
= NULL
;
497 TRACE("(%p)->(%p)\n", This
, dtName
);
502 hr
= IXMLDOMText_get_parentNode(iface
, &parent
);
506 hr
= IXMLDOMNode_put_dataType(parent
, dtName
);
507 IXMLDOMNode_Release(parent
);
517 static HRESULT WINAPI
domtext_get_xml(
521 domtext
*This
= impl_from_IXMLDOMText( iface
);
523 TRACE("(%p)->(%p)\n", This
, p
);
525 return node_get_xml(&This
->node
, TRUE
, p
);
528 static HRESULT WINAPI
domtext_transformNode(
530 IXMLDOMNode
*node
, BSTR
*p
)
532 domtext
*This
= impl_from_IXMLDOMText( iface
);
533 TRACE("(%p)->(%p %p)\n", This
, node
, p
);
534 return node_transform_node(&This
->node
, node
, p
);
537 static HRESULT WINAPI
domtext_selectNodes(
539 BSTR p
, IXMLDOMNodeList
** outList
)
541 domtext
*This
= impl_from_IXMLDOMText( iface
);
542 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), outList
);
543 return node_select_nodes(&This
->node
, p
, outList
);
546 static HRESULT WINAPI
domtext_selectSingleNode(
548 BSTR p
, IXMLDOMNode
** outNode
)
550 domtext
*This
= impl_from_IXMLDOMText( iface
);
551 TRACE("(%p)->(%s %p)\n", This
, debugstr_w(p
), outNode
);
552 return node_select_singlenode(&This
->node
, p
, outNode
);
555 static HRESULT WINAPI
domtext_get_parsed(
557 VARIANT_BOOL
* isParsed
)
559 domtext
*This
= impl_from_IXMLDOMText( iface
);
560 FIXME("(%p)->(%p) stub!\n", This
, isParsed
);
561 *isParsed
= VARIANT_TRUE
;
565 static HRESULT WINAPI
domtext_get_namespaceURI(
569 domtext
*This
= impl_from_IXMLDOMText( iface
);
570 TRACE("(%p)->(%p)\n", This
, p
);
571 return node_get_namespaceURI(&This
->node
, p
);
574 static HRESULT WINAPI
domtext_get_prefix(
578 domtext
*This
= impl_from_IXMLDOMText( iface
);
579 TRACE("(%p)->(%p)\n", This
, prefix
);
580 return return_null_bstr( prefix
);
583 static HRESULT WINAPI
domtext_get_baseName(
587 domtext
*This
= impl_from_IXMLDOMText( iface
);
588 TRACE("(%p)->(%p)\n", This
, name
);
589 return return_null_bstr( name
);
592 static HRESULT WINAPI
domtext_transformNodeToObject(
594 IXMLDOMNode
* domNode
, VARIANT var1
)
596 domtext
*This
= impl_from_IXMLDOMText( iface
);
597 FIXME("(%p)->(%p %s)\n", This
, domNode
, debugstr_variant(&var1
));
601 static HRESULT WINAPI
domtext_get_data(
605 domtext
*This
= impl_from_IXMLDOMText( iface
);
610 *p
= bstr_from_xmlChar(This
->node
.node
->content
);
614 static HRESULT WINAPI
domtext_put_data(
618 domtext
*This
= impl_from_IXMLDOMText( iface
);
619 BSTR normalized_data
= NULL
;
623 TRACE("(%p)->(%s)\n", This
, debugstr_w(data
));
627 /* normalize line endings */
628 normalized_data
= SysAllocStringLen(NULL
, SysStringLen(data
));
629 if (!normalized_data
) return E_OUTOFMEMORY
;
630 for (i
= 0, j
= 0; data
[i
]; i
++)
634 if (data
[i
+ 1] == '\n') i
++; /* change \r\n to just \n */
635 normalized_data
[j
++] = '\n'; /* change \r by itself to \n */
638 normalized_data
[j
++] = data
[i
];
640 normalized_data
[j
] = 0;
643 domtext_reset_noenc(This
);
644 hr
= node_set_content(&This
->node
, normalized_data
);
646 SysFreeString(normalized_data
);
650 static HRESULT WINAPI
domtext_get_length(
654 domtext
*This
= impl_from_IXMLDOMText( iface
);
658 TRACE("(%p)->(%p)\n", This
, len
);
663 hr
= IXMLDOMText_get_data(iface
, &data
);
666 *len
= SysStringLen(data
);
673 static HRESULT WINAPI
domtext_substringData(
675 LONG offset
, LONG count
, BSTR
*p
)
677 domtext
*This
= impl_from_IXMLDOMText( iface
);
681 TRACE("(%p)->(%d %d %p)\n", This
, offset
, count
, p
);
687 if(offset
< 0 || count
< 0)
693 hr
= IXMLDOMText_get_data(iface
, &data
);
696 LONG len
= SysStringLen(data
);
700 if(offset
+ count
> len
)
701 *p
= SysAllocString(&data
[offset
]);
703 *p
= SysAllocStringLen(&data
[offset
], count
);
714 static HRESULT WINAPI
domtext_appendData(
718 domtext
*This
= impl_from_IXMLDOMText( iface
);
723 TRACE("(%p)->(%s)\n", This
, debugstr_w(p
));
725 /* Nothing to do if NULL or an Empty string passed in. */
726 if((p_len
= SysStringLen(p
)) == 0) return S_OK
;
728 hr
= IXMLDOMText_get_data(iface
, &data
);
731 LONG len
= SysStringLen(data
);
732 BSTR str
= SysAllocStringLen(NULL
, p_len
+ len
);
734 memcpy(str
, data
, len
*sizeof(WCHAR
));
735 memcpy(&str
[len
], p
, p_len
*sizeof(WCHAR
));
738 hr
= IXMLDOMText_put_data(iface
, str
);
747 static HRESULT WINAPI
domtext_insertData(
751 domtext
*This
= impl_from_IXMLDOMText( iface
);
756 TRACE("(%p)->(%d %s)\n", This
, offset
, debugstr_w(p
));
758 /* If have a NULL or empty string, don't do anything. */
759 if((p_len
= SysStringLen(p
)) == 0)
767 hr
= IXMLDOMText_get_data(iface
, &data
);
770 LONG len
= SysStringLen(data
);
779 str
= SysAllocStringLen(NULL
, len
+ p_len
);
780 /* start part, supplied string and end part */
781 memcpy(str
, data
, offset
*sizeof(WCHAR
));
782 memcpy(&str
[offset
], p
, p_len
*sizeof(WCHAR
));
783 memcpy(&str
[offset
+p_len
], &data
[offset
], (len
-offset
)*sizeof(WCHAR
));
786 hr
= IXMLDOMText_put_data(iface
, str
);
795 static HRESULT WINAPI
domtext_deleteData(
797 LONG offset
, LONG count
)
803 TRACE("(%p)->(%d %d)\n", iface
, offset
, count
);
805 hr
= IXMLDOMText_get_length(iface
, &len
);
806 if(hr
!= S_OK
) return hr
;
808 if((offset
< 0) || (offset
> len
) || (count
< 0))
811 if(len
== 0) return S_OK
;
813 /* cutting start or end */
814 if((offset
== 0) || ((count
+ offset
) >= len
))
817 IXMLDOMText_substringData(iface
, count
, len
- count
, &str
);
819 IXMLDOMText_substringData(iface
, 0, offset
, &str
);
820 hr
= IXMLDOMText_put_data(iface
, str
);
823 /* cutting from the inside */
827 IXMLDOMText_substringData(iface
, 0, offset
, &str
);
828 IXMLDOMText_substringData(iface
, offset
+ count
, len
- count
, &str_end
);
830 hr
= IXMLDOMText_put_data(iface
, str
);
832 hr
= IXMLDOMText_appendData(iface
, str_end
);
834 SysFreeString(str_end
);
842 static HRESULT WINAPI
domtext_replaceData(
844 LONG offset
, LONG count
, BSTR p
)
846 domtext
*This
= impl_from_IXMLDOMText( iface
);
849 TRACE("(%p)->(%d %d %s)\n", This
, offset
, count
, debugstr_w(p
));
851 hr
= IXMLDOMText_deleteData(iface
, offset
, count
);
854 hr
= IXMLDOMText_insertData(iface
, offset
, p
);
859 static HRESULT WINAPI
domtext_splitText(
861 LONG offset
, IXMLDOMText
**txtNode
)
863 domtext
*This
= impl_from_IXMLDOMText( iface
);
866 TRACE("(%p)->(%d %p)\n", This
, offset
, txtNode
);
868 if (!txtNode
|| offset
< 0) return E_INVALIDARG
;
872 IXMLDOMText_get_length(iface
, &length
);
874 if (offset
> length
) return E_INVALIDARG
;
875 if (offset
== length
) return S_FALSE
;
877 FIXME("adjacent text nodes are not supported\n");
882 static const struct IXMLDOMTextVtbl domtext_vtbl
=
884 domtext_QueryInterface
,
887 domtext_GetTypeInfoCount
,
889 domtext_GetIDsOfNames
,
891 domtext_get_nodeName
,
892 domtext_get_nodeValue
,
893 domtext_put_nodeValue
,
894 domtext_get_nodeType
,
895 domtext_get_parentNode
,
896 domtext_get_childNodes
,
897 domtext_get_firstChild
,
898 domtext_get_lastChild
,
899 domtext_get_previousSibling
,
900 domtext_get_nextSibling
,
901 domtext_get_attributes
,
902 domtext_insertBefore
,
903 domtext_replaceChild
,
906 domtext_hasChildNodes
,
907 domtext_get_ownerDocument
,
909 domtext_get_nodeTypeString
,
912 domtext_get_specified
,
913 domtext_get_definition
,
914 domtext_get_nodeTypedValue
,
915 domtext_put_nodeTypedValue
,
916 domtext_get_dataType
,
917 domtext_put_dataType
,
919 domtext_transformNode
,
921 domtext_selectSingleNode
,
923 domtext_get_namespaceURI
,
925 domtext_get_baseName
,
926 domtext_transformNodeToObject
,
930 domtext_substringData
,
938 static const tid_t domtext_iface_tids
[] = {
943 static dispex_static_data_t domtext_dispex
= {
950 IUnknown
* create_text( xmlNodePtr text
)
954 This
= heap_alloc( sizeof *This
);
958 This
->IXMLDOMText_iface
.lpVtbl
= &domtext_vtbl
;
961 init_xmlnode(&This
->node
, text
, (IXMLDOMNode
*)&This
->IXMLDOMText_iface
, &domtext_dispex
);
963 return (IUnknown
*)&This
->IXMLDOMText_iface
;