You are here

public static function AmpCustomStyle::preRenderHtmlTag in Accelerated Mobile Pages (AMP) 8.3

Pre-render callback: Renders a generic HTML tag with attributes.

Parameters

array $element: An associative array containing:

  • #tag: The tag name to output. Typical tags added to the HTML HEAD:

    • meta: To provide meta information, such as a page refresh.
    • link: To refer to stylesheets and other contextual information.
    • script: To load JavaScript.

    The value of #tag is escaped.

  • #attributes: (optional) An array of HTML attributes to apply to the tag. The attributes are escaped, see \Drupal\Core\Template\Attribute.
  • #value: (optional) A string containing tag content, such as inline CSS. The value of #value will be XSS admin filtered if it is not safe.
  • #noscript: (optional) If TRUE, the markup (including any prefix or suffix) will be wrapped in a <noscript> element. (Note that passing any non-empty value here will add the <noscript> tag.)

Return value

array

Overrides HtmlTag::preRenderHtmlTag

File

src/Element/AmpCustomStyle.php, line 26

Class

AmpCustomStyle
Provides a render element for the amp-custom style tag.

Namespace

Drupal\amp\Element

Code

public static function preRenderHtmlTag($element) {

  // An HTML tag should not contain any special characters. Escape them to
  // ensure this cannot be abused.
  $escaped_tag = HtmlUtility::escape($element['#tag']);

  // We can't pass amp-custom in as an attribute, a key/value pair will not
  // validate, we must force it to be rendered <style amp-custom>...</style>.
  $open_tag = '<' . $escaped_tag . ' amp-custom>';
  $close_tag = '</' . $escaped_tag . ">\n";

  // Avoid escaping valid css attributes.
  // For instance '.content > li' would be converted to '.content &gt; li'
  $markup = $element['#value'];
  $markup = str_replace('>', 'xxxxxxxxxx', $markup);
  $markup = $markup instanceof MarkupInterface ? $markup : Xss::filterAdmin($markup);
  $markup = str_replace('xxxxxxxxxx', '>', $markup);
  $markup = Markup::create($markup);

  // Avoid re-escaping valid css attributes in later sanitization if $markup
  // is set in #markup by setting the value in #children instead.
  $element['#markup'] = "\n";
  $element['#children'] = $markup;
  $element['#prefix'] = Markup::create($open_tag);
  $element['#suffix'] = Markup::create($close_tag);
  return $element;
}