-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from ampproject/add/14-minify-html-transformer
- Loading branch information
Showing
8 changed files
with
717 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/Optimizer/Configuration/MinifyHtmlConfiguration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace AmpProject\Optimizer\Configuration; | ||
|
||
use AmpProject\Optimizer\Exception\InvalidConfigurationValue; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* Configuration for the MinifyHtml transformer. | ||
* | ||
* @property bool $minify Whether minification is enabled. | ||
* @property bool $minifyAmpScript Whether amp-script minification is enabled. | ||
* @property bool $minifyJSON Whether JSON data minification is enabled. | ||
* @property bool $collapseWhitespace Whether collapsing whitespace is enabled. | ||
* @property bool $removeComments Whether comments should be removed. | ||
* @property bool $canCollapseWhitespace Whether whitespace can be collapsed. | ||
* @property bool $inBody Whether the node is in the body. | ||
* @property string $commentIgnorePattern Regex pattern of comments to keep. | ||
* | ||
* @package ampproject/amp-toolbox | ||
*/ | ||
final class MinifyHtmlConfiguration extends BaseTransformerConfiguration | ||
{ | ||
|
||
/** | ||
* Whether minification is enabled. | ||
* | ||
* @var bool | ||
*/ | ||
const MINIFY = 'minify'; | ||
|
||
/** | ||
* Whether amp-script minification is enabled. | ||
* | ||
* @var bool | ||
*/ | ||
const MINIFY_AMP_SCRIPT = 'minifyAmpScript'; | ||
|
||
/** | ||
* Whether JSON data minification is enabled. | ||
* | ||
* @var bool | ||
*/ | ||
const MINIFY_JSON = 'minifyJSON'; | ||
|
||
/** | ||
* Whether collapsing whitespace is enabled. | ||
* | ||
* @var bool | ||
*/ | ||
const COLLAPSE_WHITESPACE = 'collapseWhitespace'; | ||
|
||
/** | ||
* Whether comments should be removed. | ||
* | ||
* @var bool | ||
*/ | ||
const REMOVE_COMMENTS = 'removeComments'; | ||
|
||
/** | ||
* Regular expression pattern of comments to keep. | ||
* | ||
* @var string | ||
*/ | ||
const COMMENT_IGNORE_PATTERN = 'commentIgnorePattern'; | ||
|
||
/** | ||
* Get the associative array of allowed keys and their respective default values. | ||
* | ||
* The array index is the key and the array value is the key's default value. | ||
* | ||
* @return array Associative array of allowed keys and their respective default values. | ||
*/ | ||
protected function getAllowedKeys() | ||
{ | ||
return [ | ||
self::MINIFY => true, | ||
self::MINIFY_AMP_SCRIPT => false, | ||
self::MINIFY_JSON => true, | ||
self::COLLAPSE_WHITESPACE => true, | ||
self::REMOVE_COMMENTS => true, | ||
self::COMMENT_IGNORE_PATTERN => '', | ||
]; | ||
} | ||
|
||
/** | ||
* Validate an individual configuration entry. | ||
* | ||
* @param string $key Key of the configuration entry to validate. | ||
* @param mixed $value Value of the configuration entry to validate. | ||
* @return mixed Validated value. | ||
*/ | ||
protected function validate($key, $value) | ||
{ | ||
switch ($key) { | ||
case self::MINIFY: | ||
case self::MINIFY_JSON: | ||
case self::COLLAPSE_WHITESPACE: | ||
case self::REMOVE_COMMENTS: | ||
if (! is_bool($value)) { | ||
throw InvalidConfigurationValue::forInvalidSubValueType( | ||
self::class, | ||
$key, | ||
'boolean', | ||
is_object($value) ? get_class($value) : gettype($value) | ||
); | ||
} | ||
break; | ||
|
||
case self::MINIFY_AMP_SCRIPT: | ||
if (! is_bool($value)) { | ||
throw InvalidConfigurationValue::forInvalidSubValueType( | ||
self::class, | ||
$key, | ||
'boolean', | ||
is_object($value) ? get_class($value) : gettype($value) | ||
); | ||
} | ||
|
||
if ($value) { | ||
$message = 'Script minification feature is not implemented yet.'; | ||
throw new InvalidArgumentException($message); | ||
} | ||
break; | ||
|
||
case self::COMMENT_IGNORE_PATTERN: | ||
if (! is_string($value)) { | ||
throw InvalidConfigurationValue::forInvalidSubValueType( | ||
self::class, | ||
self::COMMENT_IGNORE_PATTERN, | ||
'string', | ||
is_object($value) ? get_class($value) : gettype($value) | ||
); | ||
} | ||
$value = trim($value); | ||
break; | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace AmpProject\Optimizer\Error; | ||
|
||
use AmpProject\Optimizer\Error; | ||
|
||
/** | ||
* Optimizer error object for invalid JSON data. | ||
* | ||
* @package ampproject/amp-toolbox | ||
*/ | ||
final class InvalidJson implements Error | ||
{ | ||
use ErrorProperties; | ||
|
||
/** | ||
* Instantiate an InvalidJson object after decoding JSON data. | ||
* | ||
* @return self | ||
*/ | ||
public static function fromLastErrorMsgAfterDecoding() | ||
{ | ||
$errorMsg = 'Error decoding JSON: ' . json_last_error_msg(); | ||
return new self($errorMsg); | ||
} | ||
|
||
/** | ||
* Instantiate an InvalidJson object after encoding JSON data. | ||
* | ||
* @return self | ||
*/ | ||
public static function fromLastErrorMsgAfterEncoding() | ||
{ | ||
$errorMsg = 'Error encoding JSON: ' . json_last_error_msg(); | ||
return new self($errorMsg); | ||
} | ||
} |
Oops, something went wrong.