Improve legend tag rendering in forms

The legend html was not escaped and was ignoring block settings coming
from the form editor.
This commit adds a new helper function for rendering legend that respects
applies block settings and also escapes the output.
[MAILPOET-4471]
This commit is contained in:
Rostislav Wolny
2022-07-19 10:38:07 +02:00
committed by Veljko V
parent 243e1bdac8
commit 7f525068b3
5 changed files with 53 additions and 4 deletions

View File

@@ -153,6 +153,37 @@ class BlockRendererHelper {
return $html;
}
public function renderLegend(array $block, array $formSettings): string {
$html = '';
if (
isset($block['params']['hide_label'])
&& $block['params']['hide_label']
) {
return $html;
}
if (
isset($block['params']['label'])
&& strlen(trim($block['params']['label'])) > 0
) {
// Use _label suffix for backward compatibility
$labelClass = 'class="mailpoet_' . $block['type'] . '_label" ';
$html .= '<legend '
. $labelClass
. $this->renderFontStyle($formSettings, $block['styles'] ?? [])
. '>';
$html .= htmlspecialchars($block['params']['label']);
if (isset($block['params']['required']) && $block['params']['required']) {
$html .= ' <span class="mailpoet_required">*</span>';
}
$html .= '</legend>';
}
return $html;
}
public function renderFontStyle(array $formSettings, array $styles = []) {
$rules = [];
if (isset($formSettings['fontSize'])) {

View File

@@ -33,7 +33,7 @@ class Checkbox {
$fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId);
$html .= '<fieldset>';
$html .= '<legend>' . $block['params']['label'] . '</legend>';
$html .= $this->rendererHelper->renderLegend($block, $formSettings);
$options = (!empty($block['params']['values'])
? $block['params']['values']

View File

@@ -33,7 +33,7 @@ class Radio {
$fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId);
$html .= '<fieldset>';
$html .= '<legend>' . $block['params']['label'] . '</legend>';
$html .= $this->rendererHelper->renderLegend($block, $formSettings);
$options = (!empty($block['params']['values'])
? $block['params']['values']

View File

@@ -40,8 +40,7 @@ class Segment {
// Add fieldset around the checkboxes
$html .= '<fieldset>';
$html .= '<legend>' . $block['params']['label'] . '</legend>';
$html .= $this->rendererHelper->renderLegend($block, $formSettings);
$options = (!empty($block['params']['values'])
? $block['params']['values']

View File

@@ -60,6 +60,25 @@ class BlockRendererHelperTest extends \MailPoetUnitTest {
expect($label)->equals('');
}
public function testItShouldRenderLegend() {
$block = $this->block;
$label = $this->rendererHelper->renderLegend($block, []);
expect($label)->regExp('#<legend.*class="mailpoet_text_label".*>Input label</legend>#m');
$block['styles'] = ['bold' => '1'];
$label = $this->rendererHelper->renderLegend($block, []);
expect($label)->equals('<legend class="mailpoet_text_label" style="font-weight: bold;">Input label</legend>');
$block['params']['required'] = '1';
$block['styles'] = [];
$label = $this->rendererHelper->renderLegend($block, []);
expect($label)->equals('<legend class="mailpoet_text_label" >Input label <span class="mailpoet_required">*</span></legend>');
$block['params']['hide_label'] = '1';
$label = $this->rendererHelper->renderLegend($block, []);
expect($label)->equals('');
}
public function testItShouldRenderPlaceholder() {
$block = $this->block;
$placeholder = $this->rendererHelper->renderInputPlaceholder($block);