Skip to content

Commit

Permalink
Merge pull request #20 from StyleShit/fix/includes-with-data
Browse files Browse the repository at this point in the history
fix: includes with data sometimes throw exceptions
  • Loading branch information
freekmurze committed Oct 17, 2023
2 parents b2a9f03 + 8789e5d commit 9850bfe
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Commenters/BladeCommenters/IncludeCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function pattern(): string
$excludesRegex = '(?!'.implode('|', $excludes).')';
}

return "/@include\([\'\"]{$excludesRegex}(.*?)['\"]\)/";
return "/@include\((?<q>[\'\"]){$excludesRegex}(.*?)\k<q>(,(.*))?\)/s";
}

public function replacement(): string
{
return '<!-- Start include: $1 -->$0<!-- End include: $1 -->';
return '<!-- Start include: $2 -->$0<!-- End include: $2 -->';
}
}
8 changes: 4 additions & 4 deletions src/Commenters/BladeCommenters/IncludeIfCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class IncludeIfCommenter implements BladeCommenter
{
public function pattern(): string
{
$excludeRegex = '';
$excludesRegex = '';
$excludes = config('blade-comments.excludes.includes', []);

if (count($excludes)) {
$excludeRegex = '(?!'.implode('|', $excludes).')';
$excludesRegex = '(?!'.implode('|', $excludes).')';
}

return "/@includeIf\([\'\"]{$excludeRegex}(.*?)['\"]\)/";
return "/@includeIf\((?<q>[\'\"]){$excludesRegex}(.*?)\k<q>(,(.*))?\)/s";
}

public function replacement(): string
{
return '<!-- Start includeIf: $1 -->$0<!-- End includeIf: $1 -->';
return '<!-- Start includeIf: $2 -->$0<!-- End includeIf: $2 -->';
}
}
12 changes: 12 additions & 0 deletions tests/PrecompilerTests/BladeIncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
assertMatchesHtmlSnapshot($renderedView);
});

it('will add comments for includes with data', function () {
$renderedView = view('includes.include.page-with-data')->render();

assertMatchesHtmlSnapshot($renderedView);
});

it('should filter excluded includes', function () {
config(['blade-comments.excludes.includes' => ['includes.exclude']]);

Expand All @@ -22,6 +28,12 @@
assertMatchesHtmlSnapshot($renderedView);
});

it('will add comments for includeIf with data', function () {
$renderedView = view('includes.includeIf.page-with-data')->render();

assertMatchesHtmlSnapshot($renderedView);
});

it('should filter excluded includeIfs', function () {
config(['blade-comments.excludes.includes' => ['includes.exclude']]);
$renderedView = view('includes.includeIf.excludes')->render();
Expand Down
10 changes: 10 additions & 0 deletions tests/TestSupport/views/includes/include/page-with-data.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is the start of the page

@include('includes.include.include', [
'number' => 1,
'string' => 'string',
'array' => ['one', 'two', 'three'],
'function_call' => __('test'),
])

This is the end of the page
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is the start of the page

@includeIf('includes.includeIf.includeIf', [
'number' => 1,
'string' => 'string',
'array' => ['one', 'two', 'three'],
'function_call' => __('test'),
])

This is the end of the page
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html><body><p>This is the start of the page

<!-- Start includeIf: includes.includeIf.includeIf -->This is the includeIf
<!-- End includeIf: includes.includeIf.includeIf -->

This is the end of the page
</p></body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html><body><p>This is the start of the page

<!-- Start include: includes.include.include -->This is the include
<!-- End include: includes.include.include -->

This is the end of the page
</p></body></html>

0 comments on commit 9850bfe

Please sign in to comment.