Title: [108750] trunk
Revision
108750
Author
[email protected]
Date
2012-02-24 01:33:28 -0800 (Fri, 24 Feb 2012)

Log Message

CSS3 calc(): handle non-negative values
https://bugs.webkit.org/show_bug.cgi?id=79188

Reviewed by Daniel Bates.

Source/WebCore:

Some CSS properties (e.g. padding) are required to be non-negative. These
are now restricted to the correct range.

Tests: css3/calc/negative-padding-expected.html
       css3/calc/negative-padding.html

* css/CSSCalculationValue.cpp:
(WebCore):
(WebCore::CSSCalcValue::clampToPermittedRange): Added
(WebCore::CSSCalcValue::doubleValue):
(WebCore::CSSCalcValue::isNegative): Added
(WebCore::CSSCalcValue::computeLengthPx):
(WebCore::CSSCalcValue::create):
* css/CSSCalculationValue.h:
(CSSCalcValue):
(WebCore::CSSCalcValue::CSSCalcValue):
* css/CSSParser.cpp:
(WebCore::CSSParser::validCalculationUnit):
(WebCore::CSSParser::parseCalculation):
* css/CSSParser.h:
* platform/CalculationValue.h:

LayoutTests:

* css3/calc/negative-padding-expected.html: Added.
* css3/calc/negative-padding.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (108749 => 108750)


--- trunk/LayoutTests/ChangeLog	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/LayoutTests/ChangeLog	2012-02-24 09:33:28 UTC (rev 108750)
@@ -1,3 +1,13 @@
+2012-02-24  Mike Lawther  <[email protected]>
+
+        CSS3 calc(): handle non-negative values
+        https://bugs.webkit.org/show_bug.cgi?id=79188
+
+        Reviewed by Daniel Bates.
+
+        * css3/calc/negative-padding-expected.html: Added.
+        * css3/calc/negative-padding.html: Added.
+
 2012-02-22  Vsevolod Vlasov  <[email protected]>
 
         Web Inspector: [Regression] xhr tests are crashing after r108506.

Added: trunk/LayoutTests/css3/calc/negative-padding-expected.html (0 => 108750)


--- trunk/LayoutTests/css3/calc/negative-padding-expected.html	                        (rev 0)
+++ trunk/LayoutTests/css3/calc/negative-padding-expected.html	2012-02-24 09:33:28 UTC (rev 108750)
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML>
+<style>
+    p { width:200px; height:120px; background-color: lightgreen;}
+    .control { padding: -100px; }
+</style>
+
+<div style="background-color: yellow;">
+    <p class="control">This element should have zero padding.</p>
+    <p class="control">This element should have zero padding.</p>
+    <p class="control">This element should have zero padding.</p>
+</div>

Added: trunk/LayoutTests/css3/calc/negative-padding.html (0 => 108750)


--- trunk/LayoutTests/css3/calc/negative-padding.html	                        (rev 0)
+++ trunk/LayoutTests/css3/calc/negative-padding.html	2012-02-24 09:33:28 UTC (rev 108750)
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML>
+<style>
+    p { width:200px; height:120px; background-color: lightgreen; }
+    .control        { padding: -100px; }
+    .simple-px      { padding: -webkit-calc(100px - 200px); }
+    .simple-percent { padding: -webkit-calc(50% - 60%); }
+</style>
+
+<div style="background-color: yellow;">
+    <p class="control">This element should have zero padding.</p>
+    <p class="simple-px">This element should have zero padding.</p>
+    <p class="simple-percent">This element should have zero padding.</p>
+</div>

Modified: trunk/Source/WebCore/ChangeLog (108749 => 108750)


