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);
390 case DOCUMENT_TYPE_NODE:
396 case DOCUMENT_FRAGMENT_NODE:
402 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
403 * It needs more tests.
405 FIXME("type %u\n", type);
412 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
414 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
420 TRACE("(%p)->(%p)\n", This, p);
422 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
423 if(NS_FAILED(nsres)) {
424 ERR("GetParentNode failed: %08x\n", nsres);
433 hres = get_node(This->doc, nsnode, TRUE, &node);
434 nsIDOMNode_Release(nsnode);
438 *p = &node->IHTMLDOMNode_iface;
442 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
444 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
445 cpp_bool has_child = FALSE;
448 TRACE("(%p)->(%p)\n", This, fChildren);
450 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
452 ERR("HasChildNodes failed: %08x\n", nsres);
454 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
458 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
460 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
461 nsIDOMNodeList *nslist;
464 TRACE("(%p)->(%p)\n", This, p);
466 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
467 if(NS_FAILED(nsres)) {
468 ERR("GetChildNodes failed: %08x\n", nsres);
472 *p = (IDispatch*)create_child_collection(This->doc, nslist);
473 nsIDOMNodeList_Release(nslist);
475 return *p ? S_OK : E_OUTOFMEMORY;
478 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
480 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
481 HTMLAttributeCollection *col;
484 TRACE("(%p)->(%p)\n", This, p);
486 if(This->vtbl->get_attr_col) {
487 hres = This->vtbl->get_attr_col(This, &col);
491 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
499 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
500 VARIANT refChild, IHTMLDOMNode **node)
502 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
503 HTMLDOMNode *new_child, *node_obj, *ref_node = NULL;
508 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
510 new_child = get_node_obj(newChild);
512 ERR("invalid newChild\n");
516 switch(V_VT(&refChild)) {
520 IHTMLDOMNode *ref_iface;
522 if(!V_DISPATCH(&refChild))
525 hres = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IHTMLDOMNode, (void**)&ref_iface);
529 ref_node = get_node_obj(ref_iface);
530 IHTMLDOMNode_Release(ref_iface);
532 ERR("unvalid node\n");
539 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
543 if(SUCCEEDED(hres)) {
544 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, ref_node ? ref_node->nsnode : NULL, &nsnode);
545 if(NS_FAILED(nsres)) {
546 ERR("InsertBefore failed: %08x\n", nsres);
550 node_release(new_child);
552 node_release(ref_node);
556 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
557 nsIDOMNode_Release(nsnode);
561 *node = &node_obj->IHTMLDOMNode_iface;
565 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
568 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
569 HTMLDOMNode *node_obj;
574 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
576 node_obj = get_node_obj(oldChild);
580 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
581 node_release(node_obj);
582 if(NS_FAILED(nsres)) {
583 ERR("RemoveChild failed: %08x\n", nsres);
587 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
588 nsIDOMNode_Release(nsnode);
592 /* FIXME: Make sure that node != newChild */
593 *node = &node_obj->IHTMLDOMNode_iface;
597 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
598 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
600 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
601 HTMLDOMNode *node_new, *node_old, *ret_node;
606 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
608 node_new = get_node_obj(newChild);
612 node_old = get_node_obj(oldChild);
614 node_release(node_new);
618 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
619 node_release(node_new);
620 node_release(node_old);
624 hres = get_node(This->doc, nsnode, TRUE, &ret_node);
625 nsIDOMNode_Release(nsnode);
629 *node = &ret_node->IHTMLDOMNode_iface;
633 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
634 IHTMLDOMNode **clonedNode)
636 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
637 HTMLDOMNode *new_node;
642 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
644 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, 1, &nsnode);
645 if(NS_FAILED(nsres) || !nsnode) {
646 ERR("CloneNode failed: %08x\n", nsres);
650 hres = This->vtbl->clone(This, nsnode, &new_node);
654 *clonedNode = &new_node->IHTMLDOMNode_iface;
658 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
659 IHTMLDOMNode **removed)
661 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
662 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
666 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
667 IHTMLDOMNode **swappedNode)
669 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
670 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
674 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
675 IHTMLDOMNode **replaced)
677 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
678 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
682 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
685 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
686 HTMLDOMNode *node_obj;
691 TRACE("(%p)->(%p %p)\n", This, newChild, node);
693 node_obj = get_node_obj(newChild);
697 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
698 node_release(node_obj);
699 if(NS_FAILED(nsres)) {
700 ERR("AppendChild failed: %08x\n", nsres);
704 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
705 nsIDOMNode_Release(nsnode);
709 /* FIXME: Make sure that node != newChild */
710 *node = &node_obj->IHTMLDOMNode_iface;
714 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
716 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
720 TRACE("(%p)->(%p)\n", This, p);
722 nsAString_Init(&name, NULL);
723 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
724 return return_nsstr(nsres, &name, p);
727 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
729 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
731 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
737 nsAString_InitDepend(&val_str, V_BSTR(&v));
738 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
739 nsAString_Finish(&val_str);
745 FIXME("unsupported value %s\n", debugstr_variant(&v));
751 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
753 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
754 const PRUnichar *val;
757 TRACE("(%p)->(%p)\n", This, p);
759 nsAString_Init(&val_str, NULL);
760 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
761 nsAString_GetData(&val_str, &val);
765 V_BSTR(p) = SysAllocString(val);
770 nsAString_Finish(&val_str);
775 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
777 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
778 nsIDOMNode *nschild = NULL;
782 TRACE("(%p)->(%p)\n", This, p);
784 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
790 hres = get_node(This->doc, nschild, TRUE, &node);
791 nsIDOMNode_Release(nschild);
795 *p = &node->IHTMLDOMNode_iface;
799 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
801 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
802 nsIDOMNode *nschild = NULL;
806 TRACE("(%p)->(%p)\n", This, p);
808 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
814 hres = get_node(This->doc, nschild, TRUE, &node);
815 nsIDOMNode_Release(nschild);
819 *p = &node->IHTMLDOMNode_iface;
823 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
825 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
826 nsIDOMNode *nschild = NULL;
830 TRACE("(%p)->(%p)\n", This, p);
832 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
838 hres = get_node(This->doc, nschild, TRUE, &node);
839 nsIDOMNode_Release(nschild);
843 *p = &node->IHTMLDOMNode_iface;
847 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
849 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
850 nsIDOMNode *nssibling = NULL;
854 TRACE("(%p)->(%p)\n", This, p);
856 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
862 hres = get_node(This->doc, nssibling, TRUE, &node);
863 nsIDOMNode_Release(nssibling);
867 *p = &node->IHTMLDOMNode_iface;
871 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
872 HTMLDOMNode_QueryInterface,
875 HTMLDOMNode_GetTypeInfoCount,
876 HTMLDOMNode_GetTypeInfo,
877 HTMLDOMNode_GetIDsOfNames,
879 HTMLDOMNode_get_nodeType,
880 HTMLDOMNode_get_parentNode,
881 HTMLDOMNode_hasChildNodes,
882 HTMLDOMNode_get_childNodes,
883 HTMLDOMNode_get_attributes,
884 HTMLDOMNode_insertBefore,
885 HTMLDOMNode_removeChild,
886 HTMLDOMNode_replaceChild,
887 HTMLDOMNode_cloneNode,
888 HTMLDOMNode_removeNode,
889 HTMLDOMNode_swapNode,
890 HTMLDOMNode_replaceNode,
891 HTMLDOMNode_appendChild,
892 HTMLDOMNode_get_nodeName,
893 HTMLDOMNode_put_nodeValue,
894 HTMLDOMNode_get_nodeValue,
895 HTMLDOMNode_get_firstChild,
896 HTMLDOMNode_get_lastChild,
897 HTMLDOMNode_get_previousSibling,
898 HTMLDOMNode_get_nextSibling
901 static HTMLDOMNode *get_node_obj(IHTMLDOMNode *iface)
905 if(iface->lpVtbl != &HTMLDOMNodeVtbl)
908 ret = impl_from_IHTMLDOMNode(iface);
913 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
915 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
918 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
919 REFIID riid, void **ppv)
921 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
923 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
926 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
928 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
930 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
933 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
935 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
937 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
940 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
942 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
943 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
946 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
947 LCID lcid, ITypeInfo **ppTInfo)
949 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
950 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
953 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
954 LPOLESTR *rgszNames, UINT cNames,
955 LCID lcid, DISPID *rgDispId)
957 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
958 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
962 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
963 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
964 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
966 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
967 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
968 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
971 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
973 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
975 TRACE("(%p)->(%p)\n", This, p);
977 /* FIXME: Better check for document node */
978 if(This == &This->doc->node) {
981 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
982 IDispatch_AddRef(*p);
987 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
988 HTMLDOMNode2_QueryInterface,
990 HTMLDOMNode2_Release,
991 HTMLDOMNode2_GetTypeInfoCount,
992 HTMLDOMNode2_GetTypeInfo,
993 HTMLDOMNode2_GetIDsOfNames,
995 HTMLDOMNode2_get_ownerDocument
998 static nsXPCOMCycleCollectionParticipant node_ccp;
1000 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
1004 if(IsEqualGUID(&IID_IUnknown, riid)) {
1005 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1006 *ppv = &This->IHTMLDOMNode_iface;
1007 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1008 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1009 *ppv = &This->IHTMLDOMNode_iface;
1010 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1011 if(This->dispex.data) {
1012 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1013 *ppv = &This->dispex.IDispatchEx_iface;
1015 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1016 return E_NOINTERFACE;
1018 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
1019 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
1020 *ppv = &This->IHTMLDOMNode_iface;
1021 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
1022 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
1023 *ppv = &This->IHTMLDOMNode2_iface;
1024 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant, riid)) {
1025 TRACE("(%p)->(IID_nsXPCOMCycleCollectionParticipant %p)\n", This, ppv);
1028 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1029 TRACE("(%p)->(IID_nsCycleCollectionISupports %p)\n", This, ppv);
1030 *ppv = &This->IHTMLDOMNode_iface;
1032 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1033 return *ppv ? S_OK : E_NOINTERFACE;
1037 IUnknown_AddRef((IUnknown*)*ppv);
1041 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1042 return E_NOINTERFACE;
1045 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1048 nsIDOMNode_Release(This->nsnode);
1049 if(This->doc && &This->doc->node != This)
1050 htmldoc_release(&This->doc->basedoc);
1051 if(This->event_target)
1052 release_event_target(This->event_target);
1055 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1057 return create_node(This->doc, nsnode, ret);
1060 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1062 HTMLDOMNode_destructor,
1066 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1070 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1071 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1073 ccref_init(&node->ccref, 1);
1075 if(&doc->node != node)
1076 htmldoc_addref(&doc->basedoc);
1079 nsIDOMNode_AddRef(nsnode);
1080 node->nsnode = nsnode;
1082 nsres = nsIDOMNode_SetMshtmlNode(nsnode, (nsISupports*)&node->IHTMLDOMNode_iface);
1083 assert(nsres == NS_OK);
1086 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1091 nsIDOMNode_GetNodeType(nsnode, &node_type);
1094 case ELEMENT_NODE: {
1096 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1103 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1107 /* doctype nodes are represented as comment nodes (at least in quirks mode) */
1108 case DOCUMENT_TYPE_NODE:
1109 case COMMENT_NODE: {
1110 HTMLElement *comment;
1111 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1114 *ret = &comment->node;
1117 case ATTRIBUTE_NODE:
1118 ERR("Called on attribute node\n");
1119 return E_UNEXPECTED;
1123 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1125 return E_OUTOFMEMORY;
1127 node->vtbl = &HTMLDOMNodeImplVtbl;
1128 HTMLDOMNode_Init(doc, node, nsnode);
1133 TRACE("type %d ret %p\n", node_type, *ret);
1137 static void NSAPI HTMLDOMNode_unmark_if_purple(void *p)
1139 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1140 ccref_unmark_if_purple(&This->ccref);
1143 static nsresult NSAPI HTMLDOMNode_traverse(void *ccp, void *p, nsCycleCollectionTraversalCallback *cb)
1145 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1147 TRACE("%p\n", This);
1149 describe_cc_node(&This->ccref, "HTMLDOMNode", cb);
1152 note_cc_edge((nsISupports*)This->nsnode, "This->nsnode", cb);
1153 if(This->doc && &This->doc->node != This)
1154 note_cc_edge((nsISupports*)&This->doc->node.IHTMLDOMNode_iface, "This->doc", cb);
1155 dispex_traverse(&This->dispex, cb);
1157 if(This->vtbl->traverse)
1158 This->vtbl->traverse(This, cb);
1163 static nsresult NSAPI HTMLDOMNode_unlink(void *p)
1165 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1167 TRACE("%p\n", This);
1169 if(This->vtbl->unlink)
1170 This->vtbl->unlink(This);
1172 dispex_unlink(&This->dispex);
1175 nsIDOMNode *nsnode = This->nsnode;
1176 This->nsnode = NULL;
1177 nsIDOMNode_Release(nsnode);
1180 if(This->doc && &This->doc->node != This) {
1181 HTMLDocument *doc = &This->doc->basedoc;
1183 htmldoc_release(doc);
1191 void init_node_cc(void)
1193 static const CCObjCallback node_ccp_callback = {
1194 HTMLDOMNode_unmark_if_purple,
1195 HTMLDOMNode_traverse,
1199 ccp_init(&node_ccp, &node_ccp_callback);
1202 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1204 nsISupports *unk = NULL;
1207 nsres = nsIDOMNode_GetMshtmlNode(nsnode, &unk);
1208 assert(nsres == NS_OK);
1211 *ret = get_node_obj((IHTMLDOMNode*)unk);
1220 return create_node(This, nsnode, ret);