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 ref_node = get_node_obj(This->doc, (IUnknown*)V_DISPATCH(&refChild));
518 ERR("unvalid node\n");
525 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
529 if(SUCCEEDED(hres)) {
530 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, ref_node ? ref_node->nsnode : NULL, &nsnode);
531 if(NS_FAILED(nsres)) {
532 ERR("InsertBefore failed: %08x\n", nsres);
536 node_release(new_child);
538 node_release(ref_node);
542 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
543 nsIDOMNode_Release(nsnode);
547 *node = &node_obj->IHTMLDOMNode_iface;
551 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
554 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
555 HTMLDOMNode *node_obj;
560 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
562 node_obj = get_node_obj(This->doc, (IUnknown*)oldChild);
566 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
567 node_release(node_obj);
568 if(NS_FAILED(nsres)) {
569 ERR("RemoveChild failed: %08x\n", nsres);
573 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
574 nsIDOMNode_Release(nsnode);
578 /* FIXME: Make sure that node != newChild */
579 *node = &node_obj->IHTMLDOMNode_iface;
583 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
584 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
586 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
587 HTMLDOMNode *node_new, *node_old, *ret_node;
592 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
594 node_new = get_node_obj(This->doc, (IUnknown*)newChild);
598 node_old = get_node_obj(This->doc, (IUnknown*)oldChild);
600 node_release(node_new);
604 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
605 node_release(node_new);
606 node_release(node_old);
610 hres = get_node(This->doc, nsnode, TRUE, &ret_node);
611 nsIDOMNode_Release(nsnode);
615 *node = &ret_node->IHTMLDOMNode_iface;
619 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
620 IHTMLDOMNode **clonedNode)
622 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
623 HTMLDOMNode *new_node;
628 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
630 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, 1, &nsnode);
631 if(NS_FAILED(nsres) || !nsnode) {
632 ERR("CloneNode failed: %08x\n", nsres);
636 hres = This->vtbl->clone(This, nsnode, &new_node);
640 *clonedNode = &new_node->IHTMLDOMNode_iface;
644 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
645 IHTMLDOMNode **removed)
647 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
648 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
652 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
653 IHTMLDOMNode **swappedNode)
655 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
656 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
660 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
661 IHTMLDOMNode **replaced)
663 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
664 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
668 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
671 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
672 HTMLDOMNode *node_obj;
677 TRACE("(%p)->(%p %p)\n", This, newChild, node);
679 node_obj = get_node_obj(This->doc, (IUnknown*)newChild);
683 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
684 node_release(node_obj);
685 if(NS_FAILED(nsres)) {
686 ERR("AppendChild failed: %08x\n", nsres);
690 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
691 nsIDOMNode_Release(nsnode);
695 /* FIXME: Make sure that node != newChild */
696 *node = &node_obj->IHTMLDOMNode_iface;
700 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
702 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
706 TRACE("(%p)->(%p)\n", This, p);
708 nsAString_Init(&name, NULL);
709 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
710 return return_nsstr(nsres, &name, p);
713 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
715 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
717 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
723 nsAString_InitDepend(&val_str, V_BSTR(&v));
724 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
725 nsAString_Finish(&val_str);
731 FIXME("unsupported value %s\n", debugstr_variant(&v));
737 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
739 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
740 const PRUnichar *val;
743 TRACE("(%p)->(%p)\n", This, p);
745 nsAString_Init(&val_str, NULL);
746 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
747 nsAString_GetData(&val_str, &val);
751 V_BSTR(p) = SysAllocString(val);
756 nsAString_Finish(&val_str);
761 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
763 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
764 nsIDOMNode *nschild = NULL;
768 TRACE("(%p)->(%p)\n", This, p);
770 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
776 hres = get_node(This->doc, nschild, TRUE, &node);
777 nsIDOMNode_Release(nschild);
781 *p = &node->IHTMLDOMNode_iface;
785 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
787 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
788 nsIDOMNode *nschild = NULL;
792 TRACE("(%p)->(%p)\n", This, p);
794 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
800 hres = get_node(This->doc, nschild, TRUE, &node);
801 nsIDOMNode_Release(nschild);
805 *p = &node->IHTMLDOMNode_iface;
809 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
811 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
812 nsIDOMNode *nschild = NULL;
816 TRACE("(%p)->(%p)\n", This, p);
818 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
824 hres = get_node(This->doc, nschild, TRUE, &node);
825 nsIDOMNode_Release(nschild);
829 *p = &node->IHTMLDOMNode_iface;
833 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
835 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
836 nsIDOMNode *nssibling = NULL;
840 TRACE("(%p)->(%p)\n", This, p);
842 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
848 hres = get_node(This->doc, nssibling, TRUE, &node);
849 nsIDOMNode_Release(nssibling);
853 *p = &node->IHTMLDOMNode_iface;
857 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
858 HTMLDOMNode_QueryInterface,
861 HTMLDOMNode_GetTypeInfoCount,
862 HTMLDOMNode_GetTypeInfo,
863 HTMLDOMNode_GetIDsOfNames,
865 HTMLDOMNode_get_nodeType,
866 HTMLDOMNode_get_parentNode,
867 HTMLDOMNode_hasChildNodes,
868 HTMLDOMNode_get_childNodes,
869 HTMLDOMNode_get_attributes,
870 HTMLDOMNode_insertBefore,
871 HTMLDOMNode_removeChild,
872 HTMLDOMNode_replaceChild,
873 HTMLDOMNode_cloneNode,
874 HTMLDOMNode_removeNode,
875 HTMLDOMNode_swapNode,
876 HTMLDOMNode_replaceNode,
877 HTMLDOMNode_appendChild,
878 HTMLDOMNode_get_nodeName,
879 HTMLDOMNode_put_nodeValue,
880 HTMLDOMNode_get_nodeValue,
881 HTMLDOMNode_get_firstChild,
882 HTMLDOMNode_get_lastChild,
883 HTMLDOMNode_get_previousSibling,
884 HTMLDOMNode_get_nextSibling
887 static HTMLDOMNode *get_node_obj(HTMLDocumentNode *This, IUnknown *iface)
892 hres = IUnknown_QueryInterface(iface, &IID_IHTMLDOMNode, (void**)&node);
893 return hres == S_OK && node->lpVtbl == &HTMLDOMNodeVtbl ? impl_from_IHTMLDOMNode(node) : NULL;
896 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
898 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
901 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
902 REFIID riid, void **ppv)
904 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
906 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
909 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
911 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
913 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
916 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
918 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
920 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
923 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
925 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
926 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
929 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
930 LCID lcid, ITypeInfo **ppTInfo)
932 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
933 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
936 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
937 LPOLESTR *rgszNames, UINT cNames,
938 LCID lcid, DISPID *rgDispId)
940 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
941 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
945 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
946 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
947 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
949 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
950 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
951 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
954 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
956 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
958 TRACE("(%p)->(%p)\n", This, p);
960 /* FIXME: Better check for document node */
961 if(This == &This->doc->node) {
964 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
965 IDispatch_AddRef(*p);
970 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
971 HTMLDOMNode2_QueryInterface,
973 HTMLDOMNode2_Release,
974 HTMLDOMNode2_GetTypeInfoCount,
975 HTMLDOMNode2_GetTypeInfo,
976 HTMLDOMNode2_GetIDsOfNames,
978 HTMLDOMNode2_get_ownerDocument
981 static nsXPCOMCycleCollectionParticipant node_ccp;
983 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
987 if(IsEqualGUID(&IID_IUnknown, riid)) {
988 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
989 *ppv = &This->IHTMLDOMNode_iface;
990 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
991 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
992 *ppv = &This->IHTMLDOMNode_iface;
993 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
994 if(This->dispex.data) {
995 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
996 *ppv = &This->dispex.IDispatchEx_iface;
998 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
999 return E_NOINTERFACE;
1001 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
1002 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
1003 *ppv = &This->IHTMLDOMNode_iface;
1004 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
1005 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
1006 *ppv = &This->IHTMLDOMNode2_iface;
1007 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant, riid)) {
1008 TRACE("(%p)->(IID_nsXPCOMCycleCollectionParticipant %p)\n", This, ppv);
1011 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1012 TRACE("(%p)->(IID_nsCycleCollectionISupports %p)\n", This, ppv);
1013 *ppv = &This->IHTMLDOMNode_iface;
1015 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1016 return *ppv ? S_OK : E_NOINTERFACE;
1020 IUnknown_AddRef((IUnknown*)*ppv);
1024 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1025 return E_NOINTERFACE;
1028 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1031 nsIDOMNode_Release(This->nsnode);
1032 if(This->doc && &This->doc->node != This)
1033 htmldoc_release(&This->doc->basedoc);
1034 if(This->event_target)
1035 release_event_target(This->event_target);
1038 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1040 return create_node(This->doc, nsnode, ret);
1043 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1045 HTMLDOMNode_destructor,
1049 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1053 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1054 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1056 ccref_init(&node->ccref, 1);
1058 if(&doc->node != node)
1059 htmldoc_addref(&doc->basedoc);
1063 nsIDOMNode_AddRef(nsnode);
1064 node->nsnode = nsnode;
1066 nsres = nsIDOMNode_SetMshtmlNode(nsnode, (nsISupports*)&node->IHTMLDOMNode_iface);
1067 assert(nsres == NS_OK);
1070 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1075 nsIDOMNode_GetNodeType(nsnode, &node_type);
1078 case ELEMENT_NODE: {
1080 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1087 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1091 case COMMENT_NODE: {
1092 HTMLElement *comment;
1093 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1096 *ret = &comment->node;
1099 case ATTRIBUTE_NODE:
1100 ERR("Called on attribute node\n");
1101 return E_UNEXPECTED;
1105 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1107 return E_OUTOFMEMORY;
1109 node->vtbl = &HTMLDOMNodeImplVtbl;
1110 HTMLDOMNode_Init(doc, node, nsnode);
1115 TRACE("type %d ret %p\n", node_type, *ret);
1119 static void NSAPI HTMLDOMNode_unmark_if_purple(void *p)
1121 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1122 ccref_unmark_if_purple(&This->ccref);
1125 static nsresult NSAPI HTMLDOMNode_traverse(void *ccp, void *p, nsCycleCollectionTraversalCallback *cb)
1127 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1129 TRACE("%p\n", This);
1131 describe_cc_node(&This->ccref, sizeof(*This), "HTMLDOMNode", cb);
1134 note_cc_edge((nsISupports*)This->nsnode, "This->nsnode", cb);
1135 if(This->doc && &This->doc->node != This)
1136 note_cc_edge((nsISupports*)&This->doc->node.IHTMLDOMNode_iface, "This->doc", cb);
1137 dispex_traverse(&This->dispex, cb);
1139 if(This->vtbl->traverse)
1140 This->vtbl->traverse(This, cb);
1145 static nsresult NSAPI HTMLDOMNode_unlink(void *p)
1147 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1149 TRACE("%p\n", This);
1151 if(This->vtbl->unlink)
1152 This->vtbl->unlink(This);
1154 dispex_unlink(&This->dispex);
1157 nsIDOMNode *nsnode = This->nsnode;
1158 This->nsnode = NULL;
1159 nsIDOMNode_Release(nsnode);
1162 if(This->doc && &This->doc->node != This) {
1163 HTMLDocument *doc = &This->doc->basedoc;
1165 htmldoc_release(doc);
1173 void init_node_cc(void)
1175 static const CCObjCallback node_ccp_callback = {
1176 HTMLDOMNode_unmark_if_purple,
1177 HTMLDOMNode_traverse,
1181 ccp_init(&node_ccp, &node_ccp_callback);
1184 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1186 nsISupports *unk = NULL;
1189 nsres = nsIDOMNode_GetMshtmlNode(nsnode, &unk);
1190 assert(nsres == NS_OK);
1193 *ret = get_node_obj(This, (IUnknown*)unk);
1202 return create_node(This, nsnode, ret);