Skip to content

Commit 02c204d

Browse files
committed
PB-370: Introduce upgrade mechanism for Page Builder content
1 parent d72a6ac commit 02c204d

File tree

5 files changed

+175
-13
lines changed

5 files changed

+175
-13
lines changed

app/code/Magento/PageBuilder/Model/Dom/Adapter/ElementInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ public function querySelector(string $selector): ElementInterface;
6969
* @return HtmlCollectionInterface
7070
*/
7171
public function querySelectorAll(string $selector): HtmlCollectionInterface;
72+
73+
/**
74+
* Removes the specified style property from the style attribute.
75+
*
76+
* @param string $styleProperty
77+
*/
78+
public function removeStyle(string $styleProperty): string;
79+
80+
/**
81+
* Adds the specified property & value to the style attribute.
82+
*
83+
* @param string $styleProperty
84+
* @param string $value
85+
* @return string
86+
*/
87+
public function addStyle(string $styleProperty, string $value): string;
7288
}

app/code/Magento/PageBuilder/Model/Dom/Element.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ public function querySelectorAll(string $selector): HtmlCollectionInterface
111111
[ 'collection' => $this->element->querySelectorAll($selector) ]
112112
);
113113
}
114+
115+
/**
116+
* @inheritDoc
117+
*/
118+
public function removeStyle(string $styleProperty): string
119+
{
120+
$style = $this->getAttribute('style');
121+
$this->setAttribute('style', preg_replace("/${styleProperty}:(.*?);/", '', $style ?? ''));
122+
return $this->getAttribute('style') ?? '';
123+
}
124+
125+
/**
126+
* @inheritDoc
127+
*/
128+
public function addStyle(string $styleProperty, string $value): string
129+
{
130+
$this->removeStyle($styleProperty);
131+
$this->setAttribute('style', "${styleProperty}: $value; " . $this->getAttribute('style') ?? '');
132+
return $this->getAttribute('style') ?? '';
133+
}
114134
}

app/code/Magento/PageBuilder/Setup/Converters/FixFullWidthRowPadding.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,31 @@ public function convert($value)
3131
);
3232
// remove padding from main row element
3333
$fullWidthRows = $document->querySelectorAll("div[data-content-type='row'][data-appearance='full-width']");
34-
/** @var ElementInterface $row */
34+
/**
35+
* @var ElementInterface $row
36+
*/
3537
foreach ($fullWidthRows as $row) {
3638
$style = $row->getAttribute("style");
3739
preg_match("/padding:(.*?);/", $style, $results);
38-
$padding = isset($results[0]) ? $results[0] : '';
40+
$padding = isset($results[1]) ? trim($results[1]) : '';
3941
// remove padding from main row element
40-
$row->setAttribute("style", preg_replace("/padding:(.*?);/", "", $style));
42+
$row->removeStyle("padding");
4143
// add padding to inner row element
42-
/** @var ElementInterface $innerDiv */
4344
$innerDiv = $row->querySelector(".row-full-width-inner");
44-
$innerDiv->setAttribute("style", $padding . $innerDiv->getAttribute("style") ?: '');
45+
$innerDiv->addStyle("padding", $padding);
4546
}
46-
//strip the added html content
47-
preg_match('/<body>(.*)<\/body>/', $document->getContents(), $matches);
47+
return $this->stripHtmlWrapperTags($document->getContents());
48+
}
49+
50+
/**
51+
* Strips the HTML doctype, body, etc. tags that are automatically wrapped around the content.
52+
*
53+
* @param string $content
54+
* @return string
55+
*/
56+
private function stripHtmlWrapperTags(string $content): string
57+
{
58+
preg_match('/<body>(.*)<\/body>/', $content, $matches);
4859
return isset($matches[1]) ? $matches[1] : '';
4960
}
5061
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Model\Dom;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\PageBuilder\Model\Dom\Adapter\HtmlDocumentInterface;
12+
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
13+
use Magento\PageBuilder\Model\Dom\HtmlDocument;
14+
use PHPUnit\Framework\TestCase;
15+
16+
17+
class ElementTest extends TestCase
18+
{
19+
/**
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$this->objectManager = Bootstrap::getObjectManager();
30+
}
31+
32+
/**
33+
* Tests the removeStyle function
34+
*
35+
* @dataProvider removeStylesDataProvider
36+
* @param string $elementData
37+
* @param string $styleProperty
38+
* @param string $expectedResult
39+
*/
40+
public function testRemoveStyle(string $elementData, string $styleProperty, string $expectedResult)
41+
{
42+
$document = $this->objectManager->create(
43+
HtmlDocumentInterface::class,
44+
[ 'document' => $elementData ]
45+
);
46+
/** @var ElementInterface $element */
47+
$element = $document->querySelector('div');
48+
$this->assertEquals($expectedResult, $element->removeStyle($styleProperty));
49+
}
50+
51+
public function removeStylesDataProvider()
52+
{
53+
return [
54+
[
55+
'<div data-element="inner" style="border-style: none; border-width: 1px; border-radius: 0px; margin: 0px 0px 10px; padding: 10px;">',
56+
'margin',
57+
'border-style: none; border-width: 1px; border-radius: 0px; padding: 10px;',
58+
],
59+
[
60+
'<div data-element="inner" style="border-style: none; border-width: 1px; border-radius: 0px; padding: 10px;">',
61+
'margin',
62+
'border-style: none; border-width: 1px; border-radius: 0px; padding: 10px;',
63+
],
64+
[
65+
'<div data-element="inner">',
66+
'margin',
67+
'',
68+
],
69+
];
70+
}
71+
72+
/**
73+
* Tests the addStyle function
74+
*
75+
* @dataProvider addStyleDataProvider
76+
* @param string $elementData
77+
* @param string $styleProperty
78+
* @param string $styleValue
79+
* @param string $expectedResult
80+
*/
81+
public function testAddStyle(string $elementData, string $styleProperty, string $styleValue, string $expectedResult)
82+
{
83+
$document = $this->objectManager->create(
84+
HtmlDocumentInterface::class,
85+
[ 'document' => $elementData ]
86+
);
87+
/** @var ElementInterface $element */
88+
$element = $document->querySelector('div');
89+
$this->assertEquals($expectedResult, $element->addStyle($styleProperty, $styleValue));
90+
}
91+
92+
public function addStyleDataProvider()
93+
{
94+
return [
95+
[
96+
'<div data-element="inner" style="border-style: none; border-width: 1px; border-radius: 0px; padding: 10px;">',
97+
'margin',
98+
'10px',
99+
'margin: 10px; border-style: none; border-width: 1px; border-radius: 0px; padding: 10px;',
100+
],
101+
[
102+
'<div data-element="inner" style="border-style: none; border-width: 1px; border-radius: 0px; margin: 0px 0px 10px; padding: 10px;">',
103+
'margin',
104+
'10px',
105+
'margin: 10px; border-style: none; border-width: 1px; border-radius: 0px; padding: 10px;',
106+
],
107+
[
108+
'<div data-element="inner">',
109+
'margin',
110+
'10px',
111+
'margin: 10px; ',
112+
],
113+
];
114+
}
115+
}

0 commit comments

Comments
 (0)