Title: [124549] trunk
Revision
124549
Author
[email protected]
Date
2012-08-02 19:05:51 -0700 (Thu, 02 Aug 2012)

Log Message

Slider should snap to datalist tick marks
https://bugs.webkit.org/show_bug.cgi?id=92640

Reviewed by Kent Tamura.

Source/WebCore:

Input type=range slider snaps to datalist tick marks.

Test: fast/forms/datalist/range-snap-to-datalist.html

* html/HTMLInputElement.cpp:
(WebCore::HTMLInputElement::findClosestTickMarkValue): Just calls InputType::findClosestTickMarkValue.
(WebCore):
* html/HTMLInputElement.h:
(HTMLInputElement):
* html/InputType.cpp:
(WebCore):
(WebCore::InputType::findClosestTickMarkValue): Overridden by RangeInputType.
* html/InputType.h:
(InputType):
* html/RangeInputType.cpp:
(WebCore::RangeInputType::RangeInputType):
(WebCore):
(WebCore::RangeInputType::listAttributeTargetChanged): Mark m_tickMarkValues as dirty.
(WebCore::decimalCompare):
(WebCore::RangeInputType::updateTickMarkValues): Sets m_tickMarkValues to a list of sorted valid datalist values.
(WebCore::RangeInputType::findClosestTickMarkValue): Finds closest tick mark value to a given value.
* html/RangeInputType.h:
(RangeInputType):
* html/shadow/SliderThumbElement.cpp:
(WebCore):
(WebCore::SliderThumbElement::setPositionFromPoint): Snaps to the closest tick mark value if it is within a certain distance.

LayoutTests:

* fast/forms/datalist/range-snap-to-datalist-expected.txt: Added.
* fast/forms/datalist/range-snap-to-datalist.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (124548 => 124549)


--- trunk/LayoutTests/ChangeLog	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/LayoutTests/ChangeLog	2012-08-03 02:05:51 UTC (rev 124549)
@@ -1,3 +1,13 @@
+2012-08-02  Keishi Hattori  <[email protected]>
+
+        Slider should snap to datalist tick marks
+        https://bugs.webkit.org/show_bug.cgi?id=92640
+
+        Reviewed by Kent Tamura.
+
+        * fast/forms/datalist/range-snap-to-datalist-expected.txt: Added.
+        * fast/forms/datalist/range-snap-to-datalist.html: Added.
+
 2012-08-02  Vincent Scheib  <[email protected]>
 
         Remove old Pointer Lock API.

Added: trunk/LayoutTests/fast/forms/datalist/range-snap-to-datalist-expected.txt (0 => 124549)


--- trunk/LayoutTests/fast/forms/datalist/range-snap-to-datalist-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/forms/datalist/range-snap-to-datalist-expected.txt	2012-08-03 02:05:51 UTC (rev 124549)
@@ -0,0 +1,28 @@
+Tests if clicking close to a tick mark snaps to it.
+
+PASS input.value is "0"
+PASS parseInt(input.value, 10) < 500 is true
+PASS input.value is "0"
+PASS parseInt(input.value, 10) < 500 is true
+PASS input.value is "0"
+PASS parseInt(input.value, 10) < 500 is true
+PASS input.value is "0"
+PASS input.value is '500'
+PASS input.value is "0"
+PASS input.value is '500'
+PASS input.value is "0"
+PASS input.value is '500'
+PASS input.value is "0"
+PASS input.value is '500'
+PASS input.value is "0"
+PASS input.value is '500'
+PASS input.value is "0"
+PASS parseInt(input.value, 10) > 500 is true
+PASS input.value is "0"
+PASS parseInt(input.value, 10) > 500 is true
+PASS input.value is "0"
+PASS parseInt(input.value, 10) > 500 is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/forms/datalist/range-snap-to-datalist.html (0 => 124549)


