Skip to content
Permalink
Browse files
Add generic media query parser and evaluator
https://bugs.webkit.org/show_bug.cgi?id=243771

Reviewed by Alan Bujtas.

Generalize container query parsing and evaluation code so it can be used for media queries too in the future.

This patch adds generic parser and evaluator classes and moves condition handling there.

* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/css/ContainerQuery.cpp:
(WebCore::CQ::serialize):
* Source/WebCore/css/ContainerQuery.h:
* Source/WebCore/css/ContainerQueryParser.cpp:
(WebCore::ContainerQueryParser::consumeCondition): Deleted.
* Source/WebCore/css/ContainerQueryParser.h:
* Source/WebCore/css/query/GenericMediaQueryEvaluator.h: Added.
(WebCore::MQ::GenericMediaQueryEvaluator::concreteEvaluator const):
(WebCore::MQ::GenericMediaQueryEvaluator<ConcreteEvaluator>::evaluateCondition const):
(WebCore::MQ::operator&):
(WebCore::MQ::operator!):
(WebCore::MQ::toEvaluationResult):
* Source/WebCore/css/query/GenericMediaQueryParser.h: Added.
(WebCore::MQ::GenericMediaQueryParser::concreteParser):
(WebCore::MQ::GenericMediaQueryParser<ConcreteParser>::consumeCondition):
* Source/WebCore/css/query/GenericMediaQueryTypes.h: Copied from Source/WebCore/css/ContainerQueryParser.h.
* Source/WebCore/style/ContainerQueryEvaluator.cpp:
(WebCore::Style::ContainerQueryEvaluator::evaluate const):
(WebCore::Style::ContainerQueryEvaluator::evaluateQueryInParens const):
(WebCore::Style::ContainerQueryEvaluator::evaluateSizeFeature const):
(WebCore::Style::ContainerQueryEvaluator::evaluateCondition const): Deleted.
* Source/WebCore/style/ContainerQueryEvaluator.h:
(WebCore::Style::toEvaluationResult): Deleted.
(WebCore::Style::operator&): Deleted.
(WebCore::Style::operator!): Deleted.

Canonical link: https://commits.webkit.org/253298@main
  • Loading branch information
