|
6 | 6 |
|
7 | 7 | #include "nsRangeFrame.h"
|
8 | 8 |
|
| 9 | +#include "mozilla/Assertions.h" |
9 | 10 | #include "mozilla/PresShell.h"
|
10 | 11 | #include "mozilla/TouchEvents.h"
|
11 | 12 |
|
|
19 | 20 | #include "mozilla/dom/Document.h"
|
20 | 21 | #include "nsNameSpaceManager.h"
|
21 | 22 | #include "nsGkAtoms.h"
|
| 23 | +#include "mozilla/dom/HTMLDataListElement.h" |
22 | 24 | #include "mozilla/dom/HTMLInputElement.h"
|
| 25 | +#include "mozilla/dom/HTMLOptionElement.h" |
23 | 26 | #include "nsPresContext.h"
|
24 | 27 | #include "nsPresContextInlines.h"
|
25 | 28 | #include "nsNodeInfoManager.h"
|
26 | 29 | #include "mozilla/dom/Element.h"
|
27 | 30 | #include "mozilla/ServoStyleSet.h"
|
28 | 31 | #include "nsStyleConsts.h"
|
| 32 | +#include "nsTArray.h" |
29 | 33 |
|
30 | 34 | #ifdef ACCESSIBILITY
|
31 | 35 | # include "nsAccessibilityService.h"
|
@@ -307,28 +311,29 @@ a11y::AccType nsRangeFrame::AccessibleType() { return a11y::eHTMLRangeType; }
|
307 | 311 | #endif
|
308 | 312 |
|
309 | 313 | double nsRangeFrame::GetValueAsFractionOfRange() {
|
310 |
| - MOZ_ASSERT(mContent->IsHTMLElement(nsGkAtoms::input), "bad cast"); |
311 |
| - auto* input = static_cast<dom::HTMLInputElement*>(GetContent()); |
312 |
| - MOZ_ASSERT(input->ControlType() == FormControlType::InputRange); |
| 314 | + return GetDoubleAsFractionOfRange(InputElement().GetValueAsDecimal()); |
| 315 | +} |
313 | 316 |
|
314 |
| - Decimal value = input->GetValueAsDecimal(); |
315 |
| - Decimal minimum = input->GetMinimum(); |
316 |
| - Decimal maximum = input->GetMaximum(); |
| 317 | +double nsRangeFrame::GetDoubleAsFractionOfRange(const Decimal& aValue) { |
| 318 | + auto& input = InputElement(); |
| 319 | + |
| 320 | + Decimal minimum = input.GetMinimum(); |
| 321 | + Decimal maximum = input.GetMaximum(); |
317 | 322 |
|
318 |
| - MOZ_ASSERT(value.isFinite() && minimum.isFinite() && maximum.isFinite(), |
| 323 | + MOZ_ASSERT(aValue.isFinite() && minimum.isFinite() && maximum.isFinite(), |
319 | 324 | "type=range should have a default maximum/minimum");
|
320 | 325 |
|
321 | 326 | if (maximum <= minimum) {
|
322 | 327 | // Avoid rounding triggering the assert by checking against an epsilon.
|
323 |
| - MOZ_ASSERT((value - minimum).abs().toDouble() < |
| 328 | + MOZ_ASSERT((aValue - minimum).abs().toDouble() < |
324 | 329 | std::numeric_limits<float>::epsilon(),
|
325 | 330 | "Unsanitized value");
|
326 | 331 | return 0.0;
|
327 | 332 | }
|
328 | 333 |
|
329 |
| - MOZ_ASSERT(value >= minimum && value <= maximum, "Unsanitized value"); |
| 334 | + MOZ_ASSERT(aValue >= minimum && aValue <= maximum, "Unsanitized value"); |
330 | 335 |
|
331 |
| - return ((value - minimum) / (maximum - minimum)).toDouble(); |
| 336 | + return ((aValue - minimum) / (maximum - minimum)).toDouble(); |
332 | 337 | }
|
333 | 338 |
|
334 | 339 | Decimal nsRangeFrame::GetValueAtEventPoint(WidgetGUIEvent* aEvent) {
|
@@ -454,6 +459,44 @@ void nsRangeFrame::UpdateForValueChange() {
|
454 | 459 | SchedulePaint();
|
455 | 460 | }
|
456 | 461 |
|
| 462 | +nsTArray<double> nsRangeFrame::TickMarks() { |
| 463 | + nsTArray<double> tickMarks; |
| 464 | + auto& input = InputElement(); |
| 465 | + auto* list = input.GetList(); |
| 466 | + if (!list) { |
| 467 | + return tickMarks; |
| 468 | + } |
| 469 | + auto min = input.GetMinimumAsDouble(); |
| 470 | + auto max = input.GetMaximumAsDouble(); |
| 471 | + auto* options = list->Options(); |
| 472 | + nsAutoString label; |
| 473 | + for (uint32_t i = 0; i < options->Length(); ++i) { |
| 474 | + auto* item = options->Item(i); |
| 475 | + auto* option = HTMLOptionElement::FromNode(item); |
| 476 | + MOZ_ASSERT(option); |
| 477 | + if (option->Disabled()) { |
| 478 | + continue; |
| 479 | + } |
| 480 | + nsAutoString str; |
| 481 | + option->GetValue(str); |
| 482 | + nsresult rv; |
| 483 | + double tickMark = str.ToDouble(&rv); |
| 484 | + if (NS_FAILED(rv) || tickMark < min || tickMark > max) { |
| 485 | + continue; |
| 486 | + } |
| 487 | + tickMarks.AppendElement(tickMark); |
| 488 | + } |
| 489 | + tickMarks.Sort(); |
| 490 | + return tickMarks; |
| 491 | +} |
| 492 | + |
| 493 | +mozilla::dom::HTMLInputElement& nsRangeFrame::InputElement() const { |
| 494 | + MOZ_ASSERT(mContent->IsHTMLElement(nsGkAtoms::input), "bad cast"); |
| 495 | + auto& input = *static_cast<dom::HTMLInputElement*>(GetContent()); |
| 496 | + MOZ_ASSERT(input.ControlType() == FormControlType::InputRange); |
| 497 | + return input; |
| 498 | +} |
| 499 | + |
457 | 500 | void nsRangeFrame::DoUpdateThumbPosition(nsIFrame* aThumbFrame,
|
458 | 501 | const nsSize& aRangeSize) {
|
459 | 502 | MOZ_ASSERT(aThumbFrame);
|
|
0 commit comments