winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / mshtml / htmlattr.c
blob8359847193196c642dab3a3712aed51524968af5
1 /*
2 * Copyright 2011 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
20 #include <stdarg.h>
21 #include <assert.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
30 #include "mshtml_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 static inline HTMLDOMAttribute *impl_from_IHTMLDOMAttribute(IHTMLDOMAttribute *iface)
38 return CONTAINING_RECORD(iface, HTMLDOMAttribute, IHTMLDOMAttribute_iface);
41 static HRESULT WINAPI HTMLDOMAttribute_QueryInterface(IHTMLDOMAttribute *iface,
42 REFIID riid, void **ppv)
44 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
46 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
48 if(IsEqualGUID(&IID_IUnknown, riid)) {
49 *ppv = &This->IHTMLDOMAttribute_iface;
50 }else if(IsEqualGUID(&IID_IHTMLDOMAttribute, riid)) {
51 *ppv = &This->IHTMLDOMAttribute_iface;
52 }else if(IsEqualGUID(&IID_IHTMLDOMAttribute2, riid)) {
53 *ppv = &This->IHTMLDOMAttribute2_iface;
54 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
55 return *ppv ? S_OK : E_NOINTERFACE;
56 }else {
57 WARN("%s not supported\n", debugstr_mshtml_guid(riid));
58 *ppv = NULL;
59 return E_NOINTERFACE;
62 IUnknown_AddRef((IUnknown*)*ppv);
63 return S_OK;
66 static ULONG WINAPI HTMLDOMAttribute_AddRef(IHTMLDOMAttribute *iface)
68 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
69 LONG ref = InterlockedIncrement(&This->ref);
71 TRACE("(%p) ref=%d\n", This, ref);
73 return ref;
76 static ULONG WINAPI HTMLDOMAttribute_Release(IHTMLDOMAttribute *iface)
78 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
79 LONG ref = InterlockedDecrement(&This->ref);
81 TRACE("(%p) ref=%d\n", This, ref);
83 if(!ref) {
84 assert(!This->elem);
85 release_dispex(&This->dispex);
86 heap_free(This->name);
87 heap_free(This);
90 return ref;
93 static HRESULT WINAPI HTMLDOMAttribute_GetTypeInfoCount(IHTMLDOMAttribute *iface, UINT *pctinfo)
95 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
96 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
99 static HRESULT WINAPI HTMLDOMAttribute_GetTypeInfo(IHTMLDOMAttribute *iface, UINT iTInfo,
100 LCID lcid, ITypeInfo **ppTInfo)
102 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
103 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
106 static HRESULT WINAPI HTMLDOMAttribute_GetIDsOfNames(IHTMLDOMAttribute *iface, REFIID riid,
107 LPOLESTR *rgszNames, UINT cNames,
108 LCID lcid, DISPID *rgDispId)
110 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
111 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
112 lcid, rgDispId);
115 static HRESULT WINAPI HTMLDOMAttribute_Invoke(IHTMLDOMAttribute *iface, DISPID dispIdMember,
116 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
117 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
119 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
120 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
121 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
124 static HRESULT WINAPI HTMLDOMAttribute_get_nodeName(IHTMLDOMAttribute *iface, BSTR *p)
126 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
128 TRACE("(%p)->(%p)\n", This, p);
130 if(!This->elem) {
131 if(!This->name) {
132 FIXME("No name available\n");
133 return E_FAIL;
136 *p = SysAllocString(This->name);
137 return *p ? S_OK : E_OUTOFMEMORY;
140 return IDispatchEx_GetMemberName(&This->elem->node.dispex.IDispatchEx_iface, This->dispid, p);
143 static HRESULT WINAPI HTMLDOMAttribute_put_nodeValue(IHTMLDOMAttribute *iface, VARIANT v)
145 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
146 DISPID dispidNamed = DISPID_PROPERTYPUT;
147 DISPPARAMS dp = {&v, &dispidNamed, 1, 1};
148 EXCEPINFO ei;
149 VARIANT ret;
151 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
153 if(!This->elem) {
154 FIXME("NULL This->elem\n");
155 return E_UNEXPECTED;
158 memset(&ei, 0, sizeof(ei));
160 return IDispatchEx_InvokeEx(&This->elem->node.dispex.IDispatchEx_iface, This->dispid, LOCALE_SYSTEM_DEFAULT,
161 DISPATCH_PROPERTYPUT, &dp, &ret, &ei, NULL);
164 static HRESULT WINAPI HTMLDOMAttribute_get_nodeValue(IHTMLDOMAttribute *iface, VARIANT *p)
166 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
167 DISPPARAMS dp = {NULL, NULL, 0, 0};
168 EXCEPINFO ei;
170 TRACE("(%p)->(%p)\n", This, p);
172 if(!This->elem) {
173 FIXME("NULL This->elem\n");
174 return E_UNEXPECTED;
177 memset(&ei, 0, sizeof(ei));
178 return IDispatchEx_InvokeEx(&This->elem->node.dispex.IDispatchEx_iface, This->dispid, LOCALE_SYSTEM_DEFAULT,
179 DISPATCH_PROPERTYGET, &dp, p, &ei, NULL);
182 static HRESULT WINAPI HTMLDOMAttribute_get_specified(IHTMLDOMAttribute *iface, VARIANT_BOOL *p)
184 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute(iface);
185 nsIDOMAttr *nsattr;
186 nsAString nsname;
187 BSTR name;
188 nsresult nsres;
189 HRESULT hres;
191 TRACE("(%p)->(%p)\n", This, p);
193 if(!This->elem || !This->elem->nselem) {
194 FIXME("NULL This->elem\n");
195 return E_UNEXPECTED;
198 if(get_dispid_type(This->dispid) != DISPEXPROP_BUILTIN) {
199 *p = VARIANT_TRUE;
200 return S_OK;
203 hres = IDispatchEx_GetMemberName(&This->elem->node.dispex.IDispatchEx_iface, This->dispid, &name);
204 if(FAILED(hres))
205 return hres;
207 /* FIXME: This is not exactly right, we have some attributes that don't map directly to Gecko attributes. */
208 nsAString_InitDepend(&nsname, name);
209 nsres = nsIDOMHTMLElement_GetAttributeNode(This->elem->nselem, &nsname, &nsattr);
210 nsAString_Finish(&nsname);
211 SysFreeString(name);
212 if(NS_FAILED(nsres))
213 return E_FAIL;
215 /* If the Gecko attribute node can be found, we know that the attribute is specified.
216 There is no point in calling GetSpecified */
217 if(nsattr) {
218 nsIDOMAttr_Release(nsattr);
219 *p = VARIANT_TRUE;
220 }else {
221 *p = VARIANT_FALSE;
223 return S_OK;
226 static const IHTMLDOMAttributeVtbl HTMLDOMAttributeVtbl = {
227 HTMLDOMAttribute_QueryInterface,
228 HTMLDOMAttribute_AddRef,
229 HTMLDOMAttribute_Release,
230 HTMLDOMAttribute_GetTypeInfoCount,
231 HTMLDOMAttribute_GetTypeInfo,
232 HTMLDOMAttribute_GetIDsOfNames,
233 HTMLDOMAttribute_Invoke,
234 HTMLDOMAttribute_get_nodeName,
235 HTMLDOMAttribute_put_nodeValue,
236 HTMLDOMAttribute_get_nodeValue,
237 HTMLDOMAttribute_get_specified
240 static inline HTMLDOMAttribute *impl_from_IHTMLDOMAttribute2(IHTMLDOMAttribute2 *iface)
242 return CONTAINING_RECORD(iface, HTMLDOMAttribute, IHTMLDOMAttribute2_iface);
245 static HRESULT WINAPI HTMLDOMAttribute2_QueryInterface(IHTMLDOMAttribute2 *iface, REFIID riid, void **ppv)
247 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
248 return IHTMLDOMAttribute_QueryInterface(&This->IHTMLDOMAttribute_iface, riid, ppv);
251 static ULONG WINAPI HTMLDOMAttribute2_AddRef(IHTMLDOMAttribute2 *iface)
253 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
254 return IHTMLDOMAttribute_AddRef(&This->IHTMLDOMAttribute_iface);
257 static ULONG WINAPI HTMLDOMAttribute2_Release(IHTMLDOMAttribute2 *iface)
259 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
260 return IHTMLDOMAttribute_Release(&This->IHTMLDOMAttribute_iface);
263 static HRESULT WINAPI HTMLDOMAttribute2_GetTypeInfoCount(IHTMLDOMAttribute2 *iface, UINT *pctinfo)
265 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
266 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
269 static HRESULT WINAPI HTMLDOMAttribute2_GetTypeInfo(IHTMLDOMAttribute2 *iface, UINT iTInfo,
270 LCID lcid, ITypeInfo **ppTInfo)
272 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
273 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
276 static HRESULT WINAPI HTMLDOMAttribute2_GetIDsOfNames(IHTMLDOMAttribute2 *iface, REFIID riid,
277 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
279 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
280 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
281 lcid, rgDispId);
284 static HRESULT WINAPI HTMLDOMAttribute2_Invoke(IHTMLDOMAttribute2 *iface, DISPID dispIdMember,
285 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
286 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
288 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
289 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
290 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
293 static HRESULT WINAPI HTMLDOMAttribute2_get_name(IHTMLDOMAttribute2 *iface, BSTR *p)
295 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
296 FIXME("(%p)->(%p)\n", This, p);
297 return E_NOTIMPL;
300 static HRESULT WINAPI HTMLDOMAttribute2_put_value(IHTMLDOMAttribute2 *iface, BSTR v)
302 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
303 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
304 return E_NOTIMPL;
307 static HRESULT WINAPI HTMLDOMAttribute2_get_value(IHTMLDOMAttribute2 *iface, BSTR *p)
309 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
310 FIXME("(%p)->(%p)\n", This, p);
311 return E_NOTIMPL;
314 static HRESULT WINAPI HTMLDOMAttribute2_get_expando(IHTMLDOMAttribute2 *iface, VARIANT_BOOL *p)
316 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
318 TRACE("(%p)->(%p)\n", This, p);
320 *p = get_dispid_type(This->dispid) == DISPEXPROP_BUILTIN ? VARIANT_FALSE : VARIANT_TRUE;
321 return S_OK;
324 static HRESULT WINAPI HTMLDOMAttribute2_get_nodeType(IHTMLDOMAttribute2 *iface, LONG *p)
326 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
327 FIXME("(%p)->(%p)\n", This, p);
328 return E_NOTIMPL;
331 static HRESULT WINAPI HTMLDOMAttribute2_get_parentNode(IHTMLDOMAttribute2 *iface, IHTMLDOMNode **p)
333 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
334 FIXME("(%p)->(%p)\n", This, p);
335 return E_NOTIMPL;
338 static HRESULT WINAPI HTMLDOMAttribute2_get_childNodes(IHTMLDOMAttribute2 *iface, IDispatch **p)
340 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
341 FIXME("(%p)->(%p)\n", This, p);
342 return E_NOTIMPL;
345 static HRESULT WINAPI HTMLDOMAttribute2_get_firstChild(IHTMLDOMAttribute2 *iface, IHTMLDOMNode **p)
347 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
348 FIXME("(%p)->(%p)\n", This, p);
349 return E_NOTIMPL;
352 static HRESULT WINAPI HTMLDOMAttribute2_get_lastChild(IHTMLDOMAttribute2 *iface, IHTMLDOMNode **p)
354 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
355 FIXME("(%p)->(%p)\n", This, p);
356 return E_NOTIMPL;
359 static HRESULT WINAPI HTMLDOMAttribute2_get_previousSibling(IHTMLDOMAttribute2 *iface, IHTMLDOMNode **p)
361 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
362 FIXME("(%p)->(%p)\n", This, p);
363 return E_NOTIMPL;
366 static HRESULT WINAPI HTMLDOMAttribute2_get_nextSibling(IHTMLDOMAttribute2 *iface, IHTMLDOMNode **p)
368 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
369 FIXME("(%p)->(%p)\n", This, p);
370 return E_NOTIMPL;
373 static HRESULT WINAPI HTMLDOMAttribute2_get_attributes(IHTMLDOMAttribute2 *iface, IDispatch **p)
375 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
376 FIXME("(%p)->(%p)\n", This, p);
377 return E_NOTIMPL;
380 static HRESULT WINAPI HTMLDOMAttribute2_get_ownerDocument(IHTMLDOMAttribute2 *iface, IDispatch **p)
382 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
383 FIXME("(%p)->(%p)\n", This, p);
384 return E_NOTIMPL;
387 static HRESULT WINAPI HTMLDOMAttribute2_insertBefore(IHTMLDOMAttribute2 *iface, IHTMLDOMNode *newChild,
388 VARIANT refChild, IHTMLDOMNode **node)
390 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
391 FIXME("(%p)->(%p %s %p)\n", This, newChild, debugstr_variant(&refChild), node);
392 return E_NOTIMPL;
395 static HRESULT WINAPI HTMLDOMAttribute2_replaceChild(IHTMLDOMAttribute2 *iface, IHTMLDOMNode *newChild,
396 IHTMLDOMNode *oldChild, IHTMLDOMNode **node)
398 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
399 FIXME("(%p)->(%p %p %p)\n", This, newChild, oldChild, node);
400 return E_NOTIMPL;
403 static HRESULT WINAPI HTMLDOMAttribute2_removeChild(IHTMLDOMAttribute2 *iface, IHTMLDOMNode *oldChild,
404 IHTMLDOMNode **node)
406 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
407 FIXME("(%p)->(%p %p)\n", This, oldChild, node);
408 return E_NOTIMPL;
411 static HRESULT WINAPI HTMLDOMAttribute2_appendChild(IHTMLDOMAttribute2 *iface, IHTMLDOMNode *newChild,
412 IHTMLDOMNode **node)
414 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
415 FIXME("(%p)->(%p %p)\n", This, newChild, node);
416 return E_NOTIMPL;
419 static HRESULT WINAPI HTMLDOMAttribute2_hasChildNodes(IHTMLDOMAttribute2 *iface, VARIANT_BOOL *fChildren)
421 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
422 FIXME("(%p)->(%p)\n", This, fChildren);
423 return E_NOTIMPL;
426 static HRESULT WINAPI HTMLDOMAttribute2_cloneNode(IHTMLDOMAttribute2 *iface, VARIANT_BOOL fDeep,
427 IHTMLDOMAttribute **clonedNode)
429 HTMLDOMAttribute *This = impl_from_IHTMLDOMAttribute2(iface);
430 FIXME("(%p)->(%x %p)\n", This, fDeep, clonedNode);
431 return E_NOTIMPL;
434 static const IHTMLDOMAttribute2Vtbl HTMLDOMAttribute2Vtbl = {
435 HTMLDOMAttribute2_QueryInterface,
436 HTMLDOMAttribute2_AddRef,
437 HTMLDOMAttribute2_Release,
438 HTMLDOMAttribute2_GetTypeInfoCount,
439 HTMLDOMAttribute2_GetTypeInfo,
440 HTMLDOMAttribute2_GetIDsOfNames,
441 HTMLDOMAttribute2_Invoke,
442 HTMLDOMAttribute2_get_name,
443 HTMLDOMAttribute2_put_value,
444 HTMLDOMAttribute2_get_value,
445 HTMLDOMAttribute2_get_expando,
446 HTMLDOMAttribute2_get_nodeType,
447 HTMLDOMAttribute2_get_parentNode,
448 HTMLDOMAttribute2_get_childNodes,
449 HTMLDOMAttribute2_get_firstChild,
450 HTMLDOMAttribute2_get_lastChild,
451 HTMLDOMAttribute2_get_previousSibling,
452 HTMLDOMAttribute2_get_nextSibling,
453 HTMLDOMAttribute2_get_attributes,
454 HTMLDOMAttribute2_get_ownerDocument,
455 HTMLDOMAttribute2_insertBefore,
456 HTMLDOMAttribute2_replaceChild,
457 HTMLDOMAttribute2_removeChild,
458 HTMLDOMAttribute2_appendChild,
459 HTMLDOMAttribute2_hasChildNodes,
460 HTMLDOMAttribute2_cloneNode
463 static const tid_t HTMLDOMAttribute_iface_tids[] = {
464 IHTMLDOMAttribute_tid,
465 IHTMLDOMAttribute2_tid,
468 static dispex_static_data_t HTMLDOMAttribute_dispex = {
469 NULL,
470 DispHTMLDOMAttribute_tid,
472 HTMLDOMAttribute_iface_tids
475 HRESULT HTMLDOMAttribute_Create(const WCHAR *name, HTMLElement *elem, DISPID dispid, HTMLDOMAttribute **attr)
477 HTMLAttributeCollection *col;
478 HTMLDOMAttribute *ret;
479 HRESULT hres;
481 ret = heap_alloc_zero(sizeof(*ret));
482 if(!ret)
483 return E_OUTOFMEMORY;
485 ret->IHTMLDOMAttribute_iface.lpVtbl = &HTMLDOMAttributeVtbl;
486 ret->IHTMLDOMAttribute2_iface.lpVtbl = &HTMLDOMAttribute2Vtbl;
487 ret->ref = 1;
488 ret->dispid = dispid;
489 ret->elem = elem;
491 init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLDOMAttribute_iface,
492 &HTMLDOMAttribute_dispex);
494 /* For attributes attached to an element, (elem,dispid) pair should be valid used for its operation. */
495 if(elem) {
496 hres = HTMLElement_get_attr_col(&elem->node, &col);
497 if(FAILED(hres)) {
498 IHTMLDOMAttribute_Release(&ret->IHTMLDOMAttribute_iface);
499 return hres;
501 IHTMLAttributeCollection_Release(&col->IHTMLAttributeCollection_iface);
503 list_add_tail(&elem->attrs->attrs, &ret->entry);
506 /* For detached attributes we may still do most operations if we have its name available. */
507 if(name) {
508 ret->name = heap_strdupW(name);
509 if(!ret->name) {
510 IHTMLDOMAttribute_Release(&ret->IHTMLDOMAttribute_iface);
511 return E_OUTOFMEMORY;
515 *attr = ret;
516 return S_OK;