1/*
2 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@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#include <wtf/text/TextPosition.h>
27
28namespace WebCore {
29
30class CachedScript;
31class Element;
32class ScriptElement;
33class ScriptSourceCode;
34
35class ScriptElement : private CachedResourceClient {
36    WTF_MAKE_FAST_ALLOCATED;
37public:
38    ScriptElement(Element*, bool createdByParser, bool isEvaluated);
39    virtual ~ScriptElement();
40
41    Element* element() const { return m_element; }
42
43    enum LegacyTypeSupport { DisallowLegacyTypeInTypeAttribute, AllowLegacyTypeInTypeAttribute };
44    bool prepareScript(const TextPosition1& scriptStartPosition = TextPosition1::minimumPosition(), LegacyTypeSupport = DisallowLegacyTypeInTypeAttribute);
45
46    String scriptCharset() const { return m_characterEncoding; }
47    String scriptContent() const;
48    void executeScript(const ScriptSourceCode&);
49    void execute(CachedScript*);
50
51    // XML parser calls these
52    virtual void dispatchLoadEvent() = 0;
53    virtual void dispatchErrorEvent() = 0;
54    bool isScriptTypeSupported(LegacyTypeSupport) const;
55
56    bool haveFiredLoadEvent() const { return m_haveFiredLoad; }
57    bool willBeParserExecuted() const { return m_willBeParserExecuted; }
58    bool readyToBeParserExecuted() const { return m_readyToBeParserExecuted; }
59    bool willExecuteWhenDocumentFinishedParsing() const { return m_willExecuteWhenDocumentFinishedParsing; }
60    CachedResourceHandle<CachedScript> cachedScript() { return m_cachedScript; }
61
62protected:
63    void setHaveFiredLoadEvent(bool haveFiredLoad) { m_haveFiredLoad = haveFiredLoad; }
64    bool isParserInserted() const { return m_parserInserted; }
65    bool alreadyStarted() const { return m_alreadyStarted; }
66    bool forceAsync() const { return m_forceAsync; }
67
68    // Helper functions used by our parent classes.
69    void insertedIntoDocument();
70    void removedFromDocument();
71    void childrenChanged();
72    void handleSourceAttribute(const String& sourceUrl);
73    void handleAsyncAttribute();
74
75private:
76    bool ignoresLoadRequest() const;
77    bool isScriptForEventSupported() const;
78
79    bool requestScript(const String& sourceUrl);
80    void stopLoadRequest();
81
82    virtual void notifyFinished(CachedResource*);
83
84    virtual String sourceAttributeValue() const = 0;
85    virtual String charsetAttributeValue() const = 0;
86    virtual String typeAttributeValue() const = 0;
87    virtual String languageAttributeValue() const = 0;
88    virtual String forAttributeValue() const = 0;
89    virtual String eventAttributeValue() const = 0;
90    virtual bool asyncAttributeValue() const = 0;
91    virtual bool deferAttributeValue() const = 0;
92    virtual bool hasSourceAttribute() const = 0;
93
94    Element* m_element;
95    CachedResourceHandle<CachedScript> m_cachedScript;
96    bool m_parserInserted : 1;
97    bool m_isExternalScript : 1;
98    bool m_alreadyStarted : 1;
99    bool m_haveFiredLoad : 1;
100    bool m_willBeParserExecuted : 1; // Same as "The parser will handle executing the script."
101    bool m_readyToBeParserExecuted : 1;
102    bool m_willExecuteWhenDocumentFinishedParsing : 1;
103    bool m_forceAsync : 1;
104    bool m_willExecuteInOrder : 1;
105    String m_characterEncoding;
106    String m_fallbackCharacterEncoding;
107};
108
109ScriptElement* toScriptElement(Element*);
110
111}
112
113#endif
114