| 1 | /* |
| 2 | * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org> |
| 3 | * Copyright (C) 2009-2017 Apple Inc. All rights reserved. |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #pragma once |
| 23 | |
| 24 | #include "ContainerNode.h" |
| 25 | #include "LoadableScript.h" |
| 26 | #include "UserGestureIndicator.h" |
| 27 | #include <wtf/MonotonicTime.h> |
| 28 | #include <wtf/text/TextPosition.h> |
| 29 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | class CachedScript; |
| 33 | class ContainerNode; |
| 34 | class Element; |
| 35 | class LoadableModuleScript; |
| 36 | class PendingScript; |
| 37 | class ScriptSourceCode; |
| 38 | |
| 39 | class ScriptElement { |
| 40 | public: |
| 41 | virtual ~ScriptElement() = default; |
| 42 | |
| 43 | Element& element() { return m_element; } |
| 44 | const Element& element() const { return m_element; } |
| 45 | |
| 46 | enum LegacyTypeSupport { DisallowLegacyTypeInTypeAttribute, AllowLegacyTypeInTypeAttribute }; |
| 47 | bool prepareScript(const TextPosition& scriptStartPosition = TextPosition(), LegacyTypeSupport = DisallowLegacyTypeInTypeAttribute); |
| 48 | |
| 49 | String scriptCharset() const { return m_characterEncoding; } |
| 50 | WEBCORE_EXPORT String scriptContent() const; |
| 51 | void executeClassicScript(const ScriptSourceCode&); |
| 52 | void executeModuleScript(LoadableModuleScript&); |
| 53 | |
| 54 | void executePendingScript(PendingScript&); |
| 55 | |
| 56 | // XML parser calls these |
| 57 | virtual void dispatchLoadEvent() = 0; |
| 58 | void dispatchErrorEvent(); |
| 59 | |
| 60 | bool haveFiredLoadEvent() const { return m_haveFiredLoad; } |
| 61 | bool willBeParserExecuted() const { return m_willBeParserExecuted; } |
| 62 | bool readyToBeParserExecuted() const { return m_readyToBeParserExecuted; } |
| 63 | bool willExecuteWhenDocumentFinishedParsing() const { return m_willExecuteWhenDocumentFinishedParsing; } |
| 64 | bool willExecuteInOrder() const { return m_willExecuteInOrder; } |
| 65 | LoadableScript* loadableScript() { return m_loadableScript.get(); } |
| 66 | |
| 67 | // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type |
| 68 | enum class ScriptType { Classic, Module }; |
| 69 | ScriptType scriptType() const { return m_isModuleScript ? ScriptType::Module : ScriptType::Classic; } |
| 70 | |
| 71 | void ref(); |
| 72 | void deref(); |
| 73 | |
| 74 | protected: |
| 75 | ScriptElement(Element&, bool createdByParser, bool isEvaluated); |
| 76 | |
| 77 | void setHaveFiredLoadEvent(bool haveFiredLoad) { m_haveFiredLoad = haveFiredLoad; } |
| 78 | bool isParserInserted() const { return m_parserInserted; } |
| 79 | bool alreadyStarted() const { return m_alreadyStarted; } |
| 80 | bool forceAsync() const { return m_forceAsync; } |
| 81 | |
| 82 | // Helper functions used by our parent classes. |
| 83 | Node::InsertedIntoAncestorResult insertedIntoAncestor(Node::InsertionType insertionType, ContainerNode&) const |
| 84 | { |
| 85 | if (insertionType.connectedToDocument && !m_parserInserted) |
| 86 | return Node::InsertedIntoAncestorResult::NeedsPostInsertionCallback; |
| 87 | return Node::InsertedIntoAncestorResult::Done; |
| 88 | } |
| 89 | |
| 90 | void didFinishInsertingNode(); |
| 91 | void childrenChanged(const ContainerNode::ChildChange&); |
| 92 | void handleSourceAttribute(const String& sourceURL); |
| 93 | void handleAsyncAttribute(); |
| 94 | |
| 95 | private: |
| 96 | void executeScriptAndDispatchEvent(LoadableScript&); |
| 97 | |
| 98 | Optional<ScriptType> determineScriptType(LegacyTypeSupport) const; |
| 99 | bool ignoresLoadRequest() const; |
| 100 | bool isScriptForEventSupported() const; |
| 101 | void dispatchLoadEventRespectingUserGestureIndicator(); |
| 102 | |
| 103 | bool requestClassicScript(const String& sourceURL); |
| 104 | bool requestModuleScript(const TextPosition& scriptStartPosition); |
| 105 | |
| 106 | virtual String sourceAttributeValue() const = 0; |
| 107 | virtual String charsetAttributeValue() const = 0; |
| 108 | virtual String typeAttributeValue() const = 0; |
| 109 | virtual String languageAttributeValue() const = 0; |
| 110 | virtual String forAttributeValue() const = 0; |
| 111 | virtual String eventAttributeValue() const = 0; |
| 112 | virtual bool hasAsyncAttribute() const = 0; |
| 113 | virtual bool hasDeferAttribute() const = 0; |
| 114 | virtual bool hasSourceAttribute() const = 0; |
| 115 | virtual bool hasNoModuleAttribute() const = 0; |
| 116 | |
| 117 | Element& m_element; |
| 118 | WTF::OrdinalNumber m_startLineNumber; |
| 119 | bool m_parserInserted : 1; |
| 120 | bool m_isExternalScript : 1; |
| 121 | bool m_alreadyStarted : 1; |
| 122 | bool m_haveFiredLoad : 1; |
| 123 | bool m_willBeParserExecuted : 1; // Same as "The parser will handle executing the script." |
| 124 | bool m_readyToBeParserExecuted : 1; |
| 125 | bool m_willExecuteWhenDocumentFinishedParsing : 1; |
| 126 | bool m_forceAsync : 1; |
| 127 | bool m_willExecuteInOrder : 1; |
| 128 | bool m_isModuleScript : 1; |
| 129 | String m_characterEncoding; |
| 130 | String m_fallbackCharacterEncoding; |
| 131 | RefPtr<LoadableScript> m_loadableScript; |
| 132 | |
| 133 | MonotonicTime m_creationTime; |
| 134 | RefPtr<UserGestureToken> m_userGestureToken; |
| 135 | }; |
| 136 | |
| 137 | // FIXME: replace with is/downcast<ScriptElement>. |
| 138 | bool isScriptElement(Element&); |
| 139 | ScriptElement& downcastScriptElement(Element&); |
| 140 | |
| 141 | } |
| 142 | |