--- trunk/LayoutTests/fast/forms/datalist/range-snap-to-datalist.html	                        (rev 0)
+++ trunk/LayoutTests/fast/forms/datalist/range-snap-to-datalist.html	2012-08-03 02:05:51 UTC (rev 124549)
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<p id="description">Tests if clicking close to a tick mark snaps to it.</p>
+<div id="console"></div>
+<input id="input" type=range min=0 max=1000 step=1 list=target style="width: 100px">
+<datalist id="target">
+    <option>500</option>
+</datalist>
+<script>
+var input = document.getElementById("input");
+function clickSlider(offsetLeft) {
+    var centerY = input.offsetTop + input.offsetHeight / 2;
+    eventSender.mouseMoveTo(input.offsetLeft + offsetLeft, centerY);
+    eventSender.mouseDown();
+    eventSender.mouseUp();
+}
+function resetSliderPosition() {
+    clickSlider(0);
+    shouldBe('input.value', '"0"');
+}
+resetSliderPosition();
+clickSlider(45);
+shouldBeTrue('parseInt(input.value, 10) < 500');
+resetSliderPosition();
+clickSlider(46);
+shouldBeTrue('parseInt(input.value, 10) < 500');
+resetSliderPosition();
+clickSlider(47);
+shouldBeTrue('parseInt(input.value, 10) < 500');
+resetSliderPosition();
+clickSlider(48);
+shouldBe('input.value', "'500'");
+resetSliderPosition();
+clickSlider(49);
+shouldBe('input.value', "'500'");
+resetSliderPosition();
+clickSlider(50);
+shouldBe('input.value', "'500'");
+resetSliderPosition();
+clickSlider(51);
+shouldBe('input.value', "'500'");
+resetSliderPosition();
+clickSlider(52);
+shouldBe('input.value', "'500'");
+resetSliderPosition();
+clickSlider(53);
+shouldBeTrue('parseInt(input.value, 10) > 500');
+resetSliderPosition();
+clickSlider(54);
+shouldBeTrue('parseInt(input.value, 10) > 500');
+resetSliderPosition();
+clickSlider(55);
+shouldBeTrue('parseInt(input.value, 10) > 500');
+</script>
+
+<script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (124548 => 124549)


--- trunk/Source/WebCore/ChangeLog	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/ChangeLog	2012-08-03 02:05:51 UTC (rev 124549)
@@ -1,3 +1,37 @@
+2012-08-02  Keishi Hattori  <[email protected]>
+
+        Slider should snap to datalist tick marks
+        https://bugs.webkit.org/show_bug.cgi?id=92640
+
+        Reviewed by Kent Tamura.
+
+        Input type=range slider snaps to datalist tick marks.
+
+        Test: fast/forms/datalist/range-snap-to-datalist.html
+
+        * html/HTMLInputElement.cpp:
+        (WebCore::HTMLInputElement::findClosestTickMarkValue): Just calls InputType::findClosestTickMarkValue.
+        (WebCore):
+        * html/HTMLInputElement.h:
+        (HTMLInputElement):
+        * html/InputType.cpp:
+        (WebCore):
+        (WebCore::InputType::findClosestTickMarkValue): Overridden by RangeInputType.
+        * html/InputType.h:
+        (InputType):
+        * html/RangeInputType.cpp:
+        (WebCore::RangeInputType::RangeInputType):
+        (WebCore):
+        (WebCore::RangeInputType::listAttributeTargetChanged): Mark m_tickMarkValues as dirty.
+        (WebCore::decimalCompare):
+        (WebCore::RangeInputType::updateTickMarkValues): Sets m_tickMarkValues to a list of sorted valid datalist values.
+        (WebCore::RangeInputType::findClosestTickMarkValue): Finds closest tick mark value to a given value.
+        * html/RangeInputType.h:
+        (RangeInputType):
+        * html/shadow/SliderThumbElement.cpp:
+        (WebCore):
+        (WebCore::SliderThumbElement::setPositionFromPoint): Snaps to the closest tick mark value if it is within a certain distance.
+
 2012-08-02  Ryuan Choi  <[email protected]>
 
         [EFL] Crash while loading plugin after r121467

Modified: trunk/Source/WebCore/html/HTMLInputElement.cpp (124548 => 124549)


--- trunk/Source/WebCore/html/HTMLInputElement.cpp	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/HTMLInputElement.cpp	2012-08-03 02:05:51 UTC (rev 124549)
@@ -313,6 +313,13 @@
     return m_inputType->createStepRange(anyStepHandling);
 }
 
