Skip to content

Commit a8e2252

Browse files
committed
Make column and line numbers match Gecko's CSS parser.
1 parent 5a2d392 commit a8e2252

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,26 +451,26 @@ fn serialize_rgba_two_digit_float_if_roundtrips() {
451451
fn line_numbers() {
452452
let mut input = ParserInput::new("foo bar\nbaz\r\n\n\"a\\\r\nb\"");
453453
let mut input = Parser::new(&mut input);
454-
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 1 });
454+
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 0 });
455455
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("foo".into())));
456-
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 4 });
456+
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 3 });
457457
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace(" ")));
458-
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 5 });
458+
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 4 });
459459
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("bar".into())));
460-
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 8 });
460+
assert_eq!(input.current_source_location(), SourceLocation { line: 0, column: 7 });
461461
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace("\n")));
462-
assert_eq!(input.current_source_location(), SourceLocation { line: 2, column: 1 });
462+
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 0 });
463463
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident("baz".into())));
464-
assert_eq!(input.current_source_location(), SourceLocation { line: 2, column: 4 });
464+
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 3 });
465465
let position = input.position();
466466

467467
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace("\r\n\n")));
468-
assert_eq!(input.current_source_location(), SourceLocation { line: 4, column: 1 });
468+
assert_eq!(input.current_source_location(), SourceLocation { line: 3, column: 0 });
469469

470-
assert_eq!(input.source_location(position), SourceLocation { line: 2, column: 4 });
470+
assert_eq!(input.source_location(position), SourceLocation { line: 1, column: 3 });
471471

472472
assert_eq!(input.next_including_whitespace(), Ok(Token::QuotedString("ab".into())));
473-
assert_eq!(input.current_source_location(), SourceLocation { line: 5, column: 3 });
473+
assert_eq!(input.current_source_location(), SourceLocation { line: 4, column: 2 });
474474
assert!(input.next_including_whitespace().is_err());
475475
}
476476

src/tokenizer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'a> Tokenizer<'a> {
226226
input: input,
227227
position: 0,
228228
last_known_source_location: Cell::new((SourcePosition(0),
229-
SourceLocation { line: 1, column: 1 })),
229+
SourceLocation { line: 0, column: 0 })),
230230
var_functions: SeenStatus::DontCare,
231231
viewport_percentages: SeenStatus::DontCare,
232232
}
@@ -312,7 +312,7 @@ impl<'a> Tokenizer<'a> {
312312
// So if the requested position is before the last known one,
313313
// start over from the beginning.
314314
position = 0;
315-
location = SourceLocation { line: 1, column: 1 };
315+
location = SourceLocation { line: 0, column: 0 };
316316
}
317317
let mut source = &self.input[position..target];
318318
while let Some(newline_position) = source.find(|c| matches!(c, '\n' | '\r' | '\x0C')) {
@@ -321,7 +321,7 @@ impl<'a> Tokenizer<'a> {
321321
source = &source[offset..];
322322
position += offset;
323323
location.line += 1;
324-
location.column = 1;
324+
location.column = 0;
325325
}
326326
debug_assert!(position <= target);
327327
location.column += (target - position) as u32;
@@ -397,10 +397,10 @@ pub struct SourcePosition(usize);
397397
/// The line and column number for a given position within the input.
398398
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
399399
pub struct SourceLocation {
400-
/// The line number, starting at 1 for the first line.
400+
/// The line number, starting at 0 for the first line.
401401
pub line: u32,
402402

403-
/// The column number within a line, starting at 1 for first the character of the line.
403+
/// The column number within a line, starting at 0 for first the character of the line.
404404
pub column: u32,
405405
}
406406

0 commit comments

Comments
 (0)