| /* |
| * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| * (C) 2001 Dirk Mueller (mueller@kde.org) |
| * Copyright (C) 2003-2025 Apple Inc. All rights reserved. |
| * Copyright (C) 2009 Rob Buis (rwlbuis@gmail.com) |
| * Copyright (C) 2011 Google Inc. All rights reserved. |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Library General Public |
| * License as published by the Free Software Foundation; either |
| * version 2 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Library General Public License for more details. |
| * |
| * You should have received a copy of the GNU Library General Public License |
| * along with this library; see the file COPYING.LIB. If not, write to |
| * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| * Boston, MA 02110-1301, USA. |
| */ |
| |
| #include "config.h" |
| #include "HTMLLinkElement.h" |
| |
| #include "Attribute.h" |
| #include "CSSParserContext.h" |
| #include "CachedCSSStyleSheet.h" |
| #include "CachedResource.h" |
| #include "CachedResourceRequest.h" |
| #include "ContentSecurityPolicy.h" |
| #include "CrossOriginAccessControl.h" |
| #include "DOMTokenList.h" |
| #include "DefaultResourceLoadPriority.h" |
| #include "DocumentInlines.h" |
| #include "DocumentLoader.h" |
| #include "DocumentPage.h" |
| #include "DocumentQuirks.h" |
| #include "DocumentResourceLoader.h" |
| #include "DocumentView.h" |
| #include "ElementInlines.h" |
| #include "Event.h" |
| #include "EventNames.h" |
| #include "EventSender.h" |
| #include "FrameDestructionObserverInlines.h" |
| #include "FrameLoader.h" |
| #include "FrameTree.h" |
| #include "HTMLAnchorElement.h" |
| #include "HTMLDocumentParser.h" |
| #include "HTMLNames.h" |
| #include "HTMLParserIdioms.h" |
| #include "IdTargetObserver.h" |
| #include "JSRequestPriority.h" |
| #include "LocalFrame.h" |
| #include "LocalFrameLoaderClient.h" |
| #include "LocalFrameView.h" |
| #include "Logging.h" |
| #include "MediaQueryEvaluator.h" |
| #include "MediaQueryParser.h" |
| #include "MouseEvent.h" |
| #include "NodeName.h" |
| #include "Page.h" |
| #include "ParsedContentType.h" |
| #include "RequestPriority.h" |
| #include "SecurityOrigin.h" |
| #include "Settings.h" |
| #include "StyleComputedStyle.h" |
| #include "StyleResolveForDocument.h" |
| #include "StyleScope.h" |
| #include "StyleSheetContents.h" |
| #include "SubresourceIntegrity.h" |
| #include "TreeScopeInlines.h" |
| #include <wtf/Ref.h> |
| #include <wtf/Scope.h> |
| #include <wtf/StdLibExtras.h> |
| #include <wtf/text/MakeString.h> |
| #include <wtf/text/TextStream.h> |
| |
| namespace WebCore { |
| |
| WTF_MAKE_TZONE_ALLOCATED_IMPL(HTMLLinkElement); |
| |
| using namespace HTMLNames; |
| |
| static LinkEventSender& linkLoadEventSenderSingleton() |
| { |
| static NeverDestroyed<LinkEventSender> sharedLoadEventSender; |
| return sharedLoadEventSender; |
| } |
| |
| class ExpectIdTargetObserver final : public IdTargetObserver { |
| WTF_MAKE_TZONE_ALLOCATED(ExpectIdTargetObserver); |
| WTF_OVERRIDE_DELETE_FOR_CHECKED_PTR(ExpectIdTargetObserver); |
| public: |
| ExpectIdTargetObserver(const AtomString& id, HTMLLinkElement&); |
| |
| void idTargetChanged(Element&) override; |
| |
| private: |
| WeakPtr<HTMLLinkElement, WeakPtrImplWithEventTargetData> m_element; |
| }; |
| |
| WTF_MAKE_TZONE_ALLOCATED_IMPL(ExpectIdTargetObserver); |
| |
| ExpectIdTargetObserver::ExpectIdTargetObserver(const AtomString& id, HTMLLinkElement& element) |
| : IdTargetObserver(protect(element)->treeScope().idTargetObserverRegistry(), id) |
| , m_element(element) |
| { |
| } |
| |
| void ExpectIdTargetObserver::idTargetChanged(Element& element) |
| { |
| if (RefPtr link = m_element.get()) |
| link->processInternalResourceLink(&element); |
| } |
| |
| inline HTMLLinkElement::HTMLLinkElement(const QualifiedName& tagName, Document& document, bool createdByParser) |
| : HTMLElement(tagName, document) |
| , m_linkLoader(LinkLoader::create(*this)) |
| , m_disabledState(Unset) |
| , m_loading(false) |
| , m_createdByParser(createdByParser) |
| , m_loadedResource(false) |
| , m_isHandlingBeforeLoad(false) |
| , m_allowPrefetchLoadAndErrorForTesting(false) |
| , m_pendingSheetType(PendingSheetType::Unknown) |
| { |
| ASSERT(hasTagName(linkTag)); |
| } |
| |
| Ref<HTMLLinkElement> HTMLLinkElement::create(const QualifiedName& tagName, Document& document, bool createdByParser) |
| { |
| return adoptRef(*new HTMLLinkElement(tagName, document, createdByParser)); |
| } |
| |
| HTMLLinkElement::~HTMLLinkElement() |
| { |
| if (m_sheet) |
| protect(m_sheet)->clearOwnerNode(); |
| |
| if (m_cachedSheet) |
| protect(m_cachedSheet)->removeClient(*this); |
| |
| if (CheckedPtr styleScope = m_styleScope) |
| styleScope->removeStyleSheetCandidateNode(*this); |
| |
| linkLoadEventSenderSingleton().cancelEvent(*this); |
| } |
| |
| void HTMLLinkElement::setDisabledState(bool disabled) |
| { |
| DisabledState oldDisabledState = m_disabledState; |
| m_disabledState = disabled ? Disabled : EnabledViaScript; |
| if (oldDisabledState == m_disabledState) |
| return; |
| |
| ASSERT(isConnected() || !styleSheetIsLoading()); |
| if (!isConnected()) |
| return; |
| |
| // If we change the disabled state while the sheet is still loading, then we have to |
| // perform three checks: |
| if (styleSheetIsLoading()) { |
| // Check #1: The sheet becomes disabled while loading. |
| if (m_disabledState == Disabled) |
| removePendingSheet(); |
| |
| // Check #2: An alternate sheet becomes enabled while it is still loading. |
| if (m_relAttribute.isAlternate && m_disabledState == EnabledViaScript) |
| addPendingSheet(PendingSheetType::Active); |
| |
| // Check #3: A main sheet becomes enabled while it was still loading and |
| // after it was disabled via script. It takes really terrible code to make this |
| // happen (a double toggle for no reason essentially). This happens on |
| // virtualplastic.net, which manages to do about 12 enable/disables on only 3 |
| // sheets. :) |
| if (!m_relAttribute.isAlternate && m_disabledState == EnabledViaScript && oldDisabledState == Disabled) |
| addPendingSheet(PendingSheetType::Active); |
| |
| // If the sheet is already loading just bail. |
| return; |
| } |
| |
| // Load the sheet, since it's never been loaded before. |
| if (!m_sheet && m_disabledState == EnabledViaScript) |
| process(); |
| else { |
| protect(m_styleScope)->didChangeActiveStyleSheetCandidates(); |
| if (m_sheet) |
| clearSheet(); |
| } |
| } |
| |
| void HTMLLinkElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason) |
| { |
| switch (name.nodeName()) { |
| case AttributeNames::relAttr: { |
| if (equalLettersIgnoringASCIICase(newValue, "spatial-backdrop"_s)) |
| document().addConsoleMessage(MessageSource::Other, MessageLevel::Error, "The \"spatial-backdrop\" link rel value is no longer supported and was ignored. Use the <model> immersive API instead."_s); |
| auto parsedRel = LinkRelAttribute(document(), newValue); |
| auto didMutateRel = parsedRel != m_relAttribute; |
| m_relAttribute = WTF::move(parsedRel); |
| if (m_relList) |
| m_relList->associatedAttributeValueChanged(); |
| if (didMutateRel) |
| process(); |
| break; |
| } |
| case AttributeNames::hrefAttr: { |
| URL url = getNonEmptyURLAttribute(hrefAttr); |
| if (url == m_url) |
| return; |
| m_url = WTF::move(url); |
| process(); |
| break; |
| } |
| case AttributeNames::typeAttr: |
| if (newValue == m_type) |
| return; |
| m_type = newValue; |
| process(); |
| break; |
| case AttributeNames::sizesAttr: |
| if (m_sizes) |
| m_sizes->associatedAttributeValueChanged(); |
| process(); |
| break; |
| case AttributeNames::blockingAttr: |
| blocking().associatedAttributeValueChanged(); |
| if (protect(blocking())->contains("render"_s)) { |
| processInternalResourceLink(); |
| if (m_loading && mediaAttributeMatches() && !isAlternate()) |
| potentiallyBlockRendering(); |
| } else if (!isImplicitlyPotentiallyRenderBlocking()) |
| unblockRendering(); |
| break; |
| case AttributeNames::mediaAttr: { |
| auto media = newValue.string().convertToASCIILowercase(); |
| if (media == m_media) |
| return; |
| m_media = WTF::move(media); |
| process(); |
| if (m_sheet && !isDisabled()) |
| m_styleScope->didChangeActiveStyleSheetCandidates(); |
| break; |
| } |
| case AttributeNames::crossoriginAttr: |
| HTMLElement::attributeChanged(name, oldValue, newValue, attributeModificationReason); |
| // Only compression-dictionary lists crossorigin among its appropriate times to fetch: |
| // https://html.spec.whatwg.org/multipage/links.html#link-type-compression-dictionary |
| if (oldValue != newValue && m_relAttribute.isCompressionDictionary) |
| process(); |
| break; |
| case AttributeNames::disabledAttr: |
| setDisabledState(!newValue.isNull()); |
| break; |
| case AttributeNames::titleAttr: |
| if (m_sheet && !isInShadowTree()) |
| protect(m_sheet)->setTitle(newValue); |
| break; |
| default: |
| HTMLElement::attributeChanged(name, oldValue, newValue, attributeModificationReason); |
| break; |
| } |
| } |
| |
| bool HTMLLinkElement::shouldLoadLink() |
| { |
| return isConnected(); |
| } |
| |
| String HTMLLinkElement::crossOrigin() const |
| { |
| return parseCORSSettingsAttribute(attributeWithoutSynchronization(crossoriginAttr)); |
| } |
| |
| String HTMLLinkElement::as() const |
| { |
| String as = attributeWithoutSynchronization(asAttr); |
| if (equalLettersIgnoringASCIICase(as, "fetch"_s) |
| || equalLettersIgnoringASCIICase(as, "image"_s) |
| || equalLettersIgnoringASCIICase(as, "script"_s) |
| || equalLettersIgnoringASCIICase(as, "style"_s) |
| || (document().settings().mediaPreloadingEnabled() && (equalLettersIgnoringASCIICase(as, "video"_s) || equalLettersIgnoringASCIICase(as, "audio"_s))) |
| || equalLettersIgnoringASCIICase(as, "track"_s) |
| || equalLettersIgnoringASCIICase(as, "font"_s)) |
| return as.convertToASCIILowercase(); |
| return String(); |
| } |
| |
| void HTMLLinkElement::process() |
| { |
| if (!isConnected()) { |
| ASSERT(!m_sheet); |
| return; |
| } |
| |
| // Prevent recursive loading of link. |
| if (m_isHandlingBeforeLoad) |
| return; |
| |
| processInternalResourceLink(); |
| if (m_relAttribute.isInternalResourceLink) |
| return; |
| |
| Ref document = this->document(); |
| LinkLoadParameters params { |
| m_relAttribute, |
| m_url, |
| attributeWithoutSynchronization(asAttr), |
| attributeWithoutSynchronization(mediaAttr), |
| attributeWithoutSynchronization(typeAttr), |
| attributeWithoutSynchronization(crossoriginAttr), |
| attributeWithoutSynchronization(imagesrcsetAttr), |
| attributeWithoutSynchronization(imagesizesAttr), |
| nonce(), |
| referrerPolicy(), |
| fetchPriority(), |
| }; |
| |
| m_linkLoader->loadLink(params, document); |
| |
| bool treatAsStyleSheet = false; |
| if (m_relAttribute.isStyleSheet) { |
| if (m_type.isNull()) |
| treatAsStyleSheet = true; |
| else if (auto parsedContentType = ParsedContentType::create(m_type)) |
| treatAsStyleSheet = equalLettersIgnoringASCIICase(parsedContentType->mimeType(), "text/css"_s); |
| } |
| if (!treatAsStyleSheet) |
| treatAsStyleSheet = document->settings().treatsAnyTextCSSLinkAsStylesheet() && m_type.containsIgnoringASCIICase("text/css"_s); |
| |
| LOG_WITH_STREAM(StyleSheets, stream << "HTMLLinkElement " << this << " process() - treatAsStyleSheet " << treatAsStyleSheet); |
| |
| if (m_disabledState != Disabled && treatAsStyleSheet && document->frame() && m_url.isValid()) { |
| String charset = attributeWithoutSynchronization(charsetAttr); |
| if (!PAL::TextEncoding { charset }.isValid()) |
| charset = document->charset(); |
| |
| if (RefPtr cachedSheet = std::exchange(m_cachedSheet, nullptr)) { |
| removePendingSheet(); |
| cachedSheet->removeClient(*this); |
| } |
| |
| { |
| bool previous = m_isHandlingBeforeLoad; |
| m_isHandlingBeforeLoad = true; |
| auto scopeExit = makeScopeExit([&] { m_isHandlingBeforeLoad = previous; }); |
| if (!shouldLoadLink()) |
| return; |
| } |
| |
| m_loading = true; |
| |
| // Don't hold up render tree construction and script execution on stylesheets |
| // that are not needed for the rendering at the moment. |
| bool isActive = mediaAttributeMatches() && !isAlternate(); |
| addPendingSheet(isActive ? PendingSheetType::Active : PendingSheetType::Inactive); |
| |
| if (isActive) |
| potentiallyBlockRendering(); |
| else |
| unblockRendering(); |
| |
| // Load stylesheets that are not needed for the rendering immediately with low priority. |
| std::optional<ResourceLoadPriority> priority; |
| if (!isActive) |
| priority = DefaultResourceLoadPriority::inactiveStyleSheet; |
| |
| m_integrityMetadataForPendingSheetRequest = attributeWithoutSynchronization(HTMLNames::integrityAttr); |
| |
| ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions(); |
| options.nonce = nonce(); |
| options.sameOriginDataURLFlag = SameOriginDataURLFlag::Set; |
| if (protect(document->contentSecurityPolicy())->allowStyleWithNonce(options.nonce)) |
| options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck; |
| options.integrity = m_integrityMetadataForPendingSheetRequest; |
| options.referrerPolicy = params.referrerPolicy; |
| options.fetchPriority = fetchPriority(); |
| |
| auto request = createPotentialAccessControlRequest(URL { m_url }, WTF::move(options), document, crossOrigin()); |
| request.setPriority(WTF::move(priority)); |
| request.setCharset(WTF::move(charset)); |
| request.setInitiator(*this); |
| |
| ASSERT_WITH_SECURITY_IMPLICATION(!m_cachedSheet); |
| if (auto result = protect(document->cachedResourceLoader())->requestCSSStyleSheet(WTF::move(request))) |
| m_cachedSheet = WTF::move(result.value()); |
| else |
| m_cachedSheet = nullptr; |
| if (RefPtr cachedSheet = m_cachedSheet) |
| cachedSheet->addClient(*this); |
| else { |
| // The request may have been denied if (for example) the stylesheet is local and the document is remote. |
| m_loading = false; |
| sheetLoaded(); |
| notifyLoadedSheetAndAllCriticalSubresources(true); |
| unblockRendering(); |
| } |
| |
| return; |
| } |
| |
| unblockRendering(); |
| |
| if (m_sheet) { |
| // we no longer contain a stylesheet, e.g. perhaps rel or type was changed |
| clearSheet(); |
| protect(m_styleScope)->didChangeActiveStyleSheetCandidates(); |
| return; |
| } |
| |
| if (m_relAttribute.isCompressionDictionary) { |
| m_linkLoader->loadCompressionDictionaryLink(params, document); |
| return; |
| } |
| |
| #if ENABLE(APPLICATION_MANIFEST) |
| if (isApplicationManifest()) { |
| if (RefPtr loader = document->loader()) |
| loader->loadApplicationManifest({ }); |
| return; |
| } |
| #endif // ENABLE(APPLICATION_MANIFEST) |
| } |
| |
| void HTMLLinkElement::clearSheet() |
| { |
| ASSERT(m_sheet); |
| ASSERT(m_sheet->ownerNode() == this); |
| protect(m_sheet)->clearOwnerNode(); |
| m_sheet = nullptr; |
| } |
| |
| // https://html.spec.whatwg.org/multipage/links.html#process-internal-resource-link |
| void HTMLLinkElement::processInternalResourceLink(Element* element) |
| { |
| Ref document = this->document(); |
| if (document->wasRemovedLastRefCalled()) |
| return; |
| |
| if (!m_relAttribute.isInternalResourceLink) { |
| return; |
| } |
| |
| if (!equalIgnoringFragmentIdentifier(m_url, document->url())) { |
| unblockRendering(); |
| return; |
| } |
| |
| RefPtr<Element> indicatedElement; |
| // If the change originated from a specific element, then we can just check if that's |
| // the right one instead doing a tree search using the name |
| if (element) { |
| auto elementMatchesLinkId = [&](StringView id) { |
| if (element->getIdAttribute() == id) |
| return true; |
| RefPtr anchorElement = dynamicDowncast<HTMLAnchorElement>(element); |
| if (anchorElement && document->isMatchingAnchor(*anchorElement, m_url.fragmentIdentifier())) |
| return true; |
| return false; |
| }; |
| |
| if (element->isConnected() && (elementMatchesLinkId(m_url.fragmentIdentifier()) || elementMatchesLinkId(PAL::decodeURLEscapeSequences(m_url.fragmentIdentifier())))) |
| indicatedElement = element; |
| } else { |
| indicatedElement = document->findAnchor(m_url.fragmentIdentifier()); |
| if (!indicatedElement) |
| indicatedElement = document->findAnchor(PAL::decodeURLEscapeSequences(m_url.fragmentIdentifier())); |
| } |
| |
| // Don't match if indicatedElement "is on a stack of open elements of an HTML parser whose associated Document is doc" |
| if (RefPtr parser = document->htmlDocumentParser(); parser && indicatedElement) { |
| if (parser->isOnStackOfOpenElements(*indicatedElement)) |
| indicatedElement = nullptr; |
| } |
| |
| if (document->readyState() == Document::ReadyState::Loading && isConnected() && mediaAttributeMatches() && !indicatedElement) { |
| potentiallyBlockRendering(); |
| if (!m_expectIdTargetObserver) |
| m_expectIdTargetObserver = makeUnique<ExpectIdTargetObserver>(makeAtomString(m_url.fragmentIdentifier()), *this); |
| } else |
| unblockRendering(); |
| } |
| |
| // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#blocking-attributes |
| void HTMLLinkElement::potentiallyBlockRendering() |
| { |
| bool explicitRenderBlocking = m_blockingList && m_blockingList->contains("render"_s); |
| if (explicitRenderBlocking || isImplicitlyPotentiallyRenderBlocking()) { |
| protect(document())->blockRenderingOn(*this, explicitRenderBlocking ? Document::ImplicitRenderBlocking::No : Document::ImplicitRenderBlocking::Yes); |
| m_isRenderBlocking = true; |
| } |
| } |
| |
| void HTMLLinkElement::unblockRendering() |
| { |
| if (m_isRenderBlocking) { |
| protect(document())->unblockRenderingOn(*this); |
| m_isRenderBlocking = false; |
| } |
| } |
| |
| // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet |
| bool HTMLLinkElement::isImplicitlyPotentiallyRenderBlocking() const |
| { |
| return m_relAttribute.isStyleSheet && m_createdByParser; |
| } |
| |
| Node::NeedsPostConnectionSteps HTMLLinkElement::insertionSteps(InsertionType insertionType, ContainerNode& parentOfInsertedTree) |
| { |
| HTMLElement::insertionSteps(insertionType, parentOfInsertedTree); |
| if (!insertionType.connectedToDocument) |
| return NeedsPostConnectionSteps::No; |
| |
| m_styleScope = &Style::Scope::forNode(*this); |
| protect(m_styleScope)->addStyleSheetCandidateNode(*this, m_createdByParser); |
| |
| return NeedsPostConnectionSteps::Yes; |
| } |
| |
| void HTMLLinkElement::postConnectionSteps() |
| { |
| m_url = getNonEmptyURLAttribute(hrefAttr); |
| process(); |
| } |
| |
| void HTMLLinkElement::removingSteps(RemovalType removalType, ContainerNode& oldParentOfRemovedTree) |
| { |
| HTMLElement::removingSteps(removalType, oldParentOfRemovedTree); |
| if (!removalType.disconnectedFromDocument) |
| return; |
| |
| m_linkLoader->cancelLoad(); |
| |
| bool wasLoading = styleSheetIsLoading(); |
| |
| if (m_sheet) |
| clearSheet(); |
| |
| if (wasLoading) |
| removePendingSheet(); |
| |
| if (CheckedPtr styleScope = m_styleScope) { |
| styleScope->removeStyleSheetCandidateNode(*this); |
| m_styleScope = nullptr; |
| } |
| |
| processInternalResourceLink(); |
| unblockRendering(); |
| } |
| |
| void HTMLLinkElement::finishParsingChildren() |
| { |
| m_createdByParser = false; |
| HTMLElement::finishParsingChildren(); |
| } |
| |
| void HTMLLinkElement::initializeStyleSheet(Ref<StyleSheetContents>&& styleSheet, const CachedCSSStyleSheet& cachedStyleSheet, const CSSParserContext& context) |
| { |
| if (m_sheet) { |
| ASSERT(m_sheet->ownerNode() == this); |
| protect(m_sheet)->clearOwnerNode(); |
| } |
| |
| m_sheet = CSSStyleSheet::create(WTF::move(styleSheet), *this, cachedStyleSheet.isCORSSameOrigin()); |
| protect(m_sheet)->setMediaQueries(MQ::MediaQueryParser::parse(m_media, context)); |
| if (!isInShadowTree()) |
| protect(m_sheet)->setTitle(title()); |
| |
| if (CheckedPtr styleScope = m_styleScope) |
| styleScope->establishPreferredStylesheetSetName(*this, protect(*m_sheet)); |
| |
| if (!protect(m_sheet)->canAccessRules()) |
| m_sheet->contents().setAsLoadedFromOpaqueSource(); |
| } |
| |
| void HTMLLinkElement::setCSSStyleSheet(const String& href, const URL& baseURL, ASCIILiteral charset, const CachedCSSStyleSheet* cachedStyleSheet) |
| { |
| unblockRendering(); |
| if (!isConnected()) { |
| ASSERT(!m_sheet); |
| return; |
| } |
| Ref document = this->document(); |
| RefPtr frame = document->frame(); |
| if (!frame) |
| return; |
| |
| // Completing the sheet load may cause scripts to execute. |
| Ref<HTMLLinkElement> protectedThis(*this); |
| |
| if (!cachedStyleSheet->errorOccurred() && !matchIntegrityMetadata(*cachedStyleSheet, m_integrityMetadataForPendingSheetRequest)) { |
| document->addConsoleMessage(MessageSource::Security, MessageLevel::Error, makeString("Cannot load stylesheet "_s, integrityMismatchDescription(*cachedStyleSheet, m_integrityMetadataForPendingSheetRequest))); |
| |
| m_loading = false; |
| sheetLoaded(); |
| notifyLoadedSheetAndAllCriticalSubresources(true); |
| return; |
| } |
| |
| CSSParserContext parserContext(document.get(), baseURL, charset); |
| auto cachePolicy = frame->loader().subresourceCachePolicy(baseURL); |
| |
| if (auto restoredSheet = const_cast<CachedCSSStyleSheet*>(cachedStyleSheet)->restoreParsedStyleSheet(parserContext, cachePolicy, frame->loader())) { |
| ASSERT(restoredSheet->isCacheable()); |
| ASSERT(!restoredSheet->isLoading()); |
| initializeStyleSheet(restoredSheet.releaseNonNull(), *cachedStyleSheet, parserContext); |
| |
| m_loading = false; |
| sheetLoaded(); |
| notifyLoadedSheetAndAllCriticalSubresources(false); |
| return; |
| } |
| |
| auto styleSheet = StyleSheetContents::create(href, parserContext); |
| initializeStyleSheet(styleSheet.copyRef(), *cachedStyleSheet, parserContext); |
| |
| // FIXME: Set the visibility option based on m_sheet being clean or not. |
| // Best approach might be to set it on the style sheet content itself or its context parser otherwise. |
| if (!styleSheet.get().parseAuthorStyleSheet(cachedStyleSheet, protect(document->securityOrigin()).ptr())) { |
| m_loading = false; |
| sheetLoaded(); |
| notifyLoadedSheetAndAllCriticalSubresources(true); |
| return; |
| } |
| |
| m_loading = false; |
| styleSheet.get().notifyLoadedSheet(cachedStyleSheet); |
| styleSheet.get().checkLoaded(); |
| |
| if (styleSheet.get().isCacheable()) |
| const_cast<CachedCSSStyleSheet*>(cachedStyleSheet)->saveParsedStyleSheet(WTF::move(styleSheet)); |
| } |
| |
| bool HTMLLinkElement::styleSheetIsLoading() const |
| { |
| if (m_loading) |
| return true; |
| if (!m_sheet) |
| return false; |
| return m_sheet->contents().isLoading(); |
| } |
| |
| DOMTokenList& HTMLLinkElement::sizes() |
| { |
| if (!m_sizes) |
| lazyInitialize(m_sizes, makeUniqueWithoutRefCountedCheck<DOMTokenList>(*this, sizesAttr)); |
| return *m_sizes; |
| } |
| |
| bool HTMLLinkElement::mediaAttributeMatches() const |
| { |
| if (m_media.isEmpty()) |
| return true; |
| |
| Ref document = this->document(); |
| auto mediaQueryList = MQ::MediaQueryParser::parse(m_media, document->cssParserContext()); |
| LOG(MediaQueries, "HTMLLinkElement::mediaAttributeMatches"); |
| |
| MQ::MediaQueryEvaluator evaluator(protect(document->frame())->view()->mediaType(), document.get()); |
| return evaluator.evaluate(mediaQueryList); |
| } |
| |
| void HTMLLinkElement::linkLoaded() |
| { |
| m_loadedResource = true; |
| if (!m_relAttribute.isLinkPrefetch || m_allowPrefetchLoadAndErrorForTesting) |
| linkLoadEventSenderSingleton().dispatchEventSoon(*this, eventNames().loadEvent); |
| } |
| |
| void HTMLLinkElement::linkLoadingErrored() |
| { |
| if (!m_relAttribute.isLinkPrefetch || m_allowPrefetchLoadAndErrorForTesting) |
| linkLoadEventSenderSingleton().dispatchEventSoon(*this, eventNames().errorEvent); |
| } |
| |
| bool HTMLLinkElement::sheetLoaded() |
| { |
| if (!styleSheetIsLoading()) { |
| removePendingSheet(); |
| return true; |
| } |
| return false; |
| } |
| |
| void HTMLLinkElement::dispatchPendingLoadEvents(Page* page) |
| { |
| linkLoadEventSenderSingleton().dispatchPendingEvents(page); |
| } |
| |
| void HTMLLinkElement::dispatchPendingEvent(LinkEventSender* eventSender, const AtomString& eventType) |
| { |
| ASSERT_UNUSED(eventSender, eventSender == &linkLoadEventSenderSingleton()); |
| dispatchEvent(Event::create(eventType, Event::CanBubble::No, Event::IsCancelable::No)); |
| } |
| |
| DOMTokenList& HTMLLinkElement::relList() |
| { |
| if (!m_relList) |
| lazyInitialize(m_relList, makeUniqueWithoutRefCountedCheck<DOMTokenList>(*this, HTMLNames::relAttr, [](Document& document, StringView token) { |
| return LinkRelAttribute::isSupported(document, token); |
| })); |
| return *m_relList; |
| } |
| |
| // https://html.spec.whatwg.org/multipage/semantics.html#dom-link-blocking |
| DOMTokenList& HTMLLinkElement::blocking() |
| { |
| if (!m_blockingList) { |
| lazyInitialize(m_blockingList, makeUniqueWithoutRefCountedCheck<DOMTokenList>(*this, HTMLNames::blockingAttr, [](Document&, StringView token) { |
| if (equalLettersIgnoringASCIICase(token, "render"_s)) |
| return true; |
| return false; |
| })); |
| } |
| return *m_blockingList; |
| } |
| |
| void HTMLLinkElement::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred) |
| { |
| m_loadedResource = !errorOccurred; |
| linkLoadEventSenderSingleton().dispatchEventSoon(*this, m_loadedResource ? eventNames().loadEvent : eventNames().errorEvent); |
| } |
| |
| void HTMLLinkElement::startLoadingDynamicSheet() |
| { |
| // We don't support multiple active sheets. |
| ASSERT(m_pendingSheetType < PendingSheetType::Active); |
| addPendingSheet(PendingSheetType::Active); |
| } |
| |
| bool HTMLLinkElement::isURLAttribute(const Attribute& attribute) const |
| { |
| // FIXME: Should these be attribute.name().matches(...) to also enforce the namespace? |
| return hrefAttr->hasLocalName(attribute.name().localName()) |
| || HTMLElement::isURLAttribute(attribute); |
| } |
| |
| URL HTMLLinkElement::href() const |
| { |
| return protect(document())->encodingParseURL(attributeWithoutSynchronization(hrefAttr)); |
| } |
| |
| const AtomString& HTMLLinkElement::rel() const |
| { |
| return attributeWithoutSynchronization(relAttr); |
| } |
| |
| AtomString HTMLLinkElement::target() const |
| { |
| return attributeWithoutSynchronization(targetAttr); |
| } |
| |
| const AtomString& HTMLLinkElement::type() const |
| { |
| return attributeWithoutSynchronization(typeAttr); |
| } |
| |
| std::optional<LinkIconType> HTMLLinkElement::iconType() const |
| { |
| return m_relAttribute.iconType; |
| } |
| |
| static bool NODELETE mayFetchResource(LinkRelAttribute relAttribute) |
| { |
| // https://html.spec.whatwg.org/multipage/links.html#linkTypes |
| return relAttribute.isStyleSheet |
| || relAttribute.isLinkModulePreload |
| || relAttribute.isLinkPreload |
| #if ENABLE(APPLICATION_MANIFEST) |
| || relAttribute.isApplicationManifest |
| #endif |
| || !!relAttribute.iconType; |
| } |
| |
| void HTMLLinkElement::addSubresourceAttributeURLs(OrderedHashSet<URL>& urls) const |
| { |
| HTMLElement::addSubresourceAttributeURLs(urls); |
| |
| if (!mayFetchResource(m_relAttribute)) |
| return; |
| |
| // Append the URL of this link element. |
| addSubresourceURL(urls, href()); |
| |
| if (RefPtr styleSheet = this->sheet()) { |
| protect(styleSheet->contents())->traverseSubresources([&] (auto& resource) { |
| urls.add(resource.url()); |
| return false; |
| }); |
| } |
| } |
| |
| void HTMLLinkElement::addPendingSheet(PendingSheetType type) |
| { |
| if (type <= m_pendingSheetType) |
| return; |
| m_pendingSheetType = type; |
| |
| if (m_pendingSheetType == PendingSheetType::Inactive) |
| return; |
| protect(m_styleScope)->addPendingSheet(*this); |
| } |
| |
| void HTMLLinkElement::removePendingSheet() |
| { |
| PendingSheetType type = m_pendingSheetType; |
| m_pendingSheetType = PendingSheetType::Unknown; |
| |
| if (type == PendingSheetType::Unknown) |
| return; |
| |
| CheckedRef styleScope = *m_styleScope; |
| if (type == PendingSheetType::Inactive) { |
| // Document just needs to know about the sheet for exposure through document.styleSheets |
| styleScope->didChangeActiveStyleSheetCandidates(); |
| return; |
| } |
| |
| styleScope->removePendingSheet(*this); |
| } |
| |
| String HTMLLinkElement::referrerPolicyForBindings() const |
| { |
| return referrerPolicyToString(referrerPolicy()); |
| } |
| |
| ReferrerPolicy HTMLLinkElement::referrerPolicy() const |
| { |
| return parseReferrerPolicy(attributeWithoutSynchronization(referrerpolicyAttr), ReferrerPolicySource::ReferrerPolicyAttribute).value_or(ReferrerPolicy::EmptyString); |
| } |
| |
| String HTMLLinkElement::debugDescription() const |
| { |
| return makeString(HTMLElement::debugDescription(), ' ', type(), ' ', href().string()); |
| } |
| |
| String HTMLLinkElement::fetchPriorityForBindings() const |
| { |
| return convertEnumerationToString(fetchPriority()); |
| } |
| |
| RequestPriority HTMLLinkElement::fetchPriority() const |
| { |
| return parseEnumerationFromString<RequestPriority>(attributeWithoutSynchronization(fetchpriorityAttr)).value_or(RequestPriority::Auto); |
| } |
| |
| } // namespace WebCore |