Skip to content

Fix Links in blade views are misplaced after multibyte characters #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions app/Parsers/InlineHtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class InlineHtmlParser extends AbstractParser

protected array $items = [];

/**
* Stillat\BladeParser\Document\Document::fromText treats multibyte characters
* as indentations and spaces resulting in a miscalculated Node position.
*
* This function replaces the multibyte characters with a single, placeholder character
*/
private function replaceMultibyteChars(string $text, string $placeholder = '*'): string
{
return preg_replace('/[^\x00-\x7F]/u', $placeholder, $text);
}

public function parse(InlineHtml $node)
{
if ($node->getStartPosition() > 0) {
Expand All @@ -46,7 +57,9 @@ public function parse(InlineHtml $node)
$this->startLine = $range->start->line;
}

$this->parseBladeContent(Document::fromText($node->getText()));
$this->parseBladeContent(Document::fromText(
$this->replaceMultibyteChars($node->getText())
));

if (count($this->items)) {
$blade = new Blade;
Expand Down Expand Up @@ -95,7 +108,7 @@ protected function doEchoParse(BaseNode $node, $prefix, $content)
}

$range->start->line += $this->startLine + $node->position->startLine - 2;
$range->end->line += $this->startLine + $node->position->startLine - 2;
$range->end->line += $this->startLine + $node->position->startLine - 2;

return $range;
};
Expand Down