1515#include " mozilla/dom/BlobImpl.h"
1616#include " mozilla/dom/CustomEvent.h"
1717#include " mozilla/dom/Directory.h"
18- #include " mozilla/dom/DocumentInlines.h"
1918#include " mozilla/dom/DocumentOrShadowRoot.h"
2019#include " mozilla/dom/ElementBinding.h"
2120#include " mozilla/dom/FileSystemUtils.h"
3433#include " mozilla/Maybe.h"
3534#include " mozilla/MouseEvents.h"
3635#include " mozilla/PresShell.h"
37- #include " mozilla/ServoCSSParser.h"
3836#include " mozilla/StaticPrefs_dom.h"
3937#include " mozilla/StaticPrefs_signon.h"
4038#include " mozilla/TextUtils.h"
@@ -726,40 +724,6 @@ static bool IsPickerBlocked(Document* aDoc) {
726724 return true ;
727725}
728726
729- /* *
730- * Parse a CSS color string and convert it to the target colorspace, to match:
731- * https://html.spec.whatwg.org/#update-a-color-well-control-color
732- *
733- * @param aValue the string to be parsed
734- * @return the parsed result as a HTML compatible form
735- */
736- static StyleAbsoluteColor ComputeColor (Document* aDocument,
737- const nsAString& aValue) {
738- // A few steps are ignored given we don't support alpha and colorspace. See
739- // bug 1919718.
740- return ServoCSSParser::ComputeColorWellControlColor (
741- aDocument->EnsureStyleSet ().RawData (), NS_ConvertUTF16toUTF8(aValue),
742- StyleColorSpace::Srgb);
743- }
744-
745- /* *
746- * https://html.spec.whatwg.org/#serialize-a-color-well-control-color
747- * https://drafts.csswg.org/css-color/#color-serialization-html-compatible-serialization-is-requested
748- *
749- * @param aColor The parsed color
750- * @param aResult The result in the form of #ffffff.
751- */
752- static void SerializeColorForHTMLCompatibility (const StyleAbsoluteColor& aColor,
753- nsAString& aResult) {
754- // Raw StyleAbsoluteColor can have floats outside of 0-1 range e.g. when
755- // display-p3 color is converted to srgb, and ToColor guarantees to fit the
756- // values within the range.
757- nscolor color = aColor.ToColor ();
758- aResult.Truncate ();
759- aResult.AppendPrintf (" #%02x%02x%02x" , NS_GET_R(color), NS_GET_G(color),
760- NS_GET_B (color));
761- }
762-
763727nsTArray<nsString> HTMLInputElement::GetColorsFromList () {
764728 RefPtr<HTMLDataListElement> dataList = GetList ();
765729 if (!dataList) {
@@ -776,15 +740,12 @@ nsTArray<nsString> HTMLInputElement::GetColorsFromList() {
776740 continue ;
777741 }
778742
779- nsAutoString value;
743+ nsString value;
780744 option->GetValue (value);
781- // https://html.spec.whatwg.org/#update-a-color-well-control-color
782- // https://html.spec.whatwg.org/#serialize-a-color-well-control-color
783- StyleAbsoluteColor result = ComputeColor (OwnerDoc (), value);
784- // Serialization step 6: If htmlCompatible is true, then do so with
785- // HTML-compatible serialization requested.
786- SerializeColorForHTMLCompatibility (result, value);
787- colors.AppendElement (value);
745+ if (IsValidSimpleColor (value)) {
746+ ToLowerCase (value);
747+ colors.AppendElement (value);
748+ }
788749 }
789750
790751 return colors;
@@ -5020,14 +4981,13 @@ void HTMLInputElement::SanitizeValue(nsAString& aValue,
50204981 }
50214982 } break ;
50224983 case FormControlType::InputColor: {
5023- // https://html.spec.whatwg.org/#update-a-color-well-control-color
5024- // https://html.spec.whatwg.org/#serialize-a-color-well-control-color
5025- StyleAbsoluteColor color = ComputeColor (OwnerDoc (), aValue);
5026- // Serialization step 6: If htmlCompatible is true, then do so with
5027- // HTML-compatible serialization requested.
5028- SerializeColorForHTMLCompatibility (color, aValue);
5029- break ;
5030- }
4984+ if (IsValidSimpleColor (aValue)) {
4985+ ToLowerCase (aValue);
4986+ } else {
4987+ // Set default (black) color, if aValue wasn't parsed correctly.
4988+ aValue.AssignLiteral (" #000000" );
4989+ }
4990+ } break ;
50314991 default :
50324992 break ;
50334993 }
@@ -5049,6 +5009,20 @@ Maybe<nscolor> HTMLInputElement::ParseSimpleColor(const nsAString& aColor) {
50495009 return Some (color);
50505010}
50515011
5012+ bool HTMLInputElement::IsValidSimpleColor (const nsAString& aValue) const {
5013+ if (aValue.Length () != 7 || aValue.First () != ' #' ) {
5014+ return false ;
5015+ }
5016+
5017+ for (int i = 1 ; i < 7 ; ++i) {
5018+ if (!IsAsciiDigit (aValue[i]) && !(aValue[i] >= ' a' && aValue[i] <= ' f' ) &&
5019+ !(aValue[i] >= ' A' && aValue[i] <= ' F' )) {
5020+ return false ;
5021+ }
5022+ }
5023+ return true ;
5024+ }
5025+
50525026bool HTMLInputElement::IsLeapYear (uint32_t aYear) const {
50535027 if ((aYear % 4 == 0 && aYear % 100 != 0 ) || (aYear % 400 == 0 )) {
50545028 return true ;
0 commit comments