Skip to content

Commit

Permalink
Remove DISALLOWED_TAG_MULTIPLE_CHOICES validation error code
Browse files Browse the repository at this point in the history
  • Loading branch information
pierlon committed Mar 20, 2020
1 parent 29eea3c commit 3c29860
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
5 changes: 5 additions & 0 deletions includes/sanitizers/class-amp-base-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ public function remove_invalid_child( $node, $validation_error = [] ) {

$should_remove = $this->should_sanitize_validation_error( $validation_error, compact( 'node' ) );
if ( $should_remove ) {
if ( null === $node->parentNode ) {
// Node no longer exists.
return $should_remove;
}

$node->parentNode->removeChild( $node );
} else {
$this->nodes_to_keep[ $node->nodeName ][] = $node;
Expand Down
24 changes: 15 additions & 9 deletions includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
class AMP_Tag_And_Attribute_Sanitizer extends AMP_Base_Sanitizer {

const DISALLOWED_TAG = 'DISALLOWED_TAG';
const DISALLOWED_TAG_MULTIPLE_CHOICES = 'DISALLOWED_TAG_MULTIPLE_CHOICES';
const DISALLOWED_CHILD_TAG = 'DISALLOWED_CHILD_TAG';
const DISALLOWED_FIRST_CHILD_TAG = 'DISALLOWED_FIRST_CHILD_TAG';
const INCORRECT_NUM_CHILD_TAGS = 'INCORRECT_NUM_CHILD_TAGS';
Expand Down Expand Up @@ -505,7 +504,15 @@ private function process_node( DOMElement $node ) {
array_unique(
array_map(
static function ( $validation_error ) {
unset( $validation_error['spec_name'] );
unset(
$validation_error['spec_name'],
// Remove other keys that may not not make the error unique.
$validation_error['required_parent_name'],
$validation_error['required_ancestor_name'],
$validation_error['required_child_count'],
$validation_error['required_min_child_count'],
$validation_error['required_attr_value']
);
return $validation_error;
},
$validation_errors
Expand All @@ -527,13 +534,12 @@ static function ( $validation_error ) {
);
} else {
// Otherwise, we have a rare condition where multiple tag specs fail for different reasons.
$this->remove_invalid_child(
$node,
[
'code' => self::DISALLOWED_TAG_MULTIPLE_CHOICES,
'errors' => $validation_errors,
]
);
foreach ( $validation_errors as $validation_error ) {
$this->remove_invalid_child(
$node,
$validation_error
);
}
}
}
return null;
Expand Down
21 changes: 8 additions & 13 deletions tests/php/test-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,14 @@ public function get_body_data() {
[],
[
[
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG_MULTIPLE_CHOICES,
'errors' => [
[
'code' => AMP_Tag_And_Attribute_Sanitizer::WRONG_PARENT_TAG,
'spec_name' => 'noscript enclosure for boilerplate',
'required_parent_name' => 'head'
],
[
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG_ANCESTOR,
'disallowed_ancestor' => 'noscript',
'spec_name' => 'noscript',
],
],
'code' => AMP_Tag_And_Attribute_Sanitizer::WRONG_PARENT_TAG,
'spec_name' => 'noscript enclosure for boilerplate',
'required_parent_name' => 'head'
],
[
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG_ANCESTOR,
'disallowed_ancestor' => 'noscript',
'spec_name' => 'noscript',
],
],
],
Expand Down

0 comments on commit 3c29860

Please sign in to comment.