HTMLScriptElement.cpp [plain text]
#include "config.h"
#include "HTMLScriptElement.h"
#include "CachedScript.h"
#include "DocLoader.h"
#include "Document.h"
#include "EventNames.h"
#include "Frame.h"
#include "HTMLNames.h"
#include "kjs_proxy.h"
#include "Text.h"
namespace WebCore {
using namespace HTMLNames;
using namespace EventNames;
HTMLScriptElement::HTMLScriptElement(Document *doc)
: HTMLElement(scriptTag, doc)
, m_cachedScript(0)
, m_createdByParser(false)
, m_evaluated(false)
{
}
HTMLScriptElement::~HTMLScriptElement()
{
if (m_cachedScript)
m_cachedScript->deref(this);
}
bool HTMLScriptElement::isURLAttribute(Attribute *attr) const
{
return attr->name() == srcAttr;
}
void HTMLScriptElement::childrenChanged()
{
if (!m_createdByParser && inDocument() && firstChild())
evaluateScript(document()->URL(), text());
}
void HTMLScriptElement::parseMappedAttribute(MappedAttribute *attr)
{
const QualifiedName& attrName = attr->name();
if (attrName == srcAttr) {
if (m_evaluated || m_cachedScript || m_createdByParser || !inDocument())
return;
if (!document()->frame())
return;
const AtomicString& url = attr->value();
if (!url.isEmpty()) {
DeprecatedString charset = getAttribute(charsetAttr).deprecatedString();
m_cachedScript = document()->docLoader()->requestScript(url, charset);
m_cachedScript->ref(this);
}
} else if (attrName == onloadAttr)
setHTMLEventListener(loadEvent, attr);
else
HTMLElement::parseMappedAttribute(attr);
}
void HTMLScriptElement::closeRenderer()
{
if (getAttribute(srcAttr).isEmpty() && text().isEmpty())
setCreatedByParser(false);
HTMLElement::closeRenderer();
}
void HTMLScriptElement::insertedIntoDocument()
{
HTMLElement::insertedIntoDocument();
assert(!m_cachedScript);
if (m_createdByParser)
return;
if (!document()->frame())
return;
const AtomicString& url = getAttribute(srcAttr);
if (!url.isEmpty()) {
DeprecatedString charset = getAttribute(charsetAttr).deprecatedString();
m_cachedScript = document()->docLoader()->requestScript(url, charset);
m_cachedScript->ref(this);
return;
}
String scriptString = text();
if (!scriptString.isEmpty())
evaluateScript(document()->URL(), scriptString);
}
void HTMLScriptElement::removedFromDocument()
{
HTMLElement::removedFromDocument();
if (m_cachedScript) {
m_cachedScript->deref(this);
m_cachedScript = 0;
}
}
void HTMLScriptElement::notifyFinished(CachedResource* o)
{
CachedScript *cs = static_cast<CachedScript *>(o);
assert(cs == m_cachedScript);
RefPtr<HTMLScriptElement> protect(this);
if (cs->errorOccurred())
dispatchHTMLEvent(errorEvent, true, false);
else {
evaluateScript(cs->url(), cs->script());
dispatchHTMLEvent(loadEvent, false, false);
}
if (m_cachedScript) {
m_cachedScript->deref(this);
m_cachedScript = 0;
}
}
void HTMLScriptElement::evaluateScript(const String& URL, const String& script)
{
if (m_evaluated)
return;
Frame* frame = document()->frame();
if (frame) {
KJSProxy* proxy = frame->jScript();
if (proxy) {
m_evaluated = true;
proxy->evaluate(URL, 0, script, 0);
Document::updateDocumentsRendering();
}
}
}
String HTMLScriptElement::text() const
{
String val = "";
for (Node *n = firstChild(); n; n = n->nextSibling()) {
if (n->isTextNode())
val += static_cast<Text *>(n)->data();
}
return val;
}
void HTMLScriptElement::setText(const String &value)
{
ExceptionCode ec = 0;
int numChildren = childNodeCount();
if (numChildren == 1 && firstChild()->isTextNode()) {
static_cast<Text *>(firstChild())->setData(value, ec);
return;
}
if (numChildren > 0) {
removeChildren();
}
appendChild(document()->createTextNode(value.impl()), ec);
}
String HTMLScriptElement::htmlFor() const
{
return String();
}
void HTMLScriptElement::setHtmlFor(const String &)
{
}
String HTMLScriptElement::event() const
{
return String();
}
void HTMLScriptElement::setEvent(const String &)
{
}
String HTMLScriptElement::charset() const
{
return getAttribute(charsetAttr);
}
void HTMLScriptElement::setCharset(const String &value)
{
setAttribute(charsetAttr, value);
}
bool HTMLScriptElement::defer() const
{
return !getAttribute(deferAttr).isNull();
}
void HTMLScriptElement::setDefer(bool defer)
{
setAttribute(deferAttr, defer ? "" : 0);
}
String HTMLScriptElement::src() const
{
return document()->completeURL(getAttribute(srcAttr));
}
void HTMLScriptElement::setSrc(const String &value)
{
setAttribute(srcAttr, value);
}
String HTMLScriptElement::type() const
{
return getAttribute(typeAttr);
}
void HTMLScriptElement::setType(const String &value)
{
setAttribute(typeAttr, value);
}
}