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(HTMLDocumentNode*,IUnknown*);
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;
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);
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;
73 IUnknown_AddRef((IUnknown*)*ppv);
77 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
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);
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);
99 htmldoc_release(&This->doc->basedoc);
100 nsIDOMNodeList_Release(This->nslist);
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,
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);
142 TRACE("(%p)->(%p)\n", This, p);
144 nsIDOMNodeList_GetLength(This->nslist, &length);
149 static HRESULT WINAPI HTMLDOMChildrenCollection__newEnum(IHTMLDOMChildrenCollection *iface, IUnknown **p)
151 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
152 FIXME("(%p)->(%p)\n", This, p);
156 static HRESULT WINAPI HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection *iface, LONG index, IDispatch **ppItem)
158 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
159 nsIDOMNode *nsnode = NULL;
165 TRACE("(%p)->(%d %p)\n", This, index, ppItem);
172 nsIDOMNodeList_GetLength(This->nslist, &length);
173 if(index < 0 || index >= length)
176 nsres = nsIDOMNodeList_Item(This->nslist, index, &nsnode);
177 if(NS_FAILED(nsres) || !nsnode) {
178 ERR("Item failed: %08x\n", nsres);
182 hres = get_node(This->doc, nsnode, TRUE, &node);
186 *ppItem = (IDispatch*)&node->IHTMLDOMNode_iface;
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);
217 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
218 idx = idx*10 + (*ptr-'0');
220 return DISP_E_UNKNOWNNAME;
222 nsIDOMNodeList_GetLength(This->nslist, &len);
224 return DISP_E_UNKNOWNNAME;
226 *dispid = DISPID_CHILDCOL_0 + idx;
227 TRACE("ret %x\n", *dispid);
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);
239 case DISPATCH_PROPERTYGET: {
240 IDispatch *disp = NULL;
243 hres = IHTMLDOMChildrenCollection_item(&This->IHTMLDOMChildrenCollection_iface,
244 id - DISPID_CHILDCOL_0, &disp);
248 V_VT(res) = VT_DISPATCH;
249 V_DISPATCH(res) = disp;
254 FIXME("unimplemented flags %x\n", flags);
261 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl = {
263 HTMLDOMChildrenCollection_get_dispid,
264 HTMLDOMChildrenCollection_invoke,
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,
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;
288 nsIDOMNodeList_AddRef(nslist);
289 ret->nslist = nslist;
291 htmldoc_addref(&doc->basedoc);
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);
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);
331 This->vtbl->destructor(This);
332 release_dispex(&This->dispex);
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,
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);
375 TRACE("(%p)->(%p)\n", This, p);
377 nsIDOMNode_GetNodeType(This->nsnode, &type);
392 case DOCUMENT_FRAGMENT_NODE:
398 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
399 * It needs more tests.
401 FIXME("type %u\n", type);
408 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
410 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
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);
429 hres = get_node(This->doc, nsnode, TRUE, &node);
430 nsIDOMNode_Release(nsnode);
434 *p = &node->IHTMLDOMNode_iface;
438 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
440 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
441 cpp_bool has_child = FALSE;
444 TRACE("(%p)->(%p)\n", This, fChildren);
446 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
448 ERR("HasChildNodes failed: %08x\n", nsres);
450 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
454 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
456 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
457 nsIDOMNodeList *nslist;
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);
468 *p = (IDispatch*)create_child_collection(This->doc, nslist);
469 nsIDOMNodeList_Release(nslist);
474 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
476 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
477 HTMLAttributeCollection *col;
480 TRACE("(%p)->(%p)\n", This, p);
482 if(This->vtbl->get_attr_col) {
483 hres = This->vtbl->get_attr_col(This, &col);
487 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
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;
504 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
506 new_child = get_node_obj(This->doc, (IUnknown*)newChild);
508 ERR("invalid newChild\n");
512 switch(V_VT(&refChild)) {
516 if(!V_DISPATCH(&refChild))
518 ref_node = get_node_obj(This->doc, (IUnknown*)V_DISPATCH(&refChild));
520 ERR("unvalid node\n");
527 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
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);
538 node_release(new_child);
540 node_release(ref_node);
544 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
545 nsIDOMNode_Release(nsnode);
549 *node = &node_obj->IHTMLDOMNode_iface;
553 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
556 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
557 HTMLDOMNode *node_obj;
562 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
564 node_obj = get_node_obj(This->doc, (IUnknown*)oldChild);
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);
575 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
576 nsIDOMNode_Release(nsnode);
580 /* FIXME: Make sure that node != newChild */
581 *node = &node_obj->IHTMLDOMNode_iface;
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;
594 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
596 node_new = get_node_obj(This->doc, (IUnknown*)newChild);
600 node_old = get_node_obj(This->doc, (IUnknown*)oldChild);
602 node_release(node_new);
606 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
607 node_release(node_new);
608 node_release(node_old);
612 hres = get_node(This->doc, nsnode, TRUE, &ret_node);
613 nsIDOMNode_Release(nsnode);
617 *node = &ret_node->IHTMLDOMNode_iface;
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;
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);
638 hres = This->vtbl->clone(This, nsnode, &new_node);
642 *clonedNode = &new_node->IHTMLDOMNode_iface;
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);
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);
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);
670 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
673 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
674 HTMLDOMNode *node_obj;
679 TRACE("(%p)->(%p %p)\n", This, newChild, node);
681 node_obj = get_node_obj(This->doc, (IUnknown*)newChild);
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);
692 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
693 nsIDOMNode_Release(nsnode);
697 /* FIXME: Make sure that node != newChild */
698 *node = &node_obj->IHTMLDOMNode_iface;
702 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
704 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
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));
725 nsAString_InitDepend(&val_str, V_BSTR(&v));
726 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
727 nsAString_Finish(&val_str);
733 FIXME("unsupported value %s\n", debugstr_variant(&v));
739 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
741 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
742 const PRUnichar *val;
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);
753 V_BSTR(p) = SysAllocString(val);
758 nsAString_Finish(&val_str);
763 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
765 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
766 nsIDOMNode *nschild = NULL;
770 TRACE("(%p)->(%p)\n", This, p);
772 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
778 hres = get_node(This->doc, nschild, TRUE, &node);
779 nsIDOMNode_Release(nschild);
783 *p = &node->IHTMLDOMNode_iface;
787 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
789 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
790 nsIDOMNode *nschild = NULL;
794 TRACE("(%p)->(%p)\n", This, p);
796 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
802 hres = get_node(This->doc, nschild, TRUE, &node);
803 nsIDOMNode_Release(nschild);
807 *p = &node->IHTMLDOMNode_iface;
811 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
813 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
814 nsIDOMNode *nschild = NULL;
818 TRACE("(%p)->(%p)\n", This, p);
820 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
826 hres = get_node(This->doc, nschild, TRUE, &node);
827 nsIDOMNode_Release(nschild);
831 *p = &node->IHTMLDOMNode_iface;
835 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
837 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
838 nsIDOMNode *nssibling = NULL;
842 TRACE("(%p)->(%p)\n", This, p);
844 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
850 hres = get_node(This->doc, nssibling, TRUE, &node);
851 nsIDOMNode_Release(nssibling);
855 *p = &node->IHTMLDOMNode_iface;
859 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
860 HTMLDOMNode_QueryInterface,
863 HTMLDOMNode_GetTypeInfoCount,
864 HTMLDOMNode_GetTypeInfo,
865 HTMLDOMNode_GetIDsOfNames,
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)
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,
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) {
966 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
967 IDispatch_AddRef(*p);
972 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
973 HTMLDOMNode2_QueryInterface,
975 HTMLDOMNode2_Release,
976 HTMLDOMNode2_GetTypeInfoCount,
977 HTMLDOMNode2_GetTypeInfo,
978 HTMLDOMNode2_GetIDsOfNames,
980 HTMLDOMNode2_get_ownerDocument
983 static nsXPCOMCycleCollectionParticipant node_ccp;
985 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
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;
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);
1013 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1014 TRACE("(%p)->(IID_nsCycleCollectionISupports %p)\n", This, ppv);
1015 *ppv = &This->IHTMLDOMNode_iface;
1017 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1018 return *ppv ? S_OK : E_NOINTERFACE;
1022 IUnknown_AddRef((IUnknown*)*ppv);
1026 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1027 return E_NOINTERFACE;
1030 void HTMLDOMNode_destructor(HTMLDOMNode *This)
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 = {
1047 HTMLDOMNode_destructor,
1051 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
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);
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)
1077 nsIDOMNode_GetNodeType(nsnode, &node_type);
1080 case ELEMENT_NODE: {
1082 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1089 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1093 case COMMENT_NODE: {
1094 HTMLElement *comment;
1095 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1098 *ret = &comment->node;
1101 case ATTRIBUTE_NODE:
1102 ERR("Called on attribute node\n");
1103 return E_UNEXPECTED;
1107 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1109 return E_OUTOFMEMORY;
1111 node->vtbl = &HTMLDOMNodeImplVtbl;
1112 HTMLDOMNode_Init(doc, node, nsnode);
1117 TRACE("type %d ret %p\n", node_type, *ret);
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);
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);
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);
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;
1167 htmldoc_release(doc);
1175 void init_node_cc(void)
1177 static const CCObjCallback node_ccp_callback = {
1178 HTMLDOMNode_unmark_if_purple,
1179 HTMLDOMNode_traverse,
1183 ccp_init(&node_ccp, &node_ccp_callback);
1186 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1188 nsISupports *unk = NULL;
1191 nsres = nsIDOMNode_GetMshtmlNode(nsnode, &unk);
1192 assert(nsres == NS_OK);
1195 *ret = get_node_obj(This, (IUnknown*)unk);
1204 return create_node(This, nsnode, ret);