-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[12.x] Add @maybe
Blade directive for conditional HTML attributes
#57235
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
NickSdot
wants to merge
3
commits into
laravel:12.x
Choose a base branch
from
NickSdot:12.x
base: 12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\View\Blade; | ||
|
||
use Illuminate\Contracts\View\ViewCompilationException; | ||
|
||
class BladeMaybeStatementsTest extends AbstractBladeTestCase | ||
{ | ||
public function testMaybeWithMissingParameter() | ||
{ | ||
$this->expectException(ViewCompilationException::class); | ||
$this->expectExceptionMessage('The @maybe directive requires exactly 2 parameters.'); | ||
|
||
$string = "@maybe('title')"; | ||
$this->compiler->compileString($string); | ||
} | ||
|
||
public function testMaybeWithSimpleVariable() | ||
{ | ||
$string = '<a @maybe(\'title\', $title)>Link</a>'; | ||
$expected = '<a <?php if($title !== \'\' && $title !== null && trim(is_bool($title) ? ($title ? \'true\' : \'false\') : $title) !== \'\') echo \'title\' . \'="\' . e(is_bool($title) ? ($title ? \'true\' : \'false\') : $title) . \'"\'; ?>>Link</a>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testMaybeWithObjectProperty() | ||
{ | ||
$string = '<a @maybe(\'title\', $link->title)>Link</a>'; | ||
$expected = '<a <?php if($link->title !== \'\' && $link->title !== null && trim(is_bool($link->title) ? ($link->title ? \'true\' : \'false\') : $link->title) !== \'\') echo \'title\' . \'="\' . e(is_bool($link->title) ? ($link->title ? \'true\' : \'false\') : $link->title) . \'"\'; ?>>Link</a>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testMaybeWithArrayAccess() | ||
{ | ||
$string = '<a @maybe(\'title\', $data[\'title\'])>Link</a>'; | ||
$expected = '<a <?php if($data[\'title\'] !== \'\' && $data[\'title\'] !== null && trim(is_bool($data[\'title\']) ? ($data[\'title\'] ? \'true\' : \'false\') : $data[\'title\']) !== \'\') echo \'title\' . \'="\' . e(is_bool($data[\'title\']) ? ($data[\'title\'] ? \'true\' : \'false\') : $data[\'title\']) . \'"\'; ?>>Link</a>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testMaybeWithMultipleAttributes() | ||
{ | ||
$string = '<img @maybe(\'alt\', $alt) @maybe(\'data-src\', $src)/>'; | ||
$expected = '<img <?php if($alt !== \'\' && $alt !== null && trim(is_bool($alt) ? ($alt ? \'true\' : \'false\') : $alt) !== \'\') echo \'alt\' . \'="\' . e(is_bool($alt) ? ($alt ? \'true\' : \'false\') : $alt) . \'"\'; ?> <?php if($src !== \'\' && $src !== null && trim(is_bool($src) ? ($src ? \'true\' : \'false\') : $src) !== \'\') echo \'data-src\' . \'="\' . e(is_bool($src) ? ($src ? \'true\' : \'false\') : $src) . \'"\'; ?>/>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testMaybeWithSpacesAroundParameters() | ||
{ | ||
$string = '<a @maybe( \'title\' , $title )>Link</a>'; | ||
$expected = '<a <?php if($title !== \'\' && $title !== null && trim(is_bool($title) ? ($title ? \'true\' : \'false\') : $title) !== \'\') echo \'title\' . \'="\' . e(is_bool($title) ? ($title ? \'true\' : \'false\') : $title) . \'"\'; ?>>Link</a>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testMaybeWithNonEmptyString() | ||
{ | ||
$this->assertSame( | ||
"<?php if(\$title !== '' && \$title !== null && trim(is_bool(\$title) ? (\$title ? 'true' : 'false') : \$title) !== '') echo 'title' . '=\"' . e(is_bool(\$title) ? (\$title ? 'true' : 'false') : \$title) . '\"'; ?>", | ||
$this->compiler->compileString("@maybe('title', \$title)") | ||
); | ||
} | ||
|
||
public function testMaybeWithEmptyString() | ||
{ | ||
$string = "@maybe('title', \$title)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('', $this->evaluateBlade($compiled, ['title' => ''])); | ||
} | ||
|
||
public function testMaybeWithNull() | ||
{ | ||
$string = "@maybe('title', \$title)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('', $this->evaluateBlade($compiled, ['title' => null])); | ||
} | ||
|
||
public function testMaybeWithZero() | ||
{ | ||
$string = "@maybe('data-count', \$count)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('data-count="0"', $this->evaluateBlade($compiled, ['count' => 0])); | ||
} | ||
|
||
public function testMaybeWithFalse() | ||
{ | ||
$string = "@maybe('data-active', \$active)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('data-active="false"', $this->evaluateBlade($compiled, ['active' => false])); | ||
} | ||
|
||
public function testMaybeWithTrue() | ||
{ | ||
$string = "@maybe('data-active', \$active)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('data-active="true"', $this->evaluateBlade($compiled, ['active' => true])); | ||
} | ||
|
||
public function testMaybeWithValidString() | ||
{ | ||
$string = "@maybe('title', \$title)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('title="You can just do things"', $this->evaluateBlade($compiled, ['title' => 'You can just do things'])); | ||
} | ||
|
||
public function testMaybeEscapesHtmlEntities() | ||
{ | ||
$string = "@maybe('title', \$title)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('title="<script>alert('xss')</script>"', | ||
$this->evaluateBlade($compiled, ['title' => "<script>alert('xss')</script>"])); | ||
} | ||
|
||
public function testMaybeWithWhitespaceOnlyString() | ||
{ | ||
$string = "@maybe('title', \$title)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
// Whitespace-only strings are considered empty. | ||
$this->assertSame('', $this->evaluateBlade($compiled, ['title' => ' '])); | ||
} | ||
|
||
public function testMaybeWithNumericString() | ||
{ | ||
$string = "@maybe('data-id', \$id)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('data-id="123"', $this->evaluateBlade($compiled, ['id' => '123'])); | ||
} | ||
|
||
public function testMaybeWithInt() | ||
{ | ||
$string = "@maybe('data-id', \$id)"; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('data-id="123"', $this->evaluateBlade($compiled, ['id' => 123])); | ||
} | ||
|
||
public function testMaybeInHtmlContext() | ||
{ | ||
$string = '<a href="#" @maybe(\'title\', $title)>Link</a>'; | ||
$compiled = $this->compiler->compileString($string); | ||
|
||
$this->assertSame('<a href="#" title="click">Link</a>', | ||
$this->evaluateBlade($compiled, ['title' => 'click'])); | ||
|
||
$this->assertSame('<a href="#" >Link</a>', | ||
$this->evaluateBlade($compiled, ['title' => ''])); | ||
} | ||
|
||
protected function evaluateBlade(string $compiled, array $data = []): string | ||
{ | ||
extract($data); | ||
ob_start(); | ||
eval('?>'.$compiled); | ||
|
||
return ob_get_clean(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.