Skip to content

Commit

Permalink
Merge pull request #27802 from browner12/json-blade
Browse files Browse the repository at this point in the history
[5.8] Update blade JSON directive
  • Loading branch information
taylorotwell committed Mar 6, 2019
2 parents 6142f81 + e5fca63 commit 8c96448
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
15 changes: 1 addition & 14 deletions src/Illuminate/View/Compilers/Concerns/CompilesJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

trait CompilesJson
{
/**
* The default JSON encoding options.
*
* @var int
*/
private $encodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;

/**
* Compile the JSON statement into valid PHP.
*
Expand All @@ -19,12 +12,6 @@ trait CompilesJson
*/
protected function compileJson($expression)
{
$parts = explode(',', $this->stripParentheses($expression));

$options = isset($parts[1]) ? trim($parts[1]) : $this->encodingOptions;

$depth = isset($parts[2]) ? trim($parts[2]) : 512;

return "<?php echo json_encode($parts[0], $options, $depth) ?>";
return '<?php echo json_encode('.$this->stripParentheses($expression).') ?>';
}
}
32 changes: 28 additions & 4 deletions tests/View/Blade/BladeJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,42 @@

class BladeJsonTest extends AbstractBladeTestCase
{
public function testStatementIsCompiledWithSafeDefaultEncodingOptions()
public function testBasicStatementIsCompiled()
{
$string = 'var foo = @json($var);';
$expected = 'var foo = <?php echo json_encode($var, 15, 512) ?>;';
$expected = 'var foo = <?php echo json_encode($var) ?>;';

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testEncodingOptionsCanBeOverwritten()
public function testOptionsArgumentCanBeSpecified()
{
$string = 'var foo = @json($var, JSON_HEX_TAG);';
$expected = 'var foo = <?php echo json_encode($var, JSON_HEX_TAG, 512) ?>;';
$expected = 'var foo = <?php echo json_encode($var, JSON_HEX_TAG) ?>;';

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testDepthArgumentCanBeSpecified()
{
$string = 'var foo = @json($var, 0, 128);';
$expected = 'var foo = <?php echo json_encode($var, 0, 128) ?>;';

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testValueArgumentCanContainCommas()
{
$string = 'var foo = @json(["value1", "value2", "value3"], JSON_HEX_TAG, 512);';
$expected = 'var foo = <?php echo json_encode(["value1", "value2", "value3"], JSON_HEX_TAG, 512) ?>;';

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testValueArgumentCanContainFunctions()
{
$string = 'var foo = @json(array_merge(["value1", "value2"], ["value3", "value4"]), JSON_HEX_TAG, 512);';
$expected = 'var foo = <?php echo json_encode(array_merge(["value1", "value2"], ["value3", "value4"]), JSON_HEX_TAG, 512) ?>;';

$this->assertEquals($expected, $this->compiler->compileString($string));
}
Expand Down

0 comments on commit 8c96448

Please sign in to comment.