public function includeInHTML($templateFile, $content) { if ($this->Eventful()) { $this->Eventful()->fire('assets:beforeProcessHtml', $templateFile, $content); } $this->assets(); $body = parent::includeInHTML($templateFile, $content); $this->attachCustomScriptsToResponse(); if ($this->Eventful()) { $this->Eventful()->fire('assets:afterProcessHtml', $body, $templateFile, $content); } return $body; }
/** * Default includeInHTML strips out CSS if the file doesn't exist. We need to add it back in if it's a redirected rewrite * @see sapphire/core/Requirements_Backend#includeInHTML($templateFile, $content) */ function includeInHTML($templateFile, $content) { $content = parent::includeInHTML($templateFile, $content); $requirements = ''; foreach (array_diff_key($this->css, $this->blocked) as $file => $params) { $path = self::path_for_file($file); if (!$path && ($path = self::path_for_file('assets/' . basename($file)))) { $media = isset($params['media']) && !empty($params['media']) ? " media=\"{$params['media']}\"" : ""; $requirements .= "<link rel=\"stylesheet\" type=\"text/css\"{$media} href=\"{$file}\" />\n"; } } return preg_replace("/(<\\/head[^>]*>)/i", $requirements . "\\1", $content); }
public function testExternalUrls() { $backend = new Requirements_Backend(); $backend->setCombinedFilesEnabled(true); $backend->javascript('http://www.mydomain.com/test.js'); $backend->javascript('https://www.mysecuredomain.com/test.js'); $backend->javascript('//scheme-relative.example.com/test.js'); $backend->css('http://www.mydomain.com/test.css'); $backend->css('https://www.mysecuredomain.com/test.css'); $backend->css('//scheme-relative.example.com/test.css'); $html = $backend->includeInHTML(false, self::$html_template); $this->assertTrue(strpos($html, 'http://www.mydomain.com/test.js') !== false, 'Load external javascript URL'); $this->assertTrue(strpos($html, 'https://www.mysecuredomain.com/test.js') !== false, 'Load external secure javascript URL'); $this->assertTrue(strpos($html, '//scheme-relative.example.com/test.js') !== false, 'Load external scheme-relative javascript URL'); $this->assertTrue(strpos($html, 'http://www.mydomain.com/test.css') !== false, 'Load external CSS URL'); $this->assertTrue(strpos($html, 'https://www.mysecuredomain.com/test.css') !== false, 'Load external secure CSS URL'); $this->assertTrue(strpos($html, '//scheme-relative.example.com/test.css') !== false, 'Load scheme-relative CSS URL'); }
public function includeInHTML($templateFile, $content) { $this->compile(); return parent::includeInHTML($templateFile, $content); }
function testArgsInUrls() { $backend = new Requirements_Backend(); $backend->set_combined_files_enabled(true); $backend->javascript(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.js?test=1&test=2&test=3'); $backend->css(SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.css?test=1&test=2&test=3'); $backend->delete_combined_files('RequirementsTest_bc.js'); $html = $backend->includeInHTML(false, self::$html_template); /* Javascript has correct path */ $this->assertTrue((bool) preg_match('/src=".*\\/RequirementsTest_a\\.js\\?m=\\d\\d+&test=1&test=2&test=3/', $html), 'javascript has correct path'); /* CSS has correct path */ $this->assertTrue((bool) preg_match('/href=".*\\/RequirementsTest_a\\.css\\?m=\\d\\d+&test=1&test=2&test=3/', $html), 'css has correct path'); }
public function testSuffix() { $template = '<html><head></head><body><header>My header</header><p>Body</p></body></html>'; $basePath = $this->getCurrentRelativePath(); $basePath = 'framework' . substr($basePath, strlen(FRAMEWORK_DIR)); $backend = new Requirements_Backend(); $backend->javascript($basePath . '/RequirementsTest_a.js'); $backend->javascript($basePath . '/RequirementsTest_b.js?foo=bar&bla=blubb'); $backend->css($basePath . '/RequirementsTest_a.css'); $backend->css($basePath . '/RequirementsTest_b.css?foo=bar&bla=blubb'); $backend->set_suffix_requirements(true); $html = $backend->includeInHTML(false, $template); $this->assertRegexp('/RequirementsTest_a\\.js\\?m=[\\d]*"/', $html); $this->assertRegexp('/RequirementsTest_b\\.js\\?m=[\\d]*&foo=bar&bla=blubb"/', $html); $this->assertRegexp('/RequirementsTest_a\\.css\\?m=[\\d]*"/', $html); $this->assertRegexp('/RequirementsTest_b\\.css\\?m=[\\d]*&foo=bar&bla=blubb"/', $html); $backend->set_suffix_requirements(false); $html = $backend->includeInHTML(false, $template); $this->assertNotContains('RequirementsTest_a.js=', $html); $this->assertNotRegexp('/RequirementsTest_a\\.js\\?m=[\\d]*"/', $html); $this->assertNotRegexp('/RequirementsTest_b\\.js\\?m=[\\d]*&foo=bar&bla=blubb"/', $html); $this->assertNotRegexp('/RequirementsTest_a\\.css\\?m=[\\d]*"/', $html); $this->assertNotRegexp('/RequirementsTest_b\\.css\\?m=[\\d]*&foo=bar&bla=blubb"/', $html); }
public function testJsWriteToBody() { $backend = new Requirements_Backend(); $backend->javascript('http://www.mydomain.com/test.js'); // Test matching with HTML5 <header> tags as well $template = '<html><head></head><body><header>My header</header><p>Body</p></body></html>'; $backend->set_write_js_to_body(false); $html = $backend->includeInHTML(false, $template); $this->assertContains('<head><script', $html); $backend->set_write_js_to_body(true); $html = $backend->includeInHTML(false, $template); $this->assertNotContains('<head><script', $html); $this->assertContains('</script></body>', $html); }
/** * Update the given HTML content with the appropriate include tags for the registered * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, * including a <head> tag. The requirements will insert before the closing <head> tag automatically. * * @todo Calculate $prefix properly * * @param string $templateFilePath Absolute path for the *.ss template file * @param string $content HTML content that has already been parsed from the $templateFilePath through {@link SSViewer}. * @return string HTML content thats augumented with the requirements before the closing <head> tag. */ function includeInHTML($templateFile, $content) { if ($this->isBackendController()) { //currently, it's not loading tinymce otherwise return parent::includeInHTML($templateFile, $content); } $hasHead = strpos($content, '</head>') !== false || strpos($content, '</head ') !== false; $hasRequirements = $this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags; if (!$hasHead || !$hasRequirements) { return $content; } $script_template = "<script type=\"text/javascript\">\n//<![CDATA[\n" . "%s" . "\n//]]>\n</script>\n"; $ready_template = "head.ready(function() {\n" . "%s" . "\n});\n"; $readyRequirements = $cleanRequirements = ''; // Include HeadJS, the only script in your head $headerRequirements = "<script type=\"text/javascript\" src=\"" . self::getHeadJsUrl() . "\"></script>\n"; // Combine files - updates $this->javascript and $this->css $this->process_combined_files(); $named_files = self::getNamedFiles(); // add the css requirements, even allow a callback $cssFiles = array_diff_key($this->css, $this->blocked); if (!empty($cssFiles)) { foreach ($cssFiles as $file => $params) { $name = str_replace('-', '', basename($file, '.css')); $path = Convert::raw2xml($this->path_for_file($file)); if ($path) { if (isset($params['media']) && !empty($params['media'])) { $headerRequirements .= "<link rel=\"stylesheet\" type=\"text/css\" media=\"{$params['media']}\" href=\"{$path}\" />\n"; } else { $path = str_replace('&', '&', $path); if ($named_files) { $path = $name . '":"' . $path; } $callback = $this->get_callback($file) ? ", function(){" . $this->get_callback($file) . "}" : ""; $readyRequirements .= "head.load(\"" . $path . "\"{$callback});\n"; } } } } $jsFiles = array_diff_key($this->javascript, $this->blocked); if (!empty($jsFiles)) { foreach ($jsFiles as $file => $dummy) { $name = str_replace('-', '', basename($file, '.js')); $path = Convert::raw2xml($this->path_for_file($file)); $path = str_replace('&', '&', $path); if ($path) { if ($named_files) { $path = $name . '":"' . $path; } $callback = $this->get_callback($file) ? ", function(){" . $this->get_callback($file) . "}" : ""; $readyRequirements .= "head.load(\"" . $path . "\"{$callback});\n"; } } } // @todo: store all css files in arrays with media param as the key, then do a headjs test for each mediaparam // store all "custom" css in the header, because this may have critical styling to fix FOUC issues $customCSS = array_diff_key($this->customCSS, $this->blocked); if (!empty($customCSS)) { foreach ($customCSS as $css) { $headerRequirements .= "<style type=\"text/css\">\n{$css}\n</style>\n"; } } // add all inline javascript *after* including external files which // they might rely on $customJS = array_diff_key($this->customScript, $this->blocked); if (!empty($customJS)) { foreach ($customJS as $script) { if (in_array($script, self::$do_not_wrap)) { $cleanRequirements .= $script; } else { $readyRequirements .= $script; } } } foreach (array_diff_key($this->customHeadTags, $this->blocked) as $customHeadTag) { $headerRequirements .= "{$customHeadTag}\n"; } // put the core js just before the </head> $content = preg_replace("/(<\\/head>)/i", $headerRequirements . "\\1", $content); $readyRequirements = $readyRequirements == "" ? "" : sprintf($ready_template, $readyRequirements); $readyRequirements = sprintf($script_template, $readyRequirements . $cleanRequirements); if ($this->force_js_to_bottom) { // Remove all newlines from code to preserve layout $readyRequirements = preg_replace('/>\\n*/', '>', $readyRequirements); // We put script tags into the body, for performance. // We forcefully put it at the bottom instead of before // the first script-tag occurence $content = preg_replace("/(<\\/body[^>]*>)/i", $readyRequirements . "\\1", $content); } elseif ($this->write_js_to_body) { // Remove all newlines from code to preserve layout $readyRequirements = preg_replace('/>\\n*/', '>', $readyRequirements); // 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. $p2 = stripos($content, '<body'); $p1 = stripos($content, '<script', $p2); // @todo - should we move the inline script tags below everything else? // we could do this with regex, or a while loop... if ($p1 !== false) { $content = substr($content, 0, $p1) . $readyRequirements . substr($content, $p1); } else { $content = preg_replace("/(<\\/body[^>]*>)/i", $readyRequirements . "\\1", $content); } } else { $content = preg_replace("/(<\\/head>)/i", $readyRequirements . "\\1", $content); } return $content; }