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 "FrameLoader.h"
#include "HTMLNames.h"
#include "kjs_proxy.h"
#include "MIMETypeRegistry.h"
#include "Text.h"
#include "ScriptSourceCode.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(bool changedByParser)
{
if (!m_createdByParser && inDocument() && firstChild())
evaluateScript(document()->url(), text());
HTMLElement::childrenChanged(changedByParser);
}
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()) {
m_cachedScript = document()->docLoader()->requestScript(url, getAttribute(charsetAttr));
if (m_cachedScript)
m_cachedScript->ref(this);
else
dispatchHTMLEvent(errorEvent, true, false);
}
} else if (attrName == onloadAttr)
setHTMLEventListener(loadEvent, attr);
else
HTMLElement::parseMappedAttribute(attr);
}
void HTMLScriptElement::finishParsingChildren()
{
if (getAttribute(srcAttr).isEmpty() && text().isEmpty())
setCreatedByParser(false);
HTMLElement::finishParsingChildren();
}
void HTMLScriptElement::insertedIntoDocument()
{
HTMLElement::insertedIntoDocument();
ASSERT(!m_cachedScript);
if (m_createdByParser)
return;
if (!document()->frame())
return;
const AtomicString& url = getAttribute(srcAttr);
if (!url.isEmpty()) {
String scriptSrcCharset = getAttribute(charsetAttr).domString().stripWhiteSpace();
if (scriptSrcCharset.isEmpty()) {
if (Frame* frame = document()->frame())
scriptSrcCharset = frame->loader()->encoding();
}
m_cachedScript = document()->docLoader()->requestScript(url, scriptSrcCharset);
if (m_cachedScript)
m_cachedScript->ref(this);
else
dispatchHTMLEvent(errorEvent, true, false);
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;
}
}
bool HTMLScriptElement::shouldExecuteAsJavaScript()
{
static const AtomicString validLanguages[] = {
"javascript",
"javascript1.0",
"javascript1.1",
"javascript1.2",
"javascript1.3",
"javascript1.4",
"javascript1.5",
"javascript1.6",
"javascript1.7",
"livescript",
"ecmascript",
"jscript"
};
static const unsigned validLanguagesCount = sizeof(validLanguages) / sizeof(validLanguages[0]);
const AtomicString& type = getAttribute(typeAttr);
if (!type.isEmpty()) {
String lowerType = type.domString().stripWhiteSpace().lower();
if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(lowerType))
return true;
return false;
}
const AtomicString& language = getAttribute(languageAttr);
if (!language.isEmpty()) {
String lowerLanguage = language.domString().lower();
for (unsigned i = 0; i < validLanguagesCount; ++i)
if (lowerLanguage == validLanguages[i])
return true;
return false;
}
return true;
}
void HTMLScriptElement::evaluateScript(const String& url, const String& script)
{
if (m_evaluated)
return;
if (!shouldExecuteAsJavaScript())
return;
Frame* frame = document()->frame();
if (frame) {
if (frame->scriptProxy()->isEnabled()) {
m_evaluated = true;
if (m_cachedScript)
frame->scriptProxy()->evaluate(ScriptSourceCode(m_cachedScript));
else
frame->scriptProxy()->evaluate(ScriptSourceCode(script, KURL(url.deprecatedString())));
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);
}
}