Render styles for the form

[MAILPOET-2600]

Signed-off-by: Pavel Dohnal <pavel@mailpoet.com>
This commit is contained in:
Pavel Dohnal
2020-02-26 13:22:29 +01:00
committed by Jack Kitterhing
parent b49ce7d4e4
commit ce127eadc1
2 changed files with 92 additions and 1 deletions

View File

@ -37,7 +37,10 @@ class Renderer {
public function renderHTML(array $form = []): string {
if (isset($form['body']) && !empty($form['body'])) {
return $this->renderBlocks($form['body']);
return $this->wrapForm(
$this->renderBlocks($form['body']),
$form['settings']
);
}
return '';
}
@ -88,4 +91,23 @@ class Renderer {
<input class="mailpoet_recaptcha_field" type="hidden" name="recaptcha">
</div>';
}
private function wrapForm(string $formBody, array $formSettings): string {
$styles = [];
if (isset($formSettings['backgroundColor'])) {
$styles[] = 'background-color: ' . trim($formSettings['backgroundColor']);
}
if (isset($formSettings['fontColor'])) {
$styles[] = 'color: ' . trim($formSettings['fontColor']);
}
if (isset($formSettings['fontSize'])) {
$styles[] = 'font-size: ' . trim($formSettings['fontSize']) . 'px';
}
if (empty($styles)) return $formBody;
return '<div style="' . join(';', $styles) . '">' . $formBody . '</div>';
}
}

View File

@ -97,4 +97,73 @@ class RendererTest extends \MailPoetUnitTest {
$recaptcha = $this->htmlParser->findByXpath($html, "//div[@class='mailpoet_recaptcha']");
expect($recaptcha->length)->equals(0);
}
public function testItShouldRenderBackgroundColour() {
$this->blocksRendererMock
->expects($this->exactly(2))
->method('renderBlock')
->willReturn('<span class="block">Dummy</span>');
$this->settingsMock
->method('get')
->with('captcha.type')
->willReturn(Captcha::TYPE_DISABLED);
$formBody = Fixtures::get('simple_form_body');
$html = $this->renderer->renderHTML([
'body' => $formBody,
'settings' => ['backgroundColor' => 'red'],
]);
$found = $this->htmlParser->findByXpath($html, "//div");
expect($found->length)->equals(1);
$div = $found->item(0);
assert($div instanceof \DOMNode);
$source = $div->attributes->getNamedItem('style');
assert($source instanceof \DOMAttr);
expect($source->value)->equals("background-color: red");
}
public function testItShouldRenderColour() {
$this->blocksRendererMock
->expects($this->exactly(2))
->method('renderBlock')
->willReturn('<span class="block">Dummy</span>');
$this->settingsMock
->method('get')
->with('captcha.type')
->willReturn(Captcha::TYPE_DISABLED);
$formBody = Fixtures::get('simple_form_body');
$html = $this->renderer->renderHTML([
'body' => $formBody,
'settings' => ['fontColor' => 'red'],
]);
$found = $this->htmlParser->findByXpath($html, "//div");
expect($found->length)->equals(1);
$div = $found->item(0);
assert($div instanceof \DOMNode);
$source = $div->attributes->getNamedItem('style');
assert($source instanceof \DOMAttr);
expect($source->value)->equals("color: red");
}
public function testItShouldRenderFontSize() {
$this->blocksRendererMock
->expects($this->exactly(2))
->method('renderBlock')
->willReturn('<span class="block">Dummy</span>');
$this->settingsMock
->method('get')
->with('captcha.type')
->willReturn(Captcha::TYPE_DISABLED);
$formBody = Fixtures::get('simple_form_body');
$html = $this->renderer->renderHTML([
'body' => $formBody,
'settings' => ['fontSize' => '20'],
]);
$found = $this->htmlParser->findByXpath($html, "//div");
expect($found->length)->equals(1);
$div = $found->item(0);
assert($div instanceof \DOMNode);
$source = $div->attributes->getNamedItem('style');
assert($source instanceof \DOMAttr);
expect($source->value)->equals("font-size: 20px");
}
}