@@ -21,6 +21,8 @@ class BlockPlugin
2121
2222 const REPLACEMENT_LABEL = 'mf-lazy ' ;
2323
24+ const PATTERN = '#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' ;
25+
2426 /**
2527 * Request
2628 * @var \Magento\Framework\App\RequestInterface
@@ -46,11 +48,6 @@ class BlockPlugin
4648 */
4749 protected $ config ;
4850
49- /**
50- * @var array
51- */
52- protected $ labelsValues = [];
53-
5451 /**
5552 * @var array
5653 */
@@ -85,65 +82,75 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
8582 return $ html ;
8683 }
8784
88- if ( $ this ->config ->getIsJavascriptLazyLoadMethod ()) {
85+ $ numberOfReplacements = $ this ->config ->getBlockFirstImagesToSkip ( $ this -> getBlockIdentifier ( $ block ));
8986
90- $ numberOfReplacements = $ this -> config -> getBlockFirstImagesToSkip (
91- $ this ->getBlockIdentifier ( $ block )
92- );
87+ if ( $ numberOfReplacements) {
88+ $ html = $ this ->removeLoadingLazyAttributeFromFirstNImages ( $ html , $ numberOfReplacements );
89+ }
9390
94- if ( $ numberOfReplacements ) {
95- $ html = $ this ->removeFirstNImagesWithCustomLabel ( $ html , $ numberOfReplacements );
96- }
91+ $ html = $ this -> config -> getIsJavascriptLazyLoadMethod ()
92+ ? $ this ->prepareForJsLazyLoad ( $ block , $ html )
93+ : $ this -> prepareForNativeBrowserLazyLoad ( $ html );
9794
98- $ pixelSrc = ' src=" ' . $ block -> getViewFileUrl ( ' Magefan_LazyLoad::images/pixel.jpg ' ) . ' " ' ;
99- $ tmpSrc = ' TMP_SRC ' ;
95+ return $ html ;
96+ }
10097
101- $ html = str_replace ($ pixelSrc , $ tmpSrc , $ html );
10298
103- $ noscript = '' ;
104- if ($ this ->config ->isNoScriptEnabled ()) {
105- $ noscript = '<noscript>
99+ /**
100+ * @param string $html
101+ * @return string
102+ */
103+ private function prepareForJsLazyLoad ($ block , string $ html ): string
104+ {
105+ $ pixelSrc = ' src=" ' . $ block ->getViewFileUrl ('Magefan_LazyLoad::images/pixel.jpg ' ) . '" ' ;
106+ $ tmpSrc = 'TMP_SRC ' ;
107+
108+ $ html = str_replace ($ pixelSrc , $ tmpSrc , $ html );
109+
110+ $ noscript = '' ;
111+ if ($ this ->config ->isNoScriptEnabled ()) {
112+ $ noscript = '<noscript>
106113 <img src="$2" $1 $3 />
107114 </noscript> ' ;
108- }
109-
110- $ html = preg_replace (
111- '#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' ,
112- '<img data-original="$2" $1 $3/> ' . $ noscript ,
113- $ html
114- );
115-
116- $ html = str_replace (' data-original= ' , $ pixelSrc . ' data-original= ' , $ html );
117-
118- $ html = str_replace ($ tmpSrc , $ pixelSrc , $ html );
119- $ html = str_replace (self ::LAZY_TAG , '' , $ html );
120-
121- /* Disable Owl Slider LazyLoad */
122- $ html = str_replace (
123- ['"lazyLoad":true, ' , '"lazyLoad":true, ' , 'owl-lazy ' ],
124- ['"lazyLoad":false, ' , '"lazyLoad":false, ' , '' ],
125- $ html
126- );
127-
128- /* Fix for page builder bg images */
129- if (false !== strpos ($ html , 'background-image- ' )) {
130- $ html = str_replace ('.background-image- ' , '.tmpbgimg- ' , $ html );
131- $ html = str_replace ('background-image- ' , 'mflazy-background-image mflazy-background-image- ' , $ html );
132- $ html = str_replace ('.tmpbgimg- ' , '.background-image- ' , $ html );
133- }
115+ }
134116
135- if ($ numberOfReplacements ) {
136- $ html = $ this ->revertFirstNImageToInital ($ html );
137- }
138- } else {
139- $ html = preg_replace ('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , '<img ' .
140- ' src="$2" $1 $3 loading="lazy" />
141- ' , $ html );
117+ $ html = preg_replace (
118+ self ::PATTERN ,
119+ '<img data-original="$2" $1 $3/> ' . $ noscript ,
120+ $ html
121+ );
122+
123+ $ html = str_replace (' data-original= ' , $ pixelSrc . ' data-original= ' , $ html );
124+
125+ $ html = str_replace ($ tmpSrc , $ pixelSrc , $ html );
126+ $ html = str_replace (self ::LAZY_TAG , '' , $ html );
127+
128+ /* Disable Owl Slider LazyLoad */
129+ $ html = str_replace (
130+ ['"lazyLoad":true, ' , '"lazyLoad":true, ' , 'owl-lazy ' ],
131+ ['"lazyLoad":false, ' , '"lazyLoad":false, ' , '' ],
132+ $ html
133+ );
134+
135+ /* Fix for page builder bg images */
136+ if (false !== strpos ($ html , 'background-image- ' )) {
137+ $ html = str_replace ('.background-image- ' , '.tmpbgimg- ' , $ html );
138+ $ html = str_replace ('background-image- ' , 'mflazy-background-image mflazy-background-image- ' , $ html );
139+ $ html = str_replace ('.tmpbgimg- ' , '.background-image- ' , $ html );
142140 }
143141
144142 return $ html ;
145143 }
146144
145+ /**
146+ * @param string $html
147+ * @return string
148+ */
149+ protected function prepareForNativeBrowserLazyLoad (string $ html ) :string
150+ {
151+ return preg_replace (self ::PATTERN , '<img src="$2" $1 $3 loading="lazy" /> ' , $ html );
152+ }
153+
147154 /**
148155 * @param \Magento\Framework\View\Element\AbstractBlock $block
149156 * @return string
@@ -166,40 +173,32 @@ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBl
166173 }
167174
168175 /**
169- * @param $html
176+ * @param string $html
170177 * @param int $numberOfReplacements
171- * @return array| string|string[]|null
178+ * @return string
172179 */
173- protected function removeFirstNImagesWithCustomLabel ( $ html , int $ numberOfReplacements )
180+ protected function removeLoadingLazyAttributeFromFirstNImages ( string $ html , int $ numberOfReplacements ): string
174181 {
175- $ count = 0 ;
176- return preg_replace_callback ('#<img([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , function ($ match ) use (&$ count , &$ numberOfReplacements ) {
177- $ count ++;
178- if ($ count <= $ numberOfReplacements ) {
179- $ label = self ::REPLACEMENT_LABEL . '_ ' . $ count ;
180- $ imgTag = $ match [0 ];
181-
182- if (strpos ($ imgTag , 'mfdislazy ' ) === false ) {
183- $ imgTag = str_replace ('<img ' , '<img mfdislazy="1" ' , $ imgTag );
182+ $ position = 0 ;
183+
184+ if (preg_match_all (self ::PATTERN , $ html , $ matches , PREG_OFFSET_CAPTURE ) !== false ) {
185+ foreach ($ matches [0 ] as $ i => $ match ) {
186+ if ($ i > $ numberOfReplacements - 1 ) {
187+ break ;
184188 }
185189
186- $ this ->labelsValues [$ label ] = $ imgTag ;
190+ $ offset = $ match [1 ] + $ position ;
191+ $ htmlTag = $ matches [0 ][$ i ][0 ];
187192
188- return $ label ;
189- }
190-
191- return $ match [0 ];
192- }, $ html , $ numberOfReplacements );
193- }
193+ $ newHtmlTag = str_replace (
194+ ['loading="lazy" ' , '<img ' ],
195+ ['' , '<img mfdislazy="1" ' ],
196+ $ htmlTag
197+ );
194198
195- /**
196- * @param $html
197- * @return array|mixed|string|string[]
198- */
199- protected function revertFirstNImageToInital ($ html )
200- {
201- foreach ($ this ->labelsValues as $ labelsValue => $ img ) {
202- $ html = str_replace ($ labelsValue , $ img , $ html );
199+ $ html = substr_replace ($ html , $ newHtmlTag , $ offset , strlen ($ htmlTag ));
200+ $ position = $ position + (strlen ($ newHtmlTag ) - strlen ($ htmlTag ));
201+ }
203202 }
204203
205204 return $ html ;
@@ -226,7 +225,7 @@ protected function isEnabled($block, string $html): bool
226225 return false ;
227226 }
228227
229- if ($ this ->config ->getIsAllBlocksAddedToLazy () && !$ this ->isBlockSkiped ($ block )) {
228+ if ($ this ->config ->isAllBlocksAddedToLazy () && !$ this ->isBlockSkiped ($ block )) {
230229 return true ;
231230 }
232231
0 commit comments