@@ -415,8 +415,19 @@ protected function stripWhitespace($content)
415415 * to be the for-loop's body... Same goes for while loops.
416416 * I'm going to double that semicolon (if any) so after the next line,
417417 * which strips semicolons here & there, we're still left with this one.
418+ * Note the special recursive construct in the three inner parts of the for:
419+ * (\{([^\{\}]*(?-2))*[^\{\}]*\})? - it is intended to match inline
420+ * functions bodies, e.g.: i<arr.map(function(e){return e}).length.
421+ * Also note that the construct is applied only once and multiplied
422+ * for each part of the for, otherwise it risks a catastrophic backtracking.
423+ * The limitation is that it will not allow closures in more than one
424+ * of the three parts for a specific for() case.
425+ * REGEX throwing catastrophic backtracking: $content = preg_replace('/(for\([^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*\));(\}|$)/s', '\\1;;\\8', $content);
418426 */
419- $ content = preg_replace ('/(for\([^;\{]*;[^;\{]*;[^;\{]*\));(\}|$)/s ' , '\\1;; \\2 ' , $ content );
427+ $ content = preg_replace ('/(for\([^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*;[^;\{]*;[^;\{]*\));(\}|$)/s ' , '\\1;; \\4 ' , $ content );
428+ $ content = preg_replace ('/(for\([^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*;[^;\{]*\));(\}|$)/s ' , '\\1;; \\4 ' , $ content );
429+ $ content = preg_replace ('/(for\([^;\{]*;[^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*\));(\}|$)/s ' , '\\1;; \\4 ' , $ content );
430+
420431 $ content = preg_replace ('/(for\([^;\{]+\s+in\s+[^;\{]+\));(\}|$)/s ' , '\\1;; \\2 ' , $ content );
421432 /*
422433 * Below will also keep `;` after a `do{}while();` along with `while();`
0 commit comments