As mentioned by @merlinofchaos in #1441218-28: Use keyword replacements for CSS settings.

This is necessary to allow context keyword replacement for CSS values to be safe.

Patch to follow shortly.

CommentFileSizeAuthor
#1 ctools-css-safe-1944712-0.patch869 bytesmalberts
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

malberts’s picture

Status: Active » Needs review
FileSize
869 bytes

This patch only converts the value to be CSS-safe.

[edit]
If this value is to be used as an HTML id or class, further processing is needed.
In the case of an id, drupal_html_id() needs to be called on the resulting value.
In the case of a class, drupal_strtolower() needs to be called instead of drupal_html_class(). The latter does only that in addition to drupal_clean_css_identifier() which is done in this patch.

geek-merlin’s picture

i see some issues with this:
* the standard filter of drupal_clean_css_identifier() is: array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '')
* we should not filter out the underscore it is legal and has legitimate use cases (also see the d8 changenotice that underscores in nodetypes are no longer replaced with dashes in urls)
* we should not filter spaces and offer the possibility to pass multiple css classes (do wen need 'single clss safe' vs 'multiple css safe' options?)

malberts’s picture

* we should not filter out the underscore it is legal and has legitimate use cases (also see the d8 changenotice that underscores in nodetypes are no longer replaced with dashes in urls)

I allowed it primarily because the code mentions using Drupal's coding standards by default. However, I did not look at any D8 changes at that time and now I read #1109854: Overly aggressive transliteration in drupal_clean_css_identifier removes underscores from classes which highlights that issue. I also see now that Views allows the underscore with views_clean_css_identifier().

* we should not filter spaces and offer the possibility to pass multiple css classes (do wen need 'single clss safe' vs 'multiple css safe' options?)

I think a more flexible way is to pass in a seperator string . Either as an additional (but optional) option "css safe separator" or directly as part of "css safe". This can be used to explode(), convert and implode(). This should allow a more flexible multiple split when necessary, e.g. "Project Issue|Allow context keyword blah blah" => "project-issue allow-context-keyword-blah-blah" when you want only some space-separated substrings to become separate classes.

Off the top of my head, maybe something like:

if (!empty($converter_options['css safe'])) {
  if (is_string($converter_options['css safe'])) {
    $classes = explode($converter_options['css safe'], $value);
    foreach ($classes as &$class) {
      $class = drupal_clean_css_identifier($class, array(' ' => '-', '/' => '-', '[' => '-', ']' => ''));
    }
    $value = implode(' ', $classes);
  }
  else {
    $value = drupal_clean_css_identifier($value, array(' ' => '-', '/' => '-', '[' => '-', ']' => ''));
  }
}

So for single split use 'css safe' => TRUE and for multiple split use 'css safe' => '|'.

The idea where this patch came from is that you can use Ctools keywords to create class names in Panels. So if I wanted to use, e.g. "%node:type %node:title" it would give me "project-issue allow-context-keyword-blah-blah", where each keyword gets changed into a single class. I was thinking mostly in turns of 1 keyword = 1 class. But for example, if somebody wants to turn a multi-value term reference on a node into separate classes then it won't be possible. Though not a Ctools issue, this freedom makes the Panels issue (and other possible UI instances) difficult because now we will have to offer the user a choice between converting to single or multiple, and whether to do this per Ctools keyword or for the entire string of keywords. However, Ctools would then at least support it properly and the current Panels patch will still be OK but without the flexibility.

What do you think?

Fidelix’s picture

Issue summary: View changes

For reference, this Clean Markup patch has a soft-dependency on this issue:
#2015305: Clean markup: Use keyword replacements for CSS settings

DamienMcKenna’s picture

At what point should this work around standard logic found in core?

I suggest checking with japerry and/or merlinofchaos on whether this should just rely on what is already in core or if it should copy the function in Views (possibly replacing the function from Views).

Chris Matthews’s picture

Status: Needs review » Needs work

The 6 year old initial patch to context.inc still applies cleanly to the latest 7.x-1.x-dev, but I'm changing the status to 'Needs work' per the follow up comments in this thread.

ctools-css-safe-1944712-0.patch:19: trailing whitespace.
  
Checking patch includes/context.inc...
Hunk #1 succeeded at 875 (offset 339 lines).
Applied patch includes/context.inc cleanly.
warning: 1 line adds whitespace errors.