Skip to content
Permalink
Browse files
[MQ4] Replace the last uses of legacy media query code
https://bugs.webkit.org/show_bug.cgi?id=248594
rdar://102853711

Reviewed by Darin Adler

Use the modern parser and evaluator.

* Source/WebCore/css/LegacyMediaQueryEvaluator.cpp:
(WebCore::mediaQueryViewportStateForDocument): Deleted.
* Source/WebCore/css/LegacyMediaQueryEvaluator.h:
* Source/WebCore/css/parser/SizesAttributeParser.cpp:
(WebCore::SizesAttributeParser::parse):
* Source/WebCore/html/HTMLLinkElement.h:
* Source/WebCore/html/parser/HTMLPreloadScanner.cpp:
(WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):
* Source/WebCore/html/parser/HTMLResourcePreloader.cpp:
(WebCore::HTMLResourcePreloader::preload):
* Source/WebCore/loader/LinkLoader.cpp:
(WebCore::LinkLoader::preloadIfNeeded):
* Source/WebCore/style/RuleSet.cpp:
(WebCore::Style::RuleSet::evaluateDynamicMediaQueryRules):
* Source/WebCore/style/StyleResolver.cpp:
* Source/WebCore/style/StyleScope.cpp:
(WebCore::Style::Scope::mediaQueryViewportStateForDocument):
* Source/WebCore/style/StyleScope.h:
* Source/WebKitLegacy/mac/DOM/DOM.mm:
(-[DOMHTMLLinkElement _mediaQueryMatches]):

Canonical link: https://commits.webkit.org/257252@main
  • Loading branch information
anttijk committed Dec 1, 2022
1 parent bb23e4e commit be64fdb44bd3c123ed5408b1491d55e6795e705d
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 36 deletions.
@@ -98,12 +98,6 @@ static bool isAppearanceDependent(const AtomString& mediaFeature)
return mediaFeature == MediaFeatureNames::prefersDarkInterface;
}

MediaQueryViewportState mediaQueryViewportStateForDocument(const Document& document)
{
// These things affect evaluation of viewport dependent media queries.
return { document.view()->layoutSize(), document.frame()->pageZoomFactor(), document.printing() };
}

LegacyMediaQueryEvaluator::LegacyMediaQueryEvaluator(bool mediaFeatureResult)
: m_fallbackResult(mediaFeatureResult)
{
@@ -44,10 +44,6 @@ struct MediaQueryResult {
bool result;
};

using MediaQueryViewportState = std::tuple<IntSize, float, bool>;

MediaQueryViewportState mediaQueryViewportStateForDocument(const Document&);

// Some of the constructors are used for cases where the device characteristics are not known.
// These can be used to prune the loading of stylesheets to only those which are not already known to not match.

@@ -136,7 +136,7 @@ bool SizesAttributeParser::parse(CSSParserTokenRange range)
if (!mediaCondition)
continue;
bool matches = mediaConditionMatches(*mediaCondition);
MQ::MediaQueryEvaluator evaluator { screenAtom(), m_document, nullptr };
MQ::MediaQueryEvaluator evaluator { screenAtom() };
if (!evaluator.collectDynamicDependencies(*mediaCondition).isEmpty())
m_dynamicMediaQueryResults.append({ MQ::MediaQueryList { *mediaCondition }, matches });
if (!matches)
@@ -69,7 +69,7 @@ class HTMLLinkElement final : public HTMLElement, public CachedStyleSheetClient,
bool isEnabledViaScript() const { return m_disabledState == EnabledViaScript; }
DOMTokenList& sizes();

bool mediaAttributeMatches() const;
WEBCORE_EXPORT bool mediaAttributeMatches() const;

WEBCORE_EXPORT void setCrossOrigin(const AtomString&);
WEBCORE_EXPORT String crossOrigin() const;
@@ -34,13 +34,13 @@
#include "HTMLSrcsetParser.h"
#include "HTMLTokenizer.h"
#include "InputTypeNames.h"
#include "LegacyMediaQueryEvaluator.h"
#include "LinkLoader.h"
#include "LinkRelAttribute.h"
#include "Logging.h"
#include "MIMETypeRegistry.h"
#include "MediaList.h"
#include "MediaQueryParserContext.h"
#include "MediaQueryEvaluator.h"
#include "MediaQueryParser.h"
#include "RenderView.h"
#include "SecurityPolicy.h"
#include "Settings.h"
@@ -227,10 +227,10 @@ class TokenPreloadScanner::StartTagScanner {
}
if (match(attributeName, mediaAttr) && m_mediaAttribute.isNull()) {
m_mediaAttribute = attributeValue.toString();
auto mediaSet = MediaQuerySet::create(m_mediaAttribute, MediaQueryParserContext(m_document));
auto mediaQueries = MQ::MediaQueryParser::parse(m_mediaAttribute, { m_document });
RefPtr documentElement = m_document.documentElement();
LOG(MediaQueries, "HTMLPreloadScanner %p processAttribute evaluating media queries", this);
m_mediaMatched = LegacyMediaQueryEvaluator { m_document.printing() ? "print"_s : "screen"_s, m_document, documentElement ? documentElement->computedStyle() : nullptr }.evaluate(mediaSet.get());
m_mediaMatched = MQ::MediaQueryEvaluator { m_document.printing() ? printAtom() : screenAtom(), m_document, documentElement ? documentElement->computedStyle() : nullptr }.evaluate(mediaQueries);
}
if (match(attributeName, typeAttr) && m_typeAttribute.isNull()) {
// when multiple type attributes present: first value wins, ignore subsequent (to match ImageElement parser and Blink behaviours)
@@ -30,7 +30,9 @@
#include "CrossOriginAccessControl.h"
#include "DefaultResourceLoadPriority.h"
#include "Document.h"
#include "LegacyMediaQueryEvaluator.h"
#include "MediaQueryEvaluator.h"
#include "MediaQueryParser.h"
#include "NodeRenderStyle.h"
#include "RenderView.h"
#include "ScriptElementCachedScriptFetcher.h"

@@ -81,11 +83,12 @@ void HTMLResourcePreloader::preload(std::unique_ptr<PreloadRequest> preload)
{
ASSERT(m_document.frame());
ASSERT(m_document.renderView());
if (!preload->media().isEmpty() && !LegacyMediaQueryEvaluator::mediaAttributeMatches(m_document, preload->media()))

auto queries = MQ::MediaQueryParser::parse(preload->media(), { m_document });
if (!MQ::MediaQueryEvaluator { screenAtom(), m_document, m_document.renderStyle() }.evaluate(queries))
return;

m_document.cachedResourceLoader().preload(preload->resourceType(), preload->resourceRequest(m_document));
}


}
@@ -48,13 +48,14 @@
#include "FrameView.h"
#include "HTMLSrcsetParser.h"
#include "JSFetchRequestDestination.h"
#include "LegacyMediaQueryEvaluator.h"
#include "LinkHeader.h"
#include "LinkPreloadResourceClients.h"
#include "LinkRelAttribute.h"
#include "LoaderStrategy.h"
#include "MIMETypeRegistry.h"
#include "MediaList.h"
#include "MediaQueryEvaluator.h"
#include "MediaQueryParser.h"
#include "NodeRenderStyle.h"
#include "PlatformStrategies.h"
#include "ResourceError.h"
#include "Settings.h"
@@ -305,7 +306,8 @@ std::unique_ptr<LinkPreloadResourceClient> LinkLoader::preloadIfNeeded(const Lin
document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, "<link rel=preload> has an invalid `imagesrcset` value"_s);
return nullptr;
}
if (!LegacyMediaQueryEvaluator::mediaAttributeMatches(document, params.media))
auto queries = MQ::MediaQueryParser::parse(params.media, { document });
if (!MQ::MediaQueryEvaluator { screenAtom(), document, document.renderStyle() }.evaluate(queries))
return nullptr;
if (!isSupportedType(type.value(), params.mimeType, document))
return nullptr;
@@ -350,7 +350,7 @@ std::optional<DynamicMediaQueryEvaluationChanges> RuleSet::evaluateDynamicMediaQ

