From 336ddf1a558b466324d4ba43c8129f952fe8a043 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Sat, 29 Jun 2013 11:42:07 +0100 Subject: [PATCH 1/2] FormFields now allow setting of extra CSSClasses en masse Each CSS class passed in to `addExtraClass` or `removeExtraClass` will be set as their own key in the `extraClasses` array Also make `Form` consistent with `FormField` --- forms/Form.php | 20 +++++++++++--------- forms/FormField.php | 20 ++++++++++++++------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/forms/Form.php b/forms/Form.php index 4dd0f84f2..4947408e0 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -1473,14 +1473,12 @@ class Form extends RequestHandler { * names delimited by a single space. */ public function addExtraClass($class) { - $classes = explode(' ', $class); - - foreach($classes as $class) { - $value = trim($class); - - $this->extraClasses[] = $value; + //split at white space + $classes = preg_split('/\s+/', $class); + foreach ($classes as $class) { + //add classes one by one + $this->extraClasses[$class] = $class; } - return $this; } @@ -1491,8 +1489,12 @@ class Form extends RequestHandler { * @param string $class */ public function removeExtraClass($class) { - $classes = explode(' ', $class); - $this->extraClasses = array_diff($this->extraClasses, $classes); + //split at white space + $classes = preg_split('/\s+/', $class); + foreach ($classes as $class) { + //unset one by one + unset($this->extraClasses[$class]); + } return $this; } diff --git a/forms/FormField.php b/forms/FormField.php index 87b73e63c..ae522c22f 100644 --- a/forms/FormField.php +++ b/forms/FormField.php @@ -293,24 +293,32 @@ class FormField extends RequestHandler { } /** - * Add a CSS-class to the formfield-container. + * Add one or more CSS-classes to the formfield-container. * * @param $class String */ public function addExtraClass($class) { - $this->extraClasses[$class] = $class; + //split at white space to extract all the classes + $classes = preg_split('/\s+/', $class); + foreach ($classes as $class) { + //add each class one by one + $this->extraClasses[$class] = $class; + } return $this; } /** - * Remove a CSS-class from the formfield-container. + * Remove one or more CSS-classes from the formfield-container. * * @param $class String */ public function removeExtraClass($class) { - $pos = array_search($class, $this->extraClasses); - if($pos !== false) unset($this->extraClasses[$pos]); - + //split at white space to extract all the classes + $classes = preg_split('/\s+/', $class); + foreach ($classes as $class) { + //unset each class one by one + unset($this->extraClasses[$class]); + } return $this; } From 9a40b1649673353bed7510efac6a1e464ff325a7 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Sat, 29 Jun 2013 12:07:59 +0100 Subject: [PATCH 2/2] Adding tests to `FormField` and `Form` for extra classes Added tests that were missing from `Form` and ones for my new logic --- tests/forms/FormFieldTest.php | 39 ++++++++++++++++++++++++ tests/forms/FormTest.php | 57 ++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/tests/forms/FormFieldTest.php b/tests/forms/FormFieldTest.php index c7c23645d..be5d4588a 100644 --- a/tests/forms/FormFieldTest.php +++ b/tests/forms/FormFieldTest.php @@ -21,6 +21,45 @@ class FormFieldTest extends SapphireTest { $this->assertStringEndsWith('class2', $field->extraClass()); } + public function testAddManyExtraClasses() { + $field = new FormField('MyField'); + //test we can split by a range of spaces and tabs + $field->addExtraClass('class1 class2 class3 class4 class5'); + $this->assertStringEndsWith( + 'class1 class2 class3 class4 class5', + $field->extraClass() + ); + //test that duplicate classes don't get added + $field->addExtraClass('class1 class2'); + $this->assertStringEndsWith( + 'class1 class2 class3 class4 class5', + $field->extraClass() + ); + } + + public function testRemoveManyExtraClasses() { + $field = new FormField('MyField'); + $field->addExtraClass('class1 class2 class3 class4 class5'); + //test we can remove a single class we just added + $field->removeExtraClass('class3'); + $this->assertStringEndsWith( + 'class1 class2 class4 class5', + $field->extraClass() + ); + //check we can remove many classes at once + $field->removeExtraClass('class1 class5'); + $this->assertStringEndsWith( + 'class2 class4', + $field->extraClass() + ); + //check that removing a dud class is fine + $field->removeExtraClass('dudClass'); + $this->assertStringEndsWith( + 'class2 class4', + $field->extraClass() + ); + } + public function testAttributes() { $field = new FormField('MyField'); $field->setAttribute('foo', 'bar'); diff --git a/tests/forms/FormTest.php b/tests/forms/FormTest.php index 36573da5f..0a7fb807c 100644 --- a/tests/forms/FormTest.php +++ b/tests/forms/FormTest.php @@ -384,6 +384,61 @@ class FormTest extends FunctionalTest { $this->assertEquals('application/x-www-form-urlencoded', $form->getEncType()); } + public function testAddExtraClass() { + $form = $this->getStubForm(); + $form->addExtraClass('class1'); + $form->addExtraClass('class2'); + $this->assertStringEndsWith('class1 class2', $form->extraClass()); + } + + public function testRemoveExtraClass() { + $form = $this->getStubForm(); + $form->addExtraClass('class1'); + $form->addExtraClass('class2'); + $this->assertStringEndsWith('class1 class2', $form->extraClass()); + $form->removeExtraClass('class1'); + $this->assertStringEndsWith('class2', $form->extraClass()); + } + + public function testAddManyExtraClasses() { + $form = $this->getStubForm(); + //test we can split by a range of spaces and tabs + $form->addExtraClass('class1 class2 class3 class4 class5'); + $this->assertStringEndsWith( + 'class1 class2 class3 class4 class5', + $form->extraClass() + ); + //test that duplicate classes don't get added + $form->addExtraClass('class1 class2'); + $this->assertStringEndsWith( + 'class1 class2 class3 class4 class5', + $form->extraClass() + ); + } + + public function testRemoveManyExtraClasses() { + $form = $this->getStubForm(); + $form->addExtraClass('class1 class2 class3 class4 class5'); + //test we can remove a single class we just added + $form->removeExtraClass('class3'); + $this->assertStringEndsWith( + 'class1 class2 class4 class5', + $form->extraClass() + ); + //check we can remove many classes at once + $form->removeExtraClass('class1 class5'); + $this->assertStringEndsWith( + 'class2 class4', + $form->extraClass() + ); + //check that removing a dud class is fine + $form->removeExtraClass('dudClass'); + $this->assertStringEndsWith( + 'class2 class4', + $form->extraClass() + ); + } + public function testAttributes() { $form = $this->getStubForm(); @@ -579,4 +634,4 @@ class FormTest_ControllerWithStrictPostCheck extends Controller implements TestO $form->sessionMessage('Test save was successful', 'good'); return $this->redirectBack(); } -} \ No newline at end of file +}