Title: [289742] trunk
Revision
289742
Author
[email protected]
Date
2022-02-14 11:05:56 -0800 (Mon, 14 Feb 2022)

Log Message

[CSS Container Queries] Implement full query parser and evaluator
https://bugs.webkit.org/show_bug.cgi?id=236580

Reviewed by Sam Weinig.

LayoutTests/imported/w3c:

* web-platform-tests/css/css-contain/container-queries/at-container-parsing-expected.txt:
* web-platform-tests/css/css-contain/container-queries/container-for-shadow-dom.tentative-expected.txt:
* web-platform-tests/css/css-contain/container-queries/container-type-invalidation-expected.txt:
* web-platform-tests/css/css-contain/container-queries/query-evaluation-expected.txt:
* web-platform-tests/css/css-contain/container-queries/size-feature-evaluation-expected.txt:

Source/WebCore:

So far we have been using the media query parser to parse container queries.
It does not support nesting and other necessary features. Also the produced data
structures are awkward to evaluate.

https://drafts.csswg.org/css-contain-3/#container-rule

This patch supports size queries but not style queries. It supports both the current spec
size query syntax "size(foo)" (used in WPTs), and the non-function POR syntax from
https://github.com/w3c/csswg-drafts/issues/6870#issuecomment-1022430911.

* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* css/ContainerQuery.h:
* css/ContainerQueryParser.cpp: Added.
(WebCore::ContainerQueryParser::parse):
(WebCore::ContainerQueryParser::consumeContainerQuery):
(WebCore::ContainerQueryParser::consumeCondition):
(WebCore::ContainerQueryParser::consumeSizeQuery):

No support for range operators ('<' etc) yet.

* css/ContainerQueryParser.h: Copied from Source/WebCore/css/ContainerQuery.h.
(WebCore::ContainerQueryParser::ContainerQueryParser):
* css/parser/CSSParserImpl.cpp:
(WebCore::CSSParserImpl::consumeContainerRule):
* style/ContainerQueryEvaluator.cpp:
(WebCore::Style::computeSize):
(WebCore::Style::ContainerQueryEvaluator::evaluate const):
(WebCore::Style::ContainerQueryEvaluator::evaluateQuery const):
(WebCore::Style::ContainerQueryEvaluator::evaluateCondition const):
(WebCore::Style::ContainerQueryEvaluator::evaluateSizeFeature const):

Just 'width' and 'height' for now, no new size features yet.

* style/ContainerQueryEvaluator.h:

LayoutTests:

* TestExpectations:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (289741 => 289742)


--- trunk/LayoutTests/ChangeLog	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/ChangeLog	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,3 +1,12 @@
+2022-02-14  Antti Koivisto  <[email protected]>
+
+        [CSS Container Queries] Implement full query parser and evaluator
+        https://bugs.webkit.org/show_bug.cgi?id=236580
+
+        Reviewed by Sam Weinig.
+
+        * TestExpectations:
+
 2022-02-14  Frédéric Wang  <[email protected]>
 
         Nullptr crash in CompositeEditCommand::splitTreeToNode via InsertParagraphSeparatorCommand::doApply

Modified: trunk/LayoutTests/TestExpectations (289741 => 289742)


--- trunk/LayoutTests/TestExpectations	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/TestExpectations	2022-02-14 19:05:56 UTC (rev 289742)
@@ -4757,6 +4757,7 @@
 webkit.org/b/229659 imported/w3c/web-platform-tests/css/css-contain/container-queries/display-in-container.html [ ImageOnlyFailure ]
 webkit.org/b/229659 imported/w3c/web-platform-tests/css/css-contain/container-queries/pseudo-elements-002.tentative.html [ ImageOnlyFailure ]
 webkit.org/b/229659 imported/w3c/web-platform-tests/css/css-contain/container-queries/multicol-inside-container.html [ ImageOnlyFailure ]
+webkit.org/b/229659 imported/w3c/web-platform-tests/css/css-contain/container-queries/inline-size-bfc-floats.html [ ImageOnlyFailure ]
 
 # Flaky css-contain test
 imported/w3c/web-platform-tests/css/css-contain/content-visibility/animation-display-lock.html [ Failure Pass ]

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (289741 => 289742)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,3 +1,16 @@
+2022-02-14  Antti Koivisto  <[email protected]>
+
+        [CSS Container Queries] Implement full query parser and evaluator
+        https://bugs.webkit.org/show_bug.cgi?id=236580
+
+        Reviewed by Sam Weinig.
+
+        * web-platform-tests/css/css-contain/container-queries/at-container-parsing-expected.txt:
+        * web-platform-tests/css/css-contain/container-queries/container-for-shadow-dom.tentative-expected.txt:
+        * web-platform-tests/css/css-contain/container-queries/container-type-invalidation-expected.txt:
+        * web-platform-tests/css/css-contain/container-queries/query-evaluation-expected.txt:
+        * web-platform-tests/css/css-contain/container-queries/size-feature-evaluation-expected.txt:
+
 2022-02-14  Martin Robinson  <[email protected]>
 
         Animation from "scale()" to "scale() translate()" does not yield the expected result

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/at-container-parsing-expected.txt (289741 => 289742)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/at-container-parsing-expected.txt	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/at-container-parsing-expected.txt	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,14 +1,14 @@
 
-FAIL size(width) assert_equals: expected "true" but got ""
-FAIL size(min-width: 0px) assert_equals: expected "true" but got ""
-FAIL size(max-width: 0px) assert_equals: expected "true" but got ""
-FAIL size(height) assert_equals: expected "true" but got ""
-FAIL size(min-height: 0px) assert_equals: expected "true" but got ""
-FAIL size(max-height: 0px) assert_equals: expected "true" but got ""
+PASS size(width)
+PASS size(min-width: 0px)
+PASS size(max-width: 0px)
+PASS size(height)
+PASS size(min-height: 0px)
+PASS size(max-height: 0px)
 FAIL size(aspect-ratio) assert_equals: expected "true" but got ""
