Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created October 30, 2009 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/222830 to your computer and use it in GitHub Desktop.
Save sminnee/222830 to your computer and use it in GitHub Desktop.
Index: sapphire/core/SSViewer.php
===================================================================
--- sapphire/core/SSViewer.php (revision 90546)
+++ sapphire/core/SSViewer.php (working copy)
@@ -517,7 +517,7 @@
$content = ereg_replace('<' . '% +sprintf\(_t\((\'([^\']*)\'|"([^"]*)")(([^)]|\)[^ ]|\) +[^% ])*)\),\<\?= +([^\?]*) +\?\>) +%' . '>', '<?= sprintf(_t(\'\\2\\3\'\\4),\\6) ?>', $content);
// </base> isnt valid html? !?
- $content = ereg_replace('<' . '% +base_tag +%' . '>', '<base href="<?= Director::absoluteBaseURL(); ?>" />', $content);
+ $content = ereg_replace('<' . '% +base_tag +%' . '>', '<?= SSViewer::get_base_tag($val); ?>', $content);
$content = ereg_replace('<' . '% +current_page +%' . '>', '<?= $_SERVER[SCRIPT_URL] ?>', $content);
@@ -568,6 +568,24 @@
public function setTemplateFile($type, $file) {
$this->chosenTemplates[$type] = $file;
}
+
+ /**
+ * Return an appropriate base tag for the given template.
+ * It will be closed on an XHTML document, and unclosed on an HTML document.
+ *
+ * @param $contentGeneratedSoFar The content of the template generated so far; it should contain
+ * the DOCTYPE declaration.
+ */
+ static function get_base_tag($contentGeneratedSoFar) {
+ $base = Director::absoluteBaseURL();
+
+ // Is the document XHTML?
+ if(preg_match('/<!DOCTYPE[^>]+xhtml/i', $contentGeneratedSoFar)) {
+ return "<base href=\"$base\"></base>";
+ } else {
+ return "<base href=\"$base\"><!--[if IE]></base><![endif]-->";
+ }
+ }
}
/**
Index: sapphire/tests/SSViewerTest.php
===================================================================
--- sapphire/tests/SSViewerTest.php (revision 90546)
+++ sapphire/tests/SSViewerTest.php (working copy)
@@ -69,6 +69,33 @@
"Object method calls in dot notation work with two arguments"
);
}
+
+ function testBaseTagGeneration() {
+ // XHTML wil have a closed base tag
+ $tmpl1 = SSViewer::fromString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+ <html>
+ <head><% base_tag %></head>
+ <body><p>test</p><body>
+ </html>');
+ $this->assertRegExp('/<head><base href=".*"><\/base><\/head>/', $tmpl1->process(new ViewableData()));
+
+
+ // HTML4 and 5 will only have it for IE
+ $tmpl2 = SSViewer::fromString('<!DOCTYPE html>
+ <html>
+ <head><% base_tag %></head>
+ <body><p>test</p><body>
+ </html>');
+ $this->assertRegExp('/<head><base href=".*"><!--\[if IE\]><\/base><!\[endif\]--><\/head>/', $tmpl2->process(new ViewableData()));
+
+
+ $tmpl3 = SSViewer::fromString('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+ <html>
+ <head><% base_tag %></head>
+ <body><p>test</p><body>
+ </html>');
+ $this->assertRegExp('/<head><base href=".*"><!--\[if IE\]><\/base><!\[endif\]--><\/head>/', $tmpl3->process(new ViewableData()));
+ }
}
class SSViewerTest_ViewableData extends ViewableData implements TestOnly {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment