diff --git a/tests/forms/RequirementsTest.php b/tests/forms/RequirementsTest.php index 3c1e4b4ad..70a30c8f3 100644 --- a/tests/forms/RequirementsTest.php +++ b/tests/forms/RequirementsTest.php @@ -350,6 +350,28 @@ class RequirementsTest extends SapphireTest { $this->assertContains('', $html); } + public function testIncludedJsIsNotCommentedOut() { + $template = ''; + $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 = '' + . '

more content

'; + $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('' + . '

more content

', $html); + } + public function testForceJsToBottom() { $backend = new Requirements_Backend(); $backend->javascript('http://www.mydomain.com/test.js'); diff --git a/view/Requirements.php b/view/Requirements.php index 8848c6087..0574a50c2 100644 --- a/view/Requirements.php +++ b/view/Requirements.php @@ -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, '))/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);