Skip to content

Commit c643f04

Browse files
committedNov 11, 2024·
fix(material/core): incorrect validation in get-theme-color (#29994)
Fixes that the `get-theme-color` function was incorrectly validating its number of arguments. We were always checking if the arguments are between 2 and 4, whereas it actually accepts either 2 or 3 for M3 themes and between 2 and 4 for M2 themes. Fixes #29819. (cherry picked from commit fe631c5)
1 parent 8900360 commit c643f04

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed
 

‎src/material/core/theming/_inspection.scss

+28-17
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,17 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
6262
}
6363
}
6464

65-
/// Gets a color from a theme object. This function can take 2 or 3 arguments. If 2 arguments are
66-
/// passed, the second argument is treated as the name of a color role. If 3 arguments are passed,
67-
/// the second argument is treated as the name of a color palette (primary, secondary, etc.) and the
68-
/// third is treated as the palette hue (10, 50, etc.)
65+
/// Gets a color from a theme object. This function take a different amount of arguments depending
66+
/// on if it's working with an M2 or M3 theme:
67+
/// - With an M3 theme it accepts either 2 or 3 arguments. If 2 arguments are passed, the second
68+
/// argument is treated as the name of a color role. If 3 arguments are passed, the second argument
69+
/// is treated as the name of a color palette (primary, secondary, etc.) and the third is treated
70+
/// as the palette hue (10, 50, etc.).
71+
/// - With an M2 theme theme it accepts between 2 and 4 arguments, or the equivalent of calling
72+
/// the `m2-get-theme-color` function. The first argument is the theme, the second one is the
73+
/// palette from which to extract the color, the third one is the hue within the palette and the
74+
/// fourth is the opacity of the returned color.
75+
/// the second one is the
6976
/// @param {Map} $theme The theme
7077
/// @param {String} $color-role-or-palette-name The name of the color role to get, or the name of a
7178
/// color palette.
@@ -74,25 +81,29 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
7481
/// @return {Color} The requested theme color.
7582
@function get-theme-color($theme, $args...) {
7683
$version: get-theme-version($theme);
77-
$args-count: list.length($args);
78-
@if $args-count != 1 and $args-count != 2 and $args-count != 3 {
79-
@error #{'Expected between 2 and 4 arguments. Got:'} $args-count + 1;
80-
}
84+
$args-count: list.length($args) + 1;
8185

86+
// M2 theme
8287
@if $version == 0 {
88+
@if $args-count < 2 or $args-count > 4 {
89+
@error 'Expected between 2 and 4 arguments when working with an M2 theme. ' +
90+
'Got: #{$args-count}';
91+
}
8392
@return m2-inspection.get-theme-color($theme, $args...);
8493
}
85-
@else if $version == 1 {
86-
@if $args-count == 1 {
87-
@return _get-theme-role-color($theme, $args...);
88-
}
89-
@else if $args-count == 2 {
90-
@return _get-theme-palette-color($theme, $args...);
94+
95+
// M3 theme
96+
@if $version == 1 {
97+
@if $args-count < 2 or $args-count > 3 {
98+
@error 'Expected either 2 or 3 arguments when working with an M3 theme. Got: #{$args-count}';
9199
}
100+
@return if($args-count == 2,
101+
_get-theme-role-color($theme, $args...),
102+
_get-theme-palette-color($theme, $args...)
103+
);
92104
}
93-
@else {
94-
@error #{'Unrecognized theme version:'} $version;
95-
}
105+
106+
@error 'Unrecognized theme version: #{$version}';
96107
}
97108

98109
/// Gets a role color from a theme object.

‎src/material/core/theming/_m2-inspection.scss

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ $_typography-properties: (font, font-family, line-height, font-size, letter-spac
102102
@return if(map.get($colors, is-dark), dark, light);
103103
}
104104

105-
/// Gets a color from a theme object. This function can take 2 or 3 arguments. If 2 arguments are
106-
/// passed, the second argument is treated as the name of a color role. If 3 arguments are passed,
107-
/// the second argument is treated as the name of a color palette (primary, secondary, etc.) and the
108-
/// third is treated as the palette hue (10, 50, etc.)
105+
/// Gets a color from a theme object. This function can take between 2 and 4 arguments. The first
106+
/// argument is the theme, the second one is the palette from which to extract the color, the third
107+
/// one is the hue within the palette and the fourth is the opacity of the returned color.
109108
/// @param {Map} $theme The theme
110109
/// @param {String} $palette-name The name of a color palette.
111110
/// @param {Number} $hue The palette hue to get (passing this argument means the second argument is
112111
/// interpreted as a palette name).
112+
/// @param {Number} $opacity The alpha channel value for the color.
113113
/// @return {Color} The requested theme color.
114114
@function get-theme-color($theme, $palette-name, $args...) {
115115
$theme: _get-m2-config($theme);

‎src/material/core/theming/tests/theming-inspection-api.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ describe('theming inspection api', () => {
315315
color: mat.get-theme-color($theme);
316316
}
317317
`),
318-
).toThrowError(/Expected between 2 and 4 arguments\. Got: 1/);
318+
).toThrowError(/Expected either 2 or 3 arguments when working with an M3 theme\. Got: 1/);
319319
});
320320

321321
it('should get typography properties from theme', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.