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"
43 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 node_has_childnodes(&This->node, 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, FALSE, 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 static WCHAR rnW[] = {'\r','\n',0};
621 TRACE("(%p)->(%s)\n", This, debugstr_w(data));
623 if (data && !strcmpW(rnW, data))
624 This->node.node->name = xmlStringTextNoenc;
626 domtext_reset_noenc(This);
627 return node_set_content(&This->node, data);
630 static HRESULT WINAPI domtext_get_length(
634 domtext *This = impl_from_IXMLDOMText( iface );
638 TRACE("(%p)->(%p)\n", This, len);
643 hr = IXMLDOMText_get_data(iface, &data);
646 *len = SysStringLen(data);
653 static HRESULT WINAPI domtext_substringData(
655 LONG offset, LONG count, BSTR *p)
657 domtext *This = impl_from_IXMLDOMText( iface );
661 TRACE("(%p)->(%d %d %p)\n", This, offset, count, p);
667 if(offset < 0 || count < 0)
673 hr = IXMLDOMText_get_data(iface, &data);
676 LONG len = SysStringLen(data);
680 if(offset + count > len)
681 *p = SysAllocString(&data[offset]);
683 *p = SysAllocStringLen(&data[offset], count);
694 static HRESULT WINAPI domtext_appendData(
698 domtext *This = impl_from_IXMLDOMText( iface );
703 TRACE("(%p)->(%s)\n", This, debugstr_w(p));
705 /* Nothing to do if NULL or an Empty string passed in. */
706 if((p_len = SysStringLen(p)) == 0) return S_OK;
708 hr = IXMLDOMText_get_data(iface, &data);
711 LONG len = SysStringLen(data);
712 BSTR str = SysAllocStringLen(NULL, p_len + len);
714 memcpy(str, data, len*sizeof(WCHAR));
715 memcpy(&str[len], p, p_len*sizeof(WCHAR));
718 hr = IXMLDOMText_put_data(iface, str);
727 static HRESULT WINAPI domtext_insertData(
731 domtext *This = impl_from_IXMLDOMText( iface );
736 TRACE("(%p)->(%d %s)\n", This, offset, debugstr_w(p));
738 /* If have a NULL or empty string, don't do anything. */
739 if((p_len = SysStringLen(p)) == 0)
747 hr = IXMLDOMText_get_data(iface, &data);
750 LONG len = SysStringLen(data);
759 str = SysAllocStringLen(NULL, len + p_len);
760 /* start part, supplied string and end part */
761 memcpy(str, data, offset*sizeof(WCHAR));
762 memcpy(&str[offset], p, p_len*sizeof(WCHAR));
763 memcpy(&str[offset+p_len], &data[offset], (len-offset)*sizeof(WCHAR));
766 hr = IXMLDOMText_put_data(iface, str);
775 static HRESULT WINAPI domtext_deleteData(
777 LONG offset, LONG count)
783 TRACE("(%p)->(%d %d)\n", iface, offset, count);
785 hr = IXMLDOMText_get_length(iface, &len);
786 if(hr != S_OK) return hr;
788 if((offset < 0) || (offset > len) || (count < 0))
791 if(len == 0) return S_OK;
793 /* cutting start or end */
794 if((offset == 0) || ((count + offset) >= len))
797 IXMLDOMText_substringData(iface, count, len - count, &str);
799 IXMLDOMText_substringData(iface, 0, offset, &str);
800 hr = IXMLDOMText_put_data(iface, str);
803 /* cutting from the inside */
807 IXMLDOMText_substringData(iface, 0, offset, &str);
808 IXMLDOMText_substringData(iface, offset + count, len - count, &str_end);
810 hr = IXMLDOMText_put_data(iface, str);
812 hr = IXMLDOMText_appendData(iface, str_end);
814 SysFreeString(str_end);
822 static HRESULT WINAPI domtext_replaceData(
824 LONG offset, LONG count, BSTR p)
826 domtext *This = impl_from_IXMLDOMText( iface );
829 TRACE("(%p)->(%d %d %s)\n", This, offset, count, debugstr_w(p));
831 hr = IXMLDOMText_deleteData(iface, offset, count);
834 hr = IXMLDOMText_insertData(iface, offset, p);
839 static HRESULT WINAPI domtext_splitText(
841 LONG offset, IXMLDOMText **txtNode)
843 domtext *This = impl_from_IXMLDOMText( iface );
846 TRACE("(%p)->(%d %p)\n", This, offset, txtNode);
848 if (!txtNode || offset < 0) return E_INVALIDARG;
852 IXMLDOMText_get_length(iface, &length);
854 if (offset > length) return E_INVALIDARG;
855 if (offset == length) return S_FALSE;
857 FIXME("adjacent text nodes are not supported\n");
862 static const struct IXMLDOMTextVtbl domtext_vtbl =
864 domtext_QueryInterface,
867 domtext_GetTypeInfoCount,
869 domtext_GetIDsOfNames,
871 domtext_get_nodeName,
872 domtext_get_nodeValue,
873 domtext_put_nodeValue,
874 domtext_get_nodeType,
875 domtext_get_parentNode,
876 domtext_get_childNodes,
877 domtext_get_firstChild,
878 domtext_get_lastChild,
879 domtext_get_previousSibling,
880 domtext_get_nextSibling,
881 domtext_get_attributes,
882 domtext_insertBefore,
883 domtext_replaceChild,
886 domtext_hasChildNodes,
887 domtext_get_ownerDocument,
889 domtext_get_nodeTypeString,
892 domtext_get_specified,
893 domtext_get_definition,
894 domtext_get_nodeTypedValue,
895 domtext_put_nodeTypedValue,
896 domtext_get_dataType,
897 domtext_put_dataType,
899 domtext_transformNode,
901 domtext_selectSingleNode,
903 domtext_get_namespaceURI,
905 domtext_get_baseName,
906 domtext_transformNodeToObject,
910 domtext_substringData,
918 static const tid_t domtext_iface_tids[] = {
923 static dispex_static_data_t domtext_dispex = {
930 IUnknown* create_text( xmlNodePtr text )
934 This = heap_alloc( sizeof *This );
938 This->IXMLDOMText_iface.lpVtbl = &domtext_vtbl;
941 init_xmlnode(&This->node, text, (IXMLDOMNode*)&This->IXMLDOMText_iface, &domtext_dispex);
943 return (IUnknown*)&This->IXMLDOMText_iface;