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"
37 using namespace HTMLNames;
39 inline HTMLScriptElement::HTMLScriptElement(Document& document, bool wasInsertedByParser, bool alreadyStarted)
40 : HTMLElement(scriptTag, document)
41 , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
43 ScriptWrappable::init(this);
46 PassRefPtrWillBeRawPtr<HTMLScriptElement> HTMLScriptElement::create(Document& document, bool wasInsertedByParser, bool alreadyStarted)
48 return adoptRefWillBeRefCountedGarbageCollected(new HTMLScriptElement(document, wasInsertedByParser, alreadyStarted));
51 bool HTMLScriptElement::isURLAttribute(const Attribute& attribute) const
53 return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
56 bool HTMLScriptElement::hasLegalLinkAttribute(const QualifiedName& name) const
58 return name == srcAttr || HTMLElement::hasLegalLinkAttribute(name);
61 const QualifiedName& HTMLScriptElement::subResourceAttributeName() const
66 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
68 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
69 m_loader->childrenChanged();
72 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
75 m_loader->handleSourceAttribute(value);
76 else if (name == asyncAttr)
77 m_loader->handleAsyncAttribute();
79 HTMLElement::parseAttribute(name, value);
82 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
84 HTMLElement::insertedInto(insertionPoint);
85 return InsertionShouldCallDidNotifySubtreeInsertions;
88 void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
90 m_loader->didNotifySubtreeInsertionsToDocument();
93 void HTMLScriptElement::setText(const String &value)
95 RefPtr<Node> protectFromMutationEvents(this);
97 if (hasOneTextChild()) {
98 toText(firstChild())->setData(value);
103 appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
106 void HTMLScriptElement::setAsync(bool async)
108 setBooleanAttribute(asyncAttr, async);
109 m_loader->handleAsyncAttribute();
112 bool HTMLScriptElement::async() const
114 return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
117 KURL HTMLScriptElement::src() const
119 return document().completeURL(sourceAttributeValue());
122 String HTMLScriptElement::sourceAttributeValue() const
124 return getAttribute(srcAttr).string();
127 String HTMLScriptElement::charsetAttributeValue() const
129 return getAttribute(charsetAttr).string();
132 String HTMLScriptElement::typeAttributeValue() const
134 return getAttribute(typeAttr).string();
137 String HTMLScriptElement::languageAttributeValue() const
139 return getAttribute(languageAttr).string();
142 String HTMLScriptElement::forAttributeValue() const
144 return getAttribute(forAttr).string();
147 String HTMLScriptElement::eventAttributeValue() const
149 return getAttribute(eventAttr).string();
152 bool HTMLScriptElement::asyncAttributeValue() const
154 return fastHasAttribute(asyncAttr);
157 bool HTMLScriptElement::deferAttributeValue() const
159 return fastHasAttribute(deferAttr);
162 bool HTMLScriptElement::hasSourceAttribute() const
164 return fastHasAttribute(srcAttr);
167 void HTMLScriptElement::dispatchLoadEvent()
169 ASSERT(!m_loader->haveFiredLoadEvent());
170 dispatchEvent(Event::create(EventTypeNames::load));
173 PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
175 return adoptRef(new HTMLScriptElement(document(), false, m_loader->alreadyStarted()));