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 "Element.h"
33
34 #include "Attr.h"
35 #include "CSSHelper.h"
36 #include "Document.h"
37 #include "ExceptionCode.h"
38 #include "HTMLFrameElementBase.h"
39 #include "HTMLNames.h"
40 #include "Node.h"
41
42 #include "V8Attr.h"
43 #include "V8Binding.h"
44 #include "V8CustomBinding.h"
45 #include "V8Proxy.h"
46
47 #include <wtf/RefPtr.h>
48
49 namespace WebCore {
50
CALLBACK_FUNC_DECL(ElementSetAttribute)51 CALLBACK_FUNC_DECL(ElementSetAttribute)
52 {
53 INC_STATS("DOM.Element.setAttribute()");
54 Element* element = V8DOMWrapper::convertDOMWrapperToNode<Element>(args.Holder());
55 String name = toWebCoreString(args[0]);
56 String value = toWebCoreString(args[1]);
57
58 if (!allowSettingSrcToJavascriptURL(element, name, value))
59 return v8::Undefined();
60
61 ExceptionCode ec = 0;
62 element->setAttribute(name, value, ec);
63 if (ec)
64 return throwError(ec);
65
66 return v8::Undefined();
67 }
68
CALLBACK_FUNC_DECL(ElementSetAttributeNode)69 CALLBACK_FUNC_DECL(ElementSetAttributeNode)
70 {
71 INC_STATS("DOM.Element.setAttributeNode()");
72 if (!V8Attr::HasInstance(args[0]))
73 return throwError(TYPE_MISMATCH_ERR);
74
75 Attr* newAttr = V8DOMWrapper::convertDOMWrapperToNode<Attr>(v8::Handle<v8::Object>::Cast(args[0]));
76 Element* element = V8DOMWrapper::convertDOMWrapperToNode<Element>(args.Holder());
77
78 if (!allowSettingSrcToJavascriptURL(element, newAttr->name(), newAttr->value()))
79 return v8::Undefined();
80
81 ExceptionCode ec = 0;
82 RefPtr<Attr> result = element->setAttributeNode(newAttr, ec);
83 if (ec)
84 throwError(ec);
85
86 return V8DOMWrapper::convertNodeToV8Object(result.release());
87 }
88
CALLBACK_FUNC_DECL(ElementSetAttributeNS)89 CALLBACK_FUNC_DECL(ElementSetAttributeNS)
90 {
91 INC_STATS("DOM.Element.setAttributeNS()");
92 Element* element = V8DOMWrapper::convertDOMWrapperToNode<Element>(args.Holder());
93 String namespaceURI = toWebCoreStringWithNullCheck(args[0]);
94 String qualifiedName = toWebCoreString(args[1]);
95 String value = toWebCoreString(args[2]);
96
97 if (!allowSettingSrcToJavascriptURL(element, qualifiedName, value))
98 return v8::Undefined();
99
100 ExceptionCode ec = 0;
101 element->setAttributeNS(namespaceURI, qualifiedName, value, ec);
102 if (ec)
103 throwError(ec);
104
105 return v8::Undefined();
106 }
107
CALLBACK_FUNC_DECL(ElementSetAttributeNodeNS)108 CALLBACK_FUNC_DECL(ElementSetAttributeNodeNS)
109 {
110 INC_STATS("DOM.Element.setAttributeNodeNS()");
111 if (!V8Attr::HasInstance(args[0]))
112 return throwError(TYPE_MISMATCH_ERR);
113
114 Attr* newAttr = V8DOMWrapper::convertDOMWrapperToNode<Attr>(v8::Handle<v8::Object>::Cast(args[0]));
115 Element* element = V8DOMWrapper::convertDOMWrapperToNode<Element>(args.Holder());
116
117 if (!allowSettingSrcToJavascriptURL(element, newAttr->name(), newAttr->value()))
118 return v8::Undefined();
119
120 ExceptionCode ec = 0;
121 RefPtr<Attr> result = element->setAttributeNodeNS(newAttr, ec);
122 if (ec)
123 throwError(ec);
124
125 return V8DOMWrapper::convertNodeToV8Object(result.release());
126 }
127
128 } // namespace WebCore
129