auto& ruleSet = m_mediaQueryInvalidationRuleSetCache.ensure(collectedChanges.changedQueryIndexes, [&] {
auto ruleSet = RuleSet::create();
RuleSetBuilder builder(ruleSet, MQ::MediaQueryEvaluator("screen"_s, MQ::EvaluationResult::True));
RuleSetBuilder builder(ruleSet, MQ::MediaQueryEvaluator { screenAtom(), MQ::EvaluationResult::True });
for (auto* rules : collectedChanges.affectedRules) {
for (auto& rule : *rules)
builder.addStyleRule(rule);
@@ -47,7 +47,6 @@
#include "FrameView.h"
#include "InspectorInstrumentation.h"
#include "KeyframeList.h"
#include "LegacyMediaQueryEvaluator.h"
#include "Logging.h"
#include "MediaList.h"
#include "NodeRenderStyle.h"
@@ -699,6 +699,12 @@ void Scope::scheduleUpdate(UpdateType update)
m_pendingUpdateTimer.startOneShot(0_s);
}

auto Scope::mediaQueryViewportStateForDocument(const Document& document) -> MediaQueryViewportState
{
// These things affect evaluation of viewport dependent media queries.
return { document.view()->layoutSize(), document.frame()->pageZoomFactor(), document.printing() };
}

void Scope::evaluateMediaQueriesForViewportChange()
{
auto viewportState = mediaQueryViewportStateForDocument(m_document);
@@ -28,7 +28,6 @@
#pragma once

#include "LayoutSize.h"
#include "LegacyMediaQueryEvaluator.h"
#include "StyleScopeOrdinal.h"
#include "Timer.h"
#include <memory>
@@ -188,6 +187,9 @@ class Scope : public CanMakeWeakPtr<Scope> {

TreeScope& treeScope();

using MediaQueryViewportState = std::tuple<IntSize, float, bool>;
static MediaQueryViewportState mediaQueryViewportStateForDocument(const Document&);

Document& m_document;
ShadowRoot* m_shadowRoot { nullptr };

@@ -50,8 +50,6 @@
#import <WebCore/Image.h>
#import <WebCore/JSNode.h>
#import <WebCore/KeyboardEvent.h>
#import <WebCore/LegacyMediaQueryEvaluator.h>
#import <WebCore/MediaList.h>
#import <WebCore/NodeFilter.h>
#import <WebCore/NodeRenderStyle.h>
#import <WebCore/Page.h>
@@ -708,15 +706,7 @@ - (BOOL)_mediaQueryMatchesForOrientation:(int)orientation

- (BOOL)_mediaQueryMatches
{
HTMLLinkElement& link = *static_cast<HTMLLinkElement*>(core(self));

auto& media = link.attributeWithoutSynchronization(HTMLNames::mediaAttr);
if (media.isEmpty())
return true;

Document& document = link.document();
auto mediaQuerySet = MediaQuerySet::create(media, MediaQueryParserContext(document));
return LegacyMediaQueryEvaluator { "screen"_s, document, document.renderView() ? &document.renderView()->style() : nullptr }.evaluate(mediaQuerySet.get());
return downcast<HTMLLinkElement>(core(self))->mediaAttributeMatches();
}

@end

0 comments on commit be64fdb

Please sign in to comment.