Merge pull request #1277 from g4b0/contentnegotiator-xhtml-addition-rc2

Added some more XHTML replacement and relative unit test
This commit is contained in:
Ingo Schommer 2013-03-12 08:14:41 -07:00
commit c56f70ba3b
2 changed files with 92 additions and 10 deletions

View File

@ -28,6 +28,8 @@
*/
class ContentNegotiator {
protected static $content_type = '';
protected static $encoding = 'utf-8';
protected static $enabled = false;
@ -122,20 +124,24 @@ class ContentNegotiator {
$negotiator->$chosenFormat( $response );
}
/**
* Only sends the HTTP Content-Type as "application/xhtml+xml"
* if the template starts with the typical "<?xml" Pragma.
* Assumes that a correct doctype is set, and doesn't change or append to it.
* Replaces a few common tags and entities with their XHTML representations (<br>, <img>, &nbsp;).
/**
* Check user defined content type and use it, if it's empty use the strict application/xhtml+xml.
* Replaces a few common tags and entities with their XHTML representations (<br>, <img>, &nbsp;
* <input>, checked, selected).
*
* @param $response SS_HTTPResponse
* @return string
* @todo More flexible tag and entity parsing through regular expressions or tag definition lists
* @todo Search for more xhtml replacement
*/
public function xhtml(SS_HTTPResponse $response) {
$content = $response->getBody();
$response->addHeader("Content-Type", "application/xhtml+xml; charset=" . self::$encoding);
$contentType = Config::inst()->get('ContentNegotiator', 'content_type');
if (empty($contentType)) {
$response->addHeader("Content-Type", "application/xhtml+xml; charset=" . self::$encoding);
} else {
$response->addHeader("Content-Type", $contentType . "; charset=" . self::$encoding);
}
$response->addHeader("Vary" , "Accept");
// Fix base tag
@ -146,19 +152,28 @@ class ContentNegotiator {
$content = str_replace('<br>','<br />', $content);
$content = str_replace('<hr>','<hr />', $content);
$content = preg_replace('#(<img[^>]*[^/>])>#i', '\\1/>', $content);
$content = preg_replace('#(<input[^>]*[^/>])>#i', '\\1/>', $content);
$content = preg_replace("#(\<option[^>]*[\s]+selected)(?!\s*\=)#si", "$1=\"selected\"$2", $content);
$content = preg_replace("#(\<input[^>]*[\s]+checked)(?!\s*\=)#si", "$1=\"checked\"$2", $content);
$response->setBody($content);
}
/*
* Sends HTTP Content-Type as "text/html", and replaces existing doctypes with
* HTML4.01 Strict.
* Check user defined content type and use it, if it's empty use the text/html.
* If find a XML header replaces it and existing doctypes with HTML4.01 Strict.
* Replaces self-closing tags like <img /> with unclosed solitary tags like <img>.
* Replaces all occurrences of "application/xhtml+xml" with "text/html" in the template.
* Removes "xmlns" attributes and any <?xml> Pragmas.
*/
public function html(SS_HTTPResponse $response) {
$response->addHeader("Content-Type", "text/html; charset=" . self::$encoding);
$contentType = Config::inst()->get('ContentNegotiator', 'content_type');
if (empty($contentType)) {
$response->addHeader("Content-Type", "text/html; charset=" . self::$encoding);
} else {
$response->addHeader("Content-Type", $contentType . "; charset=" . self::$encoding);
}
$response->addHeader("Vary", "Accept");
$content = $response->getBody();

View File

@ -0,0 +1,67 @@
<?php
class ContentNegotiatorTest extends SapphireTest {
/**
* Small helper to render templates from strings
* Cloned from SSViewerTest
*/
private function render($templateString, $data = null) {
$t = SSViewer::fromString($templateString);
if(!$data) $data = new SSViewerTestFixture();
return $t->process($data);
}
public function testXhtmltagReplacement() {
$tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
<!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>
<form action="#">
<select>
<option>aa</option>
<option selected = "selected">bb</option>
<option selected="selected">cc</option>
<option class="foo" selected>dd</option>
<option>ee</option>
<option selected value="">ll</option>
</select>
<input type="checkbox">ff
<input type="checkbox" checked = "checked">gg
<input type="checkbox" checked="checked">hh
<input class="bar" type="checkbox" checked>ii
<input type="checkbox" checked class="foo">jj
<input type="submit">
</form>
<body>
</html>';
// Check that the content negotiator converts to the equally legal formats
$negotiator = new ContentNegotiator();
$response = new SS_HTTPResponse($this->render($tmpl1));
$negotiator->xhtml($response);
////////////////////////
// XHTML select options
////////////////////////
$this->assertRegExp('/<option>aa<\/option>/', $response->getBody());
$this->assertRegExp('/<option selected = "selected">bb<\/option>/', $response->getBody());
$this->assertRegExp('/<option selected="selected">cc<\/option>/', $response->getBody());
// Just transform this
$this->assertRegExp('/<option class="foo" selected="selected">dd<\/option>/', $response->getBody());
$this->assertRegExp('/<option selected="selected" value="">ll<\/option>/', $response->getBody());
////////////////////////////////////////////////
// XHTML checkbox options + XHTML input closure
////////////////////////////////////////////////
$this->assertRegExp('/<input type="checkbox"\/>ff/', $response->getBody());
$this->assertRegExp('/<input type="checkbox" checked = "checked"\/>g/', $response->getBody());
$this->assertRegExp('/<input type="checkbox" checked="checked"\/>hh/', $response->getBody());
// Just transform this
$this->assertRegExp('/<input class="bar" type="checkbox" checked="checked"\/>ii/', $response->getBody());
$this->assertRegExp('/<input type="checkbox" checked="checked" class="foo"\/>jj/', $response->getBody());
}
}