Skip to content
Permalink
Browse files
2010-03-19 Joseph Pecoraro <joepeck@webkit.org>
        Reviewed by David Kilzer.

        <input type=range> does not validate correctly without a renderer and the tests are incorrect
        https://bugs.webkit.org/show_bug.cgi?id=36259

        Part 1 of 2: Refactoring the SliderRange struct out of RenderSlider
        into a more appropriate place. Changed the named to StepRange. Changed
        from a struct to a class.

          Added new files to the build.

        * GNUmakefile.am:
        * WebCore.gypi:
        * WebCore.pro:
        * WebCore.vcproj/WebCore.vcproj:
        * WebCore.xcodeproj/project.pbxproj:

          Renamed and moved class SliderRange to StepRange.

        * html/StepRange.cpp: Added.
        (WebCore::StepRange::StepRange):
        (WebCore::StepRange::clampValue):
        (WebCore::StepRange::valueFromElement):
        (WebCore::sliderPosition):
        * html/StepRange.h: Added.
        (WebCore::StepRange::proportionFromValue):
        (WebCore::StepRange::valueFromProportion):
        * rendering/RenderSlider.cpp:
        (WebCore::RenderSlider::updateFromElement): updated to use StepRange
        (WebCore::RenderSlider::setValueForPosition): updated to use StepRange

Canonical link: https://commits.webkit.org/47535@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@56241 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
JosephPecoraro committed Mar 19, 2010
1 parent 78dabe2 commit bdaf6ba95df5c63d1b3a858afc03ec7d0d5e5f36
@@ -1,3 +1,36 @@
2010-03-19 Joseph Pecoraro <joepeck@webkit.org>

Reviewed by David Kilzer.

<input type=range> does not validate correctly without a renderer and the tests are incorrect
https://bugs.webkit.org/show_bug.cgi?id=36259

Part 1 of 2: Refactoring the SliderRange struct out of RenderSlider
into a more appropriate place. Changed the named to StepRange. Changed
from a struct to a class.

Added new files to the build.

* GNUmakefile.am:
* WebCore.gypi:
* WebCore.pro:
* WebCore.vcproj/WebCore.vcproj:
* WebCore.xcodeproj/project.pbxproj:

Renamed and moved class SliderRange to StepRange.

* html/StepRange.cpp: Added.
(WebCore::StepRange::StepRange):
(WebCore::StepRange::clampValue):
(WebCore::StepRange::valueFromElement):
(WebCore::sliderPosition):
* html/StepRange.h: Added.
(WebCore::StepRange::proportionFromValue):
(WebCore::StepRange::valueFromProportion):
* rendering/RenderSlider.cpp:
(WebCore::RenderSlider::updateFromElement): updated to use StepRange
(WebCore::RenderSlider::setValueForPosition): updated to use StepRange

2010-03-19 Antti Koivisto <koivisto@iki.fi>

