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