2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2008, 2010 Apple Inc. All rights reserved.
5 * Copyright (C) 2011 Google Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef HTMLLinkElement_h
25 #define HTMLLinkElement_h
27 #include "core/CoreExport.h"
28 #include "core/css/CSSStyleSheet.h"
29 #include "core/dom/DOMSettableTokenList.h"
30 #include "core/dom/IconURL.h"
31 #include "core/fetch/ResourceOwner.h"
32 #include "core/fetch/StyleSheetResource.h"
33 #include "core/fetch/StyleSheetResourceClient.h"
34 #include "core/html/HTMLElement.h"
35 #include "core/html/LinkRelAttribute.h"
36 #include "core/html/LinkResource.h"
37 #include "core/loader/LinkLoader.h"
38 #include "core/loader/LinkLoaderClient.h"
42 class HTMLLinkElement
;
46 template<typename T
> class EventSender
;
47 typedef EventSender
<HTMLLinkElement
> LinkEventSender
;
50 // LinkStyle handles dynaically change-able link resources, which is
51 // typically @rel="stylesheet".
53 // It could be @rel="shortcut icon" or soething else though. Each of
54 // types might better be handled by a separate class, but dynamically
55 // changing @rel makes it harder to move such a design so we are
56 // sticking current way so far.
58 class LinkStyle final
: public LinkResource
, ResourceOwner
<StyleSheetResource
> {
59 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED(LinkStyle
);
61 static PassOwnPtrWillBeRawPtr
<LinkStyle
> create(HTMLLinkElement
* owner
);
63 explicit LinkStyle(HTMLLinkElement
* owner
);
64 ~LinkStyle() override
;
66 Type
type() const override
{ return Style
; }
67 void process() override
;
68 void ownerRemoved() override
;
69 bool hasLoaded() const override
{ return m_loadedSheet
; }
70 DECLARE_VIRTUAL_TRACE();
72 void startLoadingDynamicSheet();
73 void notifyLoadedSheetAndAllCriticalSubresources(Node::LoadedSheetErrorStatus
);
76 void setDisabledState(bool);
77 void setSheetTitle(const String
&);
79 bool styleSheetIsLoading() const;
80 bool hasSheet() const { return m_sheet
; }
81 bool isDisabled() const { return m_disabledState
== Disabled
; }
82 bool isEnabledViaScript() const { return m_disabledState
== EnabledViaScript
; }
83 bool isUnset() const { return m_disabledState
== Unset
; }
85 CSSStyleSheet
* sheet() const { return m_sheet
.get(); }
88 // From StyleSheetResourceClient
89 void setCSSStyleSheet(const String
& href
, const KURL
& baseURL
, const String
& charset
, const CSSStyleSheetResource
*) override
;
97 enum PendingSheetType
{
104 void addPendingSheet(PendingSheetType
);
105 void removePendingSheet();
106 Document
& document();
108 void setCrossOriginStylesheetStatus(CSSStyleSheet
*);
109 void setFetchFollowingCORS()
111 ASSERT(!m_fetchFollowingCORS
);
112 m_fetchFollowingCORS
= true;
115 RefPtrWillBeMember
<CSSStyleSheet
> m_sheet
;
116 DisabledState m_disabledState
;
117 PendingSheetType m_pendingSheetType
;
121 bool m_fetchFollowingCORS
;
125 class CORE_EXPORT HTMLLinkElement final
: public HTMLElement
, public LinkLoaderClient
{
126 DEFINE_WRAPPERTYPEINFO();
128 static PassRefPtrWillBeRawPtr
<HTMLLinkElement
> create(Document
&, bool createdByParser
);
129 ~HTMLLinkElement() override
;
132 const AtomicString
& rel() const;
133 String
media() const { return m_media
; }
134 String
typeValue() const { return m_type
; }
135 String
asValue() const { return m_as
; }
136 const LinkRelAttribute
& relAttribute() const { return m_relAttribute
; }
138 const AtomicString
& type() const;
140 IconType
iconType() const;
142 // the icon sizes as parsed from the HTML attribute
143 const Vector
<IntSize
>& iconSizes() const;
147 CSSStyleSheet
* sheet() const { return linkStyle() ? linkStyle()->sheet() : 0; }
148 Document
* import() const;
150 bool styleSheetIsLoading() const;
152 bool isImport() const { return linkImport(); }
153 bool isDisabled() const { return linkStyle() && linkStyle()->isDisabled(); }
154 bool isEnabledViaScript() const { return linkStyle() && linkStyle()->isEnabledViaScript(); }
156 DOMSettableTokenList
* sizes() const;
158 void dispatchPendingEvent(LinkEventSender
*);
159 void scheduleEvent();
160 void dispatchEventImmediately();
161 static void dispatchPendingLoadEvents();
163 // From LinkLoaderClient
164 bool shouldLoadLink() override
;
167 bool loadLink(const String
& type
, const String
& as
, const KURL
&);
168 bool isAlternate() const { return linkStyle()->isUnset() && m_relAttribute
.isAlternate(); }
169 bool shouldProcessStyle() { return linkResourceToProcess() && linkStyle(); }
170 bool isCreatedByParser() const { return m_createdByParser
; }
172 // Parse the icon size attribute into |iconSizes|, make this method public
173 // visible for testing purpose.
174 static void parseSizesAttribute(const AtomicString
& value
, Vector
<IntSize
>& iconSizes
);
176 DECLARE_VIRTUAL_TRACE();
179 void attributeWillChange(const QualifiedName
&, const AtomicString
& oldValue
, const AtomicString
& newValue
) override
;
180 void parseAttribute(const QualifiedName
&, const AtomicString
&) override
;
182 LinkStyle
* linkStyle() const;
183 LinkImport
* linkImport() const;
184 LinkResource
* linkResourceToProcess();
187 static void processCallback(Node
*);
189 // From Node and subclassses
190 InsertionNotificationRequest
insertedInto(ContainerNode
*) override
;
191 void removedFrom(ContainerNode
*) override
;
192 bool isURLAttribute(const Attribute
&) const override
;
193 bool hasLegalLinkAttribute(const QualifiedName
&) const override
;
194 const QualifiedName
& subResourceAttributeName() const override
;
195 bool sheetLoaded() override
;
196 void notifyLoadedSheetAndAllCriticalSubresources(LoadedSheetErrorStatus
) override
;
197 void startLoadingDynamicSheet() override
;
198 void finishParsingChildren() override
;
200 // From LinkLoaderClient
201 void linkLoaded() override
;
202 void linkLoadingErrored() override
;
203 void didStartLinkPrerender() override
;
204 void didStopLinkPrerender() override
;
205 void didSendLoadForLinkPrerender() override
;
206 void didSendDOMContentLoadedForLinkPrerender() override
;
209 HTMLLinkElement(Document
&, bool createdByParser
);
211 OwnPtrWillBeMember
<LinkResource
> m_link
;
212 LinkLoader m_linkLoader
;
217 RefPtrWillBeMember
<DOMSettableTokenList
> m_sizes
;
218 Vector
<IntSize
> m_iconSizes
;
219 LinkRelAttribute m_relAttribute
;
221 bool m_createdByParser
;
222 bool m_isInShadowTree
;
227 #endif // HTMLLinkElement_h