Reviewed by Simon Hausmann.
@@ -1189,6 +1189,8 @@ webcore_sources += \
WebCore/html/MediaError.h \
WebCore/html/PreloadScanner.cpp \
WebCore/html/PreloadScanner.h \
WebCore/html/StepRange.cpp \
WebCore/html/StepRange.h \
WebCore/html/TextMetrics.h \
WebCore/html/ValidityState.cpp \
WebCore/html/ValidityState.h \
@@ -1604,6 +1604,8 @@
'html/MediaError.h',
'html/PreloadScanner.cpp',
'html/PreloadScanner.h',
'html/StepRange.cpp',
'html/StepRange.h',
'html/TextMetrics.h',
'html/TimeRanges.cpp',
'html/TimeRanges.h',
@@ -687,6 +687,7 @@ SOURCES += \
html/HTMLViewSourceDocument.cpp \
html/ImageData.cpp \
html/PreloadScanner.cpp \
html/StepRange.cpp \
html/ValidityState.cpp \
inspector/ConsoleMessage.cpp \
inspector/InjectedScript.cpp \
@@ -1394,6 +1395,7 @@ HEADERS += \
html/HTMLViewSourceDocument.h \
html/ImageData.h \
html/PreloadScanner.h \
html/StepRange.h \
html/TimeRanges.h \
html/ValidityState.h \
inspector/ConsoleMessage.h \
@@ -34384,6 +34384,14 @@
RelativePath="..\html\PreloadScanner.h"
>
</File>
<File
RelativePath="..\html\StepRange.cpp"
>
</File>
<File
RelativePath="..\html\StepRange.h"
>
</File>
<File
RelativePath="..\html\TimeRanges.cpp"
>
@@ -2493,6 +2493,8 @@
A43BF5991149290A00C643CA /* HTMLProgressElement.h in Headers */ = {isa = PBXBuildFile; fileRef = A43BF5971149290A00C643CA /* HTMLProgressElement.h */; };
A43BF59C1149292800C643CA /* RenderProgress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A43BF59A1149292800C643CA /* RenderProgress.cpp */; };
A43BF59D1149292800C643CA /* RenderProgress.h in Headers */ = {isa = PBXBuildFile; fileRef = A43BF59B1149292800C643CA /* RenderProgress.h */; };
A5AFB34F115151A700B045CB /* StepRange.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A5AFB34D115151A700B045CB /* StepRange.cpp */; };
A5AFB350115151A700B045CB /* StepRange.h in Headers */ = {isa = PBXBuildFile; fileRef = A5AFB34E115151A700B045CB /* StepRange.h */; };
A718760E0B2A120100A16ECE /* DragActions.h in Headers */ = {isa = PBXBuildFile; fileRef = A718760D0B2A120100A16ECE /* DragActions.h */; settings = {ATTRIBUTES = (Private, ); }; };
A71878900B2D04AC00A16ECE /* DragControllerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = A718788F0B2D04AC00A16ECE /* DragControllerMac.mm */; };
A7352C190B1BB89D00A986D0 /* RenderSVGBlock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7352C170B1BB89D00A986D0 /* RenderSVGBlock.cpp */; };
@@ -7895,6 +7897,8 @@
A43BF5971149290A00C643CA /* HTMLProgressElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLProgressElement.h; sourceTree = "<group>"; };
A43BF59A1149292800C643CA /* RenderProgress.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RenderProgress.cpp; sourceTree = "<group>"; };
A43BF59B1149292800C643CA /* RenderProgress.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RenderProgress.h; sourceTree = "<group>"; };
A5AFB34D115151A700B045CB /* StepRange.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StepRange.cpp; sourceTree = "<group>"; };
A5AFB34E115151A700B045CB /* StepRange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StepRange.h; sourceTree = "<group>"; };
A718760D0B2A120100A16ECE /* DragActions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DragActions.h; sourceTree = "<group>"; };
A718788F0B2D04AC00A16ECE /* DragControllerMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DragControllerMac.mm; sourceTree = "<group>"; };
A7352C170B1BB89D00A986D0 /* RenderSVGBlock.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = RenderSVGBlock.cpp; sourceTree = "<group>"; };
@@ -13176,6 +13180,8 @@
E446139C0CD6331000FADA75 /* MediaError.idl */,
E4D4ABE00D7542F000F96869 /* PreloadScanner.cpp */,
E4D4ABE10D7542F100F96869 /* PreloadScanner.h */,
A5AFB34D115151A700B045CB /* StepRange.cpp */,
A5AFB34E115151A700B045CB /* StepRange.h */,
BCEF45E80E687767001C1287 /* TextMetrics.h */,
BCEF453F0E676AC1001C1287 /* TextMetrics.idl */,
E446139D0CD6331000FADA75 /* TimeRanges.cpp */,
@@ -18697,6 +18703,7 @@
A43BF5991149290A00C643CA /* HTMLProgressElement.h in Headers */,
A43BF59D1149292800C643CA /* RenderProgress.h in Headers */,
F375CC071150D300008DDB81 /* InspectorWorkerResource.h in Headers */,
A5AFB350115151A700B045CB /* StepRange.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -20904,6 +20911,7 @@
97DD4D860FDF4D6E00ECF9A4 /* XSSAuditor.cpp in Sources */,
A43BF5981149290A00C643CA /* HTMLProgressElement.cpp in Sources */,
A43BF59C1149292800C643CA /* RenderProgress.cpp in Sources */,
A5AFB34F115151A700B045CB /* StepRange.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/

#include "config.h"
#include "StepRange.h"

#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include <wtf/MathExtras.h>

using namespace std;

namespace WebCore {

using namespace HTMLNames;

StepRange::StepRange(HTMLInputElement* element)
{
if (element->hasAttribute(precisionAttr)) {
step = 1.0;
hasStep = !equalIgnoringCase(element->getAttribute(precisionAttr), "float");
} else
hasStep = element->getAllowedValueStep(&step);

maximum = element->maximum();
minimum = element->minimum();
}

double StepRange::clampValue(double value)
{
double clampedValue = max(minimum, min(value, maximum));
if (!hasStep)
return clampedValue;
// Rounds clampedValue to minimum + N * step.
clampedValue = minimum + round((clampedValue - minimum) / step) * step;
if (clampedValue > maximum)
clampedValue -= step;
ASSERT(clampedValue >= minimum);
ASSERT(clampedValue <= maximum);
return clampedValue;
}

double StepRange::valueFromElement(HTMLInputElement* element, bool* wasClamped)
{
double oldValue;
bool parseSuccess = HTMLInputElement::parseToDoubleForNumberType(element->value(), &oldValue);
if (!parseSuccess)
oldValue = (minimum + maximum) / 2;
double newValue = clampValue(oldValue);

if (wasClamped)
*wasClamped = !parseSuccess || newValue != oldValue;

return newValue;
}

}
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/

#ifndef StepRange_h
#define StepRange_h

#include <wtf/Noncopyable.h>

namespace WebCore {

class HTMLInputElement;

class StepRange : public Noncopyable {
public:
bool hasStep;
double step;
double minimum;
double maximum; // maximum must be >= minimum.

explicit StepRange(HTMLInputElement*);
double clampValue(double value);

// Map value into 0-1 range
double proportionFromValue(double value)
{
if (minimum == maximum)
return 0;

return (value - minimum) / (maximum - minimum);
}

// Map from 0-1 range to value
double valueFromProportion(double proportion)
{
return minimum + proportion * (maximum - minimum);
}

double valueFromElement(HTMLInputElement*, bool* wasClamped = 0);
};

}

#endif // StepRange_h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
* Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -35,6 +35,7 @@
#include "RenderLayer.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "StepRange.h"
#include <wtf/MathExtras.h>

using std::min;
@@ -45,80 +46,10 @@ using namespace HTMLNames;

static const int defaultTrackLength = 129;

// FIXME: The SliderRange class and functions are entirely based on the DOM,
// and could be put with HTMLInputElement (possibly with a new name) instead of here.
struct SliderRange {
bool hasStep;
double step;
double minimum;
double maximum; // maximum must be >= minimum.

explicit SliderRange(HTMLInputElement*);
double clampValue(double value);

// Map value into 0-1 range
double proportionFromValue(double value)
{
if (minimum == maximum)
return 0;

return (value - minimum) / (maximum - minimum);
}

// Map from 0-1 range to value
double valueFromProportion(double proportion)
{
return minimum + proportion * (maximum - minimum);
}

double valueFromElement(HTMLInputElement*, bool* wasClamped = 0);
};

SliderRange::SliderRange(HTMLInputElement* element)
{
if (element->hasAttribute(precisionAttr)) {
step = 1.0;
hasStep = !equalIgnoringCase(element->getAttribute(precisionAttr), "float");
} else
hasStep = element->getAllowedValueStep(&step);

maximum = element->maximum();
minimum = element->minimum();
}

double SliderRange::clampValue(double value)
{
double clampedValue = max(minimum, min(value, maximum));
if (!hasStep)
return clampedValue;
// Rounds clampedValue to minimum + N * step.
clampedValue = minimum + round((clampedValue - minimum) / step) * step;
if (clampedValue > maximum)
clampedValue -= step;
ASSERT(clampedValue >= minimum);
ASSERT(clampedValue <= maximum);
return clampedValue;
}

double SliderRange::valueFromElement(HTMLInputElement* element, bool* wasClamped)
{
double oldValue;
bool parseSuccess = HTMLInputElement::parseToDoubleForNumberType(element->value(), &oldValue);
if (!parseSuccess)
oldValue = (minimum + maximum) / 2;
double newValue = clampValue(oldValue);

if (wasClamped)
*wasClamped = !parseSuccess || newValue != oldValue;

return newValue;
}

// Returns a value between 0 and 1.
// As with SliderRange, this could be on HTMLInputElement instead of here.
static double sliderPosition(HTMLInputElement* element)
{
SliderRange range(element);
StepRange range(element);
return range.proportionFromValue(range.valueFromElement(element));
}

@@ -375,7 +306,7 @@ void RenderSlider::updateFromElement()
HTMLInputElement* element = static_cast<HTMLInputElement*>(node());

// Send the value back to the element if the range changes it.
SliderRange range(element);
StepRange range(element);
bool clamped;
double value = range.valueFromElement(element, &clamped);
if (clamped)
@@ -430,7 +361,7 @@ void RenderSlider::setValueForPosition(int position)
HTMLInputElement* element = static_cast<HTMLInputElement*>(node());

// Calculate the new value based on the position, and send it to the element.
SliderRange range(element);
StepRange range(element);
double fraction = static_cast<double>(position) / trackSize();
if (style()->appearance() == SliderVerticalPart || style()->appearance() == MediaVolumeSliderPart)
fraction = 1 - fraction;

0 comments on commit bdaf6ba

Please sign in to comment.