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