Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Script #623

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
91c59fb
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Aug 27, 2020
2f5e019
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Aug 27, 2020
90f7518
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Aug 27, 2020
3dffe71
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 9, 2020
3790cc3
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 9, 2020
1e7b674
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 10, 2020
12fe2e6
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 10, 2020
30ffcdb
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 14, 2020
b293714
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 14, 2020
f50a767
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 14, 2020
c5f9154
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 14, 2020
ad244d7
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 15, 2020
4527f1e
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 15, 2020
c6383f6
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 15, 2020
ac32a27
Merge branch 'csso-alternative' of github.com:magento/magento2-page-b…
omiroshnichenko Sep 17, 2020
3f1a669
magento/magento2-page-builder#558: Content Types Output Style Attribu…
omiroshnichenko Sep 17, 2020
368d6fa
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 21, 2020
60343e5
Merge from `558_1_content-type-style-attribute-removal`
bluemwhitew Sep 21, 2020
043b358
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 21, 2020
f88405d
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 21, 2020
b2661d9
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 22, 2020
0c3aa56
magento/magento2-page-builder#558: Content Types Output Style Attribu…
bluemwhitew Sep 22, 2020
2d5dfd7
Merge from `csso-alternative`
bluemwhitew Sep 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public function closest(string $selectors): ?ElementInterface;
*/
public function getAttribute($name): ?string;

/**
* Removes the Specified Attribute
*
* @param string $name
* @return bool|null
*/
public function removeAttribute($name): ?bool;

/**
* Sets the value of the specified attribute
*
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/PageBuilder/Model/Dom/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public function getAttribute($name): ?string
return $this->element->getAttribute($name);
}

/**
* @inheritDoc
*/
public function removeAttribute($name): ?bool
{
return $this->element->removeAttribute($name);
}

/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types = 1);

namespace Magento\PageBuilder\Setup\Converters;

use DOMDocument;
use DOMElement;
use DOMXPath;
use Magento\Framework\DB\DataConverter\DataConverterInterface;

/**
* Convert Inline Styles to Internal
*/
class PageBuilderStripStyles implements DataConverterInterface
{
const BODY_ID = 'html-body';
const DATA_ATTRIBUTE = 'data-pb-style';
const XPATH_SELECTOR = '//*[@data-content-type][@style]|//*[@data-content-type]/*[@style]';
bluemwhitew marked this conversation as resolved.
Show resolved Hide resolved
bluemwhitew marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var DOMDocument
*/
private $domDocument;

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

/**
* Generates `mageUtils.uniqueid()` Naming Convention
*
* @return string
*/
private function generateDataAttribute(): string
{
return \strtoupper(\uniqid());
}

/**
* Converts Inline Styles to Internal Styles
*
* @param array $styleMap
* @return string
*/
private function generateInternalStyles(array $styleMap): string
{
$output = '';

foreach ($styleMap as $selector => $styles) {
$output .= '#' . self::BODY_ID . ' [' . self::DATA_ATTRIBUTE . '="' . $selector . '"]';
$output .= '{' . $styles . '}';
}

return $output;
}

/**
* @inheritDoc
*/
public function convert($value): string
{
\libxml_use_internal_errors(true);
$document = new DOMDocument();
bluemwhitew marked this conversation as resolved.
Show resolved Hide resolved
$document->loadHTML($value);
$xpath = new DOMXPath($document);
\libxml_clear_errors();

$body = $document->documentElement->lastChild;
$nodes = $xpath->query(self::XPATH_SELECTOR); // Query for Inline Styles
$styleMap = [];

foreach ($nodes as $node) {
bluemwhitew marked this conversation as resolved.
Show resolved Hide resolved
bluemwhitew marked this conversation as resolved.
Show resolved Hide resolved
/* @var DOMElement $node */
$styleAttr = $node->getAttribute('style');

if ($styleAttr) {
$generatedDataAttribute = $this->generateDataAttribute();
$node->setAttribute(self::DATA_ATTRIBUTE, $generatedDataAttribute);
$styleMap[$generatedDataAttribute] = $styleAttr; // Amend Array for Internal Style Generation
$node->removeAttribute('style');
}
}

// Style Block Generation
$style = $document->createElement(
'style',
$this->generateInternalStyles($styleMap)
);
$body->appendChild($style);

// @todo: Refactor
\preg_match(
'/<html><body>(.+)<\/body><\/html>$/si',
$document->saveHTML(),
$matches
);

return $matches && $nodes->count() > 0 ? $matches[1] : $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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;

/**
* Patch Upgrade Mechanism for Converting Inline Styles to Internal
*/
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()
bluemwhitew marked this conversation as resolved.
Show resolved Hide resolved
{
$this->helper->upgrade([
PageBuilderStripStyles::class
]);
}

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

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