|
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 "Attribute.h" |
|
27 #include "Document.h" |
|
28 #include "Event.h" |
|
29 #include "EventNames.h" |
|
30 #include "HTMLNames.h" |
|
31 #include "ScriptEventListener.h" |
|
32 #include "Text.h" |
|
33 |
|
34 namespace WebCore { |
|
35 |
|
36 using namespace HTMLNames; |
|
37 |
|
38 inline HTMLScriptElement::HTMLScriptElement(const QualifiedName& tagName, Document* document, bool createdByParser) |
|
39 : HTMLElement(tagName, document) |
|
40 , m_data(this, this) |
|
41 { |
|
42 ASSERT(hasTagName(scriptTag)); |
|
43 m_data.setCreatedByParser(createdByParser); |
|
44 } |
|
45 |
|
46 PassRefPtr<HTMLScriptElement> HTMLScriptElement::create(const QualifiedName& tagName, Document* document, bool createdByParser) |
|
47 { |
|
48 return adoptRef(new HTMLScriptElement(tagName, document, createdByParser)); |
|
49 } |
|
50 |
|
51 bool HTMLScriptElement::isURLAttribute(Attribute* attr) const |
|
52 { |
|
53 return attr->name() == srcAttr; |
|
54 } |
|
55 |
|
56 bool HTMLScriptElement::shouldExecuteAsJavaScript() const |
|
57 { |
|
58 return m_data.shouldExecuteAsJavaScript(); |
|
59 } |
|
60 |
|
61 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) |
|
62 { |
|
63 ScriptElement::childrenChanged(m_data); |
|
64 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); |
|
65 } |
|
66 |
|
67 void HTMLScriptElement::parseMappedAttribute(Attribute* attr) |
|
68 { |
|
69 const QualifiedName& attrName = attr->name(); |
|
70 |
|
71 if (attrName == srcAttr) |
|
72 handleSourceAttribute(m_data, attr->value()); |
|
73 else if (attrName == onloadAttr) |
|
74 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr)); |
|
75 else if (attrName == onbeforeloadAttr) |
|
76 setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr)); |
|
77 else if (attrName == onbeforeprocessAttr) |
|
78 setAttributeEventListener(eventNames().beforeprocessEvent, createAttributeEventListener(this, attr)); |
|
79 else |
|
80 HTMLElement::parseMappedAttribute(attr); |
|
81 } |
|
82 |
|
83 void HTMLScriptElement::finishParsingChildren() |
|
84 { |
|
85 ScriptElement::finishParsingChildren(m_data, sourceAttributeValue()); |
|
86 HTMLElement::finishParsingChildren(); |
|
87 } |
|
88 |
|
89 void HTMLScriptElement::insertedIntoDocument() |
|
90 { |
|
91 HTMLElement::insertedIntoDocument(); |
|
92 ScriptElement::insertedIntoDocument(m_data, sourceAttributeValue()); |
|
93 } |
|
94 |
|
95 void HTMLScriptElement::removedFromDocument() |
|
96 { |
|
97 HTMLElement::removedFromDocument(); |
|
98 ScriptElement::removedFromDocument(m_data); |
|
99 } |
|
100 |
|
101 String HTMLScriptElement::text() const |
|
102 { |
|
103 return m_data.scriptContent(); |
|
104 } |
|
105 |
|
106 void HTMLScriptElement::setText(const String &value) |
|
107 { |
|
108 ExceptionCode ec = 0; |
|
109 int numChildren = childNodeCount(); |
|
110 |
|
111 if (numChildren == 1 && firstChild()->isTextNode()) { |
|
112 static_cast<Text*>(firstChild())->setData(value, ec); |
|
113 return; |
|
114 } |
|
115 |
|
116 if (numChildren > 0) |
|
117 removeChildren(); |
|
118 |
|
119 appendChild(document()->createTextNode(value.impl()), ec); |
|
120 } |
|
121 |
|
122 KURL HTMLScriptElement::src() const |
|
123 { |
|
124 return document()->completeURL(sourceAttributeValue()); |
|
125 } |
|
126 |
|
127 String HTMLScriptElement::scriptCharset() const |
|
128 { |
|
129 return m_data.scriptCharset(); |
|
130 } |
|
131 |
|
132 String HTMLScriptElement::scriptContent() const |
|
133 { |
|
134 return m_data.scriptContent(); |
|
135 } |
|
136 |
|
137 void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const |
|
138 { |
|
139 HTMLElement::addSubresourceAttributeURLs(urls); |
|
140 |
|
141 addSubresourceURL(urls, src()); |
|
142 } |
|
143 |
|
144 String HTMLScriptElement::sourceAttributeValue() const |
|
145 { |
|
146 return getAttribute(srcAttr).string(); |
|
147 } |
|
148 |
|
149 String HTMLScriptElement::charsetAttributeValue() const |
|
150 { |
|
151 return getAttribute(charsetAttr).string(); |
|
152 } |
|
153 |
|
154 String HTMLScriptElement::typeAttributeValue() const |
|
155 { |
|
156 return getAttribute(typeAttr).string(); |
|
157 } |
|
158 |
|
159 String HTMLScriptElement::languageAttributeValue() const |
|
160 { |
|
161 return getAttribute(languageAttr).string(); |
|
162 } |
|
163 |
|
164 String HTMLScriptElement::forAttributeValue() const |
|
165 { |
|
166 return getAttribute(forAttr).string(); |
|
167 } |
|
168 |
|
169 String HTMLScriptElement::eventAttributeValue() const |
|
170 { |
|
171 return getAttribute(eventAttr).string(); |
|
172 } |
|
173 |
|
174 bool HTMLScriptElement::asyncAttributeValue() const |
|
175 { |
|
176 return !getAttribute(asyncAttr).isNull(); |
|
177 } |
|
178 |
|
179 bool HTMLScriptElement::deferAttributeValue() const |
|
180 { |
|
181 return !getAttribute(deferAttr).isNull(); |
|
182 } |
|
183 |
|
184 void HTMLScriptElement::dispatchLoadEvent() |
|
185 { |
|
186 ASSERT(!m_data.haveFiredLoadEvent()); |
|
187 m_data.setHaveFiredLoadEvent(true); |
|
188 |
|
189 dispatchEvent(Event::create(eventNames().loadEvent, false, false)); |
|
190 } |
|
191 |
|
192 void HTMLScriptElement::dispatchErrorEvent() |
|
193 { |
|
194 dispatchEvent(Event::create(eventNames().errorEvent, true, false)); |
|
195 } |
|
196 |
|
197 } |