You are here

function _filter_lazy_process in Lazy-load 7

Implements callback_filter_process().

1 string reference to '_filter_lazy_process'
lazy_filter_info in ./lazy.module
Implements hook_filter_info().

File

./lazy.module, line 104
Module file for Lazy-load.

Code

function _filter_lazy_process($text, $filter) {
  $opt_skipClass = variable_get('lazy_filter_skipClass');
  $opt_selector = ltrim(variable_get('lazy_filter_selector'), '.');
  $opt_tags = variable_get('lazy_filter_alter_tag');
  $opt_src = variable_get('lazy_filter_src') !== 'src' ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
  $opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
  $library_installed = variable_get('lazy_library_installed');
  if ($library_installed && lazy_is_path_allowed()) {
    $html_dom = filter_dom_load($text);
    foreach ($opt_tags as $tag) {
      $matches = $html_dom
        ->getElementsByTagName($tag);
      foreach ($matches as $element) {
        $classes = $element
          ->getAttribute('class');
        $classes = $classes != '' ? explode(' ', $classes) : array();
        if (!in_array($opt_skipClass, $classes, TRUE)) {
          if (variable_get('lazy_prefer_native') && !$element
            ->hasAttribute('loading')) {

            // Required attribute for enabling native lazy-loading.
            $element
              ->setAttribute('loading', 'lazy');
          }
          elseif (!$element
            ->hasAttribute($opt_src)) {
            $classes[] = $opt_selector;
            $element
              ->setAttribute('class', implode(' ', $classes));
            $src = $element
              ->getAttribute('src');
            $element
              ->removeAttribute('src');
            $element
              ->setAttribute($opt_src, $src);
            $element
              ->setAttribute('src', $opt_placeholderSrc);
          }
        }
      }
    }
    $text = filter_dom_serialize($html_dom);
  }
  return trim($text);
}