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
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(IHTMLDOMNode
*);
37 static HRESULT
create_node(HTMLDocumentNode
*,nsIDOMNode
*,HTMLDOMNode
**);
41 IHTMLDOMChildrenCollection IHTMLDOMChildrenCollection_iface
;
45 /* FIXME: implement weak reference */
46 HTMLDocumentNode
*doc
;
48 nsIDOMNodeList
*nslist
;
49 } HTMLDOMChildrenCollection
;
52 IEnumVARIANT IEnumVARIANT_iface
;
57 HTMLDOMChildrenCollection
*col
;
58 } HTMLDOMChildrenCollectionEnum
;
60 static inline HTMLDOMChildrenCollectionEnum
*impl_from_IEnumVARIANT(IEnumVARIANT
*iface
)
62 return CONTAINING_RECORD(iface
, HTMLDOMChildrenCollectionEnum
, IEnumVARIANT_iface
);
65 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_QueryInterface(IEnumVARIANT
*iface
, REFIID riid
, void **ppv
)
67 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
69 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
71 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
72 *ppv
= &This
->IEnumVARIANT_iface
;
73 }else if(IsEqualGUID(riid
, &IID_IEnumVARIANT
)) {
74 *ppv
= &This
->IEnumVARIANT_iface
;
76 FIXME("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
81 IUnknown_AddRef((IUnknown
*)*ppv
);
85 static ULONG WINAPI
HTMLDOMChildrenCollectionEnum_AddRef(IEnumVARIANT
*iface
)
87 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
88 LONG ref
= InterlockedIncrement(&This
->ref
);
90 TRACE("(%p) ref=%d\n", This
, ref
);
95 static ULONG WINAPI
HTMLDOMChildrenCollectionEnum_Release(IEnumVARIANT
*iface
)
97 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
98 LONG ref
= InterlockedDecrement(&This
->ref
);
100 TRACE("(%p) ref=%d\n", This
, ref
);
103 IHTMLDOMChildrenCollection_Release(&This
->col
->IHTMLDOMChildrenCollection_iface
);
110 static ULONG
get_enum_len(HTMLDOMChildrenCollectionEnum
*This
)
115 nsres
= nsIDOMNodeList_GetLength(This
->col
->nslist
, &len
);
116 assert(nsres
== NS_OK
);
120 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Next(IEnumVARIANT
*iface
, ULONG celt
, VARIANT
*rgVar
, ULONG
*pCeltFetched
)
122 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
123 ULONG fetched
= 0, len
;
129 TRACE("(%p)->(%d %p %p)\n", This
, celt
, rgVar
, pCeltFetched
);
131 len
= get_enum_len(This
);
133 while(This
->iter
+fetched
< len
&& fetched
< celt
) {
134 nsres
= nsIDOMNodeList_Item(This
->col
->nslist
, This
->iter
+fetched
, &nsnode
);
135 assert(nsres
== NS_OK
);
137 hres
= get_node(This
->col
->doc
, nsnode
, TRUE
, &node
);
138 nsIDOMNode_Release(nsnode
);
140 ERR("get_node failed: %08x\n", hres
);
144 V_VT(rgVar
+fetched
) = VT_DISPATCH
;
145 IHTMLDOMNode_AddRef(&node
->IHTMLDOMNode_iface
);
146 V_DISPATCH(rgVar
+fetched
) = (IDispatch
*)&node
->IHTMLDOMNode_iface
;
150 This
->iter
+= fetched
;
152 *pCeltFetched
= fetched
;
153 return fetched
== celt
? S_OK
: S_FALSE
;
156 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Skip(IEnumVARIANT
*iface
, ULONG celt
)
158 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
161 TRACE("(%p)->(%d)\n", This
, celt
);
163 len
= get_enum_len(This
);
164 if(This
->iter
+ celt
> len
) {
173 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Reset(IEnumVARIANT
*iface
)
175 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
177 TRACE("(%p)->()\n", This
);
183 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Clone(IEnumVARIANT
*iface
, IEnumVARIANT
**ppEnum
)
185 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
186 FIXME("(%p)->(%p)\n", This
, ppEnum
);
190 static const IEnumVARIANTVtbl HTMLDOMChildrenCollectionEnumVtbl
= {
191 HTMLDOMChildrenCollectionEnum_QueryInterface
,
192 HTMLDOMChildrenCollectionEnum_AddRef
,
193 HTMLDOMChildrenCollectionEnum_Release
,
194 HTMLDOMChildrenCollectionEnum_Next
,
195 HTMLDOMChildrenCollectionEnum_Skip
,
196 HTMLDOMChildrenCollectionEnum_Reset
,
197 HTMLDOMChildrenCollectionEnum_Clone
200 static inline HTMLDOMChildrenCollection
*impl_from_IHTMLDOMChildrenCollection(IHTMLDOMChildrenCollection
*iface
)
202 return CONTAINING_RECORD(iface
, HTMLDOMChildrenCollection
, IHTMLDOMChildrenCollection_iface
);
205 static HRESULT WINAPI
HTMLDOMChildrenCollection_QueryInterface(IHTMLDOMChildrenCollection
*iface
, REFIID riid
, void **ppv
)
207 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
209 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
211 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
212 *ppv
= &This
->IHTMLDOMChildrenCollection_iface
;
213 }else if(IsEqualGUID(&IID_IHTMLDOMChildrenCollection
, riid
)) {
214 *ppv
= &This
->IHTMLDOMChildrenCollection_iface
;
215 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
216 return *ppv
? S_OK
: E_NOINTERFACE
;
219 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
220 return E_NOINTERFACE
;
223 IUnknown_AddRef((IUnknown
*)*ppv
);
227 static ULONG WINAPI
HTMLDOMChildrenCollection_AddRef(IHTMLDOMChildrenCollection
*iface
)
229 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
230 LONG ref
= InterlockedIncrement(&This
->ref
);
232 TRACE("(%p) ref=%d\n", This
, ref
);
237 static ULONG WINAPI
HTMLDOMChildrenCollection_Release(IHTMLDOMChildrenCollection
*iface
)
239 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
240 LONG ref
= InterlockedDecrement(&This
->ref
);
242 TRACE("(%p) ref=%d\n", This
, ref
);
245 htmldoc_release(&This
->doc
->basedoc
);
246 nsIDOMNodeList_Release(This
->nslist
);
253 static HRESULT WINAPI
HTMLDOMChildrenCollection_GetTypeInfoCount(IHTMLDOMChildrenCollection
*iface
, UINT
*pctinfo
)
255 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
256 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
259 static HRESULT WINAPI
HTMLDOMChildrenCollection_GetTypeInfo(IHTMLDOMChildrenCollection
*iface
, UINT iTInfo
,
260 LCID lcid
, ITypeInfo
**ppTInfo
)
262 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
263 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
266 static HRESULT WINAPI
HTMLDOMChildrenCollection_GetIDsOfNames(IHTMLDOMChildrenCollection
*iface
, REFIID riid
,
267 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
269 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
270 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
274 static HRESULT WINAPI
HTMLDOMChildrenCollection_Invoke(IHTMLDOMChildrenCollection
*iface
, DISPID dispIdMember
,
275 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
276 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
278 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
279 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
280 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
283 static HRESULT WINAPI
HTMLDOMChildrenCollection_get_length(IHTMLDOMChildrenCollection
*iface
, LONG
*p
)
285 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
288 TRACE("(%p)->(%p)\n", This
, p
);
290 nsIDOMNodeList_GetLength(This
->nslist
, &length
);
295 static HRESULT WINAPI
HTMLDOMChildrenCollection_get__newEnum(IHTMLDOMChildrenCollection
*iface
, IUnknown
**p
)
297 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
298 HTMLDOMChildrenCollectionEnum
*ret
;
300 TRACE("(%p)->(%p)\n", This
, p
);
302 ret
= heap_alloc(sizeof(*ret
));
304 return E_OUTOFMEMORY
;
306 ret
->IEnumVARIANT_iface
.lpVtbl
= &HTMLDOMChildrenCollectionEnumVtbl
;
310 IHTMLDOMChildrenCollection_AddRef(&This
->IHTMLDOMChildrenCollection_iface
);
313 *p
= (IUnknown
*)&ret
->IEnumVARIANT_iface
;
317 static HRESULT WINAPI
HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection
*iface
, LONG index
, IDispatch
**ppItem
)
319 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
320 nsIDOMNode
*nsnode
= NULL
;
326 TRACE("(%p)->(%d %p)\n", This
, index
, ppItem
);
333 nsIDOMNodeList_GetLength(This
->nslist
, &length
);
334 if(index
< 0 || index
>= length
)
337 nsres
= nsIDOMNodeList_Item(This
->nslist
, index
, &nsnode
);
338 if(NS_FAILED(nsres
) || !nsnode
) {
339 ERR("Item failed: %08x\n", nsres
);
343 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node
);
347 *ppItem
= (IDispatch
*)&node
->IHTMLDOMNode_iface
;
351 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl
= {
352 HTMLDOMChildrenCollection_QueryInterface
,
353 HTMLDOMChildrenCollection_AddRef
,
354 HTMLDOMChildrenCollection_Release
,
355 HTMLDOMChildrenCollection_GetTypeInfoCount
,
356 HTMLDOMChildrenCollection_GetTypeInfo
,
357 HTMLDOMChildrenCollection_GetIDsOfNames
,
358 HTMLDOMChildrenCollection_Invoke
,
359 HTMLDOMChildrenCollection_get_length
,
360 HTMLDOMChildrenCollection_get__newEnum
,
361 HTMLDOMChildrenCollection_item
364 static inline HTMLDOMChildrenCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
366 return CONTAINING_RECORD(iface
, HTMLDOMChildrenCollection
, dispex
);
369 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
371 static HRESULT
HTMLDOMChildrenCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
373 HTMLDOMChildrenCollection
*This
= impl_from_DispatchEx(dispex
);
378 for(ptr
= name
; *ptr
&& isdigitW(*ptr
); ptr
++)
379 idx
= idx
*10 + (*ptr
-'0');
381 return DISP_E_UNKNOWNNAME
;
383 nsIDOMNodeList_GetLength(This
->nslist
, &len
);
385 return DISP_E_UNKNOWNNAME
;
387 *dispid
= DISPID_CHILDCOL_0
+ idx
;
388 TRACE("ret %x\n", *dispid
);
392 static HRESULT
HTMLDOMChildrenCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
393 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
395 HTMLDOMChildrenCollection
*This
= impl_from_DispatchEx(dispex
);
397 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
400 case DISPATCH_PROPERTYGET
: {
401 IDispatch
*disp
= NULL
;
404 hres
= IHTMLDOMChildrenCollection_item(&This
->IHTMLDOMChildrenCollection_iface
,
405 id
- DISPID_CHILDCOL_0
, &disp
);
409 V_VT(res
) = VT_DISPATCH
;
410 V_DISPATCH(res
) = disp
;
415 FIXME("unimplemented flags %x\n", flags
);
422 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl
= {
424 HTMLDOMChildrenCollection_get_dispid
,
425 HTMLDOMChildrenCollection_invoke
,
429 static const tid_t HTMLDOMChildrenCollection_iface_tids
[] = {
430 IHTMLDOMChildrenCollection_tid
,
434 static dispex_static_data_t HTMLDOMChildrenCollection_dispex
= {
435 &HTMLDOMChildrenCollection_dispex_vtbl
,
436 DispDOMChildrenCollection_tid
,
438 HTMLDOMChildrenCollection_iface_tids
441 static IHTMLDOMChildrenCollection
*create_child_collection(HTMLDocumentNode
*doc
, nsIDOMNodeList
*nslist
)
443 HTMLDOMChildrenCollection
*ret
;
445 ret
= heap_alloc_zero(sizeof(*ret
));
449 ret
->IHTMLDOMChildrenCollection_iface
.lpVtbl
= &HTMLDOMChildrenCollectionVtbl
;
452 nsIDOMNodeList_AddRef(nslist
);
453 ret
->nslist
= nslist
;
455 htmldoc_addref(&doc
->basedoc
);
458 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLDOMChildrenCollection_iface
,
459 &HTMLDOMChildrenCollection_dispex
);
461 return &ret
->IHTMLDOMChildrenCollection_iface
;
464 static inline HTMLDOMNode
*impl_from_IHTMLDOMNode(IHTMLDOMNode
*iface
)
466 return CONTAINING_RECORD(iface
, HTMLDOMNode
, IHTMLDOMNode_iface
);
469 static HRESULT WINAPI
HTMLDOMNode_QueryInterface(IHTMLDOMNode
*iface
,
470 REFIID riid
, void **ppv
)
472 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
474 return This
->vtbl
->qi(This
, riid
, ppv
);
477 static ULONG WINAPI
HTMLDOMNode_AddRef(IHTMLDOMNode
*iface
)
479 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
480 LONG ref
= ccref_incr(&This
->ccref
, (nsISupports
*)&This
->IHTMLDOMNode_iface
);
482 TRACE("(%p) ref=%d\n", This
, ref
);
487 static ULONG WINAPI
HTMLDOMNode_Release(IHTMLDOMNode
*iface
)
489 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
490 LONG ref
= ccref_decr(&This
->ccref
, (nsISupports
*)&This
->IHTMLDOMNode_iface
);
492 TRACE("(%p) ref=%d\n", This
, ref
);
495 if(This
->vtbl
->unlink
)
496 This
->vtbl
->unlink(This
);
497 This
->vtbl
->destructor(This
);
498 release_dispex(&This
->dispex
);
505 static HRESULT WINAPI
HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode
*iface
, UINT
*pctinfo
)
507 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
508 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
511 static HRESULT WINAPI
HTMLDOMNode_GetTypeInfo(IHTMLDOMNode
*iface
, UINT iTInfo
,
512 LCID lcid
, ITypeInfo
**ppTInfo
)
514 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
515 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
518 static HRESULT WINAPI
HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode
*iface
, REFIID riid
,
519 LPOLESTR
*rgszNames
, UINT cNames
,
520 LCID lcid
, DISPID
*rgDispId
)
522 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
523 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
527 static HRESULT WINAPI
HTMLDOMNode_Invoke(IHTMLDOMNode
*iface
, DISPID dispIdMember
,
528 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
529 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
531 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
532 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
533 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
536 static HRESULT WINAPI
HTMLDOMNode_get_nodeType(IHTMLDOMNode
*iface
, LONG
*p
)
538 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
541 TRACE("(%p)->(%p)\n", This
, p
);
543 nsIDOMNode_GetNodeType(This
->nsnode
, &type
);
553 case DOCUMENT_TYPE_NODE
:
559 case DOCUMENT_FRAGMENT_NODE
:
565 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
566 * It needs more tests.
568 FIXME("type %u\n", type
);
575 static HRESULT WINAPI
HTMLDOMNode_get_parentNode(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
577 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
583 TRACE("(%p)->(%p)\n", This
, p
);
585 nsres
= nsIDOMNode_GetParentNode(This
->nsnode
, &nsnode
);
586 if(NS_FAILED(nsres
)) {
587 ERR("GetParentNode failed: %08x\n", nsres
);
596 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node
);
597 nsIDOMNode_Release(nsnode
);
601 *p
= &node
->IHTMLDOMNode_iface
;
605 static HRESULT WINAPI
HTMLDOMNode_hasChildNodes(IHTMLDOMNode
*iface
, VARIANT_BOOL
*fChildren
)
607 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
608 cpp_bool has_child
= FALSE
;
611 TRACE("(%p)->(%p)\n", This
, fChildren
);
613 nsres
= nsIDOMNode_HasChildNodes(This
->nsnode
, &has_child
);
615 ERR("HasChildNodes failed: %08x\n", nsres
);
617 *fChildren
= has_child
? VARIANT_TRUE
: VARIANT_FALSE
;
621 static HRESULT WINAPI
HTMLDOMNode_get_childNodes(IHTMLDOMNode
*iface
, IDispatch
**p
)
623 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
624 nsIDOMNodeList
*nslist
;
627 TRACE("(%p)->(%p)\n", This
, p
);
629 nsres
= nsIDOMNode_GetChildNodes(This
->nsnode
, &nslist
);
630 if(NS_FAILED(nsres
)) {
631 ERR("GetChildNodes failed: %08x\n", nsres
);
635 *p
= (IDispatch
*)create_child_collection(This
->doc
, nslist
);
636 nsIDOMNodeList_Release(nslist
);
638 return *p
? S_OK
: E_OUTOFMEMORY
;
641 static HRESULT WINAPI
HTMLDOMNode_get_attributes(IHTMLDOMNode
*iface
, IDispatch
**p
)
643 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
644 HTMLAttributeCollection
*col
;
647 TRACE("(%p)->(%p)\n", This
, p
);
649 if(This
->vtbl
->get_attr_col
) {
650 hres
= This
->vtbl
->get_attr_col(This
, &col
);
654 *p
= (IDispatch
*)&col
->IHTMLAttributeCollection_iface
;
662 static HRESULT WINAPI
HTMLDOMNode_insertBefore(IHTMLDOMNode
*iface
, IHTMLDOMNode
*newChild
,
663 VARIANT refChild
, IHTMLDOMNode
**node
)
665 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
666 HTMLDOMNode
*new_child
, *node_obj
, *ref_node
= NULL
;
671 TRACE("(%p)->(%p %s %p)\n", This
, newChild
, debugstr_variant(&refChild
), node
);
673 new_child
= get_node_obj(newChild
);
675 ERR("invalid newChild\n");
679 switch(V_VT(&refChild
)) {
683 IHTMLDOMNode
*ref_iface
;
685 if(!V_DISPATCH(&refChild
))
688 hres
= IDispatch_QueryInterface(V_DISPATCH(&refChild
), &IID_IHTMLDOMNode
, (void**)&ref_iface
);
692 ref_node
= get_node_obj(ref_iface
);
693 IHTMLDOMNode_Release(ref_iface
);
695 ERR("unvalid node\n");
702 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild
));
706 if(SUCCEEDED(hres
)) {
707 nsres
= nsIDOMNode_InsertBefore(This
->nsnode
, new_child
->nsnode
, ref_node
? ref_node
->nsnode
: NULL
, &nsnode
);
708 if(NS_FAILED(nsres
)) {
709 ERR("InsertBefore failed: %08x\n", nsres
);
713 node_release(new_child
);
715 node_release(ref_node
);
719 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node_obj
);
720 nsIDOMNode_Release(nsnode
);
724 *node
= &node_obj
->IHTMLDOMNode_iface
;
728 static HRESULT WINAPI
HTMLDOMNode_removeChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
*oldChild
,
731 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
732 HTMLDOMNode
*node_obj
;
737 TRACE("(%p)->(%p %p)\n", This
, oldChild
, node
);
739 node_obj
= get_node_obj(oldChild
);
743 nsres
= nsIDOMNode_RemoveChild(This
->nsnode
, node_obj
->nsnode
, &nsnode
);
744 node_release(node_obj
);
745 if(NS_FAILED(nsres
)) {
746 ERR("RemoveChild failed: %08x\n", nsres
);
750 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node_obj
);
751 nsIDOMNode_Release(nsnode
);
755 /* FIXME: Make sure that node != newChild */
756 *node
= &node_obj
->IHTMLDOMNode_iface
;
760 static HRESULT WINAPI
HTMLDOMNode_replaceChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
*newChild
,
761 IHTMLDOMNode
*oldChild
, IHTMLDOMNode
**node
)
763 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
764 HTMLDOMNode
*node_new
, *node_old
, *ret_node
;
769 TRACE("(%p)->(%p %p %p)\n", This
, newChild
, oldChild
, node
);
771 node_new
= get_node_obj(newChild
);
775 node_old
= get_node_obj(oldChild
);
777 node_release(node_new
);
781 nsres
= nsIDOMNode_ReplaceChild(This
->nsnode
, node_new
->nsnode
, node_old
->nsnode
, &nsnode
);
782 node_release(node_new
);
783 node_release(node_old
);
787 hres
= get_node(This
->doc
, nsnode
, TRUE
, &ret_node
);
788 nsIDOMNode_Release(nsnode
);
792 *node
= &ret_node
->IHTMLDOMNode_iface
;
796 static HRESULT WINAPI
HTMLDOMNode_cloneNode(IHTMLDOMNode
*iface
, VARIANT_BOOL fDeep
,
797 IHTMLDOMNode
**clonedNode
)
799 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
800 HTMLDOMNode
*new_node
;
805 TRACE("(%p)->(%x %p)\n", This
, fDeep
, clonedNode
);
807 nsres
= nsIDOMNode_CloneNode(This
->nsnode
, fDeep
!= VARIANT_FALSE
, 1, &nsnode
);
808 if(NS_FAILED(nsres
) || !nsnode
) {
809 ERR("CloneNode failed: %08x\n", nsres
);
813 hres
= This
->vtbl
->clone(This
, nsnode
, &new_node
);
817 *clonedNode
= &new_node
->IHTMLDOMNode_iface
;
821 static HRESULT WINAPI
HTMLDOMNode_removeNode(IHTMLDOMNode
*iface
, VARIANT_BOOL fDeep
,
822 IHTMLDOMNode
**removed
)
824 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
825 FIXME("(%p)->(%x %p)\n", This
, fDeep
, removed
);
829 static HRESULT WINAPI
HTMLDOMNode_swapNode(IHTMLDOMNode
*iface
, IHTMLDOMNode
*otherNode
,
830 IHTMLDOMNode
**swappedNode
)
832 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
833 FIXME("(%p)->(%p %p)\n", This
, otherNode
, swappedNode
);
837 static HRESULT WINAPI
HTMLDOMNode_replaceNode(IHTMLDOMNode
*iface
, IHTMLDOMNode
*replacement
,
838 IHTMLDOMNode
**replaced
)
840 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
841 FIXME("(%p)->(%p %p)\n", This
, replacement
, replaced
);
845 static HRESULT WINAPI
HTMLDOMNode_appendChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
*newChild
,
848 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
849 HTMLDOMNode
*node_obj
;
854 TRACE("(%p)->(%p %p)\n", This
, newChild
, node
);
856 node_obj
= get_node_obj(newChild
);
860 nsres
= nsIDOMNode_AppendChild(This
->nsnode
, node_obj
->nsnode
, &nsnode
);
861 node_release(node_obj
);
862 if(NS_FAILED(nsres
)) {
863 ERR("AppendChild failed: %08x\n", nsres
);
867 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node_obj
);
868 nsIDOMNode_Release(nsnode
);
872 /* FIXME: Make sure that node != newChild */
873 *node
= &node_obj
->IHTMLDOMNode_iface
;
877 static HRESULT WINAPI
HTMLDOMNode_get_nodeName(IHTMLDOMNode
*iface
, BSTR
*p
)
879 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
883 TRACE("(%p)->(%p)\n", This
, p
);
885 nsAString_Init(&name
, NULL
);
886 nsres
= nsIDOMNode_GetNodeName(This
->nsnode
, &name
);
887 return return_nsstr(nsres
, &name
, p
);
890 static HRESULT WINAPI
HTMLDOMNode_put_nodeValue(IHTMLDOMNode
*iface
, VARIANT v
)
892 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
894 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
900 nsAString_InitDepend(&val_str
, V_BSTR(&v
));
901 nsIDOMNode_SetNodeValue(This
->nsnode
, &val_str
);
902 nsAString_Finish(&val_str
);
908 FIXME("unsupported value %s\n", debugstr_variant(&v
));
914 static HRESULT WINAPI
HTMLDOMNode_get_nodeValue(IHTMLDOMNode
*iface
, VARIANT
*p
)
916 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
917 const PRUnichar
*val
;
920 TRACE("(%p)->(%p)\n", This
, p
);
922 nsAString_Init(&val_str
, NULL
);
923 nsIDOMNode_GetNodeValue(This
->nsnode
, &val_str
);
924 nsAString_GetData(&val_str
, &val
);
928 V_BSTR(p
) = SysAllocString(val
);
933 nsAString_Finish(&val_str
);
938 static HRESULT WINAPI
HTMLDOMNode_get_firstChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
940 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
941 nsIDOMNode
*nschild
= NULL
;
945 TRACE("(%p)->(%p)\n", This
, p
);
947 nsIDOMNode_GetFirstChild(This
->nsnode
, &nschild
);
953 hres
= get_node(This
->doc
, nschild
, TRUE
, &node
);
954 nsIDOMNode_Release(nschild
);
958 *p
= &node
->IHTMLDOMNode_iface
;
962 static HRESULT WINAPI
HTMLDOMNode_get_lastChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
964 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
965 nsIDOMNode
*nschild
= NULL
;
969 TRACE("(%p)->(%p)\n", This
, p
);
971 nsIDOMNode_GetLastChild(This
->nsnode
, &nschild
);
977 hres
= get_node(This
->doc
, nschild
, TRUE
, &node
);
978 nsIDOMNode_Release(nschild
);
982 *p
= &node
->IHTMLDOMNode_iface
;
986 static HRESULT WINAPI
HTMLDOMNode_get_previousSibling(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
988 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
989 nsIDOMNode
*nschild
= NULL
;
993 TRACE("(%p)->(%p)\n", This
, p
);
995 nsIDOMNode_GetPreviousSibling(This
->nsnode
, &nschild
);
1001 hres
= get_node(This
->doc
, nschild
, TRUE
, &node
);
1002 nsIDOMNode_Release(nschild
);
1006 *p
= &node
->IHTMLDOMNode_iface
;
1010 static HRESULT WINAPI
HTMLDOMNode_get_nextSibling(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
1012 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
1013 nsIDOMNode
*nssibling
= NULL
;
1017 TRACE("(%p)->(%p)\n", This
, p
);
1019 nsIDOMNode_GetNextSibling(This
->nsnode
, &nssibling
);
1025 hres
= get_node(This
->doc
, nssibling
, TRUE
, &node
);
1026 nsIDOMNode_Release(nssibling
);
1030 *p
= &node
->IHTMLDOMNode_iface
;
1034 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl
= {
1035 HTMLDOMNode_QueryInterface
,
1037 HTMLDOMNode_Release
,
1038 HTMLDOMNode_GetTypeInfoCount
,
1039 HTMLDOMNode_GetTypeInfo
,
1040 HTMLDOMNode_GetIDsOfNames
,
1042 HTMLDOMNode_get_nodeType
,
1043 HTMLDOMNode_get_parentNode
,
1044 HTMLDOMNode_hasChildNodes
,
1045 HTMLDOMNode_get_childNodes
,
1046 HTMLDOMNode_get_attributes
,
1047 HTMLDOMNode_insertBefore
,
1048 HTMLDOMNode_removeChild
,
1049 HTMLDOMNode_replaceChild
,
1050 HTMLDOMNode_cloneNode
,
1051 HTMLDOMNode_removeNode
,
1052 HTMLDOMNode_swapNode
,
1053 HTMLDOMNode_replaceNode
,
1054 HTMLDOMNode_appendChild
,
1055 HTMLDOMNode_get_nodeName
,
1056 HTMLDOMNode_put_nodeValue
,
1057 HTMLDOMNode_get_nodeValue
,
1058 HTMLDOMNode_get_firstChild
,
1059 HTMLDOMNode_get_lastChild
,
1060 HTMLDOMNode_get_previousSibling
,
1061 HTMLDOMNode_get_nextSibling
1064 static HTMLDOMNode
*get_node_obj(IHTMLDOMNode
*iface
)
1068 if(iface
->lpVtbl
!= &HTMLDOMNodeVtbl
)
1071 ret
= impl_from_IHTMLDOMNode(iface
);
1076 static inline HTMLDOMNode
*impl_from_IHTMLDOMNode2(IHTMLDOMNode2
*iface
)
1078 return CONTAINING_RECORD(iface
, HTMLDOMNode
, IHTMLDOMNode2_iface
);
1081 static HRESULT WINAPI
HTMLDOMNode2_QueryInterface(IHTMLDOMNode2
*iface
,
1082 REFIID riid
, void **ppv
)
1084 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1086 return IHTMLDOMNode_QueryInterface(&This
->IHTMLDOMNode_iface
, riid
, ppv
);
1089 static ULONG WINAPI
HTMLDOMNode2_AddRef(IHTMLDOMNode2
*iface
)
1091 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1093 return IHTMLDOMNode_AddRef(&This
->IHTMLDOMNode_iface
);
1096 static ULONG WINAPI
HTMLDOMNode2_Release(IHTMLDOMNode2
*iface
)
1098 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1100 return IHTMLDOMNode_Release(&This
->IHTMLDOMNode_iface
);
1103 static HRESULT WINAPI
HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2
*iface
, UINT
*pctinfo
)
1105 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1106 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
1109 static HRESULT WINAPI
HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2
*iface
, UINT iTInfo
,
1110 LCID lcid
, ITypeInfo
**ppTInfo
)
1112 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1113 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
1116 static HRESULT WINAPI
HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2
*iface
, REFIID riid
,
1117 LPOLESTR
*rgszNames
, UINT cNames
,
1118 LCID lcid
, DISPID
*rgDispId
)
1120 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1121 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
1125 static HRESULT WINAPI
HTMLDOMNode2_Invoke(IHTMLDOMNode2
*iface
, DISPID dispIdMember
,
1126 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
1127 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1129 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1130 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
1131 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
1134 static HRESULT WINAPI
HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2
*iface
, IDispatch
**p
)
1136 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1138 TRACE("(%p)->(%p)\n", This
, p
);
1140 /* FIXME: Better check for document node */
1141 if(This
== &This
->doc
->node
) {
1144 *p
= (IDispatch
*)&This
->doc
->basedoc
.IHTMLDocument2_iface
;
1145 IDispatch_AddRef(*p
);
1150 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl
= {
1151 HTMLDOMNode2_QueryInterface
,
1152 HTMLDOMNode2_AddRef
,
1153 HTMLDOMNode2_Release
,
1154 HTMLDOMNode2_GetTypeInfoCount
,
1155 HTMLDOMNode2_GetTypeInfo
,
1156 HTMLDOMNode2_GetIDsOfNames
,
1157 HTMLDOMNode2_Invoke
,
1158 HTMLDOMNode2_get_ownerDocument
1161 static nsXPCOMCycleCollectionParticipant node_ccp
;
1163 HRESULT
HTMLDOMNode_QI(HTMLDOMNode
*This
, REFIID riid
, void **ppv
)
1165 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
1167 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
1168 *ppv
= &This
->IHTMLDOMNode_iface
;
1169 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
1170 *ppv
= &This
->IHTMLDOMNode_iface
;
1171 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
) && This
->dispex
.data
) {
1172 *ppv
= &This
->dispex
.IDispatchEx_iface
;
1173 }else if(IsEqualGUID(&IID_IHTMLDOMNode
, riid
)) {
1174 *ppv
= &This
->IHTMLDOMNode_iface
;
1175 }else if(IsEqualGUID(&IID_IHTMLDOMNode2
, riid
)) {
1176 *ppv
= &This
->IHTMLDOMNode2_iface
;
1177 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant
, riid
)) {
1180 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports
, riid
)) {
1181 *ppv
= &This
->IHTMLDOMNode_iface
;
1183 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
1184 return *ppv
? S_OK
: E_NOINTERFACE
;
1187 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
1188 return E_NOINTERFACE
;
1191 IUnknown_AddRef((IUnknown
*)*ppv
);
1195 void HTMLDOMNode_destructor(HTMLDOMNode
*This
)
1198 nsIDOMNode_Release(This
->nsnode
);
1199 if(This
->doc
&& &This
->doc
->node
!= This
)
1200 htmldoc_release(&This
->doc
->basedoc
);
1201 if(This
->event_target
)
1202 release_event_target(This
->event_target
);
1205 static HRESULT
HTMLDOMNode_clone(HTMLDOMNode
*This
, nsIDOMNode
*nsnode
, HTMLDOMNode
**ret
)
1207 return create_node(This
->doc
, nsnode
, ret
);
1210 static const cpc_entry_t HTMLDOMNode_cpc
[] = {{NULL
}};
1212 static const NodeImplVtbl HTMLDOMNodeImplVtbl
= {
1214 HTMLDOMNode_destructor
,
1219 void HTMLDOMNode_Init(HTMLDocumentNode
*doc
, HTMLDOMNode
*node
, nsIDOMNode
*nsnode
)
1223 node
->IHTMLDOMNode_iface
.lpVtbl
= &HTMLDOMNodeVtbl
;
1224 node
->IHTMLDOMNode2_iface
.lpVtbl
= &HTMLDOMNode2Vtbl
;
1226 ccref_init(&node
->ccref
, 1);
1228 if(&doc
->node
!= node
)
1229 htmldoc_addref(&doc
->basedoc
);
1232 nsIDOMNode_AddRef(nsnode
);
1233 node
->nsnode
= nsnode
;
1235 nsres
= nsIDOMNode_SetMshtmlNode(nsnode
, (nsISupports
*)&node
->IHTMLDOMNode_iface
);
1236 assert(nsres
== NS_OK
);
1239 static HRESULT
create_node(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, HTMLDOMNode
**ret
)
1244 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1247 case ELEMENT_NODE
: {
1249 hres
= HTMLElement_Create(doc
, nsnode
, FALSE
, &elem
);
1256 hres
= HTMLDOMTextNode_Create(doc
, nsnode
, ret
);
1260 /* doctype nodes are represented as comment nodes (at least in quirks mode) */
1261 case DOCUMENT_TYPE_NODE
:
1262 case COMMENT_NODE
: {
1263 HTMLElement
*comment
;
1264 hres
= HTMLCommentElement_Create(doc
, nsnode
, &comment
);
1267 *ret
= &comment
->node
;
1270 case ATTRIBUTE_NODE
:
1271 ERR("Called on attribute node\n");
1272 return E_UNEXPECTED
;
1276 node
= heap_alloc_zero(sizeof(HTMLDOMNode
));
1278 return E_OUTOFMEMORY
;
1280 node
->vtbl
= &HTMLDOMNodeImplVtbl
;
1281 HTMLDOMNode_Init(doc
, node
, nsnode
);
1286 TRACE("type %d ret %p\n", node_type
, *ret
);
1290 static void NSAPI
HTMLDOMNode_unmark_if_purple(void *p
)
1292 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(p
);
1293 ccref_unmark_if_purple(&This
->ccref
);
1296 static nsresult NSAPI
HTMLDOMNode_traverse(void *ccp
, void *p
, nsCycleCollectionTraversalCallback
*cb
)
1298 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(p
);
1300 TRACE("%p\n", This
);
1302 describe_cc_node(&This
->ccref
, "HTMLDOMNode", cb
);
1305 note_cc_edge((nsISupports
*)This
->nsnode
, "This->nsnode", cb
);
1306 if(This
->doc
&& &This
->doc
->node
!= This
)
1307 note_cc_edge((nsISupports
*)&This
->doc
->node
.IHTMLDOMNode_iface
, "This->doc", cb
);
1308 dispex_traverse(&This
->dispex
, cb
);
1310 if(This
->vtbl
->traverse
)
1311 This
->vtbl
->traverse(This
, cb
);
1316 static nsresult NSAPI
HTMLDOMNode_unlink(void *p
)
1318 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(p
);
1320 TRACE("%p\n", This
);
1322 if(This
->vtbl
->unlink
)
1323 This
->vtbl
->unlink(This
);
1325 dispex_unlink(&This
->dispex
);
1328 nsIDOMNode
*nsnode
= This
->nsnode
;
1329 This
->nsnode
= NULL
;
1330 nsIDOMNode_Release(nsnode
);
1333 if(This
->doc
&& &This
->doc
->node
!= This
) {
1334 HTMLDocument
*doc
= &This
->doc
->basedoc
;
1336 htmldoc_release(doc
);
1344 void init_node_cc(void)
1346 static const CCObjCallback node_ccp_callback
= {
1347 HTMLDOMNode_unmark_if_purple
,
1348 HTMLDOMNode_traverse
,
1352 ccp_init(&node_ccp
, &node_ccp_callback
);
1355 HRESULT
get_node(HTMLDocumentNode
*This
, nsIDOMNode
*nsnode
, BOOL create
, HTMLDOMNode
**ret
)
1357 nsISupports
*unk
= NULL
;
1360 nsres
= nsIDOMNode_GetMshtmlNode(nsnode
, &unk
);
1361 assert(nsres
== NS_OK
);
1364 *ret
= get_node_obj((IHTMLDOMNode
*)unk
);
1365 nsISupports_Release(unk
);
1374 return create_node(This
, nsnode
, ret
);