From a453c16b3290eabdf35778c54498101c737544e1 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Tue, 18 Aug 2015 20:36:08 +0200
Subject: [PATCH] Form: more flexible label handling #1312

You now can add labels that don't wrap around inputs, but you have to
ensure IDs are properly assigned yourself.

The Label class has been renamed to LabelElement to reflect the naming
scheme of the other elements.
---
 inc/Form/Form.php                        | 35 ++++++++++++++++++++++++
 inc/Form/InputElement.php                |  8 +++---
 inc/Form/{Label.php => LabelElement.php} |  6 ++--
 3 files changed, 42 insertions(+), 7 deletions(-)
 rename inc/Form/{Label.php => LabelElement.php} (74%)

diff --git a/inc/Form/Form.php b/inc/Form/Form.php
index 738f2bcf8..73b5b9cb3 100644
--- a/inc/Form/Form.php
+++ b/inc/Form/Form.php
@@ -257,6 +257,41 @@ class Form extends Element {
         return $this->addElement(new ButtonElement($name, $html), $pos);
     }
 
+    /**
+     * Adds a label referencing another input element, escapes the label for you
+     *
+     * @param $label
+     * @param string $for
+     * @param int $pos
+     * @return Element
+     */
+    public function addLabel($label, $for='', $pos = -1) {
+        return $this->addLabelHTML(hsc($label), $for, $pos);
+    }
+
+    /**
+     * Adds a label referencing another input element, allows HTML for content
+     *
+     * @param string $content
+     * @param string|Element $for
+     * @param int $pos
+     * @return Element
+     */
+    public function addLabelHTML($content, $for='', $pos = -1) {
+        $element = new LabelElement(hsc($content));
+
+        if(is_a($for, '\dokuwiki\Form\Element')) {
+            /** @var Element $for */
+            $for = $for->id();
+        }
+        $for = (string) $for;
+        if($for !== '') {
+            $element->attr('for', $for);
+        }
+
+        return $this->addElement($element, $pos);
+    }
+
     /**
      * Add fixed HTML to the form
      *
diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php
index 693eeffc5..694dd0848 100644
--- a/inc/Form/InputElement.php
+++ b/inc/Form/InputElement.php
@@ -12,7 +12,7 @@ namespace dokuwiki\Form;
  */
 class InputElement extends Element {
     /**
-     * @var Label
+     * @var LabelElement
      */
     protected $label = null;
 
@@ -24,19 +24,19 @@ class InputElement extends Element {
     /**
      * @param string $type The type of this element
      * @param string $name The name of this form element
-     * @param string $label The label text for this element
+     * @param string $label The label text for this element (will be autoescaped)
      */
     public function __construct($type, $name, $label = '') {
         parent::__construct($type, array('name' => $name));
         $this->attr('name', $name);
         $this->attr('type', $type);
-        if($label) $this->label = new Label($label);
+        if($label) $this->label = new LabelElement($label);
     }
 
     /**
      * Returns the label element if there's one set
      *
-     * @return Label|null
+     * @return LabelElement|null
      */
     public function getLabel() {
         return $this->label;
diff --git a/inc/Form/Label.php b/inc/Form/LabelElement.php
similarity index 74%
rename from inc/Form/Label.php
rename to inc/Form/LabelElement.php
index 8dcd7cd5f..9c8d54277 100644
--- a/inc/Form/Label.php
+++ b/inc/Form/LabelElement.php
@@ -5,12 +5,12 @@ namespace dokuwiki\Form;
  * Class Label
  * @package dokuwiki\Form
  */
-class Label extends ValueElement {
+class LabelElement extends ValueElement {
 
     /**
      * Creates a new Label
      *
-     * @param string $label
+     * @param string $label This is is raw HTML and will not be escaped
      */
     public function __construct($label) {
         parent::__construct('label', $label);
@@ -22,6 +22,6 @@ class Label extends ValueElement {
      * @return string
      */
     public function toHTML() {
-        return '<label ' . buildAttributes($this->attrs()) . '>' . hsc($this->val()) . '</label>';
+        return '<label ' . buildAttributes($this->attrs()) . '>' . $this->val() . '</label>';
     }
 }
-- 
GitLab