Fix #3356 js not properly included

This commit is contained in:
Juri Demeschew 2014-08-30 20:32:28 +02:00
parent f0ca37fdea
commit 3eabd7d41a
2 changed files with 34 additions and 2 deletions

View File

@ -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');

View File

@ -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);