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