function AttributeTest::providerTestAttributeValues

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::providerTestAttributeValues()
  2. 10 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::providerTestAttributeValues()
  3. 9 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::providerTestAttributeValues()
  4. 8.9.x core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::providerTestAttributeValues()

File

core/tests/Drupal/Tests/Core/Template/AttributeTest.php, line 390

Class

AttributeTest
Tests Drupal\Core\Template\Attribute.

Namespace

Drupal\Tests\Core\Template

Code

public static function providerTestAttributeValues() : array {
  $data = [
    'HTML-encoded attribute name' => [
      [
        '&"\'<>' => 'value',
      ],
      ' &amp;&quot;&#039;&lt;&gt;="value"',
    ],
    'HTML-encoded attribute value' => [
      [
        'title' => '&"\'<>',
      ],
      ' title="&amp;&quot;&#039;&lt;&gt;"',
    ],
    'multi-value attribute' => [
      [
        'class' => [
          'first',
          'last',
        ],
      ],
      ' class="first last"',
    ],
    'true boolean attribute' => [
      [
        'disabled' => TRUE,
      ],
      ' disabled',
    ],
    'false boolean attribute' => [
      [
        'disabled' => FALSE,
      ],
      '',
    ],
    'empty attribute value' => [
      [
        'alt' => '',
      ],
      ' alt=""',
    ],
    'null attribute value' => [
      [
        'alt' => NULL,
      ],
      '',
    ],
    'multiple attributes' => [
      [
        'id' => 'id-test',
        'class' => [
          'first',
          'last',
        ],
        'alt' => 'Alternate',
      ],
      ' id="id-test" class="first last" alt="Alternate"',
    ],
    'empty attributes array' => [
      [],
      '',
    ],
  ];
  $string = '"> <script>alert(123)</script>"';
  $data['safe-object-xss1'] = [
    [
      'title' => Markup::create($string),
    ],
    ' title="&quot;&gt; alert(123)&quot;"',
  ];
  $data['non-safe-object-xss1'] = [
    [
      'title' => $string,
    ],
    ' title="' . Html::escape($string) . '"',
  ];
  $string = '&quot;><script>alert(123)</script>';
  $data['safe-object-xss2'] = [
    [
      'title' => Markup::create($string),
    ],
    ' title="&quot;&gt;alert(123)"',
  ];
  $data['non-safe-object-xss2'] = [
    [
      'title' => $string,
    ],
    ' title="' . Html::escape($string) . '"',
  ];
  // \Twig\Markup objects are generated when using twig defined variables
  // like `{% set xxx %}Foo{% endset %}`.
  $data['twig-markup'] = [
    [
      'title' => new TwigMarkup('foo', 'UTF-8'),
    ],
    ' title="foo"',
  ];
  return $data;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.