3 namespace Drupal\Core\Render\Element;
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\Component\Utility\Html as HtmlUtility;
7 use Drupal\Core\Render\Markup;
8 use Drupal\Component\Utility\Xss;
9 use Drupal\Core\Template\Attribute;
12 * Provides a render element for any HTML tag, with properties and value.
15 * - #tag: The tag name to output.
16 * - #attributes: (array, optional) HTML attributes to apply to the tag. The
17 * attributes are escaped, see \Drupal\Core\Template\Attribute.
18 * - #value: (string, optional) A string containing the textual contents of
20 * - #noscript: (bool, optional) When set to TRUE, the markup
21 * (including any prefix or suffix) will be wrapped in a <noscript> element.
26 * '#type' => 'html_tag',
28 * '#value' => $this->t('Hello World'),
32 * @RenderElement("html_tag")
34 class HtmlTag extends RenderElement {
37 * Void elements do not contain values or closing tags.
38 * @see http://www.w3.org/TR/html5/syntax.html#syntax-start-tag
39 * @see http://www.w3.org/TR/html5/syntax.html#void-elements
41 static protected $voidElements = [
42 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
43 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr',
44 'rect', 'circle', 'polygon', 'ellipse', 'stop', 'use', 'path',
50 public function getInfo() {
51 $class = get_class($this);
54 [$class, 'preRenderConditionalComments'],
55 [$class, 'preRenderHtmlTag'],
63 * Pre-render callback: Renders a generic HTML tag with attributes.
65 * @param array $element
66 * An associative array containing:
67 * - #tag: The tag name to output. Typical tags added to the HTML HEAD:
68 * - meta: To provide meta information, such as a page refresh.
69 * - link: To refer to stylesheets and other contextual information.
70 * - script: To load JavaScript.
71 * The value of #tag is escaped.
72 * - #attributes: (optional) An array of HTML attributes to apply to the
73 * tag. The attributes are escaped, see \Drupal\Core\Template\Attribute.
74 * - #value: (optional) A string containing tag content, such as inline
75 * CSS. The value of #value will be XSS admin filtered if it is not safe.
76 * - #noscript: (optional) If TRUE, the markup (including any prefix or
77 * suffix) will be wrapped in a <noscript> element. (Note that passing
78 * any non-empty value here will add the <noscript> tag.)
82 public static function preRenderHtmlTag($element) {
83 $attributes = isset($element['#attributes']) ? new Attribute($element['#attributes']) : '';
85 // An HTML tag should not contain any special characters. Escape them to
86 // ensure this cannot be abused.
87 $escaped_tag = HtmlUtility::escape($element['#tag']);
88 $open_tag = '<' . $escaped_tag . $attributes;
89 $close_tag = '</' . $escaped_tag . ">\n";
90 // Construct a void element.
91 if (in_array($element['#tag'], self::$voidElements)) {
95 // Construct all other elements.
98 $markup = $element['#value'] instanceof MarkupInterface ? $element['#value'] : Xss::filterAdmin($element['#value']);
99 $element['#markup'] = Markup::create($markup);
101 $prefix = isset($element['#prefix']) ? $element['#prefix'] . $open_tag : $open_tag;
102 $suffix = isset($element['#suffix']) ? $close_tag . $element['#suffix'] : $close_tag;
103 if (!empty($element['#noscript'])) {
104 $prefix = '<noscript>' . $prefix;
105 $suffix .= '</noscript>';
107 $element['#prefix'] = Markup::create($prefix);
108 $element['#suffix'] = Markup::create($suffix);
113 * Pre-render callback: Renders #browsers into #prefix and #suffix.
115 * @param array $element
116 * A render array with a '#browsers' property. The '#browsers' property can
117 * contain any or all of the following keys:
118 * - 'IE': If FALSE, the element is not rendered by Internet Explorer. If
119 * TRUE, the element is rendered by Internet Explorer. Can also be a string
120 * containing an expression for Internet Explorer to evaluate as part of a
121 * conditional comment. For example, this can be set to 'lt IE 7' for the
122 * element to be rendered in Internet Explorer 6, but not in Internet
123 * Explorer 7 or higher. Defaults to TRUE.
124 * - '!IE': If FALSE, the element is not rendered by browsers other than
125 * Internet Explorer. If TRUE, the element is rendered by those browsers.
128 * - To render an element in all browsers, '#browsers' can be left out or set
129 * to array('IE' => TRUE, '!IE' => TRUE).
130 * - To render an element in Internet Explorer only, '#browsers' can be set
131 * to array('!IE' => FALSE).
132 * - To render an element in Internet Explorer 6 only, '#browsers' can be set
133 * to array('IE' => 'lt IE 7', '!IE' => FALSE).
134 * - To render an element in Internet Explorer 8 and higher and in all other
135 * browsers, '#browsers' can be set to array('IE' => 'gte IE 8').
138 * The passed-in element with markup for conditional comments potentially
139 * added to '#prefix' and '#suffix'.
141 public static function preRenderConditionalComments($element) {
142 $browsers = isset($element['#browsers']) ? $element['#browsers'] : [];
148 // If rendering in all browsers, no need for conditional comments.
149 if ($browsers['IE'] === TRUE && $browsers['!IE']) {
153 // Determine the conditional comment expression for Internet Explorer to
155 if ($browsers['IE'] === TRUE) {
158 elseif ($browsers['IE'] === FALSE) {
162 // The IE expression might contain some user input data.
163 $expression = Xss::filterAdmin($browsers['IE']);
166 // If the #prefix and #suffix properties are used, wrap them with
167 // conditional comment markup. The conditional comment expression is
168 // evaluated by Internet Explorer only. To control the rendering by other
169 // browsers, use either the "downlevel-hidden" or "downlevel-revealed"
170 // technique. See http://wikipedia.org/wiki/Conditional_comment
173 // Ensure what we are dealing with is safe.
174 // This would be done later anyway in drupal_render().
175 $prefix = isset($element['#prefix']) ? $element['#prefix'] : '';
176 if ($prefix && !($prefix instanceof MarkupInterface)) {
177 $prefix = Xss::filterAdmin($prefix);
179 $suffix = isset($element['#suffix']) ? $element['#suffix'] : '';
180 if ($suffix && !($suffix instanceof MarkupInterface)) {
181 $suffix = Xss::filterAdmin($suffix);
184 // We ensured above that $expression is either a string we created or is
185 // admin XSS filtered, and that $prefix and $suffix are also admin XSS
186 // filtered if they are unsafe. Thus, all these strings are safe.
187 if (!$browsers['!IE']) {
188 // "downlevel-hidden".
189 $element['#prefix'] = Markup::create("\n<!--[if $expression]>\n" . $prefix);
190 $element['#suffix'] = Markup::create($suffix . "<![endif]-->\n");
193 // "downlevel-revealed".
194 $element['#prefix'] = Markup::create("\n<!--[if $expression]><!-->\n" . $prefix);
195 $element['#suffix'] = Markup::create($suffix . "<!--<![endif]-->\n");