Skip to content

Commit 7a1ca0d

Browse files
committed
Bug 1919718 - Part 2: Implement HTMLInputElement.alpha r=emilio,webidl,firefox-style-system-reviewers,layout-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D278296
1 parent 609b28f commit 7a1ca0d

File tree

14 files changed

+67
-462
lines changed

14 files changed

+67
-462
lines changed

dom/html/HTMLInputElement.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "mozilla/dom/HTMLInputElement.h"
88

99
#include <algorithm>
10+
#include <cmath>
1011

1112
#include "HTMLDataListElement.h"
1213
#include "HTMLFormSubmissionConstants.h"
@@ -774,26 +775,52 @@ static void SerializeColorForHTMLCompatibility(const StyleAbsoluteColor& aColor,
774775
NS_GET_B(color));
775776
}
776777

778+
static void ClampColorComponents(StyleAbsoluteColor& aColor) {
779+
MOZ_ASSERT(aColor.color_space == StyleColorSpace::Srgb);
780+
aColor.components._0 = std::clamp(aColor.components._0, 0.0f, 1.0f);
781+
aColor.components._1 = std::clamp(aColor.components._1, 0.0f, 1.0f);
782+
aColor.components._2 = std::clamp(aColor.components._2, 0.0f, 1.0f);
783+
}
784+
777785
// https://html.spec.whatwg.org/#serialize-a-color-well-control-color
778786
static void SerializeColor(const StyleAbsoluteColor& aColor,
779787
StyleColorSpace aTargetColorSpace,
780-
nsAString& aResult) {
781-
// A few steps are ignored given alpha support is still coming.
788+
bool aSpecifiedAlpha, nsAString& aResult) {
789+
// Step 2: Let htmlCompatible be false.
790+
bool htmlCompatible = false;
782791

783792
// Step 3: If element's alpha attribute is not specified, then set color's
784793
// alpha component to be fully opaque.
785794
// (Setting colorspace here as it's easier.)
786795
StyleAbsoluteColor color = aColor.ToColorSpace(aTargetColorSpace);
787-
color.alpha = 1.0;
796+
if (!aSpecifiedAlpha) {
797+
color.alpha = 1.0;
798+
}
788799

789800
// Step 4: If element's colorspace attribute is in the Limited sRGB state:
790801
if (color.color_space == StyleColorSpace::Srgb) {
791-
// Step 6: ... then do so with HTML-compatible serialization requested.
802+
// Step 4.2: Round each of color's components so they are in the range 0 to
803+
// 255, inclusive. Components are to be rounded towards +∞.
804+
ClampColorComponents(color);
805+
806+
if (!aSpecifiedAlpha) {
807+
// Step 4.3: If element's alpha attribute is not specified, then set
808+
// htmlCompatible to true.
809+
htmlCompatible = true;
810+
} else {
811+
// Step 4.4: Otherwise, set color to color converted using the 'color()'
812+
// function.
813+
// (Unset the legacy bit to force `color()`)
814+
color.flags &= ~StyleColorFlags::IS_LEGACY_SRGB;
815+
}
816+
}
817+
818+
// Step 6: Return the result of serializing color. If htmlCompatible is true,
819+
// then do so with HTML-compatible serialization requested.
820+
if (htmlCompatible) {
792821
SerializeColorForHTMLCompatibility(color, aResult);
793822
return;
794823
}
795-
796-
// Step 6: Return the result of serializing color.
797824
nsAutoCString result;
798825
Servo_AbsoluteColor_ToCss(&color, &result);
799826
CopyUTF8toUTF16(result, aResult);
@@ -821,7 +848,7 @@ nsTArray<nsString> HTMLInputElement::GetColorsFromList() {
821848
// https://html.spec.whatwg.org/#serialize-a-color-well-control-color
822849
if (Maybe<StyleAbsoluteColor> result =
823850
MaybeComputeColor(OwnerDoc(), value)) {
824-
SerializeColor(*result, GetColorSpaceEnum(), value);
851+
SerializeColor(*result, GetColorSpaceEnum(), Alpha(), value);
825852
colors.AppendElement(value);
826853
}
827854
}
@@ -1595,6 +1622,11 @@ void HTMLInputElement::ResultForDialogSubmit(nsAString& aResult) {
15951622
}
15961623
}
15971624