anttijk committed Aug 10, 2022
1 parent ea06abc commit e923d76d5954a6c20e6ebb7b8c2e92890a834402
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 158 deletions.
@@ -87,6 +87,7 @@ set(WebCore_PRIVATE_INCLUDE_DIRECTORIES
"${WEBCORE_DIR}/css"
"${WEBCORE_DIR}/css/calc"
"${WEBCORE_DIR}/css/parser"
"${WEBCORE_DIR}/css/query"
"${WEBCORE_DIR}/css/typedom"
"${WEBCORE_DIR}/css/typedom/color"
"${WEBCORE_DIR}/css/typedom/numeric"
@@ -601,6 +601,8 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
css/parser/CSSParserToken.h
css/parser/CSSParserTokenRange.h

css/query/GenericMediaQueryTypes.h

cssjit/CompiledSelector.h

display/DisplayTree.h
@@ -5506,6 +5506,9 @@
E4D68EB517B4DBDC00CBDCA8 /* StyleResolveForFontRaw.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D68EB317B4DBDC00CBDCA8 /* StyleResolveForFontRaw.h */; };
E4D86E6D2640394C00B62425 /* StyleScopeOrdinal.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D86E6B2640394C00B62425 /* StyleScopeOrdinal.h */; settings = {ATTRIBUTES = (Private, ); }; };
E4D988B417BFD1F60084FB88 /* TextNodeTraversal.h in Headers */ = {isa = PBXBuildFile; fileRef = E4D988B317BFD1F60084FB88 /* TextNodeTraversal.h */; };
E4DA5E36289D242000849BCF /* GenericMediaQueryParser.h in Headers */ = {isa = PBXBuildFile; fileRef = E4DA5E34289D23F300849BCF /* GenericMediaQueryParser.h */; };
E4DA5E3D28A2805700849BCF /* GenericMediaQueryTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = E4DA5E35289D23FD00849BCF /* GenericMediaQueryTypes.h */; settings = {ATTRIBUTES = (Private, ); }; };
E4DA5E4028A288F800849BCF /* GenericMediaQueryEvaluator.h in Headers */ = {isa = PBXBuildFile; fileRef = E4DA5E3F28A288F800849BCF /* GenericMediaQueryEvaluator.h */; };
E4DACE6A1D12E10B0075980F /* StylePendingResources.h in Headers */ = {isa = PBXBuildFile; fileRef = E4DACE691D12E10B0075980F /* StylePendingResources.h */; };
E4DEAA1817A93DC3000E0430 /* StyleTreeResolver.h in Headers */ = {isa = PBXBuildFile; fileRef = E4DEAA1617A93DC3000E0430 /* StyleTreeResolver.h */; settings = {ATTRIBUTES = (Private, ); }; };
E4E39AFB1330EFA8003AB274 /* LegacyTileLayerPool.h in Headers */ = {isa = PBXBuildFile; fileRef = E4E39AFA1330EFA8003AB274 /* LegacyTileLayerPool.h */; };
@@ -17890,6 +17893,9 @@
E4D86E6B2640394C00B62425 /* StyleScopeOrdinal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StyleScopeOrdinal.h; sourceTree = "<group>"; };
E4D988B317BFD1F60084FB88 /* TextNodeTraversal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextNodeTraversal.h; sourceTree = "<group>"; };
E4D988B517BFEB210084FB88 /* TextNodeTraversal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextNodeTraversal.cpp; sourceTree = "<group>"; };
E4DA5E34289D23F300849BCF /* GenericMediaQueryParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GenericMediaQueryParser.h; sourceTree = "<group>"; };
E4DA5E35289D23FD00849BCF /* GenericMediaQueryTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GenericMediaQueryTypes.h; sourceTree = "<group>"; };
E4DA5E3F28A288F800849BCF /* GenericMediaQueryEvaluator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GenericMediaQueryEvaluator.h; sourceTree = "<group>"; };
E4DACE691D12E10B0075980F /* StylePendingResources.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StylePendingResources.h; sourceTree = "<group>"; };
E4DACE6B1D12E1160075980F /* StylePendingResources.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StylePendingResources.cpp; sourceTree = "<group>"; };
E4DEAA1517A93DC3000E0430 /* StyleTreeResolver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StyleTreeResolver.cpp; sourceTree = "<group>"; };
@@ -32037,6 +32043,16 @@
path = updating;
sourceTree = "<group>";
};
E4DA5E3E28A2867300849BCF /* query */ = {
isa = PBXGroup;
children = (
E4DA5E3F28A288F800849BCF /* GenericMediaQueryEvaluator.h */,
E4DA5E34289D23F300849BCF /* GenericMediaQueryParser.h */,
E4DA5E35289D23FD00849BCF /* GenericMediaQueryTypes.h */,
);
path = query;
sourceTree = "<group>";
};
E4FB4B1E2395356F003C336A /* integration */ = {
isa = PBXGroup;
children = (
@@ -32174,6 +32190,7 @@
children = (
BCF1644D26629F9A0002F7EF /* calc */,
946D37271D6CB2250077084F /* parser */,
E4DA5E3E28A2867300849BCF /* query */,
4BAFD0DA21921EAD00C0AB64 /* typedom */,
FBD6AF8415EF21D4008B7110 /* BasicShapeFunctions.cpp */,
FBD6AF8515EF21D4008B7110 /* BasicShapeFunctions.h */,
@@ -35286,6 +35303,9 @@
C0C054CC1118C8E400CE2636 /* generate-bindings.pl in Headers */,
BC23F0DB0DAFF4A4009FDC91 /* GeneratedImage.h in Headers */,
830030F61B7D33B500ED3AAC /* GenericCachedHTMLCollection.h in Headers */,
E4DA5E4028A288F800849BCF /* GenericMediaQueryEvaluator.h in Headers */,
E4DA5E36289D242000849BCF /* GenericMediaQueryParser.h in Headers */,
E4DA5E3D28A2805700849BCF /* GenericMediaQueryTypes.h in Headers */,
9746AF2414F4DDE6003E7A70 /* Geolocation.h in Headers */,
9746AF2514F4DDE6003E7A71 /* GeolocationClient.h in Headers */,
9746AF2714F4DDE6003E7A70 /* GeolocationController.h in Headers */,
@@ -173,15 +173,15 @@ void serialize(StringBuilder& builder, const SizeFeature& sizeFeature)
template<typename ConditionType>
void serialize(StringBuilder& builder, const ConditionType& condition)
{
if (condition.queries.size() == 1 && condition.logicalOperator == LogicalOperator::Not) {
if (condition.queries.size() == 1 && condition.logicalOperator == MQ::LogicalOperator::Not) {
builder.append("not ");
serialize(builder, condition.queries.first());
return;
}

for (auto& query : condition.queries) {
if (&query != &condition.queries.first())
builder.append(condition.logicalOperator == LogicalOperator::And ? " and " : " or ");
builder.append(condition.logicalOperator == MQ::LogicalOperator::And ? " and " : " or ");
serialize(builder, query);
}
}
@@ -24,6 +24,7 @@

#pragma once

#include "GenericMediaQueryTypes.h"
#include <wtf/Forward.h>
#include <wtf/OptionSet.h>
#include <wtf/text/AtomString.h>
@@ -46,12 +47,11 @@ struct UnknownQuery {

using QueryInParens = std::variant<ContainerCondition, SizeFeature, UnknownQuery>;

enum class LogicalOperator : uint8_t { And, Or, Not };
enum class ComparisonOperator : uint8_t { LessThan, LessThanOrEqual, Equal, GreaterThan, GreaterThanOrEqual };
enum class Syntax : uint8_t { Boolean, Colon, Range };

struct ContainerCondition {
LogicalOperator logicalOperator { LogicalOperator::And };
MQ::LogicalOperator logicalOperator { MQ::LogicalOperator::And };
Vector<QueryInParens> queries;
};

@@ -87,64 +87,6 @@ std::optional<CQ::QueryInParens> ContainerQueryParser::consumeQueryInParens(CSSP
return { };
}

template<typename ConditionType>
std::optional<ConditionType> ContainerQueryParser::consumeCondition(CSSParserTokenRange& range)
{
auto consumeQuery = [&](CSSParserTokenRange& range) {
if constexpr (std::is_same_v<CQ::ContainerCondition, ConditionType>)
return consumeQueryInParens(range);
// Style query support would be here.
};

if (range.peek().type() == IdentToken) {
if (range.peek().id() == CSSValueNot) {
range.consumeIncludingWhitespace();
if (auto query = consumeQuery(range))
return ConditionType { CQ::LogicalOperator::Not, { *query } };
return { };
}
}

ConditionType condition;

auto query = consumeQuery(range);
if (!query)
return { };

condition.queries.append(*query);
range.consumeWhitespace();

auto consumeOperator = [&]() -> std::optional<CQ::LogicalOperator> {
auto operatorToken = range.consumeIncludingWhitespace();
if (operatorToken.type() != IdentToken)
return { };
if (operatorToken.id() == CSSValueAnd)
return CQ::LogicalOperator::And;
if (operatorToken.id() == CSSValueOr)
return CQ::LogicalOperator::Or;
return { };
};

while (!range.atEnd()) {
auto op = consumeOperator();
if (!op)
return { };
if (condition.queries.size() > 1 && condition.logicalOperator != *op)
return { };

condition.logicalOperator = *op;

auto query = consumeQuery(range);
if (!query)
return { };

condition.queries.append(*query);
range.consumeWhitespace();
}

return condition;
}

std::optional<CQ::SizeFeature> ContainerQueryParser::consumeSizeFeature(CSSParserTokenRange& range)
{
auto consume = [&] {
@@ -27,24 +27,24 @@
#include "CSSParserContext.h"
#include "CSSParserToken.h"
#include "ContainerQuery.h"
#include "GenericMediaQueryParser.h"

namespace WebCore {

class CSSParserTokenRange;
class ContainerQueryParser;

class ContainerQueryParser {
class ContainerQueryParser : public MQ::GenericMediaQueryParser<ContainerQueryParser> {
public:
static std::optional<CQ::ContainerQuery> consumeContainerQuery(CSSParserTokenRange&, const CSSParserContext&);

private:
std::optional<CQ::ContainerQuery> consumeContainerQuery(CSSParserTokenRange&);
std::optional<CQ::QueryInParens> consumeQueryInParens(CSSParserTokenRange&);
template<typename ConditionType> std::optional<ConditionType> consumeCondition(CSSParserTokenRange&);
std::optional<CQ::SizeFeature> consumeSizeFeature(CSSParserTokenRange&);
std::optional<CQ::SizeFeature> consumePlainSizeFeature(CSSParserTokenRange&);
std::optional<CQ::SizeFeature> consumeRangeSizeFeature(CSSParserTokenRange&);
RefPtr<CSSValue> consumeValue(CSSParserTokenRange&);

private:
ContainerQueryParser(const CSSParserContext& context)
: m_context(context) { }

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

namespace WebCore {
namespace MQ {

enum class EvaluationResult : uint8_t { False, True, Unknown };

template<typename ConcreteEvaluator>
class GenericMediaQueryEvaluator {
public:
template<typename ConditionType, typename Context> EvaluationResult evaluateCondition(const ConditionType&, const Context&) const;

private:
const ConcreteEvaluator& concreteEvaluator() const { return static_cast<const ConcreteEvaluator&>(*this); }
};

template<typename ConcreteEvaluator>
template<typename ConditionType, typename Context>
EvaluationResult GenericMediaQueryEvaluator<ConcreteEvaluator>::evaluateCondition(const ConditionType& condition, const Context& context) const
{
if (condition.queries.isEmpty())
return EvaluationResult::Unknown;

switch (condition.logicalOperator) {
case LogicalOperator::Not:
return !concreteEvaluator().evaluateQueryInParens(condition.queries.first(), context);

case LogicalOperator::And: {
auto result = EvaluationResult::True;
for (auto query : condition.queries) {
auto queryResult = concreteEvaluator().evaluateQueryInParens(query, context);
if (queryResult == EvaluationResult::False)
return EvaluationResult::False;
if (queryResult == EvaluationResult::Unknown)
result = EvaluationResult::Unknown;
}
return result;
}

case LogicalOperator::Or: {
auto result = EvaluationResult::False;
for (auto query : condition.queries) {
auto queryResult = concreteEvaluator().evaluateQueryInParens(query, context);
if (queryResult == EvaluationResult::True)
return EvaluationResult::True;
if (queryResult == EvaluationResult::Unknown)
result = EvaluationResult::Unknown;
}
return result;
}
}
RELEASE_ASSERT_NOT_REACHED();
}

inline EvaluationResult operator&(EvaluationResult left, EvaluationResult right)
{
if (left == EvaluationResult::Unknown || right == EvaluationResult::Unknown)
return EvaluationResult::Unknown;
if (left == EvaluationResult::True && right == EvaluationResult::True)
return EvaluationResult::True;
return EvaluationResult::False;
}

inline EvaluationResult operator!(EvaluationResult result)
{
switch (result) {
case EvaluationResult::True:
return EvaluationResult::False;
case EvaluationResult::False:
return EvaluationResult::True;
case EvaluationResult::Unknown:
return EvaluationResult::Unknown;
}
RELEASE_ASSERT_NOT_REACHED();
}

inline EvaluationResult toEvaluationResult(bool boolean)
{
return boolean ? EvaluationResult::True : EvaluationResult::False;
}

}
}

0 comments on commit e923d76

Please sign in to comment.