Commit 13eeea65 authored by meade's avatar meade Committed by Commit bot

Update StyleValue.cssString() to use existing methods instead of rolling our own.

All StyleValues now use toCSSValue()->cssText() instead of defining
custom methods in subclasses.

Conveniently, this effectively allows toCSSValue to be tested in
LayoutTests. Also get rid of re-parsing unit types in
SimpleLength.toCSSValue, since the code there had a bug anyway
which was uncovered by the LayoutTests.

BUG=545318

Review URL: https://codereview.chromium.org/1602843003

Cr-Commit-Position: refs/heads/master@{#370341}
parent d6b55d45
......@@ -53,6 +53,23 @@ test(function() {
assert_equals(result.percent, 5.2);
}, 'Test that dividing a CalcLength produces a new CalcLength with the correct values.');
test(function() {
var values = [
{input: new CalcLength({px: 1}), cssString: 'calc(1px)'},
{input: new CalcLength({px: -1}), cssString: 'calc(-1px)'},
{input: new CalcLength({px: 1, percent: 15.6}), cssString: 'calc(1px + 15.6%)'},
{input: new CalcLength({px: 1, percent: -15.6}), cssString: 'calc(1px - 15.6%)'},
{input: new CalcLength({px: -1, percent: -15.6}), cssString: 'calc(-1px - 15.6%)'},
{input: new CalcLength({px: -1, percent: -15.6, vw: 5}), cssString: 'calc((-1px - 15.6%) + 5vw)'},
{input: new CalcLength({px: -1, percent: -15.6, vw: -5}), cssString: 'calc((-1px - 15.6%) - 5vw)'},
];
for (var i = 0; i < values.length; ++i) {
assert_equals(values[i].input.cssString, values[i].cssString);
}
}, 'Test that the CSS string method for a CalcLength produces the correct result');
</script>
<body>
......
......@@ -30,11 +30,6 @@ KeywordTable& keywordTable()
} // namespace
String KeywordValue::cssString() const
{
return m_keywordValue;
}
const String& KeywordValue::keywordValue() const
{
return m_keywordValue;
......
......@@ -27,7 +27,6 @@ public:
const String& keywordValue() const;
String cssString() const override;
PassRefPtrWillBeRawPtr<CSSValue> toCSSValue() const override;
private:
......
......@@ -161,6 +161,45 @@ const String& LengthValue::lengthTypeToString(LengthValue::LengthUnit unit)
}
}
CSSPrimitiveValue::UnitType LengthValue::lengthTypeToPrimitiveType(LengthValue::LengthUnit unit)
{
switch (unit) {
case Px:
return CSSPrimitiveValue::UnitType::Pixels;
case Percent:
return CSSPrimitiveValue::UnitType::Percentage;
case Em:
return CSSPrimitiveValue::UnitType::Ems;
case Ex:
return CSSPrimitiveValue::UnitType::Exs;
case Ch:
return CSSPrimitiveValue::UnitType::Chs;
case Rem:
return CSSPrimitiveValue::UnitType::Rems;
case Vw:
return CSSPrimitiveValue::UnitType::ViewportWidth;
case Vh:
return CSSPrimitiveValue::UnitType::ViewportHeight;
case Vmin:
return CSSPrimitiveValue::UnitType::ViewportMin;
case Vmax:
return CSSPrimitiveValue::UnitType::ViewportMax;
case Cm:
return CSSPrimitiveValue::UnitType::Centimeters;
case Mm:
return CSSPrimitiveValue::UnitType::Millimeters;
case In:
return CSSPrimitiveValue::UnitType::Inches;
case Pc:
return CSSPrimitiveValue::UnitType::Picas;
case Pt:
return CSSPrimitiveValue::UnitType::Points;
default:
ASSERT_NOT_REACHED();
return CSSPrimitiveValue::UnitType::Pixels;
}
}
LengthValue* LengthValue::addInternal(const LengthValue*, ExceptionState&)
{
ASSERT_NOT_REACHED();
......
......@@ -50,7 +50,8 @@ protected:
LengthValue() {}
static LengthUnit lengthUnitFromName(const String&);
static const String& lengthTypeToString(LengthUnit type);
static const String& lengthTypeToString(LengthUnit);
static CSSPrimitiveValue::UnitType lengthTypeToPrimitiveType(LengthUnit);
virtual LengthValue* addInternal(const LengthValue* other, ExceptionState&);
virtual LengthValue* subtractInternal(const LengthValue* other, ExceptionState&);
......
......@@ -28,8 +28,6 @@ public:
m_value = value;
}
String cssString() const override { return String::number(m_value); }
PassRefPtrWillBeRawPtr<CSSValue> toCSSValue() const override
{
return cssValuePool().createValue(m_value, CSSPrimitiveValue::UnitType::
......
......@@ -10,18 +10,9 @@
namespace blink {
String SimpleLength::cssString() const
{
StringBuilder s;
s.appendNumber(m_value);
s.append(unit());
return s.toString();
}
PassRefPtrWillBeRawPtr<CSSValue> SimpleLength::toCSSValue() const
{
// TODO: Don't re-parse the unit.
return cssValuePool().createValue(m_value, CSSPrimitiveValue::fromName(unit()));
return cssValuePool().createValue(m_value, LengthValue::lengthTypeToPrimitiveType(m_unit));
}
LengthValue* SimpleLength::addInternal(const LengthValue* other, ExceptionState& exceptionState)
......
......@@ -39,7 +39,6 @@ public:
StyleValueType type() const override { return StyleValueType::SimpleLengthType; }
String cssString() const override;
PassRefPtrWillBeRawPtr<CSSValue> toCSSValue() const override;
protected:
......
......@@ -124,29 +124,6 @@ LengthValue* StyleCalcLength::divideInternal(double x, ExceptionState& exception
return result;
}
String StyleCalcLength::cssString() const
{
StringBuilder builder;
builder.appendLiteral("calc(");
for (unsigned i = 0; i < LengthUnit::Count; ++i) {
LengthUnit lengthUnit = static_cast<LengthUnit>(i);
if (has(lengthUnit)) {
double value = get(lengthUnit);
if (value >= 0 && i > 0) {
builder.appendLiteral(" + ");
} else if (value < 0 && i > 0) {
builder.appendLiteral(" - ");
} else if (value < 0) {
builder.append('-');
}
builder.appendNumber(std::abs(get(lengthUnit)));
builder.append(lengthTypeToString(lengthUnit));
}
}
builder.append(')');
return builder.toString();
}
PassRefPtrWillBeRawPtr<CSSValue> StyleCalcLength::toCSSValue() const
{
// Create a CSS Calc Value, then put it into a CSSPrimitiveValue
......@@ -154,20 +131,14 @@ PassRefPtrWillBeRawPtr<CSSValue> StyleCalcLength::toCSSValue() const
for (unsigned i = 0; i < LengthUnit::Count; ++i) {
LengthUnit lengthUnit = static_cast<LengthUnit>(i);
if (!has(lengthUnit))
break;
continue;
double value = get(lengthUnit);
CSSPrimitiveValue::UnitType primitiveUnit;
if (lengthUnit == LengthUnit::Percent) {
primitiveUnit = CSSPrimitiveValue::UnitType::Percentage;
} else {
// TODO: Don't re-parse the unit here.
primitiveUnit = CSSPrimitiveValue::fromName(lengthTypeToString(lengthUnit));
}
CSSPrimitiveValue::UnitType primitiveUnit = lengthTypeToPrimitiveType(lengthUnit);
if (node) {
node = CSSCalcValue::createExpressionNode(
node,
CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(value, primitiveUnit)),
CalcAdd);
CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(std::abs(value), primitiveUnit)),
value >= 0 ? CalcAdd : CalcSubtract);
} else {
node = CSSCalcValue::createExpressionNode(CSSPrimitiveValue::create(value, primitiveUnit));
}
......
......@@ -44,7 +44,6 @@ public:
#undef GETTER_MACRO
String cssString() const override;
PassRefPtrWillBeRawPtr<CSSValue> toCSSValue() const override;
StyleValueType type() const override { return CalcLengthType; }
......
......@@ -30,8 +30,11 @@ public:
static StyleValue* create(const CSSValue&);
static ScriptValue parse(ScriptState*, const String& property, const String& cssText);
virtual String cssString() const = 0;
virtual PassRefPtrWillBeRawPtr<CSSValue> toCSSValue() const = 0;
virtual String cssString() const
{
return toCSSValue()->cssText();
}
DEFINE_INLINE_VIRTUAL_TRACE() { }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment