Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / HTMLScriptElement.cpp
blob6bf842423a6bfa43d0bb4ef84448b90090364334
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.
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.
23 #include "config.h"
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"
38 namespace blink {
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
65 return srcAttr;
68 void HTMLScriptElement::childrenChanged(const ChildrenChange& change)
70 HTMLElement::childrenChanged(change);
71 m_loader->childrenChanged();
74 void HTMLScriptElement::didMoveToNewDocument(Document& oldDocument)
76 ScriptRunner::movePendingAsyncScript(oldDocument, document(), m_loader.get());
77 HTMLElement::didMoveToNewDocument(oldDocument);
80 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
82 if (name == srcAttr)
83 m_loader->handleSourceAttribute(value);
84 else if (name == asyncAttr)
85 m_loader->handleAsyncAttribute();
86 else
87 HTMLElement::parseAttribute(name, value);
90 void HTMLScriptElement::attributeWillChange(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue)
92 if (name == srcAttr && inDocument()) {
93 V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
94 if (activityLogger) {
95 Vector<String> argv;
96 argv.append("script");
97 argv.append(srcAttr.toString());
98 argv.append(oldValue);
99 argv.append(newValue);
100 activityLogger->logEvent("blinkSetAttribute", argv.size(), argv.data());
103 HTMLElement::attributeWillChange(name, oldValue, newValue);
106 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
108 if (insertionPoint->inDocument()) {
109 V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
110 if (activityLogger) {
111 Vector<String> argv;
112 argv.append("script");
113 argv.append(fastGetAttribute(srcAttr));
114 activityLogger->logEvent("blinkAddElement", argv.size(), argv.data());
117 if (hasSourceAttribute() && !loader()->isScriptTypeSupported(ScriptLoader::DisallowLegacyTypeInTypeAttribute))
118 UseCounter::count(document(), UseCounter::ScriptElementWithInvalidTypeHasSrc);
120 HTMLElement::insertedInto(insertionPoint);
121 return InsertionShouldCallDidNotifySubtreeInsertions;
124 void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
126 m_loader->didNotifySubtreeInsertionsToDocument();
129 void HTMLScriptElement::setText(const String &value)
131 setTextContent(value);
134 void HTMLScriptElement::setAsync(bool async)
136 setBooleanAttribute(asyncAttr, async);
137 m_loader->handleAsyncAttribute();
140 bool HTMLScriptElement::async() const
142 return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
145 KURL HTMLScriptElement::src() const
147 return document().completeURL(sourceAttributeValue());
150 String HTMLScriptElement::sourceAttributeValue() const
152 return getAttribute(srcAttr).string();
155 String HTMLScriptElement::charsetAttributeValue() const
157 return getAttribute(charsetAttr).string();
160 String HTMLScriptElement::typeAttributeValue() const
162 return getAttribute(typeAttr).string();
165 String HTMLScriptElement::languageAttributeValue() const
167 return getAttribute(languageAttr).string();
170 String HTMLScriptElement::forAttributeValue() const
172 return getAttribute(forAttr).string();
175 String HTMLScriptElement::eventAttributeValue() const
177 return getAttribute(eventAttr).string();
180 bool HTMLScriptElement::asyncAttributeValue() const
182 return fastHasAttribute(asyncAttr);
185 bool HTMLScriptElement::deferAttributeValue() const
187 return fastHasAttribute(deferAttr);
190 bool HTMLScriptElement::hasSourceAttribute() const
192 return fastHasAttribute(srcAttr);
195 void HTMLScriptElement::dispatchLoadEvent()
197 ASSERT(!m_loader->haveFiredLoadEvent());
198 dispatchEvent(Event::create(EventTypeNames::load));
201 PassRefPtrWillBeRawPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
203 return adoptRefWillBeNoop(new HTMLScriptElement(document(), false, m_loader->alreadyStarted()));
206 DEFINE_TRACE(HTMLScriptElement)
208 visitor->trace(m_loader);
209 HTMLElement::trace(visitor);