+#if ENABLE(DATALIST_ELEMENT)
+Decimal HTMLInputElement::findClosestTickMarkValue(const Decimal& value)
+{
+    return m_inputType->findClosestTickMarkValue(value);
+}
+#endif
+
 void HTMLInputElement::stepUp(int n, ExceptionCode& ec)
 {
     m_inputType->stepUp(n, ec);

Modified: trunk/Source/WebCore/html/HTMLInputElement.h (124548 => 124549)


--- trunk/Source/WebCore/html/HTMLInputElement.h	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/HTMLInputElement.h	2012-08-03 02:05:51 UTC (rev 124549)
@@ -72,6 +72,10 @@
     bool getAllowedValueStep(Decimal*) const;
     StepRange createStepRange(AnyStepHandling) const;
 
+#if ENABLE(DATALIST_ELEMENT)
+    Decimal findClosestTickMarkValue(const Decimal&);
+#endif
+
     // Implementations of HTMLInputElement::stepUp() and stepDown().
     void stepUp(int, ExceptionCode&);
     void stepDown(int, ExceptionCode&);

Modified: trunk/Source/WebCore/html/InputType.cpp (124548 => 124549)


--- trunk/Source/WebCore/html/InputType.cpp	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/InputType.cpp	2012-08-03 02:05:51 UTC (rev 124549)
@@ -908,6 +908,12 @@
 void InputType::listAttributeTargetChanged()
 {
 }
+
+Decimal InputType::findClosestTickMarkValue(const Decimal&)
+{
+    ASSERT_NOT_REACHED();
+    return Decimal::nan();
+}
 #endif
 
 bool InputType::supportsIndeterminateAppearance() const

Modified: trunk/Source/WebCore/html/InputType.h (124548 => 124549)


--- trunk/Source/WebCore/html/InputType.h	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/InputType.h	2012-08-03 02:05:51 UTC (rev 124549)
@@ -281,6 +281,7 @@
     virtual String defaultToolTip() const;
 #if ENABLE(DATALIST_ELEMENT)
     virtual void listAttributeTargetChanged();
+    virtual Decimal findClosestTickMarkValue(const Decimal&);
 #endif
 
     // Parses the specified string for the type, and return

Modified: trunk/Source/WebCore/html/RangeInputType.cpp (124548 => 124549)


--- trunk/Source/WebCore/html/RangeInputType.cpp	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/RangeInputType.cpp	2012-08-03 02:05:51 UTC (rev 124549)
@@ -55,6 +55,12 @@
 #include "TouchList.h"
 #endif
 
+#if ENABLE(DATALIST_ELEMENT)
+#include "HTMLDataListElement.h"
+#include "HTMLOptionElement.h"
+#include <wtf/NonCopyingSort.h>
+#endif
+
 namespace WebCore {
 
 using namespace HTMLNames;
@@ -76,6 +82,14 @@
     return adoptPtr(new RangeInputType(element));
 }
 
+RangeInputType::RangeInputType(HTMLInputElement* element)
+    : InputType(element)
+#if ENABLE(DATALIST_ELEMENT)
+    , m_tickMarkValuesDirty(true)
+#endif
+{
+}
+
 bool RangeInputType::isRangeControl() const
 {
     return true;
@@ -319,8 +333,70 @@
 #if ENABLE(DATALIST_ELEMENT)
 void RangeInputType::listAttributeTargetChanged()
 {
+    m_tickMarkValuesDirty = true;
     element()->setNeedsStyleRecalc();
 }
+
+static bool decimalCompare(const Decimal& a, const Decimal& b)
+{
+    return a < b;
+}
+
+void RangeInputType::updateTickMarkValues()
+{
+    if (!m_tickMarkValuesDirty)
+        return;
+    m_tickMarkValues.clear();
+    m_tickMarkValuesDirty = false;
+    HTMLDataListElement* dataList = element()->dataList();
+    if (!dataList)
+        return;
+    RefPtr<HTMLCollection> options = dataList->options();
+    m_tickMarkValues.reserveCapacity(options->length());
+    for (unsigned i = 0; i < options->length(); ++i) {
+        Node* node = options->item(i);
+        HTMLOptionElement* optionElement = toHTMLOptionElement(node);
+        String optionValue = optionElement->value();
+        if (!element()->isValidValue(optionValue))
+            continue;
+        m_tickMarkValues.append(parseToNumber(optionValue, Decimal::nan()));
+    }
+    m_tickMarkValues.shrinkToFit();
+    nonCopyingSort(m_tickMarkValues.begin(), m_tickMarkValues.end(), decimalCompare);
+}
+
+Decimal RangeInputType::findClosestTickMarkValue(const Decimal& value)
+{
+    updateTickMarkValues();
+    if (!m_tickMarkValues.size())
+        return Decimal::nan();
+
+    size_t left = 0;
+    size_t right = m_tickMarkValues.size();
+    size_t middle;
+    while (true) {
+        ASSERT(left <= right);
+        middle = left + (right - left) / 2;
+        if (!middle)
+            break;
+        if (middle == m_tickMarkValues.size() - 1 && m_tickMarkValues[middle] < value) {
+            middle++;
+            break;
+        }
+        if (m_tickMarkValues[middle - 1] <= value && m_tickMarkValues[middle] >= value)
+            break;
+
+        if (m_tickMarkValues[middle] < value)
+            left = middle;
+        else
+            right = middle;
+    }
+    const Decimal closestLeft = middle ? m_tickMarkValues[middle - 1] : Decimal::infinity(Decimal::Negative);
+    const Decimal closestRight = middle != m_tickMarkValues.size() ? m_tickMarkValues[middle] : Decimal::infinity(Decimal::Positive);
+    if (closestRight - value < value - closestLeft)
+        return closestRight;
+    return closestLeft;
+}
 #endif
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/html/RangeInputType.h (124548 => 124549)


--- trunk/Source/WebCore/html/RangeInputType.h	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/RangeInputType.h	2012-08-03 02:05:51 UTC (rev 124549)
@@ -42,7 +42,7 @@
     static PassOwnPtr<InputType> create(HTMLInputElement*);
 
 private:
-    RangeInputType(HTMLInputElement* element) : InputType(element) { }
+    RangeInputType(HTMLInputElement*);
     virtual bool isRangeControl() const OVERRIDE;
     virtual const AtomicString& formControlType() const OVERRIDE;
     virtual double valueAsDouble() const OVERRIDE;
@@ -72,6 +72,11 @@
     virtual HTMLElement* sliderThumbElement() const OVERRIDE;
 #if ENABLE(DATALIST_ELEMENT)
     virtual void listAttributeTargetChanged() OVERRIDE;
+    void updateTickMarkValues();
+    virtual Decimal findClosestTickMarkValue(const Decimal&) OVERRIDE;
+
+    bool m_tickMarkValuesDirty;
+    Vector<Decimal> m_tickMarkValues;
 #endif
 };
 

Modified: trunk/Source/WebCore/html/shadow/SliderThumbElement.cpp (124548 => 124549)


--- trunk/Source/WebCore/html/shadow/SliderThumbElement.cpp	2012-08-03 01:54:50 UTC (rev 124548)
+++ trunk/Source/WebCore/html/shadow/SliderThumbElement.cpp	2012-08-03 02:05:51 UTC (rev 124549)
@@ -51,6 +51,8 @@
 
 namespace WebCore {
 
+using namespace HTMLNames;
+
 inline static Decimal sliderPosition(HTMLInputElement* element)
 {
     const StepRange stepRange(element->createStepRange(RejectAny));
@@ -270,8 +272,20 @@
     const Decimal ratio = Decimal::fromDouble(static_cast<double>(position) / trackSize);
     const Decimal fraction = isVertical || !renderBox()->style()->isLeftToRightDirection() ? Decimal(1) - ratio : ratio;
     StepRange stepRange(input->createStepRange(RejectAny));
-    const Decimal value = stepRange.clampValue(stepRange.valueFromProportion(fraction));
+    Decimal value = stepRange.clampValue(stepRange.valueFromProportion(fraction));
 
+#if ENABLE(DATALIST_ELEMENT)
+    Decimal closest = input->findClosestTickMarkValue(value);
+    if (closest.isFinite()) {
+        double closestFraction = stepRange.proportionFromValue(closest).toDouble();
+        double closestRatio = isVertical || !renderBox()->style()->isLeftToRightDirection() ? 1.0 - closestFraction : closestFraction;
+        LayoutUnit closestPosition = trackSize * closestRatio;
+        const LayoutUnit snapThreshold = 3;
+        if ((closestPosition - position).abs() < snapThreshold)
+            value = closest;
+    }
+#endif
+
     // FIXME: This is no longer being set from renderer. Consider updating the method name.
     input->setValueFromRenderer(serializeForNumberType(value));
     renderer()->setNeedsLayout(true);
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to