Title: [253105] trunk
Revision
253105
Author
[email protected]
Date
2019-12-04 09:12:22 -0800 (Wed, 04 Dec 2019)

Log Message

Implement the CSS clamp() function
https://bugs.webkit.org/show_bug.cgi?id=203310
<rdar://problem/56551088>

Reviewed by Antti Koivisto.
LayoutTests/imported/w3c:

* web-platform-tests/css/css-values/clamp-length-computed-expected.txt:
* web-platform-tests/css/css-values/clamp-length-serialize-expected.txt:

Source/WebCore:

Implement the CSS clamp() function, of the form clamp(min, value, max):
https://drafts.csswg.org/css-values-4/#funcdef-clamp

This is a natural extension of min() and max(), the main difference being
that it takes exactly three arguments.

Tests: fast/css/calc-parsing.html
       web-platform-tests/css/css-values/clamp-length-computed-expected.txt:
       web-platform-tests/css/css-values/clamp-length-serialize-expected.txt:

* css/CSSCalculationValue.cpp:
(WebCore::functionFromOperator):
(WebCore::determineCategory):
(WebCore::resolvedTypeForMinOrMaxOrClamp):
(WebCore::CSSCalcOperationNode::createMinOrMaxOrClamp):
(WebCore::functionPrefixForOperator):
(WebCore::CSSCalcOperationNode::evaluateOperator):
(WebCore::CSSCalcExpressionNodeParser::parseCalcFunction):
(WebCore::createCSS):
(WebCore::CSSCalcValue::isCalcFunction):
(WebCore::resolvedTypeForMinOrMax): Deleted.
(WebCore::CSSCalcOperationNode::createMinOrMax): Deleted.
* css/CSSValueKeywords.in:
* platform/CalculationValue.cpp:
(WebCore::CalcExpressionOperation::evaluate const):
(WebCore::operator<<):
* platform/CalculationValue.h:

LayoutTests:

Some basic clamp() parsing tests (valid and invalid inputs).

* fast/css/calc-parsing-expected.txt:
* fast/css/calc-parsing.html:

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (253104 => 253105)


--- trunk/LayoutTests/ChangeLog	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/LayoutTests/ChangeLog	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1,3 +1,16 @@
+2019-12-04  Simon Fraser  <[email protected]>
+
+        Implement the CSS clamp() function
+        https://bugs.webkit.org/show_bug.cgi?id=203310
+        <rdar://problem/56551088>
+
+        Reviewed by Antti Koivisto.
+
+        Some basic clamp() parsing tests (valid and invalid inputs).
+
+        * fast/css/calc-parsing-expected.txt:
+        * fast/css/calc-parsing.html:
+
 2019-12-04  youenn fablet  <[email protected]>
 
         Update WPT tools directory

Modified: trunk/LayoutTests/fast/css/calc-parsing-expected.txt (253104 => 253105)


--- trunk/LayoutTests/fast/css/calc-parsing-expected.txt	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/LayoutTests/fast/css/calc-parsing-expected.txt	2019-12-04 17:12:22 UTC (rev 253105)
@@ -20,6 +20,26 @@
 PASS testDiv.style['width'] is "max(200px)"
 PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "200px"
 
+testDiv.style["width"] = "clamp(100px,123px,200px)"
+PASS testDiv.style['width'] is "clamp(100px, 123px, 200px)"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "123px"
+
+testDiv.style["width"] = "clamp(100px,300px,200px)"
+PASS testDiv.style['width'] is "clamp(100px, 300px, 200px)"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "200px"
+
+testDiv.style["width"] = "clamp(200px,300px,100px)"
+PASS testDiv.style['width'] is "clamp(200px, 300px, 100px)"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "200px"
+
+testDiv.style["width"] = "clamp((50px + 50px),40px,200px)"
+PASS testDiv.style['width'] is "clamp(100px, 40px, 200px)"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "100px"
+
+testDiv.style["width"] = "clamp(50px + 50px,40px,200px)"
+PASS testDiv.style['width'] is "clamp(100px, 40px, 200px)"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "100px"
+
 testDiv.style["width"] = "calc(100px, 200px)"
 PASS testDiv.style['width'] is "999px"
 PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
@@ -51,6 +71,26 @@
 testDiv.style["width"] = "max(100px, 200px,)"
 PASS testDiv.style['width'] is "999px"
 PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
