diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp
index e11f054032be..7c75aa86100b 100644
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -56,6 +56,8 @@
#include "nsMappedAttributes.h"
#include "nsIFormControl.h"
#include "mozilla/dom/Document.h"
+#include "mozilla/dom/HTMLDataListElement.h"
+#include "mozilla/dom/HTMLOptionElement.h"
#include "nsIFormControlFrame.h"
#include "nsITextControlFrame.h"
#include "nsIFrame.h"
@@ -687,6 +689,33 @@ static bool IsPopupBlocked(Document* aDoc) {
return true;
}
+nsTArray HTMLInputElement::GetColorsFromList() {
+ RefPtr dataList = GetList();
+ if (!dataList) {
+ return {};
+ }
+
+ nsTArray colors;
+
+ RefPtr options = dataList->Options();
+ uint32_t length = options->Length(true);
+ for (uint32_t i = 0; i < length; ++i) {
+ auto* option = HTMLOptionElement::FromNodeOrNull(options->Item(i, false));
+ if (!option) {
+ continue;
+ }
+
+ nsString value;
+ option->GetValue(value);
+ if (IsValidSimpleColor(value)) {
+ ToLowerCase(value);
+ colors.AppendElement(value);
+ }
+ }
+
+ return colors;
+}
+
nsresult HTMLInputElement::InitColorPicker() {
MOZ_ASSERT(IsMutable());
@@ -719,7 +748,8 @@ nsresult HTMLInputElement::InitColorPicker() {
nsAutoString initialValue;
GetNonFileValueInternal(initialValue);
- nsresult rv = colorPicker->Init(win, title, initialValue);
+ nsTArray colors = GetColorsFromList();
+ nsresult rv = colorPicker->Init(win, title, initialValue, colors);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr callback =
@@ -4764,6 +4794,22 @@ void HTMLInputElement::SanitizeValue(nsAString& aValue,
}
}
+Maybe HTMLInputElement::ParseSimpleColor(const nsAString& aColor) {
+ // Input color string should be 7 length (i.e. a string representing a valid
+ // simple color)
+ if (aColor.Length() != 7 || aColor.First() != '#') {
+ return {};
+ }
+
+ const nsAString& withoutHash = StringTail(aColor, 6);
+ nscolor color;
+ if (!NS_HexToRGBA(withoutHash, nsHexColorType::NoAlpha, &color)) {
+ return {};
+ }
+
+ return Some(color);
+}
+
bool HTMLInputElement::IsValidSimpleColor(const nsAString& aValue) const {
if (aValue.Length() != 7 || aValue.First() != '#') {
return false;
diff --git a/dom/html/HTMLInputElement.h b/dom/html/HTMLInputElement.h
index 9817bd964f77..ba028f1abc4c 100644
--- a/dom/html/HTMLInputElement.h
+++ b/dom/html/HTMLInputElement.h
@@ -9,6 +9,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/Decimal.h"
+#include "mozilla/Maybe.h"
#include "mozilla/TextControlElement.h"
#include "mozilla/TextControlState.h"
#include "mozilla/UniquePtr.h"
@@ -858,6 +859,9 @@ class HTMLInputElement final : public TextControlElement,
*/
bool IsValueEmpty() const;
+ // Parse a simple (hex) color.
+ static mozilla::Maybe ParseSimpleColor(const nsAString& aColor);
+
protected:
MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual ~HTMLInputElement();
@@ -1384,6 +1388,11 @@ class HTMLInputElement final : public TextControlElement,
*/
nsresult MaybeInitPickers(EventChainPostVisitor& aVisitor);
+ /**
+ * Returns all valid colors in the