Autoptimize fails to aggregate inline Javascript containing 'src=""'
-
Bug:
Autoptimize fails to minify inline Javascript where the inline Javascript includes the text src=””, despite Autoptimize being set to aggregate inline JS.Environment:
WordPress 4.5.2
Autoptimize 2.0.2
Autoptimize settings:
– Optimize Javascript Code: Yes
– Force Javascript in head: No
– Also aggregate inline JS: YesSteps to Reproduce:
1. Setup environment as above.
2. Install https://gist.github.com/n7studios/5a54365fb31b159ca3cc1b8246eb3d74 as a WordPress Plugin, activating it.
3. Load any WordPress Page.
4. View the Page source, noting that the following script is still displayed and not included in Autoptimize’s minified JS:<script type="text/javascript"> console.log( '<img src="https://placehold.it/400x400" />' ); </script>
Cause:
This is caused by a greedy preg_match at classes/autoptimizeScripts.php:99:
preg_match('#src=("|\')(.*)("|\')#Usmi',$tag,$source)
This attempts to find the
src
tag within the <script></script> block to determine whether a script is inline or external. However, it greedily includes any other src tags that may be present, trying to validate them and therefore failing to include them in minification.Resolution:
For each script tag, once the first src match has been found, there’s no need to continue through that inline script block. The outerpreg_match_all
at line 92 will still catch all scripts, ensuring Autoptimize works correctly.
- The topic ‘Autoptimize fails to aggregate inline Javascript containing 'src=""'’ is closed to new replies.