JavaScriptMinifier: Improve docs around parsing of regexp literals
authorTimo Tijhof <krinklemail@gmail.com>
Fri, 22 Dec 2017 18:00:42 +0000 (19:00 +0100)
committerLegoktm <legoktm@member.fsf.org>
Sun, 24 Dec 2017 21:48:34 +0000 (21:48 +0000)
Bug: T75556
Change-Id: Ifcb6bc21418dfc2e1d3e44dbd2497a0f5f691bf3

includes/libs/JavaScriptMinifier.php

index 3be9ca1..bbba33a 100644 (file)
@@ -449,18 +449,24 @@ class JavaScriptMinifier {
                        // We have to distinguish between regexp literals and division operators
                        // A division operator is only possible in certain states
                        } elseif( $ch === '/' && !isset( $divStates[$state] ) ) {
                        // We have to distinguish between regexp literals and division operators
                        // A division operator is only possible in certain states
                        } elseif( $ch === '/' && !isset( $divStates[$state] ) ) {
-                               // Regexp literal, search to the end, skipping over backslash escapes and
-                               // character classes
+                               // Regexp literal
                                for( ; ; ) {
                                        do{
                                for( ; ; ) {
                                        do{
+                                               // Skip until we find "/" (end of regexp), "\" (backslash escapes),
+                                               // or "[" (start of character classes).
                                                $end += strcspn( $s, '/[\\', $end ) + 2;
                                                $end += strcspn( $s, '/[\\', $end ) + 2;
+                                               // If backslash escape, keep searching...
                                        } while( $end - 2 < $length && $s[$end - 2] === '\\' );
                                        $end--;
                                        } while( $end - 2 < $length && $s[$end - 2] === '\\' );
                                        $end--;
+                                       // If the end, stop here.
                                        if( $end - 1 >= $length || $s[$end - 1] === '/' ) {
                                                break;
                                        }
                                        if( $end - 1 >= $length || $s[$end - 1] === '/' ) {
                                                break;
                                        }
+                                       // (Implicit else), we must've found the start of a char class,
+                                       // skip until we find "]" (end of char class), or "\" (backslash escape)
                                        do{
                                                $end += strcspn( $s, ']\\', $end ) + 2;
                                        do{
                                                $end += strcspn( $s, ']\\', $end ) + 2;
+                                               // If backslash escape, keep searching...
                                        } while( $end - 2 < $length && $s[$end - 2] === '\\' );
                                        $end--;
                                };
                                        } while( $end - 2 < $length && $s[$end - 2] === '\\' );
                                        $end--;
                                };