@@ -21,6 +21,8 @@ class BlockPlugin
21
21
22
22
const REPLACEMENT_LABEL = 'mf-lazy ' ;
23
23
24
+ const PATTERN = '#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' ;
25
+
24
26
/**
25
27
* Request
26
28
* @var \Magento\Framework\App\RequestInterface
@@ -46,11 +48,6 @@ class BlockPlugin
46
48
*/
47
49
protected $ config ;
48
50
49
- /**
50
- * @var array
51
- */
52
- protected $ labelsValues = [];
53
-
54
51
/**
55
52
* @var array
56
53
*/
@@ -85,65 +82,75 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
85
82
return $ html ;
86
83
}
87
84
88
- if ( $ this ->config ->getIsJavascriptLazyLoadMethod ()) {
85
+ $ numberOfReplacements = $ this ->config ->getBlockFirstImagesToSkip ( $ this -> getBlockIdentifier ( $ block ));
89
86
90
- $ numberOfReplacements = $ this -> config -> getBlockFirstImagesToSkip (
91
- $ this ->getBlockIdentifier ( $ block )
92
- );
87
+ if ( $ numberOfReplacements) {
88
+ $ html = $ this ->removeLoadingLazyAttributeFromFirstNImages ( $ html , $ numberOfReplacements );
89
+ }
93
90
94
- if ( $ numberOfReplacements ) {
95
- $ html = $ this ->removeFirstNImagesWithCustomLabel ( $ html , $ numberOfReplacements );
96
- }
91
+ $ html = $ this -> config -> getIsJavascriptLazyLoadMethod ()
92
+ ? $ this ->prepareForJsLazyLoad ( $ block , $ html )
93
+ : $ this -> prepareForNativeBrowserLazyLoad ( $ html );
97
94
98
- $ pixelSrc = ' src=" ' . $ block -> getViewFileUrl ( ' Magefan_LazyLoad::images/pixel.jpg ' ) . ' " ' ;
99
- $ tmpSrc = ' TMP_SRC ' ;
95
+ return $ html ;
96
+ }
100
97
101
- $ html = str_replace ($ pixelSrc , $ tmpSrc , $ html );
102
98
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>
106
113
<img src="$2" $1 $3 />
107
114
</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
+ }
134
116
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 );
142
140
}
143
141
144
142
return $ html ;
145
143
}
146
144
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
+
147
154
/**
148
155
* @param \Magento\Framework\View\Element\AbstractBlock $block
149
156
* @return string
@@ -166,40 +173,32 @@ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBl
166
173
}
167
174
168
175
/**
169
- * @param $html
176
+ * @param string $html
170
177
* @param int $numberOfReplacements
171
- * @return array| string|string[]|null
178
+ * @return string
172
179
*/
173
- protected function removeFirstNImagesWithCustomLabel ( $ html , int $ numberOfReplacements )
180
+ protected function removeLoadingLazyAttributeFromFirstNImages ( string $ html , int $ numberOfReplacements ): string
174
181
{
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 ;
184
188
}
185
189
186
- $ this ->labelsValues [$ label ] = $ imgTag ;
190
+ $ offset = $ match [1 ] + $ position ;
191
+ $ htmlTag = $ matches [0 ][$ i ][0 ];
187
192
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
+ );
194
198
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
+ }
203
202
}
204
203
205
204
return $ html ;
@@ -226,7 +225,7 @@ protected function isEnabled($block, string $html): bool
226
225
return false ;
227
226
}
228
227
229
- if ($ this ->config ->getIsAllBlocksAddedToLazy () && !$ this ->isBlockSkiped ($ block )) {
228
+ if ($ this ->config ->isAllBlocksAddedToLazy () && !$ this ->isBlockSkiped ($ block )) {
230
229
return true ;
231
230
}
232
231
0 commit comments