mshtml: Share nsscript reference with nsnode.
[wine/multimedia.git] / dlls / mshtml / htmlnode.c
blob5124c1c382092934ce17e360faa6ecfb3276fbc0
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 return S_OK;
188 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl = {
189 HTMLDOMChildrenCollection_QueryInterface,
190 HTMLDOMChildrenCollection_AddRef,
191 HTMLDOMChildrenCollection_Release,
192 HTMLDOMChildrenCollection_GetTypeInfoCount,
193 HTMLDOMChildrenCollection_GetTypeInfo,
194 HTMLDOMChildrenCollection_GetIDsOfNames,
195 HTMLDOMChildrenCollection_Invoke,
196 HTMLDOMChildrenCollection_get_length,
197 HTMLDOMChildrenCollection__newEnum,
198 HTMLDOMChildrenCollection_item
201 static inline HTMLDOMChildrenCollection *impl_from_DispatchEx(DispatchEx *iface)
203 return CONTAINING_RECORD(iface, HTMLDOMChildrenCollection, dispex);
206 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
208 static HRESULT HTMLDOMChildrenCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
210 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
211 WCHAR *ptr;
212 DWORD idx=0;
213 PRUint32 len = 0;
215 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
216 idx = idx*10 + (*ptr-'0');
217 if(*ptr)
218 return DISP_E_UNKNOWNNAME;
220 nsIDOMNodeList_GetLength(This->nslist, &len);
221 if(idx >= len)
222 return DISP_E_UNKNOWNNAME;
224 *dispid = DISPID_CHILDCOL_0 + idx;
225 TRACE("ret %x\n", *dispid);
226 return S_OK;
229 static HRESULT HTMLDOMChildrenCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
230 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
232 HTMLDOMChildrenCollection *This = impl_from_DispatchEx(dispex);
234 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
236 switch(flags) {
237 case DISPATCH_PROPERTYGET: {
238 IDispatch *disp = NULL;
239 HRESULT hres;
241 hres = IHTMLDOMChildrenCollection_item(&This->IHTMLDOMChildrenCollection_iface,
242 id - DISPID_CHILDCOL_0, &disp);
243 if(FAILED(hres))
244 return hres;
246 V_VT(res) = VT_DISPATCH;
247 V_DISPATCH(res) = disp;
248 break;
251 default:
252 FIXME("unimplemented flags %x\n", flags);
253 return E_NOTIMPL;
256 return S_OK;
259 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl = {
260 NULL,
261 HTMLDOMChildrenCollection_get_dispid,
262 HTMLDOMChildrenCollection_invoke,
263 NULL
266 static const tid_t HTMLDOMChildrenCollection_iface_tids[] = {
267 IHTMLDOMChildrenCollection_tid,
271 static dispex_static_data_t HTMLDOMChildrenCollection_dispex = {
272 &HTMLDOMChildrenCollection_dispex_vtbl,
273 DispDOMChildrenCollection_tid,
274 NULL,
275 HTMLDOMChildrenCollection_iface_tids
278 static IHTMLDOMChildrenCollection *create_child_collection(HTMLDocumentNode *doc, nsIDOMNodeList *nslist)
280 HTMLDOMChildrenCollection *ret;
282 ret = heap_alloc_zero(sizeof(*ret));
283 ret->IHTMLDOMChildrenCollection_iface.lpVtbl = &HTMLDOMChildrenCollectionVtbl;
284 ret->ref = 1;
286 nsIDOMNodeList_AddRef(nslist);
287 ret->nslist = nslist;
288 ret->doc = doc;
290 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLDOMChildrenCollection_iface,
291 &HTMLDOMChildrenCollection_dispex);
293 return &ret->IHTMLDOMChildrenCollection_iface;
296 static inline HTMLDOMNode *impl_from_IHTMLDOMNode(IHTMLDOMNode *iface)
298 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode_iface);
301 static HRESULT WINAPI HTMLDOMNode_QueryInterface(IHTMLDOMNode *iface,
302 REFIID riid, void **ppv)
304 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
306 return This->vtbl->qi(This, riid, ppv);
309 static ULONG WINAPI HTMLDOMNode_AddRef(IHTMLDOMNode *iface)
311 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
312 LONG ref = InterlockedIncrement(&This->ref);
314 TRACE("(%p) ref=%d\n", This, ref);
316 return ref;
319 static ULONG WINAPI HTMLDOMNode_Release(IHTMLDOMNode *iface)
321 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
322 LONG ref = InterlockedDecrement(&This->ref);
324 TRACE("(%p) ref=%d\n", This, ref);
326 if(!ref) {
327 This->vtbl->destructor(This);
328 release_dispex(&This->dispex);
329 heap_free(This);
332 return ref;
335 static HRESULT WINAPI HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode *iface, UINT *pctinfo)
337 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
338 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
341 static HRESULT WINAPI HTMLDOMNode_GetTypeInfo(IHTMLDOMNode *iface, UINT iTInfo,
342 LCID lcid, ITypeInfo **ppTInfo)
344 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
345 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
348 static HRESULT WINAPI HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode *iface, REFIID riid,
349 LPOLESTR *rgszNames, UINT cNames,
350 LCID lcid, DISPID *rgDispId)
352 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
353 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
354 lcid, rgDispId);
357 static HRESULT WINAPI HTMLDOMNode_Invoke(IHTMLDOMNode *iface, DISPID dispIdMember,
358 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
359 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
361 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
362 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
363 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
366 static HRESULT WINAPI HTMLDOMNode_get_nodeType(IHTMLDOMNode *iface, LONG *p)
368 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
369 PRUint16 type = -1;
371 TRACE("(%p)->(%p)\n", This, p);
373 nsIDOMNode_GetNodeType(This->nsnode, &type);
375 switch(type) {
376 case ELEMENT_NODE:
377 *p = 1;
378 break;
379 case TEXT_NODE:
380 *p = 3;
381 break;
382 case COMMENT_NODE:
383 *p = 8;
384 break;
385 case DOCUMENT_NODE:
386 *p = 9;
387 break;
388 case DOCUMENT_FRAGMENT_NODE:
389 *p = 11;
390 break;
391 default:
393 * FIXME:
394 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
395 * It needs more tests.
397 FIXME("type %u\n", type);
398 *p = 0;
401 return S_OK;
404 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
406 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
407 HTMLDOMNode *node;
408 nsIDOMNode *nsnode;
409 nsresult nsres;
410 HRESULT hres;
412 TRACE("(%p)->(%p)\n", This, p);
414 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
415 if(NS_FAILED(nsres)) {
416 ERR("GetParentNode failed: %08x\n", nsres);
417 return E_FAIL;
420 if(!nsnode) {
421 *p = NULL;
422 return S_OK;
425 hres = get_node(This->doc, nsnode, TRUE, &node);
426 nsIDOMNode_Release(nsnode);
427 if(FAILED(hres))
428 return hres;
430 *p = &node->IHTMLDOMNode_iface;
431 return S_OK;
434 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
436 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
437 cpp_bool has_child = FALSE;
438 nsresult nsres;
440 TRACE("(%p)->(%p)\n", This, fChildren);
442 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
443 if(NS_FAILED(nsres))
444 ERR("HasChildNodes failed: %08x\n", nsres);
446 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
447 return S_OK;
450 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
452 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
453 nsIDOMNodeList *nslist;
454 nsresult nsres;
456 TRACE("(%p)->(%p)\n", This, p);
458 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
459 if(NS_FAILED(nsres)) {
460 ERR("GetChildNodes failed: %08x\n", nsres);
461 return E_FAIL;
464 *p = (IDispatch*)create_child_collection(This->doc, nslist);
465 nsIDOMNodeList_Release(nslist);
467 return S_OK;
470 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
472 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
473 HTMLAttributeCollection *col;
474 HRESULT hres;
476 TRACE("(%p)->(%p)\n", This, p);
478 if(This->vtbl->get_attr_col) {
479 hres = This->vtbl->get_attr_col(This, &col);
480 if(FAILED(hres))
481 return hres;
483 *p = (IDispatch*)&col->IHTMLAttributeCollection_iface;
484 return S_OK;
487 *p = NULL;
488 return S_OK;
491 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
492 VARIANT refChild, IHTMLDOMNode **node)
494 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
495 nsIDOMNode *nsnode, *nsref = NULL;
496 HTMLDOMNode *new_child;
497 HTMLDOMNode *node_obj;
498 nsresult nsres;
499 HRESULT hres;
501 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
503 new_child = get_node_obj(This->doc, (IUnknown*)newChild);
504 if(!new_child) {
505 ERR("invalid newChild\n");
506 return E_INVALIDARG;
509 switch(V_VT(&refChild)) {
510 case VT_NULL:
511 break;
512 case VT_DISPATCH: {
513 HTMLDOMNode *ref_node;
515 ref_node = get_node_obj(This->doc, (IUnknown*)V_DISPATCH(&refChild));
516 if(!ref_node) {
517 ERR("unvalid node\n");
518 return E_FAIL;
521 nsref = ref_node->nsnode;
522 break;
524 default:
525 FIXME("unimplemented refChild %s\n", debugstr_variant(&refChild));
526 return E_NOTIMPL;
529 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, nsref, &nsnode);
530 if(NS_FAILED(nsres)) {
531 ERR("InsertBefore failed: %08x\n", nsres);
532 return E_FAIL;
535 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
536 nsIDOMNode_Release(nsnode);
537 if(FAILED(hres))
538 return hres;
540 *node = &node_obj->IHTMLDOMNode_iface;
541 return S_OK;
544 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
545 IHTMLDOMNode **node)
547 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
548 HTMLDOMNode *node_obj;
549 nsIDOMNode *nsnode;
550 nsresult nsres;
551 HRESULT hres;
553 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
555 node_obj = get_node_obj(This->doc, (IUnknown*)oldChild);
556 if(!node_obj)
557 return E_FAIL;
559 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
560 if(NS_FAILED(nsres)) {
561 ERR("RemoveChild failed: %08x\n", nsres);
562 return E_FAIL;
565 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
566 nsIDOMNode_Release(nsnode);
567 if(FAILED(hres))
568 return hres;
570 /* FIXME: Make sure that node != newChild */
571 *node = &node_obj->IHTMLDOMNode_iface;
572 return S_OK;
575 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
576 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
578 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
579 HTMLDOMNode *node_new;
580 HTMLDOMNode *node_old;
581 nsIDOMNode *nsnode;
582 nsresult nsres;
583 HRESULT hres;
585 TRACE("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
587 node_new = get_node_obj(This->doc, (IUnknown*)newChild);
588 if(!node_new)
589 return E_FAIL;
591 node_old = get_node_obj(This->doc, (IUnknown*)oldChild);
592 if(!node_old)
593 return E_FAIL;
595 nsres = nsIDOMNode_ReplaceChild(This->nsnode, node_new->nsnode, node_old->nsnode, &nsnode);
596 if(NS_FAILED(nsres)) {
597 return E_FAIL;
600 nsnode = node_new->nsnode;
602 hres = get_node(This->doc, nsnode, TRUE, &node_new);
603 nsIDOMNode_Release(nsnode);
604 if(FAILED(hres))
605 return hres;
607 *node = &node_new->IHTMLDOMNode_iface;
608 return S_OK;
611 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
612 IHTMLDOMNode **clonedNode)
614 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
615 HTMLDOMNode *new_node;
616 nsIDOMNode *nsnode;
617 nsresult nsres;
618 HRESULT hres;
620 TRACE("(%p)->(%x %p)\n", This, fDeep, clonedNode);
622 nsres = nsIDOMNode_CloneNode(This->nsnode, fDeep != VARIANT_FALSE, 1, &nsnode);
623 if(NS_FAILED(nsres) || !nsnode) {
624 ERR("CloneNode failed: %08x\n", nsres);
625 return E_FAIL;
628 hres = This->vtbl->clone(This, nsnode, &new_node);
629 if(FAILED(hres))
630 return hres;
632 *clonedNode = &new_node->IHTMLDOMNode_iface;
633 return S_OK;
636 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
637 IHTMLDOMNode **removed)
639 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
640 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
641 return E_NOTIMPL;
644 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
645 IHTMLDOMNode **swappedNode)
647 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
648 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
649 return E_NOTIMPL;
652 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
653 IHTMLDOMNode **replaced)
655 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
656 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
657 return E_NOTIMPL;
660 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
661 IHTMLDOMNode **node)
663 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
664 HTMLDOMNode *node_obj;
665 nsIDOMNode *nsnode;
666 nsresult nsres;
667 HRESULT hres;
669 TRACE("(%p)->(%p %p)\n", This, newChild, node);
671 node_obj = get_node_obj(This->doc, (IUnknown*)newChild);
672 if(!node_obj)
673 return E_FAIL;
675 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
676 if(NS_FAILED(nsres)) {
677 WARN("AppendChild failed: %08x\n", nsres);
678 nsnode = node_obj->nsnode;
681 hres = get_node(This->doc, nsnode, TRUE, &node_obj);
682 nsIDOMNode_Release(nsnode);
683 if(FAILED(hres))
684 return hres;
686 /* FIXME: Make sure that node != newChild */
687 *node = &node_obj->IHTMLDOMNode_iface;
688 return S_OK;
691 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
693 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
694 nsAString name;
695 nsresult nsres;
697 TRACE("(%p)->(%p)\n", This, p);
699 nsAString_Init(&name, NULL);
700 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name);
701 return return_nsstr(nsres, &name, p);
704 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
706 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
708 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
710 switch(V_VT(&v)) {
711 case VT_BSTR: {
712 nsAString val_str;
714 nsAString_InitDepend(&val_str, V_BSTR(&v));
715 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
716 nsAString_Finish(&val_str);
718 return S_OK;
721 default:
722 FIXME("unsupported value %s\n", debugstr_variant(&v));
725 return E_NOTIMPL;
728 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
730 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
731 const PRUnichar *val;
732 nsAString val_str;
734 TRACE("(%p)->(%p)\n", This, p);
736 nsAString_Init(&val_str, NULL);
737 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
738 nsAString_GetData(&val_str, &val);
740 if(*val) {
741 V_VT(p) = VT_BSTR;
742 V_BSTR(p) = SysAllocString(val);
743 }else {
744 V_VT(p) = VT_NULL;
747 nsAString_Finish(&val_str);
749 return S_OK;
752 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
754 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
755 nsIDOMNode *nschild = NULL;
756 HTMLDOMNode *node;
757 HRESULT hres;
759 TRACE("(%p)->(%p)\n", This, p);
761 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
762 if(!nschild) {
763 *p = NULL;
764 return S_OK;
767 hres = get_node(This->doc, nschild, TRUE, &node);
768 nsIDOMNode_Release(nschild);
769 if(FAILED(hres))
770 return hres;
772 *p = &node->IHTMLDOMNode_iface;
773 return S_OK;
776 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
778 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
779 nsIDOMNode *nschild = NULL;
780 HTMLDOMNode *node;
781 HRESULT hres;
783 TRACE("(%p)->(%p)\n", This, p);
785 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
786 if(!nschild) {
787 *p = NULL;
788 return S_OK;
791 hres = get_node(This->doc, nschild, TRUE, &node);
792 nsIDOMNode_Release(nschild);
793 if(FAILED(hres))
794 return hres;
796 *p = &node->IHTMLDOMNode_iface;
797 return S_OK;
800 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
802 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
803 nsIDOMNode *nschild = NULL;
804 HTMLDOMNode *node;
805 HRESULT hres;
807 TRACE("(%p)->(%p)\n", This, p);
809 nsIDOMNode_GetPreviousSibling(This->nsnode, &nschild);
810 if(!nschild) {
811 *p = NULL;
812 return S_OK;
815 hres = get_node(This->doc, nschild, TRUE, &node);
816 nsIDOMNode_Release(nschild);
817 if(FAILED(hres))
818 return hres;
820 *p = &node->IHTMLDOMNode_iface;
821 return S_OK;
824 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
826 HTMLDOMNode *This = impl_from_IHTMLDOMNode(iface);
827 nsIDOMNode *nssibling = NULL;
828 HTMLDOMNode *node;
829 HRESULT hres;
831 TRACE("(%p)->(%p)\n", This, p);
833 nsIDOMNode_GetNextSibling(This->nsnode, &nssibling);
834 if(!nssibling) {
835 *p = NULL;
836 return S_OK;
839 hres = get_node(This->doc, nssibling, TRUE, &node);
840 nsIDOMNode_Release(nssibling);
841 if(FAILED(hres))
842 return hres;
844 *p = &node->IHTMLDOMNode_iface;
845 return S_OK;
848 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
849 HTMLDOMNode_QueryInterface,
850 HTMLDOMNode_AddRef,
851 HTMLDOMNode_Release,
852 HTMLDOMNode_GetTypeInfoCount,
853 HTMLDOMNode_GetTypeInfo,
854 HTMLDOMNode_GetIDsOfNames,
855 HTMLDOMNode_Invoke,
856 HTMLDOMNode_get_nodeType,
857 HTMLDOMNode_get_parentNode,
858 HTMLDOMNode_hasChildNodes,
859 HTMLDOMNode_get_childNodes,
860 HTMLDOMNode_get_attributes,
861 HTMLDOMNode_insertBefore,
862 HTMLDOMNode_removeChild,
863 HTMLDOMNode_replaceChild,
864 HTMLDOMNode_cloneNode,
865 HTMLDOMNode_removeNode,
866 HTMLDOMNode_swapNode,
867 HTMLDOMNode_replaceNode,
868 HTMLDOMNode_appendChild,
869 HTMLDOMNode_get_nodeName,
870 HTMLDOMNode_put_nodeValue,
871 HTMLDOMNode_get_nodeValue,
872 HTMLDOMNode_get_firstChild,
873 HTMLDOMNode_get_lastChild,
874 HTMLDOMNode_get_previousSibling,
875 HTMLDOMNode_get_nextSibling
878 static inline HTMLDOMNode *impl_from_IHTMLDOMNode2(IHTMLDOMNode2 *iface)
880 return CONTAINING_RECORD(iface, HTMLDOMNode, IHTMLDOMNode2_iface);
883 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
884 REFIID riid, void **ppv)
886 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
888 return IHTMLDOMNode_QueryInterface(&This->IHTMLDOMNode_iface, riid, ppv);
891 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
893 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
895 return IHTMLDOMNode_AddRef(&This->IHTMLDOMNode_iface);
898 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
900 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
902 return IHTMLDOMNode_Release(&This->IHTMLDOMNode_iface);
905 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
907 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
908 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
911 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
912 LCID lcid, ITypeInfo **ppTInfo)
914 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
915 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
918 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
919 LPOLESTR *rgszNames, UINT cNames,
920 LCID lcid, DISPID *rgDispId)
922 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
923 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
924 lcid, rgDispId);
927 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
928 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
929 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
931 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
932 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
933 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
936 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
938 HTMLDOMNode *This = impl_from_IHTMLDOMNode2(iface);
940 TRACE("(%p)->(%p)\n", This, p);
942 /* FIXME: Better check for document node */
943 if(This == &This->doc->node) {
944 *p = NULL;
945 }else {
946 *p = (IDispatch*)&This->doc->basedoc.IHTMLDocument2_iface;
947 IDispatch_AddRef(*p);
949 return S_OK;
952 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
953 HTMLDOMNode2_QueryInterface,
954 HTMLDOMNode2_AddRef,
955 HTMLDOMNode2_Release,
956 HTMLDOMNode2_GetTypeInfoCount,
957 HTMLDOMNode2_GetTypeInfo,
958 HTMLDOMNode2_GetIDsOfNames,
959 HTMLDOMNode2_Invoke,
960 HTMLDOMNode2_get_ownerDocument
963 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
965 *ppv = NULL;
967 if(IsEqualGUID(&IID_IUnknown, riid)) {
968 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
969 *ppv = &This->IHTMLDOMNode_iface;
970 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
971 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
972 *ppv = &This->IHTMLDOMNode_iface;
973 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
974 if(This->dispex.data) {
975 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
976 *ppv = &This->dispex.IDispatchEx_iface;
977 }else {
978 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
979 return E_NOINTERFACE;
981 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
982 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
983 *ppv = &This->IHTMLDOMNode_iface;
984 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
985 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
986 *ppv = &This->IHTMLDOMNode2_iface;
987 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
988 return *ppv ? S_OK : E_NOINTERFACE;
991 if(*ppv) {
992 IUnknown_AddRef((IUnknown*)*ppv);
993 return S_OK;
996 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
997 return E_NOINTERFACE;
1000 void HTMLDOMNode_destructor(HTMLDOMNode *This)
1002 if(This->nsnode)
1003 nsIDOMNode_Release(This->nsnode);
1004 if(This->event_target)
1005 release_event_target(This->event_target);
1008 static HRESULT HTMLDOMNode_clone(HTMLDOMNode *This, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1010 return create_node(This->doc, nsnode, ret);
1013 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
1014 HTMLDOMNode_QI,
1015 HTMLDOMNode_destructor,
1016 HTMLDOMNode_clone
1019 void HTMLDOMNode_Init(HTMLDocumentNode *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
1021 node->IHTMLDOMNode_iface.lpVtbl = &HTMLDOMNodeVtbl;
1022 node->IHTMLDOMNode2_iface.lpVtbl = &HTMLDOMNode2Vtbl;
1023 node->ref = 2;
1024 node->doc = doc;
1026 if(&doc->node != node)
1027 node->ref++; /* one extra for list entry reference */
1029 if(nsnode)
1030 nsIDOMNode_AddRef(nsnode);
1031 node->nsnode = nsnode;
1033 node->next = doc->nodes;
1034 doc->nodes = node;
1037 static HRESULT create_node(HTMLDocumentNode *doc, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1039 PRUint16 node_type;
1040 HRESULT hres;
1042 nsIDOMNode_GetNodeType(nsnode, &node_type);
1044 switch(node_type) {
1045 case ELEMENT_NODE: {
1046 HTMLElement *elem;
1047 hres = HTMLElement_Create(doc, nsnode, FALSE, &elem);
1048 if(FAILED(hres))
1049 return hres;
1050 *ret = &elem->node;
1051 break;
1053 case TEXT_NODE:
1054 hres = HTMLDOMTextNode_Create(doc, nsnode, ret);
1055 if(FAILED(hres))
1056 return hres;
1057 break;
1058 case COMMENT_NODE: {
1059 HTMLElement *comment;
1060 hres = HTMLCommentElement_Create(doc, nsnode, &comment);
1061 if(FAILED(hres))
1062 return hres;
1063 *ret = &comment->node;
1064 break;
1066 case ATTRIBUTE_NODE:
1067 ERR("Called on attribute node\n");
1068 return E_UNEXPECTED;
1069 default: {
1070 HTMLDOMNode *node;
1072 node = heap_alloc_zero(sizeof(HTMLDOMNode));
1073 if(!node)
1074 return E_OUTOFMEMORY;
1076 node->vtbl = &HTMLDOMNodeImplVtbl;
1077 HTMLDOMNode_Init(doc, node, nsnode);
1078 *ret = node;
1082 TRACE("type %d ret %p\n", node_type, *ret);
1083 return S_OK;
1087 * FIXME
1088 * List looks really ugly here. We should use a better data structure or
1089 * (better) find a way to store HTMLDOMelement pointer in nsIDOMNode.
1092 HRESULT get_node(HTMLDocumentNode *This, nsIDOMNode *nsnode, BOOL create, HTMLDOMNode **ret)
1094 HTMLDOMNode *iter = This->nodes;
1096 while(iter) {
1097 if(iter->nsnode == nsnode)
1098 break;
1099 iter = iter->next;
1102 if(iter || !create) {
1103 if(iter)
1104 IHTMLDOMNode_AddRef(&iter->IHTMLDOMNode_iface);
1105 *ret = iter;
1106 return S_OK;
1109 return create_node(This, nsnode, ret);
1113 * FIXME
1114 * We should use better way for getting node object (like private interface)
1115 * or avoid it at all.
1117 static HTMLDOMNode *get_node_obj(HTMLDocumentNode *This, IUnknown *iface)
1119 HTMLDOMNode *iter = This->nodes;
1120 IHTMLDOMNode *node;
1122 IUnknown_QueryInterface(iface, &IID_IHTMLDOMNode, (void**)&node);
1123 IHTMLDOMNode_Release(node);
1125 while(iter) {
1126 if(&iter->IHTMLDOMNode_iface == node)
1127 return iter;
1128 iter = iter->next;
1131 FIXME("Not found %p\n", iface);
1132 return NULL;
1135 void release_nodes(HTMLDocumentNode *This)
1137 HTMLDOMNode *iter, *next;
1139 if(!This->nodes)
1140 return;
1142 for(iter = This->nodes; iter; iter = next) {
1143 next = iter->next;
1144 iter->doc = NULL;
1145 if(&This->node != iter)
1146 IHTMLDOMNode_Release(&iter->IHTMLDOMNode_iface);