Skip to content

Commit

Permalink
Eliminate duplicated copy of spec in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 26, 2019
1 parent cf6c786 commit 545611e
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,25 @@ class AMP_Tag_And_Attribute_Sanitizer extends AMP_Base_Sanitizer {
* @param array $args Args.
*/
public function __construct( $dom, $args = [] ) {
// @todo It is pointless to have this DEFAULT_ARGS copying the array values. We should only get the data from AMP_Allowed_Tags_Generated.
$this->DEFAULT_ARGS = [
'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(),
];

parent::__construct( $dom, $args );

$this->allowed_tags = AMP_Allowed_Tags_Generated::get_allowed_tags();
$this->globally_allowed_attributes = AMP_Allowed_Tags_Generated::get_allowed_attributes();
$this->layout_allowed_attributes = AMP_Allowed_Tags_Generated::get_layout_attributes();

// @todo AMP dev mode should eventually be used instead of allow_dirty_styles.
if ( ! empty( $this->args['allow_dirty_styles'] ) ) {

// Allow style attribute on all elements.
$this->args['amp_globally_allowed_attributes']['style'] = [];
$this->globally_allowed_attributes['style'] = [];

// Remove restrictions on use of !important.
foreach ( $this->args['amp_allowed_tags']['style'] as &$style ) {
foreach ( $this->allowed_tags['style'] as &$style ) {
$style['cdata'] = [];
}

// Allow style elements.
$this->args['amp_allowed_tags']['style'][] = [
$this->allowed_tags['style'][] = [
'attr_spec_list' => [
'type' => [
'value_casei' => 'text/css',
Expand All @@ -176,7 +173,7 @@ public function __construct( $dom, $args = [] ) {
];

// Allow stylesheet links.
$this->args['amp_allowed_tags']['link'][] = [
$this->allowed_tags['link'][] = [
'attr_spec_list' => [
'async' => [],
'crossorigin' => [],
Expand All @@ -203,7 +200,7 @@ public function __construct( $dom, $args = [] ) {
// @todo AMP dev mode should eventually be used instead of allow_dirty_scripts.
// Allow scripts if requested.
if ( ! empty( $this->args['allow_dirty_scripts'] ) ) {
$this->args['amp_allowed_tags']['script'][] = [
$this->allowed_tags['script'][] = [
'attr_spec_list' => [
'type' => [],
'src' => [],
Expand All @@ -218,7 +215,6 @@ public function __construct( $dom, $args = [] ) {
}

// 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;
}
Expand All @@ -244,8 +240,8 @@ public function __construct( $dom, $args = [] ) {

unset( $tag_specs );

$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'] );
$this->globally_allowed_attributes = $this->process_alternate_names( $this->globally_allowed_attributes );
$this->layout_allowed_attributes = $this->process_alternate_names( $this->layout_allowed_attributes );
}

/**
Expand Down

0 comments on commit 545611e

Please sign in to comment.