Thay đổi thuộc tính mặc định của bài viết src, alt, srcset, sizes, lazysizes (ok)

Trước

Sau

function add_lazyload($content) {
  $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
  $dom = new DOMDocument();
  @$dom->loadHTML($content);
  $images = [];
  foreach ($dom->getElementsByTagName('img') as $node) {
    $images[] = $node;
  }
  foreach ($images as $node) {
  	echo '<pre>';
  		var_export($node);
  	echo '</pre>';
   	$fallback = $node->cloneNode(true);
   	$oldsrc = $node->getAttribute('src');
   	$node->setAttribute('data-src', $oldsrc );
   	$newsrc = 'https://d1zczzapudl1mr.cloudfront.net/blank-kraken.gif';
   	$node->setAttribute('src', $newsrc);
   	$oldsrcset = $node->getAttribute('srcset');
   	$node->setAttribute('data-srcset', $oldsrcset );
   	$newsrcset = '';
   	$node->setAttribute('srcset', $newsrcset);
   	$classes = $node->getAttribute('class');
   	$newclasses = $classes . ' lazyload';
   	$node->setAttribute('class', $newclasses);
   	$noscript = $dom->createElement('noscript', '');
   	$node->parentNode->insertBefore($noscript, $node);
   	$noscript->appendChild($fallback);
  }
  $newHtml = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''),
  $dom->saveHTML()));
  return $newHtml;
}
add_filter('the_content', 'add_lazyload', 10);

Một ví dụ đã hoàn thành :)

Ask QuestionAsked 3 years, 11 months agoActive 3 years, 10 months agoViewed 1k times1

I'm having a weird issue trying to append an image element to a noscript element using php DomDocument.

If I create a new div node I can append it without issue to the noscript element but as soon as a try to append an image element the script just times out.

What am I doing wrong?

phpdomdocumentShareImprove this questionFollowasked May 23 '17 at 23:00Cesar3,70277 gold badges4040 silver badges6666 bronze badges

  • Afaik div's don't have src attribute. – Pedro Lobito May 23 '17 at 23:05

  • 1getElementsByTagName returns a live NodeList, at least according to DOM specification. If that is true for PHP/DomDocuments implementation as well, then I am not sure whether it's a good idea to loop over such a NodeList while moving around elements inside it ... – CBroe May 23 '17 at 23:09

  • 1Well the thing is that it works with a div but not with an img – Cesar May 23 '17 at 23:51

  • @CBroe You were right the problem was in fact that getElementsByTagName return a live node list – Cesar Jun 12 '17 at 15:13

Add a comment

1 Answer

ActiveOldestVotes1

You're getting caught in a recursive loop. This will help you visualize what's going on. I've added indenting for clarity:

The troublesome line causing the recursion is

if you comment that out, the recursion goes away. Notice that when it recurses, the x-data-src is being applied to all but the last one.

I haven't quite figured out what is causing this behaviour, but hopefully being able to visualize it will help you diagnose it further.

**UPDATE

The OP took this and ran with it, and completed the answer with his solution as shown below.

The problem was in fact that getElementsByTagName returns a LiveNodeList so appending an image to the doc will cause the infinite recursion.

I solved it by first collecting all the image tags in a simple array

Share

Last updated

Was this helpful?