getInfo(); $this->assertArrayHasKey('#pre_render', $info); $this->assertArrayHasKey('#attributes', $info); $this->assertArrayHasKey('#value', $info); } /** * @covers ::preRenderHtmlTag * @dataProvider providerPreRenderHtmlTag */ public function testPreRenderHtmlTag($element, $expected) { $result = HtmlTag::preRenderHtmlTag($element); foreach ($result as &$child) { if (is_array($child) && isset($child['#tag'])) { $child = HtmlTag::preRenderHtmlTag($child); } } $this->assertEquals($expected, (string) $this->renderer->renderRoot($result)); } /** * Data provider for preRenderHtmlTag test. */ public function providerPreRenderHtmlTag() { $tags = []; // Value prefix/suffix. $element = [ '#value' => 'value', '#tag' => 'p', ]; $tags['value'] = [$element, '

value

' . "\n"]; // Normal element without a value should not result in a void element. $element = [ '#tag' => 'p', '#value' => NULL, ]; $tags['no-value'] = [$element, "

\n"]; // A void element. $element = [ '#tag' => 'br', ]; $tags['void-element'] = [$element, "
\n"]; // Attributes. $element = [ '#tag' => 'div', '#attributes' => ['class' => 'test', 'id' => 'id'], '#value' => 'value', ]; $tags['attributes'] = [$element, '
value
' . "\n"]; // No script tags. $element['#noscript'] = TRUE; $tags['noscript'] = [$element, '']; // Ensure that #tag is sanitised. $element = [ '#tag' => 'p> 'value', ]; $tags['sanitized-tag'] = [$element, "value\n"]; // Ensure that #value is not filtered if it is marked as safe. $element = [ '#tag' => 'p', '#value' => Markup::create(''), ]; $tags['value-safe'] = [$element, "

\n"]; // Ensure that #value is filtered if it is not safe. $element = [ '#tag' => 'p', '#value' => '', ]; $tags['value-not-safe'] = [$element, "

value

\n"]; // Ensure that nested render arrays render properly. $element = [ '#tag' => 'p', '#value' => NULL, [ ['#markup' => 'value1'], ['#markup' => 'value2'], ], ]; $tags['nested'] = [$element, "

value1value2

\n"]; // Ensure svg elements. $element = [ '#tag' => 'rect', '#attributes' => [ 'width' => 25, 'height' => 25, 'x' => 5, 'y' => 10, ], ]; $tags['rect'] = [$element, '' . "\n"]; $element = [ '#tag' => 'circle', '#attributes' => [ 'cx' => 100, 'cy' => 100, 'r' => 100, ], ]; $tags['circle'] = [$element, '' . "\n"]; $element = [ '#tag' => 'polygon', '#attributes' => [ 'points' => '60,20 100,40 100,80 60,100 20,80 20,40', ], ]; $tags['polygon'] = [$element, '' . "\n"]; $element = [ '#tag' => 'ellipse', '#attributes' => [ 'cx' => 60, 'cy' => 60, 'rx' => 50, 'ry' => 25, ], ]; $tags['ellipse'] = [$element, '' . "\n"]; $element = [ '#tag' => 'use', '#attributes' => [ 'x' => 50, 'y' => 10, 'width' => 50, 'height' => 50, ], ]; $tags['use'] = [$element, '' . "\n"]; $element = [ '#tag' => 'path', '#attributes' => [ 'd' => 'M 100 100 L 300 100 L 200 300 z', 'fill' => 'orange', 'stroke' => 'black', 'stroke-width' => 3, ], ]; $tags['path'] = [$element, '' . "\n"]; $element = [ '#tag' => 'stop', '#attributes' => [ 'offset' => '5%', 'stop-color' => '#F60', ], ]; $tags['stop'] = [$element, '' . "\n"]; // Nested svg elements. $element = [ '#tag' => 'linearGradient', '#value' => NULL, [ '#tag' => 'stop', '#value' => NULL, '#attributes' => [ 'offset' => '5%', 'stop-color' => '#F60', ], ], [ '#tag' => 'stop', '#value' => NULL, '#attributes' => [ 'offset' => '95%', 'stop-color' => '#FF6', ], ], ]; $tags['linearGradient'] = [$element, '' . "\n" . '' . "\n" . '' . "\n"]; // Simple link. $element = [ '#tag' => 'link', ]; $tags['link'] = [HtmlTag::preRenderConditionalComments($element), '' . "\n"]; // Conditional link. $element = [ '#tag' => 'link', '#browsers' => [ 'IE' => TRUE, '!IE' => FALSE, ], ]; $tags['conditional-link'] = [HtmlTag::preRenderConditionalComments($element), "\n" . '' . "\n"]; return $tags; } /** * @covers ::preRenderConditionalComments * @dataProvider providerPreRenderConditionalComments */ public function testPreRenderConditionalComments($element, $expected, $set_safe = FALSE) { if ($set_safe) { $element['#prefix'] = Markup::create($element['#prefix']); $element['#suffix'] = Markup::create($element['#suffix']); } $this->assertEquals($expected, HtmlTag::preRenderConditionalComments($element)); } /** * Data provider for conditional comments test. */ public function providerPreRenderConditionalComments() { // No browser specification. $element = [ '#tag' => 'link', ]; $tags['no-browser'] = [$element, $element]; // Specify all browsers. $element['#browsers'] = [ 'IE' => TRUE, '!IE' => TRUE, ]; $tags['all-browsers'] = [$element, $element]; // All IE. $element = [ '#tag' => 'link', '#browsers' => [ 'IE' => TRUE, '!IE' => FALSE, ], ]; $expected = $element; $expected['#prefix'] = "\n\n"; $tags['all-ie'] = [$element, $expected]; // Exclude IE. $element = [ '#tag' => 'link', '#browsers' => [ 'IE' => FALSE, ], ]; $expected = $element; $expected['#prefix'] = "\n\n"; $expected['#suffix'] = "\n"; $tags['no-ie'] = [$element, $expected]; // IE gt 8 $element = [ '#tag' => 'link', '#browsers' => [ 'IE' => 'gt IE 8', ], ]; $expected = $element; $expected['#prefix'] = "\n\n"; $expected['#suffix'] = "\n"; $tags['ie9plus'] = [$element, $expected]; // Prefix and suffix filtering if not safe. $element = [ '#tag' => 'link', '#browsers' => [ 'IE' => FALSE, ], '#prefix' => 'prefix', '#suffix' => 'suffix', ]; $expected = $element; $expected['#prefix'] = "\n\nprefix"; $expected['#suffix'] = "suffix\n"; $tags['non-ie-unsafe'] = [$element, $expected]; // Prefix and suffix filtering if marked as safe. This has to come after the // previous test case. $expected['#prefix'] = "\n\nprefix"; $expected['#suffix'] = "suffix\n"; $tags['non-ie-safe'] = [$element, $expected, TRUE]; return $tags; } }