Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit 541b168

Browse files
committed
Merge pull request #12 from winstonewert/master
Fixes a bug in the CSS string escape handling.
2 parents 5e05216 + bc7bf22 commit 541b168

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.1
2+
3+
* Fix to handling of escapes in strings.
4+
15
## 0.12.0+1
26

37
* Allow the lastest version of `logging` package.

lib/parser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,8 +2363,8 @@ class _Parser {
23632363

23642364
// Note: disable skipping whitespace tokens inside a string.
23652365
// TODO(jmesserly): the layering here feels wrong.
2366-
var skipWhitespace = tokenizer._skipWhitespace;
2367-
tokenizer._skipWhitespace = false;
2366+
var inString = tokenizer._inString;
2367+
tokenizer._inString = false;
23682368

23692369
switch (_peek()) {
23702370
case TokenKind.SINGLE_QUOTE:
@@ -2396,7 +2396,7 @@ class _Parser {
23962396
stringValue.write(_next().text);
23972397
}
23982398

2399-
tokenizer._skipWhitespace = skipWhitespace;
2399+
tokenizer._inString = inString;
24002400

24012401
// All characters between quotes is the string.
24022402
if (stopToken != TokenKind.RPAREN) {

lib/src/tokenizer.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class Tokenizer extends TokenizerBase {
273273
// if followed by hexadecimal digits, create the appropriate character.
274274
// otherwise, include the character in the identifier and don't treat it
275275
// specially.
276-
if (ch == 92 /*\*/) {
276+
if (ch == 92 /*\*/ && _inString) {
277277
int startHex = ++_index;
278278
eatHexDigits(startHex + 6);
279279
if (_index != startHex) {
@@ -395,7 +395,7 @@ class Tokenizer extends TokenizerBase {
395395
return _finishToken(TokenKind.INCOMPLETE_COMMENT);
396396
} else if (ch == 42 /*'*'*/) {
397397
if (_maybeEatChar(47 /*'/'*/)) {
398-
if (_skipWhitespace) {
398+
if (_inString) {
399399
return next();
400400
} else {
401401
return _finishToken(TokenKind.COMMENT);
@@ -405,7 +405,7 @@ class Tokenizer extends TokenizerBase {
405405
/* Check if close part of Comment Definition --> (CDC). */
406406
if (_maybeEatChar(TokenChar.MINUS)) {
407407
if (_maybeEatChar(TokenChar.GREATER)) {
408-
if (_skipWhitespace) {
408+
if (_inString) {
409409
return next();
410410
} else {
411411
return _finishToken(TokenKind.HTML_COMMENT);

lib/src/tokenizer_base.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class TokenizerBase {
2727
final SourceFile _file;
2828
final String _text;
2929

30-
bool _skipWhitespace;
30+
bool _inString;
3131

3232
/**
3333
* Changes tokenization when in a pseudo function expression. If true then
@@ -55,7 +55,7 @@ abstract class TokenizerBase {
5555
int _index = 0;
5656
int _startIndex = 0;
5757

58-
TokenizerBase(this._file, this._text, this._skipWhitespace,
58+
TokenizerBase(this._file, this._text, this._inString,
5959
[this._index = 0]);
6060

6161
Token next();
@@ -119,12 +119,12 @@ abstract class TokenizerBase {
119119
ch == TokenChar.RETURN) {
120120
// do nothing
121121
} else if (ch == TokenChar.NEWLINE) {
122-
if (!_skipWhitespace) {
122+
if (!_inString) {
123123
return _finishToken(TokenKind.WHITESPACE); // note the newline?
124124
}
125125
} else {
126126
_index--;
127-
if (_skipWhitespace) {
127+
if (_inString) {
128128
return next();
129129
} else {
130130
return _finishToken(TokenKind.WHITESPACE);
@@ -151,7 +151,7 @@ abstract class TokenizerBase {
151151
}
152152
} while (nesting > 0);
153153

154-
if (_skipWhitespace) {
154+
if (_inString) {
155155
return next();
156156
} else {
157157
return _finishToken(TokenKind.COMMENT);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: csslib
2-
version: 0.12.0+1
2+
version: 0.12.1
33
author: Polymer.dart Team <[email protected]>
44
description: A library for parsing CSS.
55
homepage: https://github.com/dart-lang/csslib

test/compiler_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,19 @@ void testHost() {
677677
}''');
678678
}
679679

680+
void testStringEscape() {
681+
var errors = [];
682+
var input = r'''a { foo: '{"text" : "a\\\""}' }''';
683+
var stylesheet = parseCss(input, errors: errors, opts: simpleOptions);
684+
expect(stylesheet != null, true);
685+
expect(errors.isEmpty, true, reason: errors.toString());
686+
687+
expect(prettyPrint(stylesheet), r'''
688+
a {
689+
foo: '{"text" : "a\\\""}';
690+
}''');
691+
}
692+
680693
// TODO(terry): Move to emitter_test.dart when real emitter exist.
681694
void testEmitter() {
682695
var errors = [];
@@ -721,6 +734,7 @@ main() {
721734
test('Attributes', testAttribute);
722735
test('Negation', testNegation);
723736
test('@host', testHost);
737+
test('stringEscape', testStringEscape);
724738
test('Parse List<int> as input', testArrayOfChars);
725739
test('Simple Emitter', testEmitter);
726740
}

0 commit comments

Comments
 (0)