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;
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));
288 ret->IHTMLDOMChildrenCollection_iface.lpVtbl = &HTMLDOMChildrenCollectionVtbl;
291 nsIDOMNodeList_AddRef(nslist);
292 ret->nslist = nslist;
294 htmldoc_addref(&doc->basedoc);
297 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLDOMChildrenCollection_iface,
298 &HTMLDOMChildrenCollection_dispex);
300 return &ret->IHTMLDOMChildrenCollection_iface;
303 static inline HTMLDOMNode *impl_from_IHTMLDOMNode(IHTMLDOMNode *iface)
305 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode_iface);
308 static HRESULT WINAPI HTMLDOMNode_QueryInterface(IHTMLDOMNode *iface,
309 REFIID riid, void **ppv)
311 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
313 return This->vtbl->qi(This, riid, ppv);
316 static ULONG WINAPI HTMLDOMNode_AddRef(IHTMLDOMNode *iface)
318 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
319 LONG ref = ccref_incr(&This->ccref, (nsISupports*)&This->IHTMLDOMNode_iface);
321 TRACE("(%p) ref=%d\n", This, ref);
326 static ULONG WINAPI HTMLDOMNode_Release(IHTMLDOMNode *iface)
328 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
329 LONG ref = ccref_decr(&This->ccref, (nsISupports*)&This->IHTMLDOMNode_iface);
331 TRACE("(%p) ref=%d\n", This, ref);
334 This->vtbl->destructor(This);
335 release_dispex(&This->dispex);
342 static HRESULT WINAPI HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode *iface, UINT *pctinfo)
344 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
345 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
348 static HRESULT WINAPI HTMLDOMNode_GetTypeInfo(IHTMLDOMNode *iface, UINT iTInfo,
349 LCID lcid, ITypeInfo **ppTInfo)
351 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
352 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
355 static HRESULT WINAPI HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode *iface, REFIID riid,
356 LPOLESTR *rgszNames, UINT cNames,
357 LCID lcid, DISPID *rgDispId)
359 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
360 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
364 static HRESULT WINAPI HTMLDOMNode_Invoke(IHTMLDOMNode *iface, DISPID dispIdMember,
365 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
366 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
368 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
369 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
370 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
373 static HRESULT WINAPI HTMLDOMNode_get_nodeType(IHTMLDOMNode *iface, LONG *p)
375 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
378 TRACE("(%p)->(%p)\n", This, p);
380 nsIDOMNode_GetNodeType(This->nsnode, &type);
395 case DOCUMENT_FRAGMENT_NODE:
401 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
402 * It needs more tests.
404 FIXME("type %u\n", type);
411 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
413 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
419 TRACE("(%p)->(%p)\n", This, p);
421 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
422 if(NS_FAILED(nsres)) {
423 ERR("GetParentNode failed: %08x\n", nsres);
432 hres = get_node(This->doc, nsnode, TRUE, &node);
433 nsIDOMNode_Release(nsnode);
437 *p = &node->IHTMLDOMNode_iface;
441 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
443 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
444 cpp_bool has_child = FALSE;
447 TRACE("(%p)->(%p)\n", This, fChildren);
449 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
451 ERR("HasChildNodes failed: %08x\n", nsres);
453 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
457 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
459 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
460 nsIDOMNodeList *nslist;
463 TRACE("(%p)->(%p)\n", This, p);
465 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
466 if(NS_FAILED(nsres)) {
467 ERR("GetChildNodes failed: %08x\n", nsres);
471 *p = (IDispatch*)create_child_collection(This->doc, nslist);
472 nsIDOMNodeList_Release(nslist);
474 return *p ? S_OK : E_OUTOFMEMORY;
477 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
479 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
480 HTMLAttributeCollection *col;
483 TRACE("(%p)->(%p)\n", This, p);
485 if(This->vtbl->get_attr_col) {
486 hres = This->vtbl->get_attr_col(This, &col);
490 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
498 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
499 VARIANT refChild, IHTMLDOMNode **node)
501 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
502 HTMLDOMNode *new_child, *node_obj, *ref_node = NULL;
507 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
509 new_child = get_node_obj(newChild);
511 ERR("invalid newChild\n");
515 switch(V_VT(&refChild)) {
519 IHTMLDOMNode *ref_iface;
521 if(!V_DISPATCH(&refChild))
524 hres = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IHTMLDOMNode, (void**)&ref_iface);
528 ref_node = get_node_obj(ref_iface);
529 IHTMLDOMNode_Release(ref_iface);
531 ERR("unvalid node\n");
538 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
542 if(SUCCEEDED(hres)) {
543 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, ref_node ? ref_node->nsnode : NULL, &nsnode);
544 if(NS_FAILED(nsres)) {
545 ERR("InsertBefore failed: %08x\n", nsres);
549 node_release(new_child);
551 node_release(ref_node);
555 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
556 nsIDOMNode_Release(nsnode);
560 *node = &node_obj->IHTMLDOMNode_iface;
564 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
567 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
568 HTMLDOMNode *node_obj;
573 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
575 node_obj = get_node_obj(oldChild);
579 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
580 node_release(node_obj);
581 if(NS_FAILED(nsres)) {
582 ERR("RemoveChild failed: %08x\n", nsres);
586 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
587 nsIDOMNode_Release(nsnode);
591 /* FIXME: Make sure that node != newChild */
592 *node = &node_obj->IHTMLDOMNode_iface;
596 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
597 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
599 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
600 HTMLDOMNode *node_new, *node_old, *ret_node;
605 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
607 node_new = get_node_obj(newChild);
611 node_old = get_node_obj(oldChild);
613 node_release(node_new);
617 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
618 node_release(node_new);
619 node_release(node_old);
623 hres = get_node(This->doc, nsnode, TRUE, &ret_node);
624 nsIDOMNode_Release(nsnode);
628 *node = &ret_node->IHTMLDOMNode_iface;
632 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
633 IHTMLDOMNode **clonedNode)
635 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
636 HTMLDOMNode *new_node;
641 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
643 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, 1, &nsnode);
644 if(NS_FAILED(nsres) || !nsnode) {
645 ERR("CloneNode failed: %08x\n", nsres);
649 hres = This->vtbl->clone(This, nsnode, &new_node);
653 *clonedNode = &new_node->IHTMLDOMNode_iface;
657 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
658 IHTMLDOMNode **removed)
660 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
661 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
665 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
666 IHTMLDOMNode **swappedNode)
668 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
669 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
673 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
674 IHTMLDOMNode **replaced)
676 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
677 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
681 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
684 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
685 HTMLDOMNode *node_obj;
690 TRACE("(%p)->(%p %p)\n", This, newChild, node);
692 node_obj = get_node_obj(newChild);
696 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
697 node_release(node_obj);
698 if(NS_FAILED(nsres)) {
699 ERR("AppendChild failed: %08x\n", nsres);
703 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
704 nsIDOMNode_Release(nsnode);
708 /* FIXME: Make sure that node != newChild */
709 *node = &node_obj->IHTMLDOMNode_iface;
713 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
715 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
719 TRACE("(%p)->(%p)\n", This, p);
721 nsAString_Init(&name, NULL);
722 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
723 return return_nsstr(nsres, &name, p);
726 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
728 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
730 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
736 nsAString_InitDepend(&val_str, V_BSTR(&v));
737 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
738 nsAString_Finish(&val_str);
744 FIXME("unsupported value %s\n", debugstr_variant(&v));
750 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
752 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
753 const PRUnichar *val;
756 TRACE("(%p)->(%p)\n", This, p);
758 nsAString_Init(&val_str, NULL);
759 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
760 nsAString_GetData(&val_str, &val);
764 V_BSTR(p) = SysAllocString(val);
769 nsAString_Finish(&val_str);
774 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
776 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
777 nsIDOMNode *nschild = NULL;
781 TRACE("(%p)->(%p)\n", This, p);
783 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
789 hres = get_node(This->doc, nschild, TRUE, &node);
790 nsIDOMNode_Release(nschild);
794 *p = &node->IHTMLDOMNode_iface;
798 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
800 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
801 nsIDOMNode *nschild = NULL;
805 TRACE("(%p)->(%p)\n", This, p);
807 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
813 hres = get_node(This->doc, nschild, TRUE, &node);
814 nsIDOMNode_Release(nschild);
818 *p = &node->IHTMLDOMNode_iface;
822 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
824 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
825 nsIDOMNode *nschild = NULL;
829 TRACE("(%p)->(%p)\n", This, p);
831 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
837 hres = get_node(This->doc, nschild, TRUE, &node);
838 nsIDOMNode_Release(nschild);
842 *p = &node->IHTMLDOMNode_iface;
846 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
848 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
849 nsIDOMNode *nssibling = NULL;
853 TRACE("(%p)->(%p)\n", This, p);
855 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
861 hres = get_node(This->doc, nssibling, TRUE, &node);
862 nsIDOMNode_Release(nssibling);
866 *p = &node->IHTMLDOMNode_iface;
870 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
871 HTMLDOMNode_QueryInterface,
874 HTMLDOMNode_GetTypeInfoCount,
875 HTMLDOMNode_GetTypeInfo,
876 HTMLDOMNode_GetIDsOfNames,
878 HTMLDOMNode_get_nodeType,
879 HTMLDOMNode_get_parentNode,
880 HTMLDOMNode_hasChildNodes,
881 HTMLDOMNode_get_childNodes,
882 HTMLDOMNode_get_attributes,
883 HTMLDOMNode_insertBefore,
884 HTMLDOMNode_removeChild,
885 HTMLDOMNode_replaceChild,
886 HTMLDOMNode_cloneNode,
887 HTMLDOMNode_removeNode,
888 HTMLDOMNode_swapNode,
889 HTMLDOMNode_replaceNode,
890 HTMLDOMNode_appendChild,
891 HTMLDOMNode_get_nodeName,
892 HTMLDOMNode_put_nodeValue,
893 HTMLDOMNode_get_nodeValue,
894 HTMLDOMNode_get_firstChild,
895 HTMLDOMNode_get_lastChild,
896 HTMLDOMNode_get_previousSibling,
897 HTMLDOMNode_get_nextSibling
900 static HTMLDOMNode *get_node_obj(IHTMLDOMNode *iface)
904 if(iface->lpVtbl != &HTMLDOMNodeVtbl)
907 ret = impl_from_IHTMLDOMNode(iface);
912 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
914 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
917 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
918 REFIID riid, void **ppv)
920 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
922 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
925 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
927 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
929 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
932 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
934 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
936 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
939 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
941 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
942 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
945 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
946 LCID lcid, ITypeInfo **ppTInfo)
948 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
949 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
952 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
953 LPOLESTR *rgszNames, UINT cNames,
954 LCID lcid, DISPID *rgDispId)
956 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
957 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
961 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
962 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
963 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
965 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
966 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
967 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
970 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
972 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
974 TRACE("(%p)->(%p)\n", This, p);
976 /* FIXME: Better check for document node */
977 if(This == &This->doc->node) {
980 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
981 IDispatch_AddRef(*p);
986 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
987 HTMLDOMNode2_QueryInterface,
989 HTMLDOMNode2_Release,
990 HTMLDOMNode2_GetTypeInfoCount,
991 HTMLDOMNode2_GetTypeInfo,
992 HTMLDOMNode2_GetIDsOfNames,
994 HTMLDOMNode2_get_ownerDocument
997 static nsXPCOMCycleCollectionParticipant node_ccp;
999 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
1003 if(IsEqualGUID(&IID_IUnknown, riid)) {
1004 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1005 *ppv = &This->IHTMLDOMNode_iface;
1006 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1007 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1008 *ppv = &This->IHTMLDOMNode_iface;
1009 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1010 if(This->dispex.data) {
1011 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1012 *ppv = &This->dispex.IDispatchEx_iface;
1014 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1015 return E_NOINTERFACE;
1017 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
1018 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
1019 *ppv = &This->IHTMLDOMNode_iface;
1020 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
1021 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
1022 *ppv = &This->IHTMLDOMNode2_iface;
1023 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant, riid)) {
1024 TRACE("(%p)->(IID_nsXPCOMCycleCollectionParticipant %p)\n", This, ppv);
1027 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1028 TRACE("(%p)->(IID_nsCycleCollectionISupports %p)\n", This, ppv);
1029 *ppv = &This->IHTMLDOMNode_iface;
1031 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1032 return *ppv ? S_OK : E_NOINTERFACE;
1036 IUnknown_AddRef((IUnknown*)*ppv);
1040 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1041 return E_NOINTERFACE;
1044 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1047 nsIDOMNode_Release(This->nsnode);
1048 if(This->doc && &This->doc->node != This)
1049 htmldoc_release(&This->doc->basedoc);
1050 if(This->event_target)
1051 release_event_target(This->event_target);
1054 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1056 return create_node(This->doc, nsnode, ret);
1059 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1061 HTMLDOMNode_destructor,
1065 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1069 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1070 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1072 ccref_init(&node->ccref, 1);
1074 if(&doc->node != node)
1075 htmldoc_addref(&doc->basedoc);
1078 nsIDOMNode_AddRef(nsnode);
1079 node->nsnode = nsnode;
1081 nsres = nsIDOMNode_SetMshtmlNode(nsnode, (nsISupports*)&node->IHTMLDOMNode_iface);
1082 assert(nsres == NS_OK);
1085 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1090 nsIDOMNode_GetNodeType(nsnode, &node_type);
1093 case ELEMENT_NODE: {
1095 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1102 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1106 case COMMENT_NODE: {
1107 HTMLElement *comment;
1108 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1111 *ret = &comment->node;
1114 case ATTRIBUTE_NODE:
1115 ERR("Called on attribute node\n");
1116 return E_UNEXPECTED;
1120 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1122 return E_OUTOFMEMORY;
1124 node->vtbl = &HTMLDOMNodeImplVtbl;
1125 HTMLDOMNode_Init(doc, node, nsnode);
1130 TRACE("type %d ret %p\n", node_type, *ret);
1134 static void NSAPI HTMLDOMNode_unmark_if_purple(void *p)
1136 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1137 ccref_unmark_if_purple(&This->ccref);
1140 static nsresult NSAPI HTMLDOMNode_traverse(void *ccp, void *p, nsCycleCollectionTraversalCallback *cb)
1142 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1144 TRACE("%p\n", This);
1146 describe_cc_node(&This->ccref, sizeof(*This), "HTMLDOMNode", cb);
1149 note_cc_edge((nsISupports*)This->nsnode, "This->nsnode", cb);
1150 if(This->doc && &This->doc->node != This)
1151 note_cc_edge((nsISupports*)&This->doc->node.IHTMLDOMNode_iface, "This->doc", cb);
1152 dispex_traverse(&This->dispex, cb);
1154 if(This->vtbl->traverse)
1155 This->vtbl->traverse(This, cb);
1160 static nsresult NSAPI HTMLDOMNode_unlink(void *p)
1162 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1164 TRACE("%p\n", This);
1166 if(This->vtbl->unlink)
1167 This->vtbl->unlink(This);
1169 dispex_unlink(&This->dispex);
1172 nsIDOMNode *nsnode = This->nsnode;
1173 This->nsnode = NULL;
1174 nsIDOMNode_Release(nsnode);
1177 if(This->doc && &This->doc->node != This) {
1178 HTMLDocument *doc = &This->doc->basedoc;
1180 htmldoc_release(doc);
1188 void init_node_cc(void)
1190 static const CCObjCallback node_ccp_callback = {
1191 HTMLDOMNode_unmark_if_purple,
1192 HTMLDOMNode_traverse,
1196 ccp_init(&node_ccp, &node_ccp_callback);
1199 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1201 nsISupports *unk = NULL;
1204 nsres = nsIDOMNode_GetMshtmlNode(nsnode, &unk);
1205 assert(nsres == NS_OK);
1208 *ret = get_node_obj((IHTMLDOMNode*)unk);
1217 return create_node(This, nsnode, ret);