Fixed srcset handling
authorAlexander Ebert <ebert@woltlab.com>
Mon, 26 Sep 2016 15:56:15 +0000 (17:56 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 26 Sep 2016 15:56:20 +0000 (17:56 +0200)
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeImg.class.php

index 15a5e3bfe3b89dcd36c364b1392831d9fdae5980..70cb8741ab96736765b6a3ec0d6d91c0c1bda100 100644 (file)
@@ -4,8 +4,10 @@ use wcf\data\smiley\Smiley;
 use wcf\data\smiley\SmileyCache;
 use wcf\system\html\node\AbstractHtmlNodeProcessor;
 use wcf\system\request\LinkHandler;
+use wcf\util\DOMUtil;
 use wcf\util\exception\CryptoException;
 use wcf\util\CryptoUtil;
+use wcf\util\StringUtil;
 
 /**
  * Processes images.
@@ -52,8 +54,30 @@ class HtmlOutputNodeImg extends AbstractHtmlOutputNode {
                        }
                        else if (MODULE_IMAGE_PROXY) {
                                $src = $element->getAttribute('src');
-                               if ($src) {
-                                       $element->setAttribute('src', $this->getProxyLink($src));
+                               if (!$src) {
+                                       DOMUtil::removeNode($element);
+                                       continue;
+                               }
+                               
+                               $element->setAttribute('src', $this->getProxyLink($src));
+                               
+                               $srcset = $element->getAttribute('srcset');
+                               if ($srcset) {
+                                       // simplified regex to check if it appears to be a valid list of sources
+                                       if (!preg_match('~^[^\s]+\s+[0-9\.]+[wx](,\s*[^\s]+\s+[0-9\.]+[wx])*~', $srcset)) {
+                                               $element->removeAttribute('srcset');
+                                               continue;
+                                       }
+                                       
+                                       $sources = explode(',', $srcset);
+                                       $srcset = '';
+                                       foreach ($sources as $source) {
+                                               $tmp = preg_split('~\s+~', StringUtil::trim($source));
+                                               if (!empty($srcset)) $srcset .= ', ';
+                                               $srcset .= $this->getProxyLink($tmp[0]) . ' ' . $tmp[1];
+                                       }
+                                       
+                                       $element->setAttribute('srcset', $srcset);
                                }
                        }
                }