darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 1 | /* |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 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) |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 21 | */ |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 22 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 23 | #include "config.h" |
| 24 | #include "HTMLScriptElement.h" |
| 25 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 26 | #include "Document.h" |
| 27 | #include "EventNames.h" |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 28 | #include "HTMLNames.h" |
darin@apple.com | 9a925fa | 2009-05-04 18:00:34 +0000 | [diff] [blame] | 29 | #include "MappedAttribute.h" |
dglazkov@chromium.org | 26789aa | 2009-04-24 22:00:44 +0000 | [diff] [blame] | 30 | #include "ScriptEventListener.h" |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 31 | #include "Text.h" |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | using namespace HTMLNames; |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 36 | |
jchaffraix@webkit.org | 4cc683b | 2008-12-09 00:06:41 +0000 | [diff] [blame] | 37 | HTMLScriptElement::HTMLScriptElement(const QualifiedName& tagName, Document* doc, bool createdByParser) |
jchaffraix@webkit.org | 94d95b0 | 2008-12-04 22:39:05 +0000 | [diff] [blame] | 38 | : HTMLElement(tagName, doc) |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 39 | , m_data(this, this) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 40 | { |
jchaffraix@webkit.org | 94d95b0 | 2008-12-04 22:39:05 +0000 | [diff] [blame] | 41 | ASSERT(hasTagName(scriptTag)); |
jchaffraix@webkit.org | 4cc683b | 2008-12-09 00:06:41 +0000 | [diff] [blame] | 42 | m_data.setCreatedByParser(createdByParser); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | HTMLScriptElement::~HTMLScriptElement() |
| 46 | { |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 49 | bool HTMLScriptElement::isURLAttribute(Attribute* attr) const |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 50 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 51 | return attr->name() == sourceAttributeValue(); |
| 52 | } |
| 53 | |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 54 | bool HTMLScriptElement::shouldExecuteAsJavaScript() const |
| 55 | { |
| 56 | return m_data.shouldExecuteAsJavaScript(); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
hyatt@apple.com | 86b942b | 2008-02-27 23:13:51 +0000 | [diff] [blame] | 59 | void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 60 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 61 | ScriptElement::childrenChanged(m_data); |
hyatt@apple.com | 86b942b | 2008-02-27 23:13:51 +0000 | [diff] [blame] | 62 | HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 65 | void HTMLScriptElement::parseMappedAttribute(MappedAttribute* attr) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 66 | { |
| 67 | const QualifiedName& attrName = attr->name(); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 68 | |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 69 | if (attrName == srcAttr) |
| 70 | handleSourceAttribute(m_data, attr->value()); |
| 71 | else if (attrName == onloadAttr) |
ggaren@apple.com | 181565c6 | 2009-04-21 05:58:59 +0000 | [diff] [blame] | 72 | setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr)); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 73 | else |
| 74 | HTMLElement::parseMappedAttribute(attr); |
| 75 | } |
| 76 | |
hyatt@apple.com | 1c531c9 | 2008-02-09 22:49:13 +0000 | [diff] [blame] | 77 | void HTMLScriptElement::finishParsingChildren() |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 78 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 79 | ScriptElement::finishParsingChildren(m_data, sourceAttributeValue()); |
hyatt@apple.com | 1c531c9 | 2008-02-09 22:49:13 +0000 | [diff] [blame] | 80 | HTMLElement::finishParsingChildren(); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void HTMLScriptElement::insertedIntoDocument() |
| 84 | { |
| 85 | HTMLElement::insertedIntoDocument(); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 86 | ScriptElement::insertedIntoDocument(m_data, sourceAttributeValue()); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void HTMLScriptElement::removedFromDocument() |
| 90 | { |
| 91 | HTMLElement::removedFromDocument(); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 92 | ScriptElement::removedFromDocument(m_data); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | String HTMLScriptElement::text() const |
| 96 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 97 | return m_data.scriptContent(); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void HTMLScriptElement::setText(const String &value) |
| 101 | { |
| 102 | ExceptionCode ec = 0; |
| 103 | int numChildren = childNodeCount(); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 104 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 105 | if (numChildren == 1 && firstChild()->isTextNode()) { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 106 | static_cast<Text*>(firstChild())->setData(value, ec); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 107 | return; |
| 108 | } |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 109 | |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 110 | if (numChildren > 0) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 111 | removeChildren(); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 112 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 113 | appendChild(document()->createTextNode(value.impl()), ec); |
| 114 | } |
| 115 | |
| 116 | String HTMLScriptElement::htmlFor() const |
| 117 | { |
| 118 | // DOM Level 1 says: reserved for future use. |
| 119 | return String(); |
| 120 | } |
| 121 | |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 122 | void HTMLScriptElement::setHtmlFor(const String&) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 123 | { |
| 124 | // DOM Level 1 says: reserved for future use. |
| 125 | } |
| 126 | |
| 127 | String HTMLScriptElement::event() const |
| 128 | { |
| 129 | // DOM Level 1 says: reserved for future use. |
| 130 | return String(); |
| 131 | } |
| 132 | |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 133 | void HTMLScriptElement::setEvent(const String&) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 134 | { |
| 135 | // DOM Level 1 says: reserved for future use. |
| 136 | } |
| 137 | |
| 138 | String HTMLScriptElement::charset() const |
| 139 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 140 | return charsetAttributeValue(); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void HTMLScriptElement::setCharset(const String &value) |
| 144 | { |
| 145 | setAttribute(charsetAttr, value); |
| 146 | } |
| 147 | |
| 148 | bool HTMLScriptElement::defer() const |
| 149 | { |
| 150 | return !getAttribute(deferAttr).isNull(); |
| 151 | } |
| 152 | |
| 153 | void HTMLScriptElement::setDefer(bool defer) |
| 154 | { |
| 155 | setAttribute(deferAttr, defer ? "" : 0); |
| 156 | } |
| 157 | |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 158 | KURL HTMLScriptElement::src() const |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 159 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 160 | return document()->completeURL(sourceAttributeValue()); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void HTMLScriptElement::setSrc(const String &value) |
| 164 | { |
| 165 | setAttribute(srcAttr, value); |
| 166 | } |
| 167 | |
| 168 | String HTMLScriptElement::type() const |
| 169 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 170 | return typeAttributeValue(); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void HTMLScriptElement::setType(const String &value) |
| 174 | { |
| 175 | setAttribute(typeAttr, value); |
| 176 | } |
| 177 | |
ap@webkit.org | 16a7aeb | 2008-03-10 15:38:04 +0000 | [diff] [blame] | 178 | String HTMLScriptElement::scriptCharset() const |
| 179 | { |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 180 | return m_data.scriptCharset(); |
| 181 | } |
| 182 | |
| 183 | String HTMLScriptElement::scriptContent() const |
| 184 | { |
| 185 | return m_data.scriptContent(); |
ap@webkit.org | 16a7aeb | 2008-03-10 15:38:04 +0000 | [diff] [blame] | 186 | } |
| 187 | |
ddkilzer@apple.com | b27ae03 | 2008-12-06 11:54:46 +0000 | [diff] [blame] | 188 | void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const |
beidson@apple.com | a4fb38f | 2008-03-27 04:08:17 +0000 | [diff] [blame] | 189 | { |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 190 | HTMLElement::addSubresourceAttributeURLs(urls); |
| 191 | |
ddkilzer@apple.com | b27ae03 | 2008-12-06 11:54:46 +0000 | [diff] [blame] | 192 | addSubresourceURL(urls, src()); |
beidson@apple.com | a4fb38f | 2008-03-27 04:08:17 +0000 | [diff] [blame] | 193 | } |
| 194 | |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 195 | String HTMLScriptElement::sourceAttributeValue() const |
| 196 | { |
| 197 | return getAttribute(srcAttr).string(); |
| 198 | } |
| 199 | |
| 200 | String HTMLScriptElement::charsetAttributeValue() const |
| 201 | { |
weinig@apple.com | 064861a | 2008-09-14 22:12:49 +0000 | [diff] [blame] | 202 | return getAttribute(charsetAttr).string(); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | String HTMLScriptElement::typeAttributeValue() const |
| 206 | { |
weinig@apple.com | 064861a | 2008-09-14 22:12:49 +0000 | [diff] [blame] | 207 | return getAttribute(typeAttr).string(); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | String HTMLScriptElement::languageAttributeValue() const |
| 211 | { |
| 212 | return getAttribute(languageAttr).string(); |
| 213 | } |
beidson@apple.com | dc156cd | 2009-04-30 16:20:49 +0000 | [diff] [blame] | 214 | |
| 215 | String HTMLScriptElement::forAttributeValue() const |
| 216 | { |
| 217 | return getAttribute(forAttr).string(); |
| 218 | } |
zimmermann@webkit.org | 8cc8ad0 | 2008-09-16 00:01:11 +0000 | [diff] [blame] | 219 | |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 220 | void HTMLScriptElement::dispatchLoadEvent() |
| 221 | { |
zimmermann@webkit.org | 50e33c7 | 2008-09-20 03:33:43 +0000 | [diff] [blame] | 222 | ASSERT(!m_data.haveFiredLoadEvent()); |
| 223 | m_data.setHaveFiredLoadEvent(true); |
| 224 | |
ggaren@apple.com | 15903b1 | 2009-04-18 01:31:50 +0000 | [diff] [blame] | 225 | dispatchEvent(eventNames().loadEvent, false, false); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void HTMLScriptElement::dispatchErrorEvent() |
| 229 | { |
ggaren@apple.com | 15903b1 | 2009-04-18 01:31:50 +0000 | [diff] [blame] | 230 | dispatchEvent(eventNames().errorEvent, true, false); |
zimmermann@webkit.org | 9ae47e9 | 2008-08-14 02:22:35 +0000 | [diff] [blame] | 231 | } |
| 232 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 233 | } |