Update BlocksWidthPreprocessor to calculate with borders

[MAILPOET-5821]
This commit is contained in:
Jan Lysý
2024-02-19 13:53:14 +01:00
committed by Jan Lysý
parent 53a192b3d8
commit 2636bc1055
2 changed files with 94 additions and 0 deletions

View File

@@ -16,6 +16,9 @@ class BlocksWidthPreprocessor implements Preprocessor {
if ($alignment !== 'full') { if ($alignment !== 'full') {
$layoutWidth -= $this->parseNumberFromStringWithPixels($layoutStyles['padding']['left'] ?? '0px'); $layoutWidth -= $this->parseNumberFromStringWithPixels($layoutStyles['padding']['left'] ?? '0px');
$layoutWidth -= $this->parseNumberFromStringWithPixels($layoutStyles['padding']['right'] ?? '0px'); $layoutWidth -= $this->parseNumberFromStringWithPixels($layoutStyles['padding']['right'] ?? '0px');
$borderWidth = $block['attrs']['style']['border']['width'] ?? '0px';
$layoutWidth -= $this->parseNumberFromStringWithPixels($block['attrs']['style']['border']['left']['width'] ?? $borderWidth);
$layoutWidth -= $this->parseNumberFromStringWithPixels($block['attrs']['style']['border']['right']['width'] ?? $borderWidth);
} }
$widthInput = $block['attrs']['width'] ?? '100%'; $widthInput = $block['attrs']['width'] ?? '100%';
@@ -75,6 +78,9 @@ class BlocksWidthPreprocessor implements Preprocessor {
$definedColumnWidth += $this->parseNumberFromStringWithPixels($column['attrs']['style']['spacing']['padding']['left'] ?? '0px'); $definedColumnWidth += $this->parseNumberFromStringWithPixels($column['attrs']['style']['spacing']['padding']['left'] ?? '0px');
$definedColumnWidth += $this->parseNumberFromStringWithPixels($column['attrs']['style']['spacing']['padding']['right'] ?? '0px'); $definedColumnWidth += $this->parseNumberFromStringWithPixels($column['attrs']['style']['spacing']['padding']['right'] ?? '0px');
} }
$borderWidth = $column['attrs']['style']['border']['width'] ?? '0px';
$definedColumnWidth += $this->parseNumberFromStringWithPixels($column['attrs']['style']['border']['left']['width'] ?? $borderWidth);
$definedColumnWidth += $this->parseNumberFromStringWithPixels($column['attrs']['style']['border']['right']['width'] ?? $borderWidth);
} }
if ($columnsCount - $columnsCountWithDefinedWidth > 0) { if ($columnsCount - $columnsCountWithDefinedWidth > 0) {

View File

@@ -362,4 +362,92 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
verify($result[0]['innerBlocks'][0]['email_attrs']['width'])->equals('140px'); verify($result[0]['innerBlocks'][0]['email_attrs']['width'])->equals('140px');
verify($result[0]['innerBlocks'][1]['email_attrs']['width'])->equals('500px'); verify($result[0]['innerBlocks'][1]['email_attrs']['width'])->equals('500px');
} }
public function testItCalculatesWidthForColumnWithBorder(): void {
$blocks = [[
'blockName' => 'core/columns',
'attrs' => [
'style' => [
'border' => [
'width' => '10px',
],
'spacing' => [
'padding' => [
'left' => '25px',
'right' => '15px',
],
],
],
],
'innerBlocks' => [
[
'blockName' => 'core/column',
'attrs' => [
'width' => '140px',
'style' => [
'border' => [
'left' => [
'width' => '5px',
],
'right' => [
'width' => '5px',
],
],
'spacing' => [
'padding' => [
'left' => '25px',
'right' => '15px',
],
],
],
],
'innerBlocks' => [],
],
[
'blockName' => 'core/column',
'attrs' => [],
'innerBlocks' => [
[
'blockName' => 'core/image',
'attrs' => [],
'innerBlocks' => [],
],
],
],
[
'blockName' => 'core/column',
'attrs' => [
'style' => [
'border' => [
'width' => '15px',
],
'spacing' => [
'padding' => [
'left' => '20px',
'right' => '20px',
],
],
],
],
'innerBlocks' => [
[
'blockName' => 'core/image',
'attrs' => [],
'innerBlocks' => [],
],
],
],
],
]];
$result = $this->preprocessor->preprocess($blocks, ['width' => '660px', 'padding' => ['left' => '10px', 'right' => '10px']]);
verify($result[0]['innerBlocks'])->arrayCount(3);
verify($result[0]['innerBlocks'][0]['email_attrs']['width'])->equals('140px');
verify($result[0]['innerBlocks'][1]['email_attrs']['width'])->equals('180px');
verify($result[0]['innerBlocks'][2]['email_attrs']['width'])->equals('220px');
$imageBlock = $result[0]['innerBlocks'][1]['innerBlocks'][0];
verify($imageBlock['email_attrs']['width'])->equals('180px');
$imageBlock = $result[0]['innerBlocks'][2]['innerBlocks'][0];
verify($imageBlock['email_attrs']['width'])->equals('180px');
}
} }