Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for source screset #1003

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/front-overlay.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions php/class-delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ public function convert_tags( $content, $context = 'view' ) {
$cached = $has_cache[ $type ];
}

$tags = $this->get_media_tags( $content, 'img|video|article' );
$tags = $this->get_media_tags( $content, 'img|video|article|source' );
$tags = array_map( array( $this, 'parse_element' ), $tags );
$tags = array_filter( $tags );

Expand Down Expand Up @@ -1365,14 +1365,14 @@ public function parse_element( $element ) {

return null;
}
$tag_element['type'] = 'img' === $tag_element['tag'] ? 'image' : $tag_element['tag'];
$third_party_change = Utils::maybe_get_third_party_changes( $attributes );
$tag_element['type'] = in_array( $tag_element['tag'], array( 'img', 'source' ), true ) ? 'image' : $tag_element['tag'];
$third_party_change = Utils::maybe_get_third_party_changes( $attributes, $tag_element['tag'] );
if ( ! empty( $third_party_change ) ) {
Utils::log( $third_party_change, 'third-party-loading' );

return null;
}
$raw_url = $attributes['src'];
$raw_url = 'source' === $tag_element['tag'] && ! empty( $attributes['srcset'] ) ? $attributes['srcset'] : $attributes['src'];
$url = $this->maybe_unsize_url( Utils::clean_url( $this->sanitize_url( $raw_url ) ) );
$tag_element['base_url'] = $url;
// Track back the found URL.
Expand Down Expand Up @@ -1401,7 +1401,7 @@ public function parse_element( $element ) {
$attributes['data-public-id'] = $public_id;
$tag_element['format'] = $item['format'];

if ( 'img' === $tag_element['tag'] ) {
if ( in_array( $tag_element['tag'], array( 'img', 'source' ), true ) ) {
// Check if this is a crop or a scale.
$has_size = $this->media->get_size_from_url( $this->sanitize_url( $raw_url ) );
if ( ! empty( $has_size ) && ! empty( $item['height'] ) ) {
Expand All @@ -1424,7 +1424,7 @@ public function parse_element( $element ) {
}
}
if ( ! empty( $attributes['class'] ) ) {
if ( preg_match( '/wp-post-(\d+)+/', $attributes['class'], $match ) ) {
if ( preg_match( '/wp-post-(\d+)/', $attributes['class'], $match ) ) {
$tag_element['context'] = (int) $match[1];
$post_context = $tag_element['context'];
}
Expand Down
25 changes: 15 additions & 10 deletions php/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,12 @@ public static function get_debug_messages() {
/**
* Check if the tag attributes contain possible third party manipulated data, and return found data.
*
* @param array $attributes The tag attributes.
* @param array $attributes The tag attributes.
* @param string $tag The tag.
*
* @return string|false
*/
public static function maybe_get_third_party_changes( $attributes ) {
public static function maybe_get_third_party_changes( $attributes, $tag ) {
static $filtered_keys, $filtered_classes;
$lazy_keys = array(
'src',
Expand Down Expand Up @@ -833,14 +834,18 @@ public static function maybe_get_third_party_changes( $attributes ) {
$filtered_classes = apply_filters( 'cloudinary_ignored_class_keywords', $lazy_classes );
}
$is = false;
if ( ! isset( $attributes['src'] ) ) {
$is = __( 'Missing SRC attribute.', 'cloudinary' );
} elseif ( false !== strpos( $attributes['src'], 'data:image' ) ) {
$is = $attributes['src'];
} elseif ( isset( $attributes['class'] ) ) {
$classes = explode( '-', str_replace( ' ', '-', $attributes['class'] ) );
if ( ! empty( array_intersect( $filtered_classes, $classes ) ) ) {
$is = $attributes['class'];

// Source tag on Picture tags are not lazy-loaded.
if ( 'source' !== $tag ) {
if ( ! isset( $attributes['src'] ) ) {
$is = __( 'Missing SRC attribute.', 'cloudinary' );
} elseif ( false !== strpos( $attributes['src'], 'data:image' ) ) {
$is = $attributes['src'];
} elseif ( isset( $attributes['class'] ) ) {
$classes = explode( '-', str_replace( ' ', '-', $attributes['class'] ) );
if ( ! empty( array_intersect( $filtered_classes, $classes ) ) ) {
$is = $attributes['class'];
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions php/delivery/class-lazy-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function bypass_lazy_load( $bypass, $tag_element ) {
$bypass = true;
}

// Sources on Picture tag are not lazyloaded.
if ( 'source' === $tag_element['tag'] ) {
$bypass = true;
}

/**
* Filter the classes that bypass lazy loading.
*
Expand Down
77 changes: 62 additions & 15 deletions src/js/front-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,44 @@ const Front_Overlay = {
init() {

[ ...document.images ].forEach( ( image ) => {
this.wrapImage( image );
} );
const parent = image.parentNode;
if ( parent.tagName === 'PICTURE' ) {
this.processPicture( image, parent );
image.addEventListener( 'load', this.load.bind( this, image, parent) );
} else {
this.wrapImage( image );
}
});

const tooltips = document.querySelectorAll( '.cld-tag' );

tippy( tooltips, {
placement: 'bottom-start',
interactive: true,
appendTo: () => document.body,
aria: {
content: 'auto',
expanded: 'auto',
},
content( reference ) {
return reference.template.innerHTML;
},
allowHTML: true,
} );
this.tippy( tooltips );
},
load( image, parent ) {
parent.querySelectorAll( '.overlay-tag' ).forEach( tag => tag.remove() );
this.processPicture( image, parent );
this.tippy( parent.querySelectorAll( '.cld-tag' ) );
},
processPicture( image, parent ) {
const siblings = parent.querySelectorAll( 'source' );
if ( siblings.length > 0 ) {
siblings.forEach(
( sibling ) => {
if ( [ sibling.src, sibling.srcset ].some( src => src.includes( image.currentSrc ) )) {
[ ...image.attributes ].forEach(
attr => {
if ( attr.name.startsWith( 'data-' ) ) {
image.removeAttribute( attr.name );
}
}
);
Object.assign( image.dataset, { ...sibling.dataset } );
}
}
);
}

this.wrapImage( image );
},
wrapImage( image ) {
if ( image.dataset.publicId ) {
Expand Down Expand Up @@ -104,6 +124,33 @@ const Front_Overlay = {
line.appendChild( title );
line.appendChild( detail );
return line;
},
tippy( tooltips ) {
tippy( tooltips, {
placement: 'bottom-start',
interactive: true,
appendTo: () => document.body,
aria: {
content: 'auto',
expanded: 'auto',
},
content( reference ) {
return reference.template.innerHTML;
},
allowHTML: true,
} );
},
debounce( callback, wait ) {
let timeoutId = null;
return (...args) => {
window.clearTimeout( timeoutId );
timeoutId = window.setTimeout(
() => {
callback( ...args );
},
wait
);
};
}
};

Expand Down