--- trunk/Source/WebCore/ChangeLog	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/Source/WebCore/ChangeLog	2012-02-24 09:33:28 UTC (rev 108750)
@@ -1,3 +1,32 @@
+2012-02-24  Mike Lawther  <[email protected]>
+
+        CSS3 calc(): handle non-negative values
+        https://bugs.webkit.org/show_bug.cgi?id=79188
+
+        Reviewed by Daniel Bates.
+
+        Some CSS properties (e.g. padding) are required to be non-negative. These
+        are now restricted to the correct range.
+
+        Tests: css3/calc/negative-padding-expected.html
+               css3/calc/negative-padding.html
+
+        * css/CSSCalculationValue.cpp:
+        (WebCore):
+        (WebCore::CSSCalcValue::clampToPermittedRange): Added
+        (WebCore::CSSCalcValue::doubleValue):
+        (WebCore::CSSCalcValue::isNegative): Added
+        (WebCore::CSSCalcValue::computeLengthPx):
+        (WebCore::CSSCalcValue::create):
+        * css/CSSCalculationValue.h:
+        (CSSCalcValue):
+        (WebCore::CSSCalcValue::CSSCalcValue):
+        * css/CSSParser.cpp:
+        (WebCore::CSSParser::validCalculationUnit):
+        (WebCore::CSSParser::parseCalculation):
+        * css/CSSParser.h:
+        * platform/CalculationValue.h:
+
 2012-02-22  Vsevolod Vlasov  <[email protected]>
 
         Web Inspector: [Regression] xhr tests are crashing after r108506.

Modified: trunk/Source/WebCore/css/CSSCalculationValue.cpp (108749 => 108750)


--- trunk/Source/WebCore/css/CSSCalculationValue.cpp	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/Source/WebCore/css/CSSCalculationValue.cpp	2012-02-24 09:33:28 UTC (rev 108750)
@@ -76,15 +76,20 @@
 {
     return "";
 }
-
+    
+double CSSCalcValue::clampToPermittedRange(double value) const
+{
+    return m_nonNegative && value < 0 ? 0 : value;
+}    
+    
 double CSSCalcValue::doubleValue() const 
 { 
-    return m_expression->doubleValue();
+    return clampToPermittedRange(m_expression->doubleValue());
 }
-    
+
 double CSSCalcValue::computeLengthPx(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier, bool computingFontSize) const
 {
-    return m_expression->computeLengthPx(currentStyle, rootStyle, multiplier, computingFontSize);
+    return clampToPermittedRange(m_expression->computeLengthPx(currentStyle, rootStyle, multiplier, computingFontSize));
 }
     
 CSSCalcExpressionNode::~CSSCalcExpressionNode() 
@@ -381,7 +386,7 @@
     }
 };
 
-PassRefPtr<CSSCalcValue> CSSCalcValue::create(CSSParserString name, CSSParserValueList* parserValueList)
+PassRefPtr<CSSCalcValue> CSSCalcValue::create(CSSParserString name, CSSParserValueList* parserValueList, CalculationPermittedValueRange range)
 {    
     CSSCalcExpressionNodeParser parser;    
     RefPtr<CSSCalcExpressionNode> _expression_;
@@ -390,7 +395,7 @@
         _expression_ = parser.parseCalc(parserValueList);    
     // FIXME calc (http://webkit.org/b/16662) Add parsing for min and max here
 
-    return _expression_ ? adoptRef(new CSSCalcValue(_expression_)) : 0;
+    return _expression_ ? adoptRef(new CSSCalcValue(_expression_, range)) : 0;
 }
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/css/CSSCalculationValue.h (108749 => 108750)


--- trunk/Source/WebCore/css/CSSCalculationValue.h	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/Source/WebCore/css/CSSCalculationValue.h	2012-02-24 09:33:28 UTC (rev 108750)
@@ -60,7 +60,7 @@
     
     virtual ~CSSCalcExpressionNode() = 0;  
     virtual bool isZero() const = 0;
-    virtual double doubleValue() const = 0;  
+    virtual double doubleValue() const = 0;
     virtual double computeLengthPx(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false) const = 0;
     
     CalculationCategory category() const { return m_category; }    
@@ -79,23 +79,28 @@
         
 class CSSCalcValue : public CSSValue {
 public:
-    static PassRefPtr<CSSCalcValue> create(CSSParserString name, CSSParserValueList*);
+    static PassRefPtr<CSSCalcValue> create(CSSParserString name, CSSParserValueList*, CalculationPermittedValueRange);
 
     CalculationCategory category() const { return m_expression->category(); }
     bool isInt() const { return m_expression->isInteger(); }    
     double doubleValue() const;
+    bool isNegative() const { return m_expression->doubleValue() < 0; }
     double computeLengthPx(RenderStyle* currentStyle, RenderStyle* rootStyle, double multiplier = 1.0, bool computingFontSize = false) const;
         
     String customCssText() const;
     
 private:    
-    CSSCalcValue(PassRefPtr<CSSCalcExpressionNode> _expression_)
+    CSSCalcValue(PassRefPtr<CSSCalcExpressionNode> _expression_, CalculationPermittedValueRange range)
         : CSSValue(CalculationClass)
         , m_expression(_expression_)
+        , m_nonNegative(range == CalculationRangeNonNegative)
     {
     }
     
+    double clampToPermittedRange(double) const;
+
     const RefPtr<CSSCalcExpressionNode> m_expression;
+    const bool m_nonNegative;
 };
     
 } // namespace WebCore

