initial import
[vuplus_webkit] / Source / WebCore / html / HTMLScriptElement.cpp
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 "ScriptEventListener.h"
32 #include "Text.h"
33
34 namespace WebCore {
35
36 using namespace HTMLNames;
37
38 inline 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
45 PassRefPtr<HTMLScriptElement> HTMLScriptElement::create(const QualifiedName& tagName, Document* document, bool wasInsertedByParser)
46 {
47     return adoptRef(new HTMLScriptElement(tagName, document, wasInsertedByParser, false));
48 }
49
50 bool HTMLScriptElement::isURLAttribute(Attribute* attr) const
51 {
52     return attr->name() == srcAttr;
53 }
54
55 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
56 {
57     ScriptElement::childrenChanged();
58     HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
59 }
60
61 void HTMLScriptElement::attributeChanged(Attribute* attr, bool preserveDecls)
62 {
63     if (attr->name() == asyncAttr)
64         handleAsyncAttribute();
65     HTMLElement::attributeChanged(attr, preserveDecls);
66 }
67
68 void HTMLScriptElement::parseMappedAttribute(Attribute* attr)
69 {
70     const QualifiedName& attrName = attr->name();
71
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));
78     else
79         HTMLElement::parseMappedAttribute(attr);
80 }
81
82 void HTMLScriptElement::insertedIntoDocument()
83 {
84     HTMLElement::insertedIntoDocument();
85     ScriptElement::insertedIntoDocument();
86 }
87
88 void HTMLScriptElement::removedFromDocument()
89 {
90     HTMLElement::removedFromDocument();
91     ScriptElement::removedFromDocument();
92 }
93
94 void HTMLScriptElement::setText(const String &value)
95 {
96     ExceptionCode ec = 0;
97     int numChildren = childNodeCount();
98
99     if (numChildren == 1 && firstChild()->isTextNode()) {
100         static_cast<Text*>(firstChild())->setData(value, ec);
101         return;
102     }
103
104     if (numChildren > 0)
105         removeChildren();
106
107     appendChild(document()->createTextNode(value.impl()), ec);
108 }
109
110 void HTMLScriptElement::setAsync(bool async)
111 {
112     setBooleanAttribute(asyncAttr, async);
113     handleAsyncAttribute();
114 }
115
116 bool HTMLScriptElement::async() const
117 {
118     return fastHasAttribute(asyncAttr) || forceAsync();
119 }
120
121 KURL HTMLScriptElement::src() const
122 {
123     return document()->completeURL(sourceAttributeValue());
124 }
125
126 void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
127 {
128     HTMLElement::addSubresourceAttributeURLs(urls);
129
130     addSubresourceURL(urls, src());
131 }
132
133 String HTMLScriptElement::sourceAttributeValue() const
134 {
135     return getAttribute(srcAttr).string();
136 }
137
138 String HTMLScriptElement::charsetAttributeValue() const
139 {
140     return getAttribute(charsetAttr).string();
141 }
142
143 String HTMLScriptElement::typeAttributeValue() const
144 {
145     return getAttribute(typeAttr).string();
146 }
147
148 String HTMLScriptElement::languageAttributeValue() const
149 {
150     return getAttribute(languageAttr).string();
151 }
152
153 String HTMLScriptElement::forAttributeValue() const
154 {
155     return getAttribute(forAttr).string();
156 }
157
158 String HTMLScriptElement::eventAttributeValue() const
159 {
160     return getAttribute(eventAttr).string();
161 }
162
163 bool HTMLScriptElement::asyncAttributeValue() const
164 {
165     return fastHasAttribute(asyncAttr);
166 }
167
168 bool HTMLScriptElement::deferAttributeValue() const
169 {
170     return fastHasAttribute(deferAttr);
171 }
172
173 bool HTMLScriptElement::hasSourceAttribute() const
174 {
175     return fastHasAttribute(srcAttr);
176 }
177
178 void HTMLScriptElement::dispatchLoadEvent()
179 {
180     ASSERT(!haveFiredLoadEvent());
181     setHaveFiredLoadEvent(true);
182
183     dispatchEvent(Event::create(eventNames().loadEvent, false, false));
184 }
185
186 PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
187 {
188     return adoptRef(new HTMLScriptElement(tagQName(), document(), false, alreadyStarted()));
189 }
190
191 }