Skip to content

Commit 63181cf

Browse files
committed
Dedent properly in certain situations
1 parent 09f7ad1 commit 63181cf

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

src/Blade/CodeComponent.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace Torchlight\Blade;
77

8+
use Illuminate\Support\Str;
89
use Illuminate\View\Component;
910
use Torchlight\Block;
1011
use Torchlight\Torchlight;
@@ -19,6 +20,8 @@ class CodeComponent extends Component
1920

2021
public $block;
2122

23+
protected $trimFixDelimiter = '##LARAVEL_TRIM_FIXER##';
24+
2225
/**
2326
* Create a new component instance.
2427
*
@@ -36,11 +39,48 @@ public function __construct($language, $theme = null, $contents = null, $torchli
3639
$this->block = Block::make($torchlightId)->language($this->language)->theme($this->theme);
3740
}
3841

42+
public function withAttributes(array $attributes)
43+
{
44+
// By default Laravel trims slot content in the ManagesComponents
45+
// trait. The line that does the trimming looks like this:
46+
// `$defaultSlot = new HtmlString(trim(ob_get_clean()));`
47+
48+
// The problem with this is that when you have a Blade Component
49+
// that is indented in this way:
50+
51+
// <pre>
52+
// <x-torchlight-code>
53+
// public function {
54+
// // test
55+
// }
56+
// </x-torchlight-code>
57+
// </pre>
58+
59+
// Then Laravel will strip the leading whitespace off of the first
60+
// line, of content making it impossible for us to know how
61+
// much to dedent the rest of the code.
62+
63+
// We're hijacking this `withAttributes` method because it is called
64+
// _after_ the buffer is opened but before the content. So we echo
65+
// out some nonsense which will prevent Laravel from trimming
66+
// the whitespace. We'll replace it later. We only do this
67+
// if it's not a file-based-contents component.
68+
if (is_null($this->contents)) {
69+
echo $this->trimFixDelimiter;
70+
}
71+
72+
return parent::withAttributes($attributes);
73+
}
74+
3975
public function capture($contents)
4076
{
4177
$contents = $contents ?: $this->contents;
4278
$contents = Torchlight::processFileContents($contents) ?: $contents;
4379

80+
if (Str::startsWith($contents, $this->trimFixDelimiter)) {
81+
$contents = Str::replaceFirst($this->trimFixDelimiter, '', $contents);
82+
}
83+
4484
BladeManager::registerBlock($this->block->code($contents));
4585
}
4686

tests/MiddlewareAndComponentTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,37 @@ public function code_contents_can_be_a_file_2()
186186
});
187187
}
188188

189+
/** @test */
190+
public function dedent_works_properly()
191+
{
192+
$this->withoutExceptionHandling();
193+
$this->fakeNullResponse('component');
194+
195+
$response = $this->getView('dedent_works_properly.blade.php');
196+
197+
$result = "<code class=\"torchlight\" style=\"\"><div class='line'>public function {</div><div class='line'> // test</div><div class='line'>}</div></code>";
198+
$this->assertEquals(
199+
"<pre>\n $result</pre>\n<pre>$result</pre>\n<pre>$result</pre>",
200+
$response->content()
201+
);
202+
}
203+
204+
/** @test */
205+
public function two_code_in_one_pre()
206+
{
207+
$this->withoutExceptionHandling();
208+
$this->fakeNullResponse('component');
209+
210+
$response = $this->getView('two-codes-in-one-tag.blade.php');
211+
212+
$result = "<code class=\"torchlight\" style=\"\"><div class='line'>public function {</div><div class='line'> // test</div><div class='line'>}</div></code>";
213+
$this->assertEquals(
214+
"<pre>\n $result $result</pre>",
215+
$response->content()
216+
);
217+
}
218+
219+
189220
/** @test */
190221
public function two_components_work()
191222
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<pre>
2+
<x-torchlight-code torchlight-id='component' language='php'>
3+
public function {
4+
// test
5+
}
6+
</x-torchlight-code>
7+
</pre>
8+
<pre><x-torchlight-code torchlight-id='component' language='php'>
9+
public function {
10+
// test
11+
}
12+
</x-torchlight-code></pre>
13+
<pre><x-torchlight-code torchlight-id='component' language='php'>public function {
14+
// test
15+
}
16+
</x-torchlight-code></pre>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<pre>
2+
<x-torchlight-code torchlight-id='component' language='php'>
3+
public function {
4+
// test
5+
}
6+
</x-torchlight-code>
7+
<x-torchlight-code torchlight-id='component' language='php'>
8+
public function {
9+
// test
10+
}
11+
</x-torchlight-code>
12+
</pre>

0 commit comments

Comments
 (0)