diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Dom.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Dom.php index a96f2388c4..f846a1f462 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Dom.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Dom.php @@ -10,6 +10,24 @@ class Dom implements ProcessorInterface { use HelperTrait; + /** + * Number of injects hashes. + * + * @since 3.17 + * + * @var int + */ + private $count; + + /** + * Maximum number of hashes to inject. + * + * @since 3.17 + * + * @var int + */ + private $max_hashes; + /** * Add hashes to the HTML elements * @@ -44,9 +62,10 @@ public function add_hashes( $html ) { return $html; } - $this->add_hash_to_element( $body, $this->get_depth() ); + $this->max_hashes = $this->get_max_tags(); + $this->count = 0; - return $dom->saveHTML(); + return $this->add_hash_to_element( $body, $this->get_depth(), $html ); } /** @@ -54,17 +73,24 @@ public function add_hashes( $html ) { * * @param \DOMElement $element The element to add the hash to. * @param int $depth The depth of the recursion. + * @param string $html The HTML content. + * + * @return string */ - private function add_hash_to_element( $element, $depth ) { + private function add_hash_to_element( $element, $depth, $html ) { if ( $depth < 0 ) { - return; + return $html; } $processed_tags = $this->get_processed_tags(); - static $count = 0; - foreach ( $element->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + + if ( $this->count >= $this->max_hashes ) { + Logger::warning( 'Stopping LRC hash injection as max_hashes is reached.', [ 'LazyRenderContent' ] ); + return $html; + } + if ( ! $child instanceof \DOMElement ) { continue; } @@ -81,15 +107,32 @@ private function add_hash_to_element( $element, $depth ) { $child_html = $child->ownerDocument->saveHTML( $child ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $opening_tag_html = strstr( $child_html, '>', true ) . '>'; - $hash = md5( $opening_tag_html . $count ); + $hash = md5( $opening_tag_html . $this->count ); - ++$count; + ++$this->count; - // Add the data-rocket-location-hash attribute. - $child->setAttribute( 'data-rocket-location-hash', $hash ); + // Inject the hash as an attribute in the opening tag. + $replace = preg_replace( '/' . $child->tagName . '/is', '$0 data-rocket-location-hash="' . $hash . '"', $opening_tag_html, 1 ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + if ( is_null( $replace ) ) { + continue; + } + // Replace the opening tag in the HTML by the manipulated one + // If DOMDocument automatically modified the original element, we might not find it in the HTML. + // Known issue: if there is an element with the exact same opening tag before in the HTML that did not receive a hash, it will replaced instead of the correct element in the HTML. + $element_replacements = 0; + $modified_html = preg_replace( '/' . preg_quote( $opening_tag_html, '/' ) . '/', $replace, $html, 1, $element_replacements ); + if ( $element_replacements < 1 ) { + Logger::warning( 'Opening tag from DOMDocument not found in original HTML.', [ 'LazyRenderContent' ] ); + } + if ( is_null( $modified_html ) ) { + continue; + } + $html = $modified_html; // Recursively process child elements. - $this->add_hash_to_element( $child, $depth - 1 ); + $html = $this->add_hash_to_element( $child, $depth - 1, $html ); } + + return $html; } } diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/HelperTrait.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/HelperTrait.php index d238dcaa2a..15103c1234 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/HelperTrait.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/HelperTrait.php @@ -8,6 +8,8 @@ trait HelperTrait { /** * Get filtered elements depth. * + * @since 3.17 + * * @return int */ protected function get_depth() { @@ -20,6 +22,23 @@ protected function get_depth() { return wpm_apply_filters_typed( 'integer', 'rocket_lrc_depth', 2 ); } + /** + * Get filtered element maximum count. + * + * @since 3.17 + * + * @return int + */ + protected function get_max_tags() { + /** + * Filter the maximal number of processed tags. + * High values allow to process more elements but expose to a risk of performance issue because of the regex replacement process. + * + * @param int $depth Depth value. + */ + return wpm_apply_filters_typed( 'integer', 'rocket_lrc_max_hashes', 200 ); + } + /** * Get processed tags. * @@ -29,6 +48,8 @@ protected function get_processed_tags() { /** * Filter the processed element tags. * + * @since 3.17 + * * @param array|string[] $tags Tags to be processed. */ $tags = wpm_apply_filters_typed( diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Regex.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Regex.php index 91e03a9cf9..97252455fd 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Regex.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/Regex.php @@ -9,6 +9,24 @@ class Regex implements ProcessorInterface { use HelperTrait; + /** + * Number of injects hashes. + * + * @since 3.17 + * + * @var int + */ + private $count; + + /** + * Maximum number of hashes to inject. + * + * @since 3.17 + * + * @var int + */ + private $max_hashes; + /** * Add hashes to the HTML elements * @@ -25,6 +43,9 @@ public function add_hashes( $html ) { return $html; } + $this->max_hashes = $this->get_max_tags(); + $this->count = 0; + return $this->add_hash_to_element( $html, $matches[0] ); } @@ -47,15 +68,19 @@ private function add_hash_to_element( $html, $element ) { return $html; } - $count = 0; - foreach ( $matches as $child ) { + + if ( $this->count >= $this->max_hashes ) { + Logger::warning( 'Stopping LRC hash injection as max_hashes is reached.', [ 'LazyRenderContent' ] ); + return $html; + } + // Calculate the hash of the opening tag. $opening_tag_html = strstr( $child[0], '>', true ) . '>'; - $hash = md5( $opening_tag_html . $count ); + $hash = md5( $opening_tag_html . $this->count ); - ++$count; + ++$this->count; // Add the data-rocket-location-hash attribute. $replace = preg_replace( '/' . $child[1] . '/is', '$0 data-rocket-location-hash="' . $hash . '"', $child[0], 1 ); diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/SimpleHtmlDom.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/SimpleHtmlDom.php index 022f0f6ad3..746a9300a7 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/SimpleHtmlDom.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/SimpleHtmlDom.php @@ -12,6 +12,24 @@ class SimpleHtmlDom implements ProcessorInterface { use HelperTrait; + /** + * Number of injects hashes. + * + * @since 3.17 + * + * @var int + */ + private $count; + + /** + * Maximum number of hashes to inject. + * + * @since 3.17 + * + * @var int + */ + private $max_hashes; + /** * Add hashes to the HTML elements * @@ -30,6 +48,9 @@ public function add_hashes( $html ) { return $html; } + $this->max_hashes = $this->get_max_tags(); + $this->count = 0; + $this->add_hash_to_element( $body, $this->get_depth() ); return $dom->save(); @@ -48,9 +69,13 @@ private function add_hash_to_element( $element, $depth ) { $processed_tags = $this->get_processed_tags(); - static $count = 0; - foreach ( $element->childNodes() as $child ) { + + if ( $this->count >= $this->max_hashes ) { + Logger::warning( 'Stopping LRC hash injection as max_hashes is reached.', [ 'LazyRenderContent' ] ); + return; + } + if ( ! in_array( strtoupper( $child->getTag() ), $processed_tags, true ) ) { continue; } @@ -59,9 +84,9 @@ private function add_hash_to_element( $element, $depth ) { $child_html = $child->html(); $opening_tag_html = strstr( $child_html, '>', true ) . '>'; - $hash = md5( $opening_tag_html . $count ); + $hash = md5( $opening_tag_html . $this->count ); - ++$count; + ++$this->count; // Add the data-rocket-location-hash attribute. $child->setAttribute( 'data-rocket-location-hash', $hash ); diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expected.html b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expected.html index c29f6f1a60..d50d86fe3a 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expected.html +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expected.html @@ -7,7 +7,7 @@

Original

-
+

Text

diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expectedRegex.html b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expectedRegex.html index bee28babe6..ff48967985 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expectedRegex.html +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/expectedRegex.html @@ -7,7 +7,7 @@

Original

-
+

Text

diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/original.html b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/original.html index 024512c570..b3589af23f 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/original.html +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Processor/original.html @@ -7,7 +7,7 @@

Original

-
+

Text

diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php index 6f6bd25f0c..4398813895 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php @@ -22,6 +22,39 @@ 'expected' => [ 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected.php' ), ] + ], + 'shouldStopAt200Hashes' => [ + 'config' => [ + 'row' => [ + 'url' => 'http://example.org/', + 'is_mobile' => 0, + 'below_the_fold' => json_encode( + [] + ), + 'status' => 'completed' + ], + 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_original.php' ), + ], + 'expected' => [ + 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_200_hashes.php' ), + ] + ], + 'shouldStopAtFilteredMaxHashes' => [ + 'config' => [ + 'row' => [ + 'url' => 'http://example.org/', + 'is_mobile' => 0, + 'below_the_fold' => json_encode( + [] + ), + 'status' => 'completed' + ], + 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_original.php' ), + 'max_hashes' => 150, + ], + 'expected' => [ + 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_150_hashes.php' ), + ] ] ] ]; diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected.php index ad15561af8..cb46769b07 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected.php +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected.php @@ -1,5 +1,5 @@ - - +?> + + @@ -33,24 +35,24 @@ -
400px -
800 px -































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































+
400px +
800 px +































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































1800 px
Testing something
-
+
-
+
This is a class with margin-top set to 3000px
-
+
This is a class with margin-top set to 1800px diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_150_hashes.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_150_hashes.php new file mode 100644 index 0000000000..b8cb5b4be8 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_150_hashes.php @@ -0,0 +1,375 @@ + + + + + +HTML Page with 100,000 Div Elements + + +largehtmldiv – ColorMag + + +test1 +
Div 1
+
Div 2
+
Div 3
+
Div 4
+
Div 5
+
Div 6
+
Div 7
+
Div 8
+
Div 9
+
Div 10
+
Div 11
+
Div 12
+
Div 13
+
Div 14
+
Div 15
+
Div 16
+
Div 17
+
Div 18
+
Div 19
+
Div 20
+
Div 21
+
Div 22
+
Div 23
+
Div 24
+
Div 25
+
Div 26
+
Div 27
+
Div 28
+
Div 29
+
Div 30
+
Div 31
+
Div 32
+
Div 33
+
Div 34
+
Div 35
+
Div 36
+
Div 37
+
Div 38
+
Div 39
+
Div 40
+
Div 41
+
Div 42
+
Div 43
+
Div 44
+
Div 45
+
Div 46
+
Div 47
+
Div 48
+
Div 49
+
Div 50
+
Div 51
+
Div 52
+
Div 53
+
Div 54
+
Div 55
+
Div 56
+
Div 57
+
Div 58
+
Div 59
+
Div 60
+
Div 61
+
Div 62
+
Div 63
+
Div 64
+
Div 65
+
Div 66
+
Div 67
+
Div 68
+
Div 69
+
Div 70
+
Div 71
+
Div 72
+
Div 73
+
Div 74
+
Div 75
+
Div 76
+
Div 77
+
Div 78
+
Div 79
+
Div 80
+
Div 81
+
Div 82
+
Div 83
+
Div 84
+
Div 85
+
Div 86
+
Div 87
+
Div 88
+
Div 89
+
Div 90
+
Div 91
+
Div 92
+
Div 93
+
Div 94
+
Div 95
+
Div 96
+
Div 97
+
Div 98
+
Div 99
+
Div 100
+
Div 101
+
Div 102
+
Div 103
+
Div 104
+
Div 105
+
Div 106
+
Div 107
+
Div 108
+
Div 109
+
Div 110
+
Div 111
+
Div 112
+
Div 113
+
Div 114
+
Div 115
+
Div 116
+
Div 117
+
Div 118
+
Div 119
+
Div 120
+
Div 121
+
Div 122
+
Div 123
+
Div 124
+
Div 125
+
Div 126
+
Div 127
+
Div 128
+
Div 129
+
Div 130
+
Div 131
+
Div 132
+
Div 133
+
Div 134
+
Div 135
+
Div 136
+
Div 137
+
Div 138
+
Div 139
+
Div 140
+
Div 141
+
Div 142
+
Div 143
+
Div 144
+
Div 145
+
Div 146
+
Div 147
+
Div 148
+
Div 149
+
Div 150
+
Div 151
+
Div 152
+
Div 153
+
Div 154
+
Div 155
+
Div 156
+
Div 157
+
Div 158
+
Div 159
+
Div 160
+
Div 161
+
Div 162
+
Div 163
+
Div 164
+
Div 165
+
Div 166
+
Div 167
+
Div 168
+
Div 169
+
Div 170
+
Div 171
+
Div 172
+
Div 173
+
Div 174
+
Div 175
+
Div 176
+
Div 177
+
Div 178
+
Div 179
+
Div 180
+
Div 181
+
Div 182
+
Div 183
+
Div 184
+
Div 185
+
Div 186
+
Div 187
+
Div 188
+
Div 189
+
Div 190
+
Div 191
+
Div 192
+
Div 193
+
Div 194
+
Div 195
+
Div 196
+
Div 197
+
Div 198
+
Div 199
+
Div 200
+
Div 201
+
Div 202
+
Div 203
+
Div 204
+
Div 205
+
Div 206
+
Div 207
+
Div 208
+
Div 209
+
Div 210
+
Div 211
+
Div 212
+
Div 213
+
Div 214
+
Div 215
+
Div 216
+
Div 217
+
Div 218
+
Div 219
+
Div 220
+
Div 221
+
Div 222
+
Div 223
+
Div 224
+
Div 225
+
Div 226
+
Div 227
+
Div 228
+
Div 229
+
Div 230
+
Div 231
+
Div 232
+
Div 233
+
Div 234
+
Div 235
+
Div 236
+
Div 237
+
Div 238
+
Div 239
+
Div 240
+
Div 241
+
Div 242
+
Div 243
+
Div 244
+
Div 245
+
Div 246
+
Div 247
+
Div 248
+
Div 249
+
Div 250
+
Div 251
+
Div 252
+
Div 253
+
Div 254
+
Div 255
+
Div 256
+
Div 257
+
Div 258
+
Div 259
+
Div 260
+
Div 261
+
Div 262
+
Div 263
+
Div 264
+
Div 265
+
Div 266
+
Div 267
+
Div 268
+
Div 269
+
Div 270
+
Div 271
+
Div 272
+
Div 273
+
Div 274
+
Div 275
+
Div 276
+
Div 277
+
Div 278
+
Div 279
+
Div 280
+
Div 281
+
Div 282
+
Div 283
+
Div 284
+
Div 285
+
Div 286
+
Div 287
+
Div 288
+
Div 289
+
Div 290
+
Div 291
+
Div 292
+
Div 293
+
Div 294
+
Div 295
+
Div 296
+
Div 297
+
Div 298
+
Div 299
+ + + + + + + \ No newline at end of file diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_200_hashes.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_200_hashes.php new file mode 100644 index 0000000000..868d7ba781 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_expected_200_hashes.php @@ -0,0 +1,375 @@ + + + + + +HTML Page with 100,000 Div Elements + + +largehtmldiv – ColorMag + + +test1 +
Div 1
+
Div 2
+
Div 3
+
Div 4
+
Div 5
+
Div 6
+
Div 7
+
Div 8
+
Div 9
+
Div 10
+
Div 11
+
Div 12
+
Div 13
+
Div 14
+
Div 15
+
Div 16
+
Div 17
+
Div 18
+
Div 19
+
Div 20
+
Div 21
+
Div 22
+
Div 23
+
Div 24
+
Div 25
+
Div 26
+
Div 27
+
Div 28
+
Div 29
+
Div 30
+
Div 31
+
Div 32
+
Div 33
+
Div 34
+
Div 35
+
Div 36
+
Div 37
+
Div 38
+
Div 39
+
Div 40
+
Div 41
+
Div 42
+
Div 43
+
Div 44
+
Div 45
+
Div 46
+
Div 47
+
Div 48
+
Div 49
+
Div 50
+
Div 51
+
Div 52
+
Div 53
+
Div 54
+
Div 55
+
Div 56
+
Div 57
+
Div 58
+
Div 59
+
Div 60
+
Div 61
+
Div 62
+
Div 63
+
Div 64
+
Div 65
+
Div 66
+
Div 67
+
Div 68
+
Div 69
+
Div 70
+
Div 71
+
Div 72
+
Div 73
+
Div 74
+
Div 75
+
Div 76
+
Div 77
+
Div 78
+
Div 79
+
Div 80
+
Div 81
+
Div 82
+
Div 83
+
Div 84
+
Div 85
+
Div 86
+
Div 87
+
Div 88
+
Div 89
+
Div 90
+
Div 91
+
Div 92
+
Div 93
+
Div 94
+
Div 95
+
Div 96
+
Div 97
+
Div 98
+
Div 99
+
Div 100
+
Div 101
+
Div 102
+
Div 103
+
Div 104
+
Div 105
+
Div 106
+
Div 107
+
Div 108
+
Div 109
+
Div 110
+
Div 111
+
Div 112
+
Div 113
+
Div 114
+
Div 115
+
Div 116
+
Div 117
+
Div 118
+
Div 119
+
Div 120
+
Div 121
+
Div 122
+
Div 123
+
Div 124
+
Div 125
+
Div 126
+
Div 127
+
Div 128
+
Div 129
+
Div 130
+
Div 131
+
Div 132
+
Div 133
+
Div 134
+
Div 135
+
Div 136
+
Div 137
+
Div 138
+
Div 139
+
Div 140
+
Div 141
+
Div 142
+
Div 143
+
Div 144
+
Div 145
+
Div 146
+
Div 147
+
Div 148
+
Div 149
+
Div 150
+
Div 151
+
Div 152
+
Div 153
+
Div 154
+
Div 155
+
Div 156
+
Div 157
+
Div 158
+
Div 159
+
Div 160
+
Div 161
+
Div 162
+
Div 163
+
Div 164
+
Div 165
+
Div 166
+
Div 167
+
Div 168
+
Div 169
+
Div 170
+
Div 171
+
Div 172
+
Div 173
+
Div 174
+
Div 175
+
Div 176
+
Div 177
+
Div 178
+
Div 179
+
Div 180
+
Div 181
+
Div 182
+
Div 183
+
Div 184
+
Div 185
+
Div 186
+
Div 187
+
Div 188
+
Div 189
+
Div 190
+
Div 191
+
Div 192
+
Div 193
+
Div 194
+
Div 195
+
Div 196
+
Div 197
+
Div 198
+
Div 199
+
Div 200
+
Div 201
+
Div 202
+
Div 203
+
Div 204
+
Div 205
+
Div 206
+
Div 207
+
Div 208
+
Div 209
+
Div 210
+
Div 211
+
Div 212
+
Div 213
+
Div 214
+
Div 215
+
Div 216
+
Div 217
+
Div 218
+
Div 219
+
Div 220
+
Div 221
+
Div 222
+
Div 223
+
Div 224
+
Div 225
+
Div 226
+
Div 227
+
Div 228
+
Div 229
+
Div 230
+
Div 231
+
Div 232
+
Div 233
+
Div 234
+
Div 235
+
Div 236
+
Div 237
+
Div 238
+
Div 239
+
Div 240
+
Div 241
+
Div 242
+
Div 243
+
Div 244
+
Div 245
+
Div 246
+
Div 247
+
Div 248
+
Div 249
+
Div 250
+
Div 251
+
Div 252
+
Div 253
+
Div 254
+
Div 255
+
Div 256
+
Div 257
+
Div 258
+
Div 259
+
Div 260
+
Div 261
+
Div 262
+
Div 263
+
Div 264
+
Div 265
+
Div 266
+
Div 267
+
Div 268
+
Div 269
+
Div 270
+
Div 271
+
Div 272
+
Div 273
+
Div 274
+
Div 275
+
Div 276
+
Div 277
+
Div 278
+
Div 279
+
Div 280
+
Div 281
+
Div 282
+
Div 283
+
Div 284
+
Div 285
+
Div 286
+
Div 287
+
Div 288
+
Div 289
+
Div 290
+
Div 291
+
Div 292
+
Div 293
+
Div 294
+
Div 295
+
Div 296
+
Div 297
+
Div 298
+
Div 299
+ + + + + + + \ No newline at end of file diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_original.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_original.php new file mode 100644 index 0000000000..60eaa7f71d --- /dev/null +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/long_original.php @@ -0,0 +1,375 @@ + + + + + +HTML Page with 100,000 Div Elements + + +largehtmldiv – ColorMag + + +test1 +
Div 1
+
Div 2
+
Div 3
+
Div 4
+
Div 5
+
Div 6
+
Div 7
+
Div 8
+
Div 9
+
Div 10
+
Div 11
+
Div 12
+
Div 13
+
Div 14
+
Div 15
+
Div 16
+
Div 17
+
Div 18
+
Div 19
+
Div 20
+
Div 21
+
Div 22
+
Div 23
+
Div 24
+
Div 25
+
Div 26
+
Div 27
+
Div 28
+
Div 29
+
Div 30
+
Div 31
+
Div 32
+
Div 33
+
Div 34
+
Div 35
+
Div 36
+
Div 37
+
Div 38
+
Div 39
+
Div 40
+
Div 41
+
Div 42
+
Div 43
+
Div 44
+
Div 45
+
Div 46
+
Div 47
+
Div 48
+
Div 49
+
Div 50
+
Div 51
+
Div 52
+
Div 53
+
Div 54
+
Div 55
+
Div 56
+
Div 57
+
Div 58
+
Div 59
+
Div 60
+
Div 61
+
Div 62
+
Div 63
+
Div 64
+
Div 65
+
Div 66
+
Div 67
+
Div 68
+
Div 69
+
Div 70
+
Div 71
+
Div 72
+
Div 73
+
Div 74
+
Div 75
+
Div 76
+
Div 77
+
Div 78
+
Div 79
+
Div 80
+
Div 81
+
Div 82
+
Div 83
+
Div 84
+
Div 85
+
Div 86
+
Div 87
+
Div 88
+
Div 89
+
Div 90
+
Div 91
+
Div 92
+
Div 93
+
Div 94
+
Div 95
+
Div 96
+
Div 97
+
Div 98
+
Div 99
+
Div 100
+
Div 101
+
Div 102
+
Div 103
+
Div 104
+
Div 105
+
Div 106
+
Div 107
+
Div 108
+
Div 109
+
Div 110
+
Div 111
+
Div 112
+
Div 113
+
Div 114
+
Div 115
+
Div 116
+
Div 117
+
Div 118
+
Div 119
+
Div 120
+
Div 121
+
Div 122
+
Div 123
+
Div 124
+
Div 125
+
Div 126
+
Div 127
+
Div 128
+
Div 129
+
Div 130
+
Div 131
+
Div 132
+
Div 133
+
Div 134
+
Div 135
+
Div 136
+
Div 137
+
Div 138
+
Div 139
+
Div 140
+
Div 141
+
Div 142
+
Div 143
+
Div 144
+
Div 145
+
Div 146
+
Div 147
+
Div 148
+
Div 149
+
Div 150
+
Div 151
+
Div 152
+
Div 153
+
Div 154
+
Div 155
+
Div 156
+
Div 157
+
Div 158
+
Div 159
+
Div 160
+
Div 161
+
Div 162
+
Div 163
+
Div 164
+
Div 165
+
Div 166
+
Div 167
+
Div 168
+
Div 169
+
Div 170
+
Div 171
+
Div 172
+
Div 173
+
Div 174
+
Div 175
+
Div 176
+
Div 177
+
Div 178
+
Div 179
+
Div 180
+
Div 181
+
Div 182
+
Div 183
+
Div 184
+
Div 185
+
Div 186
+
Div 187
+
Div 188
+
Div 189
+
Div 190
+
Div 191
+
Div 192
+
Div 193
+
Div 194
+
Div 195
+
Div 196
+
Div 197
+
Div 198
+
Div 199
+
Div 200
+
Div 201
+
Div 202
+
Div 203
+
Div 204
+
Div 205
+
Div 206
+
Div 207
+
Div 208
+
Div 209
+
Div 210
+
Div 211
+
Div 212
+
Div 213
+
Div 214
+
Div 215
+
Div 216
+
Div 217
+
Div 218
+
Div 219
+
Div 220
+
Div 221
+
Div 222
+
Div 223
+
Div 224
+
Div 225
+
Div 226
+
Div 227
+
Div 228
+
Div 229
+
Div 230
+
Div 231
+
Div 232
+
Div 233
+
Div 234
+
Div 235
+
Div 236
+
Div 237
+
Div 238
+
Div 239
+
Div 240
+
Div 241
+
Div 242
+
Div 243
+
Div 244
+
Div 245
+
Div 246
+
Div 247
+
Div 248
+
Div 249
+
Div 250
+
Div 251
+
Div 252
+
Div 253
+
Div 254
+
Div 255
+
Div 256
+
Div 257
+
Div 258
+
Div 259
+
Div 260
+
Div 261
+
Div 262
+
Div 263
+
Div 264
+
Div 265
+
Div 266
+
Div 267
+
Div 268
+
Div 269
+
Div 270
+
Div 271
+
Div 272
+
Div 273
+
Div 274
+
Div 275
+
Div 276
+
Div 277
+
Div 278
+
Div 279
+
Div 280
+
Div 281
+
Div 282
+
Div 283
+
Div 284
+
Div 285
+
Div 286
+
Div 287
+
Div 288
+
Div 289
+
Div 290
+
Div 291
+
Div 292
+
Div 293
+
Div 294
+
Div 295
+
Div 296
+
Div 297
+
Div 298
+
Div 299
+ + + + + + + \ No newline at end of file diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php index be2346dbb0..459f98ab23 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php @@ -45,7 +45,7 @@
-
+
This is a class with margin-top set to 3000px
diff --git a/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php b/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php index 7286fa3f0e..412f15a940 100644 --- a/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php +++ b/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php @@ -10,6 +10,8 @@ * @group PerformanceHints */ class Test_AddHashes extends TestCase { + private $max_hashes; + public static function set_up_before_class() { parent::set_up_before_class(); @@ -26,12 +28,14 @@ public static function tear_down_after_class() { public function set_up() { parent::set_up(); + $this->max_hashes = null; $this->unregisterAllCallbacksExcept( 'rocket_performance_hints_buffer', 'add_hashes', 16 ); } public function tear_down() { $this->restoreWpHook( 'rocket_performance_hints_buffer' ); remove_filter( 'rocket_lrc_optimization', '__return_false' ); + remove_filter( 'rocket_lrc_max_hashes', [ $this, 'set_lrc_max_hashes' ] ); parent::tear_down(); } @@ -44,9 +48,19 @@ public function testShouldWorkAsExpected( $config, $expected ) { add_filter( 'rocket_lrc_optimization', '__return_true' ); + if ( isset( $config['max_hashes'] ) ) { + $this->max_hashes = $config['max_hashes']; + add_filter( 'rocket_lrc_max_hashes', [ $this, 'set_lrc_max_hashes' ] ); + } + + $this->assertSame( $expected['html'], apply_filters( 'rocket_performance_hints_buffer', $config['html'] ) ); } + + public function set_lrc_max_hashes() { + return $this->max_hashes; + } }