Skip to content

Commit

Permalink
Generate valid CSS/class name for custom status with spaces
Browse files Browse the repository at this point in the history
Prior to this, for a custom status 'in progress', Mantis would generate
the following invalid code:

- HTML (class name): class="small-caption in progress-color"
- Dynamic CSS (status_config.php): .in progress-color { ... }

We now rely on a new API function html_get_css_identifier() to generate
a valid CSS class name by replacing invalid characters with '-'.

Fixes #20068
  • Loading branch information
dregad committed Oct 24, 2015
1 parent 5e12bf9 commit 94aa005
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
38 changes: 33 additions & 5 deletions core/html_api.php
Expand Up @@ -1431,9 +1431,12 @@ function html_status_legend( $p_display_position, $p_restrict_by_filter = false
$t_status_enum_string = config_get( 'status_enum_string' );
foreach( $t_status_array as $t_status => $t_name ) {
$t_val = isset( $t_status_names[$t_status] ) ? $t_status_names[$t_status] : $t_status_array[$t_status];
$t_status_label = MantisEnum::getLabel( $t_status_enum_string, $t_status );
$t_status_css = html_get_css_identifier(
MantisEnum::getLabel( $t_status_enum_string, $t_status ),
'color'
);

echo '<td class="small-caption ' . $t_status_label . '-color">' . $t_val . '</td>';
echo '<td class="small-caption ' . $t_status_css . '">' . $t_val . '</td>';
}

echo '</tr>';
Expand Down Expand Up @@ -1471,8 +1474,10 @@ function html_status_percentage_legend() {
$t_percent = ( isset( $t_status_percents[$t_status] ) ? $t_status_percents[$t_status] : 0 );

if( $t_percent > 0 ) {
$t_status_label = MantisEnum::getLabel( $t_status_enum_string, $t_status );
echo '<td class="small-caption-center ' . $t_status_label . '-color ' . $t_status_label . '-percentage">' . $t_percent . '%</td>';
$t_status_css = html_get_css_identifier(
MantisEnum::getLabel( $t_status_enum_string, $t_status )
);
echo '<td class="small-caption-center ' . $t_status_css . '-color ' . $t_status_css . '-percentage">' . $t_percent . '%</td>';
}
}

Expand Down Expand Up @@ -1906,5 +1911,28 @@ function html_buttons_view_bug_page( $p_bug_id ) {
* Build CSS including project or even user-specific colors ?
*/
function html_get_status_css_class( $p_status, $p_user = null, $p_project = null ) {
return string_attribute( MantisEnum::getLabel( config_get( 'status_enum_string', null, $p_user, $p_project ), $p_status ) . '-color' );
$t_identifier = MantisEnum::getLabel(
config_get( 'status_enum_string', null, $p_user, $p_project ),
$p_status
);
return html_get_css_identifier( $t_identifier, 'color' );
}

/**
* Get a CSS-friendly identifier for the given string
* Replace all invalid characters by a dash, remove multiple consecutive dashes
* and append the given suffix.
* This is useful e.g. for dynamic css class names used for status colors.
* @param string $p_string Identifier to convert
* @param string $p_string Suffix to append at end of string
* @return string
*/
function html_get_css_identifier( $p_string, $p_suffix = null ) {
$t_string = string_attribute( strtolower ( $p_string ) );
$t_string = preg_replace( '/[^a-z0-9_-]/', '-', $t_string );
if( $p_suffix ) {
$t_string .= '-' . $p_suffix;
}
$t_string = preg_replace( '/-+/', '-', trim( $t_string, '-' ) );
return $t_string;
}
6 changes: 4 additions & 2 deletions css/status_config.php
Expand Up @@ -77,9 +77,11 @@

foreach( $t_statuses AS $t_id=>$t_label ) {
if( array_key_exists( $t_label, $t_colors ) ) {
echo ".$t_label-color { background-color: {$t_colors[$t_label]}; width: $t_color_width%; }\n";
echo '.' . html_get_css_identifier( $t_label, 'color' )
. " { background-color: {$t_colors[$t_label]}; width: $t_color_width%; }\n";
}
if( array_key_exists( $t_id, $t_status_percents ) ) {
echo ".$t_label-percentage { width: {$t_status_percents[$t_id]}%; }\n";
echo '.' . html_get_css_identifier( $t_label, 'percentage' )
. " { width: {$t_status_percents[$t_id]}%; }\n";
}
}

0 comments on commit 94aa005

Please sign in to comment.