You are here

protected function StaticGenerator::getHtmlAssets in Tome 8

Finds and exports assets for the given HTML content.

Parameters

string $content: An HTML string.

string $root: A root path to resolve relative paths.

Return value

array An array of paths found in the given HTML string.

1 call to StaticGenerator::getHtmlAssets()
StaticGenerator::requestPath in modules/tome_static/src/StaticGenerator.php
Requests and exports a given path.

File

modules/tome_static/src/StaticGenerator.php, line 385

Class

StaticGenerator
Handles static site generation.

Namespace

Drupal\tome_static

Code

protected function getHtmlAssets($content, $root) {
  $paths = [];
  $document = new \DOMDocument();
  @$document
    ->loadHTML($content);
  $xpath = new \DOMXPath($document);

  /** @var \DOMElement $image */
  foreach ($xpath
    ->query('//img | //source | //video') as $image) {
    if ($image
      ->hasAttribute('src')) {
      $paths[] = $image
        ->getAttribute('src');
    }
    if ($image
      ->hasAttribute('poster')) {
      $paths[] = $image
        ->getAttribute('poster');
    }
    if ($image
      ->hasAttribute('srcset')) {
      $srcset = $image
        ->getAttribute('srcset');
      $sources = explode(' ', preg_replace('/ [^ ]+(,|$)/', '', $srcset));
      foreach ($sources as $src) {
        $paths[] = $src;
      }
    }
  }

  /** @var \DOMElement $node */
  foreach ($xpath
    ->query('//svg/use') as $node) {
    if ($node
      ->hasAttribute('xlink:href')) {
      $paths[] = $node
        ->getAttribute('xlink:href');
    }
  }
  $rels = [
    'stylesheet',
    'shortcut icon',
    'icon',
    'image_src',
  ];

  /** @var \DOMElement $node */
  foreach ($document
    ->getElementsByTagName('link') as $node) {
    if (in_array($node
      ->getAttribute('rel'), $rels, TRUE) && $node
      ->hasAttribute('href')) {
      $paths[] = $node
        ->getAttribute('href');
    }
  }
  $meta_files = [
    'twitter:image',
    'twitter:player:stream',
    'og:image',
    'og:video',
    'og:audio',
    'og:image:url',
    'og:image:secure_url',
  ];

  /** @var \DOMElement $node */
  foreach ($document
    ->getElementsByTagName('meta') as $node) {
    if ((in_array($node
      ->getAttribute('property'), $meta_files, TRUE) || in_array($node
      ->getAttribute('name'), $meta_files, TRUE)) && $node
      ->hasAttribute('content')) {
      $paths[] = $node
        ->getAttribute('content');
    }
  }

  /** @var \DOMElement $node */
  foreach ($document
    ->getElementsByTagName('a') as $node) {
    if ($node
      ->hasAttribute('href')) {
      $paths[] = $node
        ->getAttribute('href');
    }
  }

  /** @var \DOMElement $node */
  foreach ($document
    ->getElementsByTagName('script') as $node) {
    if ($node
      ->hasAttribute('src')) {
      $paths[] = $node
        ->getAttribute('src');
    }
  }

  /** @var \DOMElement $node */
  foreach ($document
    ->getElementsByTagName('style') as $node) {
    $paths = array_merge($paths, $this
      ->getCssAssets($node->textContent, $root));
  }
  foreach ($xpath
    ->query('//*[@style]') as $node) {
    $paths = array_merge($paths, $this
      ->getCssAssets($node
      ->getAttribute('style'), $root));
  }

  /** @var \DOMElement $node */
  foreach ($document
    ->getElementsByTagName('iframe') as $node) {
    if ($node
      ->hasAttribute('src')) {
      $paths[] = $node
        ->getAttribute('src');
    }
  }

  // Recursive call in HTML comments in order to retrieve conditional assets.

  /** @var \DOMElement $node */
  foreach ($xpath
    ->query('//comment()') as $node) {
    $paths = array_merge($paths, $this
      ->getHtmlAssets($node->nodeValue, $root));
  }
  return $paths;
}