Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks

Text::adjustStyleAttribute │ private │ WC 1.0

1) We need to remove padding because we render padding on wrapping table cell
2) We also need to replace font-size to avoid clamp() because clamp() is not supported in many email clients. The font size values is automatically converted to clamp() when WP site theme is configured to use fluid layouts. Currently (WP 6.5), there is no way to disable this behavior.

Method of the class: Text{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->adjustStyleAttribute( $block_content ): string;
$block_content(string) (required)
Block content.

Text::adjustStyleAttribute() code WC 11.2.1

private function adjustStyleAttribute( string $block_content ): string {
	$html = new \WP_HTML_Tag_Processor( $block_content );

	if ( $html->next_tag() ) {
		$element_style_value = $html->get_attribute( 'style' );
		$element_style       = isset( $element_style_value ) ? strval( $element_style_value ) : '';
		// Padding may contain value like 10px or variable like var(--spacing-10).
		$element_style = (string) preg_replace( '/padding[^:]*:.?[0-9a-z-()]+;?/', '', $element_style );

		// Margin is not supported in email renderer, so we need to remove it.
		$element_style = (string) preg_replace( '/margin[^:]*:.?[0-9a-z-()]+;?/', '', $element_style );

		// Remove border styles. We apply border styles on the wrapping table cell.
		$element_style = (string) preg_replace( '/border[^:]*:.?[0-9a-z-()#]+;?/', '', $element_style );

		// Remove the background color for the same reason we remove the background classes: it is
		// rendered on the wrapping table cell, and a translucent color left here paints twice.
		// This assumes the cell gets the same color from the block attributes, which is true for
		// anything the editor saves. Markup that carries an inline background without the matching
		// attribute loses it, the same way padding, margin, and border above already behave.
		// The lookbehind keeps the match anchored to the start of a property name, so a longer
		// property that merely ends in "background-color" (a custom property, say) is not cut in
		// half, which would leave its prefix fused to the following declaration. Property names
		// are case-insensitive in CSS and a colon may be surrounded by whitespace, so both are
		// matched — unlike the class names above, which the CSS inliner matches case-sensitively.
		$element_style = (string) preg_replace( '/(?<![a-z-])background-color\s*:\s*[^;]+;?/i', '', $element_style );

		// We define the font-size on the wrapper element, but we need to keep font-size definition here
		// to prevent CSS Inliner from adding a default value and overriding the value set by user, which is on the wrapper element.
		// The value provided by WP uses clamp() function which is not supported in many email clients.
		$element_style = (string) preg_replace( '/font-size:[^;]+;?/', 'font-size: inherit;', $element_style );
		/** @var string $element_style */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
		$html->set_attribute( 'style', esc_attr( $element_style ) );
		$block_content = $html->get_updated_html();
	}

	return $block_content;
}