Skip to content

Commit

Permalink
ensure end of body placement
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 18, 2024
1 parent 2fb3468 commit edf49c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/InjectAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ protected function inject(string $html, string $assets): string

if ($html->test('/<\s*\/\s*head\s*>/i')) {
return $html
->replaceMatches('/(<\s*\\s*head\s*>)/i', '$1' . $assets)
->replaceMatches('/(<\s*\/\s*head\s*>)/i', $assets . '$1')
->toString();
}

return $html
->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1' . $assets)
->replaceMatches('/(<\s*\/\s*html\s*>)/i', $assets . '$1')
->toString();
}
}
16 changes: 16 additions & 0 deletions tests/Feature/InjectsAssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
->assertSee($expected, false);
});

it('injects assets into the end of the html body when no head tag is present', function () {
Route::get('test-inject-in-response', fn () => '<html><p>Hello World</p></html>');

$expected = <<< 'HTML'
<html><p>Hello World</p>
<!--[TEST_PACKAGE]-->
TEST_PACKAGE_ASSETS_INJECTED
<!--[ENDTEST_PACKAGE]-->
</html>
HTML;

$this->get('test-inject-in-response')
->assertOk()
->assertSee($expected, false);
});

it('doesnt inject assets into responses without a closing html tag', function () {
Route::get('test-inject-in-response', fn () => 'OK');

Expand Down

0 comments on commit edf49c2

Please sign in to comment.