-FAIL size(min-aspect-ratio: 1/2) assert_equals: expected "true" but got ""
-FAIL size(max-aspect-ratio: 1/2) assert_equals: expected "true" but got ""
-FAIL size(orientation: portrait) assert_equals: expected "true" but got ""
+FAIL size(min-aspect-ratio: 1/2) assert_equals: expected 2 but got 0
+FAIL size(max-aspect-ratio: 1/2) assert_equals: expected 2 but got 0
+FAIL size(orientation: portrait) assert_equals: expected 2 but got 0
 FAIL size(inline-size) assert_equals: expected "true" but got ""
 FAIL size(min-inline-size: 0px) assert_equals: expected "true" but got ""
 FAIL size(max-inline-size: 0px) assert_equals: expected "true" but got ""
@@ -15,22 +15,22 @@
 FAIL size(block-size) assert_equals: expected "true" but got ""
 FAIL size(min-block-size: 0px) assert_equals: expected "true" but got ""
 FAIL size(max-block-size: 0px) assert_equals: expected "true" but got ""
-FAIL size(width: 100px) assert_equals: expected "true" but got ""
-FAIL size((width: 100px)) assert_equals: expected "true" but got ""
-FAIL size(not (width: 100px)) assert_equals: expected "true" but got ""
-FAIL size((width: 100px) and (height: 100px)) assert_equals: expected "true" but got ""
-FAIL size((width: 50px) or (height: 100px)) assert_equals: expected "true" but got ""
-FAIL size(width < 100px) assert_equals: expected "true" but got ""
-FAIL size(100px < width) assert_equals: expected "true" but got ""
-FAIL size(100px < width < 200px) assert_equals: expected "true" but got ""
+PASS size(width: 100px)
+PASS size((width: 100px))
+PASS size(not (width: 100px))
+PASS size((width: 100px) and (height: 100px))
+PASS size((width: 50px) or (height: 100px))
+PASS size(width < 100px)
+FAIL size(100px < width) assert_equals: expected 2 but got 0
+FAIL size(100px < width < 200px) assert_equals: expected 2 but got 0
 PASS foo(width)
 PASS size(asdf)
-PASS size(resolution > 100dpi)
-PASS size(resolution: 150dpi)
+FAIL size(resolution > 100dpi) assert_equals: expected 2 but got 0
+FAIL size(resolution: 150dpi) assert_equals: expected 2 but got 0
 PASS size(color)
-PASS size(min-color: 1)
-PASS size(color-index >= 1)
-PASS (color-index >= 1)
+FAIL size(min-color: 1) assert_equals: expected 2 but got 0
+FAIL size(color-index >= 1) assert_equals: expected 2 but got 0
+FAIL (color-index >= 1) assert_equals: expected 2 but got 0
 PASS size(grid)
 PASS (grid)
 PASS screen
@@ -41,28 +41,28 @@
 PASS screen or (width: 100px)
 PASS not screen and (width: 100px)
 PASS not screen or (width: 100px)
-FAIL (width: 100px), (height: 100px) assert_equals: expected 0 but got 1
+PASS (width: 100px), (height: 100px)
 PASS Container selector:  foo
 PASS Container selector:  foo
-PASS Container selector: name(foo)
-PASS Container selector: name( foo )
-PASS Container selector: type(size)
-PASS Container selector: type( size )
-PASS Container selector: type(inline-size)
-PASS Container selector: type(block-size)
-PASS Container selector: name(bar) type(block-size)
-PASS Container selector: type(block-size) name(bar)
+FAIL Container selector: name(foo) assert_equals: expected 1 but got 0
+FAIL Container selector: name( foo ) assert_equals: expected 1 but got 0
+FAIL Container selector: type(size) assert_equals: expected 1 but got 0
+FAIL Container selector: type( size ) assert_equals: expected 1 but got 0
+FAIL Container selector: type(inline-size) assert_equals: expected 1 but got 0
+FAIL Container selector: type(block-size) assert_equals: expected 1 but got 0
+FAIL Container selector: name(bar) type(block-size) assert_equals: expected 1 but got 0
+FAIL Container selector: type(block-size) name(bar) assert_equals: expected 1 but got 0
 PASS Container selector: foo foo
 PASS Container selector: 1px
 PASS Container selector: 50gil
-FAIL Container selector: name(1px) assert_equals: expected 0 but got 1
-FAIL Container selector: type(1px) assert_equals: expected 0 but got 1
-FAIL Container selector: type(red) assert_equals: expected 0 but got 1
-FAIL Container selector: type(size) type(size) assert_equals: expected 0 but got 1
-FAIL Container selector: type(inline-size) type(block-size) assert_equals: expected 0 but got 1
-FAIL Container selector: name(foo) name(bar) assert_equals: expected 0 but got 1
-FAIL Container selector: type(inline-size) name(foo) type(block-size) assert_equals: expected 0 but got 1
-FAIL Container selector: name(foo) type(size) name(bar) assert_equals: expected 0 but got 1
-FAIL Container selector: name(type(size)) assert_equals: expected 0 but got 1
-FAIL Container selector: type(name(bar)) assert_equals: expected 0 but got 1
+PASS Container selector: name(1px)
+PASS Container selector: type(1px)
+PASS Container selector: type(red)
+PASS Container selector: type(size) type(size)
+PASS Container selector: type(inline-size) type(block-size)
+PASS Container selector: name(foo) name(bar)
+PASS Container selector: type(inline-size) name(foo) type(block-size)
+PASS Container selector: name(foo) type(size) name(bar)
+PASS Container selector: name(type(size))
+PASS Container selector: type(name(bar))
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/container-for-shadow-dom.tentative-expected.txt (289741 => 289742)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/container-for-shadow-dom.tentative-expected.txt	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/container-for-shadow-dom.tentative-expected.txt	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,6 +1,6 @@
 
 FAIL Match container in outer tree null is not an object (evaluating 'document.querySelector("#inclusive-ancestor-across-root > div").shadowRoot.querySelector')
