Skip to content

Commit

Permalink
[Color 4] Be stricter about slash-separated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed May 10, 2024
1 parent 2c92c89 commit c4e8e5c
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 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,16 +1355,15 @@ 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
},
_ => null
[var channel3, var alpha] => (
SassList([...initial, _parseNumberOrString(channel3)],
ListSeparator.space),
_parseNumberOrString(alpha)
),
var components => throw SassScriptException(
"Only 2 slash-separated elements allowed, but ${components.length} "
"${pluralize('was', components.length, plural: 'were')} passed.",
name)
},
[...var initial, SassNumber(asSlash: (var before, var after))] => (
SassList([...initial, before], ListSeparator.space),
Expand All @@ -1370,15 +1372,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

0 comments on commit c4e8e5c

Please sign in to comment.