Skip to content

Commit 06cdd6a

Browse files
Fix Pre-8.23.0 Laravel Bug (#2)
* Fix Laravel bug * Additional tests, remove composer 1 * Apply fixes from StyleCI
1 parent e8dc94b commit 06cdd6a

11 files changed

+83
-36
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ jobs:
1616
- laravel: 7.*
1717
testbench: 5.*
1818

19-
- laravel: 7.*
20-
dependency-version: prefer-lowest
21-
composer-version: --1
22-
2319
- laravel: 8.*
2420
testbench: 6.*
2521

@@ -44,7 +40,7 @@ jobs:
4440

4541
- name: Install dependencies
4642
run: |
47-
composer self-update ${{ matrix.composer-version }}
43+
composer self-update
4844
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
4945
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
5046

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
## 0.4.2 - 2021-05-24
6+
7+
### Fixed
8+
- Cover a bug in Laravel pre 8.23.0.
9+
510
## 0.4.1 - 2021-05-23
611

712
### Added

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"illuminate/console": "^7.26.0|^8.0",
2222
"illuminate/http": "^7.26.0|^8.0",
2323
"illuminate/cache": "^7.26.0|^8.0",
24+
"illuminate/view": "^7.26.0|^8.0",
2425
"guzzlehttp/guzzle": "^7.2",
2526
"ramsey/uuid": "^3.7|^4.0"
2627
},

src/Blade/BladeManager.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212

1313
class BladeManager
1414
{
15+
/**
16+
* Laravel before 8.23.0 has a bug that adds extra spaces around components.
17+
* Obviously this is a problem if your component is wrapped in <pre></pre>
18+
* tags, which ours usually is.
19+
*
20+
* @see https://github.com/laravel/framework/blob/8.x/CHANGELOG-8.x.md#v8230-2021-01-19.
21+
* @var bool
22+
*/
23+
public static $affectedBySpacingBug = false;
24+
25+
/**
26+
* @var array
27+
*/
1528
protected static $blocks = [];
1629

1730
public static function registerBlock(Block $block)
@@ -66,6 +79,18 @@ public static function renderContent($content)
6679
$swap[$block->placeholder('styles')] = $block->styles;
6780
}
6881

82+
// If this version of Laravel is affected by the spacing bug, then
83+
// we will swap our our placeholders with a preceding space, and
84+
// a following space. This effectively fixes the bug.
85+
if (static::$affectedBySpacingBug) {
86+
$swap[' ##PRE_TL_COMPONENT##'] = '';
87+
$swap['##POST_TL_COMPONENT## '] = '';
88+
}
89+
90+
// No matter what, always get rid of the placeholders.
91+
$swap['##PRE_TL_COMPONENT##'] = '';
92+
$swap['##POST_TL_COMPONENT##'] = '';
93+
6994
return str_replace(array_keys($swap), array_values($swap), $content);
7095
}
7196
}

src/Blade/CodeComponent.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ public function render()
5959
// classes and style string. Echo out the slot, but capture it using output
6060
// buffering. We then pass it through as the contents to highlight, leaving
6161
// the placeholder so we can replace it later with fully highlighted code.
62+
// We have to add the ##PRE## and ##POST## tags to cover a framework bug.
63+
// @see BladeManager::renderContent.
6264
return <<<'EOT'
63-
<code {{
65+
##PRE_TL_COMPONENT##<code {{
6466
$attributes->except('style')->merge([
6567
'class' => $block->placeholder('classes'),
6668
'style' => $attributes->get('style') . $block->placeholder('styles')
6769
])
68-
}}><?php ob_start(); ?>{{ $slot }}<?php $capture(ob_get_clean()) ?>{{ $block->placeholder() }}</code>
70+
}}><?php ob_start(); ?>{{ $slot }}<?php $capture(ob_get_clean()) ?>{{ $block->placeholder() }}</code>##POST_TL_COMPONENT##
6971
EOT;
7072
}
7173
}

src/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function environment()
7070
}
7171

