msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / mshtml / htmlnode.c
blobbee0376636e514f0c9ad00759c250f2e079c0270
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(HTMLDocument*,IUnknown*);
37 typedef struct {
38 DispatchEx dispex;
39 const IHTMLDOMChildrenCollectionVtbl *lpIHTMLDOMChildrenCollectionVtbl;
41 LONG ref;
43 /* FIXME: implement weak reference */
44 HTMLDocument *doc;
46 nsIDOMNodeList *nslist;
47 } HTMLDOMChildrenCollection;
49 #define HTMLCHILDCOL(x) ((IHTMLDOMChildrenCollection*) &(x)->lpIHTMLDOMChildrenCollectionVtbl)
51 #define HTMLCHILDCOL_THIS(iface) DEFINE_THIS(HTMLDOMChildrenCollection, IHTMLDOMChildrenCollection, iface)
53 static HRESULT WINAPI HTMLDOMChildrenCollection_QueryInterface(IHTMLDOMChildrenCollection *iface, REFIID riid, void **ppv)
55 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
57 *ppv = NULL;
59 if(IsEqualGUID(&IID_IUnknown, riid)) {
60 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
61 *ppv = HTMLCHILDCOL(This);
62 }else if(IsEqualGUID(&IID_IHTMLDOMChildrenCollection, riid)) {
63 TRACE("(%p)->(IID_IHTMLDOMChildrenCollection %p)\n", This, ppv);
64 *ppv = HTMLCHILDCOL(This);
65 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
66 return *ppv ? S_OK : E_NOINTERFACE;
69 if(*ppv) {
70 IUnknown_AddRef((IUnknown*)*ppv);
71 return S_OK;
74 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
75 return E_NOINTERFACE;
78 static ULONG WINAPI HTMLDOMChildrenCollection_AddRef(IHTMLDOMChildrenCollection *iface)
80 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
81 LONG ref = InterlockedIncrement(&This->ref);
83 TRACE("(%p) ref=%d\n", This, ref);
85 return ref;
88 static ULONG WINAPI HTMLDOMChildrenCollection_Release(IHTMLDOMChildrenCollection *iface)
90 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
91 LONG ref = InterlockedDecrement(&This->ref);
93 TRACE("(%p) ref=%d\n", This, ref);
95 if(!ref) {
96 nsIDOMNodeList_Release(This->nslist);
97 heap_free(This);
100 return ref;
103 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfoCount(IHTMLDOMChildrenCollection *iface, UINT *pctinfo)
105 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
106 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
109 static HRESULT WINAPI HTMLDOMChildrenCollection_GetTypeInfo(IHTMLDOMChildrenCollection *iface, UINT iTInfo,
110 LCID lcid, ITypeInfo **ppTInfo)
112 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
113 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
116 static HRESULT WINAPI HTMLDOMChildrenCollection_GetIDsOfNames(IHTMLDOMChildrenCollection *iface, REFIID riid,
117 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
119 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
120 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
123 static HRESULT WINAPI HTMLDOMChildrenCollection_Invoke(IHTMLDOMChildrenCollection *iface, DISPID dispIdMember,
124 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
125 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
127 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
128 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
129 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
132 static HRESULT WINAPI HTMLDOMChildrenCollection_get_length(IHTMLDOMChildrenCollection *iface, long *p)
134 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
135 PRUint32 length=0;
137 TRACE("(%p)->(%p)\n", This, p);
139 nsIDOMNodeList_GetLength(This->nslist, &length);
140 *p = length;
141 return S_OK;
144 static HRESULT WINAPI HTMLDOMChildrenCollection__newEnum(IHTMLDOMChildrenCollection *iface, IUnknown **p)
146 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
147 FIXME("(%p)->(%p)\n", This, p);
148 return E_NOTIMPL;
151 static HRESULT WINAPI HTMLDOMChildrenCollection_item(IHTMLDOMChildrenCollection *iface, long index, IDispatch **ppItem)
153 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
154 nsIDOMNode *nsnode = NULL;
155 PRUint32 length=0;
156 nsresult nsres;
158 TRACE("(%p)->(%ld %p)\n", This, index, ppItem);
160 nsIDOMNodeList_GetLength(This->nslist, &length);
161 if(index < 0 || index >= length)
162 return E_INVALIDARG;
164 nsres = nsIDOMNodeList_Item(This->nslist, index, &nsnode);
165 if(NS_FAILED(nsres) || !nsnode) {
166 ERR("Item failed: %08x\n", nsres);
167 return E_FAIL;
170 *ppItem = (IDispatch*)get_node(This->doc, nsnode, TRUE);
171 IDispatch_AddRef(*ppItem);
172 return S_OK;
175 #define DISPID_CHILDCOL_0 MSHTML_DISPID_CUSTOM_MIN
177 static HRESULT HTMLDOMChildrenCollection_get_dispid(IUnknown *iface, BSTR name, DWORD flags, DISPID *dispid)
179 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
180 WCHAR *ptr;
181 DWORD idx=0;
182 PRUint32 len = 0;
184 for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
185 idx = idx*10 + (*ptr-'0');
186 if(*ptr)
187 return DISP_E_UNKNOWNNAME;
189 nsIDOMNodeList_GetLength(This->nslist, &len);
190 if(idx >= len)
191 return DISP_E_UNKNOWNNAME;
193 *dispid = DISPID_CHILDCOL_0 + idx;
194 TRACE("ret %x\n", *dispid);
195 return S_OK;
198 static HRESULT HTMLDOMChildrenCollection_invoke(IUnknown *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
199 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
201 HTMLDOMChildrenCollection *This = HTMLCHILDCOL_THIS(iface);
203 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
205 switch(flags) {
206 case INVOKE_PROPERTYGET: {
207 IDispatch *disp = NULL;
208 HRESULT hres;
210 hres = IHTMLDOMChildrenCollection_item(HTMLCHILDCOL(This), id - DISPID_CHILDCOL_0, &disp);
211 if(0&&FAILED(hres))
212 return hres;
214 V_VT(res) = VT_DISPATCH;
215 V_DISPATCH(res) = disp;
216 break;
219 default:
220 FIXME("unimplemented flags %x\n", flags);
221 return E_NOTIMPL;
224 return S_OK;
227 #undef HTMLCHILDCOL_THIS
229 static const IHTMLDOMChildrenCollectionVtbl HTMLDOMChildrenCollectionVtbl = {
230 HTMLDOMChildrenCollection_QueryInterface,
231 HTMLDOMChildrenCollection_AddRef,
232 HTMLDOMChildrenCollection_Release,
233 HTMLDOMChildrenCollection_GetTypeInfoCount,
234 HTMLDOMChildrenCollection_GetTypeInfo,
235 HTMLDOMChildrenCollection_GetIDsOfNames,
236 HTMLDOMChildrenCollection_Invoke,
237 HTMLDOMChildrenCollection_get_length,
238 HTMLDOMChildrenCollection__newEnum,
239 HTMLDOMChildrenCollection_item
242 static const tid_t HTMLDOMChildrenCollection_iface_tids[] = {
243 IHTMLDOMChildrenCollection_tid,
247 static const dispex_static_data_vtbl_t HTMLDOMChildrenCollection_dispex_vtbl = {
248 HTMLDOMChildrenCollection_get_dispid,
249 HTMLDOMChildrenCollection_invoke
252 static dispex_static_data_t HTMLDOMChildrenCollection_dispex = {
253 &HTMLDOMChildrenCollection_dispex_vtbl,
254 DispDOMChildrenCollection_tid,
255 NULL,
256 HTMLDOMChildrenCollection_iface_tids
259 static IHTMLDOMChildrenCollection *create_child_collection(HTMLDocument *doc, nsIDOMNodeList *nslist)
261 HTMLDOMChildrenCollection *ret;
263 ret = heap_alloc_zero(sizeof(*ret));
264 ret->lpIHTMLDOMChildrenCollectionVtbl = &HTMLDOMChildrenCollectionVtbl;
265 ret->ref = 1;
267 nsIDOMNodeList_AddRef(nslist);
268 ret->nslist = nslist;
269 ret->doc = doc;
271 init_dispex(&ret->dispex, (IUnknown*)HTMLCHILDCOL(ret), &HTMLDOMChildrenCollection_dispex);
273 return HTMLCHILDCOL(ret);
276 #define HTMLDOMNODE_THIS(iface) DEFINE_THIS(HTMLDOMNode, HTMLDOMNode, iface)
278 static HRESULT WINAPI HTMLDOMNode_QueryInterface(IHTMLDOMNode *iface,
279 REFIID riid, void **ppv)
281 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
283 return This->vtbl->qi(This, riid, ppv);
286 static ULONG WINAPI HTMLDOMNode_AddRef(IHTMLDOMNode *iface)
288 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
289 LONG ref = InterlockedIncrement(&This->ref);
291 TRACE("(%p) ref=%d\n", This, ref);
293 return ref;
296 static ULONG WINAPI HTMLDOMNode_Release(IHTMLDOMNode *iface)
298 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
299 LONG ref = InterlockedDecrement(&This->ref);
301 TRACE("(%p) ref=%d\n", This, ref);
303 if(!ref) {
304 This->vtbl->destructor(This);
305 heap_free(This);
308 return ref;
311 static HRESULT WINAPI HTMLDOMNode_GetTypeInfoCount(IHTMLDOMNode *iface, UINT *pctinfo)
313 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
314 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
317 static HRESULT WINAPI HTMLDOMNode_GetTypeInfo(IHTMLDOMNode *iface, UINT iTInfo,
318 LCID lcid, ITypeInfo **ppTInfo)
320 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
321 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
324 static HRESULT WINAPI HTMLDOMNode_GetIDsOfNames(IHTMLDOMNode *iface, REFIID riid,
325 LPOLESTR *rgszNames, UINT cNames,
326 LCID lcid, DISPID *rgDispId)
328 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
329 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
332 static HRESULT WINAPI HTMLDOMNode_Invoke(IHTMLDOMNode *iface, DISPID dispIdMember,
333 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
334 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
336 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
337 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
338 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
341 static HRESULT WINAPI HTMLDOMNode_get_nodeType(IHTMLDOMNode *iface, long *p)
343 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
344 PRUint16 type = -1;
346 TRACE("(%p)->(%p)\n", This, p);
348 nsIDOMNode_GetNodeType(This->nsnode, &type);
350 switch(type) {
351 case ELEMENT_NODE:
352 *p = 1;
353 break;
354 case TEXT_NODE:
355 *p = 3;
356 break;
357 case COMMENT_NODE:
358 *p = 8;
359 break;
360 case DOCUMENT_NODE:
361 *p = 9;
362 break;
363 default:
365 * FIXME:
366 * According to MSDN only ELEMENT_NODE and TEXT_NODE are supported.
367 * It needs more tests.
369 FIXME("type %u\n", type);
370 *p = 0;
373 return S_OK;
376 static HRESULT WINAPI HTMLDOMNode_get_parentNode(IHTMLDOMNode *iface, IHTMLDOMNode **p)
378 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
379 HTMLDOMNode *node;
380 nsIDOMNode *nsnode;
381 nsresult nsres;
383 TRACE("(%p)->(%p)\n", This, p);
385 nsres = nsIDOMNode_GetParentNode(This->nsnode, &nsnode);
386 if(NS_FAILED(nsres)) {
387 ERR("GetParentNode failed: %08x\n", nsres);
388 return E_FAIL;
391 if(!nsnode) {
392 *p = NULL;
393 return S_OK;
396 node = get_node(This->doc, nsnode, TRUE);
397 *p = HTMLDOMNODE(node);
398 IHTMLDOMNode_AddRef(*p);
399 return S_OK;
402 static HRESULT WINAPI HTMLDOMNode_hasChildNodes(IHTMLDOMNode *iface, VARIANT_BOOL *fChildren)
404 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
405 PRBool has_child = FALSE;
406 nsresult nsres;
408 TRACE("(%p)->(%p)\n", This, fChildren);
410 nsres = nsIDOMNode_HasChildNodes(This->nsnode, &has_child);
411 if(NS_FAILED(nsres))
412 ERR("HasChildNodes failed: %08x\n", nsres);
414 *fChildren = has_child ? VARIANT_TRUE : VARIANT_FALSE;
415 return S_OK;
418 static HRESULT WINAPI HTMLDOMNode_get_childNodes(IHTMLDOMNode *iface, IDispatch **p)
420 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
421 nsIDOMNodeList *nslist;
422 nsresult nsres;
424 TRACE("(%p)->(%p)\n", This, p);
426 nsres = nsIDOMNode_GetChildNodes(This->nsnode, &nslist);
427 if(NS_FAILED(nsres)) {
428 ERR("GetChildNodes failed: %08x\n", nsres);
429 return E_FAIL;
432 *p = (IDispatch*)create_child_collection(This->doc, nslist);
433 nsIDOMNodeList_Release(nslist);
435 return S_OK;
438 static HRESULT WINAPI HTMLDOMNode_get_attributes(IHTMLDOMNode *iface, IDispatch **p)
440 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
441 FIXME("(%p)->(%p)\n", This, p);
442 return E_NOTIMPL;
445 static HRESULT WINAPI HTMLDOMNode_insertBefore(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
446 VARIANT refChild, IHTMLDOMNode **node)
448 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
449 nsIDOMNode *nsnode, *nsref = NULL;
450 HTMLDOMNode *new_child;
451 nsresult nsres;
453 TRACE("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
455 new_child = get_node_obj(This->doc, (IUnknown*)newChild);
456 if(!new_child) {
457 ERR("invalid newChild\n");
458 return E_INVALIDARG;
461 switch(V_VT(&refChild)) {
462 case VT_NULL:
463 break;
464 case VT_DISPATCH: {
465 HTMLDOMNode *ref_node;
467 ref_node = get_node_obj(This->doc, (IUnknown*)V_DISPATCH(&refChild));
468 if(!ref_node) {
469 ERR("unvalid node\n");
470 return E_FAIL;
473 nsref = ref_node->nsnode;
474 break;
476 default:
477 FIXME("unimplemented vt %d\n", V_VT(&refChild));
478 return E_NOTIMPL;
481 nsres = nsIDOMNode_InsertBefore(This->nsnode, new_child->nsnode, nsref, &nsnode);
482 if(NS_FAILED(nsres)) {
483 ERR("InsertBefore failed: %08x\n", nsres);
484 return E_FAIL;
487 *node = HTMLDOMNODE(get_node(This->doc, nsnode, TRUE));
488 nsIDOMNode_Release(nsnode);
489 IHTMLDOMNode_AddRef(*node);
490 return S_OK;
493 static HRESULT WINAPI HTMLDOMNode_removeChild(IHTMLDOMNode *iface, IHTMLDOMNode *oldChild,
494 IHTMLDOMNode **node)
496 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
497 HTMLDOMNode *node_obj;
498 nsIDOMNode *nsnode;
499 nsresult nsres;
501 TRACE("(%p)->(%p %p)\n", This, oldChild, node);
503 node_obj = get_node_obj(This->doc, (IUnknown*)oldChild);
504 if(!node_obj)
505 return E_FAIL;
507 nsres = nsIDOMNode_RemoveChild(This->nsnode, node_obj->nsnode, &nsnode);
508 if(NS_FAILED(nsres)) {
509 ERR("RemoveChild failed: %08x\n", nsres);
510 return E_FAIL;
513 /* FIXME: Make sure that node != newChild */
514 *node = HTMLDOMNODE(get_node(This->doc, nsnode, TRUE));
515 nsIDOMNode_Release(nsnode);
516 IHTMLDOMNode_AddRef(*node);
517 return S_OK;
520 static HRESULT WINAPI HTMLDOMNode_replaceChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
521 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
523 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
524 FIXME("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
525 return E_NOTIMPL;
528 static HRESULT WINAPI HTMLDOMNode_cloneNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
529 IHTMLDOMNode **clonedNode)
531 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
532 FIXME("(%p)->(%x %p)\n", This, fDeep, clonedNode);
533 return E_NOTIMPL;
536 static HRESULT WINAPI HTMLDOMNode_removeNode(IHTMLDOMNode *iface, VARIANT_BOOL fDeep,
537 IHTMLDOMNode **removed)
539 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
540 FIXME("(%p)->(%x %p)\n", This, fDeep, removed);
541 return E_NOTIMPL;
544 static HRESULT WINAPI HTMLDOMNode_swapNode(IHTMLDOMNode *iface, IHTMLDOMNode *otherNode,
545 IHTMLDOMNode **swappedNode)
547 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
548 FIXME("(%p)->(%p %p)\n", This, otherNode, swappedNode);
549 return E_NOTIMPL;
552 static HRESULT WINAPI HTMLDOMNode_replaceNode(IHTMLDOMNode *iface, IHTMLDOMNode *replacement,
553 IHTMLDOMNode **replaced)
555 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
556 FIXME("(%p)->(%p %p)\n", This, replacement, replaced);
557 return E_NOTIMPL;
560 static HRESULT WINAPI HTMLDOMNode_appendChild(IHTMLDOMNode *iface, IHTMLDOMNode *newChild,
561 IHTMLDOMNode **node)
563 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
564 HTMLDOMNode *node_obj;
565 nsIDOMNode *nsnode;
566 nsresult nsres;
568 TRACE("(%p)->(%p %p)\n", This, newChild, node);
570 node_obj = get_node_obj(This->doc, (IUnknown*)newChild);
571 if(!node_obj)
572 return E_FAIL;
574 nsres = nsIDOMNode_AppendChild(This->nsnode, node_obj->nsnode, &nsnode);
575 if(NS_FAILED(nsres)) {
576 WARN("AppendChild failed: %08x\n", nsres);
577 nsnode = node_obj->nsnode;
580 /* FIXME: Make sure that node != newChild */
581 *node = HTMLDOMNODE(get_node(This->doc, nsnode, TRUE));
582 IHTMLDOMNode_AddRef(*node);
583 return S_OK;
586 static HRESULT WINAPI HTMLDOMNode_get_nodeName(IHTMLDOMNode *iface, BSTR *p)
588 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
590 TRACE("(%p)->(%p)\n", This, p);
592 *p = NULL;
594 if(This->nsnode) {
595 nsAString name_str;
596 const PRUnichar *name;
597 nsresult nsres;
599 nsAString_Init(&name_str, NULL);
600 nsres = nsIDOMNode_GetNodeName(This->nsnode, &name_str);
602 if(NS_SUCCEEDED(nsres)) {
603 nsAString_GetData(&name_str, &name);
604 *p = SysAllocString(name);
605 }else {
606 ERR("GetNodeName failed: %08x\n", nsres);
609 nsAString_Finish(&name_str);
612 return S_OK;
615 static HRESULT WINAPI HTMLDOMNode_put_nodeValue(IHTMLDOMNode *iface, VARIANT v)
617 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
619 TRACE("(%p)->()\n", This);
621 switch(V_VT(&v)) {
622 case VT_BSTR: {
623 nsAString val_str;
625 TRACE("bstr %s\n", debugstr_w(V_BSTR(&v)));
627 nsAString_Init(&val_str, V_BSTR(&v));
628 nsIDOMNode_SetNodeValue(This->nsnode, &val_str);
629 nsAString_Finish(&val_str);
631 return S_OK;
634 default:
635 FIXME("unsupported vt %d\n", V_VT(&v));
638 return E_NOTIMPL;
641 static HRESULT WINAPI HTMLDOMNode_get_nodeValue(IHTMLDOMNode *iface, VARIANT *p)
643 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
644 const PRUnichar *val;
645 nsAString val_str;
647 TRACE("(%p)->(%p)\n", This, p);
649 nsAString_Init(&val_str, NULL);
650 nsIDOMNode_GetNodeValue(This->nsnode, &val_str);
651 nsAString_GetData(&val_str, &val);
653 if(*val) {
654 V_VT(p) = VT_BSTR;
655 V_BSTR(p) = SysAllocString(val);
656 }else {
657 V_VT(p) = VT_NULL;
660 nsAString_Finish(&val_str);
662 return S_OK;
665 static HRESULT WINAPI HTMLDOMNode_get_firstChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
667 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
668 nsIDOMNode *nschild = NULL;
670 TRACE("(%p)->(%p)\n", This, p);
672 nsIDOMNode_GetFirstChild(This->nsnode, &nschild);
673 if(nschild) {
674 *p = HTMLDOMNODE(get_node(This->doc, nschild, TRUE));
675 IHTMLDOMNode_AddRef(*p);
676 }else {
677 *p = NULL;
680 return S_OK;
683 static HRESULT WINAPI HTMLDOMNode_get_lastChild(IHTMLDOMNode *iface, IHTMLDOMNode **p)
685 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
686 nsIDOMNode *nschild = NULL;
688 TRACE("(%p)->(%p)\n", This, p);
690 nsIDOMNode_GetLastChild(This->nsnode, &nschild);
691 if(nschild) {
692 *p = HTMLDOMNODE(get_node(This->doc, nschild, TRUE));
693 IHTMLDOMNode_AddRef(*p);
694 }else {
695 *p = NULL;
698 return S_OK;
701 static HRESULT WINAPI HTMLDOMNode_get_previousSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
703 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
704 FIXME("(%p)->(%p)\n", This, p);
705 return E_NOTIMPL;
708 static HRESULT WINAPI HTMLDOMNode_get_nextSibling(IHTMLDOMNode *iface, IHTMLDOMNode **p)
710 HTMLDOMNode *This = HTMLDOMNODE_THIS(iface);
711 FIXME("(%p)->(%p)\n", This, p);
712 return E_NOTIMPL;
715 #undef HTMLDOMNODE_THIS
717 static const IHTMLDOMNodeVtbl HTMLDOMNodeVtbl = {
718 HTMLDOMNode_QueryInterface,
719 HTMLDOMNode_AddRef,
720 HTMLDOMNode_Release,
721 HTMLDOMNode_GetTypeInfoCount,
722 HTMLDOMNode_GetTypeInfo,
723 HTMLDOMNode_GetIDsOfNames,
724 HTMLDOMNode_Invoke,
725 HTMLDOMNode_get_nodeType,
726 HTMLDOMNode_get_parentNode,
727 HTMLDOMNode_hasChildNodes,
728 HTMLDOMNode_get_childNodes,
729 HTMLDOMNode_get_attributes,
730 HTMLDOMNode_insertBefore,
731 HTMLDOMNode_removeChild,
732 HTMLDOMNode_replaceChild,
733 HTMLDOMNode_cloneNode,
734 HTMLDOMNode_removeNode,
735 HTMLDOMNode_swapNode,
736 HTMLDOMNode_replaceNode,
737 HTMLDOMNode_appendChild,
738 HTMLDOMNode_get_nodeName,
739 HTMLDOMNode_put_nodeValue,
740 HTMLDOMNode_get_nodeValue,
741 HTMLDOMNode_get_firstChild,
742 HTMLDOMNode_get_lastChild,
743 HTMLDOMNode_get_previousSibling,
744 HTMLDOMNode_get_nextSibling
747 #define HTMLDOMNODE2_THIS(iface) DEFINE_THIS(HTMLDOMNode, HTMLDOMNode2, iface)
749 static HRESULT WINAPI HTMLDOMNode2_QueryInterface(IHTMLDOMNode2 *iface,
750 REFIID riid, void **ppv)
752 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
754 return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(This), riid, ppv);
757 static ULONG WINAPI HTMLDOMNode2_AddRef(IHTMLDOMNode2 *iface)
759 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
761 return IHTMLDOMNode_AddRef(HTMLDOMNODE(This));
764 static ULONG WINAPI HTMLDOMNode2_Release(IHTMLDOMNode2 *iface)
766 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
768 return IHTMLDOMNode_Release(HTMLDOMNODE(This));
771 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfoCount(IHTMLDOMNode2 *iface, UINT *pctinfo)
773 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
774 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
777 static HRESULT WINAPI HTMLDOMNode2_GetTypeInfo(IHTMLDOMNode2 *iface, UINT iTInfo,
778 LCID lcid, ITypeInfo **ppTInfo)
780 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
781 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
784 static HRESULT WINAPI HTMLDOMNode2_GetIDsOfNames(IHTMLDOMNode2 *iface, REFIID riid,
785 LPOLESTR *rgszNames, UINT cNames,
786 LCID lcid, DISPID *rgDispId)
788 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
789 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
792 static HRESULT WINAPI HTMLDOMNode2_Invoke(IHTMLDOMNode2 *iface, DISPID dispIdMember,
793 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
794 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
796 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
797 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
798 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
801 static HRESULT WINAPI HTMLDOMNode2_get_ownerDocument(IHTMLDOMNode2 *iface, IDispatch **p)
803 HTMLDOMNode *This = HTMLDOMNODE2_THIS(iface);
804 FIXME("(%p)->(%p)\n", This, p);
805 return E_NOTIMPL;
808 #undef HTMLDOMNODE2_THIS
810 static const IHTMLDOMNode2Vtbl HTMLDOMNode2Vtbl = {
811 HTMLDOMNode2_QueryInterface,
812 HTMLDOMNode2_AddRef,
813 HTMLDOMNode2_Release,
814 HTMLDOMNode2_GetTypeInfoCount,
815 HTMLDOMNode2_GetTypeInfo,
816 HTMLDOMNode2_GetIDsOfNames,
817 HTMLDOMNode2_Invoke,
818 HTMLDOMNode2_get_ownerDocument
821 HRESULT HTMLDOMNode_QI(HTMLDOMNode *This, REFIID riid, void **ppv)
823 *ppv = NULL;
825 if(IsEqualGUID(&IID_IUnknown, riid)) {
826 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
827 *ppv = HTMLDOMNODE(This);
828 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
829 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
830 *ppv = HTMLDOMNODE(This);
831 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
832 if(This->dispex.data) {
833 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
834 *ppv = DISPATCHEX(&This->dispex);
835 }else {
836 FIXME("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
837 return E_NOINTERFACE;
839 }else if(IsEqualGUID(&IID_IHTMLDOMNode, riid)) {
840 TRACE("(%p)->(IID_IHTMLDOMNode %p)\n", This, ppv);
841 *ppv = HTMLDOMNODE(This);
842 }else if(IsEqualGUID(&IID_IHTMLDOMNode2, riid)) {
843 TRACE("(%p)->(IID_IHTMLDOMNode2 %p)\n", This, ppv);
844 *ppv = HTMLDOMNODE2(This);
845 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
846 return *ppv ? S_OK : E_NOINTERFACE;
849 if(*ppv) {
850 IUnknown_AddRef((IUnknown*)*ppv);
851 return S_OK;
854 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
855 return E_NOINTERFACE;
858 void HTMLDOMNode_destructor(HTMLDOMNode *This)
860 if(This->nsnode)
861 nsIDOMNode_Release(This->nsnode);
862 if(This->event_target)
863 release_event_target(This->event_target);
866 static const NodeImplVtbl HTMLDOMNodeImplVtbl = {
867 HTMLDOMNode_QI,
868 HTMLDOMNode_destructor
871 void HTMLDOMNode_Init(HTMLDocument *doc, HTMLDOMNode *node, nsIDOMNode *nsnode)
873 node->lpHTMLDOMNodeVtbl = &HTMLDOMNodeVtbl;
874 node->lpHTMLDOMNode2Vtbl = &HTMLDOMNode2Vtbl;
875 node->ref = 1;
876 node->doc = doc;
878 nsIDOMNode_AddRef(nsnode);
879 node->nsnode = nsnode;
881 node->next = doc->nodes;
882 doc->nodes = node;
885 static HTMLDOMNode *create_node(HTMLDocument *doc, nsIDOMNode *nsnode)
887 HTMLDOMNode *ret;
888 PRUint16 node_type;
890 nsIDOMNode_GetNodeType(nsnode, &node_type);
892 switch(node_type) {
893 case ELEMENT_NODE:
894 ret = &HTMLElement_Create(doc, nsnode, FALSE)->node;
895 break;
896 case TEXT_NODE:
897 ret = HTMLDOMTextNode_Create(doc, nsnode);
898 break;
899 case COMMENT_NODE:
900 ret = &HTMLCommentElement_Create(doc, nsnode)->node;
901 break;
902 default:
903 ret = heap_alloc_zero(sizeof(HTMLDOMNode));
904 ret->vtbl = &HTMLDOMNodeImplVtbl;
905 HTMLDOMNode_Init(doc, ret, nsnode);
908 TRACE("type %d ret %p\n", node_type, ret);
910 return ret;
914 * FIXME
915 * List looks really ugly here. We should use a better data structure or
916 * (better) find a way to store HTMLDOMelement pointer in nsIDOMNode.
919 HTMLDOMNode *get_node(HTMLDocument *This, nsIDOMNode *nsnode, BOOL create)
921 HTMLDOMNode *iter = This->nodes;
923 while(iter) {
924 if(iter->nsnode == nsnode)
925 break;
926 iter = iter->next;
929 if(iter || !create)
930 return iter;
932 return create_node(This, nsnode);
936 * FIXME
937 * We should use better way for getting node object (like private interface)
938 * or avoid it at all.
940 static HTMLDOMNode *get_node_obj(HTMLDocument *This, IUnknown *iface)
942 HTMLDOMNode *iter = This->nodes;
943 IHTMLDOMNode *node;
945 IUnknown_QueryInterface(iface, &IID_IHTMLDOMNode, (void**)&node);
946 IHTMLDOMNode_Release(node);
948 while(iter) {
949 if(HTMLDOMNODE(iter) == node)
950 return iter;
951 iter = iter->next;
954 FIXME("Not found %p\n", iface);
955 return NULL;
958 void release_nodes(HTMLDocument *This)
960 HTMLDOMNode *iter, *next;
962 if(!This->nodes)
963 return;
965 for(iter = This->nodes; iter; iter = next) {
966 next = iter->next;
967 iter->doc = NULL;
968 IHTMLDOMNode_Release(HTMLDOMNODE(iter));