1625+
void HTMLInputElement::SetAlpha(bool aValue, ErrorResult& aRv) {
1626+
SetHTMLBoolAttr(nsGkAtoms::alpha, aValue, aRv);
1627+
UpdateColor();
1628+
}
1629+
15981630
void HTMLInputElement::GetAutocomplete(nsAString& aValue) {
15991631
if (!DoesAutocompleteApply()) {
16001632
return;
@@ -2128,8 +2160,8 @@ void HTMLInputElement::GetColor(InputPickerColor& aValue) {
21282160
aValue.mComponent1 = color.components._0;
21292161
aValue.mComponent2 = color.components._1;
21302162
aValue.mComponent3 = color.components._2;
2163+
aValue.mAlpha = Alpha() ? color.alpha : NAN;
21312164

2132-
// aValue.mAlpha = color.alpha;
21332165
// aValue.mColorSpace = mColorSpace;
21342166
}
21352167

@@ -2162,10 +2194,10 @@ void HTMLInputElement::SetUserInputColor(const InputPickerColor& aValue) {
21622194
._1 = aValue.mComponent2,
21632195
._2 = aValue.mComponent3,
21642196
},
2165-
.alpha = 1,
2197+
.alpha = aValue.mAlpha,
21662198
.color_space = StyleColorSpace::Srgb,
21672199
},
2168-
GetColorSpaceEnum(), serialized);
2200+
GetColorSpaceEnum(), Alpha(), serialized);
21692201

21702202
// (We are either Chrome/UA but the principal doesn't matter for color inputs)
21712203
SetUserInput(serialized, *NodePrincipal());
@@ -5136,7 +5168,7 @@ void HTMLInputElement::SanitizeValue(nsAString& aValue,
51365168
StyleAbsoluteColor color = MaybeComputeColorOrBlack(OwnerDoc(), aValue);
51375169
// Serialization step 6: If htmlCompatible is true, then do so with
51385170
// HTML-compatible serialization requested.
5139-
SerializeColor(color, GetColorSpaceEnum(), aValue);
5171+
SerializeColor(color, GetColorSpaceEnum(), Alpha(), aValue);
51405172
break;
51415173
}
51425174
default:

dom/html/HTMLInputElement.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ class HTMLInputElement final : public TextControlElement,
452452
SetHTMLAttr(nsGkAtoms::accept, aValue, aRv);
453453
}
454454

455+
bool Alpha() const { return HasAttr(nsGkAtoms::alpha); }
456+
void SetAlpha(bool aValue, ErrorResult& aRv);
457+
455458
void GetAlt(nsAString& aValue) { GetHTMLAttr(nsGkAtoms::alt, aValue); }
456459
void SetAlt(const nsAString& aValue, ErrorResult& aRv) {
457460
SetHTMLAttr(nsGkAtoms::alt, aValue, aRv);

dom/webidl/HTMLInputElement.webidl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface HTMLInputElement : HTMLElement {
2828

2929
[CEReactions, Pure, SetterThrows]
3030
attribute DOMString accept;
31+
[CEReactions, Pure, SetterThrows, Pref="dom.forms.colorspace_and_alpha.enabled"]
32+
attribute boolean alpha;
3133
[CEReactions, Pure, SetterThrows]
3234
attribute DOMString alt;
3335
[CEReactions, Pure, SetterThrows]
@@ -302,8 +304,8 @@ dictionary InputPickerColor {
302304
required float component2;
303305
required float component3;
304306

305-
// bug 1919718
306-
// required float alpha;
307+
required unrestricted float alpha;
308+
// bug 2009748
307309
// required InputColorSpace colorSpace;
308310
};
309311

layout/forms/nsColorControlFrame.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ void nsColorControlFrame::UpdateColor() {
8282
return;
8383
}
8484

85-
// Set the background-color CSS property of the swatch element to this color.
85+
// Set the color CSS property of the swatch element to this color.
8686
mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
87-
u"background-color:"_ns + color,
87+
u"color:"_ns + color,
8888
/* aNotify */ true);
8989
}
9090

