2 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2009-2017 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #include "ContainerNode.h"
25 #include "LoadableScript.h"
26 #include "ReferrerPolicy.h"
27 #include "UserGestureIndicator.h"
28 #include <wtf/MonotonicTime.h>
29 #include <wtf/text/TextPosition.h>
36 class LoadableModuleScript;
38 class ScriptSourceCode;
42 virtual ~ScriptElement() = default;
44 Element& element() { return m_element; }
45 const Element& element() const { return m_element; }
47 enum LegacyTypeSupport { DisallowLegacyTypeInTypeAttribute, AllowLegacyTypeInTypeAttribute };
48 bool prepareScript(const TextPosition& scriptStartPosition = TextPosition(), LegacyTypeSupport = DisallowLegacyTypeInTypeAttribute);
50 String scriptCharset() const { return m_characterEncoding; }
51 WEBCORE_EXPORT String scriptContent() const;
52 void executeClassicScript(const ScriptSourceCode&);
53 void executeModuleScript(LoadableModuleScript&);
55 void executePendingScript(PendingScript&);
57 // XML parser calls these
58 virtual void dispatchLoadEvent() = 0;
59 virtual void dispatchErrorEvent();
61 bool haveFiredLoadEvent() const { return m_haveFiredLoad; }
62 bool errorOccurred() const { return m_errorOccurred; }
63 bool willBeParserExecuted() const { return m_willBeParserExecuted; }
64 bool readyToBeParserExecuted() const { return m_readyToBeParserExecuted; }
65 bool willExecuteWhenDocumentFinishedParsing() const { return m_willExecuteWhenDocumentFinishedParsing; }
66 bool willExecuteInOrder() const { return m_willExecuteInOrder; }
67 LoadableScript* loadableScript() { return m_loadableScript.get(); }
69 // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type
70 enum class ScriptType { Classic, Module };
71 ScriptType scriptType() const { return m_isModuleScript ? ScriptType::Module : ScriptType::Classic; }
77 ScriptElement(Element&, bool createdByParser, bool isEvaluated);
79 void setHaveFiredLoadEvent(bool haveFiredLoad) { m_haveFiredLoad = haveFiredLoad; }
80 void setErrorOccurred(bool errorOccurred) { m_errorOccurred = errorOccurred; }
81 bool isParserInserted() const { return m_parserInserted; }
82 bool alreadyStarted() const { return m_alreadyStarted; }
83 bool forceAsync() const { return m_forceAsync; }
85 // Helper functions used by our parent classes.
86 Node::InsertedIntoAncestorResult insertedIntoAncestor(Node::InsertionType insertionType, ContainerNode&) const
88 if (insertionType.connectedToDocument && !m_parserInserted)
89 return Node::InsertedIntoAncestorResult::NeedsPostInsertionCallback;
90 return Node::InsertedIntoAncestorResult::Done;
93 void didFinishInsertingNode();
94 void childrenChanged(const ContainerNode::ChildChange&);
95 void handleSourceAttribute(const String& sourceURL);
96 void handleAsyncAttribute();
99 void executeScriptAndDispatchEvent(LoadableScript&);
101 Optional<ScriptType> determineScriptType(LegacyTypeSupport) const;
102 bool ignoresLoadRequest() const;
103 bool isScriptForEventSupported() const;
104 void dispatchLoadEventRespectingUserGestureIndicator();
106 bool requestClassicScript(const String& sourceURL);
107 bool requestModuleScript(const TextPosition& scriptStartPosition);
109 virtual String sourceAttributeValue() const = 0;
110 virtual String charsetAttributeValue() const = 0;
111 virtual String typeAttributeValue() const = 0;
112 virtual String languageAttributeValue() const = 0;
113 virtual String forAttributeValue() const = 0;
114 virtual String eventAttributeValue() const = 0;
115 virtual bool hasAsyncAttribute() const = 0;
116 virtual bool hasDeferAttribute() const = 0;
117 virtual bool hasSourceAttribute() const = 0;
118 virtual bool hasNoModuleAttribute() const = 0;
119 virtual ReferrerPolicy referrerPolicy() const = 0;
122 WTF::OrdinalNumber m_startLineNumber;
123 bool m_parserInserted : 1;
124 bool m_isExternalScript : 1;
125 bool m_alreadyStarted : 1;
126 bool m_haveFiredLoad : 1;
127 bool m_errorOccurred : 1;
128 bool m_willBeParserExecuted : 1; // Same as "The parser will handle executing the script."
129 bool m_readyToBeParserExecuted : 1;
130 bool m_willExecuteWhenDocumentFinishedParsing : 1;
131 bool m_forceAsync : 1;
132 bool m_willExecuteInOrder : 1;
133 bool m_isModuleScript : 1;
134 String m_characterEncoding;
135 String m_fallbackCharacterEncoding;
136 RefPtr<LoadableScript> m_loadableScript;
138 MonotonicTime m_creationTime;
139 RefPtr<UserGestureToken> m_userGestureToken;
142 // FIXME: replace with is/downcast<ScriptElement>.
143 bool isScriptElement(Element&);
144 ScriptElement& downcastScriptElement(Element&);