-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathraw-handler.php
More file actions
1111 lines (955 loc) · 33.3 KB
/
raw-handler.php
File metadata and controls
1111 lines (955 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Raw handler pipeline ported from Gutenberg JavaScript to PHP
*
* Uses WordPress HTML API (WP_HTML_Processor) for spec-compliant HTML5 parsing.
* Converts HTML to Gutenberg blocks using registered transforms.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main raw handler function - converts HTML to blocks
*
* @param array $args Arguments array with 'HTML' key and optional conversion context.
* @return array Array of block arrays
*/
function html_to_blocks_raw_handler( $args ) {
$html = $args['HTML'] ?? '';
if ( empty( $html ) ) {
return array();
}
if ( strpos( $html, '<!-- wp:' ) !== false ) {
$blocks = parse_blocks( $html );
$is_single_freeform = count( $blocks ) === 1
&& isset( $blocks[0]['blockName'] )
&& 'core/freeform' === $blocks[0]['blockName'];
if ( ! $is_single_freeform ) {
return html_to_blocks_normalize_parsed_image_html_blocks( $blocks );
}
$freeform_html = html_to_blocks_get_parsed_block_html( $blocks[0] );
if ( '' !== trim( $freeform_html ) ) {
$html = $freeform_html;
}
}
$pieces = html_to_blocks_shortcode_converter( $html );
$result = array();
foreach ( $pieces as $piece ) {
if ( ! is_string( $piece ) ) {
$result[] = $piece;
continue;
}
if ( ! html_to_blocks_can_skip_normalise_blocks( $piece ) ) {
$piece = html_to_blocks_normalise_blocks( $piece );
}
$blocks = html_to_blocks_convert( $piece, array_merge( $args, array( 'HTML' => $piece ) ) );
$result = array_merge( $result, $blocks );
}
return array_filter( $result );
}
/**
* Gets the HTML payload from a parsed block.
*
* @param array $block Parsed block array.
* @return string Block HTML.
*/
function html_to_blocks_get_parsed_block_html( array $block ): string {
if ( isset( $block['innerHTML'] ) && is_string( $block['innerHTML'] ) ) {
return $block['innerHTML'];
}
if ( empty( $block['innerContent'] ) || ! is_array( $block['innerContent'] ) ) {
return '';
}
$html = '';
foreach ( $block['innerContent'] as $content ) {
if ( is_string( $content ) ) {
$html .= $content;
}
}
return $html;
}
/**
* Determine whether block normalization can be skipped for an already wrapped fragment.
*
* Normalization only needs to repair top-level phrasing content. A fragment that is
* already one complete block-like root can go straight to raw conversion, avoiding
* an extra full scan of large generated HTML pages.
*
* @param string $html HTML fragment.
* @return bool True when normalization can be skipped.
*/
function html_to_blocks_can_skip_normalise_blocks( string $html ): bool {
$html = trim( $html );
if ( '' === $html || '<' !== $html[0] || ! preg_match( '/^<\s*([a-z0-9:-]+)/i', $html, $matches ) ) {
return false;
}
$tag_name = strtoupper( $matches[1] );
if ( in_array( $tag_name, html_to_blocks_phrasing_tag_names(), true ) ) {
return false;
}
return trim( (string) html_to_blocks_extract_balanced_element( $html, $tag_name ) ) === $html;
}
/**
* Gets tag names treated as phrasing content by block normalization.
*
* @return string[] Uppercase tag names.
*/
function html_to_blocks_phrasing_tag_names(): array {
return array(
'A',
'ABBR',
'B',
'BDI',
'BDO',
'BR',
'CITE',
'CODE',
'DATA',
'DFN',
'EM',
'I',
'KBD',
'MARK',
'Q',
'RP',
'RT',
'RUBY',
'S',
'SAMP',
'SMALL',
'SPAN',
'STRONG',
'SUB',
'SUP',
'TIME',
'U',
'VAR',
'WBR',
);
}
/**
* Calculate elapsed wall time in milliseconds.
*
* @param float $started Started timestamp from microtime(true).
* @return float Elapsed milliseconds.
*/
function html_to_blocks_elapsed_ms( float $started ): float {
return ( microtime( true ) - $started ) * 1000;
}
/**
* Accumulate per-transform trace metrics.
*
* @param array $metrics Metrics accumulator.
* @param string $name Transform metric key.
* @param string $field Metric field.
* @param float $value Value to add.
* @return void
*/
function html_to_blocks_record_transform_metric( array &$metrics, string $name, string $field, float $value ): void {
if ( ! isset( $metrics['transforms'][ $name ] ) ) {
$metrics['transforms'][ $name ] = array(
'count' => 0,
'execute_ms' => 0.0,
);
}
$metrics['transforms'][ $name ][ $field ] = ( $metrics['transforms'][ $name ][ $field ] ?? 0 ) + $value;
}
/**
* Converts HTML directly to blocks using registered transforms
*
* @param string $html HTML to convert
* @param array $args Raw handler arguments for transform context.
* @return array Array of blocks
*/
function html_to_blocks_convert( $html, $args = array() ) {
if ( empty( trim( $html ) ) ) {
return array();
}
$collect_metrics = function_exists( 'has_action' ) && has_action( 'html_to_blocks_convert_metrics' );
$metrics = null;
$convert_started = 0.0;
if ( $collect_metrics ) {
$metrics = array(
'html_bytes' => strlen( $html ),
'token_count' => 0,
'top_level_element_count' => 0,
'extract_ms' => 0.0,
'element_parse_ms' => 0.0,
'transform_match_ms' => 0.0,
'transform_execute_ms' => 0.0,
'content_measure_ms' => 0.0,
'total_ms' => 0.0,
'transforms' => array(),
);
$convert_started = microtime( true );
}
$processor = WP_HTML_Processor::create_fragment( $html );
if ( ! $processor ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Gated diagnostic logging for WP_DEBUG.
error_log( sprintf(
'[HTML to Blocks] create_fragment() failed | HTML length: %d | Preview: %s',
strlen( $html ),
substr( $html, 0, 300 )
) );
}
return array();
}
$original_html_length = strlen( $html );
$blocks = array();
$transforms = HTML_To_Blocks_Transform_Registry::get_raw_transforms();
$body_depth = 2;
$top_level_depth = $body_depth + 1;
$tag_occurrences = array();
$tag_positions = array();
$ignored_decorative_html_length = 0;
while ( $processor->next_token() ) {
if ( $collect_metrics ) {
++$metrics['token_count'];
}
$token_type = $processor->get_token_type();
$depth = $processor->get_current_depth();
if ( '#text' === $token_type && $depth === $top_level_depth ) {
$text = trim( $processor->get_modifiable_text() );
if ( ! empty( $text ) ) {
$blocks[] = HTML_To_Blocks_Block_Factory::create_block(
'core/paragraph',
array( 'content' => htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' ) )
);
}
continue;
}
if ( '#tag' !== $token_type ) {
continue;
}
if ( $processor->is_tag_closer() ) {
continue;
}
$tag_name = $processor->get_tag();
if ( ! isset( $tag_occurrences[ $tag_name ] ) ) {
$tag_occurrences[ $tag_name ] = 0;
$tag_positions[ $tag_name ] = html_to_blocks_find_all_tag_positions( $html, $tag_name );
}
$occurrence = $tag_occurrences[ $tag_name ]++;
if ( $depth !== $top_level_depth ) {
continue;
}
if ( $collect_metrics ) {
++$metrics['top_level_element_count'];
}
$phase_started = $collect_metrics ? microtime( true ) : 0.0;
$element_html = html_to_blocks_extract_element_at_occurrence( $html, $tag_name, $tag_positions[ $tag_name ], $occurrence );
if ( $collect_metrics ) {
$metrics['extract_ms'] += html_to_blocks_elapsed_ms( $phase_started );
}
if ( ! $element_html ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Gated diagnostic logging for WP_DEBUG.
error_log( sprintf(
'[HTML to Blocks] Element extraction failed | Tag: %s | Occurrence: %d | HTML preview: %s',
$tag_name,
$occurrence,
substr( $html, 0, 300 )
) );
}
continue;
}
$phase_started = $collect_metrics ? microtime( true ) : 0.0;
$element = HTML_To_Blocks_HTML_Element::from_html( $element_html );
if ( $collect_metrics ) {
$metrics['element_parse_ms'] += html_to_blocks_elapsed_ms( $phase_started );
}
if ( ! $element ) {
$blocks[] = html_to_blocks_create_unsupported_html_fallback_block(
$element_html,
array(
'reason' => 'element_parse_failed',
'tag_name' => $tag_name,
'occurrence' => $occurrence,
)
);
continue;
}
if ( 'BR' === $element->get_tag_name() ) {
$ignored_decorative_html_length += strlen( $element_html );
continue;
}
if ( html_to_blocks_should_ignore_empty_decorative_placeholder( $element ) ) {
$ignored_decorative_html_length += strlen( $element_html );
continue;
}
$phase_started = $collect_metrics ? microtime( true ) : 0.0;
$raw_transform = html_to_blocks_find_transform( $element, $transforms );
if ( $collect_metrics ) {
$metrics['transform_match_ms'] += html_to_blocks_elapsed_ms( $phase_started );
}
if ( ! $raw_transform ) {
if ( $collect_metrics ) {
html_to_blocks_record_transform_metric( $metrics, 'fallback:no_transform', 'count', 1 );
}
$blocks[] = html_to_blocks_create_unsupported_html_fallback_block(
$element_html,
array(
'reason' => 'no_transform',
'tag_name' => $element->get_tag_name(),
'occurrence' => $occurrence,
)
);
} else {
$transform_fn = $raw_transform['transform'] ?? null;
$metric_name = (string) ( $raw_transform['blockName'] ?? 'unknown' ) . ':p' . (string) ( $raw_transform['priority'] ?? 'default' );
if ( $collect_metrics ) {
html_to_blocks_record_transform_metric( $metrics, $metric_name, 'count', 1 );
}
if ( $transform_fn && is_callable( $transform_fn ) ) {
$phase_started = $collect_metrics ? microtime( true ) : 0.0;
$raw_handler_fn = 'html_to_blocks_raw_handler';
$raw_handler_callback = function ( $nested_args ) use ( $args, $raw_handler_fn ) {
$nested_args = is_array( $nested_args ) ? $nested_args : array();
return call_user_func( $raw_handler_fn, array_merge( $args, $nested_args ) );
};
$block = call_user_func( $transform_fn, $element, $raw_handler_callback, $args );
if ( $collect_metrics ) {
$elapsed = html_to_blocks_elapsed_ms( $phase_started );
$metrics['transform_execute_ms'] += $elapsed;
html_to_blocks_record_transform_metric( $metrics, $metric_name, 'execute_ms', $elapsed );
}
if ( $element->has_attribute( 'class' ) ) {
$existing_class = $block['attrs']['className'] ?? '';
$node_class = $element->get_attribute( 'class' );
$inner_html = $block['innerHTML'] ?? '';
if (
! empty( $node_class )
&& strpos( $existing_class, $node_class ) === false
&& strpos( $inner_html, $node_class ) === false
) {
$block['attrs']['className'] = trim( $existing_class . ' ' . $node_class );
}
}
$blocks[] = $block;
} else {
$phase_started = $collect_metrics ? microtime( true ) : 0.0;
$block_name = $raw_transform['blockName'];
$attributes = HTML_To_Blocks_Attribute_Parser::get_block_attributes(
$block_name,
$element_html
);
$blocks[] = HTML_To_Blocks_Block_Factory::create_block( $block_name, $attributes );
if ( $collect_metrics ) {
$elapsed = html_to_blocks_elapsed_ms( $phase_started );
$metrics['transform_execute_ms'] += $elapsed;
html_to_blocks_record_transform_metric( $metrics, $metric_name, 'execute_ms', $elapsed );
}
}
}
}
// Check if processor bailed due to unsupported HTML
$last_error = $processor->get_last_error();
if ( null !== $last_error ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Gated diagnostic logging for WP_DEBUG.
error_log( sprintf(
'[HTML to Blocks] WP_HTML_Processor bailed | Error: %s | Blocks created: %d | HTML length: %d | Preview: %s',
$last_error,
count( $blocks ),
$original_html_length,
substr( $html, 0, 500 )
) );
}
}
if ( empty( $blocks ) && trim( wp_strip_all_tags( $html ) ) !== '' && trim( $html ) === trim( wp_strip_all_tags( $html ) ) ) {
$blocks[] = HTML_To_Blocks_Block_Factory::create_block(
'core/paragraph',
array( 'content' => trim( $html ) )
);
}
// Check for significant content loss (input had content but output is empty/minimal)
$phase_started = $collect_metrics ? microtime( true ) : 0.0;
$output_content_length = html_to_blocks_measure_block_content_length( $blocks );
if ( $collect_metrics ) {
$metrics['content_measure_ms'] += html_to_blocks_elapsed_ms( $phase_started );
$metrics['total_ms'] = html_to_blocks_elapsed_ms( $convert_started );
do_action( 'html_to_blocks_convert_metrics', $metrics, $args );
}
$diagnostic_html_length = max( 0, $original_html_length - $ignored_decorative_html_length );
if ( $diagnostic_html_length > 100 && $output_content_length < ( $diagnostic_html_length * 0.1 ) ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Gated diagnostic logging for WP_DEBUG.
error_log( sprintf(
'[HTML to Blocks] Significant content loss detected | Input: %d chars | Output: %d chars | Blocks: %d | Processor error: %s | Preview: %s',
$diagnostic_html_length,
$output_content_length,
count( $blocks ),
$last_error ?? 'none',
substr( $html, 0, 500 )
) );
}
}
return $blocks;
}
/**
* Checks whether an empty div/span is a safe visual-only icon placeholder.
*
* @param HTML_To_Blocks_HTML_Element $element The source element.
* @return bool True when the placeholder should be ignored.
*/
function html_to_blocks_should_ignore_empty_decorative_placeholder( $element ): bool {
if ( ! in_array( $element->get_tag_name(), array( 'DIV', 'SPAN' ), true ) ) {
return false;
}
if ( trim( wp_strip_all_tags( $element->get_inner_html() ) ) !== '' || array() !== $element->get_child_elements() ) {
return false;
}
$attributes = $element->get_attributes();
if ( 'DIV' === $element->get_tag_name() && array() === $attributes ) {
return true;
}
$class_name = isset( $attributes['class'] ) ? (string) $attributes['class'] : '';
$style = isset( $attributes['style'] ) ? (string) $attributes['style'] : '';
$role = isset( $attributes['role'] ) ? strtolower( trim( (string) $attributes['role'] ) ) : '';
$decorative_class_pattern = '/(?:^|[-_\s])(icon|ico|glyph|symbol|accent|bar|divider|separator|sep|rule|line|blank|orb|blob|dot|glow)(?:$|[-_\s]|\d)/i';
if ( preg_match( $decorative_class_pattern, $class_name ) !== 1 ) {
return false;
}
foreach ( $attributes as $name => $value ) {
$name = strtolower( (string) $name );
if ( preg_match( '/^on/i', $name ) ) {
return false;
}
if ( ! in_array( $name, array( 'class', 'style', 'id', 'aria-hidden', 'role' ), true ) ) {
return false;
}
}
if ( '' !== $role && ! in_array( $role, array( 'none', 'presentation' ), true ) ) {
return false;
}
if ( preg_match( '/url\s*\(/i', $style ) ) {
return false;
}
if ( preg_match( '/(?:^|\s)code[-_]?dot(?:$|\s)/i', $class_name ) === 1 ) {
return true;
}
if ( preg_match( '/(?:^|[-_\s])(?:accent|sep)(?:$|[-_\s]|\d)/i', $class_name ) === 1 ) {
return true;
}
return preg_match( '/(?:^|;)\s*position\s*:\s*(?:absolute|fixed)\b/i', $style ) === 1
|| preg_match( '/(?:^|;)\s*opacity\s*:\s*0(?:\.0+)?\b/i', $style ) === 1
|| preg_match( '/(?:^|;)\s*(?:display\s*:\s*none|visibility\s*:\s*hidden|pointer-events\s*:\s*none)\b/i', $style ) === 1
|| strtolower( (string) ( $attributes['aria-hidden'] ?? '' ) ) === 'true';
}
/**
* Checks whether a span contains block-level markup that cannot live in a paragraph.
*
* @param HTML_To_Blocks_HTML_Element $element The source element.
* @return bool True when the span should be promoted to a block wrapper.
*/
function html_to_blocks_is_blocky_span( $element ): bool {
if ( 'SPAN' !== $element->get_tag_name() ) {
return false;
}
return preg_match( '/<(?:address|article|aside|blockquote|details|div|dl|fieldset|figcaption|figure|footer|form|h[1-6]|header|hr|main|nav|ol|p|pre|section|table|ul)\b/i', $element->get_inner_html() ) === 1;
}
/**
* Promotes an invalid span wrapper to a div while preserving safe attributes.
*
* @param HTML_To_Blocks_HTML_Element $element The span element.
* @return string A valid block-level wrapper with the original contents.
*/
function html_to_blocks_promote_span_to_div_markup( $element ): string {
$attributes = '';
foreach ( $element->get_attributes() as $name => $value ) {
$name = strtolower( (string) $name );
if ( preg_match( '/^[a-z][a-z0-9:-]*$/', $name ) !== 1 ) {
continue;
}
if ( true === $value ) {
$attributes .= ' ' . $name;
continue;
}
$attributes .= ' ' . $name . '="' . esc_attr( (string) $value ) . '"';
}
return '<div' . $attributes . '>' . $element->get_inner_html() . '</div>';
}
/**
* Measures converted block content, including nested layout descendants.
*
* @param array $blocks Converted block arrays.
* @return int Approximate HTML content length.
*/
function html_to_blocks_measure_block_content_length( array $blocks ): int {
$length = 0;
foreach ( $blocks as $block ) {
if ( ! is_array( $block ) ) {
continue;
}
$length += strlen( (string) ( $block['innerHTML'] ?? '' ) );
if ( isset( $block['attrs']['content'] ) && is_string( $block['attrs']['content'] ) ) {
$length += strlen( $block['attrs']['content'] );
}
if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
$length += html_to_blocks_measure_block_content_length( $block['innerBlocks'] );
}
}
return $length;
}
/**
* Creates the core/html fallback block and emits an observability hook.
*
* @param string $element_html Unsupported HTML fragment.
* @param array $context Fallback context such as reason, tag_name, and occurrence.
* @return array Block array.
*/
function html_to_blocks_create_unsupported_html_fallback_block( string $element_html, array $context = array() ): array {
$block = HTML_To_Blocks_Block_Factory::create_block(
'core/html',
array( 'content' => $element_html )
);
if ( function_exists( 'do_action' ) ) {
/**
* Fires when h2bc falls back to core/html because no supported transform exists.
*
* @param string $element_html Unsupported HTML fragment.
* @param array $context Context including reason, tag_name, and occurrence when available.
* @param array $block The generated core/html fallback block.
*/
do_action( 'html_to_blocks_unsupported_html_fallback', $element_html, $context, $block );
}
return $block;
}
/**
* Recursively converts parsed core/html image fragments back to native image blocks.
*
* Some upstream callers pass already-serialized block markup through h2bc. In that
* path parse_blocks() would otherwise preserve harmless image-only core/html
* fragments instead of applying the raw image transforms.
*
* @param array<int|string,array<string,mixed>> $blocks Parsed blocks.
* @return array<int|string,array<string,mixed>> Normalized blocks.
*/
function html_to_blocks_normalize_parsed_image_html_blocks( array $blocks ): array {
$normalized = array();
foreach ( $blocks as $block ) {
if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
$block['innerBlocks'] = html_to_blocks_normalize_parsed_image_html_blocks( $block['innerBlocks'] );
}
if ( ( $block['blockName'] ?? null ) !== 'core/html' ) {
$normalized[] = $block;
continue;
}
$html = '';
if ( isset( $block['attrs']['content'] ) && is_string( $block['attrs']['content'] ) ) {
$html = $block['attrs']['content'];
} elseif ( isset( $block['innerHTML'] ) && is_string( $block['innerHTML'] ) ) {
$html = $block['innerHTML'];
}
$convertible_html = $html;
$is_decorative_inline_span = false;
$is_image_only_fragment = false;
$is_form_container = false;
if ( html_to_blocks_is_decorative_inline_span_fragment( $html ) ) {
$is_decorative_inline_span = true;
$convertible_html = html_to_blocks_normalise_blocks( $html );
} elseif ( html_to_blocks_is_image_only_html_fragment( $html ) ) {
$is_image_only_fragment = true;
} elseif ( html_to_blocks_is_form_containing_container_fragment( $html ) ) {
$is_form_container = true;
} else {
$normalized[] = $block;
continue;
}
$converted = html_to_blocks_convert( $convertible_html );
if ( empty( $converted ) ) {
$normalized[] = $block;
continue;
}
if ( ( $is_decorative_inline_span || $is_image_only_fragment ) && html_to_blocks_contains_block_name( $converted, 'core/html' ) ) {
$normalized[] = $block;
continue;
}
if ( $is_form_container && html_to_blocks_is_single_html_fallback_for_fragment( $converted, $html ) ) {
$normalized[] = $block;
continue;
}
$normalized = array_merge( $normalized, $converted );
}
return $normalized;
}
/**
* Checks whether a raw HTML fallback wraps a larger static container with form controls.
*
* @param string $html HTML fragment.
* @return bool True when reconversion may shrink fallback to a form/control island.
*/
function html_to_blocks_is_form_containing_container_fragment( string $html ): bool {
$element = HTML_To_Blocks_HTML_Element::from_html( $html );
if ( ! $element ) {
return false;
}
if ( ! in_array( $element->get_tag_name(), array( 'SECTION', 'DIV', 'ARTICLE', 'ASIDE', 'HEADER', 'FOOTER', 'MAIN', 'NAV' ), true ) ) {
return false;
}
foreach ( array( 'form', 'input', 'textarea', 'select', 'button' ) as $selector ) {
if ( $element->query_selector( $selector ) ) {
return true;
}
}
return false;
}
/**
* Checks whether conversion still produced the original opaque core/html fragment.
*
* @param array $blocks Blocks produced by reconversion.
* @param string $html Original HTML fragment.
* @return bool True when fallback scope did not shrink.
*/
function html_to_blocks_is_single_html_fallback_for_fragment( array $blocks, string $html ): bool {
if ( count( $blocks ) !== 1 || ( $blocks[0]['blockName'] ?? null ) !== 'core/html' ) {
return false;
}
$fallback_html = $blocks[0]['attrs']['content'] ?? $blocks[0]['innerHTML'] ?? '';
return is_string( $fallback_html ) && trim( $fallback_html ) === trim( $html );
}
/**
* Checks whether an HTML fragment is one safe, empty decorative inline span.
*
* @param string $html HTML fragment.
* @return bool True when the fragment can be materialized as editable inline content.
*/
function html_to_blocks_is_decorative_inline_span_fragment( string $html ): bool {
$element = HTML_To_Blocks_HTML_Element::from_html( $html );
if ( ! $element || $element->get_tag_name() !== 'SPAN' ) {
return false;
}
if ( trim( wp_strip_all_tags( $element->get_inner_html() ) ) !== '' || array() !== $element->get_child_elements() ) {
return false;
}
$attributes = $element->get_attributes();
$class_name = isset( $attributes['class'] ) ? (string) $attributes['class'] : '';
$style = isset( $attributes['style'] ) ? (string) $attributes['style'] : '';
$role = isset( $attributes['role'] ) ? strtolower( trim( (string) $attributes['role'] ) ) : '';
foreach ( $attributes as $name => $value ) {
$name = strtolower( (string) $name );
if ( preg_match( '/^on/i', $name ) ) {
return false;
}
if ( ! in_array( $name, array( 'class', 'style', 'id', 'aria-hidden', 'role' ), true ) ) {
return false;
}
}
if ( '' !== $role && ! in_array( $role, array( 'none', 'presentation' ), true ) ) {
return false;
}
if ( preg_match( '/(?:url\s*\(|expression\s*\(|javascript\s*:|behavior\s*:)/i', $style ) ) {
return false;
}
$decorative_class_pattern = '/(?:^|[-_\s])(icon|ico|glyph|symbol|accent|bar|divider|separator|sep|rule|line|blank|orb|blob|dot|glow)(?:$|[-_\s]|\d)/i';
if ( preg_match( $decorative_class_pattern, $class_name ) === 1 ) {
return true;
}
return '' !== $style
&& preg_match( '/(?:^|;)\s*display\s*:\s*inline-block\b/i', $style ) === 1
&& preg_match( '/(?:^|;)\s*width\s*:\s*[^;]+/i', $style ) === 1
&& preg_match( '/(?:^|;)\s*height\s*:\s*[^;]+/i', $style ) === 1
&& preg_match( '/(?:^|;)\s*(?:background|background-color)\s*:\s*[^;]+/i', $style ) === 1;
}
/**
* Checks whether an HTML fragment is only an image, optionally inside one wrapper.
*
* @param string $html HTML fragment.
* @return bool True when the fragment can safely be re-run through image transforms.
*/
function html_to_blocks_is_image_only_html_fragment( string $html ): bool {
$element = HTML_To_Blocks_HTML_Element::from_html( $html );
if ( ! $element ) {
return false;
}
if ( $element->get_tag_name() === 'IMG' ) {
$src = $element->get_attribute( 'src' );
return is_string( $src ) && '' !== $src;
}
if ( ! in_array( $element->get_tag_name(), array( 'DIV', 'SPAN', 'FIGURE' ), true ) ) {
return false;
}
$images = $element->query_selector_all( 'img' );
$src = count( $images ) === 1 ? $images[0]->get_attribute( 'src' ) : null;
if ( count( $images ) !== 1 || ! is_string( $src ) || '' === $src ) {
return false;
}
$remaining = str_replace( $images[0]->get_outer_html(), '', $element->get_inner_html() );
return trim( wp_strip_all_tags( $remaining ) ) === '';
}
/**
* Checks whether a block tree contains a block name.
*
* @param array<int|string,array<string,mixed>> $blocks Blocks to inspect.
* @param string $name Block name.
* @return bool True when the block tree contains the name.
*/
function html_to_blocks_contains_block_name( array $blocks, string $name ): bool {
foreach ( $blocks as $block ) {
if ( ( $block['blockName'] ?? null ) === $name ) {
return true;
}
if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) && html_to_blocks_contains_block_name( $block['innerBlocks'], $name ) ) {
return true;
}
}
return false;
}
/**
* Finds all positions of a tag's opening tags in HTML
*
* @param string $html Source HTML
* @param string $tag_name Tag name to find
* @return array Array of start positions
*/
function html_to_blocks_find_all_tag_positions( $html, $tag_name ) {
$positions = array();
$pattern = '/<' . preg_quote( $tag_name, '/' ) . '(?:\s[^>]*)?>/i';
if ( preg_match_all( $pattern, $html, $matches, PREG_OFFSET_CAPTURE ) ) {
foreach ( $matches[0] as $match ) {
$positions[] = $match[1];
}
}
return $positions;
}
/**
* Extracts element HTML at a specific occurrence
*
* @param string $html Source HTML
* @param string $tag_name Tag name
* @param array $positions Array of tag start positions
* @param int $occurrence Which occurrence (0-based)
* @return string|null Element HTML or null
*/
function html_to_blocks_extract_element_at_occurrence( $html, $tag_name, $positions, $occurrence ) {
if ( ! isset( $positions[ $occurrence ] ) ) {
return null;
}
$start_pos = $positions[ $occurrence ];
$html_from_start = substr( $html, $start_pos );
$void_elements = array(
'AREA',
'BASE',
'BR',
'COL',
'EMBED',
'HR',
'IMG',
'INPUT',
'LINK',
'META',
'PARAM',
'SOURCE',
'TRACK',
'WBR',
);
if ( in_array( strtoupper( $tag_name ), $void_elements, true ) ) {
$pattern = '/<' . preg_quote( $tag_name, '/' ) . '(?:\s[^>]*)?\/?>/i';
if ( preg_match( $pattern, $html_from_start, $matches ) ) {
return $matches[0];
}
return null;
}
return html_to_blocks_extract_balanced_element( $html_from_start, $tag_name );
}
/**
* Extracts a balanced element including nested elements of the same type
*
* @param string $html HTML starting with the opening tag
* @param string $tag_name Tag name to balance
* @return string|null Balanced element HTML or null
*/
function html_to_blocks_extract_balanced_element( $html, $tag_name ) {
$depth = 0;
$tag_pattern = '/<\/?' . preg_quote( $tag_name, '/' ) . '(?:\s[^>]*)?>/i';
$matched_count = preg_match_all( $tag_pattern, $html, $matches, PREG_OFFSET_CAPTURE );
if ( false === $matched_count || 0 === $matched_count ) {
return null;
}
foreach ( $matches[0] as $match ) {
$tag_markup = $match[0];
$offset = $match[1];
if ( 0 === strpos( $tag_markup, '</' ) ) {
--$depth;
if ( 0 === $depth ) {
return substr( $html, 0, $offset + strlen( $tag_markup ) );
}
continue;
}
++$depth;
}
return null;
}
/**
* Finds a matching raw transform for an element
*
* @param HTML_To_Blocks_HTML_Element $element The element to match
* @param array $transforms Array of transforms
* @return array|null The transform data or null
*/
function html_to_blocks_find_transform( $element, $transforms ) {
foreach ( $transforms as $transform ) {
$is_match = $transform['isMatch'] ?? null;
if ( $is_match && is_callable( $is_match ) && call_user_func( $is_match, $element ) ) {
return $transform;
}
}
return null;
}
/**
* Converts shortcodes in HTML to blocks
*
* @param string $html The HTML containing shortcodes
* @return array Array of pieces (strings or blocks)
*/
function html_to_blocks_shortcode_converter( $html ) {
$pieces = array();
$last_index = 0;
preg_match_all( '/' . get_shortcode_regex() . '/', $html, $matches, PREG_OFFSET_CAPTURE );
if ( empty( $matches[0] ) ) {
return array( $html );
}
foreach ( $matches[0] as $match ) {
$shortcode = $match[0];
$index = $match[1];
if ( $index > $last_index ) {
$pieces[] = substr( $html, $last_index, $index - $last_index );
}
$parsed = html_to_blocks_parse_shortcode( $shortcode );
$pieces[] = null !== $parsed ? $parsed : $shortcode;
$last_index = $index + strlen( $shortcode );
}
if ( $last_index < strlen( $html ) ) {
$pieces[] = substr( $html, $last_index );
}
return $pieces;
}
/**
* Parses a shortcode and returns a block if possible
*
* @param string $shortcode The shortcode string
* @return array|null The block array or null
*/
function html_to_blocks_parse_shortcode( $shortcode ) {
$pattern = get_shortcode_regex();
if ( ! preg_match( "/$pattern/", $shortcode, $match ) ) {
return null;
}
return HTML_To_Blocks_Block_Factory::create_block(
'core/shortcode',
array( 'text' => $shortcode )
);
}
/**
* Normalises blocks in HTML - wraps inline content in paragraphs
*
* @param string $html The HTML
* @return string The normalized HTML
*/
function html_to_blocks_normalise_blocks( $html ) {
$processor = WP_HTML_Processor::create_fragment( $html );
if ( ! $processor ) {
return $html;
}
$phrasing_tags = html_to_blocks_phrasing_tag_names();
$body_depth = 2;
$top_level_depth = $body_depth + 1;
$output = '';
$paragraph_buffer = '';
$in_paragraph = false;
$last_was_br = false;
$tag_occurrences = array();
$tag_positions = array();