-FAIL Match container in same tree, not walking flat tree ancestors assert_equals: expected "rgb(0, 128, 0)" but got "rgb(0, 0, 0)"
+PASS Match container in same tree, not walking flat tree ancestors
 FAIL Match container in ::slotted selector's originating element tree assert_equals: expected "rgb(0, 128, 0)" but got "rgb(0, 0, 0)"
 FAIL Match container in outer tree for :host assert_equals: expected "rgb(0, 128, 0)" but got "rgb(0, 0, 0)"
 FAIL Match container in ::part selector's originating element tree null is not an object (evaluating 'document.querySelector("#inclusive-ancestor-part > div").shadowRoot.querySelector')

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/container-type-invalidation-expected.txt (289741 => 289742)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/container-type-invalidation-expected.txt	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/container-type-invalidation-expected.txt	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,4 +1,4 @@
 Test
 
-FAIL Changing the container type invalidates relevant descendants assert_equals: expected "rgb(0, 128, 0)" but got "rgb(0, 0, 0)"
+FAIL Changing the container type invalidates relevant descendants assert_equals: expected "rgb(0, 0, 0)" but got "rgb(0, 128, 0)"
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/query-evaluation-expected.txt (289741 => 289742)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/query-evaluation-expected.txt	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/query-evaluation-expected.txt	2022-02-14 19:05:56 UTC (rev 289742)
@@ -3,18 +3,18 @@
 PASS size(height)
 PASS size(unknown)
 PASS unknown(width)
-FAIL (size(width)) assert_equals: expected "true" but got "false"
+PASS (size(width))
 PASS (size(height))
 PASS (size(unknown))
-FAIL (((size(width)))) assert_equals: expected "true" but got "false"
+PASS (((size(width))))
 PASS (((size(height))))
 PASS (((size(unknown))))
 PASS (not size(width))
-FAIL (not size(height)) assert_equals: expected "true" but got "false"
+PASS (not size(height))
 PASS (not size(unknown))
 PASS (not unknown(width))
-FAIL (size(width) and size(width)) assert_equals: expected "true" but got "false"
-FAIL (size(width) and size(width) and size(width)) assert_equals: expected "true" but got "false"
+PASS (size(width) and size(width))
+PASS (size(width) and size(width) and size(width))
 PASS (size(height) and size(height))
 PASS (size(height) and size(width) and size(width))
 PASS (size(width) and size(height) and size(width))
@@ -22,30 +22,30 @@
 PASS (size(unknown) and size(width) and size(width))
 PASS (size(width) and size(unknown) and size(width))
 PASS (size(width) and size(width) and size(unknown))
-FAIL (size(width) or size(width)) assert_equals: expected "true" but got "false"
-FAIL (size(width) or size(width) or size(width)) assert_equals: expected "true" but got "false"
+PASS (size(width) or size(width))
+PASS (size(width) or size(width) or size(width))
 PASS (size(height) or size(height))
-FAIL (size(height) or size(width) or size(width)) assert_equals: expected "true" but got "false"
-FAIL (size(width) or size(height) or size(width)) assert_equals: expected "true" but got "false"
-FAIL (size(width) or size(width) or size(height)) assert_equals: expected "true" but got "false"
-FAIL (size(unknown) or size(width) or size(width)) assert_equals: expected "true" but got "false"
-FAIL (size(width) or size(unknown) or size(width)) assert_equals: expected "true" but got "false"
-FAIL (size(width) or size(width) or size(unknown)) assert_equals: expected "true" but got "false"
-FAIL (size(unknown) or size(height) or size(width)) assert_equals: expected "true" but got "false"
+PASS (size(height) or size(width) or size(width))
+PASS (size(width) or size(height) or size(width))
+PASS (size(width) or size(width) or size(height))
+PASS (size(unknown) or size(width) or size(width))
+PASS (size(width) or size(unknown) or size(width))
+PASS (size(width) or size(width) or size(unknown))
+PASS (size(unknown) or size(height) or size(width))
 PASS (not (size(width) and size(width)))
-FAIL (not (size(width) and size(height))) assert_equals: expected "true" but got "false"
+PASS (not (size(width) and size(height)))
 PASS (size(width) and (not (size(height) or size(width))))
-FAIL (size(height) or (not (size(height) and size(width)))) assert_equals: expected "true" but got "false"
+PASS (size(height) or (not (size(height) and size(width))))
 PASS (size(height) or (size(height) and size(width)))
-FAIL size((width)) assert_equals: expected "true" but got "false"
+PASS size((width))
 PASS size((height))
 PASS size((unknown))
 PASS unknown((width))
 PASS size(not (width))
-FAIL size(not (height)) assert_equals: expected "true" but got "false"
+PASS size(not (height))
 PASS size(not (unknown))
-FAIL size((width) and (width)) assert_equals: expected "true" but got "false"
-FAIL size((width) and (width) and (width)) assert_equals: expected "true" but got "false"
+PASS size((width) and (width))
+PASS size((width) and (width) and (width))
 PASS size((height) and (height))
 PASS size((height) and (width) and (width))
 PASS size((width) and (height) and (width))
@@ -53,19 +53,19 @@
 PASS size((unknown) and (width) and (width))
 PASS size((width) and (unknown) and (width))
 PASS size((width) and (width) and (unknown))
-FAIL size((width) or (width)) assert_equals: expected "true" but got "false"
-FAIL size((width) or (width) or (width)) assert_equals: expected "true" but got "false"
+PASS size((width) or (width))
+PASS size((width) or (width) or (width))
 PASS size((height) or (height))
-FAIL size((height) or (width) or (width)) assert_equals: expected "true" but got "false"
-FAIL size((width) or (height) or (width)) assert_equals: expected "true" but got "false"
-FAIL size((width) or (width) or (height)) assert_equals: expected "true" but got "false"
-FAIL size((unknown) or (width) or (width)) assert_equals: expected "true" but got "false"
-FAIL size((width) or (unknown) or (width)) assert_equals: expected "true" but got "false"
-FAIL size((width) or (width) or (unknown)) assert_equals: expected "true" but got "false"
-FAIL size((unknown) or (height) or (width)) assert_equals: expected "true" but got "false"
+PASS size((height) or (width) or (width))
+PASS size((width) or (height) or (width))
+PASS size((width) or (width) or (height))
+PASS size((unknown) or (width) or (width))
+PASS size((width) or (unknown) or (width))
+PASS size((width) or (width) or (unknown))
+PASS size((unknown) or (height) or (width))
 PASS size(not ((width) and (width)))