+
+testDiv.style["width"] = "clamp(200px,300px)"
+PASS testDiv.style['width'] is "999px"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
+
+testDiv.style["width"] = "clamp(200px,300px,)"
+PASS testDiv.style['width'] is "999px"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
+
+testDiv.style["width"] = "clamp(200px,,300px)"
+PASS testDiv.style['width'] is "999px"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
+
+testDiv.style["width"] = "clamp((),,300px)"
+PASS testDiv.style['width'] is "999px"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
+
+testDiv.style["width"] = "clamp(1px,2px,2px,4px)"
+PASS testDiv.style['width'] is "999px"
+PASS window.getComputedStyle(testDiv).getPropertyValue('width') is "999px"
 PASS successfullyParsed is true
 
 TEST COMPLETE

Modified: trunk/LayoutTests/fast/css/calc-parsing.html (253104 => 253105)


--- trunk/LayoutTests/fast/css/calc-parsing.html	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/LayoutTests/fast/css/calc-parsing.html	2019-12-04 17:12:22 UTC (rev 253105)
@@ -23,6 +23,11 @@
             testExpression('max(100px + 200px)', 'max(300px)', '300px');
             testExpression('max(100px , 200px)', 'max(200px)', '200px');
             testExpression('max(100px,200px)', 'max(200px)', '200px');
+            testExpression('clamp(100px,123px,200px)', 'clamp(100px, 123px, 200px)', '123px');
+            testExpression('clamp(100px,300px,200px)', 'clamp(100px, 300px, 200px)', '200px');
+            testExpression('clamp(200px,300px,100px)', 'clamp(200px, 300px, 100px)', '200px');
+            testExpression('clamp((50px + 50px),40px,200px)', 'clamp(100px, 40px, 200px)', '100px');
+            testExpression('clamp(50px + 50px,40px,200px)', 'clamp(100px, 40px, 200px)', '100px');
 
             // Non-parsing _expression_.
             testExpression('calc(100px, 200px)', '999px', '999px');
@@ -35,6 +40,12 @@
             testExpression('max(100px,, 200px)', '999px', '999px');
             testExpression('max(100px, , 200px)', '999px', '999px');
             testExpression('max(100px, 200px,)', '999px', '999px');
+
+            testExpression('clamp(200px,300px)', '999px', '999px');
+            testExpression('clamp(200px,300px,)', '999px', '999px');
+            testExpression('clamp(200px,,300px)', '999px', '999px');
+            testExpression('clamp((),,300px)', '999px', '999px');
+            testExpression('clamp(1px,2px,2px,4px)', '999px', '999px');
         </script>
         <script src=""
     </body>

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (253104 => 253105)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1,3 +1,14 @@
+2019-12-04  Simon Fraser  <[email protected]>
+
+        Implement the CSS clamp() function
+        https://bugs.webkit.org/show_bug.cgi?id=203310
+        <rdar://problem/56551088>
+
+        Reviewed by Antti Koivisto.
+
+        * web-platform-tests/css/css-values/clamp-length-computed-expected.txt:
+        * web-platform-tests/css/css-values/clamp-length-serialize-expected.txt:
+
 2019-12-04  Youenn Fablet  <[email protected]>
 
         Update WPT tools directory

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-values/clamp-length-computed-expected.txt (253104 => 253105)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-values/clamp-length-computed-expected.txt	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-values/clamp-length-computed-expected.txt	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1,6 +1,6 @@
 
-FAIL Property letter-spacing value 'clamp(10px, 20px, 30px)' computes to '20px' assert_true: 'clamp(10px, 20px, 30px)' is a supported value for letter-spacing. expected true got false
-FAIL Property letter-spacing value 'clamp(10px, 5px, 30px)' computes to '10px' assert_true: 'clamp(10px, 5px, 30px)' is a supported value for letter-spacing. expected true got false
-FAIL Property letter-spacing value 'clamp(10px, 35px, 30px)' computes to '30px' assert_true: 'clamp(10px, 35px, 30px)' is a supported value for letter-spacing. expected true got false
-FAIL Property letter-spacing value 'clamp(30px, 100px, 20px)' computes to '30px' assert_true: 'clamp(30px, 100px, 20px)' is a supported value for letter-spacing. expected true got false
+PASS Property letter-spacing value 'clamp(10px, 20px, 30px)' computes to '20px' 
+PASS Property letter-spacing value 'clamp(10px, 5px, 30px)' computes to '10px' 
+PASS Property letter-spacing value 'clamp(10px, 35px, 30px)' computes to '30px' 
+PASS Property letter-spacing value 'clamp(30px, 100px, 20px)' computes to '30px' 
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-values/clamp-length-serialize-expected.txt (253104 => 253105)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-values/clamp-length-serialize-expected.txt	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-values/clamp-length-serialize-expected.txt	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1,4 +1,4 @@
 
