Making WordPress.org


Ignore:
Timestamp:
03/18/2020 04:11:37 AM (5 years ago)
Author:
dd32
Message:

Support: Attempt to fix standalone <li> tags, take two.

This change operates a little differently to [9438] in which it operates on chunks of the content instead, to treat standalone <li>s differntly than those wrapped in <ul> or <ol> tags.

Unfortunately this doesn't support nested lists, and instead attempts to convert those to non-nested lists.

Fixes #20.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-support/functions.php

    r9594 r9601  
    605605 */
    606606function wporg_support_wrap_standalone_li_tags_in_ul( $content ) {
    607     if ( false !== strpos( $content, '<li>' ) ) {
    608         $content = preg_replace( '#(?<!<ul>\s|<ol>\s)<li>#', '<ul><li>', $content );
    609         $content = force_balance_tags( $content );
     607    // No lists? No worries.
     608    if ( false === stripos( $content, '<li>' ) ) {
     609        return $content;
     610    }
     611
     612    // Split the content into chunks of <Not a List, OL/UL tags, Content of Ol/UL tags>.
     613    $parts = preg_split( '#(<[uo]l(?:\s+[^>]+)?>)#im', $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
     614
     615    // We can rebuild.
     616    $content = '';
     617
     618    for ( $i = 0; $i < count( $parts ); $i++ ) {
     619        $part = $parts[ $i ];
     620        $next_part = $parts[ $i + 1 ] ?? '';
     621
     622        // If the chunk is a list element, process it as such.
     623        if ( preg_match( '#^<([uo]l)(\s+[^>]+)?>#i', $part, $m ) ) {
     624            $closing_tag_pos = stripos( $next_part, '</' . $m[1] . '>' );
     625            if ( false !== $closing_tag_pos ) {
     626                // List is closed, assume that part of the content is OK
     627                $content .= $part . substr( $next_part, 0, $closing_tag_pos + 5 );
     628
     629                // But check the content after the list too.
     630                $next_part = substr( $next_part, $closing_tag_pos + 5 );
     631                if ( $next_part ) {
     632                    // Replace the first li tag if one exists.
     633                    // This may break nested lists, but that's okay.
     634                    $next_part = preg_replace( '#<li>#i', '<ul><li>', $next_part, 1 );
     635
     636                    $content .= force_balance_tags( $next_part );
     637                }
     638            } else {
     639                // List is not closed, balance it.
     640                $content .= force_balance_tags( $part . $next_part );
     641            }
     642            $i++; // Skip $next_part;
     643
     644        // Does this text chunk contain a <li>? If so, it's got no matching start element.
     645        } elseif ( false !== stripos( $part, '<li>' ) ) {
     646            $part = preg_replace( '#<li>#i', '<ul><li>', $part, 1 ); // Replace the first li tag
     647            $content .= force_balance_tags( $part );
     648
     649        // This shouldn't actually be hit, but is here for completeness.
     650        } else {
     651            $content .= force_balance_tags( $part );
     652        }
    610653    }
    611654
Note: See TracChangeset for help on using the changeset viewer.