Skip to content
Permalink
Browse files
convertingToLengthRequiresNonNullStyle needs to consider calc values
https://bugs.webkit.org/show_bug.cgi?id=241452

Reviewed by Antti Koivisto.

* LayoutTests/fast/css/matrix-translate-calc-units.html: Added.
* Source/WebCore/css/CSSPrimitiveValueMappings.h:
(WebCore::CSSPrimitiveValue::convertingToLengthRequiresNonNullStyle const):
(WebCore::CSSPrimitiveValue::convertToLength const):
* Source/WebCore/css/TransformFunctions.cpp:
* Source/WebCore/css/calc/CSSCalcExpressionNode.h:
* Source/WebCore/css/calc/CSSCalcInvertNode.h:
* Source/WebCore/css/calc/CSSCalcNegateNode.h:
* Source/WebCore/css/calc/CSSCalcOperationNode.cpp:
(WebCore::CSSCalcOperationNode::convertingToLengthRequiresNonNullStyle const):
* Source/WebCore/css/calc/CSSCalcOperationNode.h:
* Source/WebCore/css/calc/CSSCalcPrimitiveValueNode.cpp:
(WebCore::CSSCalcPrimitiveValueNode::convertingToLengthRequiresNonNullStyle const):
* Source/WebCore/css/calc/CSSCalcPrimitiveValueNode.h:
* Source/WebCore/css/calc/CSSCalcValue.cpp:
(WebCore::CSSCalcValue::convertingToLengthRequiresNonNullStyle const):
* Source/WebCore/css/calc/CSSCalcValue.h:

