1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004-2025 Apple 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 "CSSImageValue.h"
#include "CSSMarkup.h"
#include "CSSPrimitiveValue.h"
#include "CSSURLValue.h"
#include "CSSValueKeywords.h"
#include "CachedImage.h"
#include "CachedResourceLoader.h"
#include "CachedResourceRequest.h"
#include "CachedResourceRequestInitiatorTypes.h"
#include "DeprecatedCSSOMPrimitiveValue.h"
#include "Document.h"
#include "Element.h"
#include "StyleBuilderState.h"
#include "StyleCachedImage.h"
namespace WebCore {
CSSImageValue::CSSImageValue()
: CSSValue(ClassType::Image)
, m_isInvalid(true)
{
}
CSSImageValue::CSSImageValue(CSS::URL&& location, AtomString&& initiatorType)
: CSSValue(ClassType::Image)
, m_location(WTFMove(location))
, m_initiatorType(WTFMove(initiatorType))
{
}
Ref<CSSImageValue> CSSImageValue::create()
{
return adoptRef(*new CSSImageValue);
}
Ref<CSSImageValue> CSSImageValue::create(CSS::URL location, AtomString initiatorType)
{
return adoptRef(*new CSSImageValue(WTFMove(location), WTFMove(initiatorType)));
}
Ref<CSSImageValue> CSSImageValue::create(WTF::URL imageURL, AtomString initiatorType)
{
return create(CSS::URL { .specified = imageURL.string(), .resolved = WTFMove(imageURL), .modifiers = { } }, WTFMove(initiatorType));
}
CSSImageValue::~CSSImageValue() = default;
Ref<CSSImageValue> CSSImageValue::copyForComputedStyle(const CSS::URL& resolvedURL) const
{
if (resolvedURL == m_location)
return const_cast<CSSImageValue&>(*this);
auto result = create(resolvedURL);
result->m_cachedImage = m_cachedImage;
result->m_initiatorType = m_initiatorType;
result->m_unresolvedValue = const_cast<CSSImageValue*>(this);
return result;
}
bool CSSImageValue::isLoadedFromOpaqueSource() const
{
return m_location.modifiers.loadedFromOpaqueSource == LoadedFromOpaqueSource::Yes;
}
bool CSSImageValue::isPending() const
{
return !m_cachedImage;
}
RefPtr<StyleImage> CSSImageValue::createStyleImage(const Style::BuilderState& state) const
{
auto styleLocation = Style::toStyle(m_location, state);
if (styleLocation.resolved == m_location.resolved)
return StyleCachedImage::create(WTFMove(styleLocation), const_cast<CSSImageValue&>(*this));
// FIXME: This case can only happen when a element from a document with no baseURL has an inline style with a relative image URL in it and has been moved to a document with a non-null baseURL. Instead of re-resolving in this case, moved elements with this kind of inline style should have their inline style re-parsed.
auto newLocation = m_location;
newLocation.resolved = styleLocation.resolved;
auto result = create(WTFMove(newLocation));
result->m_cachedImage = m_cachedImage;
result->m_initiatorType = m_initiatorType;
result->m_unresolvedValue = const_cast<CSSImageValue*>(this);
return StyleCachedImage::create(WTFMove(styleLocation), WTFMove(result));
}
CachedImage* CSSImageValue::loadImage(CachedResourceLoader& loader, const ResourceLoaderOptions& options)
{
if (!m_cachedImage) {
ASSERT(loader.document());
ResourceLoaderOptions loadOptions = options;
CSS::applyModifiersToLoaderOptions(m_location.modifiers, loadOptions);
CachedResourceRequest request(ResourceRequest(URL { m_location.resolved }), loadOptions);
if (m_initiatorType.isEmpty())
request.setInitiatorType(cachedResourceRequestInitiatorTypes().css);
else
request.setInitiatorType(m_initiatorType);
if (options.mode == FetchOptions::Mode::Cors)
request.updateForAccessControl(*loader.document());
m_cachedImage = loader.requestImage(WTFMove(request)).value_or(nullptr);
for (auto imageValue = this; (imageValue = imageValue->m_unresolvedValue.get()); )
imageValue->m_cachedImage = m_cachedImage;
}
return m_cachedImage.value().get();
}
bool CSSImageValue::customTraverseSubresources(NOESCAPE const Function<bool(const CachedResource&)>& handler) const
{
return m_cachedImage && *m_cachedImage && handler(**m_cachedImage);
}
bool CSSImageValue::customMayDependOnBaseURL() const
{
return WebCore::CSS::mayDependOnBaseURL(m_location);
}
bool CSSImageValue::equals(const CSSImageValue& other) const
{
return m_location == other.m_location;
}
String CSSImageValue::customCSSText(const CSS::SerializationContext& context) const
{
if (m_isInvalid)
return ""_s;
return CSS::serializationForCSS(context, m_location);
}
Ref<DeprecatedCSSOMValue> CSSImageValue::createDeprecatedCSSOMWrapper(CSSStyleDeclaration& styleDeclaration) const
{
// We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
return DeprecatedCSSOMPrimitiveValue::create(CSSURLValue::create(m_location), styleDeclaration);
}
bool CSSImageValue::knownToBeOpaque(const RenderElement& renderer) const
{
return m_cachedImage.value_or(nullptr) && (**m_cachedImage).currentFrameKnownToBeOpaque(&renderer);
}
} // namespace WebCore
|