Skip to content

Commit

Permalink
#558: Content Types Output Style Attribute Removal
Browse files Browse the repository at this point in the history
- Adding Converter to Remove `style` Attribute(s) from Page Builder
  • Loading branch information
bluemwhitew committed Aug 27, 2020
1 parent 91c59fb commit 2f5e019
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types = 1);

namespace Magento\PageBuilder\Setup\Converters;

use Magento\Framework\DB\DataConverter\DataConverterInterface;
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory;

/**
* ...
*/
class PageBuilderStripStyles implements DataConverterInterface
{
/**
* @var HtmlDocumentFactory
*/
private $htmlDocumentFactory;

/**
* @param HtmlDocumentFactory $htmlDocumentFactory
*/
public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
{
$this->htmlDocumentFactory = $htmlDocumentFactory;
}

/**
* @inheritDoc
*/
public function convert($value)
{
$document = $this->htmlDocumentFactory->create(['document' => $value]);
$queryPageBuilderElements = $document->querySelectorAll('[style]'); // @todo: Match Types

/** @var ElementInterface $element */
foreach ($queryPageBuilderElements as $element) {
$style = $element->getAttribute('style');

if ($style) {
// @todo: Convert to Inline

$element->removeAttribute('style');
}
}

return $queryPageBuilderElements->count() > 0 ? $document->stripHtmlWrapperTags() : $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types = 1);

namespace Magento\PageBuilder\Setup\Patch\Data;

use Magento\Framework\DB\FieldDataConversionException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\PageBuilder\Setup\Converters\PageBuilderStripStyles;
use Magento\PageBuilder\Setup\UpgradeContentHelper;

/**
* ...
*/
class UpgradePageBuilderStripStyles implements DataPatchInterface
{
/**
* @var UpgradeContentHelper
*/
private $helper;

/**
* @param UpgradeContentHelper $helper
*/
public function __construct(
UpgradeContentHelper $helper
) {
$this->helper = $helper;
}

/**
* Upgrade
*
* @return void
* @throws FieldDataConversionException
*/
public function apply()
{
$this->helper->upgrade([
PageBuilderStripStyles::class
]);
}

/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}

/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}
}

0 comments on commit 2f5e019

Please sign in to comment.