-FAIL size(not ((width) and (height))) assert_equals: expected "true" but got "false"
+PASS size(not ((width) and (height)))
 PASS size((width) and (not ((height) or (width))))
-FAIL size((height) or (not ((height) and (width)))) assert_equals: expected "true" but got "false"
+PASS size((height) or (not ((height) and (width))))
 PASS size((height) or ((height) and (width)))
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/size-feature-evaluation-expected.txt (289741 => 289742)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/size-feature-evaluation-expected.txt	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-contain/container-queries/size-feature-evaluation-expected.txt	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,12 +1,12 @@
 Test
 
-PASS size(width < 100px) (.horizontal)
+FAIL size(width < 100px) (.horizontal) assert_equals: expected "" but got "true"
 FAIL size(width >= 100px) (.horizontal) assert_equals: expected "true" but got ""
 PASS size(min-width: 100px) (.horizontal)
 PASS size(min-width: 101px) (.horizontal)
 PASS size(max-width: 100px) (.horizontal)
 PASS size(max-width: 99px) (.horizontal)
-PASS size(height < 200px) (.horizontal)
+FAIL size(height < 200px) (.horizontal) assert_equals: expected "" but got "true"
 FAIL size(height >= 200px) (.horizontal) assert_equals: expected "true" but got ""
 PASS size(min-height: 200px) (.horizontal)
 PASS size(min-height: 201px) (.horizontal)
@@ -28,13 +28,13 @@
 FAIL size(orientation: portrait) (.horizontal) assert_equals: expected "true" but got ""
 FAIL size(aspect-ratio: 1/2) (.horizontal) assert_equals: expected "true" but got ""
 PASS size(aspect-ratio: 2/1) (.horizontal)
-PASS size(width < 100px) (.vertical)
+FAIL size(width < 100px) (.vertical) assert_equals: expected "" but got "true"
 FAIL size(width >= 100px) (.vertical) assert_equals: expected "true" but got ""
 PASS size(min-width: 100px) (.vertical)
 PASS size(min-width: 101px) (.vertical)
 PASS size(max-width: 100px) (.vertical)
 PASS size(max-width: 99px) (.vertical)
-PASS size(height < 200px) (.vertical)
+FAIL size(height < 200px) (.vertical) assert_equals: expected "" but got "true"
 FAIL size(height >= 200px) (.vertical) assert_equals: expected "true" but got ""
 PASS size(min-height: 200px) (.vertical)
 PASS size(min-height: 201px) (.vertical)

Modified: trunk/Source/WebCore/ChangeLog (289741 => 289742)


--- trunk/Source/WebCore/ChangeLog	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/ChangeLog	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1,3 +1,46 @@
+2022-02-14  Antti Koivisto  <[email protected]>
+
+        [CSS Container Queries] Implement full query parser and evaluator
+        https://bugs.webkit.org/show_bug.cgi?id=236580
+
+        Reviewed by Sam Weinig.
+
+        So far we have been using the media query parser to parse container queries.
+        It does not support nesting and other necessary features. Also the produced data
+        structures are awkward to evaluate.
+
+        https://drafts.csswg.org/css-contain-3/#container-rule
+
+        This patch supports size queries but not style queries. It supports both the current spec
+        size query syntax "size(foo)" (used in WPTs), and the non-function POR syntax from
+        https://github.com/w3c/csswg-drafts/issues/6870#issuecomment-1022430911.
+
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * css/ContainerQuery.h:
+        * css/ContainerQueryParser.cpp: Added.
+        (WebCore::ContainerQueryParser::parse):
+        (WebCore::ContainerQueryParser::consumeContainerQuery):
+        (WebCore::ContainerQueryParser::consumeCondition):
+        (WebCore::ContainerQueryParser::consumeSizeQuery):
+
+        No support for range operators ('<' etc) yet.
+
+        * css/ContainerQueryParser.h: Copied from Source/WebCore/css/ContainerQuery.h.
+        (WebCore::ContainerQueryParser::ContainerQueryParser):
+        * css/parser/CSSParserImpl.cpp:
+        (WebCore::CSSParserImpl::consumeContainerRule):
+        * style/ContainerQueryEvaluator.cpp:
+        (WebCore::Style::computeSize):
+        (WebCore::Style::ContainerQueryEvaluator::evaluate const):
+        (WebCore::Style::ContainerQueryEvaluator::evaluateQuery const):
+        (WebCore::Style::ContainerQueryEvaluator::evaluateCondition const):
+        (WebCore::Style::ContainerQueryEvaluator::evaluateSizeFeature const):
+
+        Just 'width' and 'height' for now, no new size features yet.
+
+        * style/ContainerQueryEvaluator.h:
+
 2022-02-14  J Pascoe  <[email protected]>
 
         [WebAuthn] Modify _WKWebAuthenticationAssertionResponse according to internal needs

Modified: trunk/Source/WebCore/Sources.txt (289741 => 289742)


--- trunk/Source/WebCore/Sources.txt	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/Sources.txt	2022-02-14 19:05:56 UTC (rev 289742)
@@ -704,6 +704,7 @@
 crypto/keys/CryptoKeyRSAComponents.cpp
 crypto/keys/CryptoKeyRaw.cpp
 css/BasicShapeFunctions.cpp
+css/ContainerQueryParser.cpp
 css/CSSAspectRatioValue.cpp
 css/CSSBasicShapes.cpp
 css/CSSBorderImage.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (289741 => 289742)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2022-02-14 19:05:56 UTC (rev 289742)
