blob: 86cc3a2f2aa01c6c03546ab61d912d8677a27184 [file] [log] [blame]
darin@apple.com640fa302008-02-15 05:03:55 +00001/*
eseidel1bdb6f52006-05-12 20:44:25 +00002 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
darin@apple.com640fa302008-02-15 05:03:55 +00005 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
eseidel1bdb6f52006-05-12 20:44:25 +00006 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
eseidel1bdb6f52006-05-12 20:44:25 +000021 */
darin@apple.com640fa302008-02-15 05:03:55 +000022
eseidel1bdb6f52006-05-12 20:44:25 +000023#include "config.h"
24#include "HTMLScriptElement.h"
25
eseidel1bdb6f52006-05-12 20:44:25 +000026#include "Document.h"
27#include "EventNames.h"
eseidel1bdb6f52006-05-12 20:44:25 +000028#include "HTMLNames.h"
darin@apple.com9a925fa2009-05-04 18:00:34 +000029#include "MappedAttribute.h"
dglazkov@chromium.org26789aa2009-04-24 22:00:44 +000030#include "ScriptEventListener.h"
eseidel1bdb6f52006-05-12 20:44:25 +000031#include "Text.h"
32
33namespace WebCore {
34
35using namespace HTMLNames;
eseidel1bdb6f52006-05-12 20:44:25 +000036
jchaffraix@webkit.org4cc683b2008-12-09 00:06:41 +000037HTMLScriptElement::HTMLScriptElement(const QualifiedName& tagName, Document* doc, bool createdByParser)
jchaffraix@webkit.org94d95b02008-12-04 22:39:05 +000038 : HTMLElement(tagName, doc)
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000039 , m_data(this, this)
eseidel1bdb6f52006-05-12 20:44:25 +000040{
jchaffraix@webkit.org94d95b02008-12-04 22:39:05 +000041 ASSERT(hasTagName(scriptTag));
jchaffraix@webkit.org4cc683b2008-12-09 00:06:41 +000042 m_data.setCreatedByParser(createdByParser);
eseidel1bdb6f52006-05-12 20:44:25 +000043}
44
45HTMLScriptElement::~HTMLScriptElement()
46{
eseidel1bdb6f52006-05-12 20:44:25 +000047}
48
darin@apple.com640fa302008-02-15 05:03:55 +000049bool HTMLScriptElement::isURLAttribute(Attribute* attr) const
eseidel1bdb6f52006-05-12 20:44:25 +000050{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000051 return attr->name() == sourceAttributeValue();
52}
53
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000054bool HTMLScriptElement::shouldExecuteAsJavaScript() const
55{
56 return m_data.shouldExecuteAsJavaScript();
eseidel1bdb6f52006-05-12 20:44:25 +000057}
58
hyatt@apple.com86b942b2008-02-27 23:13:51 +000059void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
eseidel1bdb6f52006-05-12 20:44:25 +000060{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000061 ScriptElement::childrenChanged(m_data);
hyatt@apple.com86b942b2008-02-27 23:13:51 +000062 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
eseidel1bdb6f52006-05-12 20:44:25 +000063}
64
darin@apple.com640fa302008-02-15 05:03:55 +000065void HTMLScriptElement::parseMappedAttribute(MappedAttribute* attr)
eseidel1bdb6f52006-05-12 20:44:25 +000066{
67 const QualifiedName& attrName = attr->name();
eseidel1bdb6f52006-05-12 20:44:25 +000068
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000069 if (attrName == srcAttr)
70 handleSourceAttribute(m_data, attr->value());
71 else if (attrName == onloadAttr)
ggaren@apple.com181565c62009-04-21 05:58:59 +000072 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
eseidel1bdb6f52006-05-12 20:44:25 +000073 else
74 HTMLElement::parseMappedAttribute(attr);
75}
76
hyatt@apple.com1c531c92008-02-09 22:49:13 +000077void HTMLScriptElement::finishParsingChildren()
eseidel1bdb6f52006-05-12 20:44:25 +000078{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000079 ScriptElement::finishParsingChildren(m_data, sourceAttributeValue());
hyatt@apple.com1c531c92008-02-09 22:49:13 +000080 HTMLElement::finishParsingChildren();
eseidel1bdb6f52006-05-12 20:44:25 +000081}
82
83void HTMLScriptElement::insertedIntoDocument()
84{
85 HTMLElement::insertedIntoDocument();
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000086 ScriptElement::insertedIntoDocument(m_data, sourceAttributeValue());
eseidel1bdb6f52006-05-12 20:44:25 +000087}
88
89void HTMLScriptElement::removedFromDocument()
90{
91 HTMLElement::removedFromDocument();
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000092 ScriptElement::removedFromDocument(m_data);
eseidel1bdb6f52006-05-12 20:44:25 +000093}
94
95String HTMLScriptElement::text() const
96{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +000097 return m_data.scriptContent();
eseidel1bdb6f52006-05-12 20:44:25 +000098}
99
100void HTMLScriptElement::setText(const String &value)
101{
102 ExceptionCode ec = 0;
103 int numChildren = childNodeCount();
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000104
eseidel1bdb6f52006-05-12 20:44:25 +0000105 if (numChildren == 1 && firstChild()->isTextNode()) {
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000106 static_cast<Text*>(firstChild())->setData(value, ec);
eseidel1bdb6f52006-05-12 20:44:25 +0000107 return;
108 }
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000109
darin@apple.com640fa302008-02-15 05:03:55 +0000110 if (numChildren > 0)
eseidel1bdb6f52006-05-12 20:44:25 +0000111 removeChildren();
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000112
eseidel1bdb6f52006-05-12 20:44:25 +0000113 appendChild(document()->createTextNode(value.impl()), ec);
114}
115
116String HTMLScriptElement::htmlFor() const
117{
118 // DOM Level 1 says: reserved for future use.
119 return String();
120}
121
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000122void HTMLScriptElement::setHtmlFor(const String&)
eseidel1bdb6f52006-05-12 20:44:25 +0000123{
124 // DOM Level 1 says: reserved for future use.
125}
126
127String HTMLScriptElement::event() const
128{
129 // DOM Level 1 says: reserved for future use.
130 return String();
131}
132
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000133void HTMLScriptElement::setEvent(const String&)
eseidel1bdb6f52006-05-12 20:44:25 +0000134{
135 // DOM Level 1 says: reserved for future use.
136}
137
138String HTMLScriptElement::charset() const
139{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000140 return charsetAttributeValue();
eseidel1bdb6f52006-05-12 20:44:25 +0000141}
142
143void HTMLScriptElement::setCharset(const String &value)
144{
145 setAttribute(charsetAttr, value);
146}
147
148bool HTMLScriptElement::defer() const
149{
150 return !getAttribute(deferAttr).isNull();
151}
152
153void HTMLScriptElement::setDefer(bool defer)
154{
155 setAttribute(deferAttr, defer ? "" : 0);
156}
157
darin@apple.com640fa302008-02-15 05:03:55 +0000158KURL HTMLScriptElement::src() const
eseidel1bdb6f52006-05-12 20:44:25 +0000159{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000160 return document()->completeURL(sourceAttributeValue());
eseidel1bdb6f52006-05-12 20:44:25 +0000161}
162
163void HTMLScriptElement::setSrc(const String &value)
164{
165 setAttribute(srcAttr, value);
166}
167
168String HTMLScriptElement::type() const
169{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000170 return typeAttributeValue();
eseidel1bdb6f52006-05-12 20:44:25 +0000171}
172
173void HTMLScriptElement::setType(const String &value)
174{
175 setAttribute(typeAttr, value);
176}
177
ap@webkit.org16a7aeb2008-03-10 15:38:04 +0000178String HTMLScriptElement::scriptCharset() const
179{
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000180 return m_data.scriptCharset();
181}
182
183String HTMLScriptElement::scriptContent() const
184{
185 return m_data.scriptContent();
ap@webkit.org16a7aeb2008-03-10 15:38:04 +0000186}
187
ddkilzer@apple.comb27ae032008-12-06 11:54:46 +0000188void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
beidson@apple.coma4fb38f2008-03-27 04:08:17 +0000189{
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000190 HTMLElement::addSubresourceAttributeURLs(urls);
191
ddkilzer@apple.comb27ae032008-12-06 11:54:46 +0000192 addSubresourceURL(urls, src());
beidson@apple.coma4fb38f2008-03-27 04:08:17 +0000193}
194
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000195String HTMLScriptElement::sourceAttributeValue() const
196{
197 return getAttribute(srcAttr).string();
198}
199
200String HTMLScriptElement::charsetAttributeValue() const
201{
weinig@apple.com064861a2008-09-14 22:12:49 +0000202 return getAttribute(charsetAttr).string();
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000203}
204
205String HTMLScriptElement::typeAttributeValue() const
206{
weinig@apple.com064861a2008-09-14 22:12:49 +0000207 return getAttribute(typeAttr).string();
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000208}
209
210String HTMLScriptElement::languageAttributeValue() const
211{
212 return getAttribute(languageAttr).string();
213}
beidson@apple.comdc156cd2009-04-30 16:20:49 +0000214
215String HTMLScriptElement::forAttributeValue() const
216{
217 return getAttribute(forAttr).string();
218}
zimmermann@webkit.org8cc8ad02008-09-16 00:01:11 +0000219
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000220void HTMLScriptElement::dispatchLoadEvent()
221{
zimmermann@webkit.org50e33c72008-09-20 03:33:43 +0000222 ASSERT(!m_data.haveFiredLoadEvent());
223 m_data.setHaveFiredLoadEvent(true);
224
ggaren@apple.com15903b12009-04-18 01:31:50 +0000225 dispatchEvent(eventNames().loadEvent, false, false);
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000226}
227
228void HTMLScriptElement::dispatchErrorEvent()
229{
ggaren@apple.com15903b12009-04-18 01:31:50 +0000230 dispatchEvent(eventNames().errorEvent, true, false);
zimmermann@webkit.org9ae47e92008-08-14 02:22:35 +0000231}
232
eseidel1bdb6f52006-05-12 20:44:25 +0000233}