You are here

public function DocumentElement::getText in Drupal 9

File

core/tests/Drupal/Tests/DocumentElement.php, line 51

Class

DocumentElement
Document element.

Namespace

Drupal\Tests

Code

public function getText() {
  if ($this
    ->getDriver() instanceof BrowserKitDriver) {

    // Work around https://github.com/minkphp/MinkBrowserKitDriver/issues/153.
    // To simulate what the user sees, it removes:
    // - all text inside the head tags
    // - Drupal settings json.
    $raw_content = preg_replace([
      '@<head>(.+?)</head>@si',
      '@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@',
    ], '', $this
      ->getContent());

    // Filter out all HTML tags, as they are not visible in a normal browser.
    $text = strip_tags($raw_content);

    // To preserve BC and match \Behat\Mink\Element\Element::getText() include
    // the page title.
    $title_element = $this
      ->find('css', 'title');
    if ($title_element) {
      $text = $title_element
        ->getText() . ' ' . $text;
    }

    // To match what the user sees and \Behat\Mink\Element\Element::getText()
    // decode HTML entities.
    $text = html_entity_decode($text, ENT_QUOTES);

    // To match \Behat\Mink\Element\Element::getText() remove new lines and
    // normalize spaces.
    $text = str_replace("\n", ' ', $text);
    $text = preg_replace('/ {2,}/', ' ', $text);
    return trim($text);
  }

  // If using a real browser fallback to the \Behat\Mink\Element\Element
  // implementation.
  return parent::getText();
}