Skip to content

Commit

Permalink
#2893 Image sanitizer updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MARQAS committed Feb 18, 2019
1 parent ba5c1b1 commit a3565dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
71 changes: 38 additions & 33 deletions includes/vendor/amp/includes/sanitizers/class-amp-img-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,52 +70,57 @@ public function sanitize() {
* @param array $need_dimensions List of Img src url to node mappings corresponding to images that need dimensions.
*/
private function determine_dimensions( $need_dimensions ) {

$dimensions_by_url = AMP_Image_Dimension_Extractor::extract( array_keys( $need_dimensions ) );
$class = "";

foreach ( $dimensions_by_url as $url => $dimensions ) {
foreach ( $need_dimensions[ $url ] as $node ) {
// Provide default dimensions for images whose dimensions we couldn't fetch.
if ( false === $dimensions ) {
$width = isset( $this->args['content_max_width'] ) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH;
$height = self::FALLBACK_HEIGHT;
if ( isset( $dimensions['width'] ) ) {
$width = $dimensions['width'];
}
if ( isset( $dimensions['height'] ) ) {
$height = $dimensions['height'];
}
if ( ! $node instanceof \DOMElement ) {
continue;
}
$class = $node->getAttribute( 'class' );
if ( ! $class ) {
$class = '';
}
if ( ! $dimensions ) {
$class .= ' amp-wp-unknown-size';
}

if ( ! is_numeric( $node->getAttribute( 'width' ) ) ) {
$width = isset( $this->args['content_max_width'] ) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH;
$height = self::FALLBACK_HEIGHT;
if ( isset( $dimensions['width'] ) ) {
$width = $dimensions['width'];
}
if ( isset( $dimensions['height'] ) ) {
$height = $dimensions['height'];
}

// Let width have the right aspect ratio based on the height attribute.
if ( is_numeric( $node->getAttribute( 'height' ) ) && isset( $dimensions['height'] ) && isset( $dimensions['width'] ) ) {
$width = ( floatval( $node->getAttribute( 'height' ) ) * $dimensions['width'] ) / $dimensions['height'];
}
if ( ! is_numeric( $node->getAttribute( 'width' ) ) ) {

$node->setAttribute( 'width', $width );
if ( ! isset( $dimensions['width'] ) ) {
$class .= ' amp-wp-unknown-width';
}
// Let width have the right aspect ratio based on the height attribute.
if ( is_numeric( $node->getAttribute( 'height' ) ) && isset( $dimensions['height'] ) && isset( $dimensions['width'] ) ) {
$width = ( floatval( $node->getAttribute( 'height' ) ) * $dimensions['width'] ) / $dimensions['height'];
}
if ( ! is_numeric( $node->getAttribute( 'height' ) ) ) {

// Let height have the right aspect ratio based on the width attribute.
if ( is_numeric( $node->getAttribute( 'width' ) ) && isset( $dimensions['width'] ) && isset( $dimensions['height'] ) ) {
$height = ( floatval( $node->getAttribute( 'width' ) ) * $dimensions['height'] ) / $dimensions['width'];
}
$node->setAttribute( 'width', $width );
if ( ! isset( $dimensions['width'] ) ) {
$class .= ' amp-wp-unknown-width';
}
}
if ( ! is_numeric( $node->getAttribute( 'height' ) ) ) {

$node->setAttribute( 'height', $height );
if ( ! isset( $dimensions['height'] ) ) {
$class .= ' amp-wp-unknown-height';
}
// Let height have the right aspect ratio based on the width attribute.
if ( is_numeric( $node->getAttribute( 'width' ) ) && isset( $dimensions['width'] ) && isset( $dimensions['height'] ) ) {
$height = ( floatval( $node->getAttribute( 'width' ) ) * $dimensions['height'] ) / $dimensions['width'];
}
$node->setAttribute( 'width', $width );

$node->setAttribute( 'height', $height );
$node->setAttribute( 'class', trim( $class ) );
} else {
$node->setAttribute( 'width', $dimensions['width'] );
$node->setAttribute( 'height', $dimensions['height'] );
if ( ! isset( $dimensions['height'] ) ) {
$class .= ' amp-wp-unknown-height';
}
}
$node->setAttribute( 'class', trim( $class ) );
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion includes/vendor/vendor-changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ Reason: To extend the functionality of sidebars and Pagebuilder
39. Class AMP_Blacklist_Sanitizer updated #2835
40. Class AMP_Blacklist_Sanitizer updated for inernal links 2391
41. class-amp-iframe-sanitizer.php updated #2836
42. Updated check_attr_spec_rule_valid_url function and also added two more functions parse_protocol and extract_attribute_urls #2862
42. Updated check_attr_spec_rule_valid_url function and also added two more functions parse_protocol and extract_attribute_urls #2862
43. Class AMP_Img_Sanitizer Updated #2893

0 comments on commit a3565dc

Please sign in to comment.