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.
24 #include "core/html/HTMLScriptElement.h"
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"
38 using namespace HTMLNames;
40 inline HTMLScriptElement::HTMLScriptElement(const QualifiedName& tagName, Document& document, bool wasInsertedByParser, bool alreadyStarted)
41 : HTMLElement(tagName, document)
42 , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
44 ASSERT(hasTagName(scriptTag));
45 ScriptWrappable::init(this);
48 PassRefPtr<HTMLScriptElement> HTMLScriptElement::create(const QualifiedName& tagName, Document& document, bool wasInsertedByParser, bool alreadyStarted)
50 return adoptRef(new HTMLScriptElement(tagName, document, wasInsertedByParser, alreadyStarted));
53 bool HTMLScriptElement::isURLAttribute(const Attribute& attribute) const
55 return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
58 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
60 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
61 m_loader->childrenChanged();
64 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
67 m_loader->handleSourceAttribute(value);
68 else if (name == asyncAttr)
69 m_loader->handleAsyncAttribute();
70 else if (name == onbeforeloadAttr)
71 setAttributeEventListener(EventTypeNames::beforeload, createAttributeEventListener(this, name, value));
73 HTMLElement::parseAttribute(name, value);
76 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
78 HTMLElement::insertedInto(insertionPoint);
79 return InsertionShouldCallDidNotifySubtreeInsertions;
82 void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
84 m_loader->didNotifySubtreeInsertionsToDocument();
87 void HTMLScriptElement::setText(const String &value)
89 RefPtr<Node> protectFromMutationEvents(this);
91 if (hasOneTextChild()) {
92 toText(firstChild())->setData(value);
97 appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
100 void HTMLScriptElement::setAsync(bool async)
102 setBooleanAttribute(asyncAttr, async);
103 m_loader->handleAsyncAttribute();
106 bool HTMLScriptElement::async() const
108 return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
111 KURL HTMLScriptElement::src() const
113 return document().completeURL(sourceAttributeValue());
116 void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
118 HTMLElement::addSubresourceAttributeURLs(urls);
120 addSubresourceURL(urls, src());
123 String HTMLScriptElement::sourceAttributeValue() const
125 return getAttribute(srcAttr).string();
128 String HTMLScriptElement::charsetAttributeValue() const
130 return getAttribute(charsetAttr).string();
133 String HTMLScriptElement::typeAttributeValue() const
135 return getAttribute(typeAttr).string();
138 String HTMLScriptElement::languageAttributeValue() const
140 return getAttribute(languageAttr).string();
143 String HTMLScriptElement::forAttributeValue() const
145 return getAttribute(forAttr).string();
148 String HTMLScriptElement::eventAttributeValue() const
150 return getAttribute(eventAttr).string();
153 bool HTMLScriptElement::asyncAttributeValue() const
155 return fastHasAttribute(asyncAttr);
158 bool HTMLScriptElement::deferAttributeValue() const
160 return fastHasAttribute(deferAttr);
163 bool HTMLScriptElement::hasSourceAttribute() const
165 return fastHasAttribute(srcAttr);
168 void HTMLScriptElement::dispatchLoadEvent()
170 ASSERT(!m_loader->haveFiredLoadEvent());
171 dispatchEvent(Event::create(EventTypeNames::load));
174 PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
176 return adoptRef(new HTMLScriptElement(tagQName(), document(), false, m_loader->alreadyStarted()));