backdrop-issues icon indicating copy to clipboard operation
backdrop-issues copied to clipboard

[DX] Replace use of `strtr()` in `theme_form_element()` with standardized `backdrop_html_class()` instead.

Open klonos opened this issue 1 year ago • 4 comments

This came up in the PR I created for #1403. The proposed changes were the following:

if (!empty($element['#type'])) {
-    $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
+    $attributes['class'][] = backdrop_html_class('form-type-' . $element['#type']);

@herbdool raised a cocern:

Do we know for sure that this change will result in equivalent classes? This seems like a potentially breaking change.

@klonos:

Lets see:

  • the implementation of strtr() simply replaces underscores with dashes.
  • backdrop_html_class() does the following:
    • passes everything through backdrop_strtolower(), and according to https://stackoverflow.com/questions/12533926/are-class-names-in-css-selectors-case-sensitive:
      • CSS selectors are generally case-insensitive; this includes class and ID selectors 👍🏼
      • But HTML class names are case-sensitive (see the attribute definition) ... This has not changed in HTML5.1 👎🏼
    • passes everything through backdrop_clean_css_identifier(), which converts underscores (among other things) to dashes.

So yes, this is a potentially breaking change, but only in theory. For that to happen, someone needs to have declared '#type' for an element using uppercase letters, in which case, I believe that there would be errors thrown or the type would simply not be recognized. Since the scope of the $element['#type'] that is passed to strtr() is limited (only element types that have been officially specified in the Form API), I believe that this is safe to do, and it provides standardization and future-proofing.

In the same PR, I also proposed a similar change for the class that is add based on the element name:

if (!empty($element['#name'])) {
-    $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
+    $attributes['class'][] = backdrop_html_class('form-item-' . $element['#name']);

This is a follow-up issue to discuss if we should make these changes or not.

klonos avatar Jun 20 '24 10:06 klonos

2-line PR here: https://github.com/backdrop/backdrop/pull/4800

klonos avatar Jun 20 '24 10:06 klonos

...as an alternative, I'm happy to add a couple of @todos there instead, as reminder to clean that up in Backdrop 2.0.

klonos avatar Jun 20 '24 10:06 klonos

@klonos The preview site has not been created, for this PR.

avpaderno avatar Jun 25 '24 18:06 avpaderno

I did not know I could close and re-open a pull request. I did it, but it fails to create the preview site.

avpaderno avatar Jun 25 '24 18:06 avpaderno