Revisions: Generate correct number of columns in wp_text_diff.

The function `wp_text_diff` generated an invalid table structure if the $args parameter contained any values. This patch corrects the structure generated by `wp_text_diff` and related usages so that the column count matches the data generated. Additionally, this patch passes arguments to the Revisions screen so that the screen has column headings that reflect the content in each column. Improves the accessibility and usability of the Revisions table.

Props joedolson, mehulkaklotar, afercia, adamsilverstein, zodiac1978, jeremyfelt
Fixes #25473

git-svn-id: https://develop.svn.wordpress.org/trunk@50034 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Dolson 2021-01-27 21:52:10 +00:00
parent bc3177153d
commit 8023f34a6a
3 changed files with 23 additions and 16 deletions

View File

@ -86,6 +86,8 @@ function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
$args = array(
'show_split_view' => true,
'title_left' => __( 'Removed' ),
'title_right' => __( 'Added' ),
);
/**

View File

@ -176,7 +176,7 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
}
if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->emptyLine() . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
$r .= '<tr>' . $this->emptyLine() . $this->addedLine( $line ) . "</tr>\n";
} else {
$r .= '<tr>' . $this->addedLine( $line ) . "</tr>\n";
}
@ -201,7 +201,7 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
$line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'deleted' );
}
if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . $this->emptyLine() . "</tr>\n";
$r .= '<tr>' . $this->deletedLine( $line ) . $this->emptyLine() . "</tr>\n";
} else {
$r .= '<tr>' . $this->deletedLine( $line ) . "</tr>\n";
}
@ -226,7 +226,7 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
$line = apply_filters( 'process_text_diff_html', $processed_line, $line, 'unchanged' );
}
if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->contextLine( $line ) . $this->emptyLine() . $this->contextLine( $line ) . "</tr>\n";
$r .= '<tr>' . $this->contextLine( $line ) . $this->contextLine( $line ) . "</tr>\n";
} else {
$r .= '<tr>' . $this->contextLine( $line ) . "</tr>\n";
}
@ -319,7 +319,7 @@ class WP_Text_Diff_Renderer_Table extends Text_Diff_Renderer {
$r .= $this->_deleted( array( $orig_line ), false );
} else { // A true changed row.
if ( $this->_show_split_view ) {
$r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->emptyLine() . $this->addedLine( $final_line ) . "</tr>\n";
$r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";
} else {
$r .= '<tr>' . $this->deletedLine( $orig_line ) . '</tr><tr>' . $this->addedLine( $final_line ) . "</tr>\n";
}

View File

@ -2838,27 +2838,32 @@ if ( ! function_exists( 'wp_text_diff' ) ) :
return '';
}
$r = "<table class='diff'>\n";
$is_split_view = ! empty( $args['show_split_view'] );
$is_split_view_class = $is_split_view ? ' is-split-view' : '';
if ( ! empty( $args['show_split_view'] ) ) {
$r .= "<col class='content diffsplit left' /><col class='content diffsplit middle' /><col class='content diffsplit right' />";
} else {
$r .= "<col class='content' />";
$r = "<table class='diff$is_split_view_class'>\n";
if ( $args['title'] ) {
$r .= "<caption class='diff-title'>$args[title]</caption>\n";
}
if ( $args['title'] || $args['title_left'] || $args['title_right'] ) {
if ( $args['title_left'] || $args['title_right'] ) {
$r .= '<thead>';
}
if ( $args['title'] ) {
$r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
}
if ( $args['title_left'] || $args['title_right'] ) {
$th_or_td_left = empty( $args['title_left'] ) ? 'td' : 'th';
$th_or_td_right = empty( $args['title_right'] ) ? 'td' : 'th';
$r .= "<tr class='diff-sub-title'>\n";
$r .= "\t<td></td><th>$args[title_left]</th>\n";
$r .= "\t<td></td><th>$args[title_right]</th>\n";
$r .= "\t<$th_or_td_left>$args[title_left]</$th_or_td_left>\n";
if ( $is_split_view ) {
$r .= "\t<$th_or_td_right>$args[title_right]</$th_or_td_right>\n";
}
$r .= "</tr>\n";
}
if ( $args['title'] || $args['title_left'] || $args['title_right'] ) {
if ( $args['title_left'] || $args['title_right'] ) {
$r .= "</thead>\n";
}