From af7afbe91849eedad7f25989d6d4baac605cb86e Mon Sep 17 00:00:00 2001 From: Firesphere Date: Mon, 25 Nov 2013 20:49:35 +0100 Subject: [PATCH] IMPROVEMENT/FEATURE Forcefully write javascripts to the end of the HTML if wanted. It's defaulted to false. But when set to true, the JS is written to the end of the HTML, even though there are earlier scripts. This results in faster page-loading if the JS isn't needed earlier-on. --- tests/forms/RequirementsTest.php | 36 ++++++++++++++++++++------------ view/Requirements.php | 4 ++-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/tests/forms/RequirementsTest.php b/tests/forms/RequirementsTest.php index 79b0522f8..604bb06a6 100644 --- a/tests/forms/RequirementsTest.php +++ b/tests/forms/RequirementsTest.php @@ -357,36 +357,46 @@ class RequirementsTest extends SapphireTest { // Test matching with HTML5
tags as well $template = '
My header

Body

'; - // Test if the script is before the first \n
My header

Body

"; + $JsInBody = "
My header

Body

"; + $JsAtEnd = "
My header

Body

"; + + + // Test if the script is before the head tag, not before the body. + // Expected: $JsInHead $backend->set_write_js_to_body(false); $backend->set_force_js_to_bottom(false); $html = $backend->includeInHTML(false, $template); - $this->assertNotContains('', $html); - $this->assertNotContains('assertContains('assertNotEquals($JsInBody, $html); + $this->assertNotEquals($JsAtEnd, $html); + $this->assertEquals($JsInHead, $html); // Test if the script is before the first ', $html); - $this->assertContains('assertNotEquals($JsAtEnd, $html); + $this->assertEquals($JsInBody, $html); // Test if the script is placed just before the closing bodytag, with write-to-body false. + // Expected: $JsAtEnd $backend->set_write_js_to_body(false); $backend->set_force_js_to_bottom(true); $html = $backend->includeInHTML(false, $template); - $this->assertNotContains('assertNotContains('assertContains('', $html); - + $this->assertNotEquals($JsInHead, $html); + $this->assertNotEquals($JsInBody, $html); + $this->assertEquals($JsAtEnd, $html); + // Test if the script is placed just before the closing bodytag, with write-to-body true. + // Expected: $JsAtEnd $backend->set_write_js_to_body(true); $backend->set_force_js_to_bottom(true); $html = $backend->includeInHTML(false, $template); - $this->assertNotContains('assertNotContains('assertContains('', $html); + $this->assertNotEquals($JsInHead, $html); + $this->assertNotEquals($JsInBody, $html); + $this->assertEquals($JsAtEnd, $html); } public function testSuffix() { diff --git a/view/Requirements.php b/view/Requirements.php index a495bbfbf..490bec454 100644 --- a/view/Requirements.php +++ b/view/Requirements.php @@ -733,8 +733,8 @@ 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 - // tags just before those. Otherwise, we put it at the bottom. + // We forcefully put it at the bottom instead of before + // the first script-tag occurence $content = preg_replace("/(<\/body[^>]*>)/i", $jsRequirements . "\\1", $content); // Put CSS at the bottom of the head