Skip to content

Commit

Permalink
Coding Standards: uppercase constants
Browse files Browse the repository at this point in the history
mjangda committed Aug 4, 2017

Unverified

This user has not yet uploaded their public signing key.
1 parent 78195a9 commit 31025ed
Showing 2 changed files with 269 additions and 272 deletions.
365 changes: 181 additions & 184 deletions includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ private function process_node( $node ) {
$rule_spec_list = $this->allowed_tags[ $node->nodeName ];
}
foreach ( $rule_spec_list as $id => $rule_spec ) {
if ( $this->validate_tag_spec_for_node( $node, $rule_spec[AMP_Rule_Spec::tag_spec] ) ) {
if ( $this->validate_tag_spec_for_node( $node, $rule_spec[AMP_Rule_Spec::TAG_SPEC] ) ) {
$rule_spec_list_to_validate[ $id ] = $rule_spec;
}
}
@@ -116,7 +116,7 @@ private function process_node( $node ) {
// validate the node's attributes.
if ( 1 == count( $rule_spec_list_to_validate ) ) {
$rule_spec = array_pop( $rule_spec_list_to_validate );
$attr_spec_list = $rule_spec[AMP_Rule_Spec::attr_spec_list];
$attr_spec_list = $rule_spec[AMP_Rule_Spec::ATTR_SPEC_LIST];

// If there is more than one valid rule_spec for this node, then
// try to deduce which one is intended by inspecting the node's
@@ -127,15 +127,15 @@ private function process_node( $node ) {
// attributes and values match the node.
$attr_spec_scores = array();
foreach ( $rule_spec_list_to_validate as $spec_id => $rule_spec ) {
$attr_spec_scores[ $spec_id ] = $this->validate_attr_spec_list_for_node( $node, $rule_spec[AMP_Rule_Spec::attr_spec_list] );
$attr_spec_scores[ $spec_id ] = $this->validate_attr_spec_list_for_node( $node, $rule_spec[AMP_Rule_Spec::ATTR_SPEC_LIST] );
}

// Get the key(s) to the highest score(s).
$spec_ids_sorted = array_keys( $attr_spec_scores, max( $attr_spec_scores ) );

// If there is exactly one attr_spec with a max score, use that one.
if ( 1 == count( $spec_ids_sorted ) ) {
$attr_spec_list = $rule_spec_list_to_validate[ $spec_ids_sorted[0] ][AMP_Rule_Spec::attr_spec_list];
$attr_spec_list = $rule_spec_list_to_validate[ $spec_ids_sorted[0] ][AMP_Rule_Spec::ATTR_SPEC_LIST];

// Otherwise...
} else {
@@ -144,7 +144,7 @@ private function process_node( $node ) {
// be used. Try to merge the top scoring ones and cross
// your fingers.
foreach( $spec_ids_sorted as $id ) {
$attr_spec_list = array_merge( $attr_spec_list, $rule_spec_list_to_validate[ $id ][AMP_Rule_Spec::attr_spec_list] );
$attr_spec_list = array_merge( $attr_spec_list, $rule_spec_list_to_validate[ $id ][AMP_Rule_Spec::ATTR_SPEC_LIST] );
}
}
}
@@ -168,21 +168,21 @@ private function process_node( $node ) {
*/
private function validate_tag_spec_for_node( $node, $tag_spec ) {

if ( ! empty( $tag_spec[AMP_Rule_Spec::mandatory_parent] ) &&
! $this->has_parent( $node, $tag_spec[AMP_Rule_Spec::mandatory_parent] ) ) {
if ( ! empty( $tag_spec[AMP_Rule_Spec::MANDATORY_PARENT] ) &&
! $this->has_parent( $node, $tag_spec[AMP_Rule_Spec::MANDATORY_PARENT] ) ) {
return false;
}

if ( ! empty( $tag_spec[AMP_Rule_Spec::disallowed_ancestor] ) ) {
foreach ( $tag_spec[AMP_Rule_Spec::disallowed_ancestor] as $disallowed_ancestor_node_name ) {
if ( ! empty( $tag_spec[AMP_Rule_Spec::DISALLOWED_ANCESTOR] ) ) {
foreach ( $tag_spec[AMP_Rule_Spec::DISALLOWED_ANCESTOR] as $disallowed_ancestor_node_name ) {
if ( $this->has_ancestor( $node, $disallowed_ancestor_node_name ) ) {
return false;
}
}
}

if ( ! empty( $tag_spec[AMP_Rule_Spec::mandatory_ancestor] ) &&
! $this->has_ancestor( $node, $tag_spec[AMP_Rule_Spec::mandatory_ancestor] ) ) {
if ( ! empty( $tag_spec[AMP_Rule_Spec::MANDATORY_ANCESTOR] ) &&
! $this->has_ancestor( $node, $tag_spec[AMP_Rule_Spec::MANDATORY_ANCESTOR] ) ) {
return false;
}

@@ -203,8 +203,8 @@ private function validate_attr_spec_list_for_node( $node, $attr_spec_list ) {
}

foreach( $node->attributes as $attr_name => $attr_node ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] as $attr_alt_name ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] as $attr_alt_name ) {
$attr_spec_list[ $attr_alt_name ] = $attr_spec_list[ $attr_name ];
}
}
@@ -219,83 +219,83 @@ private function validate_attr_spec_list_for_node( $node, $attr_spec_list ) {
foreach ( $attr_spec_list as $attr_name => $attr_spec_rule ) {

// If a mandatory attribute is required, and attribute exists, pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::mandatory] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_mandatory( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::MANDATORY] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_mandatory( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// Check 'value' - case sensitive
// Given attribute's value must exactly equal value of the rule to pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_value( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_value( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// Check 'value_regex' - case sensitive regex match
// Given attribute's value must be a case insensitive match to regex pattern
// specified by the value of rule to pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_regex] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// Check 'value_casei' - case insensitive
// Given attribute's value must be a case insensitive match to the value of
// the rule to pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_casei] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_value_casei( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_CASEI] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_value_casei( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// Check 'value_regex_casei' - case insensitive regex match
// Given attribute's value must be a case insensitive match to the regex
// pattern specified by the value of the rule to pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_regex_casei] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_value_regex_casei( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX_CASEI] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_value_regex_casei( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// If given attribute's value is a URL with a protocol, the protocol must
// be in the array specified by the rule's value to pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::allowed_protocol] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOWED_PROTOCOL] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// If the given attribute's value is *not* a relative path, and the rule's
// value is `false`, then pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::allow_relative] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_disallowed_relative( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOW_RELATIVE] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_disallowed_relative( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// If the given attribute's value exists, is non-empty and the rule's value
// is false, then pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::allow_empty] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_disallowed_empty( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOW_EMPTY] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_disallowed_empty( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// If the given attribute's value is a URL and does not match any of the list
// of domains in the value of the rule, then pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::disallowed_domain] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_disallowed_domain( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::DISALLOWED_DOMAIN] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_disallowed_domain( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}

// If the attribute's value exists and does not match the regex specified
// by the rule's value, then pass.
if ( isset( $attr_spec_rule[AMP_Rule_Spec::blacklisted_value_regex] ) ) {
if ( AMP_Rule_Spec::pass == $this->check_attr_spec_rule_blacklisted_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::BLACKLISTED_VALUE_REGEX] ) ) {
if ( AMP_Rule_Spec::PASS == $this->check_attr_spec_rule_blacklisted_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
$score += 1;
}
}
@@ -326,8 +326,8 @@ private function sanitize_disallowed_attributes_in_node( $node, $attr_spec_list
// Make sure we're not removing an attribtue that is listed as an alternative
// for some other allowed attribtue. ex. 'srcset' is an alternative for 'src'.
foreach ( $attr_spec_list as $attr_name => $attr_spec_rule_value ) {
if ( isset( $attr_spec_rule_value[AMP_Rule_Spec::alternative_names] ) ) {
foreach ( $attr_spec_rule_value[AMP_Rule_Spec::alternative_names] as $alternative_name ) {
if ( isset( $attr_spec_rule_value[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach ( $attr_spec_rule_value[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alternative_name ) {
$alt_name_keys = array_keys( $attrs_to_remove, $alternative_name, true );
if ( ! empty( $alt_name_keys ) ) {
unset( $attrs_to_remove[ $alt_name_keys[0] ] );
@@ -357,8 +357,8 @@ private function _sanitize_disallowed_attribute_values_in_node( $node, $attr_spe
$attrs_to_remove = array();

foreach( $attr_spec_list as $attr_name => $attr_val ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] as $attr_alt_name ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] as $attr_alt_name ) {
$attr_spec_list[ $attr_alt_name ] = $attr_spec_list[ $attr_name ];
}
}
@@ -372,65 +372,65 @@ private function _sanitize_disallowed_attribute_values_in_node( $node, $attr_spe

$attr_spec_rule = $attr_spec_list[$attr_name];

if ( isset( $attr_spec_rule[AMP_Rule_Spec::value] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_value( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_value( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_casei] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_value_casei( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_CASEI] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_value_casei( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_regex] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_regex_casei] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_value_regex_casei( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX_CASEI] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_value_regex_casei( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::allowed_protocol] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOWED_PROTOCOL] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::allow_relative] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_disallowed_relative( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOW_RELATIVE] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_disallowed_relative( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::allow_empty] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_disallowed_empty( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOW_EMPTY] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_disallowed_empty( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::disallowed_domain] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_disallowed_domain( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::DISALLOWED_DOMAIN] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_disallowed_domain( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}

if ( isset( $attr_spec_rule[AMP_Rule_Spec::blacklisted_value_regex] ) &&
AMP_Rule_Spec::fail == $this->check_attr_spec_rule_blacklisted_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::BLACKLISTED_VALUE_REGEX] ) &&
AMP_Rule_Spec::FAIL == $this->check_attr_spec_rule_blacklisted_value_regex( $node, $attr_name, $attr_spec_rule ) ) {
$attrs_to_remove[] = $attr_name;
continue;
}
}

// Remove the disallowed values
foreach ( $attrs_to_remove as $attr_name ) {
if ( isset( $attr_spec_list[$attr_name][AMP_Rule_Spec::allow_empty] ) &&
( true == $attr_spec_list[$attr_name][AMP_Rule_Spec::allow_empty] ) ) {
if ( isset( $attr_spec_list[$attr_name][AMP_Rule_Spec::ALLOW_EMPTY] ) &&
( true == $attr_spec_list[$attr_name][AMP_Rule_Spec::ALLOW_EMPTY] ) ) {
$attr = $node->attributes;
$attr[ $attr_name ]->value = '';
} else {
@@ -444,165 +444,165 @@ private function _sanitize_disallowed_attribute_values_in_node( $node, $attr_spe
* whether the attribute (or a specified alternate) exists.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name is mandatory and it exists
* - AMP_Rule_Spec::fail - $attr_name is mandatory, but doesn't exist
* - AMP_Rule_Spec::not_applicable - $attr_name is not mandatory
* - AMP_Rule_Spec::PASS - $attr_name is mandatory and it exists
* - AMP_Rule_Spec::FAIL - $attr_name is mandatory, but doesn't exist
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name is not mandatory
*/
private function check_attr_spec_rule_mandatory( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::mandatory] ) &&
( true == $attr_spec_rule[AMP_Rule_Spec::mandatory] ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::MANDATORY] ) &&
( true == $attr_spec_rule[AMP_Rule_Spec::MANDATORY] ) ) {
if ( $node->hasAttribute( $attr_name ) ) {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
} else {
// check if an alternative name list is specified
if ( isset( $attr_spec_rule[AMP_Rule_Spec::alternative_names] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::alternative_names] as $alt_name ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alt_name ) {
if ( $node->hasAttribute( $alt_name ) ) {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
}
}

return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a value rule, and whether
* the attribute matches the value.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_value( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value] ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE] ) ) {
if ( $node->hasAttribute( $attr_name ) ) {
if ( $node->getAttribute( $attr_name ) == $attr_spec_rule[AMP_Rule_Spec::value] ) {
return AMP_Rule_Spec::pass;
if ( $node->getAttribute( $attr_name ) == $attr_spec_rule[AMP_Rule_Spec::VALUE] ) {
return AMP_Rule_Spec::PASS;
} else {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::alternative_names] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::alternative_names] as $alternative_name ) {
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alternative_name ) {
if ( $node->hasAttribute( $alternative_name ) ) {
if ( $node->getAttribute( $alternative_name ) == $attr_spec_rule[AMP_Rule_Spec::value] ) {
return AMP_Rule_Spec::pass;
if ( $node->getAttribute( $alternative_name ) == $attr_spec_rule[AMP_Rule_Spec::VALUE] ) {
return AMP_Rule_Spec::PASS;
} else {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
}
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a value rule, and whether
* or not the value matches the rule without respect to case.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_value_casei( $node, $attr_name, $attr_spec_rule ) {
// check 'value_casei' - case insensitive
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_casei] ) ) {
$rule_value = strtolower( $attr_spec_rule[AMP_Rule_Spec::value_casei] );
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_CASEI] ) ) {
$rule_value = strtolower( $attr_spec_rule[AMP_Rule_Spec::VALUE_CASEI] );
if ( $node->hasAttribute( $attr_name ) ) {
$attr_value = strtolower( $node->getAttribute( $attr_name ) );
if ( $attr_value == $rule_value ) {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
} else {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::alternative_names] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::alternative_names] as $alternative_name ) {
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alternative_name ) {
if ( $node->hasAttribute( $alternative_name ) ) {
$attr_value = strtolower( $node->getAttribute( $alternative_name ) );
if ( $attr_value == $rule_value ) {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
} else {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
}
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a value_regex rule, and
* whether or not the value matches the rule.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_value_regex( $node, $attr_name, $attr_spec_rule ) {
// check 'value_regex' - case sensitive regex match
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_regex] ) && $node->hasAttribute( $attr_name ) ) {
$rule_value = $attr_spec_rule[AMP_Rule_Spec::value_regex];
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX] ) && $node->hasAttribute( $attr_name ) ) {
$rule_value = $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX];
// Note: I added in the '^' and '$' to the regex pattern even though
// they weren't in the AMP spec. But leaving them out would allow
// both '_blank' and 'yyy_blankzzz' to be matched by a regex rule of
// '(_blank|_self|_top)'. The AMP JS validator only accepts '_blank',
// so I'm leaving it this way for now.
if ( preg_match('@^' . $rule_value . '$@u', $node->getAttribute( $attr_name )) ) {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
} else {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a value_regex_casei rule,
* and whether or not the value matches the rule without respect to case.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_value_regex_casei( $node, $attr_name, $attr_spec_rule ) {
// check 'value_regex_casei' - case insensitive regex match
if ( isset( $attr_spec_rule[AMP_Rule_Spec::value_regex_casei] ) && $node->hasAttribute( $attr_name ) ) {
$rule_value = $attr_spec_rule[AMP_Rule_Spec::value_regex_casei];
if ( isset( $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX_CASEI] ) && $node->hasAttribute( $attr_name ) ) {
$rule_value = $attr_spec_rule[AMP_Rule_Spec::VALUE_REGEX_CASEI];
// See note above regarding the '^' and '$' that are added here.
if ( preg_match('/^' . $rule_value . '$/ui', $node->getAttribute( $attr_name ) ) ) {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
} else {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a protocol rule, and
* whether or not the value matches the rule.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::allowed_protocol] ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOWED_PROTOCOL] ) ) {
if ( $node->hasAttribute( $attr_name ) ) {
$attr_value = $node->getAttribute( $attr_name );
$attr_value = preg_replace( '/\s*,\s*/', ',', $attr_value );
@@ -611,14 +611,14 @@ private function check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr
// This seems to be an acceptable check since the AMP validator
// will allow a URL with no protocol to pass validation.
if ( $url_scheme = AMP_WP_Utils::parse_url( $url, PHP_URL_SCHEME ) ) {
if ( ! in_array( strtolower( $url_scheme ), $attr_spec_rule[AMP_Rule_Spec::allowed_protocol] ) ) {
return AMP_Rule_Spec::fail;
if ( ! in_array( strtolower( $url_scheme ), $attr_spec_rule[AMP_Rule_Spec::ALLOWED_PROTOCOL] ) ) {
return AMP_Rule_Spec::FAIL;
}
}
}
return AMP_Rule_Spec::pass;
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::alternative_names] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::alternative_names] as $alternative_name ) {
return AMP_Rule_Spec::PASS;
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alternative_name ) {
if ( $node->hasAttribute( $alternative_name ) ) {
$attr_value = $node->getAttribute( $alternative_name );
$attr_value = preg_replace( '/\s*,\s*/', ',', $attr_value );
@@ -627,32 +627,32 @@ private function check_attr_spec_rule_allowed_protocol( $node, $attr_name, $attr
// This seems to be an acceptable check since the AMP validator
// will allow a URL with no protocol to pass validation.
if ( $url_scheme = AMP_WP_Utils::parse_url( $url, PHP_URL_SCHEME ) ) {
if ( ! in_array( strtolower( $url_scheme ), $attr_spec_rule[AMP_Rule_Spec::allowed_protocol] ) ) {
return AMP_Rule_Spec::fail;
if ( ! in_array( strtolower( $url_scheme ), $attr_spec_rule[AMP_Rule_Spec::ALLOWED_PROTOCOL] ) ) {
return AMP_Rule_Spec::FAIL;
}
}
}
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
}
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a disallowed_relative rule,
* and whether or not the value matches the rule.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_disallowed_relative( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::allow_relative] ) &&
( false == $attr_spec_rule[AMP_Rule_Spec::allow_relative] ) ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOW_RELATIVE] ) &&
( false == $attr_spec_rule[AMP_Rule_Spec::ALLOW_RELATIVE] ) ) {
if ( $node->hasAttribute( $attr_name ) ) {
$attr_value = $node->getAttribute( $attr_name );
$attr_value = preg_replace( '/\s*,\s*/', ',', $attr_value );
@@ -665,115 +665,115 @@ private function check_attr_spec_rule_disallowed_relative( $node, $attr_name, $a
// ie. '//domain.com/path' and '/path' should both be considered
// relative for purposes of AMP validation.
if ( empty( $parsed_url['scheme'] ) ) {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
return AMP_Rule_Spec::pass;
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::alternative_names] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::alternative_names] as $alternative_name ) {
return AMP_Rule_Spec::PASS;
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alternative_name ) {
if ( $node->hasAttribute( $alternative_name ) ) {
$attr_value = $node->getAttribute( $alternative_name );
$attr_value = preg_replace( '/\s*,\s*/', ',', $attr_value );
$urls_to_test = explode( ',', $attr_value );
foreach ( $urls_to_test as $url ) {
$parsed_url = AMP_WP_Utils::parse_url( $url );
if ( empty( $parsed_url['scheme'] ) ) {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
}
}
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a disallowed_relative rule,
* and whether or not the value matches the rule.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_disallowed_empty( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::allow_empty] ) &&
( false == $attr_spec_rule[AMP_Rule_Spec::allow_empty] ) &&
if ( isset( $attr_spec_rule[AMP_Rule_Spec::ALLOW_EMPTY] ) &&
( false == $attr_spec_rule[AMP_Rule_Spec::ALLOW_EMPTY] ) &&
$node->hasAttribute( $attr_name ) ) {
$attr_value = $node->getAttribute( $attr_name );
if ( empty( $attr_value ) ) {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a disallowed_domain rule,
* and whether or not the value matches the rule.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_disallowed_domain( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::disallowed_domain] ) &&
if ( isset( $attr_spec_rule[AMP_Rule_Spec::DISALLOWED_DOMAIN] ) &&
$node->hasAttribute( $attr_name ) ) {
$attr_value = $node->getAttribute( $attr_name );
$url_domain = AMP_WP_Utils::parse_url( $attr_value, PHP_URL_HOST );
if ( ! empty( $url_domain ) ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::disallowed_domain] as $disallowed_domain ) {
foreach ( $attr_spec_rule[AMP_Rule_Spec::DISALLOWED_DOMAIN] as $disallowed_domain ) {
if ( strtolower( $url_domain ) == strtolower( $disallowed_domain ) ) {
// Found a disallowed domain, fail validation.
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
}
}
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
* Checks to see if the given attribute exists, has a blacklisted_value_regex rule,
* and whether or not the value matches the rule.
*
* Returns:
* - AMP_Rule_Spec::pass - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::fail - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::not_applicable - $attr_name does not exist or there
* - AMP_Rule_Spec::PASS - $attr_name has a value that matches the rule.
* - AMP_Rule_Spec::FAIL - $attr_name has a value that does *not* match rule.
* - AMP_Rule_Spec::NOT_APPLICABLE - $attr_name does not exist or there
* is no rule for this attribute.
*/
private function check_attr_spec_rule_blacklisted_value_regex( $node, $attr_name, $attr_spec_rule ) {
if ( isset( $attr_spec_rule[AMP_Rule_Spec::blacklisted_value_regex] ) ) {
$pattern = '/' . $attr_spec_rule[AMP_Rule_Spec::blacklisted_value_regex] . '/u';
if ( isset( $attr_spec_rule[AMP_Rule_Spec::BLACKLISTED_VALUE_REGEX] ) ) {
$pattern = '/' . $attr_spec_rule[AMP_Rule_Spec::BLACKLISTED_VALUE_REGEX] . '/u';
if ( $node->hasAttribute( $attr_name ) ) {
$attr_value = $node->getAttribute( $attr_name );
if ( preg_match( $pattern, $attr_value ) ) {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
} else {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::alternative_names] ) ) {
foreach( $attr_spec_rule[AMP_Rule_Spec::alternative_names] as $alternative_name ) {
} elseif ( isset( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach( $attr_spec_rule[AMP_Rule_Spec::ALTERNATIVE_NAMES] as $alternative_name ) {
if ( $node->hasAttribute( $alternative_name ) ) {
$attr_value = $node->getAttribute( $alternative_name );
if ( preg_match( $pattern, $attr_value ) ) {
return AMP_Rule_Spec::fail;
return AMP_Rule_Spec::FAIL;
} else {
return AMP_Rule_Spec::pass;
return AMP_Rule_Spec::PASS;
}
}
}
}
}
return AMP_Rule_Spec::not_applicable;
return AMP_Rule_Spec::NOT_APPLICABLE;
}

/**
@@ -896,34 +896,31 @@ private function remove_node( $node ) {
abstract class AMP_Rule_Spec {

// AMP rule_spec types
const attr_spec_list = 'attr_spec_list';
const tag_spec = 'tag_spec';
const ATTR_SPEC_LIST = 'attr_spec_list';
const TAG_SPEC = 'tag_spec';

// AMP attr_spec value check results
const pass = 'pass';
const fail = 'fail';
const not_applicable = 'not_applicable';
const PASS = 'pass';
const FAIL = 'fail';
const NOT_APPLICABLE = 'not_applicable';

// tag rule names
const disallowed_ancestor = 'disallowed_ancestor';
const mandatory_ancestor = 'mandatory_ancestor';
const mandatory_parent = 'mandatory_parent';

// attr names
const srcset = 'srcset';
const DISALLOWED_ANCESTOR = 'disallowed_ancestor';
const MANDATORY_ANCESTOR = 'mandatory_ancestor';
const MANDATORY_PARENT = 'mandatory_parent';

// attr rule names
const allow_empty = 'allow_empty';
const allow_relative = 'allow_relative';
const allowed_protocol = 'allowed_protocol';
const alternative_names = 'alternative_names';
const blacklisted_value_regex = 'blacklisted_value_regex';
const disallowed_domain = 'disallowed_domain';
const mandatory = 'mandatory';
const value = 'value';
const value_casei = 'value_casei';
const value_regex = 'value_regex';
const value_regex_casei = 'value_regex_casei';
const ALLOW_EMPTY = 'allow_empty';
const ALLOW_RELATIVE = 'allow_relative';
const ALLOWED_PROTOCOL = 'allowed_protocol';
const ALTERNATIVE_NAMES = 'alternative_names';
const BLACKLISTED_VALUE_REGEX = 'blacklisted_value_regex';
const DISALLOWED_DOMAIN = 'disallowed_domain';
const MANDATORY = 'mandatory';
const VALUE = 'value';
const VALUE_CASEI = 'value_casei';
const VALUE_REGEX = 'value_regex';
const VALUE_REGEX_CASEI = 'value_regex_casei';

// If a node type listed here is invalid, it and it's subtree will be
// removed if it is invalid. This is mainly because any children will be
176 changes: 88 additions & 88 deletions tests/amp-tag-and-attribute-sanitizer-private-methods-tests.php
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_mandatory',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_mandatory_alternate_attr_pass' => array(
array(
@@ -39,7 +39,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_mandatory',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_mandatory_fail' => array(
array(
@@ -51,7 +51,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_mandatory',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_mandatory_na' => array(
array(
@@ -63,7 +63,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_mandatory',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -78,7 +78,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_value_fail' => array(
array(
@@ -90,7 +90,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_value_not_applicable' => array(
array(
@@ -102,7 +102,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_value',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -118,7 +118,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_casei',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_value_casei_upper_pass' => array(
array(
@@ -130,7 +130,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_casei',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_value_casei_fail' => array(
array(
@@ -142,7 +142,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_casei',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_value_casei_na' => array(
array(
@@ -154,7 +154,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_value_casei',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -169,7 +169,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_regex',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_value_regex_fail' => array(
array(
@@ -181,7 +181,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_regex',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_value_regex_na' => array(
array(
@@ -193,7 +193,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_value_regex',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -208,7 +208,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_regex_casei',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_value_regex_casei_upper_pass' => array(
array(
@@ -220,7 +220,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_regex_casei',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_value_regex_casei_fail' => array(
array(
@@ -232,7 +232,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_value_regex_casei',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_value_regex_casei_na' => array(
array(
@@ -244,7 +244,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_value_regex_casei',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -260,7 +260,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_allowed_protocol_fail' => array(
array(
@@ -272,7 +272,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_allowed_protocol_na' => array(
array(
@@ -284,7 +284,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -298,7 +298,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_allowed_protocol_srcset_multiple_pass' => array(
array(
@@ -310,7 +310,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_allowed_protocol_srcset_single_fail' => array(
array(
@@ -322,7 +322,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_allowed_protocol_srcset_multiple_fail' => array(
array(
@@ -334,7 +334,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_allowed_protocol_srcset_multiple_fail_good_first' => array(
array(
@@ -346,7 +346,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_allowed_protocol_srcset_multiple_fail_bad_first' => array(
array(
@@ -358,7 +358,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_allowed_protocol_srcset_na' => array(
array(
@@ -370,7 +370,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_allowed_protocol',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -386,7 +386,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_relative',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_disallowed_relative_fail' => array(
array(
@@ -398,7 +398,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_relative',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_disallowed_relative_na' => array(
array(
@@ -410,7 +410,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_disallowed_relative',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -425,7 +425,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_empty',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_disallowed_empty_fail' => array(
array(
@@ -437,7 +437,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_empty',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_disallowed_empty_na' => array(
array(
@@ -449,7 +449,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_disallowed_empty',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -464,7 +464,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_domain',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_disallowed_domain_fail' => array(
array(
@@ -476,7 +476,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_domain',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_disallowed_domain_fail_2' => array(
array(
@@ -488,7 +488,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_disallowed_domain',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_disallowed_domain_na' => array(
array(
@@ -500,7 +500,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_disallowed_domain',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),


@@ -516,7 +516,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_blacklisted_value_regex',
),
'expected' => AMP_Rule_Spec::pass,
'expected' => AMP_Rule_Spec::PASS,
),
'test_attr_spec_rule_blacklisted_value_regex_fail' => array(
array(
@@ -528,7 +528,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_blacklisted_value_regex',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_blacklisted_value_regex_fail_2' => array(
array(
@@ -540,7 +540,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => true,
'func_name' => 'check_attr_spec_rule_blacklisted_value_regex',
),
'expected' => AMP_Rule_Spec::fail,
'expected' => AMP_Rule_Spec::FAIL,
),
'test_attr_spec_rule_blacklisted_value_regex_na' => array(
array(
@@ -552,7 +552,7 @@ public function get_attr_spec_rule_data() {
'include_attr_value' => false,
'func_name' => 'check_attr_spec_rule_blacklisted_value_regex',
),
'expected' => AMP_Rule_Spec::not_applicable,
'expected' => AMP_Rule_Spec::NOT_APPLICABLE,
),
);
}
@@ -580,8 +580,8 @@ public function test_validate_attr_spec_rules( $data, $expected ) {

$attr_spec_list = $this->allowed_tags[ $data['tag_name'] ][$data['rule_spec_index']]['attr_spec_list'];
foreach( $attr_spec_list as $attr_name => $attr_val ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] as $attr_alt_name ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] as $attr_alt_name ) {
$attr_spec_list[ $attr_alt_name ] = $attr_spec_list[ $attr_name ];
}
}
@@ -740,8 +740,8 @@ public function test_is_allowed_attribute( $data, $expected ) {

$attr_spec_list = $this->allowed_tags[ $data['tag_name'] ][$data['rule_spec_index']]['attr_spec_list'];
foreach( $attr_spec_list as $attr_name => $attr_val ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::alternative_names] as $attr_alt_name ) {
if ( isset( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] ) ) {
foreach( $attr_spec_list[ $attr_name ][AMP_Rule_Spec::ALTERNATIVE_NAMES] as $attr_alt_name ) {
$attr_spec_list[ $attr_alt_name ] = $attr_spec_list[ $attr_name ];
}
}
@@ -1325,7 +1325,7 @@ public function get_check_attr_spec_rule_value_data() {
'attr_name' => 'attribute1',
'attr_spec_rule' => array(),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'value_pass' => array(
array(
@@ -1336,7 +1336,7 @@ public function get_check_attr_spec_rule_value_data() {
'value' => 'value1',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_fail' => array(
array(
@@ -1347,7 +1347,7 @@ public function get_check_attr_spec_rule_value_data() {
'value' => 'valuex',
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'value_no_attr' => array(
array(
@@ -1358,7 +1358,7 @@ public function get_check_attr_spec_rule_value_data() {
'value' => 'value1',
),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'value_empty_pass1' => array(
array(
@@ -1369,7 +1369,7 @@ public function get_check_attr_spec_rule_value_data() {
'value' => '',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_empty_pass2' => array(
array(
@@ -1380,7 +1380,7 @@ public function get_check_attr_spec_rule_value_data() {
'value' => '',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_empty_fail' => array(
array(
@@ -1391,7 +1391,7 @@ public function get_check_attr_spec_rule_value_data() {
'value' => '',
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'value_alternative_attr_name_pass' => array(
array(
@@ -1405,7 +1405,7 @@ public function get_check_attr_spec_rule_value_data() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_alternative_attr_name_fail' => array(
array(
@@ -1419,7 +1419,7 @@ public function get_check_attr_spec_rule_value_data() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
);
}
@@ -1452,7 +1452,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'attr_name' => 'attribute1',
'attr_spec_rule' => array(),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'value_pass' => array(
array(
@@ -1463,7 +1463,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => 'value1',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_upper_pass' => array(
array(
@@ -1474,7 +1474,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => 'value1',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_fail' => array(
array(
@@ -1485,7 +1485,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => 'valuex',
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'value_no_attr' => array(
array(
@@ -1496,7 +1496,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => 'value1',
),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'value_empty_pass1' => array(
array(
@@ -1507,7 +1507,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => '',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_empty_pass2' => array(
array(
@@ -1518,7 +1518,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => '',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_empty_fail' => array(
array(
@@ -1529,7 +1529,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
'value_casei' => '',
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'value_alternative_attr_name_pass' => array(
array(
@@ -1543,7 +1543,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_alternative_attr_name__upper_pass' => array(
array(
@@ -1557,7 +1557,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_alternative_attr_name_fail' => array(
array(
@@ -1571,7 +1571,7 @@ public function get_check_attr_spec_rule_value_casei_data() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
);
}
@@ -1604,7 +1604,7 @@ public function get_check_attr_spec_rule_blacklisted_value_regex() {
'attr_name' => 'attribute1',
'attr_spec_rule' => array(),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'value_pass' => array(
array(
@@ -1615,7 +1615,7 @@ public function get_check_attr_spec_rule_blacklisted_value_regex() {
'blacklisted_value_regex' => '(not_this|or_this)',
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_fail' => array(
array(
@@ -1626,7 +1626,7 @@ public function get_check_attr_spec_rule_blacklisted_value_regex() {
'blacklisted_value_regex' => '(not_this|or_this)',
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'value_no_attr' => array(
array(
@@ -1637,7 +1637,7 @@ public function get_check_attr_spec_rule_blacklisted_value_regex() {
'blacklisted_value_regex' => '(not_this|or_this)',
),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'value_alternative_attr_name_pass' => array(
array(
@@ -1651,7 +1651,7 @@ public function get_check_attr_spec_rule_blacklisted_value_regex() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'value_alternative_attr_name_fail' => array(
array(
@@ -1665,7 +1665,7 @@ public function get_check_attr_spec_rule_blacklisted_value_regex() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
);
}
@@ -1698,7 +1698,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
'attr_name' => 'attribute1',
'attr_spec_rule' => array(),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'protocol_pass' => array(
array(
@@ -1712,7 +1712,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'protocol_multiple_pass' => array(
array(
@@ -1726,7 +1726,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'protocol_fail' => array(
array(
@@ -1740,7 +1740,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'protocol_multiple_fail' => array(
array(
@@ -1754,7 +1754,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'protocol_alternative_pass' => array(
array(
@@ -1771,7 +1771,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'protocol_alternative_fail' => array(
array(
@@ -1788,7 +1788,7 @@ public function get_check_attr_spec_rule_allowed_protocol() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
);
}
@@ -1821,7 +1821,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
'attr_name' => 'attribute1',
'attr_spec_rule' => array(),
),
AMP_Rule_Spec::not_applicable,
AMP_Rule_Spec::NOT_APPLICABLE,
),
'disallowed_relative_pass' => array(
array(
@@ -1832,7 +1832,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
'allow_relative' => false,
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'disallowed_relative_ multiple_pass' => array(
array(
@@ -1843,7 +1843,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
'allow_relative' => false,
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'disallowed_relative_alternative_pass' => array(
array(
@@ -1857,7 +1857,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'disallowed_relative_alternative_multiple_pass' => array(
array(
@@ -1871,7 +1871,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
),
),
),
AMP_Rule_Spec::pass,
AMP_Rule_Spec::PASS,
),
'disallowed_relative_fail' => array(
array(
@@ -1882,7 +1882,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
'allow_relative' => false,
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'disallowed_relative_multiple_fail' => array(
array(
@@ -1893,7 +1893,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
'allow_relative' => false,
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'disallowed_relative_alternative_fail' => array(
array(
@@ -1907,7 +1907,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
'disallowed_relative_alternative_multiple_fail' => array(
array(
@@ -1921,7 +1921,7 @@ public function get_check_attr_spec_rule_disallowed_relative() {
),
),
),
AMP_Rule_Spec::fail,
AMP_Rule_Spec::FAIL,
),
);
}

0 comments on commit 31025ed

Please sign in to comment.