summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2015-08-18 20:12:32 +0200
committerAndreas Gohr <andi@splitbrain.org>2015-08-18 20:12:32 +0200
commit8f0df229ed3e82191f118594ea5145c9d5942d7b (patch)
treea867583b058344e1cdbbb5837f23e1e5aff97316
parent08099e4fe1e56308bc42cc639d187863088494bd (diff)
downloadrpg-8f0df229ed3e82191f118594ea5145c9d5942d7b.tar.gz
rpg-8f0df229ed3e82191f118594ea5145c9d5942d7b.tar.bz2
Form: added Button element #1312
-rw-r--r--_test/tests/inc/form/buttonelement.test.php40
-rw-r--r--inc/Form/ButtonElement.php34
-rw-r--r--inc/Form/Form.php24
3 files changed, 98 insertions, 0 deletions
diff --git a/_test/tests/inc/form/buttonelement.test.php b/_test/tests/inc/form/buttonelement.test.php
new file mode 100644
index 000000000..8e1a7e1e2
--- /dev/null
+++ b/_test/tests/inc/form/buttonelement.test.php
@@ -0,0 +1,40 @@
+<?php
+
+use dokuwiki\Form;
+
+class form_buttonelement_test extends DokuWikiTest {
+
+ function test_simple() {
+ $form = new Form\Form();
+ $form->addButton('foo', 'Hello <b>World</b>')->val('bam')->attr('type', 'submit');
+
+ $html = $form->toHTML();
+ $pq = phpQuery::newDocumentXHTML($html);
+
+ $input = $pq->find('button[name=foo]');
+ $this->assertTrue($input->length == 1);
+ $this->assertEquals('bam', $input->val());
+ $this->assertEquals('submit', $input->attr('type'));
+ $this->assertEquals('Hello <b>World</b>', $input->text()); // tags were escaped
+
+ $b = $input->find('b'); // no tags found
+ $this->assertTrue($b->length == 0);
+ }
+
+ function test_html() {
+ $form = new Form\Form();
+ $form->addButtonHTML('foo', 'Hello <b>World</b>')->val('bam')->attr('type', 'submit');
+
+ $html = $form->toHTML();
+ $pq = phpQuery::newDocumentXHTML($html);
+
+ $input = $pq->find('button[name=foo]');
+ $this->assertTrue($input->length == 1);
+ $this->assertEquals('bam', $input->val());
+ $this->assertEquals('submit', $input->attr('type'));
+ $this->assertEquals('Hello World', $input->text()); // tags are stripped here
+
+ $b = $input->find('b'); // tags found
+ $this->assertTrue($b->length == 1);
+ }
+}
diff --git a/inc/Form/ButtonElement.php b/inc/Form/ButtonElement.php
new file mode 100644
index 000000000..77c30ed4f
--- /dev/null
+++ b/inc/Form/ButtonElement.php
@@ -0,0 +1,34 @@
+<?php
+namespace dokuwiki\Form;
+
+/**
+ * Class ButtonElement
+ *
+ * Represents a simple button
+ *
+ * @package dokuwiki\Form
+ */
+class ButtonElement extends Element {
+
+ /** @var string HTML content */
+ protected $content = '';
+
+ /**
+ * @param string $name
+ * @param string $content HTML content of the button. You have to escape it yourself.
+ */
+ function __construct($name, $content = '') {
+ parent::__construct('button', array('name' => $name, 'value' => 1));
+ $this->content = $content;
+ }
+
+ /**
+ * The HTML representation of this element
+ *
+ * @return string
+ */
+ public function toHTML() {
+ return '<button ' . buildAttributes($this->attrs()) . '>'.$this->content.'</button>';
+ }
+
+}
diff --git a/inc/Form/Form.php b/inc/Form/Form.php
index 625557fa1..738f2bcf8 100644
--- a/inc/Form/Form.php
+++ b/inc/Form/Form.php
@@ -234,6 +234,30 @@ class Form extends Element {
}
/**
+ * Adds a simple button, escapes the content for you
+ *
+ * @param string $name
+ * @param string $content
+ * @param int $pos
+ * @return Element
+ */
+ public function addButton($name, $content, $pos = -1) {
+ return $this->addElement(new ButtonElement($name, hsc($content)), $pos);
+ }
+
+ /**
+ * Adds a simple button, allows HTML for content
+ *
+ * @param string $name
+ * @param string $html
+ * @param int $pos
+ * @return Element
+ */
+ public function addButtonHTML($name, $html, $pos = -1) {
+ return $this->addElement(new ButtonElement($name, $html), $pos);
+ }
+
+ /**
* Add fixed HTML to the form
*
* @param $html