/**
  * Execute the minification rule.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     $elements = (new OptionalElementsRepository())->getElements();
     $contents = $context->getContents();
     foreach ($elements as $element) {
         $contents = $this->removeElement($element, $contents);
     }
     return $context->setContents($contents);
 }
 /**
  * Minify redundant whitespaces.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     $context->setContents($this->trailingWhitespaces($context->getContents()));
     $context->setContents($this->runMinificationRules($context->getContents()));
     $context->setContents($this->removeSpacesAroundPlaceholders($context->getContents()));
     return $context->setContents($this->maxHtmlLineLength($context->getContents(), $this->maxHtmlLineLength));
 }
 /**
  * Minify javascript prefixes on html event attributes.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     $contents = preg_replace_callback('/' . Constants::$htmlEventNamePrefix . Constants::ATTRIBUTE_NAME_REGEX . '   # Match an on{attribute}
             \\s*=\\s*             # Match equals sign with optional whitespaces around it
             ["\']?              # Match an optional quote
             \\s*javascript:      # Match the text "javascript:" which should be removed
         /xis', function ($match) {
         return str_replace('javascript:', '', $match[0]);
     }, $context->getContents());
     return $context->setContents($contents);
 }
 /**
  * Replace PHP tags with a temporary placeholder.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process($context)
 {
     $contents = $context->getContents();
     $contents = preg_replace_callback('/<\\?=((?!\\?>).)*\\?>/s', function ($match) use($context) {
         return $context->getPlaceholderContainer()->addPlaceholder($match[0]);
     }, $contents);
     $contents = preg_replace_callback('/<\\?php((?!\\?>).)*(\\?>)?/s', function ($match) use($context) {
         return $context->getPlaceholderContainer()->addPlaceholder($match[0]);
     }, $contents);
     return $context->setContents($contents);
 }
 public function process()
 {
     $options = [];
     collect(Input::get('options'))->each(function ($option) use(&$options) {
         $options[$option['name']] = strtolower($option['enabled']) === 'true';
     });
     $context = new MinifyContext(new PlaceholderContainer());
     $context->setContents(Input::get('html'));
     $minify = new Minify();
     $response = ['html' => $minify->run($context, $options)->getContents()];
     return Response::json($response);
 }
 /**
  * Replace remaining comments.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     return $context->setContents(preg_replace_callback('/
             <!          # search for the start of a comment
                 [^>]*   # search for everything without a ">"
             >           # match the end of the comment
         /xs', function ($match) {
         if (Str::contains(strtolower($match[0]), 'doctype')) {
             return $match[0];
         }
         return '';
     }, $context->getContents()));
 }
 /**
  * Execute the minification rule.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     return $context->setContents(preg_replace_callback('/
             (\\s*' . Constants::ATTRIBUTE_NAME_REGEX . '\\s*)     # Match the attribute name
             =\\s*                                            # Match the equal sign with optional whitespaces
             (["\'])                                         # Match quotes and capture for backreferencing
             \\s*                                             # Strange but possible to have a whitespace in an attribute
             \\2                                              # Backreference to the matched quote
             \\s*
         /x', function ($match) {
         if ($this->isBooleanAttribute($match[1])) {
             return Html::isLastAttribute($match[0]) ? $match[1] : $match[1] . ' ';
         }
         return Html::hasSurroundingAttributes($match[0]) ? ' ' : '';
     }, $context->getContents()));
 }
 /**
  * Execute the minification rule.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     return $context->setContents(preg_replace_callback('/
             =           # start matching by a equal sign
             \\s*         # its valid to use whitespaces after the equals sign
             (["\'])?    # match a single or double quote and make it a capturing group for backreferencing
                 (
                         (?:(?=\\1)|[^\\\\])*             # normal part of "unrolling the loop". Match no quote nor escaped char
                         (?:\\\\\\1                       # match the escaped quote
                             (?:(?=\\1)|[^\\\\])*         # normal part again
                         )*                              # special part of "unrolling the loop"
                 )       # use a the "unrolling the loop" technique to be able to skip escaped quotes like ="\\""
             \\1?         # match the same quote symbol as matched before
         /x', function ($match) {
         return $this->minifyAttribute($match);
     }, $context->getContents()));
 }
 /**
  * Replace critical content with a temporary placeholder.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process($context)
 {
     $contents = $context->getContents();
     $contents = $this->whitespaceBetweenInlineElements($contents, $context->getPlaceholderContainer());
     $contents = $this->whitespaceInInlineElements($contents, $context->getPlaceholderContainer());
     $contents = $this->replaceElements($contents, $context->getPlaceholderContainer());
     return $context->setContents($contents);
 }
 /**
  * Execute the minification rule.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     $booleanAttributes = new HtmlBooleanAttributeRepository();
     $booleanAttributes = implode('|', $booleanAttributes->getAttributes()->all());
     return $context->setContents(preg_replace_callback('/
             \\s                          # first match a whitespace which is an indication if its an attribute
             (' . $booleanAttributes . ')    # match and capture a boolean attribute
             \\s*
             =
             \\s*
             ([\'"])?                    # optional to use a quote
             (\\1|true|false|([\\s>"\']))    # match the boolean attribute name again or true|false
             \\2?                         # match the quote again
         /xi', function ($match) {
         if (isset($match[4])) {
             return ' ' . $match[1];
         }
         if ($match[3] == 'false') {
             return '';
         }
         return ' ' . $match[1];
     }, $context->getContents()));
 }
 /**
  * Minify redundant attributes which are not needed by the browser.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process(MinifyContext $context)
 {
     Collection::make($this->redundantAttributes)->each(function ($attributes, $element) use(&$context) {
         Collection::make($attributes)->each(function ($value, $attribute) use($element, &$context) {
             $contents = preg_replace_callback('/
                     ' . $element . '                    # Match the given element
                     ((?!\\s*' . $attribute . '\\s*=).)*   # Match everything except the given attribute
                     (
                         \\s*' . $attribute . '\\s*        # Match the attribute
                         =\\s*                        # Match the equals sign
                         (["\']?)                    # Match the opening quotes
                         \\s*' . $value . '\\s*            # Match the value
                         \\3?                         # Match the captured opening quotes again
                         \\s*
                     )
                 /xis', function ($match) {
                 return $this->removeAttribute($match[0], $match[2]);
             }, $context->getContents());
             $context->setContents($contents);
         });
     });
     return $context;
 }
 protected function createMinifyOutput()
 {
     $measurements = $this->minifyContext->getMeasurement();
     $referencePoints = Collection::make($measurements->getReferencePoints());
     $totalBytesSavedPercentage = 0;
     $lastReferencePoint = null;
     $rows = $referencePoints->map(function (ReferencePoint $referencePoint) use(&$lastReferencePoint, &$totalBytesSavedPercentage) {
         $bytesSaved = '';
         $bytesSavedPercentage = '';
         if ($lastReferencePoint != null) {
             $bytesSaved = $lastReferencePoint->getKiloBytes() - $referencePoint->getKiloBytes();
             $totalBytesSavedPercentage += $bytesSavedPercentage = $this->calculateImprovementPercentage($referencePoint->getKiloBytes(), $lastReferencePoint->getKiloBytes());
             $bytesSavedPercentage = round(abs($bytesSavedPercentage), 1) . '%';
         }
         $lastReferencePoint = $referencePoint;
         return [$referencePoint->getName(), round($referencePoint->getKiloBytes(), 1), round($bytesSaved, 1), $bytesSavedPercentage];
     });
     $rows[] = ['Total', round($referencePoints->last()->getKiloBytes(), 1), abs(round($referencePoints->last()->getKiloBytes() - $referencePoints->first()->getKiloBytes(), 1)), abs(round($totalBytesSavedPercentage, 1)) . '%'];
     $this->table(['Minification strategy', 'Size (KB)', 'Saved (KB)', 'Size (%)'], $rows);
 }
 /**
  * Replace blade tags with a temporary placeholder.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process($context)
 {
     $contents = $this->setEchosPlaceholder($context->getContents(), $context->getPlaceholderContainer());
     $contents = $this->setBladeControlStructuresPlaceholder($contents, $context->getPlaceholderContainer());
     return $context->setContents($contents);
 }
 /**
  * Replace critical content with a temp placeholder for integrity.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 public function process($context)
 {
     $context->setContents($this->setCDataPlaceholder($context->getContents(), $context->getPlaceholderContainer()));
     return $context->setContents($this->setConditionalCommentsPlaceholder($context->getContents(), $context->getPlaceholderContainer()));
 }
Example #15
0
 /**
  * Restore placeholders with their original content.
  *
  * @param \ArjanSchouten\HtmlMinifier\MinifyContext $context
  *
  * @return \ArjanSchouten\HtmlMinifier\MinifyContext
  */
 protected function restore(MinifyContext $context)
 {
     $withoutPlaceholders = $context->getPlaceholderContainer()->restorePlaceholders($context->getContents());
     return $context->setContents($withoutPlaceholders);
 }