2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
6 * Copyright (C) 2009 Rob Buis (rwlbuis@gmail.com)
7 * Copyright (C) 2011 Google Inc. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "core/html/HTMLLinkElement.h"
28 #include "HTMLNames.h"
29 #include "RuntimeEnabledFeatures.h"
30 #include "bindings/v8/ScriptEventListener.h"
31 #include "core/css/MediaList.h"
32 #include "core/css/MediaQueryEvaluator.h"
33 #include "core/css/StyleSheetContents.h"
34 #include "core/css/resolver/StyleResolver.h"
35 #include "core/dom/Attribute.h"
36 #include "core/dom/Document.h"
37 #include "core/events/Event.h"
38 #include "core/events/EventSender.h"
39 #include "core/dom/StyleEngine.h"
40 #include "core/fetch/CSSStyleSheetResource.h"
41 #include "core/fetch/FetchRequest.h"
42 #include "core/fetch/ResourceFetcher.h"
43 #include "core/html/LinkImport.h"
44 #include "core/loader/FrameLoader.h"
45 #include "core/loader/FrameLoaderClient.h"
46 #include "core/frame/ContentSecurityPolicy.h"
47 #include "core/frame/Frame.h"
48 #include "core/frame/FrameView.h"
49 #include "wtf/StdLibExtras.h"
53 using namespace HTMLNames;
55 static LinkEventSender& linkLoadEventSender()
57 DEFINE_STATIC_LOCAL(LinkEventSender, sharedLoadEventSender, (EventTypeNames::load));
58 return sharedLoadEventSender;
61 inline HTMLLinkElement::HTMLLinkElement(Document& document, bool createdByParser)
62 : HTMLElement(linkTag, document)
64 , m_sizes(DOMSettableTokenList::create())
65 , m_createdByParser(createdByParser)
66 , m_isInShadowTree(false)
67 , m_beforeLoadRecurseCount(0)
69 ScriptWrappable::init(this);
72 PassRefPtr<HTMLLinkElement> HTMLLinkElement::create(Document& document, bool createdByParser)
74 return adoptRef(new HTMLLinkElement(document, createdByParser));
77 HTMLLinkElement::~HTMLLinkElement()
82 document().styleEngine()->removeStyleSheetCandidateNode(this);
84 linkLoadEventSender().cancelEvent(this);
87 void HTMLLinkElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
89 if (name == relAttr) {
90 m_relAttribute = LinkRelAttribute(value);
92 } else if (name == hrefAttr) {
94 } else if (name == typeAttr) {
97 } else if (name == sizesAttr) {
98 m_sizes->setValue(value);
100 } else if (name == mediaAttr) {
101 m_media = value.string().lower();
103 } else if (name == disabledAttr) {
104 if (LinkStyle* link = linkStyle())
105 link->setDisabledState(!value.isNull());
106 } else if (name == onbeforeloadAttr)
107 setAttributeEventListener(EventTypeNames::beforeload, createAttributeEventListener(this, name, value));
109 if (name == titleAttr) {
110 if (LinkStyle* link = linkStyle())
111 link->setSheetTitle(value);
114 HTMLElement::parseAttribute(name, value);
118 bool HTMLLinkElement::shouldLoadLink()
120 bool continueLoad = true;
121 RefPtr<Document> originalDocument(document());
122 int recursionRank = ++m_beforeLoadRecurseCount;
123 if (!dispatchBeforeLoadEvent(getNonEmptyURLAttribute(hrefAttr)))
124 continueLoad = false;
126 // A beforeload handler might have removed us from the document or changed the document.
127 if (continueLoad && (!inDocument() || document() != originalDocument))
128 continueLoad = false;
130 // If the beforeload handler recurses into the link element by mutating it, we should only
131 // let the latest (innermost) mutation occur.
132 if (recursionRank != m_beforeLoadRecurseCount)
133 continueLoad = false;
135 if (recursionRank == 1)
136 m_beforeLoadRecurseCount = 0;
141 bool HTMLLinkElement::loadLink(const String& type, const KURL& url)
143 return m_linkLoader.loadLink(m_relAttribute, fastGetAttribute(HTMLNames::crossoriginAttr), type, url, document());
146 LinkResource* HTMLLinkElement::linkResourceToProcess()
148 bool visible = inDocument() && !m_isInShadowTree;
150 ASSERT(!linkStyle() || !linkStyle()->hasSheet());
155 if (m_relAttribute.isImport() && RuntimeEnabledFeatures::htmlImportsEnabled())
156 m_link = LinkImport::create(this);
158 OwnPtr<LinkStyle> link = LinkStyle::create(this);
159 if (fastHasAttribute(disabledAttr))
160 link->setDisabledState(true);
161 m_link = link.release();
168 LinkStyle* HTMLLinkElement::linkStyle() const
170 if (!m_link || m_link->type() != LinkResource::Style)
172 return static_cast<LinkStyle*>(m_link.get());
175 LinkImport* HTMLLinkElement::linkImport() const
177 if (!m_link || m_link->type() != LinkResource::Import)
179 return static_cast<LinkImport*>(m_link.get());
182 bool HTMLLinkElement::importOwnsLoader() const
184 LinkImport* import = linkImport();
187 return import->ownsLoader();
190 Document* HTMLLinkElement::import() const
192 if (LinkImport* link = linkImport())
193 return link->importedDocument();
197 void HTMLLinkElement::process()
199 if (LinkResource* link = linkResourceToProcess())
203 Node::InsertionNotificationRequest HTMLLinkElement::insertedInto(ContainerNode* insertionPoint)
205 HTMLElement::insertedInto(insertionPoint);
206 if (!insertionPoint->inDocument())
207 return InsertionDone;
209 m_isInShadowTree = isInShadowTree();
210 if (m_isInShadowTree)
211 return InsertionDone;
213 document().styleEngine()->addStyleSheetCandidateNode(this, m_createdByParser);
216 return InsertionDone;
219 void HTMLLinkElement::removedFrom(ContainerNode* insertionPoint)
221 HTMLElement::removedFrom(insertionPoint);
222 if (!insertionPoint->inDocument())
225 m_linkLoader.released();
227 if (m_isInShadowTree) {
228 ASSERT(!linkStyle() || !linkStyle()->hasSheet());
231 document().styleEngine()->removeStyleSheetCandidateNode(this);
233 RefPtr<StyleSheet> removedSheet = sheet();
236 m_link->ownerRemoved();
238 document().removedStyleSheet(removedSheet.get());
241 void HTMLLinkElement::finishParsingChildren()
243 m_createdByParser = false;
244 HTMLElement::finishParsingChildren();
247 bool HTMLLinkElement::styleSheetIsLoading() const
249 return linkStyle() && linkStyle()->styleSheetIsLoading();
252 void HTMLLinkElement::linkLoaded()
254 dispatchEvent(Event::create(EventTypeNames::load));
257 void HTMLLinkElement::linkLoadingErrored()
259 dispatchEvent(Event::create(EventTypeNames::error));
262 void HTMLLinkElement::didStartLinkPrerender()
264 dispatchEvent(Event::create(EventTypeNames::webkitprerenderstart));
267 void HTMLLinkElement::didStopLinkPrerender()
269 dispatchEvent(Event::create(EventTypeNames::webkitprerenderstop));
272 void HTMLLinkElement::didSendLoadForLinkPrerender()
274 dispatchEvent(Event::create(EventTypeNames::webkitprerenderload));
277 void HTMLLinkElement::didSendDOMContentLoadedForLinkPrerender()
279 dispatchEvent(Event::create(EventTypeNames::webkitprerenderdomcontentloaded));
282 bool HTMLLinkElement::sheetLoaded()
285 return linkStyle()->sheetLoaded();
288 void HTMLLinkElement::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred)
291 linkStyle()->notifyLoadedSheetAndAllCriticalSubresources(errorOccurred);
294 void HTMLLinkElement::dispatchPendingLoadEvents()
296 linkLoadEventSender().dispatchPendingEvents();
299 void HTMLLinkElement::dispatchPendingEvent(LinkEventSender* eventSender)
301 ASSERT_UNUSED(eventSender, eventSender == &linkLoadEventSender());
303 dispatchEventImmediately();
306 void HTMLLinkElement::dispatchEventImmediately()
308 if (m_link->hasLoaded())
311 linkLoadingErrored();
314 void HTMLLinkElement::scheduleEvent()
316 linkLoadEventSender().dispatchEventSoon(this);
319 void HTMLLinkElement::startLoadingDynamicSheet()
322 linkStyle()->startLoadingDynamicSheet();
325 bool HTMLLinkElement::isURLAttribute(const Attribute& attribute) const
327 return attribute.name().localName() == hrefAttr || HTMLElement::isURLAttribute(attribute);
330 KURL HTMLLinkElement::href() const
332 return document().completeURL(getAttribute(hrefAttr));
335 const AtomicString& HTMLLinkElement::rel() const
337 return getAttribute(relAttr);
340 const AtomicString& HTMLLinkElement::type() const
342 return getAttribute(typeAttr);
345 IconType HTMLLinkElement::iconType() const
347 return m_relAttribute.iconType();
350 const AtomicString& HTMLLinkElement::iconSizes() const
352 return m_sizes->toString();
355 DOMSettableTokenList* HTMLLinkElement::sizes() const
357 return m_sizes.get();
360 PassOwnPtr<LinkStyle> LinkStyle::create(HTMLLinkElement* owner)
362 return adoptPtr(new LinkStyle(owner));
365 LinkStyle::LinkStyle(HTMLLinkElement* owner)
366 : LinkResource(owner)
367 , m_disabledState(Unset)
368 , m_pendingSheetType(None)
371 , m_loadedSheet(false)
375 LinkStyle::~LinkStyle()
378 m_sheet->clearOwnerNode();
381 Document& LinkStyle::document()
383 return m_owner->document();
386 void LinkStyle::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource* cachedStyleSheet)
388 if (!m_owner->inDocument()) {
393 // Completing the sheet load may cause scripts to execute.
394 RefPtr<Node> protector(m_owner);
396 CSSParserContext parserContext(m_owner->document(), 0, baseURL, charset);
398 if (RefPtr<StyleSheetContents> restoredSheet = const_cast<CSSStyleSheetResource*>(cachedStyleSheet)->restoreParsedStyleSheet(parserContext)) {
399 ASSERT(restoredSheet->isCacheable());
400 ASSERT(!restoredSheet->isLoading());
404 m_sheet = CSSStyleSheet::create(restoredSheet, m_owner);
405 m_sheet->setMediaQueries(MediaQuerySet::create(m_owner->media()));
406 m_sheet->setTitle(m_owner->title());
409 restoredSheet->checkLoaded();
413 RefPtr<StyleSheetContents> styleSheet = StyleSheetContents::create(href, parserContext);
417 m_sheet = CSSStyleSheet::create(styleSheet, m_owner);
418 m_sheet->setMediaQueries(MediaQuerySet::create(m_owner->media()));
419 m_sheet->setTitle(m_owner->title());
421 styleSheet->parseAuthorStyleSheet(cachedStyleSheet, m_owner->document().securityOrigin());
424 styleSheet->notifyLoadedSheet(cachedStyleSheet);
425 styleSheet->checkLoaded();
427 if (styleSheet->isCacheable())
428 const_cast<CSSStyleSheetResource*>(cachedStyleSheet)->saveParsedStyleSheet(styleSheet);
431 bool LinkStyle::sheetLoaded()
433 if (!styleSheetIsLoading()) {
434 removePendingSheet();
440 void LinkStyle::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred)
444 m_loadedSheet = !errorOccurred;
446 m_owner->scheduleEvent();
450 void LinkStyle::startLoadingDynamicSheet()
452 ASSERT(m_pendingSheetType < Blocking);
453 addPendingSheet(Blocking);
456 void LinkStyle::clearSheet()
459 ASSERT(m_sheet->ownerNode() == m_owner);
460 m_sheet->clearOwnerNode();
464 bool LinkStyle::styleSheetIsLoading() const
470 return m_sheet->contents()->isLoading();
473 void LinkStyle::addPendingSheet(PendingSheetType type)
475 if (type <= m_pendingSheetType)
477 m_pendingSheetType = type;
479 if (m_pendingSheetType == NonBlocking)
481 m_owner->document().styleEngine()->addPendingSheet();
484 void LinkStyle::removePendingSheet(RemovePendingSheetNotificationType notification)
486 PendingSheetType type = m_pendingSheetType;
487 m_pendingSheetType = None;
491 if (type == NonBlocking) {
492 // Tell StyleEngine to re-compute styleSheets of this m_owner's treescope.
493 m_owner->document().styleEngine()->modifiedStyleSheetCandidateNode(m_owner);
494 // Document::removePendingSheet() triggers the style selector recalc for blocking sheets.
495 // FIXME: We don't have enough knowledge at this point to know if we're adding or removing a sheet
496 // so we can't call addedStyleSheet() or removedStyleSheet().
497 m_owner->document().styleResolverChanged(RecalcStyleImmediately);
501 m_owner->document().styleEngine()->removePendingSheet(m_owner,
502 notification == RemovePendingSheetNotifyImmediately
503 ? StyleEngine::RemovePendingSheetNotifyImmediately
504 : StyleEngine::RemovePendingSheetNotifyLater);
507 void LinkStyle::setDisabledState(bool disabled)
509 LinkStyle::DisabledState oldDisabledState = m_disabledState;
510 m_disabledState = disabled ? Disabled : EnabledViaScript;
511 if (oldDisabledState != m_disabledState) {
512 // If we change the disabled state while the sheet is still loading, then we have to
513 // perform three checks:
514 if (styleSheetIsLoading()) {
515 // Check #1: The sheet becomes disabled while loading.
516 if (m_disabledState == Disabled)
517 removePendingSheet();
519 // Check #2: An alternate sheet becomes enabled while it is still loading.
520 if (m_owner->relAttribute().isAlternate() && m_disabledState == EnabledViaScript)
521 addPendingSheet(Blocking);
523 // Check #3: A main sheet becomes enabled while it was still loading and
524 // after it was disabled via script. It takes really terrible code to make this
525 // happen (a double toggle for no reason essentially). This happens on
526 // virtualplastic.net, which manages to do about 12 enable/disables on only 3
528 if (!m_owner->relAttribute().isAlternate() && m_disabledState == EnabledViaScript && oldDisabledState == Disabled)
529 addPendingSheet(Blocking);
531 // If the sheet is already loading just bail.
536 m_sheet->setDisabled(disabled);
538 // Load the sheet, since it's never been loaded before.
539 if (!m_sheet && m_disabledState == EnabledViaScript) {
540 if (m_owner->shouldProcessStyle())
543 // FIXME: We don't have enough knowledge here to know if we should call addedStyleSheet() or removedStyleSheet().
544 m_owner->document().styleResolverChanged(RecalcStyleDeferred);
549 void LinkStyle::process()
551 ASSERT(m_owner->shouldProcessStyle());
552 String type = m_owner->typeValue().lower();
553 LinkRequestBuilder builder(m_owner);
555 if (m_owner->relAttribute().iconType() != InvalidIcon && builder.url().isValid() && !builder.url().isEmpty()) {
556 if (!m_owner->shouldLoadLink())
558 if (!document().securityOrigin()->canDisplay(builder.url()))
560 if (!document().contentSecurityPolicy()->allowImageFromSource(builder.url()))
562 if (document().frame())
563 document().frame()->loader().client()->dispatchDidChangeIcons(m_owner->relAttribute().iconType());
566 if (!m_owner->loadLink(type, builder.url()))
569 if ((m_disabledState != Disabled) && m_owner->relAttribute().isStyleSheet()
570 && shouldLoadResource() && builder.url().isValid()) {
573 removePendingSheet();
577 if (!m_owner->shouldLoadLink())
582 bool mediaQueryMatches = true;
583 if (!m_owner->media().isEmpty()) {
584 Frame* frame = loadingFrame();
585 if (Document* document = loadingFrame()->document()) {
586 RefPtr<RenderStyle> documentStyle = StyleResolver::styleForDocument(*document);
587 RefPtr<MediaQuerySet> media = MediaQuerySet::create(m_owner->media());
588 MediaQueryEvaluator evaluator(frame->view()->mediaType(), frame, documentStyle.get());
589 mediaQueryMatches = evaluator.eval(media.get());
593 // Don't hold up render tree construction and script execution on stylesheets
594 // that are not needed for the rendering at the moment.
595 bool blocking = mediaQueryMatches && !m_owner->isAlternate();
596 addPendingSheet(blocking ? Blocking : NonBlocking);
598 // Load stylesheets that are not needed for the rendering immediately with low priority.
599 FetchRequest request = builder.build(blocking);
600 AtomicString crossOriginMode = m_owner->fastGetAttribute(HTMLNames::crossoriginAttr);
601 if (!crossOriginMode.isNull()) {
602 StoredCredentials allowCredentials = equalIgnoringCase(crossOriginMode, "use-credentials") ? AllowStoredCredentials : DoNotAllowStoredCredentials;
603 request.setCrossOriginAccessControl(document().securityOrigin(), allowCredentials);
605 setResource(document().fetcher()->fetchCSSStyleSheet(request));
608 // The request may have been denied if (for example) the stylesheet is local and the document is remote.
610 removePendingSheet();
612 } else if (m_sheet) {
613 // we no longer contain a stylesheet, e.g. perhaps rel or type was changed
614 RefPtr<StyleSheet> removedSheet = m_sheet;
616 document().removedStyleSheet(removedSheet.get());
620 void LinkStyle::setSheetTitle(const String& title)
623 m_sheet->setTitle(title);
626 void LinkStyle::ownerRemoved()
631 if (styleSheetIsLoading())
632 removePendingSheet(RemovePendingSheetNotifyLater);
635 } // namespace WebCore