mshtml: Properly handle NULL VT_DISPATCH refChild in IHTMLDOMNode::insertBefore.
[wine/multimedia.git] / dlls / mshtml / htmlnode.c
blobb0cf4c3171c2e53727222676da1ed5f970e20906
1 /*
2 * Copyright 2006 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 static HTMLDOMNode *get_node_obj(HTMLDocumentNode*,IUnknown*);
37 static HRESULT create_node(HTMLDocumentNode*,nsIDOMNode*,HTMLDOMNode**);
39 typedef struct {
40 DispatchEx dispex;
41 IHTMLDOMChildrenCollection IHTMLDOMChildrenCollection_iface;
43 LONG ref;
45 /* FIXME: implement weak reference */
46 HTMLDocumentNode *doc;
48 nsIDOMNodeList *nslist;
49 } HTMLDOMChildrenCollection;
51 static inline HTMLDOMChildrenCollection *impl_from_IHTMLDOMChildrenCollection(IHTMLDOMChildrenCollection *iface)
53 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, IHTMLDOMChildrenCollection_iface);
56 static HRESULT WINAPI HTMLDOMChildrenCollection_QueryInterface(IHTMLDOMChildrenCollection *iface, REFIID riid, void **ppv)
58 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
60 *ppv = NULL;
62 if(IsEqualGUID(&IID_IUnknown, riid)) {
63 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
64 *ppv = &This->IHTMLDOMChildrenCollection_iface;
65 }else if(IsEqualGUID(&IID_IHTMLDOMChildrenCollection, riid)) {
66 TRACE("(%p)->(IID_IHTMLDOMChildrenCollection %p)\n", This, ppv);
67 *ppv = &This->IHTMLDOMChildrenCollection_iface;
68 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
69 return *ppv ? S_OK : E_NOINTERFACE;
72 if(*ppv) {
73 IUnknown_AddRef((IUnknown*)*ppv);
74 return S_OK;
77 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
78 return E_NOINTERFACE;
81 static ULONG WINAPI HTMLDOMChildrenCollection_AddRef(IHTMLDOMChildrenCollection *iface)
83 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
84 LONG ref = InterlockedIncrement(&This->ref);
86 TRACE("(%p) ref=%d\n", This, ref);
88 return ref;
91 static ULONG WINAPI HTMLDOMChildrenCollection_Release(IHTMLDOMChildrenCollection *iface)
93 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
94 LONG ref = InterlockedDecrement(&This->ref);
96 TRACE("(%p) ref=%d\n", This, ref);
98 if(!ref) {
99 htmldoc_release(&This->doc->basedoc);
100 nsIDOMNodeList_Release(This->nslist);
101 heap_free(This);
104 return ref;
107 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfoCount(IHTMLDOMChildrenCollection *iface, UINT *pctinfo)
109 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
110 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
113 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfo(IHTMLDOMChildrenCollection *iface, UINT iTInfo,
114 LCID lcid, ITypeInfo **ppTInfo)
116 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
117 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
120 static HRESULT WINAPI HTMLDOMChildrenCollection_GetIDsOfNames(IHTMLDOMChildrenCollection *iface, REFIID riid,
121 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
123 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
124 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
125 lcid, rgDispId);
128 static HRESULT WINAPI HTMLDOMChildrenCollection_Invoke(IHTMLDOMChildrenCollection *iface, DISPID dispIdMember,
129 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
130 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
132 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
133 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
134 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
137 static HRESULT WINAPI HTMLDOMChildrenCollection_get_length(IHTMLDOMChildrenCollection *iface, LONG *p)
139 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
140 PRUint32 length=0;
142 TRACE("(%p)->(%p)\n", This, p);
144 nsIDOMNodeList_GetLength(This->nslist, &length);
145 *p = length;
146 return S_OK;
149 static HRESULT WINAPI HTMLDOMChildrenCollection__newEnum(IHTMLDOMChildrenCollection *iface, IUnknown **p)
151 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
152 FIXME("(%p)->(%p)\n", This, p);
153 return E_NOTIMPL;
156 static HRESULT WINAPI HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection *iface, LONG index, IDispatch **ppItem)
158 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
159 nsIDOMNode *nsnode = NULL;
160 HTMLDOMNode *node;
161 PRUint32 length=0;
162 nsresult nsres;
163 HRESULT hres;
165 TRACE("(%p)->(%d %p)\n", This, index, ppItem);
167 if (ppItem)
168 *ppItem = NULL;
169 else
170 return E_POINTER;
172 nsIDOMNodeList_GetLength(This->nslist, &length);
173 if(index < 0 || index >= length)
174 return E_INVALIDARG;
176 nsres = nsIDOMNodeList_Item(This->nslist, index, &nsnode);
177 if(NS_FAILED(nsres) || !nsnode) {
178 ERR("Item failed: %08x\n", nsres);
179 return E_FAIL;
182 hres = get_node(This->doc, nsnode, TRUE, &node);
183 if(FAILED(hres))
184 return hres;
186 *ppItem = (IDispatch*)&node->IHTMLDOMNode_iface;
187 return S_OK;
190 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl = {
191 HTMLDOMChildrenCollection_QueryInterface,
192 HTMLDOMChildrenCollection_AddRef,
193 HTMLDOMChildrenCollection_Release,
194 HTMLDOMChildrenCollection_GetTypeInfoCount,
195 HTMLDOMChildrenCollection_GetTypeInfo,
196 HTMLDOMChildrenCollection_GetIDsOfNames,
197 HTMLDOMChildrenCollection_Invoke,
198 HTMLDOMChildrenCollection_get_length,
199 HTMLDOMChildrenCollection__newEnum,
200 HTMLDOMChildrenCollection_item
203 static inline HTMLDOMChildrenCollection *impl_from_DispatchEx(DispatchEx *iface)
205 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, dispex);
208 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
210 static HRESULT HTMLDOMChildrenCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
212 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
213 WCHAR *ptr;
214 DWORD idx=0;
215 PRUint32 len = 0;
217 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
218 idx = idx*10 + (*ptr-'0');
219 if(*ptr)
220 return DISP_E_UNKNOWNNAME;
222 nsIDOMNodeList_GetLength(This->nslist, &len);
223 if(idx >= len)
224 return DISP_E_UNKNOWNNAME;
226 *dispid = DISPID_CHILDCOL_0 + idx;
227 TRACE("ret %x\n", *dispid);
228 return S_OK;
231 static HRESULT HTMLDOMChildrenCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
232 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
234 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
236 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
238 switch(flags) {
239 case DISPATCH_PROPERTYGET: {
240 IDispatch *disp = NULL;
241 HRESULT hres;
243 hres = IHTMLDOMChildrenCollection_item(&This->IHTMLDOMChildrenCollection_iface,
244 id - DISPID_CHILDCOL_0, &disp);
245 if(FAILED(hres))
246 return hres;
248 V_VT(res) = VT_DISPATCH;
249 V_DISPATCH(res) = disp;
250 break;
253 default:
254 FIXME("unimplemented flags %x\n", flags);
255 return E_NOTIMPL;
258 return S_OK;
261 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl = {
262 NULL,
263 HTMLDOMChildrenCollection_get_dispid,
264 HTMLDOMChildrenCollection_invoke,
265 NULL
268 static const tid_t HTMLDOMChildrenCollection_iface_tids[] = {
269 IHTMLDOMChildrenCollection_tid,
273 static dispex_static_data_t HTMLDOMChildrenCollection_dispex = {
274 &HTMLDOMChildrenCollection_dispex_vtbl,
275 DispDOMChildrenCollection_tid,
276 NULL,
277 HTMLDOMChildrenCollection_iface_tids
280 static IHTMLDOMChildrenCollection *create_child_collection(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
282 HTMLDOMChildrenCollection *ret;
284 ret = heap_alloc_zero(sizeof(*ret));
285 ret->IHTMLDOMChildrenCollection_iface.lpVtbl = &HTMLDOMChildrenCollectionVtbl;
286 ret->ref = 1;
288 nsIDOMNodeList_AddRef(nslist);
289 ret->nslist = nslist;
291 htmldoc_addref(&doc->basedoc);
292 ret->doc = doc;
294 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLDOMChildrenCollection_iface,
295 &HTMLDOMChildrenCollection_dispex);
297 return &ret->IHTMLDOMChildrenCollection_iface;
300 static inline HTMLDOMNode *impl_from_IHTMLDOMNode(IHTMLDOMNode *iface)
302 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode_iface);
305 static HRESULT WINAPI HTMLDOMNode_QueryInterface(IHTMLDOMNode *iface,
306 REFIID riid, void **ppv)
308 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
310 return This->vtbl->qi(This, riid, ppv);
313 static ULONG WINAPI HTMLDOMNode_AddRef(IHTMLDOMNode *iface)
315 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
316 LONG ref = ccref_incr(&This->ccref, (nsISupports*)&This->IHTMLDOMNode_iface);
318 TRACE("(%p) ref=%d\n", This, ref);
320 return ref;
323 static ULONG WINAPI HTMLDOMNode_Release(IHTMLDOMNode *iface)
325 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
326 LONG ref = ccref_decr(&This->ccref, (nsISupports*)&This->IHTMLDOMNode_iface);
328 TRACE("(%p) ref=%d\n", This, ref);
330 if(!ref) {
331 This->vtbl->destructor(This);
332 release_dispex(&This->dispex);
333 heap_free(This);
336 return ref;
339 static HRESULT WINAPI HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode *iface, UINT *pctinfo)
341 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
342 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
345 static HRESULT WINAPI HTMLDOMNode_GetTypeInfo(IHTMLDOMNode *iface, UINT iTInfo,
346 LCID lcid, ITypeInfo **ppTInfo)
348 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
349 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
352 static HRESULT WINAPI HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode *iface, REFIID riid,
353 LPOLESTR *rgszNames, UINT cNames,
354 LCID lcid, DISPID *rgDispId)
356 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
357 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
358 lcid, rgDispId);
361 static HRESULT WINAPI HTMLDOMNode_Invoke(IHTMLDOMNode *iface, DISPID dispIdMember,
362 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
363 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
365 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
366 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
367 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
370 static HRESULT WINAPI HTMLDOMNode_get_nodeType(IHTMLDOMNode *iface, LONG *p)
372 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
373 PRUint16 type = -1;
375 TRACE("(%p)->(%p)\n", This, p);
377 nsIDOMNode_GetNodeType(This->nsnode, &type);
379 switch(type) {
380 case ELEMENT_NODE:
381 *p = 1;
382 break;
383 case TEXT_NODE:
384 *p = 3;
385 break;
386 case COMMENT_NODE:
387 *p = 8;
388 break;
389 case DOCUMENT_NODE:
390 *p = 9;
391 break;
392 case DOCUMENT_FRAGMENT_NODE:
393 *p = 11;
394 break;
395 default:
397 * FIXME:
398 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
399 * It needs more tests.
401 FIXME("type %u\n", type);
402 *p = 0;
405 return S_OK;
408 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
410 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
411 HTMLDOMNode *node;
412 nsIDOMNode *nsnode;
413 nsresult nsres;
414 HRESULT hres;
416 TRACE("(%p)->(%p)\n", This, p);
418 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
419 if(NS_FAILED(nsres)) {
420 ERR("GetParentNode failed: %08x\n", nsres);
421 return E_FAIL;
424 if(!nsnode) {
425 *p = NULL;
426 return S_OK;
429 hres = get_node(This->doc, nsnode, TRUE, &node);
430 nsIDOMNode_Release(nsnode);
431 if(FAILED(hres))
432 return hres;
434 *p = &node->IHTMLDOMNode_iface;
435 return S_OK;
438 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
440 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
441 cpp_bool has_child = FALSE;
442 nsresult nsres;
444 TRACE("(%p)->(%p)\n", This, fChildren);
446 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
447 if(NS_FAILED(nsres))
448 ERR("HasChildNodes failed: %08x\n", nsres);
450 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
451 return S_OK;
454 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
456 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
457 nsIDOMNodeList *nslist;
458 nsresult nsres;
460 TRACE("(%p)->(%p)\n", This, p);
462 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
463 if(NS_FAILED(nsres)) {
464 ERR("GetChildNodes failed: %08x\n", nsres);
465 return E_FAIL;
468 *p = (IDispatch*)create_child_collection(This->doc, nslist);
469 nsIDOMNodeList_Release(nslist);
471 return S_OK;
474 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
476 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
477 HTMLAttributeCollection *col;
478 HRESULT hres;
480 TRACE("(%p)->(%p)\n", This, p);
482 if(This->vtbl->get_attr_col) {
483 hres = This->vtbl->get_attr_col(This, &col);
484 if(FAILED(hres))
485 return hres;
487 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
488 return S_OK;
491 *p = NULL;
492 return S_OK;
495 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
496 VARIANT refChild, IHTMLDOMNode **node)
498 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
499 HTMLDOMNode *new_child, *node_obj, *ref_node = NULL;
500 nsIDOMNode *nsnode;
501 nsresult nsres;
502 HRESULT hres = S_OK;
504 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
506 new_child = get_node_obj(This->doc, (IUnknown*)newChild);
507 if(!new_child) {
508 ERR("invalid newChild\n");
509 return E_INVALIDARG;
512 switch(V_VT(&refChild)) {
513 case VT_NULL:
514 break;
515 case VT_DISPATCH: {
516 if(!V_DISPATCH(&refChild))
517 break;
518 ref_node = get_node_obj(This->doc, (IUnknown*)V_DISPATCH(&refChild));
519 if(!ref_node) {
520 ERR("unvalid node\n");
521 hres = E_FAIL;
522 break;
524 break;
526 default:
527 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
528 hres = E_NOTIMPL;
531 if(SUCCEEDED(hres)) {
532 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, ref_node ? ref_node->nsnode : NULL, &nsnode);
533 if(NS_FAILED(nsres)) {
534 ERR("InsertBefore failed: %08x\n", nsres);
535 hres = E_FAIL;
538 node_release(new_child);
539 if(ref_node)
540 node_release(ref_node);
541 if(FAILED(hres))
542 return hres;
544 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
545 nsIDOMNode_Release(nsnode);
546 if(FAILED(hres))
547 return hres;
549 *node = &node_obj->IHTMLDOMNode_iface;
550 return S_OK;
553 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
554 IHTMLDOMNode **node)
556 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
557 HTMLDOMNode *node_obj;
558 nsIDOMNode *nsnode;
559 nsresult nsres;
560 HRESULT hres;
562 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
564 node_obj = get_node_obj(This->doc, (IUnknown*)oldChild);
565 if(!node_obj)
566 return E_FAIL;
568 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
569 node_release(node_obj);
570 if(NS_FAILED(nsres)) {
571 ERR("RemoveChild failed: %08x\n", nsres);
572 return E_FAIL;
575 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
576 nsIDOMNode_Release(nsnode);
577 if(FAILED(hres))
578 return hres;
580 /* FIXME: Make sure that node != newChild */
581 *node = &node_obj->IHTMLDOMNode_iface;
582 return S_OK;
585 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
586 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
588 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
589 HTMLDOMNode *node_new, *node_old, *ret_node;
590 nsIDOMNode *nsnode;
591 nsresult nsres;
592 HRESULT hres;
594 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
596 node_new = get_node_obj(This->doc, (IUnknown*)newChild);
597 if(!node_new)
598 return E_FAIL;
600 node_old = get_node_obj(This->doc, (IUnknown*)oldChild);
601 if(!node_old) {
602 node_release(node_new);
603 return E_FAIL;
606 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
607 node_release(node_new);
608 node_release(node_old);
609 if(NS_FAILED(nsres))
610 return E_FAIL;
612 hres = get_node(This->doc, nsnode, TRUE, &ret_node);
613 nsIDOMNode_Release(nsnode);
614 if(FAILED(hres))
615 return hres;
617 *node = &ret_node->IHTMLDOMNode_iface;
618 return S_OK;
621 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
622 IHTMLDOMNode **clonedNode)
624 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
625 HTMLDOMNode *new_node;
626 nsIDOMNode *nsnode;
627 nsresult nsres;
628 HRESULT hres;
630 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
632 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, 1, &nsnode);
633 if(NS_FAILED(nsres) || !nsnode) {
634 ERR("CloneNode failed: %08x\n", nsres);
635 return E_FAIL;
638 hres = This->vtbl->clone(This, nsnode, &new_node);
639 if(FAILED(hres))
640 return hres;
642 *clonedNode = &new_node->IHTMLDOMNode_iface;
643 return S_OK;
646 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
647 IHTMLDOMNode **removed)
649 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
650 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
651 return E_NOTIMPL;
654 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
655 IHTMLDOMNode **swappedNode)
657 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
658 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
659 return E_NOTIMPL;
662 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
663 IHTMLDOMNode **replaced)
665 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
666 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
667 return E_NOTIMPL;
670 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
671 IHTMLDOMNode **node)
673 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
674 HTMLDOMNode *node_obj;
675 nsIDOMNode *nsnode;
676 nsresult nsres;
677 HRESULT hres;
679 TRACE("(%p)->(%p %p)\n", This, newChild, node);
681 node_obj = get_node_obj(This->doc, (IUnknown*)newChild);
682 if(!node_obj)
683 return E_FAIL;
685 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
686 node_release(node_obj);
687 if(NS_FAILED(nsres)) {
688 ERR("AppendChild failed: %08x\n", nsres);
689 return E_FAIL;
692 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
693 nsIDOMNode_Release(nsnode);
694 if(FAILED(hres))
695 return hres;
697 /* FIXME: Make sure that node != newChild */
698 *node = &node_obj->IHTMLDOMNode_iface;
699 return S_OK;
702 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
704 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
705 nsAString name;
706 nsresult nsres;
708 TRACE("(%p)->(%p)\n", This, p);
710 nsAString_Init(&name, NULL);
711 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
712 return return_nsstr(nsres, &name, p);
715 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
717 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
719 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
721 switch(V_VT(&v)) {
722 case VT_BSTR: {
723 nsAString val_str;
725 nsAString_InitDepend(&val_str, V_BSTR(&v));
726 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
727 nsAString_Finish(&val_str);
729 return S_OK;
732 default:
733 FIXME("unsupported value %s\n", debugstr_variant(&v));
736 return E_NOTIMPL;
739 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
741 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
742 const PRUnichar *val;
743 nsAString val_str;
745 TRACE("(%p)->(%p)\n", This, p);
747 nsAString_Init(&val_str, NULL);
748 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
749 nsAString_GetData(&val_str, &val);
751 if(*val) {
752 V_VT(p) = VT_BSTR;
753 V_BSTR(p) = SysAllocString(val);
754 }else {
755 V_VT(p) = VT_NULL;
758 nsAString_Finish(&val_str);
760 return S_OK;
763 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
765 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
766 nsIDOMNode *nschild = NULL;
767 HTMLDOMNode *node;
768 HRESULT hres;
770 TRACE("(%p)->(%p)\n", This, p);
772 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
773 if(!nschild) {
774 *p = NULL;
775 return S_OK;
778 hres = get_node(This->doc, nschild, TRUE, &node);
779 nsIDOMNode_Release(nschild);
780 if(FAILED(hres))
781 return hres;
783 *p = &node->IHTMLDOMNode_iface;
784 return S_OK;
787 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
789 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
790 nsIDOMNode *nschild = NULL;
791 HTMLDOMNode *node;
792 HRESULT hres;
794 TRACE("(%p)->(%p)\n", This, p);
796 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
797 if(!nschild) {
798 *p = NULL;
799 return S_OK;
802 hres = get_node(This->doc, nschild, TRUE, &node);
803 nsIDOMNode_Release(nschild);
804 if(FAILED(hres))
805 return hres;
807 *p = &node->IHTMLDOMNode_iface;
808 return S_OK;
811 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
813 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
814 nsIDOMNode *nschild = NULL;
815 HTMLDOMNode *node;
816 HRESULT hres;
818 TRACE("(%p)->(%p)\n", This, p);
820 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
821 if(!nschild) {
822 *p = NULL;
823 return S_OK;
826 hres = get_node(This->doc, nschild, TRUE, &node);
827 nsIDOMNode_Release(nschild);
828 if(FAILED(hres))
829 return hres;
831 *p = &node->IHTMLDOMNode_iface;
832 return S_OK;
835 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
837 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
838 nsIDOMNode *nssibling = NULL;
839 HTMLDOMNode *node;
840 HRESULT hres;
842 TRACE("(%p)->(%p)\n", This, p);
844 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
845 if(!nssibling) {
846 *p = NULL;
847 return S_OK;
850 hres = get_node(This->doc, nssibling, TRUE, &node);
851 nsIDOMNode_Release(nssibling);
852 if(FAILED(hres))
853 return hres;
855 *p = &node->IHTMLDOMNode_iface;
856 return S_OK;
859 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
860 HTMLDOMNode_QueryInterface,
861 HTMLDOMNode_AddRef,
862 HTMLDOMNode_Release,
863 HTMLDOMNode_GetTypeInfoCount,
864 HTMLDOMNode_GetTypeInfo,
865 HTMLDOMNode_GetIDsOfNames,
866 HTMLDOMNode_Invoke,
867 HTMLDOMNode_get_nodeType,
868 HTMLDOMNode_get_parentNode,
869 HTMLDOMNode_hasChildNodes,
870 HTMLDOMNode_get_childNodes,
871 HTMLDOMNode_get_attributes,
872 HTMLDOMNode_insertBefore,
873 HTMLDOMNode_removeChild,
874 HTMLDOMNode_replaceChild,
875 HTMLDOMNode_cloneNode,
876 HTMLDOMNode_removeNode,
877 HTMLDOMNode_swapNode,
878 HTMLDOMNode_replaceNode,
879 HTMLDOMNode_appendChild,
880 HTMLDOMNode_get_nodeName,
881 HTMLDOMNode_put_nodeValue,
882 HTMLDOMNode_get_nodeValue,
883 HTMLDOMNode_get_firstChild,
884 HTMLDOMNode_get_lastChild,
885 HTMLDOMNode_get_previousSibling,
886 HTMLDOMNode_get_nextSibling
889 static HTMLDOMNode *get_node_obj(HTMLDocumentNode *This, IUnknown *iface)
891 IHTMLDOMNode *node;
892 HRESULT hres;
894 hres = IUnknown_QueryInterface(iface, &IID_IHTMLDOMNode, (void**)&node);
895 return hres == S_OK && node->lpVtbl == &HTMLDOMNodeVtbl ? impl_from_IHTMLDOMNode(node) : NULL;
898 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
900 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
903 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
904 REFIID riid, void **ppv)
906 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
908 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
911 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
913 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
915 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
918 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
920 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
922 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
925 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
927 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
928 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
931 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
932 LCID lcid, ITypeInfo **ppTInfo)
934 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
935 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
938 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
939 LPOLESTR *rgszNames, UINT cNames,
940 LCID lcid, DISPID *rgDispId)
942 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
943 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
944 lcid, rgDispId);
947 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
948 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
949 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
951 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
952 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
953 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
956 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
958 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
960 TRACE("(%p)->(%p)\n", This, p);
962 /* FIXME: Better check for document node */
963 if(This == &This->doc->node) {
964 *p = NULL;
965 }else {
966 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
967 IDispatch_AddRef(*p);
969 return S_OK;
972 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
973 HTMLDOMNode2_QueryInterface,
974 HTMLDOMNode2_AddRef,
975 HTMLDOMNode2_Release,
976 HTMLDOMNode2_GetTypeInfoCount,
977 HTMLDOMNode2_GetTypeInfo,
978 HTMLDOMNode2_GetIDsOfNames,
979 HTMLDOMNode2_Invoke,
980 HTMLDOMNode2_get_ownerDocument
983 static nsXPCOMCycleCollectionParticipant node_ccp;
985 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
987 *ppv = NULL;
989 if(IsEqualGUID(&IID_IUnknown, riid)) {
990 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
991 *ppv = &This->IHTMLDOMNode_iface;
992 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
993 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
994 *ppv = &This->IHTMLDOMNode_iface;
995 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
996 if(This->dispex.data) {
997 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
998 *ppv = &This->dispex.IDispatchEx_iface;
999 }else {
1000 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1001 return E_NOINTERFACE;
1003 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
1004 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
1005 *ppv = &This->IHTMLDOMNode_iface;
1006 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
1007 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
1008 *ppv = &This->IHTMLDOMNode2_iface;
1009 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant, riid)) {
1010 TRACE("(%p)->(IID_nsXPCOMCycleCollectionParticipant %p)\n", This, ppv);
1011 *ppv = &node_ccp;
1012 return NS_OK;
1013 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1014 TRACE("(%p)->(IID_nsCycleCollectionISupports %p)\n", This, ppv);
1015 *ppv = &This->IHTMLDOMNode_iface;
1016 return NS_OK;
1017 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1018 return *ppv ? S_OK : E_NOINTERFACE;
1021 if(*ppv) {
1022 IUnknown_AddRef((IUnknown*)*ppv);
1023 return S_OK;
1026 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1027 return E_NOINTERFACE;
1030 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1032 if(This->nsnode)
1033 nsIDOMNode_Release(This->nsnode);
1034 if(This->doc && &This->doc->node != This)
1035 htmldoc_release(&This->doc->basedoc);
1036 if(This->event_target)
1037 release_event_target(This->event_target);
1040 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1042 return create_node(This->doc, nsnode, ret);
1045 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1046 HTMLDOMNode_QI,
1047 HTMLDOMNode_destructor,
1048 HTMLDOMNode_clone
1051 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1053 nsresult nsres;
1055 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1056 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1058 ccref_init(&node->ccref, 1);
1060 if(&doc->node != node)
1061 htmldoc_addref(&doc->basedoc);
1062 node->doc = doc;
1064 if(nsnode)
1065 nsIDOMNode_AddRef(nsnode);
1066 node->nsnode = nsnode;
1068 nsres = nsIDOMNode_SetMshtmlNode(nsnode, (nsISupports*)&node->IHTMLDOMNode_iface);
1069 assert(nsres == NS_OK);
1072 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1074 PRUint16 node_type;
1075 HRESULT hres;
1077 nsIDOMNode_GetNodeType(nsnode, &node_type);
1079 switch(node_type) {
1080 case ELEMENT_NODE: {
1081 HTMLElement *elem;
1082 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1083 if(FAILED(hres))
1084 return hres;
1085 *ret = &elem->node;
1086 break;
1088 case TEXT_NODE:
1089 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1090 if(FAILED(hres))
1091 return hres;
1092 break;
1093 case COMMENT_NODE: {
1094 HTMLElement *comment;
1095 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1096 if(FAILED(hres))
1097 return hres;
1098 *ret = &comment->node;
1099 break;
1101 case ATTRIBUTE_NODE:
1102 ERR("Called on attribute node\n");
1103 return E_UNEXPECTED;
1104 default: {
1105 HTMLDOMNode *node;
1107 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1108 if(!node)
1109 return E_OUTOFMEMORY;
1111 node->vtbl = &HTMLDOMNodeImplVtbl;
1112 HTMLDOMNode_Init(doc, node, nsnode);
1113 *ret = node;
1117 TRACE("type %d ret %p\n", node_type, *ret);
1118 return S_OK;
1121 static void NSAPI HTMLDOMNode_unmark_if_purple(void *p)
1123 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1124 ccref_unmark_if_purple(&This->ccref);
1127 static nsresult NSAPI HTMLDOMNode_traverse(void *ccp, void *p, nsCycleCollectionTraversalCallback *cb)
1129 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1131 TRACE("%p\n", This);
1133 describe_cc_node(&This->ccref, sizeof(*This), "HTMLDOMNode", cb);
1135 if(This->nsnode)
1136 note_cc_edge((nsISupports*)This->nsnode, "This->nsnode", cb);
1137 if(This->doc && &This->doc->node != This)
1138 note_cc_edge((nsISupports*)&This->doc->node.IHTMLDOMNode_iface, "This->doc", cb);
1139 dispex_traverse(&This->dispex, cb);
1141 if(This->vtbl->traverse)
1142 This->vtbl->traverse(This, cb);
1144 return NS_OK;
1147 static nsresult NSAPI HTMLDOMNode_unlink(void *p)
1149 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1151 TRACE("%p\n", This);
1153 if(This->vtbl->unlink)
1154 This->vtbl->unlink(This);
1156 dispex_unlink(&This->dispex);
1158 if(This->nsnode) {
1159 nsIDOMNode *nsnode = This->nsnode;
1160 This->nsnode = NULL;
1161 nsIDOMNode_Release(nsnode);
1164 if(This->doc && &This->doc->node != This) {
1165 HTMLDocument *doc = &This->doc->basedoc;
1166 This->doc = NULL;
1167 htmldoc_release(doc);
1168 }else {
1169 This->doc = NULL;
1172 return NS_OK;
1175 void init_node_cc(void)
1177 static const CCObjCallback node_ccp_callback = {
1178 HTMLDOMNode_unmark_if_purple,
1179 HTMLDOMNode_traverse,
1180 HTMLDOMNode_unlink
1183 ccp_init(&node_ccp, &node_ccp_callback);
1186 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1188 nsISupports *unk = NULL;
1189 nsresult nsres;
1191 nsres = nsIDOMNode_GetMshtmlNode(nsnode, &unk);
1192 assert(nsres == NS_OK);
1194 if(unk) {
1195 *ret = get_node_obj(This, (IUnknown*)unk);
1196 return NS_OK;
1199 if(!create) {
1200 *ret = NULL;
1201 return S_OK;
1204 return create_node(This, nsnode, ret);