DEFAULT_ARGS = array( 'amp_allowed_tags' => AMP_Allowed_Tags_Generated::get_allowed_tags(), 'amp_globally_allowed_attributes' => AMP_Allowed_Tags_Generated::get_allowed_attributes(), 'amp_layout_allowed_attributes' => AMP_Allowed_Tags_Generated::get_layout_attributes(), 'amp_bind_placeholder_prefix' => AMP_DOM_Utils::get_amp_bind_placeholder_prefix(), ); parent::__construct( $dom, $args ); if ( ! empty( $this->args['allow_dirty_styles'] ) ) { // Allow style attribute on all elements. $this->args['amp_globally_allowed_attributes']['style'] = array(); // Allow style elements. $this->args['amp_allowed_tags']['style'][] = array( 'attr_spec_list' => array( 'type' => array( 'value_casei' => 'text/css', ), ), 'cdata' => array(), 'tag_spec' => array( 'spec_name' => 'style for Customizer preview', ), ); // Allow stylesheet links. $this->args['amp_allowed_tags']['link'][] = array( 'attr_spec_list' => array( 'async' => array(), 'crossorigin' => array(), 'href' => array( 'mandatory' => true, ), 'integrity' => array(), 'media' => array(), 'rel' => array( 'dispatch_key' => 2, 'mandatory' => true, 'value_casei' => 'stylesheet', ), 'type' => array( 'value_casei' => 'text/css', ), ), 'tag_spec' => array( 'spec_name' => 'link rel=stylesheet for Customizer preview', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet ), ); } // Allow scripts if requested. if ( ! empty( $this->args['allow_dirty_scripts'] ) ) { $this->args['amp_allowed_tags']['script'][] = array( 'attr_spec_list' => array( 'type' => array(), 'src' => array(), 'async' => array(), 'defer' => array(), ), 'cdata' => array(), 'tag_spec' => array( 'spec_name' => 'scripts for Customizer preview', ), ); } // Prepare whitelists. $this->allowed_tags = $this->args['amp_allowed_tags']; foreach ( AMP_Rule_Spec::$additional_allowed_tags as $tag_name => $tag_rule_spec ) { $this->allowed_tags[ $tag_name ][] = $tag_rule_spec; } // @todo Do the same for body when !use_document_element? if ( ! empty( $this->args['use_document_element'] ) ) { foreach ( $this->allowed_tags['html'] as &$rule_spec ) { unset( $rule_spec[ AMP_Rule_Spec::TAG_SPEC ][ AMP_Rule_Spec::MANDATORY_PARENT ] ); } } foreach ( $this->allowed_tags as &$tag_specs ) { foreach ( $tag_specs as &$tag_spec ) { if ( isset( $tag_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ] ) ) { $tag_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ] = $this->process_alternate_names( $tag_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ] ); } } } $this->globally_allowed_attributes = $this->process_alternate_names( $this->args['amp_globally_allowed_attributes'] ); $this->layout_allowed_attributes = $this->process_alternate_names( $this->args['amp_layout_allowed_attributes'] ); } /** * Return array of values that would be valid as an HTML `script` element. * * Array keys are AMP element names and array values are their respective * Javascript URLs from https://cdn.ampproject.org * * @since 0.7 * @see amp_register_default_scripts() * * @return array() Returns component name as array key and true as value (or JavaScript URL string), * respectively. When true then the default component script URL will be used. * Will return an empty array if sanitization has yet to be run * or if it did not find any HTML elements to convert to AMP equivalents. */ public function get_scripts() { return array_fill_keys( $this->script_components, true ); } /** * Process alternative names in attribute spec list. * * @since 0.7 * * @param array $attr_spec_list Attribute spec list. * @return array Modified attribute spec list. */ private function process_alternate_names( $attr_spec_list ) { foreach ( $attr_spec_list as $attr_name => &$attr_spec ) { if ( '[' === $attr_name[0] ) { $placeholder_attr_name = $this->args['amp_bind_placeholder_prefix'] . trim( $attr_name, '[]' ); if ( ! isset( $attr_spec[ AMP_Rule_Spec::ALTERNATIVE_NAMES ] ) ) { $attr_spec[ AMP_Rule_Spec::ALTERNATIVE_NAMES ] = array(); } $attr_spec[ AMP_Rule_Spec::ALTERNATIVE_NAMES ][] = $placeholder_attr_name; } // Save all alternative names in lookup to improve performance. if ( isset( $attr_spec[ AMP_Rule_Spec::ALTERNATIVE_NAMES ] ) ) { foreach ( $attr_spec[ AMP_Rule_Spec::ALTERNATIVE_NAMES ] as $alternative_name ) { $this->rev_alternate_attr_name_lookup[ $alternative_name ] = $attr_name; } } } return $attr_spec_list; } /** * Sanitize the