Skip to content

Commit

Permalink
Fix css/js replacement leaving empty line
Browse files Browse the repository at this point in the history
  • Loading branch information
timotei-litespeed committed Nov 21, 2024
1 parent ecf079b commit 30c7563
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/optimize.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,9 @@ private function _parse_js()
$src_list = array();
$html_list = array();

// TODO: fix replacement will leave new line
$content = preg_replace('#<!--.*-->#sU', '', $this->content);
preg_match_all('#<script([^>]*)>(.*)</script>#isU', $content, $matches, PREG_SET_ORDER);
// V7 added: (?:\r\n?|\n?) to fix replacement leaving empty new line
$content = preg_replace('#<!--.*-->(?:\r\n?|\n?)#sU', '', $this->content);
preg_match_all('#<script([^>]*)>(.*)</script>(?:\r\n?|\n?)#isU', $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$attrs = empty($match[1]) ? array() : Utility::parse_attr($match[1]);

Expand Down Expand Up @@ -1060,9 +1060,9 @@ private function _parse_css()
// $dom->load( $content );return $val;
// $items = $dom->find( 'link' );

// TODO: fix replacement will leave new line
$content = preg_replace(array('#<!--.*-->#sU', '#<script([^>]*)>.*</script>#isU', '#<noscript([^>]*)>.*</noscript>#isU'), '', $this->content);
preg_match_all('#<link ([^>]+)/?>|<style([^>]*)>([^<]+)</style>#isU', $content, $matches, PREG_SET_ORDER);
// V7 added: (?:\r\n?|\n?) to fix replacement leaving empty new line
$content = preg_replace(array('#<!--.*-->(?:\r\n?|\n?)#sU', '#<script([^>]*)>.*</script>(?:\r\n?|\n?)#isU', '#<noscript([^>]*)>.*</noscript>(?:\r\n?|\n?)#isU'), '', $this->content);
preg_match_all('#<link ([^>]+)/?>|<style([^>]*)>([^<]+)</style>(?:\r\n?|\n?)#isU', $content, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
// to avoid multiple replacement
Expand Down

0 comments on commit 30c7563

Please sign in to comment.