2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "RangeInputType.h"
35 #include "AXObjectCache.h"
36 #include "ElementChildIterator.h"
37 #include "EventNames.h"
38 #include "HTMLInputElement.h"
39 #include "HTMLParserIdioms.h"
40 #include "InputTypeNames.h"
41 #include "KeyboardEvent.h"
42 #include "MouseEvent.h"
43 #include "PlatformMouseEvent.h"
44 #include "RenderSlider.h"
45 #include "ScopedEventQueue.h"
46 #include "ShadowRoot.h"
47 #include "SliderThumbElement.h"
49 #include <wtf/MathExtras.h>
50 #include <wtf/NeverDestroyed.h>
52 #if ENABLE(TOUCH_EVENTS)
54 #include "TouchEvent.h"
55 #include "TouchList.h"
58 #if ENABLE(DATALIST_ELEMENT)
59 #include "HTMLDataListElement.h"
60 #include "HTMLOptionElement.h"
65 using namespace HTMLNames;
67 static const int rangeDefaultMinimum = 0;
68 static const int rangeDefaultMaximum = 100;
69 static const int rangeDefaultStep = 1;
70 static const int rangeDefaultStepBase = 0;
71 static const int rangeStepScaleFactor = 1;
73 static Decimal ensureMaximum(const Decimal& proposedValue, const Decimal& minimum, const Decimal& fallbackValue)
75 return proposedValue >= minimum ? proposedValue : std::max(minimum, fallbackValue);
78 RangeInputType::RangeInputType(HTMLInputElement& element)
83 bool RangeInputType::isRangeControl() const
88 const AtomicString& RangeInputType::formControlType() const
90 return InputTypeNames::range();
93 double RangeInputType::valueAsDouble() const
95 return parseToDoubleForNumberType(element().value());
98 ExceptionOr<void> RangeInputType::setValueAsDecimal(const Decimal& newValue, TextFieldEventBehavior eventBehavior) const
100 element().setValue(serialize(newValue), eventBehavior);
104 bool RangeInputType::typeMismatchFor(const String& value) const
106 return !value.isEmpty() && !std::isfinite(parseToDoubleForNumberType(value));
109 bool RangeInputType::supportsRequired() const
114 StepRange RangeInputType::createStepRange(AnyStepHandling anyStepHandling) const
116 static NeverDestroyed<const StepRange::StepDescription> stepDescription(rangeDefaultStep, rangeDefaultStepBase, rangeStepScaleFactor);
118 const Decimal minimum = parseToNumber(element().attributeWithoutSynchronization(minAttr), rangeDefaultMinimum);
119 const Decimal maximum = ensureMaximum(parseToNumber(element().attributeWithoutSynchronization(maxAttr), rangeDefaultMaximum), minimum, rangeDefaultMaximum);
121 const AtomicString& precisionValue = element().attributeWithoutSynchronization(precisionAttr);
122 if (!precisionValue.isNull()) {
123 const Decimal step = equalLettersIgnoringASCIICase(precisionValue, "float") ? Decimal::nan() : 1;
124 return StepRange(minimum, RangeLimitations::Valid, minimum, maximum, step, stepDescription);
127 const Decimal step = StepRange::parseStep(anyStepHandling, stepDescription, element().attributeWithoutSynchronization(stepAttr));
128 return StepRange(minimum, RangeLimitations::Valid, minimum, maximum, step, stepDescription);
131 bool RangeInputType::isSteppable() const
137 void RangeInputType::handleMouseDownEvent(MouseEvent& event)
139 if (element().isDisabledFormControl())
142 Node* targetNode = event.target()->toNode();
143 if (event.button() != LeftButton || !targetNode)
145 ASSERT(element().shadowRoot());
146 if (targetNode != &element() && !targetNode->isDescendantOf(element().userAgentShadowRoot()))
148 SliderThumbElement& thumb = typedSliderThumbElement();
149 if (targetNode == &thumb)
151 thumb.dragFrom(event.absoluteLocation());
155 #if ENABLE(TOUCH_EVENTS)
156 void RangeInputType::handleTouchEvent(TouchEvent& event)
159 typedSliderThumbElement().handleTouchEvent(event);
160 #elif ENABLE(TOUCH_SLIDER)
161 if (element().isDisabledFormControl())
164 if (event.type() == eventNames().touchendEvent) {
165 event.setDefaultHandled();
169 TouchList* touches = event.targetTouches();
170 if (touches->length() == 1) {
171 typedSliderThumbElement().setPositionFromPoint(touches->item(0)->absoluteLocation());
172 event.setDefaultHandled();
179 #if ENABLE(TOUCH_SLIDER)
180 bool RangeInputType::hasTouchEventHandler() const
187 void RangeInputType::disabledAttributeChanged()
189 typedSliderThumbElement().disabledAttributeChanged();
192 #endif // ENABLE(TOUCH_EVENTS)
194 void RangeInputType::handleKeydownEvent(KeyboardEvent& event)
196 if (element().isDisabledFormControl())
199 const String& key = event.keyIdentifier();
201 const Decimal current = parseToNumberOrNaN(element().value());
202 ASSERT(current.isFinite());
204 StepRange stepRange(createStepRange(RejectAny));
207 // FIXME: We can't use stepUp() for the step value "any". So, we increase
208 // or decrease the value by 1/100 of the value range. Is it reasonable?
209 const Decimal step = equalLettersIgnoringASCIICase(element().attributeWithoutSynchronization(stepAttr), "any") ? (stepRange.maximum() - stepRange.minimum()) / 100 : stepRange.step();
210 const Decimal bigStep = std::max((stepRange.maximum() - stepRange.minimum()) / 10, step);
212 bool isVertical = false;
213 if (element().renderer()) {
214 ControlPart part = element().renderer()->style().appearance();
215 isVertical = part == SliderVerticalPart || part == MediaVolumeSliderPart;
220 newValue = current + step;
221 else if (key == "Down")
222 newValue = current - step;
223 else if (key == "Left")
224 newValue = isVertical ? current + step : current - step;
225 else if (key == "Right")
226 newValue = isVertical ? current - step : current + step;
227 else if (key == "PageUp")
228 newValue = current + bigStep;
229 else if (key == "PageDown")
230 newValue = current - bigStep;
231 else if (key == "Home")
232 newValue = isVertical ? stepRange.maximum() : stepRange.minimum();
233 else if (key == "End")
234 newValue = isVertical ? stepRange.minimum() : stepRange.maximum();
236 return; // Did not match any key binding.
238 newValue = stepRange.clampValue(newValue);
240 if (newValue != current) {
241 EventQueueScope scope;
242 setValueAsDecimal(newValue, DispatchInputAndChangeEvent);
244 if (AXObjectCache* cache = element().document().existingAXObjectCache())
245 cache->postNotification(&element(), AXObjectCache::AXValueChanged);
248 event.setDefaultHandled();
251 void RangeInputType::createShadowSubtree()
253 ASSERT(element().userAgentShadowRoot());
255 Document& document = element().document();
256 auto track = HTMLDivElement::create(document);
257 track->setPseudo(AtomicString("-webkit-slider-runnable-track", AtomicString::ConstructFromLiteral));
258 track->appendChild(SliderThumbElement::create(document));
259 auto container = SliderContainerElement::create(document);
260 container->appendChild(track);
261 element().userAgentShadowRoot()->appendChild(container);
264 HTMLElement* RangeInputType::sliderTrackElement() const
266 ASSERT(element().userAgentShadowRoot());
267 ASSERT(element().userAgentShadowRoot()->firstChild()); // container
268 ASSERT(element().userAgentShadowRoot()->firstChild()->isHTMLElement());
269 ASSERT(element().userAgentShadowRoot()->firstChild()->firstChild()); // track
271 ShadowRoot* root = element().userAgentShadowRoot();
275 auto* container = childrenOfType<SliderContainerElement>(*root).first();
279 return childrenOfType<HTMLElement>(*container).first();
282 SliderThumbElement& RangeInputType::typedSliderThumbElement() const
284 ASSERT(sliderTrackElement()->firstChild()); // thumb
285 ASSERT(sliderTrackElement()->firstChild()->isHTMLElement());
287 return static_cast<SliderThumbElement&>(*sliderTrackElement()->firstChild());
290 HTMLElement* RangeInputType::sliderThumbElement() const
292 return &typedSliderThumbElement();
295 RenderPtr<RenderElement> RangeInputType::createInputRenderer(RenderStyle&& style)
297 return createRenderer<RenderSlider>(element(), WTFMove(style));
300 Decimal RangeInputType::parseToNumber(const String& src, const Decimal& defaultValue) const
302 return parseToDecimalForNumberType(src, defaultValue);
305 String RangeInputType::serialize(const Decimal& value) const
307 if (!value.isFinite())
309 return serializeForNumberType(value);
312 // FIXME: Could share this with BaseClickableWithKeyInputType and BaseCheckableInputType if we had a common base class.
313 void RangeInputType::accessKeyAction(bool sendMouseEvents)
315 InputType::accessKeyAction(sendMouseEvents);
317 element().dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
320 void RangeInputType::minOrMaxAttributeChanged()
322 InputType::minOrMaxAttributeChanged();
324 // Sanitize the value.
325 if (element().hasDirtyValue())
326 element().setValue(element().value());
328 typedSliderThumbElement().setPositionFromValue();
331 void RangeInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior)
333 InputType::setValue(value, valueChanged, eventBehavior);
338 if (eventBehavior == DispatchNoEvent)
339 element().setTextAsOfLastFormControlChangeEvent(value);
341 typedSliderThumbElement().setPositionFromValue();
344 String RangeInputType::fallbackValue() const
346 return serializeForNumberType(createStepRange(RejectAny).defaultValue());
349 String RangeInputType::sanitizeValue(const String& proposedValue) const
351 StepRange stepRange(createStepRange(RejectAny));
352 const Decimal proposedNumericValue = parseToNumber(proposedValue, stepRange.defaultValue());
353 return serializeForNumberType(stepRange.clampValue(proposedNumericValue));
356 bool RangeInputType::shouldRespectListAttribute()
358 return InputType::themeSupportsDataListUI(this);
361 #if ENABLE(DATALIST_ELEMENT)
362 void RangeInputType::listAttributeTargetChanged()
364 m_tickMarkValuesDirty = true;
365 HTMLElement* sliderTrackElement = this->sliderTrackElement();
366 if (sliderTrackElement->renderer())
367 sliderTrackElement->renderer()->setNeedsLayout();
370 void RangeInputType::updateTickMarkValues()
372 if (!m_tickMarkValuesDirty)
374 m_tickMarkValues.clear();
375 m_tickMarkValuesDirty = false;
376 HTMLDataListElement* dataList = element().dataList();
379 Ref<HTMLCollection> options = dataList->options();
380 m_tickMarkValues.reserveCapacity(options->length());
381 for (unsigned i = 0; i < options->length(); ++i) {
382 Node* node = options->item(i);
383 HTMLOptionElement& optionElement = downcast<HTMLOptionElement>(*node);
384 String optionValue = optionElement.value();
385 if (!element().isValidValue(optionValue))
387 m_tickMarkValues.append(parseToNumber(optionValue, Decimal::nan()));
389 m_tickMarkValues.shrinkToFit();
390 std::sort(m_tickMarkValues.begin(), m_tickMarkValues.end());
393 std::optional<Decimal> RangeInputType::findClosestTickMarkValue(const Decimal& value)
395 updateTickMarkValues();
396 if (!m_tickMarkValues.size())
400 size_t right = m_tickMarkValues.size();
403 ASSERT(left <= right);
404 middle = left + (right - left) / 2;
407 if (middle == m_tickMarkValues.size() - 1 && m_tickMarkValues[middle] < value) {
411 if (m_tickMarkValues[middle - 1] <= value && m_tickMarkValues[middle] >= value)
414 if (m_tickMarkValues[middle] < value)
420 std::optional<Decimal> closestLeft = middle ? std::make_optional(m_tickMarkValues[middle - 1]) : std::nullopt;
421 std::optional<Decimal> closestRight = middle != m_tickMarkValues.size() ? std::make_optional(m_tickMarkValues[middle]) : std::nullopt;
428 if (*closestRight - value < value - *closestLeft)
435 } // namespace WebCore