Skip to content

Commit

Permalink
Updated handling of `<br> tags in blockquotes
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Mar 14, 2024
1 parent 69430d2 commit e4d7008
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 50 deletions.
4 changes: 2 additions & 2 deletions app/View/Components/Docs/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\View\Components\Docs;

use App\View\Modifications\BladeComponentModifier;
use App\View\Modifications\BlockquoteColorModifier;
use App\View\Modifications\BlockquoteModifier;
use App\View\Modifications\HeaderLinksModifier;
use App\View\Modifications\HTMLCleanseModifier;
use App\View\Modifications\ImageAltModifier;
Expand Down Expand Up @@ -54,7 +54,7 @@ public function toHtml(): string
->send($this->content)
->through([
HTMLCleanseModifier::class, // Стандартизирует HTML
BlockquoteColorModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
BlockquoteModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
RemoveFirstHeaderModifier::class, // Удаляет h1 заголовок
HeaderLinksModifier::class, // Добавляет ссылки для заголовков
ResponsiveTableModifier::class, // Добавляет к таблице класс table-responsive
Expand Down
4 changes: 2 additions & 2 deletions app/View/Components/Posts/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\View\Components\Posts;

use App\View\Modifications\BladeComponentModifier;
use App\View\Modifications\BlockquoteColorModifier;
use App\View\Modifications\BlockquoteModifier;
use App\View\Modifications\HTMLCleanseModifier;
use App\View\Modifications\ImageAltModifier;
use App\View\Modifications\ResponsiveTableModifier;
Expand Down Expand Up @@ -59,7 +59,7 @@ public function toHtml(): string
->send($content)
->through([
HTMLCleanseModifier::class, // Стандартизирует HTML
BlockquoteColorModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
BlockquoteModifier::class, // Применяет цвет к блокам цитат (Например предупреждение)
ResponsiveTableModifier::class, // Добавляет к таблице класс table-responsive
BladeComponentModifier::class, // Применяет компоненты blade
ImageAltModifier::class, // Добавляет alt к картинкам
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Str;
use Symfony\Component\DomCrawler\Crawler;

class BlockquoteColorModifier extends HTMLModifier
class BlockquoteModifier extends HTMLModifier
{
protected array $types = [
// docs-blockquote-note
Expand All @@ -16,6 +16,7 @@ class BlockquoteColorModifier extends HTMLModifier

// docs-blockquote-tip
'{tip}' => 'docs-blockquote-tip', // for 8.x
'{video}' => 'docs-blockquote-tip', // for 8.x
'<strong>Note</strong>' => 'docs-blockquote-tip', // for 10.x
'<strong>Примечание</strong>' => 'docs-blockquote-tip', // for 10.x
'[!NOTE]' => 'docs-blockquote-tip', // for 10.x
Expand Down Expand Up @@ -50,6 +51,18 @@ public function handle(string $content, \Closure $next)
$content = Str::of($content)->replace($tag, $html);
});

$this->crawler($content)
->filter('blockquote p')
->each(function (Crawler $elm) use (&$content) {
$tag = $elm->outerHtml();

$html = Str::of($tag)->between('<p>', '</p>')->replace('<br>', PHP_EOL)->trim()->toString();
$html = nl2br($html);

$content = Str::of($content)->replace($tag, '<p>'.$html.'</p>');
});


return $next($content);
}
}

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"src": "public/img/ui/warning.svg"
},
"resources/css/app.scss": {
"file": "assets/app-BN6-1s-2.css",
"file": "assets/app-BaiyHV97.css",
"src": "resources/css/app.scss",
"isEntry": true
},
Expand Down
2 changes: 1 addition & 1 deletion resources/css/docs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ main div {
text-decoration: underline;
}

:empty {
> :empty:not(br) {
display: none;
}
}
Expand Down
42 changes: 0 additions & 42 deletions tests/Unit/Modifications/BlockquoteColorModifierTest.php

This file was deleted.

76 changes: 76 additions & 0 deletions tests/Unit/Modifications/BlockquoteModifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Tests\Unit\Modifications;

use App\View\Modifications\BlockquoteModifier;
use PHPUnit\Framework\TestCase;

class BlockquoteModifierTest extends TestCase
{
public function testItAddsCssClassesToBlockquoteTags(): void
{
$modifier = new BlockquoteModifier();

$html = '
<blockquote>{note} This is a note.</blockquote>
<blockquote><strong>Warning</strong> This is a warning.</blockquote>
<blockquote>{tip} This is a tip.</blockquote>
<blockquote><strong>Note</strong> This is a note.</blockquote>
<blockquote>[!WARNING] This is a warning.</blockquote>
<blockquote>[!NOTE] This is a tip.</blockquote>
<blockquote><p><br>Более подробную документацию по использованию Vite с Laravel можно найти в нашей <a href="/docs/10.x/vite"> специализированной документации по сборке и компиляции ваших ресурсов.</a>.</p></blockquote>
';

$modifiedHtml = $modifier->handle($html, function ($content) {
return $content;
});

$expectedResult = '
<blockquote class="docs-blockquote-note"><div> This is a note.</div></blockquote>
<blockquote class="docs-blockquote-note"><div> This is a warning.</div></blockquote>
<blockquote class="docs-blockquote-tip"><div> This is a tip.</div></blockquote>
<blockquote class="docs-blockquote-tip"><div> This is a note.</div></blockquote>
<blockquote class="docs-blockquote-note"><div> This is a warning.</div></blockquote>
<blockquote class="docs-blockquote-tip"><div> This is a tip.</div></blockquote>
';

// Удаляем лишние пробелы и символы новой строки для лучшего сравнения
$expectedResult = preg_replace('/\s+/', '', $expectedResult);
$modifiedHtml = preg_replace('/\s+/', '', $modifiedHtml);

$this->assertEquals($expectedResult, $modifiedHtml);
}

public function testItPreservesBrTagsInBlockquotes(): void
{
$modifier = new BlockquoteModifier();

$html = '
<blockquote>
<p>
<br>
Более подробную документацию по использованию Vite с Laravel можно найти в нашей <a href="/docs/10.x/vite">
специализированной документации по сборке и компиляции ваших ресурсов.</a>.
</p>
</blockquote>
';

$modifiedHtml = $modifier->handle($html, function ($content) {
return $content;
});

$expectedResult = '
<blockquote>
<p>
Более подробную документацию по использованию Vite с Laravel можно найти в нашей <a href="/docs/10.x/vite">
<br/>специализированной документации по сборке и компиляции ваших ресурсов.</a>.
</p>
</blockquote>
';

$expectedResult = preg_replace('/\s+/', '', $expectedResult);
$modifiedHtml = preg_replace('/\s+/', '', $modifiedHtml);

$this->assertEquals($expectedResult, $modifiedHtml);
}
}

0 comments on commit e4d7008

Please sign in to comment.