Skip to content

Commit

Permalink
Convert bare numbers as pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Feb 13, 2024
1 parent 461eff5 commit 5fafac2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion application/src/Form/Element/LengthCssDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class LengthCssDataType extends Text
{
// A number followed by a unit.
const PATTERN = '^(\d*\.?\d+)(%|cap|ch|em|ex|ic|lh|rem|rlh|vh|svh|lvh|dvh|vw|svw|lvw|dvw|vmax|svmax|lvmax|dvmax|vmin|svmin|lvmin|dvmin|vb|svb|lvb|dvb|vi|svi|lvi|dvi|cqw|cqh|cqi|cqb|cqmin|cqmax|px|cm|mm|Q|in|pc|pt)$';
const PATTERN = '^(\d*\.?\d+)(%|cap|ch|em|ex|ic|lh|rem|rlh|vh|svh|lvh|dvh|vw|svw|lvw|dvw|vmax|svmax|lvmax|dvmax|vmin|svmin|lvmin|dvmin|vb|svb|lvb|dvb|vi|svi|lvi|dvi|cqw|cqh|cqi|cqb|cqmin|cqmax|px|cm|mm|Q|in|pc|pt)?$';

public function __construct($name = null, iterable $options = [])
{
Expand Down
14 changes: 8 additions & 6 deletions application/src/View/Helper/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public function getBlockInlineStyles(SitePageBlockRepresentation $block)
$isValidHexColor = fn ($hexColor) => preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $hexColor);
// Validate a CSS <length>
$isValidLength = fn ($length) => preg_match(sprintf('/%s/', LengthCssDataType::PATTERN), $length);
// Prepare a CSS <length> for use in an inline style. Note that we convert bare numbers as pixels.
$prepareLength = fn ($length) => is_numeric($length) ? sprintf('%spx', $length) : $length;

// Allow modules to add inline styles for styling the layout.
$eventArgs = $this->eventManager->prepareArgs(['inline_styles' => []]);
Expand All @@ -155,28 +157,28 @@ public function getBlockInlineStyles(SitePageBlockRepresentation $block)

$maxWidth = $block->layoutDataValue('max_width');
if (is_string($maxWidth) && $isValidLength($maxWidth)) {
$inlineStyles[] = sprintf('max-width: %s', $maxWidth);
$inlineStyles[] = sprintf('max-width: %s', $prepareLength($maxWidth));
}
$minHeight = $block->layoutDataValue('min_height');
if (is_string($minHeight) && $isValidLength($minHeight)) {
$inlineStyles[] = sprintf('min-height: %s', $minHeight);
$inlineStyles[] = sprintf('min-height: %s', $prepareLength($minHeight));
}

$paddingTop = $block->layoutDataValue('padding_top');
if (is_string($paddingTop) && $isValidLength($paddingTop)) {
$inlineStyles[] = sprintf('padding-top: %s', $paddingTop);
$inlineStyles[] = sprintf('padding-top: %s', $prepareLength($paddingTop));
}
$paddingRight = $block->layoutDataValue('padding_right');
if (is_string($paddingRight) && $isValidLength($paddingRight)) {
$inlineStyles[] = sprintf('padding-right: %s', $paddingRight);
$inlineStyles[] = sprintf('padding-right: %s', $prepareLength($paddingRight));
}
$paddingBottom = $block->layoutDataValue('padding_bottom');
if (is_string($paddingBottom) && $isValidLength($paddingBottom)) {
$inlineStyles[] = sprintf('padding-bottom: %s', $paddingBottom);
$inlineStyles[] = sprintf('padding-bottom: %s', $prepareLength($paddingBottom));
}
$paddingLeft = $block->layoutDataValue('padding_left');
if (is_string($paddingLeft) && $isValidLength($paddingLeft)) {
$inlineStyles[] = sprintf('padding-left: %s', $paddingLeft);
$inlineStyles[] = sprintf('padding-left: %s', $prepareLength($paddingLeft));
}

return $inlineStyles;
Expand Down

0 comments on commit 5fafac2

Please sign in to comment.