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
185 #endif // ENABLE(TOUCH_EVENTS)
187 void RangeInputType::disabledAttributeChanged()
189 typedSliderThumbElement().disabledAttributeChanged();
192 void RangeInputType::handleKeydownEvent(KeyboardEvent& event)
194 if (element().isDisabledFormControl())
197 const String& key = event.keyIdentifier();
199 const Decimal current = parseToNumberOrNaN(element().value());
200 ASSERT(current.isFinite());
202 StepRange stepRange(createStepRange(RejectAny));
205 // FIXME: We can't use stepUp() for the step value "any". So, we increase
206 // or decrease the value by 1/100 of the value range. Is it reasonable?
207 const Decimal step = equalLettersIgnoringASCIICase(element().attributeWithoutSynchronization(stepAttr), "any") ? (stepRange.maximum() - stepRange.minimum()) / 100 : stepRange.step();
208 const Decimal bigStep = std::max((stepRange.maximum() - stepRange.minimum()) / 10, step);
210 bool isVertical = false;
211 if (element().renderer()) {
212 ControlPart part = element().renderer()->style().appearance();
213 isVertical = part == SliderVerticalPart || part == MediaVolumeSliderPart;
218 newValue = current + step;
219 else if (key == "Down")
220 newValue = current - step;
221 else if (key == "Left")
222 newValue = isVertical ? current + step : current - step;
223 else if (key == "Right")
224 newValue = isVertical ? current - step : current + step;
225 else if (key == "PageUp")
226 newValue = current + bigStep;
227 else if (key == "PageDown")
228 newValue = current - bigStep;
229 else if (key == "Home")
230 newValue = isVertical ? stepRange.maximum() : stepRange.minimum();
231 else if (key == "End")
232 newValue = isVertical ? stepRange.minimum() : stepRange.maximum();
234 return; // Did not match any key binding.
236 newValue = stepRange.clampValue(newValue);
238 if (newValue != current) {
239 EventQueueScope scope;
240 setValueAsDecimal(newValue, DispatchInputAndChangeEvent);
242 if (AXObjectCache* cache = element().document().existingAXObjectCache())
243 cache->postNotification(&element(), AXObjectCache::AXValueChanged);
246 event.setDefaultHandled();
249 void RangeInputType::createShadowSubtree()
251 ASSERT(element().userAgentShadowRoot());
253 Document& document = element().document();
254 auto track = HTMLDivElement::create(document);
255 track->setPseudo(AtomicString("-webkit-slider-runnable-track", AtomicString::ConstructFromLiteral));
256 track->appendChild(SliderThumbElement::create(document));
257 auto container = SliderContainerElement::create(document);
258 container->appendChild(track);
259 element().userAgentShadowRoot()->appendChild(container);
262 HTMLElement* RangeInputType::sliderTrackElement() const
264 ASSERT(element().userAgentShadowRoot());
265 ASSERT(element().userAgentShadowRoot()->firstChild()); // container
266 ASSERT(element().userAgentShadowRoot()->firstChild()->isHTMLElement());
267 ASSERT(element().userAgentShadowRoot()->firstChild()->firstChild()); // track
269 ShadowRoot* root = element().userAgentShadowRoot();
273 auto* container = childrenOfType<SliderContainerElement>(*root).first();
277 return childrenOfType<HTMLElement>(*container).first();
280 SliderThumbElement& RangeInputType::typedSliderThumbElement() const
282 ASSERT(sliderTrackElement()->firstChild()); // thumb
283 ASSERT(sliderTrackElement()->firstChild()->isHTMLElement());
285 return static_cast<SliderThumbElement&>(*sliderTrackElement()->firstChild());
288 HTMLElement* RangeInputType::sliderThumbElement() const
290 return &typedSliderThumbElement();
293 RenderPtr<RenderElement> RangeInputType::createInputRenderer(RenderStyle&& style)
295 return createRenderer<RenderSlider>(element(), WTFMove(style));
298 Decimal RangeInputType::parseToNumber(const String& src, const Decimal& defaultValue) const
300 return parseToDecimalForNumberType(src, defaultValue);
303 String RangeInputType::serialize(const Decimal& value) const
305 if (!value.isFinite())
307 return serializeForNumberType(value);
310 // FIXME: Could share this with BaseClickableWithKeyInputType and BaseCheckableInputType if we had a common base class.
311 void RangeInputType::accessKeyAction(bool sendMouseEvents)
313 InputType::accessKeyAction(sendMouseEvents);
315 element().dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
318 void RangeInputType::minOrMaxAttributeChanged()
320 InputType::minOrMaxAttributeChanged();
322 // Sanitize the value.
323 if (element().hasDirtyValue())
324 element().setValue(element().value());
326 typedSliderThumbElement().setPositionFromValue();
329 void RangeInputType::setValue(const String& value, bool valueChanged, TextFieldEventBehavior eventBehavior)
331 InputType::setValue(value, valueChanged, eventBehavior);
336 if (eventBehavior == DispatchNoEvent)
337 element().setTextAsOfLastFormControlChangeEvent(value);
339 typedSliderThumbElement().setPositionFromValue();
342 String RangeInputType::fallbackValue() const
344 return serializeForNumberType(createStepRange(RejectAny).defaultValue());
347 String RangeInputType::sanitizeValue(const String& proposedValue) const
349 StepRange stepRange(createStepRange(RejectAny));
350 const Decimal proposedNumericValue = parseToNumber(proposedValue, stepRange.defaultValue());
351 return serializeForNumberType(stepRange.clampValue(proposedNumericValue));
354 bool RangeInputType::shouldRespectListAttribute()
356 return InputType::themeSupportsDataListUI(this);
359 #if ENABLE(DATALIST_ELEMENT)
360 void RangeInputType::listAttributeTargetChanged()
362 m_tickMarkValuesDirty = true;
363 HTMLElement* sliderTrackElement = this->sliderTrackElement();
364 if (sliderTrackElement->renderer())
365 sliderTrackElement->renderer()->setNeedsLayout();
368 void RangeInputType::updateTickMarkValues()
370 if (!m_tickMarkValuesDirty)
372 m_tickMarkValues.clear();
373 m_tickMarkValuesDirty = false;
374 HTMLDataListElement* dataList = element().dataList();
377 Ref<HTMLCollection> options = dataList->options();
378 m_tickMarkValues.reserveCapacity(options->length());
379 for (unsigned i = 0; i < options->length(); ++i) {
380 Node* node = options->item(i);
381 HTMLOptionElement& optionElement = downcast<HTMLOptionElement>(*node);
382 String optionValue = optionElement.value();
383 if (!element().isValidValue(optionValue))
385 m_tickMarkValues.append(parseToNumber(optionValue, Decimal::nan()));
387 m_tickMarkValues.shrinkToFit();
388 std::sort(m_tickMarkValues.begin(), m_tickMarkValues.end());
391 std::optional<Decimal> RangeInputType::findClosestTickMarkValue(const Decimal& value)
393 updateTickMarkValues();
394 if (!m_tickMarkValues.size())
398 size_t right = m_tickMarkValues.size();
401 ASSERT(left <= right);
402 middle = left + (right - left) / 2;
405 if (middle == m_tickMarkValues.size() - 1 && m_tickMarkValues[middle] < value) {
409 if (m_tickMarkValues[middle - 1] <= value && m_tickMarkValues[middle] >= value)
412 if (m_tickMarkValues[middle] < value)
418 std::optional<Decimal> closestLeft = middle ? std::make_optional(m_tickMarkValues[middle - 1]) : std::nullopt;
419 std::optional<Decimal> closestRight = middle != m_tickMarkValues.size() ? std::make_optional(m_tickMarkValues[middle]) : std::nullopt;
426 if (*closestRight - value < value - *closestLeft)
433 } // namespace WebCore