-FAIL e.style['letter-spacing'] = "clamp(1px, 2px, 3px)" should set the property value assert_not_equals: property should be set got disallowed value ""
-FAIL e.style['letter-spacing'] = "clamp(1px, 2px, clamp(2px, 3px, 4px))" should set the property value assert_not_equals: property should be set got disallowed value ""
+PASS e.style['letter-spacing'] = "clamp(1px, 2px, 3px)" should set the property value 
+PASS e.style['letter-spacing'] = "clamp(1px, 2px, clamp(2px, 3px, 4px))" should set the property value 
 

Modified: trunk/Source/WebCore/ChangeLog (253104 => 253105)


--- trunk/Source/WebCore/ChangeLog	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/Source/WebCore/ChangeLog	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1,3 +1,39 @@
+2019-12-04  Simon Fraser  <[email protected]>
+
+        Implement the CSS clamp() function
+        https://bugs.webkit.org/show_bug.cgi?id=203310
+        <rdar://problem/56551088>
+
+        Reviewed by Antti Koivisto.
+        
+        Implement the CSS clamp() function, of the form clamp(min, value, max):
+        https://drafts.csswg.org/css-values-4/#funcdef-clamp
+
+        This is a natural extension of min() and max(), the main difference being
+        that it takes exactly three arguments.
+
+        Tests: fast/css/calc-parsing.html
+               web-platform-tests/css/css-values/clamp-length-computed-expected.txt:
+               web-platform-tests/css/css-values/clamp-length-serialize-expected.txt:
+
+        * css/CSSCalculationValue.cpp:
+        (WebCore::functionFromOperator):
+        (WebCore::determineCategory):
+        (WebCore::resolvedTypeForMinOrMaxOrClamp):
+        (WebCore::CSSCalcOperationNode::createMinOrMaxOrClamp):
+        (WebCore::functionPrefixForOperator):
+        (WebCore::CSSCalcOperationNode::evaluateOperator):
+        (WebCore::CSSCalcExpressionNodeParser::parseCalcFunction):
+        (WebCore::createCSS):
+        (WebCore::CSSCalcValue::isCalcFunction):
+        (WebCore::resolvedTypeForMinOrMax): Deleted.
+        (WebCore::CSSCalcOperationNode::createMinOrMax): Deleted.
+        * css/CSSValueKeywords.in:
+        * platform/CalculationValue.cpp:
+        (WebCore::CalcExpressionOperation::evaluate const):
+        (WebCore::operator<<):
+        * platform/CalculationValue.h:
+
 2019-12-04  youenn fablet  <[email protected]>
 
         Move soft update handling to network process

Modified: trunk/Source/WebCore/css/CSSCalculationValue.cpp (253104 => 253105)


--- trunk/Source/WebCore/css/CSSCalculationValue.cpp	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/Source/WebCore/css/CSSCalculationValue.cpp	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
- * Copyright (C) 2014 Apple Inc. All rights reserved.
+ * Copyright (C) 2014, 2019 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
@@ -234,6 +234,8 @@
         return CSSValueMin;
     case CalcOperator::Max:
         return CSSValueMax;
+    case CalcOperator::Clamp:
+        return CSSValueClamp;
     }
     return CSSValueCalc;
 }
@@ -633,6 +635,7 @@
         return leftCategory;
     case CalcOperator::Min:
     case CalcOperator::Max:
+    case CalcOperator::Clamp:
         ASSERT_NOT_REACHED();
         return CalculationCategory::Other;
     }
@@ -701,6 +704,7 @@
 
         case CalcOperator::Min:
         case CalcOperator::Max:
