summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2015-05-11 20:31:16 +0200
committerAndreas Gohr <andi@splitbrain.org>2015-05-11 20:31:16 +0200
commit1f5d8b65a983fe0914971ee0bb4e5e58cbf8c8a7 (patch)
tree4413a5f46730fb951305a9dd214c696f13acf406
parent370c645068595c6750cee2b38eb96aeaac3a9dbb (diff)
downloadrpg-1f5d8b65a983fe0914971ee0bb4e5e58cbf8c8a7.tar.gz
rpg-1f5d8b65a983fe0914971ee0bb4e5e58cbf8c8a7.tar.bz2
balance fieldsets
-rw-r--r--_test/tests/inc/form/form.test.php87
-rw-r--r--inc/Form/FieldsetCloseElement.php46
-rw-r--r--inc/Form/Form.php39
-rw-r--r--inc/Form/TagCloseElement.php46
4 files changed, 175 insertions, 43 deletions
diff --git a/_test/tests/inc/form/form.test.php b/_test/tests/inc/form/form.test.php
index cdf3e5a3a..3ae832b2c 100644
--- a/_test/tests/inc/form/form.test.php
+++ b/_test/tests/inc/form/form.test.php
@@ -2,6 +2,25 @@
use dokuwiki\Form;
+/**
+ * makes form internals accessible for testing
+ */
+class TestForm extends Form\Form {
+ /**
+ * @return array list of element types
+ */
+ function getElementTypeList() {
+ $list = array();
+ foreach($this->elements as $element) $list[] = $element->getType();
+ return $list;
+ }
+
+ public function balanceFieldsets() {
+ parent::balanceFieldsets();
+ }
+
+}
+
class form_form_test extends DokuWikiTest {
/**
@@ -25,4 +44,72 @@ class form_form_test extends DokuWikiTest {
$this->assertTrue($pq->find('input[name=sectok]')->length == 1);
}
+
+ function test_fieldsetbalance() {
+ $form = new TestForm();
+ $form->addFieldsetOpen();
+ $form->addHTML('ignored');
+ $form->addFieldsetClose();
+ $form->balanceFieldsets();
+
+ $this->assertEquals(
+ array(
+ 'fieldsetopen',
+ 'html',
+ 'fieldsetclose'
+ ),
+ $form->getElementTypeList()
+ );
+
+ $form = new TestForm();
+ $form->addHTML('ignored');
+ $form->addFieldsetClose();
+ $form->balanceFieldsets();
+
+ $this->assertEquals(
+ array(
+ 'fieldsetopen',
+ 'html',
+ 'fieldsetclose'
+ ),
+ $form->getElementTypeList()
+ );
+
+
+ $form = new TestForm();
+ $form->addFieldsetOpen();
+ $form->addHTML('ignored');
+ $form->balanceFieldsets();
+
+ $this->assertEquals(
+ array(
+ 'fieldsetopen',
+ 'html',
+ 'fieldsetclose'
+ ),
+ $form->getElementTypeList()
+ );
+
+ $form = new TestForm();
+ $form->addHTML('ignored');
+ $form->addFieldsetClose();
+ $form->addHTML('ignored');
+ $form->addFieldsetOpen();
+ $form->addHTML('ignored');
+ $form->balanceFieldsets();
+
+ $this->assertEquals(
+ array(
+ 'fieldsetopen',
+ 'html',
+ 'fieldsetclose',
+ 'html',
+ 'fieldsetopen',
+ 'html',
+ 'fieldsetclose'
+ ),
+ $form->getElementTypeList()
+ );
+ }
+
}
diff --git a/inc/Form/FieldsetCloseElement.php b/inc/Form/FieldsetCloseElement.php
index 0de84e251..8f26717aa 100644
--- a/inc/Form/FieldsetCloseElement.php
+++ b/inc/Form/FieldsetCloseElement.php
@@ -14,51 +14,17 @@ class FieldsetCloseElement extends TagCloseElement {
* @param array $attributes
*/
public function __construct($attributes = array()) {
- parent::__construct('tagclose', $attributes);
+ parent::__construct('', $attributes);
+ $this->type = 'fieldsetclose';
}
- /**
- * do not call this
- *
- * @param $class
- * @return void
- * @throws \BadMethodCallException
- */
- public function addClass($class) {
- throw new \BadMethodCallException('You can\t add classes to closing tag');
- }
-
- /**
- * do not call this
- *
- * @param $id
- * @return void
- * @throws \BadMethodCallException
- */
- public function id($id = null) {
- throw new \BadMethodCallException('You can\t add ID to closing tag');
- }
-
- /**
- * do not call this
- *
- * @param $name
- * @param $value
- * @return void
- * @throws \BadMethodCallException
- */
- public function attr($name, $value = null) {
- throw new \BadMethodCallException('You can\t add attributes to closing tag');
- }
/**
- * do not call this
+ * The HTML representation of this element
*
- * @param $attributes
- * @return void
- * @throws \BadMethodCallException
+ * @return string
*/
- public function attrs($attributes = null) {
- throw new \BadMethodCallException('You can\t add attributes to closing tag');
+ public function toHTML() {
+ return '</fieldset>';
}
}
diff --git a/inc/Form/Form.php b/inc/Form/Form.php
index 420399fb1..30e16615c 100644
--- a/inc/Form/Form.php
+++ b/inc/Form/Form.php
@@ -194,7 +194,6 @@ class Form extends Element {
return $this->addElement(new TagCloseElement($tag), $pos);
}
-
/**
* Open a Fieldset
*
@@ -202,7 +201,7 @@ class Form extends Element {
* @param int $pos
* @return FieldsetOpenElement
*/
- public function addFieldsetOpen($legend='', $pos = -1) {
+ public function addFieldsetOpen($legend = '', $pos = -1) {
return $this->addElement(new FieldsetOpenElement($legend), $pos);
}
@@ -218,8 +217,42 @@ class Form extends Element {
#endregion
+ /**
+ * Adjust the elements so that fieldset open and closes are matching
+ */
protected function balanceFieldsets() {
- //todo implement!
+ $lastclose = 0;
+ $isopen = false;
+ $len = count($this->elements);
+
+ for($pos = 0; $pos < $len; $pos++) {
+ $type = $this->elements[$pos]->getType();
+ if($type == 'fieldsetopen') {
+ if($isopen) {
+ //close previous feldset
+ $this->addFieldsetClose($pos);
+ $lastclose = $pos + 1;
+ $pos++;
+ $len++;
+ }
+ $isopen = true;
+ } else if($type == 'fieldsetclose') {
+ if(!$isopen) {
+ // make sure there was a fieldsetopen
+ // either right after the last close or at the begining
+ $this->addFieldsetOpen('', $lastclose);
+ $len++;
+ $pos++;
+ }
+ $lastclose = $pos;
+ $isopen = false;
+ }
+ }
+
+ // close open fieldset at the end
+ if($isopen) {
+ $this->addFieldsetClose();
+ }
}
/**
diff --git a/inc/Form/TagCloseElement.php b/inc/Form/TagCloseElement.php
index 896945b97..dc0264c21 100644
--- a/inc/Form/TagCloseElement.php
+++ b/inc/Form/TagCloseElement.php
@@ -20,6 +20,51 @@ class TagCloseElement extends ValueElement {
}
/**
+ * do not call this
+ *
+ * @param $class
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function addClass($class) {
+ throw new \BadMethodCallException('You can\t add classes to closing tag');
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $id
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function id($id = null) {
+ throw new \BadMethodCallException('You can\t add ID to closing tag');
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $name
+ * @param $value
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function attr($name, $value = null) {
+ throw new \BadMethodCallException('You can\t add attributes to closing tag');
+ }
+
+ /**
+ * do not call this
+ *
+ * @param $attributes
+ * @return void
+ * @throws \BadMethodCallException
+ */
+ public function attrs($attributes = null) {
+ throw new \BadMethodCallException('You can\t add attributes to closing tag');
+ }
+
+ /**
* The HTML representation of this element
*
* @return string
@@ -27,4 +72,5 @@ class TagCloseElement extends ValueElement {
public function toHTML() {
return '</'.$this->val().'>';
}
+
}