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
**);
39 static ExternalCycleCollectionParticipant node_ccp
;
43 IHTMLDOMChildrenCollection IHTMLDOMChildrenCollection_iface
;
47 /* FIXME: implement weak reference */
48 HTMLDocumentNode
*doc
;
50 nsIDOMNodeList
*nslist
;
51 } HTMLDOMChildrenCollection
;
54 IEnumVARIANT IEnumVARIANT_iface
;
59 HTMLDOMChildrenCollection
*col
;
60 } HTMLDOMChildrenCollectionEnum
;
62 static inline HTMLDOMChildrenCollectionEnum
*impl_from_IEnumVARIANT(IEnumVARIANT
*iface
)
64 return CONTAINING_RECORD(iface
, HTMLDOMChildrenCollectionEnum
, IEnumVARIANT_iface
);
67 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_QueryInterface(IEnumVARIANT
*iface
, REFIID riid
, void **ppv
)
69 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
71 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
73 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
74 *ppv
= &This
->IEnumVARIANT_iface
;
75 }else if(IsEqualGUID(riid
, &IID_IEnumVARIANT
)) {
76 *ppv
= &This
->IEnumVARIANT_iface
;
78 FIXME("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
83 IUnknown_AddRef((IUnknown
*)*ppv
);
87 static ULONG WINAPI
HTMLDOMChildrenCollectionEnum_AddRef(IEnumVARIANT
*iface
)
89 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
90 LONG ref
= InterlockedIncrement(&This
->ref
);
92 TRACE("(%p) ref=%d\n", This
, ref
);
97 static ULONG WINAPI
HTMLDOMChildrenCollectionEnum_Release(IEnumVARIANT
*iface
)
99 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
100 LONG ref
= InterlockedDecrement(&This
->ref
);
102 TRACE("(%p) ref=%d\n", This
, ref
);
105 IHTMLDOMChildrenCollection_Release(&This
->col
->IHTMLDOMChildrenCollection_iface
);
112 static ULONG
get_enum_len(HTMLDOMChildrenCollectionEnum
*This
)
117 nsres
= nsIDOMNodeList_GetLength(This
->col
->nslist
, &len
);
118 assert(nsres
== NS_OK
);
122 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Next(IEnumVARIANT
*iface
, ULONG celt
, VARIANT
*rgVar
, ULONG
*pCeltFetched
)
124 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
125 ULONG fetched
= 0, len
;
131 TRACE("(%p)->(%d %p %p)\n", This
, celt
, rgVar
, pCeltFetched
);
133 len
= get_enum_len(This
);
135 while(This
->iter
+fetched
< len
&& fetched
< celt
) {
136 nsres
= nsIDOMNodeList_Item(This
->col
->nslist
, This
->iter
+fetched
, &nsnode
);
137 assert(nsres
== NS_OK
);
139 hres
= get_node(This
->col
->doc
, nsnode
, TRUE
, &node
);
140 nsIDOMNode_Release(nsnode
);
142 ERR("get_node failed: %08x\n", hres
);
146 V_VT(rgVar
+fetched
) = VT_DISPATCH
;
147 IHTMLDOMNode_AddRef(&node
->IHTMLDOMNode_iface
);
148 V_DISPATCH(rgVar
+fetched
) = (IDispatch
*)&node
->IHTMLDOMNode_iface
;
152 This
->iter
+= fetched
;
154 *pCeltFetched
= fetched
;
155 return fetched
== celt
? S_OK
: S_FALSE
;
158 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Skip(IEnumVARIANT
*iface
, ULONG celt
)
160 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
163 TRACE("(%p)->(%d)\n", This
, celt
);
165 len
= get_enum_len(This
);
166 if(This
->iter
+ celt
> len
) {
175 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Reset(IEnumVARIANT
*iface
)
177 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
179 TRACE("(%p)->()\n", This
);
185 static HRESULT WINAPI
HTMLDOMChildrenCollectionEnum_Clone(IEnumVARIANT
*iface
, IEnumVARIANT
**ppEnum
)
187 HTMLDOMChildrenCollectionEnum
*This
= impl_from_IEnumVARIANT(iface
);
188 FIXME("(%p)->(%p)\n", This
, ppEnum
);
192 static const IEnumVARIANTVtbl HTMLDOMChildrenCollectionEnumVtbl
= {
193 HTMLDOMChildrenCollectionEnum_QueryInterface
,
194 HTMLDOMChildrenCollectionEnum_AddRef
,
195 HTMLDOMChildrenCollectionEnum_Release
,
196 HTMLDOMChildrenCollectionEnum_Next
,
197 HTMLDOMChildrenCollectionEnum_Skip
,
198 HTMLDOMChildrenCollectionEnum_Reset
,
199 HTMLDOMChildrenCollectionEnum_Clone
202 static inline HTMLDOMChildrenCollection
*impl_from_IHTMLDOMChildrenCollection(IHTMLDOMChildrenCollection
*iface
)
204 return CONTAINING_RECORD(iface
, HTMLDOMChildrenCollection
, IHTMLDOMChildrenCollection_iface
);
207 static HRESULT WINAPI
HTMLDOMChildrenCollection_QueryInterface(IHTMLDOMChildrenCollection
*iface
, REFIID riid
, void **ppv
)
209 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
211 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
213 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
214 *ppv
= &This
->IHTMLDOMChildrenCollection_iface
;
215 }else if(IsEqualGUID(&IID_IHTMLDOMChildrenCollection
, riid
)) {
216 *ppv
= &This
->IHTMLDOMChildrenCollection_iface
;
217 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
218 return *ppv
? S_OK
: E_NOINTERFACE
;
221 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
222 return E_NOINTERFACE
;
225 IUnknown_AddRef((IUnknown
*)*ppv
);
229 static ULONG WINAPI
HTMLDOMChildrenCollection_AddRef(IHTMLDOMChildrenCollection
*iface
)
231 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
232 LONG ref
= InterlockedIncrement(&This
->ref
);
234 TRACE("(%p) ref=%d\n", This
, ref
);
239 static ULONG WINAPI
HTMLDOMChildrenCollection_Release(IHTMLDOMChildrenCollection
*iface
)
241 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
242 LONG ref
= InterlockedDecrement(&This
->ref
);
244 TRACE("(%p) ref=%d\n", This
, ref
);
247 htmldoc_release(&This
->doc
->basedoc
);
248 nsIDOMNodeList_Release(This
->nslist
);
255 static HRESULT WINAPI
HTMLDOMChildrenCollection_GetTypeInfoCount(IHTMLDOMChildrenCollection
*iface
, UINT
*pctinfo
)
257 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
258 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
261 static HRESULT WINAPI
HTMLDOMChildrenCollection_GetTypeInfo(IHTMLDOMChildrenCollection
*iface
, UINT iTInfo
,
262 LCID lcid
, ITypeInfo
**ppTInfo
)
264 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
265 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
268 static HRESULT WINAPI
HTMLDOMChildrenCollection_GetIDsOfNames(IHTMLDOMChildrenCollection
*iface
, REFIID riid
,
269 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
271 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
272 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
276 static HRESULT WINAPI
HTMLDOMChildrenCollection_Invoke(IHTMLDOMChildrenCollection
*iface
, DISPID dispIdMember
,
277 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
278 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
280 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
281 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
282 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
285 static HRESULT WINAPI
HTMLDOMChildrenCollection_get_length(IHTMLDOMChildrenCollection
*iface
, LONG
*p
)
287 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
290 TRACE("(%p)->(%p)\n", This
, p
);
292 nsIDOMNodeList_GetLength(This
->nslist
, &length
);
297 static HRESULT WINAPI
HTMLDOMChildrenCollection_get__newEnum(IHTMLDOMChildrenCollection
*iface
, IUnknown
**p
)
299 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
300 HTMLDOMChildrenCollectionEnum
*ret
;
302 TRACE("(%p)->(%p)\n", This
, p
);
304 ret
= heap_alloc(sizeof(*ret
));
306 return E_OUTOFMEMORY
;
308 ret
->IEnumVARIANT_iface
.lpVtbl
= &HTMLDOMChildrenCollectionEnumVtbl
;
312 IHTMLDOMChildrenCollection_AddRef(&This
->IHTMLDOMChildrenCollection_iface
);
315 *p
= (IUnknown
*)&ret
->IEnumVARIANT_iface
;
319 static HRESULT WINAPI
HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection
*iface
, LONG index
, IDispatch
**ppItem
)
321 HTMLDOMChildrenCollection
*This
= impl_from_IHTMLDOMChildrenCollection(iface
);
322 nsIDOMNode
*nsnode
= NULL
;
328 TRACE("(%p)->(%d %p)\n", This
, index
, ppItem
);
335 nsIDOMNodeList_GetLength(This
->nslist
, &length
);
336 if(index
< 0 || index
>= length
)
339 nsres
= nsIDOMNodeList_Item(This
->nslist
, index
, &nsnode
);
340 if(NS_FAILED(nsres
) || !nsnode
) {
341 ERR("Item failed: %08x\n", nsres
);
345 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node
);
349 *ppItem
= (IDispatch
*)&node
->IHTMLDOMNode_iface
;
353 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl
= {
354 HTMLDOMChildrenCollection_QueryInterface
,
355 HTMLDOMChildrenCollection_AddRef
,
356 HTMLDOMChildrenCollection_Release
,
357 HTMLDOMChildrenCollection_GetTypeInfoCount
,
358 HTMLDOMChildrenCollection_GetTypeInfo
,
359 HTMLDOMChildrenCollection_GetIDsOfNames
,
360 HTMLDOMChildrenCollection_Invoke
,
361 HTMLDOMChildrenCollection_get_length
,
362 HTMLDOMChildrenCollection_get__newEnum
,
363 HTMLDOMChildrenCollection_item
366 static inline HTMLDOMChildrenCollection
*impl_from_DispatchEx(DispatchEx
*iface
)
368 return CONTAINING_RECORD(iface
, HTMLDOMChildrenCollection
, dispex
);
371 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
373 static HRESULT
HTMLDOMChildrenCollection_get_dispid(DispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*dispid
)
375 HTMLDOMChildrenCollection
*This
= impl_from_DispatchEx(dispex
);
380 for(ptr
= name
; *ptr
&& isdigitW(*ptr
); ptr
++)
381 idx
= idx
*10 + (*ptr
-'0');
383 return DISP_E_UNKNOWNNAME
;
385 nsIDOMNodeList_GetLength(This
->nslist
, &len
);
387 return DISP_E_UNKNOWNNAME
;
389 *dispid
= DISPID_CHILDCOL_0
+ idx
;
390 TRACE("ret %x\n", *dispid
);
394 static HRESULT
HTMLDOMChildrenCollection_invoke(DispatchEx
*dispex
, DISPID id
, LCID lcid
, WORD flags
, DISPPARAMS
*params
,
395 VARIANT
*res
, EXCEPINFO
*ei
, IServiceProvider
*caller
)
397 HTMLDOMChildrenCollection
*This
= impl_from_DispatchEx(dispex
);
399 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, flags
, params
, res
, ei
, caller
);
402 case DISPATCH_PROPERTYGET
: {
403 IDispatch
*disp
= NULL
;
406 hres
= IHTMLDOMChildrenCollection_item(&This
->IHTMLDOMChildrenCollection_iface
,
407 id
- DISPID_CHILDCOL_0
, &disp
);
411 V_VT(res
) = VT_DISPATCH
;
412 V_DISPATCH(res
) = disp
;
417 FIXME("unimplemented flags %x\n", flags
);
424 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl
= {
426 HTMLDOMChildrenCollection_get_dispid
,
427 HTMLDOMChildrenCollection_invoke
,
431 static const tid_t HTMLDOMChildrenCollection_iface_tids
[] = {
432 IHTMLDOMChildrenCollection_tid
,
436 static dispex_static_data_t HTMLDOMChildrenCollection_dispex
= {
437 &HTMLDOMChildrenCollection_dispex_vtbl
,
438 DispDOMChildrenCollection_tid
,
440 HTMLDOMChildrenCollection_iface_tids
443 static IHTMLDOMChildrenCollection
*create_child_collection(HTMLDocumentNode
*doc
, nsIDOMNodeList
*nslist
)
445 HTMLDOMChildrenCollection
*ret
;
447 ret
= heap_alloc_zero(sizeof(*ret
));
451 ret
->IHTMLDOMChildrenCollection_iface
.lpVtbl
= &HTMLDOMChildrenCollectionVtbl
;
454 nsIDOMNodeList_AddRef(nslist
);
455 ret
->nslist
= nslist
;
457 htmldoc_addref(&doc
->basedoc
);
460 init_dispex(&ret
->dispex
, (IUnknown
*)&ret
->IHTMLDOMChildrenCollection_iface
,
461 &HTMLDOMChildrenCollection_dispex
);
463 return &ret
->IHTMLDOMChildrenCollection_iface
;
466 static inline HTMLDOMNode
*impl_from_IHTMLDOMNode(IHTMLDOMNode
*iface
)
468 return CONTAINING_RECORD(iface
, HTMLDOMNode
, IHTMLDOMNode_iface
);
471 static HRESULT WINAPI
HTMLDOMNode_QueryInterface(IHTMLDOMNode
*iface
,
472 REFIID riid
, void **ppv
)
474 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
476 return This
->vtbl
->qi(This
, riid
, ppv
);
479 static ULONG WINAPI
HTMLDOMNode_AddRef(IHTMLDOMNode
*iface
)
481 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
484 ref
= ccref_incr(&This
->ccref
, (nsISupports
*)&This
->IHTMLDOMNode_iface
);
486 TRACE("(%p) ref=%d\n", This
, ref
);
491 static ULONG WINAPI
HTMLDOMNode_Release(IHTMLDOMNode
*iface
)
493 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
494 LONG ref
= ccref_decr(&This
->ccref
, (nsISupports
*)&This
->IHTMLDOMNode_iface
, /*&node_ccp*/ NULL
);
496 TRACE("(%p) ref=%d\n", This
, ref
);
501 static HRESULT WINAPI
HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode
*iface
, UINT
*pctinfo
)
503 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
504 return IDispatchEx_GetTypeInfoCount(&This
->event_target
.dispex
.IDispatchEx_iface
, pctinfo
);
507 static HRESULT WINAPI
HTMLDOMNode_GetTypeInfo(IHTMLDOMNode
*iface
, UINT iTInfo
,
508 LCID lcid
, ITypeInfo
**ppTInfo
)
510 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
511 return IDispatchEx_GetTypeInfo(&This
->event_target
.dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
514 static HRESULT WINAPI
HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode
*iface
, REFIID riid
,
515 LPOLESTR
*rgszNames
, UINT cNames
,
516 LCID lcid
, DISPID
*rgDispId
)
518 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
519 return IDispatchEx_GetIDsOfNames(&This
->event_target
.dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
523 static HRESULT WINAPI
HTMLDOMNode_Invoke(IHTMLDOMNode
*iface
, DISPID dispIdMember
,
524 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
525 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
527 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
528 return IDispatchEx_Invoke(&This
->event_target
.dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
529 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
532 static HRESULT WINAPI
HTMLDOMNode_get_nodeType(IHTMLDOMNode
*iface
, LONG
*p
)
534 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
537 TRACE("(%p)->(%p)\n", This
, p
);
539 nsIDOMNode_GetNodeType(This
->nsnode
, &type
);
549 case DOCUMENT_TYPE_NODE
:
555 case DOCUMENT_FRAGMENT_NODE
:
561 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
562 * It needs more tests.
564 FIXME("type %u\n", type
);
571 static HRESULT WINAPI
HTMLDOMNode_get_parentNode(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
573 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
579 TRACE("(%p)->(%p)\n", This
, p
);
581 nsres
= nsIDOMNode_GetParentNode(This
->nsnode
, &nsnode
);
582 if(NS_FAILED(nsres
)) {
583 ERR("GetParentNode failed: %08x\n", nsres
);
592 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node
);
593 nsIDOMNode_Release(nsnode
);
597 *p
= &node
->IHTMLDOMNode_iface
;
601 static HRESULT WINAPI
HTMLDOMNode_hasChildNodes(IHTMLDOMNode
*iface
, VARIANT_BOOL
*fChildren
)
603 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
604 cpp_bool has_child
= FALSE
;
607 TRACE("(%p)->(%p)\n", This
, fChildren
);
609 nsres
= nsIDOMNode_HasChildNodes(This
->nsnode
, &has_child
);
611 ERR("HasChildNodes failed: %08x\n", nsres
);
613 *fChildren
= has_child
? VARIANT_TRUE
: VARIANT_FALSE
;
617 static HRESULT WINAPI
HTMLDOMNode_get_childNodes(IHTMLDOMNode
*iface
, IDispatch
**p
)
619 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
620 nsIDOMNodeList
*nslist
;
623 TRACE("(%p)->(%p)\n", This
, p
);
625 nsres
= nsIDOMNode_GetChildNodes(This
->nsnode
, &nslist
);
626 if(NS_FAILED(nsres
)) {
627 ERR("GetChildNodes failed: %08x\n", nsres
);
631 *p
= (IDispatch
*)create_child_collection(This
->doc
, nslist
);
632 nsIDOMNodeList_Release(nslist
);
634 return *p
? S_OK
: E_OUTOFMEMORY
;
637 static HRESULT WINAPI
HTMLDOMNode_get_attributes(IHTMLDOMNode
*iface
, IDispatch
**p
)
639 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
640 HTMLAttributeCollection
*col
;
643 TRACE("(%p)->(%p)\n", This
, p
);
645 if(This
->vtbl
->get_attr_col
) {
646 hres
= This
->vtbl
->get_attr_col(This
, &col
);
650 *p
= (IDispatch
*)&col
->IHTMLAttributeCollection_iface
;
658 static HRESULT WINAPI
HTMLDOMNode_insertBefore(IHTMLDOMNode
*iface
, IHTMLDOMNode
*newChild
,
659 VARIANT refChild
, IHTMLDOMNode
**node
)
661 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
662 HTMLDOMNode
*new_child
, *node_obj
, *ref_node
= NULL
;
667 TRACE("(%p)->(%p %s %p)\n", This
, newChild
, debugstr_variant(&refChild
), node
);
669 new_child
= get_node_obj(newChild
);
671 ERR("invalid newChild\n");
675 switch(V_VT(&refChild
)) {
679 IHTMLDOMNode
*ref_iface
;
681 if(!V_DISPATCH(&refChild
))
684 hres
= IDispatch_QueryInterface(V_DISPATCH(&refChild
), &IID_IHTMLDOMNode
, (void**)&ref_iface
);
688 ref_node
= get_node_obj(ref_iface
);
689 IHTMLDOMNode_Release(ref_iface
);
691 ERR("unvalid node\n");
698 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild
));
702 if(SUCCEEDED(hres
)) {
703 nsres
= nsIDOMNode_InsertBefore(This
->nsnode
, new_child
->nsnode
, ref_node
? ref_node
->nsnode
: NULL
, &nsnode
);
704 if(NS_FAILED(nsres
)) {
705 ERR("InsertBefore failed: %08x\n", nsres
);
709 node_release(new_child
);
711 node_release(ref_node
);
715 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node_obj
);
716 nsIDOMNode_Release(nsnode
);
720 *node
= &node_obj
->IHTMLDOMNode_iface
;
724 static HRESULT WINAPI
HTMLDOMNode_removeChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
*oldChild
,
727 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
728 HTMLDOMNode
*node_obj
;
733 TRACE("(%p)->(%p %p)\n", This
, oldChild
, node
);
735 node_obj
= get_node_obj(oldChild
);
739 nsres
= nsIDOMNode_RemoveChild(This
->nsnode
, node_obj
->nsnode
, &nsnode
);
740 node_release(node_obj
);
741 if(NS_FAILED(nsres
)) {
742 ERR("RemoveChild failed: %08x\n", nsres
);
746 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node_obj
);
747 nsIDOMNode_Release(nsnode
);
751 /* FIXME: Make sure that node != newChild */
752 *node
= &node_obj
->IHTMLDOMNode_iface
;
756 static HRESULT WINAPI
HTMLDOMNode_replaceChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
*newChild
,
757 IHTMLDOMNode
*oldChild
, IHTMLDOMNode
**node
)
759 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
760 HTMLDOMNode
*node_new
, *node_old
, *ret_node
;
765 TRACE("(%p)->(%p %p %p)\n", This
, newChild
, oldChild
, node
);
767 node_new
= get_node_obj(newChild
);
771 node_old
= get_node_obj(oldChild
);
773 node_release(node_new
);
777 nsres
= nsIDOMNode_ReplaceChild(This
->nsnode
, node_new
->nsnode
, node_old
->nsnode
, &nsnode
);
778 node_release(node_new
);
779 node_release(node_old
);
783 hres
= get_node(This
->doc
, nsnode
, TRUE
, &ret_node
);
784 nsIDOMNode_Release(nsnode
);
788 *node
= &ret_node
->IHTMLDOMNode_iface
;
792 static HRESULT WINAPI
HTMLDOMNode_cloneNode(IHTMLDOMNode
*iface
, VARIANT_BOOL fDeep
,
793 IHTMLDOMNode
**clonedNode
)
795 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
796 HTMLDOMNode
*new_node
;
801 TRACE("(%p)->(%x %p)\n", This
, fDeep
, clonedNode
);
803 nsres
= nsIDOMNode_CloneNode(This
->nsnode
, fDeep
!= VARIANT_FALSE
, 1, &nsnode
);
804 if(NS_FAILED(nsres
) || !nsnode
) {
805 ERR("CloneNode failed: %08x\n", nsres
);
809 hres
= This
->vtbl
->clone(This
, nsnode
, &new_node
);
813 *clonedNode
= &new_node
->IHTMLDOMNode_iface
;
817 static HRESULT WINAPI
HTMLDOMNode_removeNode(IHTMLDOMNode
*iface
, VARIANT_BOOL fDeep
,
818 IHTMLDOMNode
**removed
)
820 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
821 FIXME("(%p)->(%x %p)\n", This
, fDeep
, removed
);
825 static HRESULT WINAPI
HTMLDOMNode_swapNode(IHTMLDOMNode
*iface
, IHTMLDOMNode
*otherNode
,
826 IHTMLDOMNode
**swappedNode
)
828 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
829 FIXME("(%p)->(%p %p)\n", This
, otherNode
, swappedNode
);
833 static HRESULT WINAPI
HTMLDOMNode_replaceNode(IHTMLDOMNode
*iface
, IHTMLDOMNode
*replacement
,
834 IHTMLDOMNode
**replaced
)
836 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
837 FIXME("(%p)->(%p %p)\n", This
, replacement
, replaced
);
841 static HRESULT WINAPI
HTMLDOMNode_appendChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
*newChild
,
844 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
845 HTMLDOMNode
*node_obj
;
850 TRACE("(%p)->(%p %p)\n", This
, newChild
, node
);
852 node_obj
= get_node_obj(newChild
);
856 nsres
= nsIDOMNode_AppendChild(This
->nsnode
, node_obj
->nsnode
, &nsnode
);
857 node_release(node_obj
);
858 if(NS_FAILED(nsres
)) {
859 ERR("AppendChild failed: %08x\n", nsres
);
863 hres
= get_node(This
->doc
, nsnode
, TRUE
, &node_obj
);
864 nsIDOMNode_Release(nsnode
);
868 /* FIXME: Make sure that node != newChild */
869 *node
= &node_obj
->IHTMLDOMNode_iface
;
873 static HRESULT WINAPI
HTMLDOMNode_get_nodeName(IHTMLDOMNode
*iface
, BSTR
*p
)
875 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
879 TRACE("(%p)->(%p)\n", This
, p
);
881 nsAString_Init(&name
, NULL
);
882 nsres
= nsIDOMNode_GetNodeName(This
->nsnode
, &name
);
883 return return_nsstr(nsres
, &name
, p
);
886 static HRESULT WINAPI
HTMLDOMNode_put_nodeValue(IHTMLDOMNode
*iface
, VARIANT v
)
888 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
890 TRACE("(%p)->(%s)\n", This
, debugstr_variant(&v
));
896 nsAString_InitDepend(&val_str
, V_BSTR(&v
));
897 nsIDOMNode_SetNodeValue(This
->nsnode
, &val_str
);
898 nsAString_Finish(&val_str
);
904 FIXME("unsupported value %s\n", debugstr_variant(&v
));
910 static HRESULT WINAPI
HTMLDOMNode_get_nodeValue(IHTMLDOMNode
*iface
, VARIANT
*p
)
912 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
913 const PRUnichar
*val
;
916 TRACE("(%p)->(%p)\n", This
, p
);
918 nsAString_Init(&val_str
, NULL
);
919 nsIDOMNode_GetNodeValue(This
->nsnode
, &val_str
);
920 nsAString_GetData(&val_str
, &val
);
924 V_BSTR(p
) = SysAllocString(val
);
929 nsAString_Finish(&val_str
);
934 static HRESULT WINAPI
HTMLDOMNode_get_firstChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
936 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
937 nsIDOMNode
*nschild
= NULL
;
941 TRACE("(%p)->(%p)\n", This
, p
);
943 nsIDOMNode_GetFirstChild(This
->nsnode
, &nschild
);
949 hres
= get_node(This
->doc
, nschild
, TRUE
, &node
);
950 nsIDOMNode_Release(nschild
);
954 *p
= &node
->IHTMLDOMNode_iface
;
958 static HRESULT WINAPI
HTMLDOMNode_get_lastChild(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
960 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
961 nsIDOMNode
*nschild
= NULL
;
965 TRACE("(%p)->(%p)\n", This
, p
);
967 nsIDOMNode_GetLastChild(This
->nsnode
, &nschild
);
973 hres
= get_node(This
->doc
, nschild
, TRUE
, &node
);
974 nsIDOMNode_Release(nschild
);
978 *p
= &node
->IHTMLDOMNode_iface
;
982 static HRESULT WINAPI
HTMLDOMNode_get_previousSibling(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
984 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
985 nsIDOMNode
*nschild
= NULL
;
989 TRACE("(%p)->(%p)\n", This
, p
);
991 nsIDOMNode_GetPreviousSibling(This
->nsnode
, &nschild
);
997 hres
= get_node(This
->doc
, nschild
, TRUE
, &node
);
998 nsIDOMNode_Release(nschild
);
1002 *p
= &node
->IHTMLDOMNode_iface
;
1006 static HRESULT WINAPI
HTMLDOMNode_get_nextSibling(IHTMLDOMNode
*iface
, IHTMLDOMNode
**p
)
1008 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(iface
);
1009 nsIDOMNode
*nssibling
= NULL
;
1013 TRACE("(%p)->(%p)\n", This
, p
);
1015 nsIDOMNode_GetNextSibling(This
->nsnode
, &nssibling
);
1021 hres
= get_node(This
->doc
, nssibling
, TRUE
, &node
);
1022 nsIDOMNode_Release(nssibling
);
1026 *p
= &node
->IHTMLDOMNode_iface
;
1030 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl
= {
1031 HTMLDOMNode_QueryInterface
,
1033 HTMLDOMNode_Release
,
1034 HTMLDOMNode_GetTypeInfoCount
,
1035 HTMLDOMNode_GetTypeInfo
,
1036 HTMLDOMNode_GetIDsOfNames
,
1038 HTMLDOMNode_get_nodeType
,
1039 HTMLDOMNode_get_parentNode
,
1040 HTMLDOMNode_hasChildNodes
,
1041 HTMLDOMNode_get_childNodes
,
1042 HTMLDOMNode_get_attributes
,
1043 HTMLDOMNode_insertBefore
,
1044 HTMLDOMNode_removeChild
,
1045 HTMLDOMNode_replaceChild
,
1046 HTMLDOMNode_cloneNode
,
1047 HTMLDOMNode_removeNode
,
1048 HTMLDOMNode_swapNode
,
1049 HTMLDOMNode_replaceNode
,
1050 HTMLDOMNode_appendChild
,
1051 HTMLDOMNode_get_nodeName
,
1052 HTMLDOMNode_put_nodeValue
,
1053 HTMLDOMNode_get_nodeValue
,
1054 HTMLDOMNode_get_firstChild
,
1055 HTMLDOMNode_get_lastChild
,
1056 HTMLDOMNode_get_previousSibling
,
1057 HTMLDOMNode_get_nextSibling
1060 static HTMLDOMNode
*get_node_obj(IHTMLDOMNode
*iface
)
1064 if(iface
->lpVtbl
!= &HTMLDOMNodeVtbl
)
1067 ret
= impl_from_IHTMLDOMNode(iface
);
1072 static inline HTMLDOMNode
*impl_from_IHTMLDOMNode2(IHTMLDOMNode2
*iface
)
1074 return CONTAINING_RECORD(iface
, HTMLDOMNode
, IHTMLDOMNode2_iface
);
1077 static HRESULT WINAPI
HTMLDOMNode2_QueryInterface(IHTMLDOMNode2
*iface
,
1078 REFIID riid
, void **ppv
)
1080 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1082 return IHTMLDOMNode_QueryInterface(&This
->IHTMLDOMNode_iface
, riid
, ppv
);
1085 static ULONG WINAPI
HTMLDOMNode2_AddRef(IHTMLDOMNode2
*iface
)
1087 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1089 return IHTMLDOMNode_AddRef(&This
->IHTMLDOMNode_iface
);
1092 static ULONG WINAPI
HTMLDOMNode2_Release(IHTMLDOMNode2
*iface
)
1094 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1096 return IHTMLDOMNode_Release(&This
->IHTMLDOMNode_iface
);
1099 static HRESULT WINAPI
HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2
*iface
, UINT
*pctinfo
)
1101 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1102 return IDispatchEx_GetTypeInfoCount(&This
->event_target
.dispex
.IDispatchEx_iface
, pctinfo
);
1105 static HRESULT WINAPI
HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2
*iface
, UINT iTInfo
,
1106 LCID lcid
, ITypeInfo
**ppTInfo
)
1108 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1109 return IDispatchEx_GetTypeInfo(&This
->event_target
.dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
1112 static HRESULT WINAPI
HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2
*iface
, REFIID riid
,
1113 LPOLESTR
*rgszNames
, UINT cNames
,
1114 LCID lcid
, DISPID
*rgDispId
)
1116 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1117 return IDispatchEx_GetIDsOfNames(&This
->event_target
.dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
1121 static HRESULT WINAPI
HTMLDOMNode2_Invoke(IHTMLDOMNode2
*iface
, DISPID dispIdMember
,
1122 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
1123 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
1125 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1126 return IDispatchEx_Invoke(&This
->event_target
.dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
1127 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
1130 static HRESULT WINAPI
HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2
*iface
, IDispatch
**p
)
1132 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode2(iface
);
1134 TRACE("(%p)->(%p)\n", This
, p
);
1136 /* FIXME: Better check for document node */
1137 if(This
== &This
->doc
->node
) {
1140 *p
= (IDispatch
*)&This
->doc
->basedoc
.IHTMLDocument2_iface
;
1141 IDispatch_AddRef(*p
);
1146 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl
= {
1147 HTMLDOMNode2_QueryInterface
,
1148 HTMLDOMNode2_AddRef
,
1149 HTMLDOMNode2_Release
,
1150 HTMLDOMNode2_GetTypeInfoCount
,
1151 HTMLDOMNode2_GetTypeInfo
,
1152 HTMLDOMNode2_GetIDsOfNames
,
1153 HTMLDOMNode2_Invoke
,
1154 HTMLDOMNode2_get_ownerDocument
1157 HRESULT
HTMLDOMNode_QI(HTMLDOMNode
*This
, REFIID riid
, void **ppv
)
1159 TRACE("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
1161 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
1162 *ppv
= &This
->IHTMLDOMNode_iface
;
1163 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
1164 *ppv
= &This
->IHTMLDOMNode_iface
;
1165 }else if(IsEqualGUID(&IID_IHTMLDOMNode
, riid
)) {
1166 *ppv
= &This
->IHTMLDOMNode_iface
;
1167 }else if(IsEqualGUID(&IID_IHTMLDOMNode2
, riid
)) {
1168 *ppv
= &This
->IHTMLDOMNode2_iface
;
1169 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant
, riid
)) {
1172 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports
, riid
)) {
1173 *ppv
= &This
->IHTMLDOMNode_iface
;
1175 }else if(dispex_query_interface(&This
->event_target
.dispex
, riid
, ppv
)) {
1176 return *ppv
? S_OK
: E_NOINTERFACE
;
1179 WARN("(%p)->(%s %p)\n", This
, debugstr_mshtml_guid(riid
), ppv
);
1180 return E_NOINTERFACE
;
1183 IUnknown_AddRef((IUnknown
*)*ppv
);
1187 void HTMLDOMNode_destructor(HTMLDOMNode
*This
)
1190 nsIDOMNode_Release(This
->nsnode
);
1191 if(This
->doc
&& &This
->doc
->node
!= This
)
1192 htmldoc_release(&This
->doc
->basedoc
);
1193 if(This
->event_target
.ptr
)
1194 release_event_target(This
->event_target
.ptr
);
1197 static HRESULT
HTMLDOMNode_clone(HTMLDOMNode
*This
, nsIDOMNode
*nsnode
, HTMLDOMNode
**ret
)
1199 return create_node(This
->doc
, nsnode
, ret
);
1202 static const cpc_entry_t HTMLDOMNode_cpc
[] = {{NULL
}};
1204 static const NodeImplVtbl HTMLDOMNodeImplVtbl
= {
1206 HTMLDOMNode_destructor
,
1211 void HTMLDOMNode_Init(HTMLDocumentNode
*doc
, HTMLDOMNode
*node
, nsIDOMNode
*nsnode
)
1215 node
->IHTMLDOMNode_iface
.lpVtbl
= &HTMLDOMNodeVtbl
;
1216 node
->IHTMLDOMNode2_iface
.lpVtbl
= &HTMLDOMNode2Vtbl
;
1218 ccref_init(&node
->ccref
, 1);
1220 if(&doc
->node
!= node
)
1221 htmldoc_addref(&doc
->basedoc
);
1224 nsIDOMNode_AddRef(nsnode
);
1225 node
->nsnode
= nsnode
;
1227 nsres
= nsIDOMNode_SetMshtmlNode(nsnode
, (nsISupports
*)&node
->IHTMLDOMNode_iface
);
1228 assert(nsres
== NS_OK
);
1231 static HRESULT
create_node(HTMLDocumentNode
*doc
, nsIDOMNode
*nsnode
, HTMLDOMNode
**ret
)
1236 nsIDOMNode_GetNodeType(nsnode
, &node_type
);
1239 case ELEMENT_NODE
: {
1241 hres
= HTMLElement_Create(doc
, nsnode
, FALSE
, &elem
);
1248 hres
= HTMLDOMTextNode_Create(doc
, nsnode
, ret
);
1252 /* doctype nodes are represented as comment nodes (at least in quirks mode) */
1253 case DOCUMENT_TYPE_NODE
:
1254 case COMMENT_NODE
: {
1255 HTMLElement
*comment
;
1256 hres
= HTMLCommentElement_Create(doc
, nsnode
, &comment
);
1259 *ret
= &comment
->node
;
1262 case ATTRIBUTE_NODE
:
1263 ERR("Called on attribute node\n");
1264 return E_UNEXPECTED
;
1268 node
= heap_alloc_zero(sizeof(HTMLDOMNode
));
1270 return E_OUTOFMEMORY
;
1272 node
->vtbl
= &HTMLDOMNodeImplVtbl
;
1273 HTMLDOMNode_Init(doc
, node
, nsnode
);
1278 TRACE("type %d ret %p\n", node_type
, *ret
);
1282 static nsresult NSAPI
HTMLDOMNode_traverse(void *ccp
, void *p
, nsCycleCollectionTraversalCallback
*cb
)
1284 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(p
);
1286 TRACE("%p\n", This
);
1288 describe_cc_node(&This
->ccref
, "HTMLDOMNode", cb
);
1291 note_cc_edge((nsISupports
*)This
->nsnode
, "This->nsnode", cb
);
1292 if(This
->doc
&& &This
->doc
->node
!= This
)
1293 note_cc_edge((nsISupports
*)&This
->doc
->node
.IHTMLDOMNode_iface
, "This->doc", cb
);
1294 dispex_traverse(&This
->event_target
.dispex
, cb
);
1296 if(This
->vtbl
->traverse
)
1297 This
->vtbl
->traverse(This
, cb
);
1302 static nsresult NSAPI
HTMLDOMNode_unlink(void *p
)
1304 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(p
);
1306 TRACE("%p\n", This
);
1308 if(This
->vtbl
->unlink
)
1309 This
->vtbl
->unlink(This
);
1311 dispex_unlink(&This
->event_target
.dispex
);
1314 nsIDOMNode
*nsnode
= This
->nsnode
;
1315 This
->nsnode
= NULL
;
1316 nsIDOMNode_Release(nsnode
);
1319 if(This
->doc
&& &This
->doc
->node
!= This
) {
1320 HTMLDocument
*doc
= &This
->doc
->basedoc
;
1322 htmldoc_release(doc
);
1330 static void NSAPI
HTMLDOMNode_delete_cycle_collectable(void *p
)
1332 HTMLDOMNode
*This
= impl_from_IHTMLDOMNode(p
);
1334 TRACE("(%p)\n", This
);
1336 if(This
->vtbl
->unlink
)
1337 This
->vtbl
->unlink(This
);
1338 This
->vtbl
->destructor(This
);
1339 release_dispex(&This
->event_target
.dispex
);
1343 void init_node_cc(void)
1345 static const CCObjCallback node_ccp_callback
= {
1346 HTMLDOMNode_traverse
,
1348 HTMLDOMNode_delete_cycle_collectable
1351 ccp_init(&node_ccp
, &node_ccp_callback
);
1354 HRESULT
get_node(HTMLDocumentNode
*This
, nsIDOMNode
*nsnode
, BOOL create
, HTMLDOMNode
**ret
)
1356 nsISupports
*unk
= NULL
;
1359 nsres
= nsIDOMNode_GetMshtmlNode(nsnode
, &unk
);
1360 assert(nsres
== NS_OK
);
1363 *ret
= get_node_obj((IHTMLDOMNode
*)unk
);
1364 nsISupports_Release(unk
);
1373 return create_node(This
, nsnode
, ret
);