1/*
2 * Copyright (C) 2007-2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "V8Element.h"
33
34#include "Attr.h"
35#include "CSSHelper.h"
36#include "Document.h"
37#include "Element.h"
38#include "ExceptionCode.h"
39#include "HTMLFrameElementBase.h"
40#include "HTMLNames.h"
41#include "Node.h"
42
43#include "V8Attr.h"
44#include "V8Binding.h"
45#include "V8BindingState.h"
46#include "V8HTMLElement.h"
47#include "V8Proxy.h"
48#include "V8SVGElement.h"
49
50#include <wtf/RefPtr.h>
51
52namespace WebCore {
53
54v8::Handle<v8::Value> V8Element::setAttributeCallback(const v8::Arguments& args)
55{
56    INC_STATS("DOM.Element.setAttribute()");
57    Element* element = V8Element::toNative(args.Holder());
58    String name = toWebCoreString(args[0]);
59    String value = toWebCoreString(args[1]);
60
61    ExceptionCode ec = 0;
62    V8BindingElement::setAttribute(V8BindingState::Only(), element, name, value, ec);
63    if (ec)
64        return throwError(ec);
65
66    return v8::Undefined();
67}
68
69v8::Handle<v8::Value> V8Element::setAttributeNodeCallback(const v8::Arguments& args)
70{
71    INC_STATS("DOM.Element.setAttributeNode()");
72    if (!V8Attr::HasInstance(args[0]))
73        return throwError(TYPE_MISMATCH_ERR);
74
75    Attr* newAttr = V8Attr::toNative(v8::Handle<v8::Object>::Cast(args[0]));
76    Element* element = V8Element::toNative(args.Holder());
77
78    ExceptionCode ec = 0;
79    RefPtr<Attr> result = V8BindingElement::setAttributeNode(V8BindingState::Only(), element, newAttr, ec);
80    if (ec)
81        throwError(ec);
82
83    return toV8(result.release());
84}
85
86v8::Handle<v8::Value> V8Element::setAttributeNSCallback(const v8::Arguments& args)
87{
88    INC_STATS("DOM.Element.setAttributeNS()");
89    Element* element = V8Element::toNative(args.Holder());
90    String namespaceURI = toWebCoreStringWithNullCheck(args[0]);
91    String qualifiedName = toWebCoreString(args[1]);
92    String value = toWebCoreString(args[2]);
93
94    ExceptionCode ec = 0;
95    V8BindingElement::setAttributeNS(V8BindingState::Only(), element, namespaceURI, qualifiedName, value, ec);
96    if (ec)
97        throwError(ec);
98
99    return v8::Undefined();
100}
101
102v8::Handle<v8::Value> V8Element::setAttributeNodeNSCallback(const v8::Arguments& args)
103{
104    INC_STATS("DOM.Element.setAttributeNodeNS()");
105    if (!V8Attr::HasInstance(args[0]))
106        return throwError(TYPE_MISMATCH_ERR);
107
108    Attr* newAttr = V8Attr::toNative(v8::Handle<v8::Object>::Cast(args[0]));
109    Element* element = V8Element::toNative(args.Holder());
110
111    ExceptionCode ec = 0;
112    RefPtr<Attr> result = V8BindingElement::setAttributeNodeNS(V8BindingState::Only(), element, newAttr, ec);
113    if (ec)
114        throwError(ec);
115
116    return toV8(result.release());
117}
118
119v8::Handle<v8::Value> toV8(Element* impl, bool forceNewObject)
120{
121    if (!impl)
122        return v8::Null();
123    if (impl->isHTMLElement())
124        return toV8(static_cast<HTMLElement*>(impl), forceNewObject);
125#if ENABLE(SVG)
126    if (impl->isSVGElement())
127        return toV8(static_cast<SVGElement*>(impl), forceNewObject);
128#endif
129    return V8Element::wrap(impl, forceNewObject);
130}
131} // namespace WebCore
132