mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #3459 from jdemeschew/3356-fix-js-not-properly-included
Fix #3356 js not properly included
This commit is contained in:
commit
f86b0bbca0
@ -350,6 +350,28 @@ class RequirementsTest extends SapphireTest {
|
||||
$this->assertContains('</script></body>', $html);
|
||||
}
|
||||
|
||||
public function testIncludedJsIsNotCommentedOut() {
|
||||
$template = '<html><head></head><body><!--<script>alert("commented out");</script>--></body></html>';
|
||||
$backend = new Requirements_Backend();
|
||||
$backend->javascript($this->getCurrentRelativePath() . '/RequirementsTest_a.js');
|
||||
$html = $backend->includeInHTML(false, $template);
|
||||
//wiping out commented-out html
|
||||
$html = preg_replace('/<!--(.*)-->/Uis', '', $html);
|
||||
$this->assertContains("RequirementsTest_a.js", $html);
|
||||
}
|
||||
|
||||
public function testCommentedOutScriptTagIsIgnored() {
|
||||
$template = '<html><head></head><body><!--<script>alert("commented out");</script>-->'
|
||||
. '<h1>more content</h1></body></html>';
|
||||
$backend = new Requirements_Backend();
|
||||
$backend->set_suffix_requirements(false);
|
||||
$src = $this->getCurrentRelativePath() . '/RequirementsTest_a.js';
|
||||
$backend->javascript($src);
|
||||
$html = $backend->includeInHTML(false, $template);
|
||||
$this->assertEquals('<html><head></head><body><!--<script>alert("commented out");</script>-->'
|
||||
. '<h1>more content</h1><script type="text/javascript" src="/' . $src . '"></script></body></html>', $html);
|
||||
}
|
||||
|
||||
public function testForceJsToBottom() {
|
||||
$backend = new Requirements_Backend();
|
||||
$backend->javascript('http://www.mydomain.com/test.js');
|
||||
|
@ -758,12 +758,22 @@ class Requirements_Backend {
|
||||
$jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements);
|
||||
|
||||
// We put script tags into the body, for performance.
|
||||
// If your template already has script tags in the body, then we put our script
|
||||
// If your template already has script tags in the body, then we try to put our script
|
||||
// tags just before those. Otherwise, we put it at the bottom.
|
||||
$p2 = stripos($content, '<body');
|
||||
$p1 = stripos($content, '<script', $p2);
|
||||
|
||||
$commentTags = array();
|
||||
$canWriteToBody = ($p1 !== false)
|
||||
&&
|
||||
//check that the script tag is not inside a html comment tag
|
||||
!(
|
||||
preg_match('/.*(?|(?<tag><!--)|(?<tag>-->))/U', $content, $commentTags, 0, $p1)
|
||||
&&
|
||||
$commentTags['tag'] == '-->'
|
||||
);
|
||||
|
||||
if($p1 !== false) {
|
||||
if($canWriteToBody) {
|
||||
$content = substr($content,0,$p1) . $jsRequirements . substr($content,$p1);
|
||||
} else {
|
||||
$content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content);
|
||||
|
Loading…
x
Reference in New Issue
Block a user