Modified: trunk/Source/WebCore/css/CSSParser.cpp (108749 => 108750)


--- trunk/Source/WebCore/css/CSSParser.cpp	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/Source/WebCore/css/CSSParser.cpp	2012-02-24 09:33:28 UTC (rev 108750)
@@ -711,7 +711,9 @@
 
 bool CSSParser::validCalculationUnit(CSSParserValue* value, Units unitflags)
 {
-    if (!parseCalculation(value))
+    bool mustBeNonNegative = unitflags & FNonNeg;
+
+    if (!parseCalculation(value, mustBeNonNegative ? CalculationRangeNonNegative : CalculationRangeAll))
         return false;
 
     bool b = false;
@@ -721,17 +723,15 @@
         break;
     case CalcPercent:
         b = (unitflags & FPercent);
-        // FIXME calc (http://webkit.org/b/16662): test FNonNeg here, eg
-        // if (b && (unitflags & FNonNeg) && m_parsedCalculation->doubleValue() < 0)
-        //    b = false;
+        if (b && mustBeNonNegative && m_parsedCalculation->isNegative())
+            b = false;
         break;
     case CalcNumber:
         b = (unitflags & FNumber);
         if (!b && (unitflags & FInteger) && m_parsedCalculation->isInt())
             b = true;
-        // FIXME calc (http://webkit.org/b/16662): test FNonNeg here, eg
-        // if (b && (unitflags & FNonNeg) && m_parsedCalculation->doubleValue() < 0)
-        //    b = false;
+        if (b && mustBeNonNegative && m_parsedCalculation->isNegative())
+            b = false;
         break;
     case CalcPercentLength:
         b = (unitflags & FPercent) && (unitflags & FLength);
@@ -7419,7 +7419,7 @@
     return true;
 }
 
-bool CSSParser::parseCalculation(CSSParserValue* value) 
+bool CSSParser::parseCalculation(CSSParserValue* value, CalculationPermittedValueRange range) 
 {
     ASSERT(isCalculation(value));
     
@@ -7428,7 +7428,7 @@
         return false;
 
     ASSERT(!m_parsedCalculation);
-    m_parsedCalculation = CSSCalcValue::create(value->function->name, args);
+    m_parsedCalculation = CSSCalcValue::create(value->function->name, args, range);
     
     if (!m_parsedCalculation)
         return false;

Modified: trunk/Source/WebCore/css/CSSParser.h (108749 => 108750)


--- trunk/Source/WebCore/css/CSSParser.h	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/Source/WebCore/css/CSSParser.h	2012-02-24 09:33:28 UTC (rev 108750)
@@ -209,7 +209,7 @@
     bool parseTextEmphasisStyle(bool important);
 
     bool parseLineBoxContain(bool important);
-    bool parseCalculation(CSSParserValue*);
+    bool parseCalculation(CSSParserValue*, CalculationPermittedValueRange);
 
     bool parseFontFeatureTag(CSSValueList*);
     bool parseFontFeatureSettings(bool important);

Modified: trunk/Source/WebCore/platform/CalculationValue.h (108749 => 108750)


--- trunk/Source/WebCore/platform/CalculationValue.h	2012-02-24 09:31:07 UTC (rev 108749)
+++ trunk/Source/WebCore/platform/CalculationValue.h	2012-02-24 09:33:28 UTC (rev 108750)
@@ -43,9 +43,14 @@
     CalcAdd = '+',
     CalcSubtract = '-',
     CalcMultiply = '*',
-    CalcDivide = '/',
+    CalcDivide = '/'
 };
 
+enum CalculationPermittedValueRange {
+    CalculationRangeAll,
+    CalculationRangeNonNegative
+};
+
 } // namespace WebCore
 
 #endif // CalculationValue_h
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to