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 "HTMLNames.h"
27#include "bindings/v8/ExceptionStatePlaceholder.h"
28#include "bindings/v8/ScriptEventListener.h"
29#include "core/dom/Attribute.h"
30#include "core/dom/Document.h"
31#include "core/dom/ScriptLoader.h"
32#include "core/dom/Text.h"
33#include "core/events/Event.h"
34#include "core/events/ThreadLocalEventNames.h"
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40inline HTMLScriptElement::HTMLScriptElement(Document& document, bool wasInsertedByParser, bool alreadyStarted)
41    : HTMLElement(scriptTag, document)
42    , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
43{
44    ScriptWrappable::init(this);
45}
46
47PassRefPtr<HTMLScriptElement> HTMLScriptElement::create(Document& document, bool wasInsertedByParser, bool alreadyStarted)
48{
49    return adoptRef(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
57void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
58{
59    HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
60    m_loader->childrenChanged();
61}
62
63void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
64{
65    if (name == srcAttr)
66        m_loader->handleSourceAttribute(value);
67    else if (name == asyncAttr)
68        m_loader->handleAsyncAttribute();
69    else if (name == onbeforeloadAttr)
70        setAttributeEventListener(EventTypeNames::beforeload, createAttributeEventListener(this, name, value));
71    else
72        HTMLElement::parseAttribute(name, value);
73}
74
75Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
76{
77    HTMLElement::insertedInto(insertionPoint);
78    return InsertionShouldCallDidNotifySubtreeInsertions;
79}
80
81void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
82{
83    m_loader->didNotifySubtreeInsertionsToDocument();
84}
85
86void HTMLScriptElement::setText(const String &value)
87{
88    RefPtr<Node> protectFromMutationEvents(this);
89
90    if (hasOneTextChild()) {
91        toText(firstChild())->setData(value);
92        return;
93    }
94
95    removeChildren();
96    appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
97}
98
99void HTMLScriptElement::setAsync(bool async)
100{
101    setBooleanAttribute(asyncAttr, async);
102    m_loader->handleAsyncAttribute();
103}
104
105bool HTMLScriptElement::async() const
106{
107    return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
108}
109
110KURL HTMLScriptElement::src() const
111{
112    return document().completeURL(sourceAttributeValue());
113}
114
115void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
116{
117    HTMLElement::addSubresourceAttributeURLs(urls);
118
119    addSubresourceURL(urls, src());
120}
121
122String HTMLScriptElement::sourceAttributeValue() const
123{
124    return getAttribute(srcAttr).string();
125}
126
127String HTMLScriptElement::charsetAttributeValue() const
128{
129    return getAttribute(charsetAttr).string();
130}
131
132String HTMLScriptElement::typeAttributeValue() const
133{
134    return getAttribute(typeAttr).string();
135}
136
137String HTMLScriptElement::languageAttributeValue() const
138{
139    return getAttribute(languageAttr).string();
140}
141
142String HTMLScriptElement::forAttributeValue() const
143{
144    return getAttribute(forAttr).string();
145}
146
147String HTMLScriptElement::eventAttributeValue() const
148{
149    return getAttribute(eventAttr).string();
150}
151
152bool HTMLScriptElement::asyncAttributeValue() const
153{
154    return fastHasAttribute(asyncAttr);
155}
156
157bool HTMLScriptElement::deferAttributeValue() const
158{
159    return fastHasAttribute(deferAttr);
160}
161
162bool HTMLScriptElement::hasSourceAttribute() const
163{
164    return fastHasAttribute(srcAttr);
165}
166
167void HTMLScriptElement::dispatchLoadEvent()
168{
169    ASSERT(!m_loader->haveFiredLoadEvent());
170    dispatchEvent(Event::create(EventTypeNames::load));
171}
172
173PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
174{
175    return adoptRef(new HTMLScriptElement(document(), false, m_loader->alreadyStarted()));
176}
177
178}
179