+        case CalcOperator::Clamp:
             // The type of a min(), max(), or clamp() _expression_ is the result of adding the types of its comma-separated calculations
             return CalculationCategory::Other;
         }
@@ -709,7 +713,7 @@
     return currentCategory;
 }
 
-static CalculationCategory resolvedTypeForMinOrMax(CalculationCategory category, CalculationCategory destinationCategory)
+static CalculationCategory resolvedTypeForMinOrMaxOrClamp(CalculationCategory category, CalculationCategory destinationCategory)
 {
     switch (category) {
     case CalculationCategory::Number:
@@ -744,7 +748,7 @@
     static RefPtr<CSSCalcOperationNode> create(CalcOperator, RefPtr<CSSCalcExpressionNode>&& leftSide, RefPtr<CSSCalcExpressionNode>&& rightSide);
     static RefPtr<CSSCalcOperationNode> createSum(Vector<Ref<CSSCalcExpressionNode>>&& values);
     static RefPtr<CSSCalcOperationNode> createProduct(Vector<Ref<CSSCalcExpressionNode>>&& values);
-    static RefPtr<CSSCalcOperationNode> createMinOrMax(CalcOperator, Vector<Ref<CSSCalcExpressionNode>>&& values, CalculationCategory destinationCategory);
+    static RefPtr<CSSCalcOperationNode> createMinOrMaxOrClamp(CalcOperator, Vector<Ref<CSSCalcExpressionNode>>&& values, CalculationCategory destinationCategory);
 
     static Ref<CSSCalcExpressionNode> simplify(Ref<CSSCalcExpressionNode>&&);
 
@@ -877,13 +881,14 @@
     return adoptRef(new CSSCalcOperationNode(newCategory, CalcOperator::Multiply, WTFMove(values)));
 }
 
-RefPtr<CSSCalcOperationNode> CSSCalcOperationNode::createMinOrMax(CalcOperator op, Vector<Ref<CSSCalcExpressionNode>>&& values, CalculationCategory destinationCategory)
+RefPtr<CSSCalcOperationNode> CSSCalcOperationNode::createMinOrMaxOrClamp(CalcOperator op, Vector<Ref<CSSCalcExpressionNode>>&& values, CalculationCategory destinationCategory)
 {
-    ASSERT(op == CalcOperator::Min || op == CalcOperator::Max);
+    ASSERT(op == CalcOperator::Min || op == CalcOperator::Max || op == CalcOperator::Clamp);
+    ASSERT_IMPLIES(op == CalcOperator::Clamp, values.size() == 3);
 
     Optional<CalculationCategory> category = WTF::nullopt;
     for (auto& value : values) {
-        auto valueCategory = resolvedTypeForMinOrMax(value->category(), destinationCategory);
+        auto valueCategory = resolvedTypeForMinOrMaxOrClamp(value->category(), destinationCategory);
 
         ASSERT(valueCategory < CalculationCategory::Other);
         if (!category) {
@@ -1399,6 +1404,7 @@
         return "";
     case CalcOperator::Min: return "min(";
     case CalcOperator::Max: return "max(";
+    case CalcOperator::Clamp: return "clamp(";
     }
     
     return "";
@@ -1592,7 +1598,15 @@
             maximum = std::max(maximum, child);
         return maximum;
     }
+    case CalcOperator::Clamp: {
+        if (children.size() != 3)
+            return std::numeric_limits<double>::quiet_NaN();
+        double min = children[0];
+        double value = children[1];
+        double max = children[2];
+        return std::max(min, std::min(value, max));
     }
+    }
     ASSERT_NOT_REACHED();
     return 0;
 }
@@ -1686,6 +1700,7 @@
         return false;
 
     // "arguments" refers to things between commas.
+    unsigned minArgumentCount = 1;
     Optional<unsigned> maxArgumentCount;
 
     switch (functionID) {
@@ -1693,6 +1708,10 @@
     case CSSValueMax:
         maxArgumentCount = WTF::nullopt;
         break;
+    case CSSValueClamp:
+        minArgumentCount = 3;
+        maxArgumentCount = 3;
+        break;
     case CSSValueCalc:
         maxArgumentCount = 1;
         break;
@@ -1724,14 +1743,20 @@
         nodes.append(node.releaseNonNull());
         requireComma = true;
     }
+    
+    if (argumentCount < minArgumentCount)
+        return false;
 
     switch (functionID) {
     case CSSValueMin:
-        result = CSSCalcOperationNode::createMinOrMax(CalcOperator::Min, WTFMove(nodes), m_destinationCategory);
+        result = CSSCalcOperationNode::createMinOrMaxOrClamp(CalcOperator::Min, WTFMove(nodes), m_destinationCategory);
         break;
     case CSSValueMax:
-        result = CSSCalcOperationNode::createMinOrMax(CalcOperator::Max, WTFMove(nodes), m_destinationCategory);
+        result = CSSCalcOperationNode::createMinOrMaxOrClamp(CalcOperator::Max, WTFMove(nodes), m_destinationCategory);
         break;
+    case CSSValueClamp:
+        result = CSSCalcOperationNode::createMinOrMaxOrClamp(CalcOperator::Clamp, WTFMove(nodes), m_destinationCategory);
+        break;
     case CSSValueWebkitCalc:
     case CSSValueCalc:
         result = CSSCalcOperationNode::createSum(WTFMove(nodes));
@@ -1970,11 +1995,12 @@
             return CSSCalcOperationNode::createProduct(createCSS(operationChildren, style));
         }
         case CalcOperator::Min:
-        case CalcOperator::Max: {
+        case CalcOperator::Max:
+        case CalcOperator::Clamp: {
             auto children = createCSS(operationChildren, style);
             if (children.isEmpty())
                 return nullptr;
-            return CSSCalcOperationNode::createMinOrMax(op, WTFMove(children), CalculationCategory::Other);
+            return CSSCalcOperationNode::createMinOrMaxOrClamp(op, WTFMove(children), CalculationCategory::Other);
         }
         }
         return nullptr;
@@ -2047,6 +2073,7 @@
     case CSSValueWebkitCalc:
     case CSSValueMin:
     case CSSValueMax:
+    case CSSValueClamp:
         return true;
     default:
         return false;

Modified: trunk/Source/WebCore/css/CSSValueKeywords.in (253104 => 253105)


--- trunk/Source/WebCore/css/CSSValueKeywords.in	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/Source/WebCore/css/CSSValueKeywords.in	2019-12-04 17:12:22 UTC (rev 253105)
@@ -1309,6 +1309,7 @@
 -webkit-calc
 min
 max
+clamp
 
 #if defined(ENABLE_CSS_IMAGE_RESOLUTION) && ENABLE_CSS_IMAGE_RESOLUTION
 from-image

Modified: trunk/Source/WebCore/platform/CalculationValue.cpp (253104 => 253105)


--- trunk/Source/WebCore/platform/CalculationValue.cpp	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/Source/WebCore/platform/CalculationValue.cpp	2019-12-04 17:12:22 UTC (rev 253105)
@@ -159,7 +159,16 @@
             maximum = std::max(maximum, child->evaluate(maxValue));
         return maximum;
     }
+    case CalcOperator::Clamp: {
+        if (m_children.size() != 3)
+            return std::numeric_limits<float>::quiet_NaN();
+
+        float min = m_children[0]->evaluate(maxValue);
+        float value = m_children[1]->evaluate(maxValue);
+        float max = m_children[2]->evaluate(maxValue);
+        return std::max(min, std::min(value, max));
     }
+    }
     ASSERT_NOT_REACHED();
     return std::numeric_limits<float>::quiet_NaN();
 }
@@ -251,6 +260,7 @@
     case CalcOperator::Divide: ts << "/"; break;
     case CalcOperator::Min: ts << "min"; break;
     case CalcOperator::Max: ts << "max"; break;
+    case CalcOperator::Clamp: ts << "clamp"; break;
     }
     return ts;
 }

Modified: trunk/Source/WebCore/platform/CalculationValue.h (253104 => 253105)


--- trunk/Source/WebCore/platform/CalculationValue.h	2019-12-04 15:43:46 UTC (rev 253104)
+++ trunk/Source/WebCore/platform/CalculationValue.h	2019-12-04 17:12:22 UTC (rev 253105)
@@ -49,7 +49,8 @@
     Multiply = '*',
     Divide = '/',
     Min = 0,
-    Max = 1,
+    Max,
+    Clamp,
 };
 
 enum class CalcExpressionNodeType : uint8_t {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to