public function setup() { $this->html = <<<HTML_CONTENT <form action="" method="POST"> \t<div id="success" data-form-success="true"> \t\tThe form was successfully submitted. \t</div> \t<input id="foo" type="text" name="foo" value="" class="formcontrol"> \t<div id="bar_error" data-show-on-error="bar" class="error" id="errors"> \t\t<input id="required_bar" type="text" name="bar" data-validators="validate_required" class="formcontrol required"> \t\t<div id="bar_error_list" data-control-errors="bar"></div> \t</div> \t<button id="submit" class="formcontrol">Submit</button> </form> HTML_CONTENT; $this->doc = HTMLDoc::create($this->html); }
/** * Create a form with controls from HTML * @param string $name Name of the form * @param string $html HTML of a form * @return FormUI The form created from the supplied HTML */ public static function from_html($name, $html) { $dom = new HTMLDoc($html); $form = new FormUI($name); // Set the form to render using the output of the HTMLDoc $form->settings['norender'] = true; $form->settings['content'] = function () use($dom) { return $dom->get(); }; $form->dom = $dom; // This form's id is an MD5 hash of the original HTML $form->settings['control_id'] = md5($html); // Add the _form_id and WSSE elements to the form // Note that these additional fields must be XML-safe, or they won't be added $dom->find_one('form')->append_html(Utils::setup_wsse()); $dom->find_one('form')->append_html('<input type="hidden" name="_form_id" value="' . $form->control_id() . '" />'); // Add synthetic controls for any found inputs foreach ($dom->find('input') as $input) { /** @var FormControl $control */ $form->append($control = FormControlDom::create($input->name)->set_node($input)); if ($input->data_validators) { foreach (explode(' ', $input->data_validators) as $validator) { $control->add_validator($validator); } } } foreach ($dom->find('textarea') as $input) { /** @var FormControl $control */ $form->append($control = FormControlDom::create($input->name)->set_node($input)); if (!empty($input->data_validators)) { foreach (explode(' ', $input->data_validators) as $validator) { $control->add_validator($validator); } } } return $form; }
function test_encode_textarea() { $form = new FormUI('encode'); $encoded = $form->append('textarea', 'encoded')->set_value('<b>strong</b>')->add_class('target'); $html = $form->get(); $doc = new HTMLDoc($html); $target = $doc->find_one('.target'); $this->assert_equal($target->inner_html(), '<b>strong</b>', 'The output value was not encoded.', htmlspecialchars($target->inner_html())); $encoded->set_value('<b>strong</b>'); $html = $form->get(); $doc = new HTMLDoc($html); $target = $doc->find_one('.target'); $this->assert_equal($target->inner_html(), '&lt;b&gt;strong&lt;/b&gt;', 'The output value was not encoded.', htmlspecialchars($target->inner_html())); }