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