diff --git a/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php b/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
index ada07196abb..47b93f0803e 100644
--- a/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
+++ b/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
@@ -1850,7 +1850,7 @@ private function remove_disallowed_descendants( $node, $allowed_descendants ) {
}
/**
- * Loop through node's children and remove the ones that are not whitelisted.
+ * Check whether the node validates the constraints for children.
*
* @param DOMNode $node Node.
* @param array $child_tags {
@@ -1878,24 +1878,22 @@ private function check_valid_children( $node, $child_tags ) {
}
// Verify that all of the child are among the set of allowed elements.
- $removed_count = 0;
if ( isset( $child_tags['child_tag_name_oneof'] ) ) {
foreach ( $child_elements as $child_element ) {
if ( ! in_array( $child_element->nodeName, $child_tags['child_tag_name_oneof'], true ) ) {
- $removed_count++;
- $this->remove_invalid_child( $child_element );
+ return false;
}
}
}
// If there aren't the exact number of elements, then mark this $node as being invalid.
if ( isset( $child_tags['mandatory_num_child_tags'] ) ) {
- return ( count( $child_elements ) - $removed_count ) === $child_tags['mandatory_num_child_tags'];
+ return count( $child_elements ) === $child_tags['mandatory_num_child_tags'];
}
// If there aren't enough elements, then mark this $node as being invalid.
if ( isset( $child_tags['mandatory_min_num_child_tags'] ) ) {
- return ( count( $child_elements ) - $removed_count ) >= $child_tags['mandatory_min_num_child_tags'];
+ return count( $child_elements ) >= $child_tags['mandatory_min_num_child_tags'];
}
return true;
diff --git a/tests/php/test-tag-and-attribute-sanitizer.php b/tests/php/test-tag-and-attribute-sanitizer.php
index 04b12cce509..df6931b5cdb 100644
--- a/tests/php/test-tag-and-attribute-sanitizer.php
+++ b/tests/php/test-tag-and-attribute-sanitizer.php
@@ -72,17 +72,23 @@ public function get_body_data() {
],
'amp-animation' => [
- ' bad Bunch of awesome content.Section 1