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/css/CSSStyleSheet.h"
28 #include "core/dom/DOMSettableTokenList.h"
29 #include "core/dom/IconURL.h"
30 #include "core/fetch/ResourceOwner.h"
31 #include "core/fetch/StyleSheetResource.h"
32 #include "core/fetch/StyleSheetResourceClient.h"
33 #include "core/html/HTMLElement.h"
34 #include "core/html/LinkRelAttribute.h"
35 #include "core/html/LinkResource.h"
36 #include "core/loader/LinkLoader.h"
37 #include "core/loader/LinkLoaderClient.h"
41 class DocumentFragment;
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;
61 static PassOwnPtrWillBeRawPtr<LinkStyle> create(HTMLLinkElement* owner);
63 explicit LinkStyle(HTMLLinkElement* owner);
66 virtual Type type() const OVERRIDE { return Style; }
67 virtual void process() OVERRIDE;
68 virtual void ownerRemoved() OVERRIDE;
69 virtual bool hasLoaded() const OVERRIDE { return m_loadedSheet; }
70 virtual void trace(Visitor*) OVERRIDE;
72 void startLoadingDynamicSheet();
73 void notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred);
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 virtual 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 RefPtrWillBeMember<CSSStyleSheet> m_sheet;
109 DisabledState m_disabledState;
110 PendingSheetType m_pendingSheetType;
117 class HTMLLinkElement FINAL : public HTMLElement, public LinkLoaderClient {
119 static PassRefPtrWillBeRawPtr<HTMLLinkElement> create(Document&, bool createdByParser);
120 virtual ~HTMLLinkElement();
123 const AtomicString& rel() const;
124 String media() const { return m_media; }
125 String typeValue() const { return m_type; }
126 const LinkRelAttribute& relAttribute() const { return m_relAttribute; }
128 const AtomicString& type() const;
130 IconType iconType() const;
132 // the icon sizes as parsed from the HTML attribute
133 const Vector<IntSize>& iconSizes() const;
137 CSSStyleSheet* sheet() const { return linkStyle() ? linkStyle()->sheet() : 0; }
138 Document* import() const;
140 bool styleSheetIsLoading() const;
142 bool isImport() const { return linkImport(); }
143 bool isDisabled() const { return linkStyle() && linkStyle()->isDisabled(); }
144 bool isEnabledViaScript() const { return linkStyle() && linkStyle()->isEnabledViaScript(); }
145 void enableIfExitTransitionStyle();
147 DOMSettableTokenList* sizes() const;
149 void dispatchPendingEvent(LinkEventSender*);
150 void scheduleEvent();
151 void dispatchEventImmediately();
152 static void dispatchPendingLoadEvents();
154 // From LinkLoaderClient
155 virtual bool shouldLoadLink() OVERRIDE;
158 bool loadLink(const String& type, const KURL&);
159 bool isAlternate() const { return linkStyle()->isUnset() && m_relAttribute.isAlternate(); }
160 bool shouldProcessStyle() { return linkResourceToProcess() && linkStyle(); }
161 bool isCreatedByParser() const { return m_createdByParser; }
163 // Parse the icon size attribute into |iconSizes|, make this method public
164 // visible for testing purpose.
165 static void parseSizesAttribute(const AtomicString& value, Vector<IntSize>& iconSizes);
167 virtual void trace(Visitor*) OVERRIDE;
170 virtual void attributeWillChange(const QualifiedName&, const AtomicString& oldValue, const AtomicString& newValue) OVERRIDE;
171 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
173 LinkStyle* linkStyle() const;
174 LinkImport* linkImport() const;
175 LinkResource* linkResourceToProcess();
178 static void processCallback(Node*);
180 // From Node and subclassses
181 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
182 virtual void removedFrom(ContainerNode*) OVERRIDE;
183 virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
184 virtual bool hasLegalLinkAttribute(const QualifiedName&) const OVERRIDE;
185 virtual const QualifiedName& subResourceAttributeName() const OVERRIDE;
186 virtual bool sheetLoaded() OVERRIDE;
187 virtual void notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred) OVERRIDE;
188 virtual void startLoadingDynamicSheet() OVERRIDE;
189 virtual void finishParsingChildren() OVERRIDE;
191 // From LinkLoaderClient
192 virtual void linkLoaded() OVERRIDE;
193 virtual void linkLoadingErrored() OVERRIDE;
194 virtual void didStartLinkPrerender() OVERRIDE;
195 virtual void didStopLinkPrerender() OVERRIDE;
196 virtual void didSendLoadForLinkPrerender() OVERRIDE;
197 virtual void didSendDOMContentLoadedForLinkPrerender() OVERRIDE;
200 HTMLLinkElement(Document&, bool createdByParser);
202 OwnPtrWillBeMember<LinkResource> m_link;
203 LinkLoader m_linkLoader;
207 RefPtrWillBeMember<DOMSettableTokenList> m_sizes;
208 Vector<IntSize> m_iconSizes;
209 LinkRelAttribute m_relAttribute;
211 bool m_createdByParser;
212 bool m_isInShadowTree;