diff options
| author | meade <meade@chromium.org> | 2016-01-05 18:04:26 -0800 |
|---|---|---|
| committer | Commit bot <commit-bot@chromium.org> | 2016-01-06 02:05:20 +0000 |
| commit | dfb22e3e430dec766ae723225c1d8ad2fa9c245c (patch) | |
| tree | 25e4c5543f4c77a6ba5794b7db091d10b49528fa | |
| parent | 23ea0f5c54db0b62d3bc0c23a8bcb2560734e852 (diff) | |
| download | chromium_src-dfb22e3e430dec766ae723225c1d8ad2fa9c245c.zip chromium_src-dfb22e3e430dec766ae723225c1d8ad2fa9c245c.tar.gz chromium_src-dfb22e3e430dec766ae723225c1d8ad2fa9c245c.tar.bz2 | |
Add SimpleLength object for CSS Typed OM.
BUG=545318
Review URL: https://codereview.chromium.org/1419853005
Cr-Commit-Position: refs/heads/master@{#367749}
9 files changed, 260 insertions, 7 deletions
diff --git a/third_party/WebKit/LayoutTests/typedcssom/simplelength.html b/third_party/WebKit/LayoutTests/typedcssom/simplelength.html new file mode 100644 index 0000000..0ca8983 --- /dev/null +++ b/third_party/WebKit/LayoutTests/typedcssom/simplelength.html @@ -0,0 +1,112 @@ +<!DOCTYPE html> +<script src='../resources/testharness.js'></script> +<script src='../resources/testharnessreport.js'></script> + +<script> + +test(function() { + var simpleLength1 = new SimpleLength(5.1, 'px'); + var simpleLength2 = new SimpleLength(10, 'px'); + + var result = simpleLength1.add(simpleLength2); + + assert_not_equals(simpleLength1, result); + assert_not_equals(simpleLength2, result); + assert_true(result instanceof SimpleLength); + assert_equals(result.value, 15.1); + assert_equals(result.type, 'px'); +}, 'Test that adding SimpleLengths produces a new SimpleLength with the correct value.'); + +test(function() { + var simpleLength1 = new SimpleLength(5.1, 'px'); + var simpleLength2 = new SimpleLength(10, 'px'); + + var result = simpleLength1.subtract(simpleLength2); + + assert_not_equals(simpleLength1, result); + assert_not_equals(simpleLength2, result); + assert_true(result instanceof SimpleLength); + assert_equals(result.value, -4.9); + assert_equals(result.type, 'px'); +}, 'Test that subtracting SimpleLengths produces a new SimpleLength with the correct value.'); + +test(function() { + var simpleLength = new SimpleLength(5.2, 'px'); + var result = simpleLength.multiply(4); + + assert_not_equals(simpleLength, result); + assert_true(result instanceof SimpleLength); + assert_approx_equals(result.value, 20.8, 0.00000001); + assert_equals(result.type, 'px'); +}, 'Test that multiplying a SimpleLength produces a new SimpleLength with the correct value.'); + +test(function() { + var simpleLength = new SimpleLength(25, 'px'); + var result = simpleLength.divide(2); + + assert_not_equals(simpleLength, result); + assert_true(result instanceof SimpleLength); + assert_equals(result.value, 12.5); + assert_equals(result.type, 'px'); +}, 'Test that dividing a SimpleLength produces a new SimpleLength with the correct value.'); + +test(function() { + var values = [ + {input: new SimpleLength(1, 'px'), cssString: '1px' }, + {input: new SimpleLength(2, 'percent'), cssString: '2%' }, + {input: new SimpleLength(3, '%'), cssString: '3%' }, + {input: new SimpleLength(4, 'em'), cssString: '4em' }, + {input: new SimpleLength(5, 'ex'), cssString: '5ex' }, + {input: new SimpleLength(6, 'ch'), cssString: '6ch' }, + {input: new SimpleLength(7, 'rem'), cssString: '7rem' }, + {input: new SimpleLength(8, 'vw'), cssString: '8vw' }, + {input: new SimpleLength(9, 'vh'), cssString: '9vh' }, + {input: new SimpleLength(10, 'vmin'), cssString: '10vmin' }, + {input: new SimpleLength(11, 'vmax'), cssString: '11vmax' }, + {input: new SimpleLength(12, 'cm'), cssString: '12cm' }, + {input: new SimpleLength(13, 'mm'), cssString: '13mm' }, + {input: new SimpleLength(14, 'in'), cssString: '14in' }, + {input: new SimpleLength(15, 'pc'), cssString: '15pc' }, + {input: new SimpleLength(16, 'pt'), cssString: '16pt' }, + // Same again to double check that it's case insensitive. + {input: new SimpleLength(1, 'PX'), cssString: '1px' }, + {input: new SimpleLength(2, 'PERCENT'), cssString: '2%' }, + {input: new SimpleLength(3, '%'), cssString: '3%' }, + {input: new SimpleLength(4, 'EM'), cssString: '4em' }, + {input: new SimpleLength(5, 'EX'), cssString: '5ex' }, + {input: new SimpleLength(6, 'CH'), cssString: '6ch' }, + {input: new SimpleLength(7, 'REM'), cssString: '7rem' }, + {input: new SimpleLength(8, 'VW'), cssString: '8vw' }, + {input: new SimpleLength(9, 'VH'), cssString: '9vh' }, + {input: new SimpleLength(10, 'VMIN'), cssString: '10vmin' }, + {input: new SimpleLength(11, 'VMAX'), cssString: '11vmax' }, + {input: new SimpleLength(12, 'CM'), cssString: '12cm' }, + {input: new SimpleLength(13, 'MM'), cssString: '13mm' }, + {input: new SimpleLength(14, 'IN'), cssString: '14in' }, + {input: new SimpleLength(15, 'PC'), cssString: '15pc' }, + {input: new SimpleLength(16, 'PT'), cssString: '16pt' }, + ]; + + for (var i = 0; i < values.length; ++i) { + assert_equals(values[i].input.cssString, values[i].cssString); + } +}, 'Test that the SimpleLength css string is generated correctly for each unit type.'); + +test(function() { + var values = [ + {value: NaN, unit: 'px'}, + {value: Infinity, unit: 'px'}, + {value: -Infinity, unit: 'px'}, + {value: 5, unit: 'puppies'} + ]; + + for (var i = 0; i < values.length; ++i) { + assert_throws(null, function() { new SimpleLength(values[i].value, values[i].unit); }); + } + +}, 'Test that invalid input throws an exception.'); + +</script> + +<body> +</body> diff --git a/third_party/WebKit/LayoutTests/webexposed/global-interface-listing-expected.txt b/third_party/WebKit/LayoutTests/webexposed/global-interface-listing-expected.txt index c7d93e6..5af0ae9e 100644 --- a/third_party/WebKit/LayoutTests/webexposed/global-interface-listing-expected.txt +++ b/third_party/WebKit/LayoutTests/webexposed/global-interface-listing-expected.txt @@ -5124,6 +5124,11 @@ interface SharedWorker : EventTarget getter workerStart method constructor setter onerror +interface SimpleLength : LengthValue + getter type + getter value + method constructor + setter value interface SpeechSynthesisEvent : Event getter charIndex getter elapsedTime diff --git a/third_party/WebKit/Source/core/core.gypi b/third_party/WebKit/Source/core/core.gypi index efab9ee..471e737 100644 --- a/third_party/WebKit/Source/core/core.gypi +++ b/third_party/WebKit/Source/core/core.gypi @@ -42,6 +42,7 @@ 'css/cssom/KeywordValue.idl', 'css/cssom/LengthValue.idl', 'css/cssom/NumberValue.idl', + 'css/cssom/SimpleLength.idl', 'css/cssom/StyleValue.idl', 'dom/ArrayBuffer.idl', 'dom/ArrayBufferView.idl', @@ -1245,6 +1246,8 @@ 'css/cssom/LengthValue.cpp', 'css/cssom/LengthValue.h', 'css/cssom/NumberValue.h', + 'css/cssom/SimpleLength.cpp', + 'css/cssom/SimpleLength.h', 'css/cssom/StyleValue.cpp', 'css/cssom/StyleValue.h', 'css/invalidation/InvalidationSet.cpp', diff --git a/third_party/WebKit/Source/core/css/cssom/LengthValue.cpp b/third_party/WebKit/Source/core/css/cssom/LengthValue.cpp index 77302ef..abd80f9 100644 --- a/third_party/WebKit/Source/core/css/cssom/LengthValue.cpp +++ b/third_party/WebKit/Source/core/css/cssom/LengthValue.cpp @@ -29,7 +29,6 @@ UnitTable createStrToLenUnitTable() table.set(String("vmax"), LengthValue::Vmax); table.set(String("cm"), LengthValue::Cm); table.set(String("mm"), LengthValue::Mm); - table.set(String("q"), LengthValue::QUnit); table.set(String("in"), LengthValue::In); table.set(String("pc"), LengthValue::Pc); table.set(String("pt"), LengthValue::Pt); @@ -86,7 +85,9 @@ PassRefPtrWillBeRawPtr<LengthValue> LengthValue::divide(double x, ExceptionState LengthValue::LengthUnit LengthValue::lengthUnitFromName(const String& str) { - return typeTable().get(str.lower()); + if (typeTable().contains(str.lower())) + return typeTable().get(str.lower()); + return LengthUnit::Count; } const String& LengthValue::lengthTypeToString(LengthValue::LengthUnit unit) @@ -103,7 +104,6 @@ const String& LengthValue::lengthTypeToString(LengthValue::LengthUnit unit) DEFINE_STATIC_LOCAL(const String, VmaxStr, ("vmax")); DEFINE_STATIC_LOCAL(const String, CmStr, ("cm")); DEFINE_STATIC_LOCAL(const String, MmStr, ("mm")); - DEFINE_STATIC_LOCAL(const String, QStr, ("q")); DEFINE_STATIC_LOCAL(const String, InStr, ("in")); DEFINE_STATIC_LOCAL(const String, PcStr, ("pc")); DEFINE_STATIC_LOCAL(const String, PtStr, ("pt")); @@ -133,8 +133,6 @@ const String& LengthValue::lengthTypeToString(LengthValue::LengthUnit unit) return CmStr; case Mm: return MmStr; - case QUnit: - return QStr; case In: return InStr; case Pc: diff --git a/third_party/WebKit/Source/core/css/cssom/LengthValue.h b/third_party/WebKit/Source/core/css/cssom/LengthValue.h index b1377e7..43bcc3f 100644 --- a/third_party/WebKit/Source/core/css/cssom/LengthValue.h +++ b/third_party/WebKit/Source/core/css/cssom/LengthValue.h @@ -27,7 +27,6 @@ public: Vmax, Cm, Mm, - QUnit, In, Pc, Pt, diff --git a/third_party/WebKit/Source/core/css/cssom/LengthValue.idl b/third_party/WebKit/Source/core/css/cssom/LengthValue.idl index d2cbca3..0b68cb1 100644 --- a/third_party/WebKit/Source/core/css/cssom/LengthValue.idl +++ b/third_party/WebKit/Source/core/css/cssom/LengthValue.idl @@ -3,7 +3,7 @@ // found in the LICENSE file. enum LengthType { - "px", "percent", "em", "ex", "ch", "rem", "vw", "vh", "vmin", "vmax", "cm", "mm", "q", "in", "pc", "pt" + "px", "percent", "em", "ex", "ch", "rem", "vw", "vh", "vmin", "vmax", "cm", "mm", "in", "pc", "pt" }; [ diff --git a/third_party/WebKit/Source/core/css/cssom/SimpleLength.cpp b/third_party/WebKit/Source/core/css/cssom/SimpleLength.cpp new file mode 100644 index 0000000..028ebb2 --- /dev/null +++ b/third_party/WebKit/Source/core/css/cssom/SimpleLength.cpp @@ -0,0 +1,58 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/css/cssom/SimpleLength.h" + +#include "core/css/CSSPrimitiveValue.h" +#include "wtf/text/StringBuilder.h" + +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())); +} + +PassRefPtrWillBeRawPtr<LengthValue> SimpleLength::addInternal(const LengthValue* other, ExceptionState& exceptionState) +{ + const SimpleLength* o = toSimpleLength(other); + if (m_unit == o->m_unit) + return create(m_value + o->value(), m_unit); + + // Different units resolve to a calc. + exceptionState.throwTypeError("Not implemented yet"); + return nullptr; +} + +PassRefPtrWillBeRawPtr<LengthValue> SimpleLength::subtractInternal(const LengthValue* other, ExceptionState& exceptionState) +{ + const SimpleLength* o = toSimpleLength(other); + if (m_unit == o->m_unit) + return create(m_value - o->value(), m_unit); + + // Different units resolve to a calc. + exceptionState.throwTypeError("Not implemented yet"); + return nullptr; +} + +PassRefPtrWillBeRawPtr<LengthValue> SimpleLength::multiplyInternal(double x, ExceptionState& exceptionState) +{ + return create(m_value * x, m_unit); +} + +PassRefPtrWillBeRawPtr<LengthValue> SimpleLength::divideInternal(double x, ExceptionState& exceptionState) +{ + return create(m_value / x, m_unit); +} + +} // namespace blink diff --git a/third_party/WebKit/Source/core/css/cssom/SimpleLength.h b/third_party/WebKit/Source/core/css/cssom/SimpleLength.h new file mode 100644 index 0000000..5d42f75 --- /dev/null +++ b/third_party/WebKit/Source/core/css/cssom/SimpleLength.h @@ -0,0 +1,66 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SimpleLength_h +#define SimpleLength_h + +#include "bindings/core/v8/ExceptionState.h" +#include "core/css/cssom/LengthValue.h" + +namespace blink { + +class CSSPrimitiveValue; + +class CORE_EXPORT SimpleLength : public LengthValue { + DEFINE_WRAPPERTYPEINFO(); +public: + static PassRefPtrWillBeRawPtr<SimpleLength> create(double value, const String& type, ExceptionState& exceptionState) + { + LengthUnit unit = LengthValue::lengthUnitFromName(type); + if (unit == LengthUnit::Count) { + exceptionState.throwTypeError("Invalid unit for SimpleLength."); + return nullptr; + } + return adoptRefWillBeNoop(new SimpleLength(value, unit)); + } + + static PassRefPtrWillBeRawPtr<SimpleLength> create(double value, LengthUnit type) + { + return adoptRefWillBeNoop(new SimpleLength(value, type)); + } + + double value() const { return m_value; } + String unit() const { return LengthValue::lengthTypeToString(m_unit); } + LengthUnit lengthUnit() const { return m_unit; } + + void setValue(double value) { m_value = value; } + + StyleValueType type() const override { return StyleValueType::SimpleLengthType; } + + String cssString() const override; + PassRefPtrWillBeRawPtr<CSSValue> toCSSValue() const override; + +protected: + SimpleLength(double value, LengthUnit unit) : LengthValue(), m_unit(unit), m_value(value) {} + + virtual PassRefPtrWillBeRawPtr<LengthValue> addInternal(const LengthValue* other, ExceptionState&); + virtual PassRefPtrWillBeRawPtr<LengthValue> subtractInternal(const LengthValue* other, ExceptionState&); + virtual PassRefPtrWillBeRawPtr<LengthValue> multiplyInternal(double, ExceptionState&); + virtual PassRefPtrWillBeRawPtr<LengthValue> divideInternal(double, ExceptionState&); + + LengthUnit m_unit; + double m_value; +}; + +#define DEFINE_SIMPLE_LENGTH_TYPE_CASTS(argumentType) \ + DEFINE_TYPE_CASTS(SimpleLength, argumentType, value, \ + value->type() == LengthValue::StyleValueType::SimpleLengthType, \ + value.type() == LengthValue::StyleValueType::SimpleLengthType) + +DEFINE_SIMPLE_LENGTH_TYPE_CASTS(LengthValue); +DEFINE_SIMPLE_LENGTH_TYPE_CASTS(StyleValue); + +} // namespace blink + +#endif diff --git a/third_party/WebKit/Source/core/css/cssom/SimpleLength.idl b/third_party/WebKit/Source/core/css/cssom/SimpleLength.idl new file mode 100644 index 0000000..07c0b49 --- /dev/null +++ b/third_party/WebKit/Source/core/css/cssom/SimpleLength.idl @@ -0,0 +1,12 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +[ + Constructor(double value, DOMString type), + RaisesException=Constructor, + RuntimeEnabled=CSSTypedOM +] interface SimpleLength : LengthValue { + [EnforceRange] attribute double value; + [ImplementedAs=unit] readonly attribute LengthType type; +}; |
