Skip to content

Commit d8d7f9c

Browse files
authored
Add a separate StylesheetParser.interpolatedStringToken() method (#2675)
Unlike `StringExpression.asInterpolation()`, this ensures that the text between interpolated expressions exactly matches the text in the original document.
1 parent 9d68793 commit d8d7f9c

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

lib/src/parse/stylesheet.dart

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,13 +1499,13 @@ abstract class StylesheetParser extends Parser {
14991499
} else {
15001500
scanner.expectChar($lparen);
15011501
whitespace(consumeNewlines: false);
1502-
var argument = interpolatedString();
1502+
var argument = interpolatedStringToken();
15031503
scanner.expectChar($rparen);
15041504

15051505
buffer
15061506
..write(identifier)
15071507
..writeCharCode($lparen)
1508-
..addInterpolation(argument.asInterpolation())
1508+
..addInterpolation(argument)
15091509
..writeCharCode($rparen);
15101510
}
15111511

@@ -1521,7 +1521,7 @@ abstract class StylesheetParser extends Parser {
15211521
case "regexp":
15221522
buffer.write("regexp(");
15231523
scanner.expectChar($lparen);
1524-
buffer.addInterpolation(interpolatedString().asInterpolation());
1524+
buffer.addInterpolation(interpolatedStringToken());
15251525
scanner.expectChar($rparen);
15261526
buffer.writeCharCode($rparen);
15271527
needsDeprecationWarning = true;
@@ -2813,8 +2813,8 @@ abstract class StylesheetParser extends Parser {
28132813

28142814
/// Consumes a quoted string expression.
28152815
StringExpression interpolatedString() {
2816-
// NOTE: this logic is largely duplicated in Parser.string. Most changes
2817-
// here should be mirrored there.
2816+
// NOTE: this logic is largely duplicated in [Parser.string] and
2817+
// [interpolatedStringToken]. Most changes here should be mirrored there.
28182818

28192819
var start = scanner.state;
28202820
var quote = scanner.readChar();
@@ -2855,6 +2855,50 @@ abstract class StylesheetParser extends Parser {
28552855
);
28562856
}
28572857

2858+
/// Consumes a quoted string expression without processing its contents
2859+
/// semantically.
2860+
Interpolation interpolatedStringToken() {
2861+
// NOTE: this logic is largely duplicated in [Parser.string] and
2862+
// [interpolatedString]. Most changes here should be mirrored there.
2863+
2864+
var start = scanner.state;
2865+
var quote = scanner.readChar();
2866+
2867+
if (quote != $single_quote && quote != $double_quote) {
2868+
scanner.error("Expected string.", position: start.position);
2869+
}
2870+
2871+
var buffer = InterpolationBuffer()..writeCharCode(quote);
2872+
loop:
2873+
while (true) {
2874+
switch (scanner.peekChar()) {
2875+
case var next when next == quote:
2876+
buffer.writeCharCode(scanner.readChar());
2877+
break loop;
2878+
case null || int(isNewline: true):
2879+
scanner.error("Expected ${String.fromCharCode(quote)}.");
2880+
case $backslash:
2881+
var second = scanner.peekChar(1);
2882+
if (second.isNewline) {
2883+
buffer.writeCharCode(scanner.readChar());
2884+
buffer.writeCharCode(scanner.readChar());
2885+
if (second == $cr) {
2886+
if (scanner.scanChar($lf)) buffer.writeCharCode($lf);
2887+
}
2888+
} else {
2889+
buffer.write(rawText(() => escapeCharacter()));
2890+
}
2891+
case $hash when scanner.peekChar(1) == $lbrace:
2892+
var (expression, span) = singleInterpolation();
2893+
buffer.add(expression, span);
2894+
case _:
2895+
buffer.writeCharCode(scanner.readChar());
2896+
}
2897+
}
2898+
2899+
return buffer.interpolation(spanFrom(start));
2900+
}
2901+
28582902
/// Consumes an expression that starts like an identifier.
28592903
@protected
28602904
Expression identifierLike() {
@@ -3108,7 +3152,7 @@ abstract class StylesheetParser extends Parser {
31083152
buffer.writeCharCode(scanner.readChar());
31093153

31103154
case $double_quote || $single_quote:
3111-
buffer.addInterpolation(interpolatedString().asInterpolation());
3155+
buffer.addInterpolation(interpolatedStringToken());
31123156

31133157
case $slash:
31143158
switch (scanner.peekChar(1)) {
@@ -3231,7 +3275,7 @@ abstract class StylesheetParser extends Parser {
32313275
wroteNewline = false;
32323276

32333277
case $double_quote || $single_quote:
3234-
buffer.addInterpolation(interpolatedString().asInterpolation());
3278+
buffer.addInterpolation(interpolatedStringToken());
32353279
wroteNewline = false;
32363280

32373281
case $slash:

0 commit comments

Comments
 (0)