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

fix: refactor inline styles in thumbnail plugin for CSP compatibility #1565

Merged
merged 2 commits into from
Feb 10, 2024
Merged
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 src/lg-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ const utils = {

getVideoPosterMarkup(
_poster: string,
dummyImg: string,
dummyImg: string | HTMLElement,
videoContStyle: string,
playVideoString: string,
_isVideo?: VideoInfo,
Expand Down
8 changes: 6 additions & 2 deletions src/lgQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,13 @@ export class lgQuery {
});
return this;
}
prepend(html: string): this {
prepend(html: string | HTMLElement): this {
this._each((el: any) => {
el.insertAdjacentHTML('afterbegin', html);
if (typeof html === 'string') {
el.insertAdjacentHTML('afterbegin', html);
} else if (html instanceof HTMLElement) {
el.insertBefore(html.cloneNode(true), el.firstChild);
}
});
return this;
}
Expand Down
20 changes: 13 additions & 7 deletions src/lightgallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ export class LightGallery {
$currentSlide: lgQuery,
index: number,
alt: string,
): string {
): HTMLImageElement | string {
let $currentItem;
if (!this.settings.dynamic) {
$currentItem = $LG(this.items).eq(index);
Expand All @@ -964,12 +964,16 @@ export class LightGallery {
}
if (!_dummyImgSrc) return '';
const imgStyle = this.getDummyImgStyles(this.currentImageSize);
const dummyImgContent = `<img ${alt} style="${imgStyle}" class="lg-dummy-img" src="${_dummyImgSrc}" />`;
const dummyImgContentImg = document.createElement('img');
dummyImgContentImg.alt = alt || '';
dummyImgContentImg.src = _dummyImgSrc;
dummyImgContentImg.className = `lg-dummy-img`;
dummyImgContentImg.style.cssText = imgStyle;

$currentSlide.addClass('lg-first-slide');
this.outer.addClass('lg-first-slide-loading');

return dummyImgContent;
return dummyImgContentImg;
}
return '';
}
Expand All @@ -980,7 +984,7 @@ export class LightGallery {

// Use the thumbnail as dummy image which will be resized to actual image size and
// displayed on top of actual image
let imgContent = '';
let imgContent: string | HTMLImageElement = '';
const altAttr = alt ? 'alt="' + alt + '"' : '';

if (this.isFirstSlideWithZoomAnimation()) {
Expand All @@ -999,8 +1003,10 @@ export class LightGallery {
sources,
);
}
const imgMarkup = `<picture class="lg-img-wrap"> ${imgContent}</picture>`;
$currentSlide.prepend(imgMarkup);
const picture = document.createElement('picture');
picture.className = 'lg-img-wrap';
picture.append(imgContent);
$currentSlide.prepend(picture);
}

onSlideObjectLoad(
Expand Down Expand Up @@ -1180,7 +1186,7 @@ export class LightGallery {
);
$currentSlide.prepend(markup);
} else if (poster) {
let dummyImg = '';
let dummyImg: string | HTMLImageElement = '';
const hasStartAnimation =
isFirstSlide &&
this.zoomFromOrigin &&
Expand Down
38 changes: 16 additions & 22 deletions src/plugins/thumbnail/lg-thumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export default class Thumbnail {
return thumbDragUtils;
}

getThumbHtml(thumb: string, index: number, alt?: string): string {
getThumbHtml(thumb: string, index: number, alt?: string): HTMLElement {
const slideVideoInfo =
this.core.galleryItems[index].__slideVideoInfo || {};
let thumbImg;
Expand All @@ -426,31 +426,25 @@ export default class Thumbnail {
thumbImg = thumb;
}

const altAttr = alt ? 'alt="' + alt + '"' : '';

return `<div data-lg-item-id="${index}" class="lg-thumb-item ${
index === this.core.index ? ' active' : ''
}"
style="width:${this.settings.thumbWidth}px; height: ${
this.settings.thumbHeight
};
margin-right: ${this.settings.thumbMargin}px;">
<img ${altAttr} data-lg-item-id="${index}" src="${thumbImg}" />
</div>`;
const div = document.createElement('div');
div.setAttribute('data-lg-item-id', index + '');
div.className = `lg-thumb-item ${
index === this.core.index ? 'active' : ''
}`;
div.style.cssText = `width: ${this.settings.thumbWidth}px; height: ${this.settings.thumbHeight}; margin-right: ${this.settings.thumbMargin}px;`;
const img = document.createElement('img');
img.alt = alt || '';
img.setAttribute('data-lg-item-id', index + '');
img.src = thumbImg;
div.appendChild(img);
return div;
}

getThumbItemHtml(items: ThumbnailGalleryItem[]): string {
let thumbList = '';
setThumbItemHtml(items: ThumbnailGalleryItem[]): void {
for (let i = 0; i < items.length; i++) {
thumbList += this.getThumbHtml(items[i].thumb, i, items[i].alt);
const thumb = this.getThumbHtml(items[i].thumb, i, items[i].alt);
this.$lgThumb.append(thumb);
}

return thumbList;
}

setThumbItemHtml(items: ThumbnailGalleryItem[]): void {
const thumbList = this.getThumbItemHtml(items);
this.$lgThumb.html(thumbList);
}

setAnimateThumbStyles(): void {
Expand Down