From 4aa50534d566722df81bfac8efd2c3db7ed92596 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 7 Jan 2016 10:27:46 +1300 Subject: [PATCH] FIX: Fixes needed to adapt to whitespace changes. API: Whitespace trimmed from custom form field templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The introduction of trailing newlines on all template files introduced some changes that needed to be made. Notably, whitespace has been trimmed from rendered form field templates. This is the minimal impact to SilverStripe developers, as it preserves the behaviour of the default field types, but I’ve still noted as a change. --- docs/en/04_Changelogs/3.4.0.md | 16 ++++++++++++++++ forms/FormField.php | 11 ++++++++++- tests/view/SSViewerTest.php | 6 +++--- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 docs/en/04_Changelogs/3.4.0.md diff --git a/docs/en/04_Changelogs/3.4.0.md b/docs/en/04_Changelogs/3.4.0.md new file mode 100644 index 000000000..8d5926046 --- /dev/null +++ b/docs/en/04_Changelogs/3.4.0.md @@ -0,0 +1,16 @@ +# 3.4.0 (unreleased) + +## Upgrading notes + +Upgrading from 3.3 to 3.4 shouldn't require any code changes. + +Note that if you are upgrading from 3.1, you should review the [3.2 upgrade notes](3.2.0). + +### Other changes to be aware of + +These changes are unlikely to affect your upgrade, but we've noted them just in case you had unusual project code that +was affected by these: + + * When FormFields are rendered, leading & trailing whitespace is now stripped. The resulting HTML for form fields is + the same for the default fields, but if you have a custom form field that is relying on trailing whitespace being + outputted. diff --git a/forms/FormField.php b/forms/FormField.php index eab6bcca0..722a3fcaf 100644 --- a/forms/FormField.php +++ b/forms/FormField.php @@ -839,7 +839,16 @@ class FormField extends RequestHandler { $this->extend('onBeforeRender', $this); - return $context->renderWith($this->getTemplates()); + $result = $context->renderWith($this->getTemplates()); + + // Trim whitespace from the result, so that trailing newlines are supressed. Works for strings and HTMLText values + if(is_string($result)) { + $result = trim($result); + } else if($result instanceof DBField) { + $result->setValue(trim($result->getValue())); + } + + return $result; } /** diff --git a/tests/view/SSViewerTest.php b/tests/view/SSViewerTest.php index 45cbf4e69..bb4cd7c44 100644 --- a/tests/view/SSViewerTest.php +++ b/tests/view/SSViewerTest.php @@ -134,7 +134,7 @@ class SSViewerTest extends SapphireTest { public function render($templateString, $data = null, $cacheTemplate = false) { $t = SSViewer::fromString($templateString, $cacheTemplate); if(!$data) $data = new SSViewerTestFixture(); - return $t->process($data); + return trim(''.$t->process($data)); } public function testRequirements() { @@ -1095,10 +1095,10 @@ after') $this->useTestTheme(dirname(__FILE__), 'layouttest', function() use ($self) { $template = new SSViewer(array('Page')); - $self->assertEquals('Foo', $template->process(new ArrayData(array()))); + $self->assertEquals("Foo\n\n", $template->process(new ArrayData(array()))); $template = new SSViewer(array('Shortcodes', 'Page')); - $self->assertEquals('[file_link]', $template->process(new ArrayData(array()))); + $self->assertEquals("[file_link]\n\n", $template->process(new ArrayData(array()))); }); }