diff --git a/forms/Form.php b/forms/Form.php index ae223abb3..87bc02209 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -1475,14 +1475,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; } @@ -1493,8 +1491,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 7608d607e..676f9837d 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; } 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 +}