Skip to content

Commit 44e2937

Browse files
committed
Fix Links in blade views are misplaced after multibyte characters
Fixes N1ebieski#9
1 parent 5823a57 commit 44e2937

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

app/Parsers/InlineHtmlParser.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ class InlineHtmlParser extends AbstractParser
3434

3535
protected array $items = [];
3636

37+
/**
38+
* There is a bug with the Stillat\BladeParser\Document\Document::fromText parser.
39+
* It doesn't parse the special characters correctly. It treats these characters
40+
* as indentations and spaces resulting in a miscalculated Node position.
41+
*
42+
* This function replaces the special characters with a single, placeholder character
43+
*/
44+
private function replaceSpecialAndEmoji(string $text, string $placeholder = '*'): string
45+
{
46+
return preg_replace(
47+
'/[\x{3040}-\x{30FF}\x{4E00}-\x{9FFF}\x{1F300}-\x{1FAFF}\x{1F000}-\x{1F6FF}\x{2190}-\x{21AA}\x{2300}-\x{23FF}\x{25A0}-\x{25FF}\x{1F600}-\x{1F64F}\x{1F680}-\x{1F6FF}\x{2700}-\x{27BF}]/u',
48+
$placeholder,
49+
$text
50+
);
51+
}
52+
3753
public function parse(InlineHtml $node)
3854
{
3955
if ($node->getStartPosition() > 0) {
@@ -46,7 +62,9 @@ public function parse(InlineHtml $node)
4662
$this->startLine = $range->start->line;
4763
}
4864

49-
$this->parseBladeContent(Document::fromText($node->getText()));
65+
$this->parseBladeContent(Document::fromText(
66+
$this->replaceSpecialAndEmoji($node->getText())
67+
));
5068

5169
if (count($this->items)) {
5270
$blade = new Blade;
@@ -95,7 +113,7 @@ protected function doEchoParse(BaseNode $node, $prefix, $content)
95113
}
96114

97115
$range->start->line += $this->startLine + $node->position->startLine - 2;
98-
$range->end->line += $this->startLine + $node->position->startLine - 2;
116+
$range->end->line += $this->startLine + $node->position->startLine - 2;
99117

100118
return $range;
101119
};

0 commit comments

Comments
 (0)