Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLScriptElement.cpp
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 "core/html/HTMLScriptElement.h"
25
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"
37
38 namespace blink {
39
40 using namespace HTMLNames;
41
42 inline HTMLScriptElement::HTMLScriptElement(Document& document, bool wasInsertedByParser, bool alreadyStarted)
43     : HTMLElement(scriptTag, document)
44     , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
45 {
46 }
47
48 PassRefPtrWillBeRawPtr<HTMLScriptElement> HTMLScriptElement::create(Document& document, bool wasInsertedByParser, bool alreadyStarted)
49 {
50     return adoptRefWillBeNoop(new HTMLScriptElement(document, wasInsertedByParser, alreadyStarted));
51 }
52
53 bool HTMLScriptElement::isURLAttribute(const Attribute& attribute) const
54 {
55     return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
56 }
57
58 bool HTMLScriptElement::hasLegalLinkAttribute(const QualifiedName& name) const
59 {
60     return name == srcAttr || HTMLElement::hasLegalLinkAttribute(name);
61 }
62
63 const QualifiedName& HTMLScriptElement::subResourceAttributeName() const
64 {
65     return srcAttr;
66 }
67
68 void HTMLScriptElement::childrenChanged(const ChildrenChange& change)
69 {
70     HTMLElement::childrenChanged(change);
71     m_loader->childrenChanged();
72 }
73
74 void HTMLScriptElement::didMoveToNewDocument(Document& oldDocument)
75 {
76     if (RefPtrWillBeRawPtr<Document> contextDocument = document().contextDocument().get())
77         oldDocument.scriptRunner()->movePendingAsyncScript(contextDocument->scriptRunner(), m_loader.get());
78     HTMLElement::didMoveToNewDocument(oldDocument);
79 }
80
81 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
82 {
83     if (name == srcAttr)
84         m_loader->handleSourceAttribute(value);
85     else if (name == asyncAttr)
86         m_loader->handleAsyncAttribute();
87     else
88         HTMLElement::parseAttribute(name, value);
89 }
90
91 void HTMLScriptElement::attributeWillChange(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue)
92 {
93     if (name == srcAttr && inDocument()) {
94         V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
95         if (activityLogger) {
96             Vector<String> argv;
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());
102         }
103     }
104     HTMLElement::attributeWillChange(name, oldValue, newValue);
105 }
106
107 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
108 {
109     if (insertionPoint->inDocument()) {
110         V8DOMActivityLogger* activityLogger = V8DOMActivityLogger::currentActivityLoggerIfIsolatedWorld();
111         if (activityLogger) {
112             Vector<String> argv;
113             argv.append("script");
114             argv.append(fastGetAttribute(srcAttr));
115             activityLogger->logEvent("blinkAddElement", argv.size(), argv.data());
116         }
117
118         if (hasSourceAttribute() && !loader()->isScriptTypeSupported(ScriptLoader::DisallowLegacyTypeInTypeAttribute))
119             UseCounter::count(document(), UseCounter::ScriptElementWithInvalidTypeHasSrc);
120     }
121     HTMLElement::insertedInto(insertionPoint);
122     return InsertionShouldCallDidNotifySubtreeInsertions;
123 }
124
125 void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
126 {
127     m_loader->didNotifySubtreeInsertionsToDocument();
128 }
129
130 void HTMLScriptElement::setText(const String &value)
131 {
132     RefPtrWillBeRawPtr<Node> protectFromMutationEvents(this);
133
134     if (hasOneTextChild()) {
135         toText(firstChild())->setData(value);
136         return;
137     }
138
139     removeChildren();
140     appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
141 }
142
143 void HTMLScriptElement::setAsync(bool async)
144 {
145     setBooleanAttribute(asyncAttr, async);
146     m_loader->handleAsyncAttribute();
147 }
148
149 bool HTMLScriptElement::async() const
150 {
151     return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
152 }
153
154 KURL HTMLScriptElement::src() const
155 {
156     return document().completeURL(sourceAttributeValue());
157 }
158
159 String HTMLScriptElement::sourceAttributeValue() const
160 {
161     return getAttribute(srcAttr).string();
162 }
163
164 String HTMLScriptElement::charsetAttributeValue() const
165 {
166     return getAttribute(charsetAttr).string();
167 }
168
169 String HTMLScriptElement::typeAttributeValue() const
170 {
171     return getAttribute(typeAttr).string();
172 }
173
174 String HTMLScriptElement::languageAttributeValue() const
175 {
176     return getAttribute(languageAttr).string();
177 }
178
179 String HTMLScriptElement::forAttributeValue() const
180 {
181     return getAttribute(forAttr).string();
182 }
183
184 String HTMLScriptElement::eventAttributeValue() const
185 {
186     return getAttribute(eventAttr).string();
187 }
188
189 bool HTMLScriptElement::asyncAttributeValue() const
190 {
191     return fastHasAttribute(asyncAttr);
192 }
193
194 bool HTMLScriptElement::deferAttributeValue() const
195 {
196     return fastHasAttribute(deferAttr);
197 }
198
199 bool HTMLScriptElement::hasSourceAttribute() const
200 {
201     return fastHasAttribute(srcAttr);
202 }
203
204 void HTMLScriptElement::dispatchLoadEvent()
205 {
206     ASSERT(!m_loader->haveFiredLoadEvent());
207     dispatchEvent(Event::create(EventTypeNames::load));
208 }
209
210 PassRefPtrWillBeRawPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
211 {
212     return adoptRefWillBeNoop(new HTMLScriptElement(document(), false, m_loader->alreadyStarted()));
213 }
214
215 void HTMLScriptElement::trace(Visitor* visitor)
216 {
217     visitor->trace(m_loader);
218     HTMLElement::trace(visitor);
219 }
220
221 }