1 /*
2 * Copyright (C) 2007, 2008 Apple 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 #include "config.h"
31 #include "JSElement.h"
32
33 #include "CSSHelper.h"
34 #include "Document.h"
35 #include "ExceptionCode.h"
36 #include "HTMLFrameElementBase.h"
37 #include "HTMLNames.h"
38 #include "JSAttr.h"
39 #include "JSHTMLElementWrapperFactory.h"
40 #include "JSNodeList.h"
41 #include "NodeList.h"
42
43 #if ENABLE(SVG)
44 #include "JSSVGElementWrapperFactory.h"
45 #include "SVGElement.h"
46 #endif
47
48 using namespace JSC;
49
50 namespace WebCore {
51
52 using namespace HTMLNames;
53
allowSettingSrcToJavascriptURL(ExecState * exec,Element * element,const String & name,const String & value)54 static inline bool allowSettingSrcToJavascriptURL(ExecState* exec, Element* element, const String& name, const String& value)
55 {
56 if ((element->hasTagName(iframeTag) || element->hasTagName(frameTag)) && equalIgnoringCase(name, "src") && protocolIsJavaScript(deprecatedParseURL(value))) {
57 HTMLFrameElementBase* frame = static_cast<HTMLFrameElementBase*>(element);
58 if (!checkNodeSecurity(exec, frame->contentDocument()))
59 return false;
60 }
61 return true;
62 }
63
setAttribute(ExecState * exec,const ArgList & args)64 JSValue JSElement::setAttribute(ExecState* exec, const ArgList& args)
65 {
66 ExceptionCode ec = 0;
67 AtomicString name = args.at(0).toString(exec);
68 AtomicString value = args.at(1).toString(exec);
69
70 Element* imp = impl();
71 if (!allowSettingSrcToJavascriptURL(exec, imp, name, value))
72 return jsUndefined();
73
74 imp->setAttribute(name, value, ec);
75 setDOMException(exec, ec);
76 return jsUndefined();
77 }
78
setAttributeNode(ExecState * exec,const ArgList & args)79 JSValue JSElement::setAttributeNode(ExecState* exec, const ArgList& args)
80 {
81 ExceptionCode ec = 0;
82 Attr* newAttr = toAttr(args.at(0));
83 if (!newAttr) {
84 setDOMException(exec, TYPE_MISMATCH_ERR);
85 return jsUndefined();
86 }
87
88 Element* imp = impl();
89 if (!allowSettingSrcToJavascriptURL(exec, imp, newAttr->name(), newAttr->value()))
90 return jsUndefined();
91
92 JSValue result = toJS(exec, globalObject(), WTF::getPtr(imp->setAttributeNode(newAttr, ec)));
93 setDOMException(exec, ec);
94 return result;
95 }
96
setAttributeNS(ExecState * exec,const ArgList & args)97 JSValue JSElement::setAttributeNS(ExecState* exec, const ArgList& args)
98 {
99 ExceptionCode ec = 0;
100 AtomicString namespaceURI = valueToStringWithNullCheck(exec, args.at(0));
101 AtomicString qualifiedName = args.at(1).toString(exec);
102 AtomicString value = args.at(2).toString(exec);
103
104 Element* imp = impl();
105 if (!allowSettingSrcToJavascriptURL(exec, imp, qualifiedName, value))
106 return jsUndefined();
107
108 imp->setAttributeNS(namespaceURI, qualifiedName, value, ec);
109 setDOMException(exec, ec);
110 return jsUndefined();
111 }
112
setAttributeNodeNS(ExecState * exec,const ArgList & args)113 JSValue JSElement::setAttributeNodeNS(ExecState* exec, const ArgList& args)
114 {
115 ExceptionCode ec = 0;
116 Attr* newAttr = toAttr(args.at(0));
117 if (!newAttr) {
118 setDOMException(exec, TYPE_MISMATCH_ERR);
119 return jsUndefined();
120 }
121
122 Element* imp = impl();
123 if (!allowSettingSrcToJavascriptURL(exec, imp, newAttr->name(), newAttr->value()))
124 return jsUndefined();
125
126 JSValue result = toJS(exec, globalObject(), WTF::getPtr(imp->setAttributeNodeNS(newAttr, ec)));
127 setDOMException(exec, ec);
128 return result;
129 }
130
toJSNewlyCreated(ExecState * exec,JSDOMGlobalObject * globalObject,Element * element)131 JSValue toJSNewlyCreated(ExecState* exec, JSDOMGlobalObject* globalObject, Element* element)
132 {
133 if (!element)
134 return jsNull();
135
136 ASSERT(!getCachedDOMNodeWrapper(element->document(), element));
137
138 JSNode* wrapper;
139 if (element->isHTMLElement())
140 wrapper = createJSHTMLWrapper(exec, globalObject, static_cast<HTMLElement*>(element));
141 #if ENABLE(SVG)
142 else if (element->isSVGElement())
143 wrapper = createJSSVGWrapper(exec, globalObject, static_cast<SVGElement*>(element));
144 #endif
145 else
146 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Element, element);
147
148 return wrapper;
149 }
150
151 } // namespace WebCore
152