Skip to content

Commit

Permalink
Merge pull request #6528 from ampproject/add/custom-script-allowance
Browse files Browse the repository at this point in the history
Add support for custom non-AMP scripts
  • Loading branch information
westonruter authored Aug 19, 2021
2 parents cd5ceab + f35c4e9 commit ebb4008
Show file tree
Hide file tree
Showing 12 changed files with 819 additions and 112 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext-json": "*",
"ext-libxml": "*",
"ext-spl": "*",
"ampproject/amp-toolbox": "dev-main#a155c37",
"ampproject/amp-toolbox": "dev-add/enforced-css-max-byte-count-transformed-identifier-config#718d9dad",
"cweagans/composer-patches": "~1.0",
"fasterimage/fasterimage": "1.5.0",
"sabberworm/php-css-parser": "dev-master#bfdd976"
Expand Down
51 changes: 25 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,11 @@ function amp_get_content_sanitizers( $post = null ) {
$native_post_forms_allowed = amp_is_native_post_form_allowed();

$sanitizers = [
// The AMP_Script_Sanitizer runs first because based on whether it allows custom scripts
// to be kept, it may impact the behavior of other sanitizers. For example, if custom
// scripts are kept then this is a signal that tree shaking in AMP_Style_Sanitizer cannot be
// performed.
AMP_Script_Sanitizer::class => [],
AMP_Embed_Sanitizer::class => [
'amp_to_amp_linking_enabled' => $amp_to_amp_linking_enabled,
],
Expand Down Expand Up @@ -1515,8 +1520,9 @@ function amp_get_content_sanitizers( $post = null ) {
'native_img_used' => $native_img_used,
],
AMP_Block_Sanitizer::class => [], // Note: Block sanitizer must come after embed / media sanitizers since its logic is using the already sanitized content.
AMP_Script_Sanitizer::class => [],
AMP_Style_Sanitizer::class => [],
AMP_Style_Sanitizer::class => [
'skip_tree_shaking' => is_customize_preview(),
],
AMP_Meta_Sanitizer::class => [],
AMP_Layout_Sanitizer::class => [],
AMP_Accessibility_Sanitizer::class => [],
Expand Down
16 changes: 14 additions & 2 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use AmpProject\Dom\Document;
use AmpProject\Extension;
use AmpProject\Optimizer;
use AmpProject\Optimizer\Configuration\TransformedIdentifierConfiguration;
use AmpProject\Optimizer\Transformer\TransformedIdentifier;
use AmpProject\RequestDestination;
use AmpProject\Tag;
use AmpProject\AmpWP\Optimizer\Transformer\AmpSchemaOrgMetadata;
Expand Down Expand Up @@ -1531,7 +1533,11 @@ public static function ensure_required_markup( Document $dom, $script_handles =
}

// When opting-in to POST forms, omit the amp-form component entirely since it blocks submission.
if ( amp_is_native_post_form_allowed() && $dom->xpath->query( '//form[ @action and @method and translate( @method, "POST", "post" ) = "post" ]' )->length > 0 ) {
if (
in_array( Extension::FORM, $script_handles, true )
&&
$dom->xpath->query( '//form[ @action and @method and translate( @method, "POST", "post" ) = "post" ]' )->length > 0
) {
$superfluous_script_handles[] = Extension::FORM;
}

Expand Down Expand Up @@ -2075,6 +2081,9 @@ public static function prepare_response( $response, $args = [] ) {
do_action( 'amp_server_timing_start', 'amp_optimizer' );

$errors = new Optimizer\ErrorCollection();

// @todo The dev mode attribute is being overloaded here. We should use something else.
$args['skip_css_max_byte_count_enforcement'] = $dom->documentElement->hasAttribute( DevMode::DEV_MODE_ATTRIBUTE );
self::get_optimizer( $args )->optimizeDom( $dom, $errors );

if ( count( $errors ) > 0 ) {
Expand Down Expand Up @@ -2164,10 +2173,13 @@ static function () use ( $args ) {
// Supply the Schema.org metadata, previously obtained just before output buffering began, to the AmpSchemaOrgMetadataConfiguration.
add_filter(
'amp_optimizer_config',
function ( $config ) {
function ( $config ) use ( $args ) {
if ( is_array( self::$metadata ) ) {
$config[ AmpSchemaOrgMetadata::class ][ AmpSchemaOrgMetadataConfiguration::METADATA ] = self::$metadata;
}
if ( ! empty( $args['skip_css_max_byte_count_enforcement'] ) ) {
$config[ TransformedIdentifier::class ][ TransformedIdentifierConfiguration::ENFORCED_CSS_MAX_BYTE_COUNT ] = false;
}
return $config;
},
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
Expand Down
11 changes: 11 additions & 0 deletions includes/sanitizers/class-amp-base-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ public function get_selector_conversion_mapping() {
return [];
}

/**
* Update args.
*
* Merges the supplied args with the existing args.
*
* @param array $args Args.
*/
public function update_args( $args ) {
$this->args = array_merge( $this->args, $args );
}

/**
* Run logic before any sanitizers are run.
*
Expand Down
Loading

0 comments on commit ebb4008

Please sign in to comment.