layout/style/res/forms.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ input[type="image"]:disabled {
438438
box-sizing: border-box;
439439
border: 1px solid grey;
440440
display: block;
441+
background-color: #fff;
442+
background-image: linear-gradient(currentColor), repeating-conic-gradient(#ccc 0 25%, transparent 0 50%);
443+
background-size: 12px 12px;
441444
}
442445

443446
/* radio buttons */

testing/web-platform/meta/html/dom/idlharness.https.html.ini

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,78 +2030,6 @@ prefs: [dom.security.featurePolicy.experimental.enabled:true, dom.security.featu
20302030
[HTMLMediaElement interface: attribute videoTracks]
20312031
expected: FAIL
20322032

2033-
[HTMLInputElement interface: attribute alpha]
2034-
expected: FAIL
2035-
2036-
[HTMLInputElement interface: document.createElement("input") must inherit property "alpha" with the proper type]
2037-
expected: FAIL
2038-
2039-
[HTMLInputElement interface: createInput("text") must inherit property "alpha" with the proper type]
2040-
expected: FAIL
2041-
2042-
[HTMLInputElement interface: createInput("hidden") must inherit property "alpha" with the proper type]
2043-
expected: FAIL
2044-
2045-
[HTMLInputElement interface: createInput("search") must inherit property "alpha" with the proper type]
2046-
expected: FAIL
2047-
2048-
[HTMLInputElement interface: createInput("tel") must inherit property "alpha" with the proper type]
2049-
expected: FAIL
2050-
2051-
[HTMLInputElement interface: createInput("url") must inherit property "alpha" with the proper type]
2052-
expected: FAIL
2053-
2054-
[HTMLInputElement interface: createInput("email") must inherit property "alpha" with the proper type]
2055-
expected: FAIL
2056-
2057-
[HTMLInputElement interface: createInput("password") must inherit property "alpha" with the proper type]
2058-
expected: FAIL
2059-
2060-
[HTMLInputElement interface: createInput("date") must inherit property "alpha" with the proper type]
2061-
expected: FAIL
2062-
2063-
[HTMLInputElement interface: createInput("month") must inherit property "alpha" with the proper type]
2064-
expected: FAIL
2065-
2066-
[HTMLInputElement interface: createInput("week") must inherit property "alpha" with the proper type]
2067-
expected: FAIL
2068-
2069-
[HTMLInputElement interface: createInput("time") must inherit property "alpha" with the proper type]
2070-
expected: FAIL
2071-
2072-
[HTMLInputElement interface: createInput("datetime-local") must inherit property "alpha" with the proper type]
2073-
expected: FAIL
2074-
2075-
[HTMLInputElement interface: createInput("number") must inherit property "alpha" with the proper type]
2076-
expected: FAIL
2077-
2078-
[HTMLInputElement interface: createInput("range") must inherit property "alpha" with the proper type]
2079-
expected: FAIL
2080-
2081-
[HTMLInputElement interface: createInput("color") must inherit property "alpha" with the proper type]
2082-
expected: FAIL
2083-
2084-
[HTMLInputElement interface: createInput("checkbox") must inherit property "alpha" with the proper type]
2085-
expected: FAIL
2086-
2087-
[HTMLInputElement interface: createInput("radio") must inherit property "alpha" with the proper type]
2088-
expected: FAIL
2089-
2090-
[HTMLInputElement interface: createInput("file") must inherit property "alpha" with the proper type]
2091-
expected: FAIL
2092-
2093-
[HTMLInputElement interface: createInput("submit") must inherit property "alpha" with the proper type]
2094-
expected: FAIL
2095-
2096-
[HTMLInputElement interface: createInput("image") must inherit property "alpha" with the proper type]
2097-
expected: FAIL
2098-
2099-
[HTMLInputElement interface: createInput("reset") must inherit property "alpha" with the proper type]
2100-
expected: FAIL
2101-
2102-
[HTMLInputElement interface: createInput("button") must inherit property "alpha" with the proper type]
2103-
expected: FAIL
2104-
21052033
[HTMLScriptElement interface: attribute blocking]
21062034
expected: FAIL
21072035

testing/web-platform/meta/html/semantics/forms/the-input-element/color-attributes.window.js.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)