Skip to content

FL_Rest::strip_spaces() introduces errors in Javascript code #1

Description

@TBarregren

FL_Rest::strip_spaces() is intended as "the poor man's" Javascript magnifier; it removes all white spaces in the Javascript. Unfortunately, it can introduce errors.

Some examples how bb-plugin/modules/post-grid/js/frontend.js is mangled by this method:

  • The lack of terminating semicolon on line 122 (which is valid JavaScript, although not recommend) creates syntax error when the white space is removed.

  • return path on line 160 is mangled to returnpath which gives syntax error.

  • The comment on line 215 will after removal of white spaces stretch to the end of the file.

One solution is to replace preg_replace('/[\n\r\t\s]/', '', $txt ) with

preg_replace(["/\s+[\r\n]/", "/[\r\n]\s+/", "/[\t ]+/"], ["\n", "\n ", " "], $txt )

Here is a patch doing just that:

`diff --git a/classes/class-fl-builder-rest.php b/classes/class-fl-builder-rest.php
index 7c294d3..606c119 100644
--- a/classes/class-fl-builder-rest.php
+++ b/classes/class-fl-builder-rest.php
@@ -18,6 +18,6 @@ final class FL_Rest{
        }
 
        static function strip_spaces( $txt ) {
-               return preg_replace('/[\n\r\t\s]/', '', $txt );
+               return preg_replace(["/\s+[\r\n]/", "/[\r\n]\s+/", "/[\t ]+/"], ["\n", "\n ", " "], $txt );
        }
 }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions