Skip to content

Commit d267863

Browse files
authored
Optimize is_amp_allowed_attribute checking for reference points (#3815)
1 parent 0fa3447 commit d267863

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,18 +1693,16 @@ private function is_amp_allowed_attribute( DOMAttr $attr_node, $attr_spec_list )
16931693
* https://github.com/ampproject/amphtml/blob/1526498116488/extensions/amp-selector/validator-amp-selector.protoascii#L81-L91
16941694
*/
16951695
$descendant_reference_points = [
1696-
'amp-selector' => AMP_Allowed_Tags_Generated::get_reference_point_spec( 'AMP-SELECTOR option' ),
1697-
'amp-story-grid-layer' => AMP_Allowed_Tags_Generated::get_reference_point_spec( 'AMP-STORY-GRID-LAYER default' ), // @todo Consider the more restrictive 'AMP-STORY-GRID-LAYER animate-in'.
1696+
'amp-selector' => 'AMP-SELECTOR option',
1697+
'amp-story-grid-layer' => 'AMP-STORY-GRID-LAYER default', // @todo Consider the more restrictive 'AMP-STORY-GRID-LAYER animate-in'.
16981698
];
1699-
foreach ( $descendant_reference_points as $ancestor_name => $reference_point_spec ) {
1699+
foreach ( $descendant_reference_points as $ancestor_name => $reference_point_spec_name ) {
1700+
if ( empty( $this->open_elements[ $ancestor_name ] ) ) {
1701+
continue;
1702+
}
1703+
$reference_point_spec = AMP_Allowed_Tags_Generated::get_reference_point_spec( $reference_point_spec_name );
17001704
if ( isset( $reference_point_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ][ $attr_name ] ) ) {
1701-
$parent = $attr_node->parentNode;
1702-
while ( $parent ) {
1703-
if ( $ancestor_name === $parent->nodeName ) {
1704-
return true;
1705-
}
1706-
$parent = $parent->parentNode;
1707-
}
1705+
return true;
17081706
}
17091707
}
17101708

tests/php/test-tag-and-attribute-sanitizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,12 @@ static function() {
11241124
[ 'amp-selector', 'amp-form', 'amp-carousel' ],
11251125
],
11261126

1127+
'reference-point-descendant-attr-outside-parent' => [
1128+
'<div option="foo">Foo!</div>',
1129+
'<div>Foo!</div>',
1130+
[],
1131+
],
1132+
11271133
'amp_live_list_sort' => [
11281134
'<amp-live-list sort="ascending" data-poll-interval="15000" data-max-items-per-page="5" id="amp-live-list-insert-blog"><button update on="tap:amp-live-list-insert-blog.update" class="ampstart-btn ml1 caps">You have updates</button><div items><div id="A green landscape with trees." data-sort-time="20180317225019">Hello</div></div></amp-live-list>',
11291135
null, // No change.

0 commit comments

Comments
 (0)