@@ -149,68 +149,49 @@ <h2 class="text-lg font-semibold">Minified Code</h2>
149149 // Enhanced minification function
150150 const minifyPHP = ( code ) => {
151151 if ( ! code . trim ( ) ) return '' ;
152- let result = code ;
153152 const options = {
154153 removeComments : document . getElementById ( 'remove-comments' ) . checked ,
155154 removeWhitespace : document . getElementById ( 'remove-whitespace' ) . checked
156155 } ;
157-
158156 try {
159- // Remove comments if enabled
157+ let result = code ;
158+ const stringMap = new Map ( ) ;
159+ let placeholderId = 0 ;
160+ // Preserve strings first
161+ result = result . replace ( / " (?: [ ^ " \\ ] | \\ .) * " / g, match => {
162+ const placeholder = `__STR_${ placeholderId ++ } __` ;
163+ stringMap . set ( placeholder , match ) ;
164+ return placeholder ;
165+ } ) . replace ( / ' (?: [ ^ ' \\ ] | \\ .) * ' / g, match => {
166+ const placeholder = `__STR_${ placeholderId ++ } __` ;
167+ stringMap . set ( placeholder , match ) ;
168+ return placeholder ;
169+ } ) . replace ( / < < < ( [ ' " ] ? ) ( \w + ) \1[ \s \S ] * ?\r ? \n \2; ? / g, match => {
170+ const placeholder = `__STR_${ placeholderId ++ } __` ;
171+ stringMap . set ( placeholder , match ) ;
172+ return placeholder ;
173+ } ) ;
174+ // Remove comments
160175 if ( options . removeComments ) {
161- // Remove multi-line comments
162- result = result . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' ) ;
163- // Remove single-line comments (careful with URLs in strings)
164- result = result . replace ( / (?< ! [: " ' ] ) \/ \/ [ ^ \n ] * | (?< ! " ) # [ ^ \n ] * / g, '' ) ;
176+ result = result . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' )
177+ . replace ( / \/ \/ [ ^ \r \n ] * | # [ ^ \r \n ] * / g, '' ) ;
165178 }
166-
167- // Remove whitespace if enabled
179+ // Handle whitespace
168180 if ( options . removeWhitespace ) {
169- // This is a smarter approach that preserves strings
170- let inString = false ;
171- let stringChar = '' ;
172- let newStr = '' ;
173- let lastChar = '' ;
174-
175- for ( let i = 0 ; i < result . length ; i ++ ) {
176- const char = result [ i ] ;
177-
178- // Handle string boundaries
179- if ( ( char === "'" || char === '"' ) && ( lastChar !== '\\' || ( lastChar === '\\' && result [ i - 2 ] === '\\' ) ) ) {
180- if ( ! inString ) {
181- inString = true ;
182- stringChar = char ;
183- newStr += char ;
184- } else if ( stringChar === char ) {
185- inString = false ;
186- newStr += char ;
187- } else {
188- newStr += char ;
189- }
190- }
191- // Handle whitespace
192- else if ( ! inString && / \s / . test ( char ) ) {
193- // Add a space only if needed for syntax
194- if ( newStr . length > 0 && ! / \s / . test ( lastChar ) &&
195- ! [ '(' , '[' , '{' , '.' , '!' , '+' , '-' , '*' , '/' , '%' , '=' , '<' , '>' , '?' , ':' , ';' , ',' ] . includes ( lastChar ) &&
196- ( i + 1 < result . length && ! [ ')' , ']' , '}' , '.' , '+' , '-' , '*' , '/' , '%' , '=' , '<' , '>' , '?' , ':' , ';' , ',' ] . includes ( result [ i + 1 ] ) ) ) {
197- newStr += ' ' ;
198- }
199- } else {
200- newStr += char ;
201- }
202-
203- lastChar = char ;
204- }
205-
206- result = newStr . trim ( ) ;
181+ result = result . replace ( / \s + / g, ' ' )
182+ . replace ( / \s * ( [ ; , { } ( ) [ \] . = < > : ? ! + \- * \/ % & | ^ ] ) \s * / g, '$1' )
183+ . replace ( / \s * ( = = = | ! = = | = = | ! = | < = | > = | = > | - > | : : | \| \| | & & | \+ = | - = | \* = | \/ = | % = | \. = | \^ = | & = | \| = | < < | > > ) \s * / g, '$1' )
184+ . trim ( ) ;
207185 }
208-
186+ // Restore strings
187+ stringMap . forEach ( ( value , key ) => {
188+ result = result . replace ( key , value ) ;
189+ } ) ;
209190 return result ;
210191 } catch ( error ) {
211192 showToast ( 'Error during minification: ' + error . message , 'error' ) ;
212193 console . error ( 'Minification error:' , error ) ;
213- return code ; // Return original on error
194+ return code ;
214195 }
215196 } ;
216197
0 commit comments