Canonical link: https://commits.webkit.org/251431@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295425 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
achristensen07 committed Jun 9, 2022
1 parent dd346ad commit b5884ef80c5fde05d167160b20d738958a30b01b
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 2 deletions.
@@ -0,0 +1,15 @@
Verify invalid DOMMatrix strings throw, and more importantly don't crash

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS new DOMMatrix('translate(min(0em))') threw exception SyntaxError: The string did not match the expected pattern..
PASS new DOMMatrix('translateX(max(0ex))') threw exception SyntaxError: The string did not match the expected pattern..
PASS new DOMMatrix('translateY(calc(0ch))') threw exception SyntaxError: The string did not match the expected pattern..
PASS new DOMMatrix('translateZ(calc(11ic))') threw exception SyntaxError: The string did not match the expected pattern..
PASS new DOMMatrix('translateZ(calc(123lh))') threw exception SyntaxError: The string did not match the expected pattern..
PASS new DOMMatrix('translate(max(123px))') did not throw exception.
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,11 @@
<script src="../../resources/js-test-pre.js"></script>
<script>
description("Verify invalid DOMMatrix strings throw, and more importantly don't crash");
shouldThrow("new DOMMatrix('translate(min(0em))')");
shouldThrow("new DOMMatrix('translateX(max(0ex))')");
shouldThrow("new DOMMatrix('translateY(calc(0ch))')");
shouldThrow("new DOMMatrix('translateZ(calc(11ic))')");
shouldThrow("new DOMMatrix('translateZ(calc(123lh))')");
shouldNotThrow("new DOMMatrix('translate(max(123px))')");
</script>
<script src="../../resources/js-test-post.js"></script>
@@ -4262,23 +4262,28 @@ enum LengthConversion {

inline bool CSSPrimitiveValue::convertingToLengthRequiresNonNullStyle(int lengthConversion) const
{
ASSERT(isFontRelativeLength());
// This matches the implementation in CSSPrimitiveValue::computeLengthDouble().
//
// FIXME: We should probably make CSSPrimitiveValue::computeLengthDouble and
// CSSPrimitiveValue::computeNonCalcLengthDouble (which has the style assertion)
// return std::optional<double> instead of having this check here.
switch (primitiveUnitType()) {
case CSSUnitType::CSS_EMS:
case CSSUnitType::CSS_EXS:
case CSSUnitType::CSS_CHS:
case CSSUnitType::CSS_IC:
case CSSUnitType::CSS_LHS:
return lengthConversion & (FixedIntegerConversion | FixedFloatConversion);
case CSSUnitType::CSS_CALC:
return m_value.calc->convertingToLengthRequiresNonNullStyle(lengthConversion);
default:
return false;
}
}

template<int supported> Length CSSPrimitiveValue::convertToLength(const CSSToLengthConversionData& conversionData) const
{
if (isFontRelativeLength() && convertingToLengthRequiresNonNullStyle(supported) && !conversionData.style())
if (convertingToLengthRequiresNonNullStyle(supported) && !conversionData.style())
return Length(LengthType::Undefined);
if ((supported & FixedIntegerConversion) && isLength())
return computeLength<Length>(conversionData);
@@ -102,6 +102,7 @@ Length convertToFloatLength(const CSSPrimitiveValue* primitiveValue, const CSSTo
return primitiveValue ? primitiveValue->convertToLength<FixedFloatConversion | PercentConversion | CalculatedConversion>(conversionData) : Length(LengthType::Undefined);
}

// FIXME: This should return std::optional<TransformOperations>
bool transformsForValue(const CSSValue& value, const CSSToLengthConversionData& conversionData, TransformOperations& outOperations)
{
ASSERT(!outOperations.size());
@@ -59,6 +59,8 @@ class CSSCalcExpressionNode : public RefCounted<CSSCalcExpressionNode> {

virtual void collectDirectComputationalDependencies(HashSet<CSSPropertyID>&) const = 0;
virtual void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>&) const = 0;
virtual bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const = 0;


CalculationCategory category() const { return m_category; }

@@ -58,6 +58,7 @@ class CSSCalcInvertNode final : public CSSCalcExpressionNode {

void collectDirectComputationalDependencies(HashSet<CSSPropertyID>& properties) const final { m_child->collectDirectComputationalDependencies(properties); }
void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>& properties) const final { m_child->collectDirectRootComputationalDependencies(properties); }
bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const final { return m_child->convertingToLengthRequiresNonNullStyle(lengthConversion); }

void dump(TextStream&) const final;

@@ -58,6 +58,7 @@ class CSSCalcNegateNode final : public CSSCalcExpressionNode {

void collectDirectComputationalDependencies(HashSet<CSSPropertyID>& properties) const final { m_child->collectDirectComputationalDependencies(properties); }
void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>& properties) const final { m_child->collectDirectRootComputationalDependencies(properties); }
bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const final { return m_child->convertingToLengthRequiresNonNullStyle(lengthConversion); }

void dump(TextStream&) const final;

@@ -35,6 +35,7 @@
#include "CSSUnits.h"
#include "CalcExpressionOperation.h"
#include "Logging.h"
#include <wtf/Algorithms.h>
#include <wtf/text/TextStream.h>

namespace WebCore {
@@ -1074,6 +1075,13 @@ void CSSCalcOperationNode::collectDirectRootComputationalDependencies(HashSet<CS
child->collectDirectRootComputationalDependencies(values);
}

bool CSSCalcOperationNode::convertingToLengthRequiresNonNullStyle(int lengthConversion) const
{
return WTF::anyOf(m_children, [lengthConversion] (auto& child) {
return child->convertingToLengthRequiresNonNullStyle(lengthConversion);
});
}

void CSSCalcOperationNode::buildCSSText(const CSSCalcExpressionNode& node, StringBuilder& builder)
{
auto shouldOutputEnclosingCalc = [](const CSSCalcExpressionNode& rootNode) {
@@ -117,6 +117,7 @@ class CSSCalcOperationNode final : public CSSCalcExpressionNode {

void collectDirectComputationalDependencies(HashSet<CSSPropertyID>&) const final;
void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>&) const final;
bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const final;

void dump(TextStream&) const final;

@@ -211,6 +211,11 @@ void CSSCalcPrimitiveValueNode::collectDirectRootComputationalDependencies(HashS
m_value->collectDirectRootComputationalDependencies(values);
}

bool CSSCalcPrimitiveValueNode::convertingToLengthRequiresNonNullStyle(int lengthConversion) const
{
return m_value->convertingToLengthRequiresNonNullStyle(lengthConversion);
}

bool CSSCalcPrimitiveValueNode::isZero() const
{
return !m_value->doubleValue();
@@ -74,6 +74,7 @@ class CSSCalcPrimitiveValueNode final : public CSSCalcExpressionNode {
double computeLengthPx(const CSSToLengthConversionData&) const final;
void collectDirectComputationalDependencies(HashSet<CSSPropertyID>&) const final;
void collectDirectRootComputationalDependencies(HashSet<CSSPropertyID>&) const final;
bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const final;

void dump(TextStream&) const final;

@@ -340,6 +340,11 @@ double CSSCalcValue::computeLengthPx(const CSSToLengthConversionData& conversion
return clampToPermittedRange(m_expression->computeLengthPx(conversionData));
}

bool CSSCalcValue::convertingToLengthRequiresNonNullStyle(int lengthConversion) const
{
return m_expression->convertingToLengthRequiresNonNullStyle(lengthConversion);
}

bool CSSCalcValue::isCalcFunction(CSSValueID functionId)
{
switch (functionId) {
@@ -73,6 +73,8 @@ class CSSCalcValue final : public CSSValue {

void dump(TextStream&) const;

bool convertingToLengthRequiresNonNullStyle(int lengthConversion) const;

private:
CSSCalcValue(Ref<CSSCalcExpressionNode>&&, bool shouldClampToNonNegative);

0 comments on commit b5884ef

Please sign in to comment.