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.
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.
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.
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.
24 #include "core/html/HTMLScriptElement.h"
26 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
27 #include "bindings/core/v8/ScriptEventListener.h"
28 #include "bindings/core/v8/V8DOMActivityLogger.h"
29 #include "core/HTMLNames.h"
30 #include "core/dom/Attribute.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/ScriptLoader.h"
33 #include "core/dom/ScriptRunner.h"
34 #include "core/dom/Text.h"
35 #include "core/events/Event.h"
36 #include "core/frame/UseCounter.h"
40 using namespace HTMLNames;
42 inline HTMLScriptElement::HTMLScriptElement(Document& document, bool wasInsertedByParser, bool alreadyStarted)
43 : HTMLElement(scriptTag, document)
44 , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
48 PassRefPtrWillBeRawPtr<HTMLScriptElement> HTMLScriptElement::create(Document& document, bool wasInsertedByParser, bool alreadyStarted)
50 return adoptRefWillBeNoop(new HTMLScriptElement(document, wasInsertedByParser, alreadyStarted));
53 bool HTMLScriptElement::isURLAttribute(const Attribute& attribute) const
55 return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
58 bool HTMLScriptElement::hasLegalLinkAttribute(const QualifiedName& name) const
60 return name == srcAttr || HTMLElement::hasLegalLinkAttribute(name);
63 const QualifiedName& HTMLScriptElement::subResourceAttributeName() const
68 void HTMLScriptElement::childrenChanged(const ChildrenChange& change)
70 HTMLElement::childrenChanged(change);
71 m_loader->childrenChanged();
74 void HTMLScriptElement::didMoveToNewDocument(Document& oldDocument)
76 if (RefPtrWillBeRawPtr<Document> contextDocument = document().contextDocument().get())
77 oldDocument.scriptRunner()->movePendingAsyncScript(contextDocument->scriptRunner(), m_loader.get());
78 HTMLElement::didMoveToNewDocument(oldDocument);
81 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
84 m_loader->handleSourceAttribute(value);
85 else if (name == asyncAttr)
86 m_loader->handleAsyncAttribute();
88 HTMLElement::parseAttribute(name, value);
91 void HTMLScriptElement::attributeWillChange(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue)
93 if (name == srcAttr && inDocument()) {
94 V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
97 argv.append("script");
98 argv.append(srcAttr.toString());
99 argv.append(oldValue);
100 argv.append(newValue);
101 activityLogger->logEvent("blinkSetAttribute", argv.size(), argv.data());
104 HTMLElement::attributeWillChange(name, oldValue, newValue);
107 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
109 if (insertionPoint->inDocument()) {
110 V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
111 if (activityLogger) {
113 argv.append("script");
114 argv.append(fastGetAttribute(srcAttr));
115 activityLogger->logEvent("blinkAddElement", argv.size(), argv.data());
118 if (hasSourceAttribute() && !loader()->isScriptTypeSupported(ScriptLoader::DisallowLegacyTypeInTypeAttribute))
119 UseCounter::count(document(), UseCounter::ScriptElementWithInvalidTypeHasSrc);
121 HTMLElement::insertedInto(insertionPoint);
122 return InsertionShouldCallDidNotifySubtreeInsertions;
125 void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
127 m_loader->didNotifySubtreeInsertionsToDocument();
130 void HTMLScriptElement::setText(const String &value)
132 RefPtrWillBeRawPtr<Node> protectFromMutationEvents(this);
134 if (hasOneTextChild()) {
135 toText(firstChild())->setData(value);
140 appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
143 void HTMLScriptElement::setAsync(bool async)
145 setBooleanAttribute(asyncAttr, async);
146 m_loader->handleAsyncAttribute();
149 bool HTMLScriptElement::async() const
151 return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
154 KURL HTMLScriptElement::src() const
156 return document().completeURL(sourceAttributeValue());
159 String HTMLScriptElement::sourceAttributeValue() const
161 return getAttribute(srcAttr).string();
164 String HTMLScriptElement::charsetAttributeValue() const
166 return getAttribute(charsetAttr).string();
169 String HTMLScriptElement::typeAttributeValue() const
171 return getAttribute(typeAttr).string();
174 String HTMLScriptElement::languageAttributeValue() const
176 return getAttribute(languageAttr).string();
179 String HTMLScriptElement::forAttributeValue() const
181 return getAttribute(forAttr).string();
184 String HTMLScriptElement::eventAttributeValue() const
186 return getAttribute(eventAttr).string();
189 bool HTMLScriptElement::asyncAttributeValue() const
191 return fastHasAttribute(asyncAttr);
194 bool HTMLScriptElement::deferAttributeValue() const
196 return fastHasAttribute(deferAttr);
199 bool HTMLScriptElement::hasSourceAttribute() const
201 return fastHasAttribute(srcAttr);
204 void HTMLScriptElement::dispatchLoadEvent()
206 ASSERT(!m_loader->haveFiredLoadEvent());
207 dispatchEvent(Event::create(EventTypeNames::load));
210 PassRefPtrWillBeRawPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
212 return adoptRefWillBeNoop(new HTMLScriptElement(document(), false, m_loader->alreadyStarted()));
215 void HTMLScriptElement::trace(Visitor* visitor)
217 visitor->trace(m_loader);
218 HTMLElement::trace(visitor);