Skip to content
Snippets Groups Projects
Commit 8f0df229 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

Form: added Button element #1312

parent 08099e4f
No related branches found
No related tags found
No related merge requests found
<?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);
}
}
<?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>';
}
}
...@@ -233,6 +233,30 @@ class Form extends Element { ...@@ -233,6 +233,30 @@ class Form extends Element {
return $this->addElement(new TextareaElement($name, $label), $pos); return $this->addElement(new TextareaElement($name, $label), $pos);
} }
/**
* 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 * Add fixed HTML to the form
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment