Render headings
[MAILPOET-2613]
This commit is contained in:
@@ -3,7 +3,71 @@
|
||||
namespace MailPoet\Form\Block;
|
||||
|
||||
class Heading {
|
||||
public function render(array $block, array $formSettings): string {
|
||||
return '<h2></h2>';
|
||||
public function render(array $block): string {
|
||||
$content = ($block['params']['content'] ?? '');
|
||||
return $this->wrapContent($content, $block);
|
||||
}
|
||||
|
||||
private function wrapContent(string $content, array $block): string {
|
||||
$tag = $this->renderTag($block);
|
||||
$attributes = $this->renderAttributes($block);
|
||||
$openTag = $this->getOpenTag($tag, $attributes);
|
||||
return $openTag
|
||||
. $content
|
||||
. "</$tag>";
|
||||
}
|
||||
|
||||
private function renderTag(array $block): string {
|
||||
$tag = 'h2';
|
||||
if (isset($block['params']['level'])) {
|
||||
$tag = 'h' . $block['params']['level'];
|
||||
}
|
||||
return $tag;
|
||||
}
|
||||
|
||||
private function renderAttributes(array $block): array {
|
||||
$result = [];
|
||||
if (isset($block['params']['class_name'])) {
|
||||
$result[] = $this->renderClass($block);
|
||||
}
|
||||
if (isset($block['params']['anchor'])) {
|
||||
$result[] = $this->renderAnchor($block);
|
||||
}
|
||||
if (isset($block['params']['align']) || isset($block['params']['text_color'])) {
|
||||
$result[] = $this->renderStyle($block);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getOpenTag(string $tag, array $attributes): string {
|
||||
if (empty($attributes)) {
|
||||
return "<$tag>";
|
||||
}
|
||||
return "<$tag " . join(' ', $attributes) . ">";
|
||||
}
|
||||
|
||||
private function renderClass(array $block): string {
|
||||
return 'class="'
|
||||
. $block['params']['class_name']
|
||||
. '"';
|
||||
}
|
||||
|
||||
private function renderAnchor(array $block): string {
|
||||
return 'id="'
|
||||
. $block['params']['anchor']
|
||||
. '"';
|
||||
}
|
||||
|
||||
private function renderStyle(array $block): string {
|
||||
$styles = [];
|
||||
if (isset($block['params']['align'])) {
|
||||
$styles[] = 'text-align: ' . $block['params']['align'];
|
||||
}
|
||||
if (isset($block['params']['text_color'])) {
|
||||
$styles[] = 'color: ' . $block['params']['text_color'];
|
||||
}
|
||||
return 'style="'
|
||||
. join('; ', $styles)
|
||||
. '"';
|
||||
}
|
||||
}
|
||||
|
@@ -94,7 +94,7 @@ class BlocksRenderer {
|
||||
break;
|
||||
|
||||
case 'heading':
|
||||
$html .= $this->heading->render($block, $formSettings);
|
||||
$html .= $this->heading->render($block);
|
||||
break;
|
||||
|
||||
case 'divider':
|
||||
|
@@ -14,7 +14,70 @@ class HeadingTest extends \MailPoetUnitTest {
|
||||
}
|
||||
|
||||
public function testItShouldRenderHeading() {
|
||||
$html = $this->heading->render([], []);
|
||||
$html = $this->heading->render([]);
|
||||
expect($html)->startsWith('<h2');
|
||||
}
|
||||
|
||||
public function testItShouldRenderContent() {
|
||||
$html = $this->heading->render([
|
||||
'params' => [
|
||||
'content' => 'Header',
|
||||
],
|
||||
]);
|
||||
expect($html)->equals('<h2>Header</h2>');
|
||||
}
|
||||
|
||||
public function testItShouldRenderLevel() {
|
||||
$html = $this->heading->render([
|
||||
'params' => [
|
||||
'content' => 'Header',
|
||||
'level' => 1,
|
||||
],
|
||||
]);
|
||||
expect($html)->equals('<h1>Header</h1>');
|
||||
}
|
||||
|
||||
public function testItShouldRenderClass() {
|
||||
$html = $this->heading->render([
|
||||
'params' => [
|
||||
'content' => 'Header',
|
||||
'level' => 1,
|
||||
'class_name' => 'class1 class2',
|
||||
],
|
||||
]);
|
||||
expect($html)->equals('<h1 class="class1 class2">Header</h1>');
|
||||
}
|
||||
|
||||
public function testItShouldRenderAnchor() {
|
||||
$html = $this->heading->render([
|
||||
'params' => [
|
||||
'content' => 'Header',
|
||||
'level' => 1,
|
||||
'anchor' => 'anchor',
|
||||
],
|
||||
]);
|
||||
expect($html)->equals('<h1 id="anchor">Header</h1>');
|
||||
}
|
||||
|
||||
public function testItShouldRenderAlign() {
|
||||
$html = $this->heading->render([
|
||||
'params' => [
|
||||
'content' => 'Header',
|
||||
'level' => 1,
|
||||
'align' => 'right',
|
||||
],
|
||||
]);
|
||||
expect($html)->equals('<h1 style="text-align: right">Header</h1>');
|
||||
}
|
||||
|
||||
public function testItShouldRenderTextColour() {
|
||||
$html = $this->heading->render([
|
||||
'params' => [
|
||||
'content' => 'Header',
|
||||
'level' => 1,
|
||||
'text_color' => 'red',
|
||||
],
|
||||
]);
|
||||
expect($html)->equals('<h1 style="color: red">Header</h1>');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user