Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Color 4] Be stricter about slash-separated strings #2238

Merged
merged 2 commits into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions lib/src/functions/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1269,15 +1269,18 @@ Value _parseChannels(String functionName, Value input,
channels = componentList;
}

for (var channel in channels) {
for (var i = 0; i < channels.length; i++) {
var channel = channels[i];
if (!channel.isSpecialNumber &&
channel is! SassNumber &&
!_isNone(channel)) {
var channelName =
space?.channels[channels.indexOf(channel)].name ?? 'channel';
var channelName = space?.channels
.elementAtOrNull(i)
?.name
.andThen((name) => '$name channel') ??
'channel ${i + 1}';
throw SassScriptException(
'Expected $channelName channel to be a number, was $channel.',
name);
'Expected $channelName to be a number, was $channel.', name);
}
}

Expand Down Expand Up @@ -1352,15 +1355,11 @@ Value _parseChannels(String functionName, Value input,
[...var initial, SassString(hasQuotes: false, :var text)] => switch (
text.split('/')) {
[_] => (input, null),
[var channel3 && 'none', var alpha] ||
[var channel3, var alpha && 'none'] =>
switch ((_parseNumberOrNone(channel3), _parseNumberOrNone(alpha))) {
(var channel3Value?, var alphaValue?) => (
SassList([...initial, channel3Value], ListSeparator.space),
alphaValue
),
_ => null
},
[var channel3, var alpha] => (
SassList([...initial, _parseNumberOrString(channel3)],
ListSeparator.space),
_parseNumberOrString(alpha)
),
_ => null
},
[...var initial, SassNumber(asSlash: (var before, var after))] => (
Expand All @@ -1370,15 +1369,12 @@ Value _parseChannels(String functionName, Value input,
_ => (input, null)
};

/// Parses [text] as either a Sass number or the unquoted Sass string "none".
///
/// If neither matches, returns null.
Value? _parseNumberOrNone(String text) {
if (text == 'none') return SassString('none', quotes: false);
/// Parses [text] as either a Sass number or an unquoted Sass string.
Value _parseNumberOrString(String text) {
try {
return ScssParser(text).parseNumber();
} on SassFormatException {
return null;
return SassString(text, quotes: false);
}
}

Expand Down
Loading