7272
/**
73-
* @param string $environment
73+
* @param string|null $environment
7474
*/
7575
public function overrideEnvironment($environment = null)
7676
{

src/TorchlightServiceProvider.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Torchlight;
77

88
use Illuminate\Support\ServiceProvider;
9+
use Torchlight\Blade\BladeManager;
910
use Torchlight\Blade\CodeComponent;
1011
use Torchlight\Commands\Install;
1112

@@ -44,11 +45,19 @@ public function publishConfig()
4445

4546
public function registerBladeComponent()
4647
{
47-
if (Torchlight::config('torchlight.blade_components')) {
48-
$this->loadViewComponentsAs('torchlight', [
49-
'code' => CodeComponent::class
50-
]);
48+
if (!Torchlight::config('torchlight.blade_components')) {
49+
return;
5150
}
51+
52+
// Laravel before 8.23.0 has a bug that adds extra spaces around components.
53+
// Obviously this is a problem if your component is wrapped in <pre></pre>
54+
// tags, which ours usually is.
55+
// See https://github.com/laravel/framework/blob/8.x/CHANGELOG-8.x.md#v8230-2021-01-19.
56+
BladeManager::$affectedBySpacingBug = version_compare(app()->version(), '8.23.0', '<');
57+
58+
$this->loadViewComponentsAs('torchlight', [
59+
'code' => CodeComponent::class
60+
]);
5261
}
5362

5463
public function register()

tests/MiddlewareAndComponentTest.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public function getEnvironmentSetUp($app)
2020

2121
protected function getView($view)
2222
{
23+
// When testing multiple versions locally, this helps.
24+
$this->artisan('view:clear');
25+
2326
Route::get('/torchlight', function () use ($view) {
2427
return View::file(__DIR__ . '/Support/' . $view);
2528
})->middleware(RenderTorchlight::class);
@@ -60,8 +63,8 @@ public function it_sends_a_simple_request_with_no_response()
6063
$response = $this->getView('simple-php-hello-world.blade.php');
6164

6265
$this->assertEquals(
63-
'<code class="" style="">echo &quot;hello world&quot;;</code>',
64-
rtrim($response->content())
66+
'<pre><code class="" style="">echo &quot;hello world&quot;;</code></pre>',
67+
$response->content()
6568
);
6669

6770
Http::assertSent(function ($request) {
@@ -83,9 +86,8 @@ public function it_sends_a_simple_request_with_highlighted_response()
8386
$response = $this->getView('simple-php-hello-world.blade.php');
8487

8588
$this->assertEquals(
86-
'<code class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code>',
87-
// See https://github.com/laravel/framework/pull/35874/files for the rtrim reasoning.
88-
rtrim($response->content())
89+
'<pre><code class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code></pre>',
90+
$response->content()
8991
);
9092
}
9193

@@ -98,7 +100,7 @@ public function classes_get_merged()
98100

99101
$this->assertEquals(
100102
'<code class="torchlight mt-4" style="background-color: #292D3E;">this is the highlighted response from the server</code>',
101-
rtrim($response->content())
103+
$response->content()
102104
);
103105
}
104106

@@ -111,7 +113,20 @@ public function attributes_are_preserved()
111113

112114
$this->assertEquals(
113115
'<code class="torchlight" style="background-color: #292D3E;" x-data="{}">this is the highlighted response from the server</code>',
114-
rtrim($response->content())
116+
$response->content()
117+
);
118+
}
119+
120+
/** @test */
121+
public function inline_keeps_its_spaces()
122+
{
123+
$this->legitApiResponse();
124+
125+
$response = $this->getView('an-inline-component.blade.php');
126+
127+
$this->assertEquals(
128+
'this is <code class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code> inline',
129+
$response->content()
115130
);
116131
}
117132

@@ -177,18 +192,11 @@ public function two_components_work()
177192
$response = $this->getView('two-simple-php-hello-world.blade.php');
178193

179194
$expected = <<<EOT
180-
<code class="torchlight1" style="background-color: #111111;">response 1</code>
181-
<code class="torchlight2" style="background-color: #222222;">response 2</code>
182-
EOT;
183-
184-
// See https://github.com/laravel/framework/pull/35874/files for reasoning.
185-
$expected = str_replace("\n", '', $expected);
195+
<pre><code class="torchlight1" style="background-color: #111111;">response 1</code></pre>
186196
187-
// Covering bugfixes from earlier versions of Laravel
188-
// https://github.com/laravel/framework/pull/35874/files.
189-
$actual = rtrim(str_replace("\n", '', $response->content()));
190-
$actual = str_replace('</code> <code', '</code><code', $actual);
197+
<pre><code class="torchlight2" style="background-color: #222222;">response 2</code></pre>
198+
EOT;
191199

192-
$this->assertEquals($expected, $actual);
200+
$this->assertEquals($expected, $response->content());
193201
}
194202
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is <x-torchlight-code torchlight-id='real_response_id' language='php'>echo "hello world"</x-torchlight-code> inline
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<x-torchlight-code torchlight-id='real_response_id' language='php'>
1+
<pre><x-torchlight-code torchlight-id='real_response_id' language='php'>
22
echo "hello world";
3-
</x-torchlight-code>
3+
</x-torchlight-code></pre>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<x-torchlight-code torchlight-id='id1' language='php'>
1+
<pre><x-torchlight-code torchlight-id='id1' language='php'>
22
echo "hello world 1";
3-
</x-torchlight-code>
3+
</x-torchlight-code></pre>
44

5-
<x-torchlight-code torchlight-id='id2' language='php'>
5+
<pre><x-torchlight-code torchlight-id='id2' language='php'>
66
echo "hello world 2";
7-
</x-torchlight-code>
7+
</x-torchlight-code></pre>

0 commit comments

Comments
 (0)