Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/html/HTMLScriptElement.h"
25
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
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 inline HTMLScriptElement::HTMLScriptElement(Document& document, bool wasInsertedByParser, bool alreadyStarted)
40     : HTMLElement(scriptTag, document)
41     , m_loader(ScriptLoader::create(this, wasInsertedByParser, alreadyStarted))
42 {
43     ScriptWrappable::init(this);
44 }
45
46 PassRefPtrWillBeRawPtr<HTMLScriptElement> HTMLScriptElement::create(Document& document, bool wasInsertedByParser, bool alreadyStarted)
47 {
48     return adoptRefWillBeRefCountedGarbageCollected(new HTMLScriptElement(document, wasInsertedByParser, alreadyStarted));
49 }
50
51 bool HTMLScriptElement::isURLAttribute(const Attribute& attribute) const
52 {
53     return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
54 }
55
56 bool HTMLScriptElement::hasLegalLinkAttribute(const QualifiedName& name) const
57 {
58     return name == srcAttr || HTMLElement::hasLegalLinkAttribute(name);
59 }
60
61 const QualifiedName& HTMLScriptElement::subResourceAttributeName() const
62 {
63     return srcAttr;
64 }
65
66 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
67 {
68     HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
69     m_loader->childrenChanged();
70 }
71
72 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
73 {
74     if (name == srcAttr)
75         m_loader->handleSourceAttribute(value);
76     else if (name == asyncAttr)
77         m_loader->handleAsyncAttribute();
78     else
79         HTMLElement::parseAttribute(name, value);
80 }
81
82 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint)
83 {
84     HTMLElement::insertedInto(insertionPoint);
85     return InsertionShouldCallDidNotifySubtreeInsertions;
86 }
87
88 void HTMLScriptElement::didNotifySubtreeInsertionsToDocument()
89 {
90     m_loader->didNotifySubtreeInsertionsToDocument();
91 }
92
93 void HTMLScriptElement::setText(const String &value)
94 {
95     RefPtr<Node> protectFromMutationEvents(this);
96
97     if (hasOneTextChild()) {
98         toText(firstChild())->setData(value);
99         return;
100     }
101
102     removeChildren();
103     appendChild(document().createTextNode(value.impl()), IGNORE_EXCEPTION);
104 }
105
106 void HTMLScriptElement::setAsync(bool async)
107 {
108     setBooleanAttribute(asyncAttr, async);
109     m_loader->handleAsyncAttribute();
110 }
111
112 bool HTMLScriptElement::async() const
113 {
114     return fastHasAttribute(asyncAttr) || (m_loader->forceAsync());
115 }
116
117 KURL HTMLScriptElement::src() const
118 {
119     return document().completeURL(sourceAttributeValue());
120 }
121
122 String HTMLScriptElement::sourceAttributeValue() const
123 {
124     return getAttribute(srcAttr).string();
125 }
126
127 String HTMLScriptElement::charsetAttributeValue() const
128 {
129     return getAttribute(charsetAttr).string();
130 }
131
132 String HTMLScriptElement::typeAttributeValue() const
133 {
134     return getAttribute(typeAttr).string();
135 }
136
137 String HTMLScriptElement::languageAttributeValue() const
138 {
139     return getAttribute(languageAttr).string();
140 }
141
142 String HTMLScriptElement::forAttributeValue() const
143 {
144     return getAttribute(forAttr).string();
145 }
146
147 String HTMLScriptElement::eventAttributeValue() const
148 {
149     return getAttribute(eventAttr).string();
150 }
151
152 bool HTMLScriptElement::asyncAttributeValue() const
153 {
154     return fastHasAttribute(asyncAttr);
155 }
156
157 bool HTMLScriptElement::deferAttributeValue() const
158 {
159     return fastHasAttribute(deferAttr);
160 }
161
162 bool HTMLScriptElement::hasSourceAttribute() const
163 {
164     return fastHasAttribute(srcAttr);
165 }
166
167 void HTMLScriptElement::dispatchLoadEvent()
168 {
169     ASSERT(!m_loader->haveFiredLoadEvent());
170     dispatchEvent(Event::create(EventTypeNames::load));
171 }
172
173 PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren()
174 {
175     return adoptRef(new HTMLScriptElement(document(), false, m_loader->alreadyStarted()));
176 }
177
178 }