Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/view/src/Parser/TempestViewLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ private function consume(int $length = 1): string

if ($length === 1) {
$char = $this->html[$this->position++] ?? null;

if ($char === "\n") {
$this->line++;
}

$this->current = $this->html[$this->position] ?? null;
return $char ?? '';
}

$buffer = substr($this->html, $this->position, $length);
$this->position += $length;
$this->line += substr_count($buffer, "\n");
$this->current = $this->html[$this->position] ?? null;

return $buffer;
Expand Down
25 changes: 25 additions & 0 deletions packages/view/tests/TempestViewLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,31 @@ public function test_single_quote_attributes(): void
);
}

public function test_source_mapping_line_count(): void
{
$tokens = iterator_to_array(
new TempestViewLexer("<div>\n<span></span>\n</div>")->lex(),
);

$this->assertSame(1, $tokens[0]->line);
$this->assertSame(1, $tokens[1]->line);
$this->assertSame(1, $tokens[2]->line);
$this->assertSame(2, $tokens[3]->line);
$this->assertSame(2, $tokens[4]->line);
$this->assertSame(2, $tokens[5]->line);
$this->assertSame(2, $tokens[6]->line);
$this->assertSame(3, $tokens[7]->line);
}

public function test_source_mapping_line_count_with_indentation(): void
{
$tokens = iterator_to_array(
new TempestViewLexer("<div>\n <span></span>\n</div>")->lex(),
);

$this->assertSame(2, $tokens[3]->line);
}

private function assertTokens(array $expected, TokenCollection $actual): void
{
$this->assertCount(count($expected), $actual);
Expand Down
Loading