Changeset 65560
- Timestamp:
- 11/17/2026 06:55:10 PM (less than one hour ago)
- Location:
- trunk
- Files:
-
- 3 edited
-
src/wp-includes/blocks.php (modified) (1 diff)
-
src/wp-includes/html-api/class-wp-html-tag-processor.php (modified) (1 diff)
-
tests/phpunit/tests/formatting/excerptRemoveFootnotes.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r65317 r65560 2357 2357 2358 2358 /** 2359 * Parses footnotes markup out of a content string, 2360 * and renders those appropriate for the excerpt. 2359 * Removes recognized footnote reference markup from a given string. 2360 * 2361 * Example: 2362 * 2363 * $content = 'Never<sup data-fn="abc-123" class="fn"><a href="#abc-123" id="abc-123">1</a></sup> say never.'; 2364 * 'Never say never' === excerpt_remove_footnotes( $content ); 2361 2365 * 2362 2366 * @since 6.3.0 2363 2367 * 2364 * @param string $content The content to parse.2365 * @return string The parsed and filtered content.2368 * @param string $content HTML which might contain footnote references from the block editor. 2369 * @return string Given input HTML without the footnote references. 2366 2370 */ 2367 2371 function excerpt_remove_footnotes( $content ) { 2368 if ( ! str_contains( $content, 'data-fn =' ) ) {2372 if ( ! str_contains( $content, 'data-fn' ) ) { 2369 2373 return $content; 2370 2374 } 2371 2375 2372 return preg_replace( 2373 '_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_', 2374 '', 2375 $content 2376 ); 2376 $processor = new class( $content ) extends WP_HTML_Tag_Processor { 2377 public function is_whitespace_text_node(): bool { 2378 if ( self::STATE_TEXT_NODE !== $this->parser_state ) { 2379 return false; 2380 } 2381 2382 $this->subdivide_text_appropriately(); 2383 return self::TEXT_IS_WHITESPACE === $this->text_node_classification; 2384 } 2385 2386 public function get_span(): WP_HTML_Span { 2387 $this->set_bookmark( 'here' ); 2388 2389 return $this->bookmarks['here']; 2390 } 2391 }; 2392 2393 $output = ''; 2394 $at = 0; 2395 $reprocess_token = false; 2396 2397 while ( $reprocess_token || $processor->next_token() ) { 2398 $reprocess_token = false; 2399 2400 // Match the <sup data-fn="..." class="..."> opening tag. 2401 if ( 2402 $processor->is_tag_closer() || 2403 'SUP' !== $processor->get_tag() || 2404 null === $processor->get_attribute( 'data-fn' ) || 2405 null === $processor->get_attribute( 'class' ) 2406 ) { 2407 continue; 2408 } 2409 2410 $footnote_opening = $processor->get_span(); 2411 2412 if ( ! $processor->next_token() ) { 2413 break; 2414 } 2415 2416 // Skip any whitespace-only text between the tags. 2417 if ( '#text' === $processor->get_token_name() ) { 2418 if ( ! $processor->is_whitespace_text_node() ) { 2419 $reprocess_token = true; 2420 continue; 2421 } 2422 2423 $processor->next_token(); 2424 } 2425 2426 // Match the <a href="..." id="..."> opening tag. 2427 if ( 2428 $processor->is_tag_closer() || 2429 'A' !== $processor->get_token_name() || 2430 null === $processor->get_attribute( 'href' ) || 2431 null === $processor->get_attribute( 'id' ) 2432 ) { 2433 $reprocess_token = true; 2434 continue; 2435 } 2436 2437 // Match an all-digit text node. 2438 if ( ! $processor->next_token() || '#text' !== $processor->get_token_name() ) { 2439 $reprocess_token = true; 2440 continue; 2441 } 2442 2443 $footnote_number = $processor->get_modifiable_text(); 2444 $digit_count = strspn( $footnote_number, '0123456789' ); 2445 if ( 0 === $digit_count || strlen( $footnote_number ) !== $digit_count ) { 2446 $reprocess_token = true; 2447 continue; 2448 } 2449 2450 // Match the closing </a> tag. 2451 if ( 2452 ! $processor->next_token() || 2453 'A' !== $processor->get_token_name() || 2454 ! $processor->is_tag_closer() 2455 ) { 2456 $reprocess_token = true; 2457 continue; 2458 } 2459 2460 if ( ! $processor->next_token() ) { 2461 break; 2462 } 2463 2464 // Skip any whitespace-only text between the tags. 2465 if ( '#text' === $processor->get_token_name() ) { 2466 if ( ! $processor->is_whitespace_text_node() ) { 2467 $reprocess_token = true; 2468 continue; 2469 } 2470 2471 $processor->next_token(); 2472 } 2473 2474 // Match the closing </sup> tag. 2475 if ( 2476 'SUP' !== $processor->get_token_name() || 2477 ! $processor->is_tag_closer() 2478 ) { 2479 $reprocess_token = true; 2480 continue; 2481 } 2482 2483 $footnote_closing = $processor->get_span(); 2484 2485 // Remove the section and continue. 2486 $output .= substr( $content, $at, $footnote_opening->start - $at ); 2487 $at = $footnote_closing->start + $footnote_closing->length; 2488 } 2489 2490 // No footnotes were found and removed: return the original input. 2491 if ( 0 === $at ) { 2492 return $content; 2493 } 2494 2495 if ( $at < strlen( $content ) ) { 2496 $output .= substr( $content, $at ); 2497 } 2498 2499 return $output; 2377 2500 } 2378 2501 -
trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php
r65388 r65560 957 957 * 958 958 * @return bool Whether a token was parsed. 959 * 960 * @phpstan-impure 959 961 */ 960 962 public function next_token(): bool { -
trunk/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php
r64439 r65560 17 17 */ 18 18 public function test_remove_footnotes( $expected, $content ) { 19 $this->assert Same( $expected, excerpt_remove_footnotes( $content ) );19 $this->assertEqualHTML( $expected, excerpt_remove_footnotes( $content ) ); 20 20 } 21 21
Note:
See TracChangeset
for help on using the changeset viewer.