Skip to content

Commit

Permalink
Copy referrerpolicy attribute from img to amp-pixel
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Mar 9, 2022
1 parent 1976e20 commit e40d370
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions includes/sanitizers/class-amp-img-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,19 @@ public function sanitize() {

// Replace img with amp-pixel when dealing with tracking pixels.
if ( self::is_tracking_pixel_url( $node->getAttribute( Attribute::SRC ) ) ) {
$attributes = [
Attribute::SRC => $node->getAttribute( Attribute::SRC ),
Attribute::LAYOUT => Layout::NODISPLAY,
];
foreach ( [ Attribute::REFERRERPOLICY ] as $allowed_attribute ) {
if ( $node->hasAttribute( $allowed_attribute ) ) {
$attributes[ $allowed_attribute ] = $node->getAttribute( $allowed_attribute );
}
}
$amp_pixel_node = AMP_DOM_Utils::create_node(
$this->dom,
Extension::PIXEL,
[
Attribute::SRC => $node->getAttribute( Attribute::SRC ),
Attribute::LAYOUT => Layout::NODISPLAY,
]
$attributes
);
$node->parentNode->replaceChild( $amp_pixel_node, $node );
continue;
Expand Down
5 changes: 5 additions & 0 deletions tests/php/test-amp-img-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ public function get_data() {
'<img height="1" width="1" style="display:none" alt="fbpx" src="https://www.facebook.com/tr?id=123456789012345&ev=PageView&noscript=1" />',
'<amp-pixel src="https://www.facebook.com/tr?id=123456789012345&amp;ev=PageView&amp;noscript=1" layout="nodisplay"></amp-pixel>',
],

'facebook_pixel_img_to_amp_pixel_with_referrer' => [
'<img height="1" width="1" style="display:none" alt="fbpx" src="https://facebook.com/tr?id=123456789012345&ev=PageView&noscript=1" referrerpolicy="no-referrer">',
'<amp-pixel src="https://facebook.com/tr?id=123456789012345&amp;ev=PageView&amp;noscript=1" layout="nodisplay" referrerpolicy="no-referrer"></amp-pixel>',
],
];
}

Expand Down

0 comments on commit e40d370

Please sign in to comment.