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 "HTMLScriptElement.h"
26 #include "Attribute.h"
29 #include "EventNames.h"
30 #include "HTMLNames.h"
31 #include "ScriptEventListener.h"
36 using namespace HTMLNames;
38 inline HTMLScriptElement::HTMLScriptElement(const QualifiedName& tagName, Document* document, bool wasInsertedByParser, bool alreadyStarted)
39 : HTMLElement(tagName, document)
40 , ScriptElement(this, wasInsertedByParser, alreadyStarted)
42 ASSERT(hasTagName(scriptTag));
45 PassRefPtr<HTMLScriptElement> HTMLScriptElement::create(const QualifiedName& tagName, Document* document, bool wasInsertedByParser)
47 return adoptRef(new HTMLScriptElement(tagName, document, wasInsertedByParser, false));
50 bool HTMLScriptElement::isURLAttribute(Attribute* attr) const
52 return attr->name() == srcAttr;
55 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
57 ScriptElement::childrenChanged();
58 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
61 void HTMLScriptElement::attributeChanged(Attribute* attr, bool preserveDecls)
63 if (attr->name() == asyncAttr)
64 handleAsyncAttribute();
65 HTMLElement::attributeChanged(attr, preserveDecls);
68 void HTMLScriptElement::parseMappedAttribute(Attribute* attr)
70 const QualifiedName& attrName = attr->name();
72 if (attrName == srcAttr)
73 handleSourceAttribute(attr->value());
74 else if (attrName == onloadAttr)
75 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attr));
76 else if (attrName == onbeforeloadAttr)
77 setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr));
79 HTMLElement::parseMappedAttribute(attr);
82 void HTMLScriptElement::insertedIntoDocument()
84 HTMLElement::insertedIntoDocument();
85 ScriptElement::insertedIntoDocument();
88 void HTMLScriptElement::removedFromDocument()
90 HTMLElement::removedFromDocument();
91 ScriptElement::removedFromDocument();
94 void HTMLScriptElement::setText(const String &value)
97 int numChildren = childNodeCount();
99 if (numChildren == 1 && firstChild()->isTextNode()) {
100 static_cast<Text*>(firstChild())->setData(value, ec);
107 appendChild(document()->createTextNode(value.impl()), ec);
110 void HTMLScriptElement::setAsync(bool async)
112 setBooleanAttribute(asyncAttr, async);
113 handleAsyncAttribute();
116 bool HTMLScriptElement::async() const
118 return fastHasAttribute(asyncAttr) || forceAsync();
121 KURL HTMLScriptElement::src() const
123 return document()->completeURL(sourceAttributeValue());
126 void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
128 HTMLElement::addSubresourceAttributeURLs(urls);
130 addSubresourceURL(urls, src());
133 String HTMLScriptElement::sourceAttributeValue() const
135 return getAttribute(srcAttr).string();
138 String HTMLScriptElement::charsetAttributeValue() const
140 return getAttribute(charsetAttr).string();
143 String HTMLScriptElement::typeAttributeValue() const
145 return getAttribute(typeAttr).string();
148 String HTMLScriptElement::languageAttributeValue() const
150 return getAttribute(languageAttr).string();
153 String HTMLScriptElement::forAttributeValue() const
155 return getAttribute(forAttr).string();
158 String HTMLScriptElement::eventAttributeValue() const
160 return getAttribute(eventAttr).string();
163 bool HTMLScriptElement::asyncAttributeValue() const
165 return fastHasAttribute(asyncAttr);
168 bool HTMLScriptElement::deferAttributeValue() const
170 return fastHasAttribute(deferAttr);
173 bool HTMLScriptElement::hasSourceAttribute() const
175 return fastHasAttribute(srcAttr);
178 void HTMLScriptElement::dispatchLoadEvent()
180 ASSERT(!haveFiredLoadEvent());
181 setHaveFiredLoadEvent(true);
183 dispatchEvent(Event::create(eventNames().loadEvent, false, false));
186 PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
188 return adoptRef(new HTMLScriptElement(tagQName(), document(), false, alreadyStarted()));