@@ -5430,6 +5430,7 @@
 		E4ED3ECC2768A51D00F17AC8 /* HasSelectorFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = E4ED3ECA2768A51C00F17AC8 /* HasSelectorFilter.h */; };
 		E4F0BE3125712F6E009E7431 /* CaretRectComputation.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F0BE2E25710A75009E7431 /* CaretRectComputation.h */; };
 		E4F1573927B9261500B330D8 /* ContainerQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F1573227B6991F00B330D8 /* ContainerQuery.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		E4F1574127B93B7700B330D8 /* ContainerQueryParser.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F1573D27B93B4C00B330D8 /* ContainerQueryParser.h */; };
 		E4F38D1B2626F13B007B1064 /* DefaultResourceLoadPriority.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F38D192626F13B007B1064 /* DefaultResourceLoadPriority.h */; };
 		E4F819C626FB4EBF0094E162 /* InlineBoxPainter.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F819C526FB4EBF0094E162 /* InlineBoxPainter.h */; };
 		E4F9EEF3156DA00700D23E7E /* StyleSheetContents.h in Headers */ = {isa = PBXBuildFile; fileRef = E4F9EEF1156D84C400D23E7E /* StyleSheetContents.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -17614,6 +17615,8 @@
 		E4F0BE2E25710A75009E7431 /* CaretRectComputation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CaretRectComputation.h; sourceTree = "<group>"; };
 		E4F0BE3025710A76009E7431 /* CaretRectComputation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CaretRectComputation.cpp; sourceTree = "<group>"; };
 		E4F1573227B6991F00B330D8 /* ContainerQuery.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ContainerQuery.h; sourceTree = "<group>"; };
+		E4F1573D27B93B4C00B330D8 /* ContainerQueryParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ContainerQueryParser.h; sourceTree = "<group>"; };
+		E4F1573F27B93B5800B330D8 /* ContainerQueryParser.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ContainerQueryParser.cpp; sourceTree = "<group>"; };
 		E4F38D192626F13B007B1064 /* DefaultResourceLoadPriority.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DefaultResourceLoadPriority.h; sourceTree = "<group>"; };
 		E4F38D1C2626F144007B1064 /* DefaultResourceLoadPriority.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DefaultResourceLoadPriority.cpp; sourceTree = "<group>"; };
 		E4F819C226FB4EAF0094E162 /* InlineBoxPainter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InlineBoxPainter.cpp; sourceTree = "<group>"; };
@@ -31599,6 +31602,8 @@
 				FBD6AF8415EF21D4008B7110 /* BasicShapeFunctions.cpp */,
 				FBD6AF8515EF21D4008B7110 /* BasicShapeFunctions.h */,
 				E4F1573227B6991F00B330D8 /* ContainerQuery.h */,
+				E4F1573F27B93B5800B330D8 /* ContainerQueryParser.cpp */,
+				E4F1573D27B93B4C00B330D8 /* ContainerQueryParser.h */,
 				A80E6CDA0A1989CA007FB8C5 /* Counter.h */,
 				CAE9F90D146441F000C245B0 /* CSSAspectRatioValue.cpp */,
 				CAE9F90E146441F000C245B0 /* CSSAspectRatioValue.h */,
@@ -33876,7 +33881,9 @@
 				E596DD352519041400C275A7 /* ContactsSelectOptions.h in Headers */,
 				A818721C0977D3C0005826D9 /* ContainerNode.h in Headers */,
 				E1A1470811102B1500EEC0F3 /* ContainerNodeAlgorithms.h in Headers */,
+				E4F1573927B9261500B330D8 /* ContainerQuery.h in Headers */,
 				E4AAB38727AA95D7009F5899 /* ContainerQueryEvaluator.h in Headers */,
+				E4F1574127B93B7700B330D8 /* ContainerQueryParser.h in Headers */,
 				6FB5E214221F2453003989CF /* ContentChangeObserver.h in Headers */,
 				BC5EB9810E82072500B25965 /* ContentData.h in Headers */,
 				51B45D211AB8D1E200117CD2 /* ContentExtension.h in Headers */,
@@ -37194,7 +37201,6 @@
 				E45322AC140CE267005A0F92 /* SelectorQuery.h in Headers */,
 				F4D43D662188038B00ECECAC /* SerializedAttachmentData.h in Headers */,
 				E18DF33518AAF12C00773E59 /* SerializedCryptoKeyWrap.h in Headers */,
-				E4F1573927B9261500B330D8 /* ContainerQuery.h in Headers */,
 				077AF14018F4AE400001ED61 /* SerializedPlatformDataCue.h in Headers */,
 				077AF14318F4B1BB0001ED61 /* SerializedPlatformDataCueMac.h in Headers */,
 				076D1C1F23F6EB5B00D95B06 /* SerializedPlatformDataCueValue.h in Headers */,

Modified: trunk/Source/WebCore/css/CSSValueKeywords.in (289741 => 289742)


--- trunk/Source/WebCore/css/CSSValueKeywords.in	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/css/CSSValueKeywords.in	2022-02-14 19:05:56 UTC (rev 289742)
@@ -1642,3 +1642,8 @@
 // farthest-corner
 sides
 // contain
+
+// for @media/@supports/@container
+and
+or
+not

Modified: trunk/Source/WebCore/css/ContainerQuery.h (289741 => 289742)


--- trunk/Source/WebCore/css/ContainerQuery.h	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/css/ContainerQuery.h	2022-02-14 19:05:56 UTC (rev 289742)
@@ -28,13 +28,45 @@
 
 namespace WebCore {
 
-class MediaQuerySet;
+class CSSPrimitiveValue;
 
-using ContainerQuery = MediaQuerySet;
+namespace CQ {
 
+struct ContainerCondition;
+struct SizeCondition;
+struct SizeFeature;
+
+struct UnknownQuery { };
+
+using SizeQuery = std::variant<SizeCondition, SizeFeature>;
+using ContainerQuery = std::variant<ContainerCondition, SizeQuery, UnknownQuery>;
+
+enum class LogicalOperator : uint8_t { And, Or, Not };
+enum class ComparisonOperator : uint8_t { LessThan, LessThanOrEqual, Equal, GreaterThan, GreaterThanOrEqual, True };
+
+struct ContainerCondition {
+    LogicalOperator logicalOperator { LogicalOperator::And };
+    Vector<ContainerQuery> queries;
+};
+
+struct SizeCondition {
+    LogicalOperator logicalOperator { LogicalOperator::And };
+    Vector<SizeQuery> queries;
+};
+
+struct SizeFeature {
+    ComparisonOperator comparisonOperator { ComparisonOperator::Equal };
+    AtomString name;
+    RefPtr<CSSPrimitiveValue> value;
+};
+
+}
+
+using ContainerQuery = CQ::ContainerQuery;
+
 struct FilteredContainerQuery {
     AtomString nameFilter;
-    Ref<ContainerQuery> query;
+    ContainerQuery query;
 };
 
 }

Added: trunk/Source/WebCore/css/ContainerQueryParser.cpp (0 => 289742)


--- trunk/Source/WebCore/css/ContainerQueryParser.cpp	                        (rev 0)
+++ trunk/Source/WebCore/css/ContainerQueryParser.cpp	2022-02-14 19:05:56 UTC (rev 289742)
@@ -0,0 +1,177 @@
+/*
+ * 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.
+ */
+
+#include "config.h"
+#include "ContainerQueryParser.h"
+
+#include "CSSPrimitiveValue.h"
+#include "CSSPropertyParserHelpers.h"
+
+namespace WebCore {
+
+std::optional<ContainerQuery> ContainerQueryParser::consumeContainerQuery(CSSParserTokenRange& range, const CSSParserContext& context)
+{
+    ContainerQueryParser parser(context);
+    return parser.consumeContainerQuery(range);
+}
+
+std::optional<CQ::ContainerQuery> ContainerQueryParser::consumeContainerQuery(CSSParserTokenRange& range)
+{
+    if (range.peek().type() == FunctionToken) {
+        bool isSizeQuery = range.peek().functionId() == CSSValueSize;
+
+        auto blockRange = range.consumeBlock();
+        blockRange.consumeWhitespace();
+
+        if (!isSizeQuery)
+            return CQ::UnknownQuery { };
+
+        auto sizeQuery = consumeSizeQuery(blockRange);
+        if (!sizeQuery)
+            return { };
+        return { *sizeQuery };
+    }
+
+    if (range.peek().type() == LeftParenthesisToken) {
+        auto blockRange = range.consumeBlock();
+        range.consumeWhitespace();
+
+        blockRange.consumeWhitespace();
+
+        // Try to parse as a size query first.
+        auto blockForSizeQuery = blockRange;
+        if (auto sizeQuery = consumeSizeQuery(blockForSizeQuery))
+            return { *sizeQuery };
+
+        if (auto condition = consumeCondition<CQ::ContainerCondition>(blockRange))
+            return { condition };
+    }
+
+    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 consumeContainerQuery(range);
+        if constexpr (std::is_same_v<CQ::SizeCondition, ConditionType>) {
+            if (range.peek().type() != LeftParenthesisToken)
+                return std::optional<CQ::SizeQuery>();
+            auto blockRange = range.consumeBlock();
+            return consumeSizeQuery(blockRange);
+        }
+    };
+
+    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::SizeQuery> ContainerQueryParser::consumeSizeQuery(CSSParserTokenRange& range)
+{
+    if (range.peek().type() != IdentToken || range.peek().id() == CSSValueNot) {
+        auto sizeCondition = consumeCondition<CQ::SizeCondition>(range);
+        if (!sizeCondition)
+            return { };
+        return { *sizeCondition };
+    }
+
+    auto nameToken = range.consumeIncludingWhitespace();
+    auto name = nameToken.value();
+
+    if (range.atEnd())
+        return CQ::SizeFeature { CQ::ComparisonOperator::True, name.toAtomString(), { } };
+
+    auto opToken = range.consumeIncludingWhitespace();
+    if (range.atEnd())
+        return { };
+
+    // FIXME: Support '<' style operators and ranges.
+    auto op = CQ::ComparisonOperator::Equal;
+    if (opToken.type() == ColonToken) {
+        if (name.startsWith("min-"))
+            op = CQ::ComparisonOperator::GreaterThanOrEqual;
+        else if (name.startsWith("max-"))
+            op = CQ::ComparisonOperator::LessThanOrEqual;
+
+        if (op != CQ::ComparisonOperator::Equal)
+            name = name.substring(4);
+        if (name.isEmpty())
+            return { };
+    }
+
+    auto length = CSSPropertyParserHelpers::consumeLength(range, m_context.mode, ValueRange::All);
+    if (!length)
+        return { };
+
+    return CQ::SizeFeature { op, name.toAtomString(), length };
+}
+
+}

Copied: trunk/Source/WebCore/css/ContainerQueryParser.h (from rev 289740, trunk/Source/WebCore/css/ContainerQuery.h) (0 => 289742)


--- trunk/Source/WebCore/css/ContainerQueryParser.h	                        (rev 0)
+++ trunk/Source/WebCore/css/ContainerQueryParser.h	2022-02-14 19:05:56 UTC (rev 289742)
@@ -0,0 +1,47 @@
+/*
+ * 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
+
+#include "CSSParserToken.h"
+#include "ContainerQuery.h"
+
+namespace WebCore {
+
+class ContainerQueryParser {
+public:
+    static std::optional<ContainerQuery> consumeContainerQuery(CSSParserTokenRange&, const CSSParserContext&);
+
+private:
+    std::optional<CQ::ContainerQuery> consumeContainerQuery(CSSParserTokenRange&);
+    std::optional<CQ::SizeQuery> consumeSizeQuery(CSSParserTokenRange&);
+    template<typename ConditionType> std::optional<ConditionType> consumeCondition(CSSParserTokenRange&);
+
+    ContainerQueryParser(const CSSParserContext& context)
+        : m_context(context) { }
+
+    const CSSParserContext m_context;
+};
+
+}

Modified: trunk/Source/WebCore/css/parser/CSSParserImpl.cpp (289741 => 289742)


--- trunk/Source/WebCore/css/parser/CSSParserImpl.cpp	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/css/parser/CSSParserImpl.cpp	2022-02-14 19:05:56 UTC (rev 289742)
@@ -47,6 +47,7 @@
 #include "CSSSupportsParser.h"
 #include "CSSTokenizer.h"
 #include "CSSVariableParser.h"
+#include "ContainerQueryParser.h"
 #include "Document.h"
 #include "Element.h"
 #include "FontPaletteValues.h"
@@ -865,12 +866,16 @@
 
     auto name = consumeName();
 
-    auto query = MediaQueryParser::parseContainerQuery(prelude, MediaQueryParserContext(m_context));
+    auto query = ContainerQueryParser::consumeContainerQuery(prelude, m_context);
     if (!query)
         return nullptr;
 
+    prelude.consumeWhitespace();
+    if (!prelude.atEnd())
+        return nullptr;
+
     if (m_deferredParser)
-        return StyleRuleContainer::create({ name, query.releaseNonNull() }, makeUnique<DeferredStyleGroupRuleList>(block, *m_deferredParser));
+        return StyleRuleContainer::create({ name, *query }, makeUnique<DeferredStyleGroupRuleList>(block, *m_deferredParser));
 
     Vector<RefPtr<StyleRuleBase>> rules;
 
@@ -888,7 +893,7 @@
     if (m_observerWrapper)
         m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(block));
 
-    return StyleRuleContainer::create({ name, query.releaseNonNull() }, WTFMove(rules));
+    return StyleRuleContainer::create({ name, *query }, WTFMove(rules));
 }
     
 RefPtr<StyleRuleKeyframe> CSSParserImpl::consumeKeyframeStyleRule(CSSParserTokenRange prelude, CSSParserTokenRange block)

Modified: trunk/Source/WebCore/style/ContainerQueryEvaluator.cpp (289741 => 289742)


--- trunk/Source/WebCore/style/ContainerQueryEvaluator.cpp	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/style/ContainerQueryEvaluator.cpp	2022-02-14 19:05:56 UTC (rev 289742)
@@ -35,30 +35,29 @@
 
 namespace WebCore::Style {
 
+struct ContainerQueryEvaluator::EvaluationContext {
+    RenderBox& renderer;
+    CSSToLengthConversionData conversionData;
+};
+
 ContainerQueryEvaluator::ContainerQueryEvaluator(const Vector<Ref<const Element>>& containers)
     : m_containers(containers)
 {
 }
 
-static std::optional<LayoutUnit> computeSize(CSSValue* value, const CSSToLengthConversionData& conversionData)
+static std::optional<LayoutUnit> computeSize(const CSSPrimitiveValue& value, const CSSToLengthConversionData& conversionData)
 {
-    if (!is<CSSPrimitiveValue>(value))
-        return { };
-
-    auto& primitiveValue = downcast<CSSPrimitiveValue>(*value);
-    if (primitiveValue.isNumberOrInteger()) {
-        if (primitiveValue.doubleValue())
+    if (value.isNumberOrInteger()) {
+        if (value.doubleValue())
             return { };
         return 0_lu;
     }
 
-    if (!primitiveValue.isLength())
+    if (!value.isLength())
         return { };
-    return primitiveValue.computeLength<LayoutUnit>(conversionData);
+    return value.computeLength<LayoutUnit>(conversionData);
 }
 
-enum class Comparator { Lesser, Greater, LesserOrEqual, GreaterOrEqual, Equal, True };
-
 bool ContainerQueryEvaluator::evaluate(const FilteredContainerQuery& filteredContainerQuery) const
 {
     if (m_containers.isEmpty())
@@ -83,90 +82,114 @@
         return false;
 
     auto& view = renderer->view();
-    CSSToLengthConversionData conversionData { &renderer->style(), &view.style(), nullptr, &view, 1 };
+    CSSToLengthConversionData { &renderer->style(), &view.style(), nullptr, &view, 1 };
 
-    auto evaluateSize = [&](const MediaQueryExpression& _expression_, Comparator comparator, auto&& sizeGetter)
-    {
+    EvaluationContext evaluationContext {
+        *renderer,
+        CSSToLengthConversionData { &renderer->style(), &view.style(), nullptr, &view, 1 }
+    };
+
+    return evaluateQuery(filteredContainerQuery.query, evaluationContext) == EvaluationResult::True;
+}
+
+auto ContainerQueryEvaluator::evaluateQuery(const CQ::ContainerQuery& containerQuery, const EvaluationContext& context) const -> EvaluationResult
+{
+    return WTF::switchOn(containerQuery, [&](const CQ::ContainerCondition& containerCondition) {
+        return evaluateCondition(containerCondition, context);
+    }, [&](const CQ::SizeQuery& sizeQuery) {
+        return evaluateQuery(sizeQuery, context);
+    }, [&](const CQ::UnknownQuery&) {
+        return EvaluationResult::Unknown;
+    });
+}
+
+auto ContainerQueryEvaluator::evaluateQuery(const CQ::SizeQuery& sizeQuery, const EvaluationContext& context) const -> EvaluationResult
+{
+    return WTF::switchOn(sizeQuery, [&](const CQ::SizeCondition& sizeCondition) {
+        return evaluateCondition(sizeCondition, context);
+    }, [&](const CQ::SizeFeature& sizeFeature) {
+        return evaluateSizeFeature(sizeFeature, context);
+    });
+}
+
+template<typename ConditionType>
+auto ContainerQueryEvaluator::evaluateCondition(const ConditionType& condition, const EvaluationContext& context) const -> EvaluationResult
+{
+    if (condition.queries.isEmpty())
+        return EvaluationResult::Unknown;
+
+    switch (condition.logicalOperator) {
+    case CQ::LogicalOperator::Not: {
+        switch (evaluateQuery(condition.queries.first(), context)) {
+        case EvaluationResult::True:
+            return EvaluationResult::False;
+        case EvaluationResult::False:
+            return EvaluationResult::True;
+        case EvaluationResult::Unknown:
+            return EvaluationResult::Unknown;
+        }
+    }
+    case CQ::LogicalOperator::And: {
+        auto result = EvaluationResult::True;
+        for (auto query : condition.queries) {
+            auto queryResult = evaluateQuery(query, context);
+            if (queryResult == EvaluationResult::False)
+                return EvaluationResult::False;
+            if (queryResult == EvaluationResult::Unknown)
+                result = EvaluationResult::Unknown;
+        }
+        return result;
+    }
+    case CQ::LogicalOperator::Or: {
+        auto result = EvaluationResult::False;
+        for (auto query : condition.queries) {
+            auto queryResult = evaluateQuery(query, context);
+            if (queryResult == EvaluationResult::True)
+                return EvaluationResult::True;
+            if (queryResult == EvaluationResult::Unknown)
+                result = EvaluationResult::Unknown;
+        }
+        return result;
+    }
+    }
+}
+
+auto ContainerQueryEvaluator::evaluateSizeFeature(const CQ::SizeFeature& sizeFeature, const EvaluationContext& context) const -> EvaluationResult
+{
+    auto evaluateSize = [&](LayoutUnit size) {
         std::optional<LayoutUnit> expressionSize;
 
-        if (comparator != Comparator::True) {
-            expressionSize = computeSize(_expression_.value(), conversionData);
+        if (sizeFeature.comparisonOperator != CQ::ComparisonOperator::True) {
+            if (!sizeFeature.value)
+                return false;
+            expressionSize = computeSize(*sizeFeature.value, context.conversionData);
             if (!expressionSize)
                 return false;
         }
 
-        auto size = sizeGetter(*renderer);
-
-        switch (comparator) {
-        case Comparator::Lesser:
+        switch (sizeFeature.comparisonOperator) {
+        case CQ::ComparisonOperator::LessThan:
             return size < *expressionSize;
-        case Comparator::Greater:
+        case CQ::ComparisonOperator::GreaterThan:
             return size > *expressionSize;
-        case Comparator::LesserOrEqual:
+        case CQ::ComparisonOperator::LessThanOrEqual:
             return size <= *expressionSize;
-        case Comparator::GreaterOrEqual:
+        case CQ::ComparisonOperator::GreaterThanOrEqual:
             return size >= *expressionSize;
-        case Comparator::Equal:
+        case CQ::ComparisonOperator::Equal:
             return size == *expressionSize;
-        case Comparator::True:
+        case CQ::ComparisonOperator::True:
             return !!size;
         }
     };
 
-    auto evaluateWidth = [&](const MediaQueryExpression& _expression_, Comparator comparator)
-    {
-        return evaluateSize(_expression_, comparator, [&](const RenderBox& renderer) {
-            return renderer.width();
-        });
-    };
+    // FIXME: Support all features.
+    if (sizeFeature.name == MediaFeatureNames::width)
+        return evaluateSize(context.renderer.width()) ? EvaluationResult::True : EvaluationResult::False;
+    if (sizeFeature.name == MediaFeatureNames::height)
+        return evaluateSize(context.renderer.height()) ? EvaluationResult::True : EvaluationResult::False;
 
-    auto evaluateHeight = [&](const MediaQueryExpression& _expression_, Comparator comparator)
-    {
-        return evaluateSize(_expression_, comparator, [&](const RenderBox& renderer) {
-            return renderer.height();
-        });
-    };
-
-    bool result = false;
-
-    // FIXME: This is very rudimentary.
-    auto& queries = filteredContainerQuery.query->queryVector();
-    for (auto& query : queries) {
-        for (auto& _expression_ : query.expressions()) {
-            if (_expression_.mediaFeature() == MediaFeatureNames::minWidth) {
-                if (evaluateWidth(_expression_, Comparator::GreaterOrEqual))
-                    result = true;
-                continue;
-            }
-            if (_expression_.mediaFeature() == MediaFeatureNames::maxWidth) {
-                if (evaluateWidth(_expression_, Comparator::LesserOrEqual))
-                    result = true;
-                continue;
-            }
-            if (_expression_.mediaFeature() == MediaFeatureNames::width) {
-                if (evaluateWidth(_expression_, _expression_.value() ? Comparator::Equal : Comparator::True))
-                    result = true;
-                continue;
-            }
-            if (_expression_.mediaFeature() == MediaFeatureNames::minHeight) {
-                if (evaluateHeight(_expression_, Comparator::GreaterOrEqual))
-                    result = true;
-                continue;
-            }
-            if (_expression_.mediaFeature() == MediaFeatureNames::maxHeight) {
-                if (evaluateHeight(_expression_, Comparator::LesserOrEqual))
-                    result = true;
-                continue;
-            }
-            if (_expression_.mediaFeature() == MediaFeatureNames::height) {
-                if (evaluateHeight(_expression_, _expression_.value() ? Comparator::Equal : Comparator::True))
-                    result = true;
-                continue;
-            }
-        }
-    }
-
-    return result;
+    return EvaluationResult::Unknown;
 }
 
 }

Modified: trunk/Source/WebCore/style/ContainerQueryEvaluator.h (289741 => 289742)


--- trunk/Source/WebCore/style/ContainerQueryEvaluator.h	2022-02-14 18:47:43 UTC (rev 289741)
+++ trunk/Source/WebCore/style/ContainerQueryEvaluator.h	2022-02-14 19:05:56 UTC (rev 289742)
@@ -24,11 +24,11 @@
 
 #pragma once
 
+#include "ContainerQuery.h"
 #include <wtf/Ref.h>
 
 namespace WebCore {
 
-struct FilteredContainerQuery;
 class Element;
 
 namespace Style {
@@ -40,6 +40,14 @@
     bool evaluate(const FilteredContainerQuery&) const;
 
 private:
+    struct EvaluationContext;
+    enum class EvaluationResult : uint8_t { False, True, Unknown };
+
+    EvaluationResult evaluateQuery(const CQ::ContainerQuery&, const EvaluationContext&) const;
+    EvaluationResult evaluateQuery(const CQ::SizeQuery&, const EvaluationContext&) const;
+    template<typename ConditionType> EvaluationResult evaluateCondition(const ConditionType&, const EvaluationContext&) const;
+    EvaluationResult evaluateSizeFeature(const CQ::SizeFeature&, const EvaluationContext&) const;
+
     const Vector<Ref<const Element>>& m_containers;
 };
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to