HtmlTag::preRenderConditionalComments

public static HtmlTag::preRenderConditionalComments($element)

Pre-render callback: Renders #browsers into #prefix and #suffix.

Parameters

array $element: A render array with a '#browsers' property. The '#browsers' property can contain any or all of the following keys:

  • 'IE': If FALSE, the element is not rendered by Internet Explorer. If TRUE, the element is rendered by Internet Explorer. Can also be a string containing an expression for Internet Explorer to evaluate as part of a conditional comment. For example, this can be set to 'lt IE 7' for the element to be rendered in Internet Explorer 6, but not in Internet Explorer 7 or higher. Defaults to TRUE.
  • '!IE': If FALSE, the element is not rendered by browsers other than Internet Explorer. If TRUE, the element is rendered by those browsers. Defaults to TRUE.

Examples:

  • To render an element in all browsers, '#browsers' can be left out or set to array('IE' => TRUE, '!IE' => TRUE).
  • To render an element in Internet Explorer only, '#browsers' can be set to array('!IE' => FALSE).
  • To render an element in Internet Explorer 6 only, '#browsers' can be set to array('IE' => 'lt IE 7', '!IE' => FALSE).
  • To render an element in Internet Explorer 8 and higher and in all other browsers, '#browsers' can be set to array('IE' => 'gte IE 8').

Return value

array The passed-in element with markup for conditional comments potentially added to '#prefix' and '#suffix'.

File

core/lib/Drupal/Core/Render/Element/HtmlTag.php, line 134

Class

HtmlTag
Provides a render element for any HTML tag, with properties and value.

Namespace

Drupal\Core\Render\Element

Code

public static function preRenderConditionalComments($element) {
  $browsers = isset($element['#browsers']) ? $element['#browsers'] : array();
  $browsers += array(
    'IE' => TRUE,
    '!IE' => TRUE,
  );

  // If rendering in all browsers, no need for conditional comments.
  if ($browsers['IE'] === TRUE && $browsers['!IE']) {
    return $element;
  }

  // Determine the conditional comment expression for Internet Explorer to
  // evaluate.
  if ($browsers['IE'] === TRUE) {
    $expression = 'IE';
  }
  elseif ($browsers['IE'] === FALSE) {
    $expression = '!IE';
  }
  else {
    // The IE expression might contain some user input data.
    $expression = Xss::filterAdmin($browsers['IE']);
  }

  // If the #prefix and #suffix properties are used, wrap them with
  // conditional comment markup. The conditional comment expression is
  // evaluated by Internet Explorer only. To control the rendering by other
  // browsers, use either the "downlevel-hidden" or "downlevel-revealed"
  // technique. See http://wikipedia.org/wiki/Conditional_comment
  // for details.

  // Ensure what we are dealing with is safe.
  // This would be done later anyway in drupal_render().
  $prefix = isset($element['#prefix']) ? $element['#prefix'] : '';
  if ($prefix && !($prefix instanceof MarkupInterface)) {
    $prefix = Xss::filterAdmin($prefix);
  }
  $suffix = isset($element['#suffix']) ? $element['#suffix'] : '';
  if ($suffix && !($suffix instanceof MarkupInterface)) {
    $suffix = Xss::filterAdmin($suffix);
  }

  // We ensured above that $expression is either a string we created or is
  // admin XSS filtered, and that $prefix and $suffix are also admin XSS
  // filtered if they are unsafe. Thus, all these strings are safe.
  if (!$browsers['!IE']) {
    // "downlevel-hidden".
    $element['#prefix'] = Markup::create("\n<!--[if $expression]>\n" . $prefix);
    $element['#suffix'] = Markup::create($suffix . "<![endif]-->\n");
  }
  else {
    // "downlevel-revealed".
    $element['#prefix'] = Markup::create("\n<!--[if $expression]><!-->\n" . $prefix);
    $element['#suffix'] = Markup::create($suffix . "<!--<![endif]-->\n");
  }

  return $element;
}
doc_Drupal
2016-10-29 09:18:56
Comments
Leave a Comment

Please login to continue.