1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) 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/Event.h" 32 #include "core/dom/EventNames.h" 33 #include "core/dom/ScriptLoader.h" 34 #include "core/dom/Text.h" 35 36 namespace WebCore { 37 38 using namespace HTMLNames; 39 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)) 43 { 44 ASSERT(hasTagName(scriptTag)); 45 ScriptWrappable::init(this); 46 } 47 48 PassRefPtr<HTMLScriptElement> HTMLScriptElement::create(const QualifiedName& tagName, Document* document, bool wasInsertedByParser, bool alreadyStarted) 49 { 50 return adoptRef(new HTMLScriptElement(tagName, document, wasInsertedByParser, alreadyStarted)); 51 } 52 53 bool HTMLScriptElement::isURLAttribute(const Attribute& attribute) const 54 { 55 return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute); 56 } 57 58 void HTMLScriptElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 59 { 60 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 61 m_loader->childrenChanged(); 62 } 63 64 void HTMLScriptElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 65 { 66 if (name == srcAttr) 67 m_loader->handleSourceAttribute(value); 68 else if (name == asyncAttr) 69 m_loader->handleAsyncAttribute(); 70 else if (name == onbeforeloadAttr) 71 setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, name, value)); 72 else 73 HTMLElement::parseAttribute(name, value); 74 } 75 76 Node::InsertionNotificationRequest HTMLScriptElement::insertedInto(ContainerNode* insertionPoint) 77 { 78 HTMLElement::insertedInto(insertionPoint); 79 m_loader->insertedInto(insertionPoint); 80 return InsertionDone; 81 } 82 83 void HTMLScriptElement::setText(const String &value) 84 { 85 RefPtr<Node> protectFromMutationEvents(this); 86 87 int numChildren = childNodeCount(); 88 89 if (numChildren == 1 && firstChild()->isTextNode()) { 90 toText(firstChild())->setData(value); 91 return; 92 } 93 94 if (numChildren > 0) 95 removeChildren(); 96 97 appendChild(document()->createTextNode(value.impl()), IGNORE_EXCEPTION); 98 } 99 100 void HTMLScriptElement::setAsync(bool async) 101 { 102 setBooleanAttribute(asyncAttr, async); 103 m_loader->handleAsyncAttribute(); 104 } 105 106 bool HTMLScriptElement::async() const 107 { 108 return fastHasAttribute(asyncAttr) || (m_loader->forceAsync()); 109 } 110 111 KURL HTMLScriptElement::src() const 112 { 113 return document()->completeURL(sourceAttributeValue()); 114 } 115 116 void HTMLScriptElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const 117 { 118 HTMLElement::addSubresourceAttributeURLs(urls); 119 120 addSubresourceURL(urls, src()); 121 } 122 123 String HTMLScriptElement::sourceAttributeValue() const 124 { 125 return getAttribute(srcAttr).string(); 126 } 127 128 String HTMLScriptElement::charsetAttributeValue() const 129 { 130 return getAttribute(charsetAttr).string(); 131 } 132 133 String HTMLScriptElement::typeAttributeValue() const 134 { 135 return getAttribute(typeAttr).string(); 136 } 137 138 String HTMLScriptElement::languageAttributeValue() const 139 { 140 return getAttribute(languageAttr).string(); 141 } 142 143 String HTMLScriptElement::forAttributeValue() const 144 { 145 return getAttribute(forAttr).string(); 146 } 147 148 String HTMLScriptElement::eventAttributeValue() const 149 { 150 return getAttribute(eventAttr).string(); 151 } 152 153 bool HTMLScriptElement::asyncAttributeValue() const 154 { 155 return fastHasAttribute(asyncAttr); 156 } 157 158 bool HTMLScriptElement::deferAttributeValue() const 159 { 160 return fastHasAttribute(deferAttr); 161 } 162 163 bool HTMLScriptElement::hasSourceAttribute() const 164 { 165 return fastHasAttribute(srcAttr); 166 } 167 168 void HTMLScriptElement::dispatchLoadEvent() 169 { 170 ASSERT(!m_loader->haveFiredLoadEvent()); 171 dispatchEvent(Event::create(eventNames().loadEvent, false, false)); 172 } 173 174 PassRefPtr<Element> HTMLScriptElement::cloneElementWithoutAttributesAndChildren() 175 { 176 return adoptRef(new HTMLScriptElement(tagQName(), document(), false, m_loader->alreadyStarted())); 177 } 178 179 } 180