msvcrt: Pass pthreadlocinfo to update_threadlocinfo_category helper function.
[wine.git] / dlls / mshtml / htmlnode.c
blob2e07c0b9be74c15ba31a19da7b24b2985fc6a4da
1 /*
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 static HTMLDOMNode *get_node_obj(IHTMLDOMNode*);
37 static HRESULT create_node(HTMLDocumentNode*,nsIDOMNode*,HTMLDOMNode**);
39 static ExternalCycleCollectionParticipant node_ccp;
41 typedef struct {
42 DispatchEx dispex;
43 IHTMLDOMChildrenCollection IHTMLDOMChildrenCollection_iface;
45 LONG ref;
47 /* FIXME: implement weak reference */
48 HTMLDocumentNode *doc;
50 nsIDOMNodeList *nslist;
51 } HTMLDOMChildrenCollection;
53 typedef struct {
54 IEnumVARIANT IEnumVARIANT_iface;
56 LONG ref;
58 ULONG iter;
59 HTMLDOMChildrenCollection *col;
60 } HTMLDOMChildrenCollectionEnum;
62 static inline HTMLDOMChildrenCollectionEnum *impl_from_IEnumVARIANT(IEnumVARIANT *iface)
64 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollectionEnum, IEnumVARIANT_iface);
67 static HRESULT WINAPI HTMLDOMChildrenCollectionEnum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
69 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
71 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
73 if(IsEqualGUID(riid, &IID_IUnknown)) {
74 *ppv = &This->IEnumVARIANT_iface;
75 }else if(IsEqualGUID(riid, &IID_IEnumVARIANT)) {
76 *ppv = &This->IEnumVARIANT_iface;
77 }else {
78 FIXME("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
79 *ppv = NULL;
80 return E_NOINTERFACE;
83 IUnknown_AddRef((IUnknown*)*ppv);
84 return S_OK;
87 static ULONG WINAPI HTMLDOMChildrenCollectionEnum_AddRef(IEnumVARIANT *iface)
89 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
90 LONG ref = InterlockedIncrement(&This->ref);
92 TRACE("(%p) ref=%d\n", This, ref);
94 return ref;
97 static ULONG WINAPI HTMLDOMChildrenCollectionEnum_Release(IEnumVARIANT *iface)
99 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
100 LONG ref = InterlockedDecrement(&This->ref);
102 TRACE("(%p) ref=%d\n", This, ref);
104 if(!ref) {
105 IHTMLDOMChildrenCollection_Release(&This->col->IHTMLDOMChildrenCollection_iface);
106 heap_free(This);
109 return ref;
112 static ULONG get_enum_len(HTMLDOMChildrenCollectionEnum *This)
114 ULONG len;
115 nsresult nsres;
117 nsres = nsIDOMNodeList_GetLength(This->col->nslist, &len);
118 assert(nsres == NS_OK);
119 return len;
122 static HRESULT WINAPI HTMLDOMChildrenCollectionEnum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
124 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
125 ULONG fetched = 0, len;
126 nsIDOMNode *nsnode;
127 HTMLDOMNode *node;
128 nsresult nsres;
129 HRESULT hres;
131 TRACE("(%p)->(%d %p %p)\n", This, celt, rgVar, pCeltFetched);
133 len = get_enum_len(This);
135 while(This->iter+fetched < len && fetched < celt) {
136 nsres = nsIDOMNodeList_Item(This->col->nslist, This->iter+fetched, &nsnode);
137 assert(nsres == NS_OK);
139 hres = get_node(This->col->doc, nsnode, TRUE, &node);
140 nsIDOMNode_Release(nsnode);
141 if(FAILED(hres)) {
142 ERR("get_node failed: %08x\n", hres);
143 break;
146 V_VT(rgVar+fetched) = VT_DISPATCH;
147 IHTMLDOMNode_AddRef(&node->IHTMLDOMNode_iface);
148 V_DISPATCH(rgVar+fetched) = (IDispatch*)&node->IHTMLDOMNode_iface;
149 fetched++;
152 This->iter += fetched;
153 if(pCeltFetched)
154 *pCeltFetched = fetched;
155 return fetched == celt ? S_OK : S_FALSE;
158 static HRESULT WINAPI HTMLDOMChildrenCollectionEnum_Skip(IEnumVARIANT *iface, ULONG celt)
160 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
161 ULONG len;
163 TRACE("(%p)->(%d)\n", This, celt);
165 len = get_enum_len(This);
166 if(This->iter + celt > len) {
167 This->iter = len;
168 return S_FALSE;
171 This->iter += celt;
172 return S_OK;
175 static HRESULT WINAPI HTMLDOMChildrenCollectionEnum_Reset(IEnumVARIANT *iface)
177 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
179 TRACE("(%p)->()\n", This);
181 This->iter = 0;
182 return S_OK;
185 static HRESULT WINAPI HTMLDOMChildrenCollectionEnum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
187 HTMLDOMChildrenCollectionEnum *This = impl_from_IEnumVARIANT(iface);
188 FIXME("(%p)->(%p)\n", This, ppEnum);
189 return E_NOTIMPL;
192 static const IEnumVARIANTVtbl HTMLDOMChildrenCollectionEnumVtbl = {
193 HTMLDOMChildrenCollectionEnum_QueryInterface,
194 HTMLDOMChildrenCollectionEnum_AddRef,
195 HTMLDOMChildrenCollectionEnum_Release,
196 HTMLDOMChildrenCollectionEnum_Next,
197 HTMLDOMChildrenCollectionEnum_Skip,
198 HTMLDOMChildrenCollectionEnum_Reset,
199 HTMLDOMChildrenCollectionEnum_Clone
202 static inline HTMLDOMChildrenCollection *impl_from_IHTMLDOMChildrenCollection(IHTMLDOMChildrenCollection *iface)
204 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, IHTMLDOMChildrenCollection_iface);
207 static HRESULT WINAPI HTMLDOMChildrenCollection_QueryInterface(IHTMLDOMChildrenCollection *iface, REFIID riid, void **ppv)
209 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
211 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
213 if(IsEqualGUID(&IID_IUnknown, riid)) {
214 *ppv = &This->IHTMLDOMChildrenCollection_iface;
215 }else if(IsEqualGUID(&IID_IHTMLDOMChildrenCollection, riid)) {
216 *ppv = &This->IHTMLDOMChildrenCollection_iface;
217 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
218 return *ppv ? S_OK : E_NOINTERFACE;
219 }else {
220 *ppv = NULL;
221 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
222 return E_NOINTERFACE;
225 IUnknown_AddRef((IUnknown*)*ppv);
226 return S_OK;
229 static ULONG WINAPI HTMLDOMChildrenCollection_AddRef(IHTMLDOMChildrenCollection *iface)
231 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
232 LONG ref = InterlockedIncrement(&This->ref);
234 TRACE("(%p) ref=%d\n", This, ref);
236 return ref;
239 static ULONG WINAPI HTMLDOMChildrenCollection_Release(IHTMLDOMChildrenCollection *iface)
241 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
242 LONG ref = InterlockedDecrement(&This->ref);
244 TRACE("(%p) ref=%d\n", This, ref);
246 if(!ref) {
247 htmldoc_release(&This->doc->basedoc);
248 nsIDOMNodeList_Release(This->nslist);
249 heap_free(This);
252 return ref;
255 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfoCount(IHTMLDOMChildrenCollection *iface, UINT *pctinfo)
257 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
258 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
261 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfo(IHTMLDOMChildrenCollection *iface, UINT iTInfo,
262 LCID lcid, ITypeInfo **ppTInfo)
264 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
265 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
268 static HRESULT WINAPI HTMLDOMChildrenCollection_GetIDsOfNames(IHTMLDOMChildrenCollection *iface, REFIID riid,
269 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
271 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
272 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
273 lcid, rgDispId);
276 static HRESULT WINAPI HTMLDOMChildrenCollection_Invoke(IHTMLDOMChildrenCollection *iface, DISPID dispIdMember,
277 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
278 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
280 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
281 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
282 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
285 static HRESULT WINAPI HTMLDOMChildrenCollection_get_length(IHTMLDOMChildrenCollection *iface, LONG *p)
287 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
288 UINT32 length=0;
290 TRACE("(%p)->(%p)\n", This, p);
292 nsIDOMNodeList_GetLength(This->nslist, &length);
293 *p = length;
294 return S_OK;
297 static HRESULT WINAPI HTMLDOMChildrenCollection_get__newEnum(IHTMLDOMChildrenCollection *iface, IUnknown **p)
299 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
300 HTMLDOMChildrenCollectionEnum *ret;
302 TRACE("(%p)->(%p)\n", This, p);
304 ret = heap_alloc(sizeof(*ret));
305 if(!ret)
306 return E_OUTOFMEMORY;
308 ret->IEnumVARIANT_iface.lpVtbl = &HTMLDOMChildrenCollectionEnumVtbl;
309 ret->ref = 1;
310 ret->iter = 0;
312 IHTMLDOMChildrenCollection_AddRef(&This->IHTMLDOMChildrenCollection_iface);
313 ret->col = This;
315 *p = (IUnknown*)&ret->IEnumVARIANT_iface;
316 return S_OK;
319 static HRESULT WINAPI HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection *iface, LONG index, IDispatch **ppItem)
321 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
322 nsIDOMNode *nsnode = NULL;
323 HTMLDOMNode *node;
324 UINT32 length=0;
325 nsresult nsres;
326 HRESULT hres;
328 TRACE("(%p)->(%d %p)\n", This, index, ppItem);
330 if (ppItem)
331 *ppItem = NULL;
332 else
333 return E_POINTER;
335 nsIDOMNodeList_GetLength(This->nslist, &length);
336 if(index < 0 || index >= length)
337 return E_INVALIDARG;
339 nsres = nsIDOMNodeList_Item(This->nslist, index, &nsnode);
340 if(NS_FAILED(nsres) || !nsnode) {
341 ERR("Item failed: %08x\n", nsres);
342 return E_FAIL;
345 hres = get_node(This->doc, nsnode, TRUE, &node);
346 if(FAILED(hres))
347 return hres;
349 *ppItem = (IDispatch*)&node->IHTMLDOMNode_iface;
350 return S_OK;
353 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl = {
354 HTMLDOMChildrenCollection_QueryInterface,
355 HTMLDOMChildrenCollection_AddRef,
356 HTMLDOMChildrenCollection_Release,
357 HTMLDOMChildrenCollection_GetTypeInfoCount,
358 HTMLDOMChildrenCollection_GetTypeInfo,
359 HTMLDOMChildrenCollection_GetIDsOfNames,
360 HTMLDOMChildrenCollection_Invoke,
361 HTMLDOMChildrenCollection_get_length,
362 HTMLDOMChildrenCollection_get__newEnum,
363 HTMLDOMChildrenCollection_item
366 static inline HTMLDOMChildrenCollection *impl_from_DispatchEx(DispatchEx *iface)
368 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, dispex);
371 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
373 static HRESULT HTMLDOMChildrenCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
375 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
376 WCHAR *ptr;
377 DWORD idx=0;
378 UINT32 len = 0;
380 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
381 idx = idx*10 + (*ptr-'0');
382 if(*ptr)
383 return DISP_E_UNKNOWNNAME;
385 nsIDOMNodeList_GetLength(This->nslist, &len);
386 if(idx >= len)
387 return DISP_E_UNKNOWNNAME;
389 *dispid = DISPID_CHILDCOL_0 + idx;
390 TRACE("ret %x\n", *dispid);
391 return S_OK;
394 static HRESULT HTMLDOMChildrenCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
395 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
397 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
399 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
401 switch(flags) {
402 case DISPATCH_PROPERTYGET: {
403 IDispatch *disp = NULL;
404 HRESULT hres;
406 hres = IHTMLDOMChildrenCollection_item(&This->IHTMLDOMChildrenCollection_iface,
407 id - DISPID_CHILDCOL_0, &disp);
408 if(FAILED(hres))
409 return hres;
411 V_VT(res) = VT_DISPATCH;
412 V_DISPATCH(res) = disp;
413 break;
416 default:
417 FIXME("unimplemented flags %x\n", flags);
418 return E_NOTIMPL;
421 return S_OK;
424 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl = {
425 NULL,
426 HTMLDOMChildrenCollection_get_dispid,
427 HTMLDOMChildrenCollection_invoke,
428 NULL
431 static const tid_t HTMLDOMChildrenCollection_iface_tids[] = {
432 IHTMLDOMChildrenCollection_tid,
436 static dispex_static_data_t HTMLDOMChildrenCollection_dispex = {
437 &HTMLDOMChildrenCollection_dispex_vtbl,
438 DispDOMChildrenCollection_tid,
439 NULL,
440 HTMLDOMChildrenCollection_iface_tids
443 static IHTMLDOMChildrenCollection *create_child_collection(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
445 HTMLDOMChildrenCollection *ret;
447 ret = heap_alloc_zero(sizeof(*ret));
448 if(!ret)
449 return NULL;
451 ret->IHTMLDOMChildrenCollection_iface.lpVtbl = &HTMLDOMChildrenCollectionVtbl;
452 ret->ref = 1;
454 nsIDOMNodeList_AddRef(nslist);
455 ret->nslist = nslist;
457 htmldoc_addref(&doc->basedoc);
458 ret->doc = doc;
460 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLDOMChildrenCollection_iface,
461 &HTMLDOMChildrenCollection_dispex);
463 return &ret->IHTMLDOMChildrenCollection_iface;
466 static inline HTMLDOMNode *impl_from_IHTMLDOMNode(IHTMLDOMNode *iface)
468 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode_iface);
471 static HRESULT WINAPI HTMLDOMNode_QueryInterface(IHTMLDOMNode *iface,
472 REFIID riid, void **ppv)
474 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
476 return This->vtbl->qi(This, riid, ppv);
479 static ULONG WINAPI HTMLDOMNode_AddRef(IHTMLDOMNode *iface)
481 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
482 LONG ref;
484 ref = ccref_incr(&This->ccref, (nsISupports*)&This->IHTMLDOMNode_iface);
486 TRACE("(%p) ref=%d\n", This, ref);
488 return ref;
491 static ULONG WINAPI HTMLDOMNode_Release(IHTMLDOMNode *iface)
493 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
494 LONG ref = ccref_decr(&This->ccref, (nsISupports*)&This->IHTMLDOMNode_iface, /*&node_ccp*/ NULL);
496 TRACE("(%p) ref=%d\n", This, ref);
498 return ref;
501 static HRESULT WINAPI HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode *iface, UINT *pctinfo)
503 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
504 return IDispatchEx_GetTypeInfoCount(&This->event_target.dispex.IDispatchEx_iface, pctinfo);
507 static HRESULT WINAPI HTMLDOMNode_GetTypeInfo(IHTMLDOMNode *iface, UINT iTInfo,
508 LCID lcid, ITypeInfo **ppTInfo)
510 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
511 return IDispatchEx_GetTypeInfo(&This->event_target.dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
514 static HRESULT WINAPI HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode *iface, REFIID riid,
515 LPOLESTR *rgszNames, UINT cNames,
516 LCID lcid, DISPID *rgDispId)
518 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
519 return IDispatchEx_GetIDsOfNames(&This->event_target.dispex.IDispatchEx_iface, riid, rgszNames, cNames,
520 lcid, rgDispId);
523 static HRESULT WINAPI HTMLDOMNode_Invoke(IHTMLDOMNode *iface, DISPID dispIdMember,
524 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
525 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
527 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
528 return IDispatchEx_Invoke(&This->event_target.dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
529 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
532 static HRESULT WINAPI HTMLDOMNode_get_nodeType(IHTMLDOMNode *iface, LONG *p)
534 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
535 UINT16 type = -1;
537 TRACE("(%p)->(%p)\n", This, p);
539 nsIDOMNode_GetNodeType(This->nsnode, &type);
541 switch(type) {
542 case ELEMENT_NODE:
543 *p = 1;
544 break;
545 case TEXT_NODE:
546 *p = 3;
547 break;
548 case COMMENT_NODE:
549 case DOCUMENT_TYPE_NODE:
550 *p = 8;
551 break;
552 case DOCUMENT_NODE:
553 *p = 9;
554 break;
555 case DOCUMENT_FRAGMENT_NODE:
556 *p = 11;
557 break;
558 default:
560 * FIXME:
561 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
562 * It needs more tests.
564 FIXME("type %u\n", type);
565 *p = 0;
568 return S_OK;
571 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
573 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
574 HTMLDOMNode *node;
575 nsIDOMNode *nsnode;
576 nsresult nsres;
577 HRESULT hres;
579 TRACE("(%p)->(%p)\n", This, p);
581 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
582 if(NS_FAILED(nsres)) {
583 ERR("GetParentNode failed: %08x\n", nsres);
584 return E_FAIL;
587 if(!nsnode) {
588 *p = NULL;
589 return S_OK;
592 hres = get_node(This->doc, nsnode, TRUE, &node);
593 nsIDOMNode_Release(nsnode);
594 if(FAILED(hres))
595 return hres;
597 *p = &node->IHTMLDOMNode_iface;
598 return S_OK;
601 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
603 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
604 cpp_bool has_child = FALSE;
605 nsresult nsres;
607 TRACE("(%p)->(%p)\n", This, fChildren);
609 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
610 if(NS_FAILED(nsres))
611 ERR("HasChildNodes failed: %08x\n", nsres);
613 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
614 return S_OK;
617 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
619 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
620 nsIDOMNodeList *nslist;
621 nsresult nsres;
623 TRACE("(%p)->(%p)\n", This, p);
625 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
626 if(NS_FAILED(nsres)) {
627 ERR("GetChildNodes failed: %08x\n", nsres);
628 return E_FAIL;
631 *p = (IDispatch*)create_child_collection(This->doc, nslist);
632 nsIDOMNodeList_Release(nslist);
634 return *p ? S_OK : E_OUTOFMEMORY;
637 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
639 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
640 HTMLAttributeCollection *col;
641 HRESULT hres;
643 TRACE("(%p)->(%p)\n", This, p);
645 if(This->vtbl->get_attr_col) {
646 hres = This->vtbl->get_attr_col(This, &col);
647 if(FAILED(hres))
648 return hres;
650 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
651 return S_OK;
654 *p = NULL;
655 return S_OK;
658 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
659 VARIANT refChild, IHTMLDOMNode **node)
661 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
662 HTMLDOMNode *new_child, *node_obj, *ref_node = NULL;
663 nsIDOMNode *nsnode;
664 nsresult nsres;
665 HRESULT hres = S_OK;
667 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
669 new_child = get_node_obj(newChild);
670 if(!new_child) {
671 ERR("invalid newChild\n");
672 return E_INVALIDARG;
675 switch(V_VT(&refChild)) {
676 case VT_NULL:
677 break;
678 case VT_DISPATCH: {
679 IHTMLDOMNode *ref_iface;
681 if(!V_DISPATCH(&refChild))
682 break;
684 hres = IDispatch_QueryInterface(V_DISPATCH(&refChild), &IID_IHTMLDOMNode, (void**)&ref_iface);
685 if(FAILED(hres))
686 break;
688 ref_node = get_node_obj(ref_iface);
689 IHTMLDOMNode_Release(ref_iface);
690 if(!ref_node) {
691 ERR("unvalid node\n");
692 hres = E_FAIL;
693 break;
695 break;
697 default:
698 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
699 hres = E_NOTIMPL;
702 if(SUCCEEDED(hres)) {
703 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, ref_node ? ref_node->nsnode : NULL, &nsnode);
704 if(NS_FAILED(nsres)) {
705 ERR("InsertBefore failed: %08x\n", nsres);
706 hres = E_FAIL;
709 node_release(new_child);
710 if(ref_node)
711 node_release(ref_node);
712 if(FAILED(hres))
713 return hres;
715 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
716 nsIDOMNode_Release(nsnode);
717 if(FAILED(hres))
718 return hres;
720 *node = &node_obj->IHTMLDOMNode_iface;
721 return S_OK;
724 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
725 IHTMLDOMNode **node)
727 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
728 HTMLDOMNode *node_obj;
729 nsIDOMNode *nsnode;
730 nsresult nsres;
731 HRESULT hres;
733 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
735 node_obj = get_node_obj(oldChild);
736 if(!node_obj)
737 return E_FAIL;
739 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
740 node_release(node_obj);
741 if(NS_FAILED(nsres)) {
742 ERR("RemoveChild failed: %08x\n", nsres);
743 return E_FAIL;
746 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
747 nsIDOMNode_Release(nsnode);
748 if(FAILED(hres))
749 return hres;
751 /* FIXME: Make sure that node != newChild */
752 *node = &node_obj->IHTMLDOMNode_iface;
753 return S_OK;
756 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
757 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
759 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
760 HTMLDOMNode *node_new, *node_old, *ret_node;
761 nsIDOMNode *nsnode;
762 nsresult nsres;
763 HRESULT hres;
765 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
767 node_new = get_node_obj(newChild);
768 if(!node_new)
769 return E_FAIL;
771 node_old = get_node_obj(oldChild);
772 if(!node_old) {
773 node_release(node_new);
774 return E_FAIL;
777 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
778 node_release(node_new);
779 node_release(node_old);
780 if(NS_FAILED(nsres))
781 return E_FAIL;
783 hres = get_node(This->doc, nsnode, TRUE, &ret_node);
784 nsIDOMNode_Release(nsnode);
785 if(FAILED(hres))
786 return hres;
788 *node = &ret_node->IHTMLDOMNode_iface;
789 return S_OK;
792 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
793 IHTMLDOMNode **clonedNode)
795 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
796 HTMLDOMNode *new_node;
797 nsIDOMNode *nsnode;
798 nsresult nsres;
799 HRESULT hres;
801 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
803 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, 1, &nsnode);
804 if(NS_FAILED(nsres) || !nsnode) {
805 ERR("CloneNode failed: %08x\n", nsres);
806 return E_FAIL;
809 hres = This->vtbl->clone(This, nsnode, &new_node);
810 if(FAILED(hres))
811 return hres;
813 *clonedNode = &new_node->IHTMLDOMNode_iface;
814 return S_OK;
817 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
818 IHTMLDOMNode **removed)
820 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
821 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
822 return E_NOTIMPL;
825 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
826 IHTMLDOMNode **swappedNode)
828 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
829 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
830 return E_NOTIMPL;
833 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
834 IHTMLDOMNode **replaced)
836 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
837 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
838 return E_NOTIMPL;
841 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
842 IHTMLDOMNode **node)
844 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
845 HTMLDOMNode *node_obj;
846 nsIDOMNode *nsnode;
847 nsresult nsres;
848 HRESULT hres;
850 TRACE("(%p)->(%p %p)\n", This, newChild, node);
852 node_obj = get_node_obj(newChild);
853 if(!node_obj)
854 return E_FAIL;
856 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
857 node_release(node_obj);
858 if(NS_FAILED(nsres)) {
859 ERR("AppendChild failed: %08x\n", nsres);
860 return E_FAIL;
863 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
864 nsIDOMNode_Release(nsnode);
865 if(FAILED(hres))
866 return hres;
868 /* FIXME: Make sure that node != newChild */
869 *node = &node_obj->IHTMLDOMNode_iface;
870 return S_OK;
873 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
875 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
876 nsAString name;
877 nsresult nsres;
879 TRACE("(%p)->(%p)\n", This, p);
881 nsAString_Init(&name, NULL);
882 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
883 return return_nsstr(nsres, &name, p);
886 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
888 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
890 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
892 switch(V_VT(&v)) {
893 case VT_BSTR: {
894 nsAString val_str;
896 nsAString_InitDepend(&val_str, V_BSTR(&v));
897 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
898 nsAString_Finish(&val_str);
900 return S_OK;
903 default:
904 FIXME("unsupported value %s\n", debugstr_variant(&v));
907 return E_NOTIMPL;
910 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
912 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
913 const PRUnichar *val;
914 nsAString val_str;
916 TRACE("(%p)->(%p)\n", This, p);
918 nsAString_Init(&val_str, NULL);
919 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
920 nsAString_GetData(&val_str, &val);
922 if(*val) {
923 V_VT(p) = VT_BSTR;
924 V_BSTR(p) = SysAllocString(val);
925 }else {
926 V_VT(p) = VT_NULL;
929 nsAString_Finish(&val_str);
931 return S_OK;
934 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
936 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
937 nsIDOMNode *nschild = NULL;
938 HTMLDOMNode *node;
939 HRESULT hres;
941 TRACE("(%p)->(%p)\n", This, p);
943 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
944 if(!nschild) {
945 *p = NULL;
946 return S_OK;
949 hres = get_node(This->doc, nschild, TRUE, &node);
950 nsIDOMNode_Release(nschild);
951 if(FAILED(hres))
952 return hres;
954 *p = &node->IHTMLDOMNode_iface;
955 return S_OK;
958 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
960 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
961 nsIDOMNode *nschild = NULL;
962 HTMLDOMNode *node;
963 HRESULT hres;
965 TRACE("(%p)->(%p)\n", This, p);
967 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
968 if(!nschild) {
969 *p = NULL;
970 return S_OK;
973 hres = get_node(This->doc, nschild, TRUE, &node);
974 nsIDOMNode_Release(nschild);
975 if(FAILED(hres))
976 return hres;
978 *p = &node->IHTMLDOMNode_iface;
979 return S_OK;
982 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
984 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
985 nsIDOMNode *nschild = NULL;
986 HTMLDOMNode *node;
987 HRESULT hres;
989 TRACE("(%p)->(%p)\n", This, p);
991 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
992 if(!nschild) {
993 *p = NULL;
994 return S_OK;
997 hres = get_node(This->doc, nschild, TRUE, &node);
998 nsIDOMNode_Release(nschild);
999 if(FAILED(hres))
1000 return hres;
1002 *p = &node->IHTMLDOMNode_iface;
1003 return S_OK;
1006 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
1008 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
1009 nsIDOMNode *nssibling = NULL;
1010 HTMLDOMNode *node;
1011 HRESULT hres;
1013 TRACE("(%p)->(%p)\n", This, p);
1015 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
1016 if(!nssibling) {
1017 *p = NULL;
1018 return S_OK;
1021 hres = get_node(This->doc, nssibling, TRUE, &node);
1022 nsIDOMNode_Release(nssibling);
1023 if(FAILED(hres))
1024 return hres;
1026 *p = &node->IHTMLDOMNode_iface;
1027 return S_OK;
1030 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
1031 HTMLDOMNode_QueryInterface,
1032 HTMLDOMNode_AddRef,
1033 HTMLDOMNode_Release,
1034 HTMLDOMNode_GetTypeInfoCount,
1035 HTMLDOMNode_GetTypeInfo,
1036 HTMLDOMNode_GetIDsOfNames,
1037 HTMLDOMNode_Invoke,
1038 HTMLDOMNode_get_nodeType,
1039 HTMLDOMNode_get_parentNode,
1040 HTMLDOMNode_hasChildNodes,
1041 HTMLDOMNode_get_childNodes,
1042 HTMLDOMNode_get_attributes,
1043 HTMLDOMNode_insertBefore,
1044 HTMLDOMNode_removeChild,
1045 HTMLDOMNode_replaceChild,
1046 HTMLDOMNode_cloneNode,
1047 HTMLDOMNode_removeNode,
1048 HTMLDOMNode_swapNode,
1049 HTMLDOMNode_replaceNode,
1050 HTMLDOMNode_appendChild,
1051 HTMLDOMNode_get_nodeName,
1052 HTMLDOMNode_put_nodeValue,
1053 HTMLDOMNode_get_nodeValue,
1054 HTMLDOMNode_get_firstChild,
1055 HTMLDOMNode_get_lastChild,
1056 HTMLDOMNode_get_previousSibling,
1057 HTMLDOMNode_get_nextSibling
1060 static HTMLDOMNode *get_node_obj(IHTMLDOMNode *iface)
1062 HTMLDOMNode *ret;
1064 if(iface->lpVtbl != &HTMLDOMNodeVtbl)
1065 return NULL;
1067 ret = impl_from_IHTMLDOMNode(iface);
1068 node_addref(ret);
1069 return ret;
1072 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
1074 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
1077 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
1078 REFIID riid, void **ppv)
1080 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1082 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
1085 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
1087 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1089 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
1092 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
1094 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1096 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
1099 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
1101 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1102 return IDispatchEx_GetTypeInfoCount(&This->event_target.dispex.IDispatchEx_iface, pctinfo);
1105 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
1106 LCID lcid, ITypeInfo **ppTInfo)
1108 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1109 return IDispatchEx_GetTypeInfo(&This->event_target.dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1112 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
1113 LPOLESTR *rgszNames, UINT cNames,
1114 LCID lcid, DISPID *rgDispId)
1116 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1117 return IDispatchEx_GetIDsOfNames(&This->event_target.dispex.IDispatchEx_iface, riid, rgszNames, cNames,
1118 lcid, rgDispId);
1121 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
1122 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1123 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1125 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1126 return IDispatchEx_Invoke(&This->event_target.dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
1127 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1130 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
1132 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
1134 TRACE("(%p)->(%p)\n", This, p);
1136 /* FIXME: Better check for document node */
1137 if(This == &This->doc->node) {
1138 *p = NULL;
1139 }else {
1140 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
1141 IDispatch_AddRef(*p);
1143 return S_OK;
1146 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
1147 HTMLDOMNode2_QueryInterface,
1148 HTMLDOMNode2_AddRef,
1149 HTMLDOMNode2_Release,
1150 HTMLDOMNode2_GetTypeInfoCount,
1151 HTMLDOMNode2_GetTypeInfo,
1152 HTMLDOMNode2_GetIDsOfNames,
1153 HTMLDOMNode2_Invoke,
1154 HTMLDOMNode2_get_ownerDocument
1157 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
1159 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
1161 if(IsEqualGUID(&IID_IUnknown, riid)) {
1162 *ppv = &This->IHTMLDOMNode_iface;
1163 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1164 *ppv = &This->IHTMLDOMNode_iface;
1165 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
1166 *ppv = &This->IHTMLDOMNode_iface;
1167 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
1168 *ppv = &This->IHTMLDOMNode2_iface;
1169 }else if(IsEqualGUID(&IID_nsXPCOMCycleCollectionParticipant, riid)) {
1170 *ppv = &node_ccp;
1171 return NS_OK;
1172 }else if(IsEqualGUID(&IID_nsCycleCollectionISupports, riid)) {
1173 *ppv = &This->IHTMLDOMNode_iface;
1174 return NS_OK;
1175 }else if(dispex_query_interface(&This->event_target.dispex, riid, ppv)) {
1176 return *ppv ? S_OK : E_NOINTERFACE;
1177 }else {
1178 *ppv = NULL;
1179 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
1180 return E_NOINTERFACE;
1183 IUnknown_AddRef((IUnknown*)*ppv);
1184 return S_OK;
1187 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1189 if(This->nsnode)
1190 nsIDOMNode_Release(This->nsnode);
1191 if(This->doc && &This->doc->node != This)
1192 htmldoc_release(&This->doc->basedoc);
1193 if(This->event_target.ptr)
1194 release_event_target(This->event_target.ptr);
1197 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1199 return create_node(This->doc, nsnode, ret);
1202 static const cpc_entry_t HTMLDOMNode_cpc[] = {{NULL}};
1204 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1205 HTMLDOMNode_QI,
1206 HTMLDOMNode_destructor,
1207 HTMLDOMNode_cpc,
1208 HTMLDOMNode_clone
1211 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1213 nsresult nsres;
1215 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1216 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1218 ccref_init(&node->ccref, 1);
1220 if(&doc->node != node)
1221 htmldoc_addref(&doc->basedoc);
1222 node->doc = doc;
1224 nsIDOMNode_AddRef(nsnode);
1225 node->nsnode = nsnode;
1227 nsres = nsIDOMNode_SetMshtmlNode(nsnode, (nsISupports*)&node->IHTMLDOMNode_iface);
1228 assert(nsres == NS_OK);
1231 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1233 UINT16 node_type;
1234 HRESULT hres;
1236 nsIDOMNode_GetNodeType(nsnode, &node_type);
1238 switch(node_type) {
1239 case ELEMENT_NODE: {
1240 HTMLElement *elem;
1241 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1242 if(FAILED(hres))
1243 return hres;
1244 *ret = &elem->node;
1245 break;
1247 case TEXT_NODE:
1248 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1249 if(FAILED(hres))
1250 return hres;
1251 break;
1252 /* doctype nodes are represented as comment nodes (at least in quirks mode) */
1253 case DOCUMENT_TYPE_NODE:
1254 case COMMENT_NODE: {
1255 HTMLElement *comment;
1256 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1257 if(FAILED(hres))
1258 return hres;
1259 *ret = &comment->node;
1260 break;
1262 case ATTRIBUTE_NODE:
1263 ERR("Called on attribute node\n");
1264 return E_UNEXPECTED;
1265 default: {
1266 HTMLDOMNode *node;
1268 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1269 if(!node)
1270 return E_OUTOFMEMORY;
1272 node->vtbl = &HTMLDOMNodeImplVtbl;
1273 HTMLDOMNode_Init(doc, node, nsnode);
1274 *ret = node;
1278 TRACE("type %d ret %p\n", node_type, *ret);
1279 return S_OK;
1282 static nsresult NSAPI HTMLDOMNode_traverse(void *ccp, void *p, nsCycleCollectionTraversalCallback *cb)
1284 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1286 TRACE("%p\n", This);
1288 describe_cc_node(&This->ccref, "HTMLDOMNode", cb);
1290 if(This->nsnode)
1291 note_cc_edge((nsISupports*)This->nsnode, "This->nsnode", cb);
1292 if(This->doc && &This->doc->node != This)
1293 note_cc_edge((nsISupports*)&This->doc->node.IHTMLDOMNode_iface, "This->doc", cb);
1294 dispex_traverse(&This->event_target.dispex, cb);
1296 if(This->vtbl->traverse)
1297 This->vtbl->traverse(This, cb);
1299 return NS_OK;
1302 static nsresult NSAPI HTMLDOMNode_unlink(void *p)
1304 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1306 TRACE("%p\n", This);
1308 if(This->vtbl->unlink)
1309 This->vtbl->unlink(This);
1311 dispex_unlink(&This->event_target.dispex);
1313 if(This->nsnode) {
1314 nsIDOMNode *nsnode = This->nsnode;
1315 This->nsnode = NULL;
1316 nsIDOMNode_Release(nsnode);
1319 if(This->doc && &This->doc->node != This) {
1320 HTMLDocument *doc = &This->doc->basedoc;
1321 This->doc = NULL;
1322 htmldoc_release(doc);
1323 }else {
1324 This->doc = NULL;
1327 return NS_OK;
1330 static void NSAPI HTMLDOMNode_delete_cycle_collectable(void *p)
1332 HTMLDOMNode *This = impl_from_IHTMLDOMNode(p);
1334 TRACE("(%p)\n", This);
1336 if(This->vtbl->unlink)
1337 This->vtbl->unlink(This);
1338 This->vtbl->destructor(This);
1339 release_dispex(&This->event_target.dispex);
1340 heap_free(This);
1343 void init_node_cc(void)
1345 static const CCObjCallback node_ccp_callback = {
1346 HTMLDOMNode_traverse,
1347 HTMLDOMNode_unlink,
1348 HTMLDOMNode_delete_cycle_collectable
1351 ccp_init(&node_ccp, &node_ccp_callback);
1354 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1356 nsISupports *unk = NULL;
1357 nsresult nsres;
1359 nsres = nsIDOMNode_GetMshtmlNode(nsnode, &unk);
1360 assert(nsres == NS_OK);
1362 if(unk) {
1363 *ret = get_node_obj((IHTMLDOMNode*)unk);
1364 nsISupports_Release(unk);
1365 return NS_OK;
1368 if(!create) {
1369 *ret = NULL;
1370 return S_OK;
1373 return create_node(This, nsnode, ret);