core/modules/big_pipe/src/Render/BigPipe.php | 46 ++++++++++++---------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/core/modules/big_pipe/src/Render/BigPipe.php b/core/modules/big_pipe/src/Render/BigPipe.php index d418975a0d..7397cd53c2 100644 --- a/core/modules/big_pipe/src/Render/BigPipe.php +++ b/core/modules/big_pipe/src/Render/BigPipe.php @@ -388,11 +388,12 @@ protected function sendPreBody($pre_body, array $no_js_placeholders, AttachedAss * simplify debugging. */ protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) { - $placeholder_keys = array_keys($no_js_placeholders); - $fragments = $this->splitTextOnPlaceholders($html, $placeholder_keys); + // Split the HTML on every no-JS placeholder string. + $placeholder_strings = array_keys($no_js_placeholders); + $fragments = static::splitHtmlOnPlaceholders($html, $placeholder_strings); // Determine how many occurrences there are of each no-JS placeholder. - $placeholder_occurrences = array_count_values(array_intersect($fragments, $placeholder_keys)); + $placeholder_occurrences = array_count_values(array_intersect($fragments, $placeholder_strings)); // Set up a variable to store the content of placeholders that have multiple // occurrences. @@ -751,45 +752,36 @@ protected function getPlaceholderOrder($html, $placeholders) { } /** - * Splits a text into fragments. + * Splits a HTML string into fragments. * - * Will create an array of fragments of text, separated by placeholders. - * The result includes the placeholders themselves. Note that the order - * of the placeholders matters. + * Creates an array of HTML fragments, separated by placeholders. The result + * includes the placeholders themselves. The original order is respected. * - * Examples: - * - * splitTextOnPlaceholders('some example text', ['e', 'ex']); - * array ('som', 'e', ' ', 'e', 'xampl', 'e', ' t', 'e', 'xt') - * - * splitTextOnPlaceholders('some example text', ['ex', 'e']); - * array ('som', 'e', ' ', 'ex', 'ampl', 'e', ' t', 'ex', 't') - * - * @param string $text - * The text to split. - * @param string[] $placeholders - * The strings to split on. + * @param string $html_string + * The HTML to split. + * @param string[] $html_placeholders + * The HTML placeholders to split on. * * @return string[] - * The created fragments. + * The resulting HTML fragments. */ - protected function splitTextOnPlaceholders($text, array $placeholders) { + private static function splitHtmlOnPlaceholders($html_string, array $html_placeholders) { $prepare_for_preg_split = function ($placeholder_string) { return '(' . preg_quote($placeholder_string, '/') . ')'; }; - $preg_placeholder_strings = array_map($prepare_for_preg_split, $placeholders); + $preg_placeholder_strings = array_map($prepare_for_preg_split, $html_placeholders); $pattern = '/' . implode('|', $preg_placeholder_strings) . '/'; if (strlen($pattern) < 31000) { - // Only small (<31K) patterns can be handled by preg_split. + // Only small (<31K characters) patterns can be handled by preg_split(). $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE; - $result = preg_split($pattern, $text, NULL, $flags); + $result = preg_split($pattern, $html_string, NULL, $flags); } else { // For large amounts of placeholders we use a simpler but slower approach. - foreach ($placeholders as $placeholder) { - $text = str_replace($placeholder, "\x1F" . $placeholder . "\x1F", $text); + foreach ($html_placeholders as $placeholder) { + $html_string = str_replace($placeholder, "\x1F" . $placeholder . "\x1F", $html_string); } - $result = array_filter(explode("\x1F", $text)); + $result = array_filter(explode("\x1F", $html_string)); } return $result; }