Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2008 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef ScriptElement_h
     22 #define ScriptElement_h
     23 
     24 #include "CachedResourceClient.h"
     25 #include "CachedResourceHandle.h"
     26 
     27 namespace WebCore {
     28 
     29 class CachedScript;
     30 class Element;
     31 class ScriptElementData;
     32 class ScriptSourceCode;
     33 
     34 class ScriptElement {
     35 public:
     36     ScriptElement() { }
     37     virtual ~ScriptElement() { }
     38 
     39     virtual String scriptContent() const = 0;
     40 
     41     virtual String sourceAttributeValue() const = 0;
     42     virtual String charsetAttributeValue() const = 0;
     43     virtual String typeAttributeValue() const = 0;
     44     virtual String languageAttributeValue() const = 0;
     45     virtual String forAttributeValue() const = 0;
     46 
     47     virtual void dispatchLoadEvent() = 0;
     48     virtual void dispatchErrorEvent() = 0;
     49 
     50     // A charset for loading the script (may be overridden by HTTP headers or a BOM).
     51     virtual String scriptCharset() const = 0;
     52 
     53     virtual bool shouldExecuteAsJavaScript() const = 0;
     54 
     55 protected:
     56     // Helper functions used by our parent classes.
     57     static void insertedIntoDocument(ScriptElementData&, const String& sourceUrl);
     58     static void removedFromDocument(ScriptElementData&);
     59     static void childrenChanged(ScriptElementData&);
     60     static void finishParsingChildren(ScriptElementData&, const String& sourceUrl);
     61     static void handleSourceAttribute(ScriptElementData&, const String& sourceUrl);
     62 };
     63 
     64 // HTML/SVGScriptElement hold this struct as member variable
     65 // and pass it to the static helper functions in ScriptElement
     66 class ScriptElementData : private CachedResourceClient {
     67 public:
     68     ScriptElementData(ScriptElement*, Element*);
     69     virtual ~ScriptElementData();
     70 
     71     bool ignoresLoadRequest() const;
     72     bool shouldExecuteAsJavaScript() const;
     73 
     74     String scriptContent() const;
     75     String scriptCharset() const;
     76 
     77     Element* element() const { return m_element; }
     78     bool createdByParser() const { return m_createdByParser; }
     79     void setCreatedByParser(bool value) { m_createdByParser = value; }
     80     bool haveFiredLoadEvent() const { return m_firedLoad; }
     81     void setHaveFiredLoadEvent(bool firedLoad) { m_firedLoad = firedLoad; }
     82 
     83     void requestScript(const String& sourceUrl);
     84     void evaluateScript(const ScriptSourceCode&);
     85     void stopLoadRequest();
     86 
     87     void execute(CachedScript*);
     88 
     89 private:
     90     virtual void notifyFinished(CachedResource*);
     91 
     92 private:
     93     ScriptElement* m_scriptElement;
     94     Element* m_element;
     95     CachedResourceHandle<CachedScript> m_cachedScript;
     96     bool m_createdByParser;
     97     bool m_requested;
     98     bool m_evaluated;
     99     bool m_firedLoad;
    100 };
    101 
    102 ScriptElement* toScriptElement(Element*);
    103 
    104 }
    105 
    106 #endif
    107