Skip to content

Commit 58dd5c5

Browse files
committed
Gather required script components once tag spec identified
1 parent c6aed8f commit 58dd5c5

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,42 @@ static function ( $validation_error ) {
572572
}
573573
}
574574

575+
$merged_attr_spec_list = array_merge(
576+
$this->globally_allowed_attributes,
577+
$attr_spec_list
578+
);
579+
580+
// Add required AMP component scripts.
581+
$script_components = [];
582+
if ( ! empty( $tag_spec['requires_extension'] ) ) {
583+
$script_components = array_merge( $script_components, $tag_spec['requires_extension'] );
584+
}
585+
586+
// Add required AMP components for attributes.
587+
foreach ( $node->attributes as $attribute ) {
588+
if ( isset( $merged_attr_spec_list[ $attribute->nodeName ]['requires_extension'] ) ) {
589+
$script_components = array_merge(
590+
$script_components,
591+
$merged_attr_spec_list[ $attribute->nodeName ]['requires_extension']
592+
);
593+
}
594+
}
595+
596+
// Manually add components for attributes; this is hard-coded because attributes do not have requires_extension like tags do. See <https://github.com/ampproject/amp-wp/issues/1808>.
597+
if ( $node->hasAttribute( 'lightbox' ) ) {
598+
$script_components[] = 'amp-lightbox-gallery';
599+
}
600+
601+
// Check if element needs amp-bind component.
602+
if ( $node instanceof DOMElement && ! in_array( 'amp-bind', $this->script_components, true ) ) {
603+
foreach ( $node->attributes as $name => $value ) {
604+
if ( Document::AMP_BIND_DATA_ATTR_PREFIX === substr( $name, 0, 14 ) ) {
605+
$script_components[] = 'amp-bind';
606+
break;
607+
}
608+
}
609+
}
610+
575611
// Remove element if it has illegal CDATA.
576612
if ( ! empty( $cdata ) && $node instanceof DOMElement ) {
577613
$validity = $this->validate_cdata_for_node( $node, $cdata );
@@ -587,11 +623,6 @@ static function ( $validation_error ) {
587623
}
588624
}
589625

590-
$merged_attr_spec_list = array_merge(
591-
$this->globally_allowed_attributes,
592-
$attr_spec_list
593-
);
594-
595626
// Amend spec list with layout.
596627
if ( isset( $tag_spec['amp_layout'] ) ) {
597628
$merged_attr_spec_list = array_merge( $merged_attr_spec_list, $this->layout_allowed_attributes );
@@ -778,37 +809,6 @@ static function ( $validation_error ) {
778809
}
779810
}
780811

781-
// Add required AMP component scripts.
782-
$script_components = [];
783-
if ( ! empty( $tag_spec['requires_extension'] ) ) {
784-
$script_components = array_merge( $script_components, $tag_spec['requires_extension'] );
785-
}
786-
787-
// Add required AMP components for attributes.
788-
foreach ( $node->attributes as $attribute ) {
789-
if ( isset( $merged_attr_spec_list[ $attribute->nodeName ]['requires_extension'] ) ) {
790-
$script_components = array_merge(
791-
$script_components,
792-
$merged_attr_spec_list[ $attribute->nodeName ]['requires_extension']
793-
);
794-
}
795-
}
796-
797-
// Manually add components for attributes; this is hard-coded because attributes do not have requires_extension like tags do. See <https://github.com/ampproject/amp-wp/issues/1808>.
798-
if ( $node->hasAttribute( 'lightbox' ) ) {
799-
$script_components[] = 'amp-lightbox-gallery';
800-
}
801-
802-
// Check if element needs amp-bind component.
803-
if ( $node instanceof DOMElement && ! in_array( 'amp-bind', $this->script_components, true ) ) {
804-
foreach ( $node->attributes as $name => $value ) {
805-
if ( Document::AMP_BIND_DATA_ATTR_PREFIX === substr( $name, 0, 14 ) ) {
806-
$script_components[] = 'amp-bind';
807-
break;
808-
}
809-
}
810-
}
811-
812812
return $script_components;
813813
}
814814

0 commit comments

Comments
 (0)