Skip to content

Commit 26401fb

Browse files
authored
Fix an attribute selector quoting bug (#599)
Closes #598
1 parent b22ae51 commit 26401fb

File tree

4 files changed

+21
-60
lines changed

4 files changed

+21
-60
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.17.1
2+
3+
* Properly quote attribute selector values that start with identifiers but end
4+
with a non-identifier character.
5+
16
## 1.17.0
27

38
* Improve error output, particularly for errors that cover multiple lines.

lib/src/parse/parser.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class Parser {
3131
static String parseIdentifier(String text, {Logger logger}) =>
3232
Parser(text, logger: logger)._parseIdentifier();
3333

34+
/// Returns whether [text] is a valid CSS identifier.
35+
static bool isIdentifier(String text, {Logger logger}) {
36+
try {
37+
parseIdentifier(text, logger: logger);
38+
return true;
39+
} on SassFormatException {
40+
return false;
41+
}
42+
}
43+
3444
@protected
3545
Parser(String contents, {url, Logger logger})
3646
: scanner = SpanScanner(contents, sourceUrl: url),

lib/src/visitor/serialize.dart

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import '../ast/node.dart';
1515
import '../ast/selector.dart';
1616
import '../color_names.dart';
1717
import '../exception.dart';
18+
import '../parse/parser.dart';
1819
import '../utils.dart';
1920
import '../util/character.dart';
2021
import '../util/no_source_map_buffer.dart';
@@ -903,7 +904,10 @@ class _SerializeVisitor implements CssVisitor, ValueVisitor, SelectorVisitor {
903904
_buffer.write(attribute.name);
904905
if (attribute.op != null) {
905906
_buffer.write(attribute.op);
906-
if (_isIdentifier(attribute.value)) {
907+
if (Parser.isIdentifier(attribute.value) &&
908+
// Emit identifiers that start with `--` with quotes, because IE11
909+
// doesn't consider them to be valid identifiers.
910+
!attribute.value.startsWith('--')) {
907911
_buffer.write(attribute.value);
908912
} else {
909913
_visitQuotedString(attribute.value);
@@ -1133,64 +1137,6 @@ class _SerializeVisitor implements CssVisitor, ValueVisitor, SelectorVisitor {
11331137
return false;
11341138
}
11351139
}
1136-
1137-
/// Returns whether [text] is a valid identifier.
1138-
///
1139-
/// This *doesn't* consider identifiers beginning with `--` to be valid,
1140-
/// because IE 11 doesn't.
1141-
bool _isIdentifier(String text) {
1142-
var scanner = StringScanner(text);
1143-
scanner.scanChar($dash);
1144-
1145-
if (scanner.isDone) return false;
1146-
var first = scanner.readChar();
1147-
1148-
if (isNameStart(first)) {
1149-
if (scanner.isDone) return true;
1150-
scanner.readChar();
1151-
} else if (first == $backslash) {
1152-
if (!_consumeEscape(scanner)) return false;
1153-
} else {
1154-
return false;
1155-
}
1156-
1157-
while (true) {
1158-
var next = scanner.peekChar();
1159-
if (next == null) return true;
1160-
1161-
if (isName(next)) {
1162-
scanner.readChar();
1163-
} else if (next == $backslash) {
1164-
if (!_consumeEscape(scanner)) return false;
1165-
} else {
1166-
return false;
1167-
}
1168-
}
1169-
}
1170-
1171-
/// Consumes an escape sequence in [scanner].
1172-
///
1173-
/// Returns whether a valid escape was consumed.
1174-
bool _consumeEscape(StringScanner scanner) {
1175-
scanner.expectChar($backslash);
1176-
1177-
var first = scanner.peekChar();
1178-
if (first == null || isNewline(first)) return false;
1179-
1180-
if (isHex(first)) {
1181-
for (var i = 0; i < 6; i++) {
1182-
var next = scanner.peekChar();
1183-
if (next == null || !isHex(next)) break;
1184-
scanner.readChar();
1185-
}
1186-
if (isWhitespace(scanner.peekChar())) scanner.readChar();
1187-
} else {
1188-
if (scanner.isDone) return false;
1189-
scanner.readChar();
1190-
}
1191-
1192-
return true;
1193-
}
11941140
}
11951141

11961142
/// An enum of generated CSS styles.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.17.0
2+
version: 1.17.1
33
description: A Sass implementation in Dart.
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/sass/dart-sass

0 commit comments

Comments
 (0)