1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include "config.h"
24 #include "HTMLScriptElement.h"
25
26 #include "Document.h"
27 #include "EventNames.h"
28 #include "HTMLNames.h"
29 #include "MappedAttribute.h"
30 #include "ScriptEventListener.h"
31 #include "Text.h"
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
HTMLScriptElement(const QualifiedName & tagName,Document * doc,bool createdByParser)37 HTMLScriptElement::HTMLScriptElement(const QualifiedName& tagName, Document* doc, bool createdByParser)
38 : HTMLElement(tagName, doc)
39 , m_data(this, this)
40 {
41 ASSERT(hasTagName(scriptTag));
42 m_data.setCreatedByParser(createdByParser);
43 }
44
~HTMLScriptElement()45 HTMLScriptElement::~HTMLScriptElement()
46 {
47 }
48
isURLAttribute(Attribute * attr) const49 bool HTMLScriptElement::isURLAttribute(Attribute* attr) const
50 {
51 return attr->name() == sourceAttributeValue();
52 }
53
shouldExecuteAsJavaScript() const54 bool HTMLScriptElement::shouldExecuteAsJavaScript() const
55 {
56 return m_data.shouldExecuteAsJavaScript();
57 }
58
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)59 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
60 {
61 ScriptElement::childrenChanged(m_data);
62 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
63 }
64
parseMappedAttribute(MappedAttribute * attr)65 void HTMLScriptElement::parseMappedAttribute(MappedAttribute* attr)
66 {
67 const QualifiedName& attrName = attr->name();
68
69 if (attrName == srcAttr)
70 handleSourceAttribute(m_data, attr->value());
71 else if (attrName == onloadAttr)
72 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
73 else
74 HTMLElement::parseMappedAttribute(attr);
75 }
76
finishParsingChildren()77 void HTMLScriptElement::finishParsingChildren()
78 {
79 ScriptElement::finishParsingChildren(m_data, sourceAttributeValue());
80 HTMLElement::finishParsingChildren();
81 }
82
insertedIntoDocument()83 void HTMLScriptElement::insertedIntoDocument()
84 {
85 HTMLElement::insertedIntoDocument();
86 ScriptElement::insertedIntoDocument(m_data, sourceAttributeValue());
87 }
88
removedFromDocument()89 void HTMLScriptElement::removedFromDocument()
90 {
91 HTMLElement::removedFromDocument();
92 ScriptElement::removedFromDocument(m_data);
93 }
94
text() const95 String HTMLScriptElement::text() const
96 {
97 return m_data.scriptContent();
98 }
99
setText(const String & value)100 void HTMLScriptElement::setText(const String &value)
101 {
102 ExceptionCode ec = 0;
103 int numChildren = childNodeCount();
104
105 if (numChildren == 1 && firstChild()->isTextNode()) {
106 static_cast<Text*>(firstChild())->setData(value, ec);
107 return;
108 }
109
110 if (numChildren > 0)
111 removeChildren();
112
113 appendChild(document()->createTextNode(value.impl()), ec);
114 }
115
htmlFor() const116 String HTMLScriptElement::htmlFor() const
117 {
118 // DOM Level 1 says: reserved for future use.
119 return String();
120 }
121
setHtmlFor(const String &)122 void HTMLScriptElement::setHtmlFor(const String&)
123 {
124 // DOM Level 1 says: reserved for future use.
125 }
126
event() const127 String HTMLScriptElement::event() const
128 {
129 // DOM Level 1 says: reserved for future use.
130 return String();
131 }
132
setEvent(const String &)133 void HTMLScriptElement::setEvent(const String&)
134 {
135 // DOM Level 1 says: reserved for future use.
136 }
137
charset() const138 String HTMLScriptElement::charset() const
139 {
140 return charsetAttributeValue();
141 }
142
setCharset(const String & value)143 void HTMLScriptElement::setCharset(const String &value)
144 {
145 setAttribute(charsetAttr, value);
146 }
147
defer() const148 bool HTMLScriptElement::defer() const
149 {
150 return !getAttribute(deferAttr).isNull();
151 }
152
setDefer(bool defer)153 void HTMLScriptElement::setDefer(bool defer)
154 {
155 setAttribute(deferAttr, defer ? "" : 0);
156 }
157
src() const158 KURL HTMLScriptElement::src() const
159 {
160 return document()->completeURL(sourceAttributeValue());
161 }
162
setSrc(const String & value)163 void HTMLScriptElement::setSrc(const String &value)
164 {
165 setAttribute(srcAttr, value);
166 }
167
type() const168 String HTMLScriptElement::type() const
169 {
170 return typeAttributeValue();
171 }
172
setType(const String & value)173 void HTMLScriptElement::setType(const String &value)
174 {
175 setAttribute(typeAttr, value);
176 }
177
scriptCharset() const178 String HTMLScriptElement::scriptCharset() const
179 {
180 return m_data.scriptCharset();
181 }
182
scriptContent() const183 String HTMLScriptElement::scriptContent() const
184 {
185 return m_data.scriptContent();
186 }
187
addSubresourceAttributeURLs(ListHashSet<KURL> & urls) const188 void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
189 {
190 HTMLElement::addSubresourceAttributeURLs(urls);
191
192 addSubresourceURL(urls, src());
193 }
194
sourceAttributeValue() const195 String HTMLScriptElement::sourceAttributeValue() const
196 {
197 return getAttribute(srcAttr).string();
198 }
199
charsetAttributeValue() const200 String HTMLScriptElement::charsetAttributeValue() const
201 {
202 return getAttribute(charsetAttr).string();
203 }
204
typeAttributeValue() const205 String HTMLScriptElement::typeAttributeValue() const
206 {
207 return getAttribute(typeAttr).string();
208 }
209
languageAttributeValue() const210 String HTMLScriptElement::languageAttributeValue() const
211 {
212 return getAttribute(languageAttr).string();
213 }
214
forAttributeValue() const215 String HTMLScriptElement::forAttributeValue() const
216 {
217 return getAttribute(forAttr).string();
218 }
219
dispatchLoadEvent()220 void HTMLScriptElement::dispatchLoadEvent()
221 {
222 ASSERT(!m_data.haveFiredLoadEvent());
223 m_data.setHaveFiredLoadEvent(true);
224
225 dispatchEvent(eventNames().loadEvent, false, false);
226 }
227
dispatchErrorEvent()228 void HTMLScriptElement::dispatchErrorEvent()
229 {
230 dispatchEvent(eventNames().errorEvent, true, false);
231 }
232
233 }
234