d3drm: Implement IDirect3DRMFrameX_AddTransform.
[wine/multimedia.git] / dlls / mshtml / htmlnode.c
blob47df09d1bc4b2886b889050a7f67859a90885cdc
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>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
31 #include "htmlevent.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 static HTMLDOMNode *get_node_obj(HTMLDocumentNode*,IUnknown*);
36 static HRESULT create_node(HTMLDocumentNode*,nsIDOMNode*,HTMLDOMNode**);
38 typedef struct {
39 DispatchEx dispex;
40 IHTMLDOMChildrenCollection IHTMLDOMChildrenCollection_iface;
42 LONG ref;
44 /* FIXME: implement weak reference */
45 HTMLDocumentNode *doc;
47 nsIDOMNodeList *nslist;
48 } HTMLDOMChildrenCollection;
50 static inline HTMLDOMChildrenCollection *impl_from_IHTMLDOMChildrenCollection(IHTMLDOMChildrenCollection *iface)
52 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, IHTMLDOMChildrenCollection_iface);
55 static HRESULT WINAPI HTMLDOMChildrenCollection_QueryInterface(IHTMLDOMChildrenCollection *iface, REFIID riid, void **ppv)
57 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
59 *ppv = NULL;
61 if(IsEqualGUID(&IID_IUnknown, riid)) {
62 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
63 *ppv = &This->IHTMLDOMChildrenCollection_iface;
64 }else if(IsEqualGUID(&IID_IHTMLDOMChildrenCollection, riid)) {
65 TRACE("(%p)->(IID_IHTMLDOMChildrenCollection %p)\n", This, ppv);
66 *ppv = &This->IHTMLDOMChildrenCollection_iface;
67 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
68 return *ppv ? S_OK : E_NOINTERFACE;
71 if(*ppv) {
72 IUnknown_AddRef((IUnknown*)*ppv);
73 return S_OK;
76 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
77 return E_NOINTERFACE;
80 static ULONG WINAPI HTMLDOMChildrenCollection_AddRef(IHTMLDOMChildrenCollection *iface)
82 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
83 LONG ref = InterlockedIncrement(&This->ref);
85 TRACE("(%p) ref=%d\n", This, ref);
87 return ref;
90 static ULONG WINAPI HTMLDOMChildrenCollection_Release(IHTMLDOMChildrenCollection *iface)
92 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
93 LONG ref = InterlockedDecrement(&This->ref);
95 TRACE("(%p) ref=%d\n", This, ref);
97 if(!ref) {
98 nsIDOMNodeList_Release(This->nslist);
99 heap_free(This);
102 return ref;
105 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfoCount(IHTMLDOMChildrenCollection *iface, UINT *pctinfo)
107 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
108 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
111 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfo(IHTMLDOMChildrenCollection *iface, UINT iTInfo,
112 LCID lcid, ITypeInfo **ppTInfo)
114 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
115 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
118 static HRESULT WINAPI HTMLDOMChildrenCollection_GetIDsOfNames(IHTMLDOMChildrenCollection *iface, REFIID riid,
119 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
121 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
122 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
123 lcid, rgDispId);
126 static HRESULT WINAPI HTMLDOMChildrenCollection_Invoke(IHTMLDOMChildrenCollection *iface, DISPID dispIdMember,
127 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
128 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
130 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
131 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
132 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
135 static HRESULT WINAPI HTMLDOMChildrenCollection_get_length(IHTMLDOMChildrenCollection *iface, LONG *p)
137 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
138 PRUint32 length=0;
140 TRACE("(%p)->(%p)\n", This, p);
142 nsIDOMNodeList_GetLength(This->nslist, &length);
143 *p = length;
144 return S_OK;
147 static HRESULT WINAPI HTMLDOMChildrenCollection__newEnum(IHTMLDOMChildrenCollection *iface, IUnknown **p)
149 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
150 FIXME("(%p)->(%p)\n", This, p);
151 return E_NOTIMPL;
154 static HRESULT WINAPI HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection *iface, LONG index, IDispatch **ppItem)
156 HTMLDOMChildrenCollection *This = impl_from_IHTMLDOMChildrenCollection(iface);
157 nsIDOMNode *nsnode = NULL;
158 HTMLDOMNode *node;
159 PRUint32 length=0;
160 nsresult nsres;
161 HRESULT hres;
163 TRACE("(%p)->(%d %p)\n", This, index, ppItem);
165 if (ppItem)
166 *ppItem = NULL;
167 else
168 return E_POINTER;
170 nsIDOMNodeList_GetLength(This->nslist, &length);
171 if(index < 0 || index >= length)
172 return E_INVALIDARG;
174 nsres = nsIDOMNodeList_Item(This->nslist, index, &nsnode);
175 if(NS_FAILED(nsres) || !nsnode) {
176 ERR("Item failed: %08x\n", nsres);
177 return E_FAIL;
180 hres = get_node(This->doc, nsnode, TRUE, &node);
181 if(FAILED(hres))
182 return hres;
184 *ppItem = (IDispatch*)&node->IHTMLDOMNode_iface;
185 IDispatch_AddRef(*ppItem);
186 return S_OK;
189 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl = {
190 HTMLDOMChildrenCollection_QueryInterface,
191 HTMLDOMChildrenCollection_AddRef,
192 HTMLDOMChildrenCollection_Release,
193 HTMLDOMChildrenCollection_GetTypeInfoCount,
194 HTMLDOMChildrenCollection_GetTypeInfo,
195 HTMLDOMChildrenCollection_GetIDsOfNames,
196 HTMLDOMChildrenCollection_Invoke,
197 HTMLDOMChildrenCollection_get_length,
198 HTMLDOMChildrenCollection__newEnum,
199 HTMLDOMChildrenCollection_item
202 static inline HTMLDOMChildrenCollection *impl_from_DispatchEx(DispatchEx *iface)
204 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, dispex);
207 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
209 static HRESULT HTMLDOMChildrenCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
211 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
212 WCHAR *ptr;
213 DWORD idx=0;
214 PRUint32 len = 0;
216 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
217 idx = idx*10 + (*ptr-'0');
218 if(*ptr)
219 return DISP_E_UNKNOWNNAME;
221 nsIDOMNodeList_GetLength(This->nslist, &len);
222 if(idx >= len)
223 return DISP_E_UNKNOWNNAME;
225 *dispid = DISPID_CHILDCOL_0 + idx;
226 TRACE("ret %x\n", *dispid);
227 return S_OK;
230 static HRESULT HTMLDOMChildrenCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
231 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
233 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
235 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
237 switch(flags) {
238 case DISPATCH_PROPERTYGET: {
239 IDispatch *disp = NULL;
240 HRESULT hres;
242 hres = IHTMLDOMChildrenCollection_item(&This->IHTMLDOMChildrenCollection_iface,
243 id - DISPID_CHILDCOL_0, &disp);
244 if(FAILED(hres))
245 return hres;
247 V_VT(res) = VT_DISPATCH;
248 V_DISPATCH(res) = disp;
249 break;
252 default:
253 FIXME("unimplemented flags %x\n", flags);
254 return E_NOTIMPL;
257 return S_OK;
260 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl = {
261 NULL,
262 HTMLDOMChildrenCollection_get_dispid,
263 HTMLDOMChildrenCollection_invoke,
264 NULL
267 static const tid_t HTMLDOMChildrenCollection_iface_tids[] = {
268 IHTMLDOMChildrenCollection_tid,
272 static dispex_static_data_t HTMLDOMChildrenCollection_dispex = {
273 &HTMLDOMChildrenCollection_dispex_vtbl,
274 DispDOMChildrenCollection_tid,
275 NULL,
276 HTMLDOMChildrenCollection_iface_tids
279 static IHTMLDOMChildrenCollection *create_child_collection(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
281 HTMLDOMChildrenCollection *ret;
283 ret = heap_alloc_zero(sizeof(*ret));
284 ret->IHTMLDOMChildrenCollection_iface.lpVtbl = &HTMLDOMChildrenCollectionVtbl;
285 ret->ref = 1;
287 nsIDOMNodeList_AddRef(nslist);
288 ret->nslist = nslist;
289 ret->doc = doc;
291 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLDOMChildrenCollection_iface,
292 &HTMLDOMChildrenCollection_dispex);
294 return &ret->IHTMLDOMChildrenCollection_iface;
297 static inline HTMLDOMNode *impl_from_IHTMLDOMNode(IHTMLDOMNode *iface)
299 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode_iface);
302 static HRESULT WINAPI HTMLDOMNode_QueryInterface(IHTMLDOMNode *iface,
303 REFIID riid, void **ppv)
305 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
307 return This->vtbl->qi(This, riid, ppv);
310 static ULONG WINAPI HTMLDOMNode_AddRef(IHTMLDOMNode *iface)
312 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
313 LONG ref = InterlockedIncrement(&This->ref);
315 TRACE("(%p) ref=%d\n", This, ref);
317 return ref;
320 static ULONG WINAPI HTMLDOMNode_Release(IHTMLDOMNode *iface)
322 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
323 LONG ref = InterlockedDecrement(&This->ref);
325 TRACE("(%p) ref=%d\n", This, ref);
327 if(!ref) {
328 This->vtbl->destructor(This);
329 release_dispex(&This->dispex);
330 heap_free(This);
333 return ref;
336 static HRESULT WINAPI HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode *iface, UINT *pctinfo)
338 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
339 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
342 static HRESULT WINAPI HTMLDOMNode_GetTypeInfo(IHTMLDOMNode *iface, UINT iTInfo,
343 LCID lcid, ITypeInfo **ppTInfo)
345 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
346 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
349 static HRESULT WINAPI HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode *iface, REFIID riid,
350 LPOLESTR *rgszNames, UINT cNames,
351 LCID lcid, DISPID *rgDispId)
353 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
354 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
355 lcid, rgDispId);
358 static HRESULT WINAPI HTMLDOMNode_Invoke(IHTMLDOMNode *iface, DISPID dispIdMember,
359 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
360 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
362 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
363 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
364 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
367 static HRESULT WINAPI HTMLDOMNode_get_nodeType(IHTMLDOMNode *iface, LONG *p)
369 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
370 PRUint16 type = -1;
372 TRACE("(%p)->(%p)\n", This, p);
374 nsIDOMNode_GetNodeType(This->nsnode, &type);
376 switch(type) {
377 case ELEMENT_NODE:
378 *p = 1;
379 break;
380 case TEXT_NODE:
381 *p = 3;
382 break;
383 case COMMENT_NODE:
384 *p = 8;
385 break;
386 case DOCUMENT_NODE:
387 *p = 9;
388 break;
389 case DOCUMENT_FRAGMENT_NODE:
390 *p = 11;
391 break;
392 default:
394 * FIXME:
395 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
396 * It needs more tests.
398 FIXME("type %u\n", type);
399 *p = 0;
402 return S_OK;
405 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
407 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
408 HTMLDOMNode *node;
409 nsIDOMNode *nsnode;
410 nsresult nsres;
411 HRESULT hres;
413 TRACE("(%p)->(%p)\n", This, p);
415 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
416 if(NS_FAILED(nsres)) {
417 ERR("GetParentNode failed: %08x\n", nsres);
418 return E_FAIL;
421 if(!nsnode) {
422 *p = NULL;
423 return S_OK;
426 hres = get_node(This->doc, nsnode, TRUE, &node);
427 nsIDOMNode_Release(nsnode);
428 if(FAILED(hres))
429 return hres;
431 *p = &node->IHTMLDOMNode_iface;
432 IHTMLDOMNode_AddRef(*p);
433 return S_OK;
436 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
438 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
439 cpp_bool has_child = FALSE;
440 nsresult nsres;
442 TRACE("(%p)->(%p)\n", This, fChildren);
444 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
445 if(NS_FAILED(nsres))
446 ERR("HasChildNodes failed: %08x\n", nsres);
448 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
449 return S_OK;
452 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
454 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
455 nsIDOMNodeList *nslist;
456 nsresult nsres;
458 TRACE("(%p)->(%p)\n", This, p);
460 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
461 if(NS_FAILED(nsres)) {
462 ERR("GetChildNodes failed: %08x\n", nsres);
463 return E_FAIL;
466 *p = (IDispatch*)create_child_collection(This->doc, nslist);
467 nsIDOMNodeList_Release(nslist);
469 return S_OK;
472 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
474 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
475 HTMLAttributeCollection *col;
476 HRESULT hres;
478 TRACE("(%p)->(%p)\n", This, p);
480 if(This->vtbl->get_attr_col) {
481 hres = This->vtbl->get_attr_col(This, &col);
482 if(FAILED(hres))
483 return hres;
485 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
486 return S_OK;
489 *p = NULL;
490 return S_OK;
493 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
494 VARIANT refChild, IHTMLDOMNode **node)
496 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
497 nsIDOMNode *nsnode, *nsref = NULL;
498 HTMLDOMNode *new_child;
499 HTMLDOMNode *node_obj;
500 nsresult nsres;
501 HRESULT hres;
503 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
505 new_child = get_node_obj(This->doc, (IUnknown*)newChild);
506 if(!new_child) {
507 ERR("invalid newChild\n");
508 return E_INVALIDARG;
511 switch(V_VT(&refChild)) {
512 case VT_NULL:
513 break;
514 case VT_DISPATCH: {
515 HTMLDOMNode *ref_node;
517 ref_node = get_node_obj(This->doc, (IUnknown*)V_DISPATCH(&refChild));
518 if(!ref_node) {
519 ERR("unvalid node\n");
520 return E_FAIL;
523 nsref = ref_node->nsnode;
524 break;
526 default:
527 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
528 return E_NOTIMPL;
531 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, nsref, &nsnode);
532 if(NS_FAILED(nsres)) {
533 ERR("InsertBefore failed: %08x\n", nsres);
534 return E_FAIL;
537 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
538 nsIDOMNode_Release(nsnode);
539 if(FAILED(hres))
540 return hres;
542 *node = &node_obj->IHTMLDOMNode_iface;
543 IHTMLDOMNode_AddRef(*node);
544 return S_OK;
547 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
548 IHTMLDOMNode **node)
550 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
551 HTMLDOMNode *node_obj;
552 nsIDOMNode *nsnode;
553 nsresult nsres;
554 HRESULT hres;
556 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
558 node_obj = get_node_obj(This->doc, (IUnknown*)oldChild);
559 if(!node_obj)
560 return E_FAIL;
562 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
563 if(NS_FAILED(nsres)) {
564 ERR("RemoveChild failed: %08x\n", nsres);
565 return E_FAIL;
568 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
569 nsIDOMNode_Release(nsnode);
570 if(FAILED(hres))
571 return hres;
573 /* FIXME: Make sure that node != newChild */
574 *node = &node_obj->IHTMLDOMNode_iface;
575 IHTMLDOMNode_AddRef(*node);
576 return S_OK;
579 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
580 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
582 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
583 HTMLDOMNode *node_new;
584 HTMLDOMNode *node_old;
585 nsIDOMNode *nsnode;
586 nsresult nsres;
587 HRESULT hres;
589 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
591 node_new = get_node_obj(This->doc, (IUnknown*)newChild);
592 if(!node_new)
593 return E_FAIL;
595 node_old = get_node_obj(This->doc, (IUnknown*)oldChild);
596 if(!node_old)
597 return E_FAIL;
599 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
600 if(NS_FAILED(nsres)) {
601 return E_FAIL;
604 nsnode = node_new->nsnode;
606 hres = get_node(This->doc, nsnode, TRUE, &node_new);
607 nsIDOMNode_Release(nsnode);
608 if(FAILED(hres))
609 return hres;
611 *node = &node_new->IHTMLDOMNode_iface;
612 IHTMLDOMNode_AddRef(*node);
613 return S_OK;
616 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
617 IHTMLDOMNode **clonedNode)
619 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
620 HTMLDOMNode *new_node;
621 nsIDOMNode *nsnode;
622 nsresult nsres;
623 HRESULT hres;
625 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
627 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, &nsnode);
628 if(NS_FAILED(nsres) || !nsnode) {
629 ERR("CloneNode failed: %08x\n", nsres);
630 return E_FAIL;
633 hres = This->vtbl->clone(This, nsnode, &new_node);
634 if(FAILED(hres))
635 return hres;
637 *clonedNode = &new_node->IHTMLDOMNode_iface;
638 return S_OK;
641 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
642 IHTMLDOMNode **removed)
644 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
645 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
646 return E_NOTIMPL;
649 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
650 IHTMLDOMNode **swappedNode)
652 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
653 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
654 return E_NOTIMPL;
657 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
658 IHTMLDOMNode **replaced)
660 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
661 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
662 return E_NOTIMPL;
665 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
666 IHTMLDOMNode **node)
668 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
669 HTMLDOMNode *node_obj;
670 nsIDOMNode *nsnode;
671 nsresult nsres;
672 HRESULT hres;
674 TRACE("(%p)->(%p %p)\n", This, newChild, node);
676 node_obj = get_node_obj(This->doc, (IUnknown*)newChild);
677 if(!node_obj)
678 return E_FAIL;
680 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
681 if(NS_FAILED(nsres)) {
682 WARN("AppendChild failed: %08x\n", nsres);
683 nsnode = node_obj->nsnode;
686 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
687 nsIDOMNode_Release(nsnode);
688 if(FAILED(hres))
689 return hres;
691 /* FIXME: Make sure that node != newChild */
692 *node = &node_obj->IHTMLDOMNode_iface;
693 IHTMLDOMNode_AddRef(*node);
694 return S_OK;
697 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
699 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
700 nsAString name;
701 nsresult nsres;
703 TRACE("(%p)->(%p)\n", This, p);
705 nsAString_Init(&name, NULL);
706 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
707 return return_nsstr(nsres, &name, p);
710 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
712 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
714 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
716 switch(V_VT(&v)) {
717 case VT_BSTR: {
718 nsAString val_str;
720 nsAString_InitDepend(&val_str, V_BSTR(&v));
721 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
722 nsAString_Finish(&val_str);
724 return S_OK;
727 default:
728 FIXME("unsupported value %s\n", debugstr_variant(&v));
731 return E_NOTIMPL;
734 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
736 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
737 const PRUnichar *val;
738 nsAString val_str;
740 TRACE("(%p)->(%p)\n", This, p);
742 nsAString_Init(&val_str, NULL);
743 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
744 nsAString_GetData(&val_str, &val);
746 if(*val) {
747 V_VT(p) = VT_BSTR;
748 V_BSTR(p) = SysAllocString(val);
749 }else {
750 V_VT(p) = VT_NULL;
753 nsAString_Finish(&val_str);
755 return S_OK;
758 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
760 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
761 nsIDOMNode *nschild = NULL;
762 HTMLDOMNode *node;
763 HRESULT hres;
765 TRACE("(%p)->(%p)\n", This, p);
767 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
768 if(!nschild) {
769 *p = NULL;
770 return S_OK;
773 hres = get_node(This->doc, nschild, TRUE, &node);
774 nsIDOMNode_Release(nschild);
775 if(FAILED(hres))
776 return hres;
778 *p = &node->IHTMLDOMNode_iface;
779 IHTMLDOMNode_AddRef(*p);
780 return S_OK;
783 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
785 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
786 nsIDOMNode *nschild = NULL;
787 HTMLDOMNode *node;
788 HRESULT hres;
790 TRACE("(%p)->(%p)\n", This, p);
792 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
793 if(!nschild) {
794 *p = NULL;
795 return S_OK;
798 hres = get_node(This->doc, nschild, TRUE, &node);
799 nsIDOMNode_Release(nschild);
800 if(FAILED(hres))
801 return hres;
803 *p = &node->IHTMLDOMNode_iface;
804 IHTMLDOMNode_AddRef(*p);
805 return S_OK;
808 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
810 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
811 nsIDOMNode *nschild = NULL;
812 HTMLDOMNode *node;
813 HRESULT hres;
815 TRACE("(%p)->(%p)\n", This, p);
817 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
818 if(!nschild) {
819 *p = NULL;
820 return S_OK;
823 hres = get_node(This->doc, nschild, TRUE, &node);
824 nsIDOMNode_Release(nschild);
825 if(FAILED(hres))
826 return hres;
828 *p = &node->IHTMLDOMNode_iface;
829 IHTMLDOMNode_AddRef(*p);
830 return S_OK;
833 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
835 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
836 nsIDOMNode *nssibling = NULL;
837 HTMLDOMNode *node;
838 HRESULT hres;
840 TRACE("(%p)->(%p)\n", This, p);
842 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
843 if(!nssibling) {
844 *p = NULL;
845 return S_OK;
848 hres = get_node(This->doc, nssibling, TRUE, &node);
849 nsIDOMNode_Release(nssibling);
850 if(FAILED(hres))
851 return hres;
853 *p = &node->IHTMLDOMNode_iface;
854 IHTMLDOMNode_AddRef(*p);
855 return S_OK;
858 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
859 HTMLDOMNode_QueryInterface,
860 HTMLDOMNode_AddRef,
861 HTMLDOMNode_Release,
862 HTMLDOMNode_GetTypeInfoCount,
863 HTMLDOMNode_GetTypeInfo,
864 HTMLDOMNode_GetIDsOfNames,
865 HTMLDOMNode_Invoke,
866 HTMLDOMNode_get_nodeType,
867 HTMLDOMNode_get_parentNode,
868 HTMLDOMNode_hasChildNodes,
869 HTMLDOMNode_get_childNodes,
870 HTMLDOMNode_get_attributes,
871 HTMLDOMNode_insertBefore,
872 HTMLDOMNode_removeChild,
873 HTMLDOMNode_replaceChild,
874 HTMLDOMNode_cloneNode,
875 HTMLDOMNode_removeNode,
876 HTMLDOMNode_swapNode,
877 HTMLDOMNode_replaceNode,
878 HTMLDOMNode_appendChild,
879 HTMLDOMNode_get_nodeName,
880 HTMLDOMNode_put_nodeValue,
881 HTMLDOMNode_get_nodeValue,
882 HTMLDOMNode_get_firstChild,
883 HTMLDOMNode_get_lastChild,
884 HTMLDOMNode_get_previousSibling,
885 HTMLDOMNode_get_nextSibling
888 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
890 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
893 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
894 REFIID riid, void **ppv)
896 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
898 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
901 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
903 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
905 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
908 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
910 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
912 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
915 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
917 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
918 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
921 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
922 LCID lcid, ITypeInfo **ppTInfo)
924 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
925 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
928 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
929 LPOLESTR *rgszNames, UINT cNames,
930 LCID lcid, DISPID *rgDispId)
932 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
933 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
934 lcid, rgDispId);
937 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
938 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
939 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
941 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
942 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
943 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
946 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
948 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
950 TRACE("(%p)->(%p)\n", This, p);
952 /* FIXME: Better check for document node */
953 if(This == &This->doc->node) {
954 *p = NULL;
955 }else {
956 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
957 IDispatch_AddRef(*p);
959 return S_OK;
962 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
963 HTMLDOMNode2_QueryInterface,
964 HTMLDOMNode2_AddRef,
965 HTMLDOMNode2_Release,
966 HTMLDOMNode2_GetTypeInfoCount,
967 HTMLDOMNode2_GetTypeInfo,
968 HTMLDOMNode2_GetIDsOfNames,
969 HTMLDOMNode2_Invoke,
970 HTMLDOMNode2_get_ownerDocument
973 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
975 *ppv = NULL;
977 if(IsEqualGUID(&IID_IUnknown, riid)) {
978 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
979 *ppv = &This->IHTMLDOMNode_iface;
980 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
981 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
982 *ppv = &This->IHTMLDOMNode_iface;
983 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
984 if(This->dispex.data) {
985 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
986 *ppv = &This->dispex.IDispatchEx_iface;
987 }else {
988 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
989 return E_NOINTERFACE;
991 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
992 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
993 *ppv = &This->IHTMLDOMNode_iface;
994 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
995 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
996 *ppv = &This->IHTMLDOMNode2_iface;
997 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
998 return *ppv ? S_OK : E_NOINTERFACE;
1001 if(*ppv) {
1002 IUnknown_AddRef((IUnknown*)*ppv);
1003 return S_OK;
1006 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1007 return E_NOINTERFACE;
1010 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1012 if(This->nsnode)
1013 nsIDOMNode_Release(This->nsnode);
1014 if(This->event_target)
1015 release_event_target(This->event_target);
1018 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1020 HRESULT hres;
1022 hres = create_node(This->doc, nsnode, ret);
1023 if(FAILED(hres))
1024 return hres;
1026 IHTMLDOMNode_AddRef(&(*ret)->IHTMLDOMNode_iface);
1027 return S_OK;
1030 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1031 HTMLDOMNode_QI,
1032 HTMLDOMNode_destructor,
1033 HTMLDOMNode_clone
1036 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1038 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1039 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1040 node->ref = 1;
1041 node->doc = doc;
1043 if(nsnode)
1044 nsIDOMNode_AddRef(nsnode);
1045 node->nsnode = nsnode;
1047 node->next = doc->nodes;
1048 doc->nodes = node;
1051 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1053 PRUint16 node_type;
1054 HRESULT hres;
1056 nsIDOMNode_GetNodeType(nsnode, &node_type);
1058 switch(node_type) {
1059 case ELEMENT_NODE: {
1060 HTMLElement *elem;
1061 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1062 if(FAILED(hres))
1063 return hres;
1064 *ret = &elem->node;
1065 break;
1067 case TEXT_NODE:
1068 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1069 if(FAILED(hres))
1070 return hres;
1071 break;
1072 case COMMENT_NODE: {
1073 HTMLElement *comment;
1074 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1075 if(FAILED(hres))
1076 return hres;
1077 *ret = &comment->node;
1078 break;
1080 case ATTRIBUTE_NODE:
1081 ERR("Called on attribute node\n");
1082 return E_UNEXPECTED;
1083 default: {
1084 HTMLDOMNode *node;
1086 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1087 if(!node)
1088 return E_OUTOFMEMORY;
1090 node->vtbl = &HTMLDOMNodeImplVtbl;
1091 HTMLDOMNode_Init(doc, node, nsnode);
1092 *ret = node;
1096 TRACE("type %d ret %p\n", node_type, *ret);
1097 return S_OK;
1101 * FIXME
1102 * List looks really ugly here. We should use a better data structure or
1103 * (better) find a way to store HTMLDOMelement pointer in nsIDOMNode.
1106 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1108 HTMLDOMNode *iter = This->nodes;
1110 while(iter) {
1111 if(iter->nsnode == nsnode)
1112 break;
1113 iter = iter->next;
1116 if(iter || !create) {
1117 *ret = iter;
1118 return S_OK;
1121 return create_node(This, nsnode, ret);
1125 * FIXME
1126 * We should use better way for getting node object (like private interface)
1127 * or avoid it at all.
1129 static HTMLDOMNode *get_node_obj(HTMLDocumentNode *This, IUnknown *iface)
1131 HTMLDOMNode *iter = This->nodes;
1132 IHTMLDOMNode *node;
1134 IUnknown_QueryInterface(iface, &IID_IHTMLDOMNode, (void**)&node);
1135 IHTMLDOMNode_Release(node);
1137 while(iter) {
1138 if(&iter->IHTMLDOMNode_iface == node)
1139 return iter;
1140 iter = iter->next;
1143 FIXME("Not found %p\n", iface);
1144 return NULL;
1147 void release_nodes(HTMLDocumentNode *This)
1149 HTMLDOMNode *iter, *next;
1151 if(!This->nodes)
1152 return;
1154 for(iter = This->nodes; iter; iter = next) {
1155 next = iter->next;
1156 iter->doc = NULL;
1157 if(&This->node != iter)
1158 IHTMLDOMNode_Release(&iter->IHTMLDOMNode_iface);