Skip to content

Commit 84221e8

Browse files
committed
Keep required script components when invalid markup is kept
1 parent 58dd5c5 commit 84221e8

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,14 @@ static function ( $validation_error ) {
612612
if ( ! empty( $cdata ) && $node instanceof DOMElement ) {
613613
$validity = $this->validate_cdata_for_node( $node, $cdata );
614614
if ( true !== $validity ) {
615-
$this->remove_invalid_child(
615+
$sanitized = $this->remove_invalid_child(
616616
$node,
617617
array_merge(
618618
$validity,
619619
[ 'spec_name' => $this->get_spec_name( $node, $tag_spec ) ]
620620
)
621621
);
622-
return null;
622+
return $sanitized ? null : $script_components;
623623
}
624624
}
625625

@@ -656,8 +656,8 @@ static function ( $validation_error ) {
656656
// Remove the element if it is has an invalid layout.
657657
$layout_validity = $this->is_valid_layout( $tag_spec, $node );
658658
if ( true !== $layout_validity ) {
659-
$this->remove_invalid_child( $node, $layout_validity );
660-
return null;
659+
$sanitized = $this->remove_invalid_child( $node, $layout_validity );
660+
return $sanitized ? null : $script_components;
661661
}
662662

663663
// Identify attribute values that don't conform to the attr_spec.
@@ -758,54 +758,54 @@ static function ( $validation_error ) {
758758
// After attributes have been sanitized (and potentially removed), if mandatory attribute(s) are missing, remove the element.
759759
$missing_mandatory_attributes = $this->get_missing_mandatory_attributes( $merged_attr_spec_list, $node );
760760
if ( ! empty( $missing_mandatory_attributes ) ) {
761-
$this->remove_invalid_child(
761+
$sanitized = $this->remove_invalid_child(
762762
$node,
763763
[
764764
'code' => self::ATTR_REQUIRED_BUT_MISSING,
765765
'attributes' => $missing_mandatory_attributes,
766766
'spec_name' => $this->get_spec_name( $node, $tag_spec ),
767767
]
768768
);
769-
return null;
769+
return $sanitized ? null : $script_components;
770770
}
771771

772772
if ( ! empty( $tag_spec[ AMP_Rule_Spec::MANDATORY_ANYOF ] ) ) {
773773
$anyof_attributes = $this->get_element_attribute_intersection( $node, $tag_spec[ AMP_Rule_Spec::MANDATORY_ANYOF ] );
774774
if ( 0 === count( $anyof_attributes ) ) {
775-
$this->remove_invalid_child(
775+
$sanitized = $this->remove_invalid_child(
776776
$node,
777777
[
778778
'code' => self::MANDATORY_ANYOF_ATTR_MISSING,
779779
'mandatory_anyof_attrs' => $tag_spec[ AMP_Rule_Spec::MANDATORY_ANYOF ], // @todo Temporary as value can be looked up via spec name. See https://github.com/ampproject/amp-wp/pull/3817.
780780
'spec_name' => $this->get_spec_name( $node, $tag_spec ),
781781
]
782782
);
783-
return null;
783+
return $sanitized ? null : $script_components;
784784
}
785785
}
786786

787787
if ( ! empty( $tag_spec[ AMP_Rule_Spec::MANDATORY_ONEOF ] ) ) {
788788
$oneof_attributes = $this->get_element_attribute_intersection( $node, $tag_spec[ AMP_Rule_Spec::MANDATORY_ONEOF ] );
789789
if ( 0 === count( $oneof_attributes ) ) {
790-
$this->remove_invalid_child(
790+
$sanitized = $this->remove_invalid_child(
791791
$node,
792792
[
793793
'code' => self::MANDATORY_ONEOF_ATTR_MISSING,
794794
'mandatory_oneof_attrs' => $tag_spec[ AMP_Rule_Spec::MANDATORY_ONEOF ], // @todo Temporary as value can be looked up via spec name. See https://github.com/ampproject/amp-wp/pull/3817.
795795
'spec_name' => $this->get_spec_name( $node, $tag_spec ),
796796
]
797797
);
798-
return null;
798+
return $sanitized ? null : $script_components;
799799
} elseif ( count( $oneof_attributes ) > 1 ) {
800-
$this->remove_invalid_child(
800+
$sanitized = $this->remove_invalid_child(
801801
$node,
802802
[
803803
'code' => self::DUPLICATE_ONEOF_ATTRS,
804804
'duplicate_oneof_attrs' => $oneof_attributes,
805805
'spec_name' => $this->get_spec_name( $node, $tag_spec ),
806806
]
807807
);
808-
return null;
808+
return $sanitized ? null : $script_components;
809809
}
810810
}
811811

0 commit comments

Comments
 (0)