1010
1111use Apple_Exporter \Theme ;
1212use DOMElement ;
13+ use WP_HTML_Tag_Processor ;
1314
1415/**
1516 * A paragraph component.
@@ -35,6 +36,13 @@ class Body extends Component {
3536 */
3637 protected $ can_be_anchor_target = true ;
3738
39+ /**
40+ * Set default value for parent text alignment property.
41+ *
42+ * @var string
43+ */
44+ protected $ text_alignment = 'left ' ;
45+
3846 /**
3947 * Look for node matches for this component.
4048 *
@@ -146,6 +154,21 @@ public function register_specs(): void {
146154 $ default_spec
147155 );
148156
157+ $ this ->register_spec (
158+ 'default-body-center ' ,
159+ __ ( 'Centered Text ' , 'apple-news ' ),
160+ [
161+ 'textAlignment ' => 'center ' ,
162+ ],
163+ );
164+ $ this ->register_spec (
165+ 'default-body-right ' ,
166+ __ ( 'Right-Aligned Text ' , 'apple-news ' ),
167+ [
168+ 'textAlignment ' => 'right ' ,
169+ ],
170+ );
171+
149172 $ dropcap_color_dark = $ theme ->get_value ( 'dropcap_color_dark ' );
150173 $ dropcap_background_color_dark = $ theme ->get_value ( 'dropcap_background_color_dark ' );
151174
@@ -290,6 +313,7 @@ private static function split_unsupported_elements( $html, $tag, $open, $close )
290313 * @access protected
291314 */
292315 protected function build ( $ html ) {
316+ $ origin = $ html ;
293317
294318 // If there is no text for this element, bail.
295319 $ html = $ this ->parser ->parse ( $ html );
@@ -298,6 +322,22 @@ protected function build( $html ) {
298322 return ;
299323 }
300324
325+ // Determine the text alignment from the first tag in the original HTML, which still has the alignment attributes.
326+ $ proc = new WP_HTML_Tag_Processor ( $ origin );
327+ if ( false !== $ proc ->next_tag () ) {
328+ // `has_class()` is available in 6.4+.
329+ $ class = (string ) $ proc ->get_attribute ( 'class ' );
330+ $ style = (string ) $ proc ->get_attribute ( 'style ' );
331+
332+ if ( preg_match ( '/\bhas-text-align-center\b/i ' , $ class ) || preg_match ( '/\btext-align:\s*center\b/i ' , $ style ) ) {
333+ $ this ->text_alignment = 'center ' ;
334+ }
335+
336+ if ( preg_match ( '/\bhas-text-align-right\b/i ' , $ class ) || preg_match ( '/\btext-align:\s*right\b/i ' , $ style ) ) {
337+ $ this ->text_alignment = 'right ' ;
338+ }
339+ }
340+
301341 // Add the JSON for this component.
302342 $ this ->register_json (
303343 'json ' ,
@@ -477,7 +517,7 @@ private function get_default_text_styles() {
477517 'fontSize ' => '#cite_size# ' ,
478518 'tracking ' => '#cite_tracking# ' ,
479519 'lineHeight ' => '#cite_line_height# ' ,
480- 'textColor ' => '#cite_color ' ,
520+ 'textColor ' => '#cite_color# ' ,
481521 ],
482522 $ conditionals ['cite ' ] ?? []
483523 ),
@@ -487,7 +527,7 @@ private function get_default_text_styles() {
487527 'fontSize ' => '#monospaced_size# ' ,
488528 'tracking ' => '#monospaced_tracking# ' ,
489529 'lineHeight ' => '#monospaced_line_height# ' ,
490- 'textColor ' => '#monospaced_color ' ,
530+ 'textColor ' => '#monospaced_color# ' ,
491531 ],
492532 $ conditionals ['monospaced ' ] ?? []
493533 ),
@@ -523,12 +563,32 @@ private function get_default_text_styles() {
523563 * @access public
524564 */
525565 public function set_default_style () {
566+ // Always register the default style.
526567 $ this ->register_style (
527568 'default-body ' ,
528569 'default-body ' ,
529570 $ this ->get_default_style_values (),
530571 'textStyle '
531572 );
573+
574+ // If necessary, register the styles that are expected to override the 'textAlignment' property.
575+ if ( 'center ' === $ this ->text_alignment ) {
576+ $ this ->register_style (
577+ 'default-body-center ' ,
578+ 'default-body-center ' ,
579+ [],
580+ 'textStyle '
581+ );
582+ }
583+
584+ if ( 'right ' === $ this ->text_alignment ) {
585+ $ this ->register_style (
586+ 'default-body-right ' ,
587+ 'default-body-right ' ,
588+ [],
589+ 'textStyle '
590+ );
591+ }
532592 }
533593
534594 /**
0 commit comments