From b1db76d7c5a3b52c4df65bf1d8246dd449bf16fe Mon Sep 17 00:00:00 2001 From: Jonas Leder Date: Fri, 15 Jul 2022 15:43:58 +0200 Subject: [PATCH] set only atributes existing on the original image --- js/customElements/image.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/js/customElements/image.js b/js/customElements/image.js index e05f79a..c6abf6d 100644 --- a/js/customElements/image.js +++ b/js/customElements/image.js @@ -21,13 +21,13 @@ class CustomImage extends HTMLElement { let image = document.createElement("img"); image.src = imgproxy; - image.setAttribute("alt", this.getAttribute("alt")); - image.setAttribute("title", this.getAttribute("title")); - image.setAttribute("class", this.getAttribute("class")); - image.setAttribute("style", this.getAttribute("style")); - image.setAttribute("width", this.getAttribute("width")); - image.setAttribute("height", this.getAttribute("height")); - image.setAttribute("id", this.getAttribute("id")); + if(this.getAttribute("alt") != null) image.setAttribute("alt", this.getAttribute("alt")); + if(this.getAttribute("title") != null) image.setAttribute("title", this.getAttribute("title")); + if(this.getAttribute("class") != null) image.setAttribute("class", this.getAttribute("class")); + if(this.getAttribute("style") != null) image.setAttribute("style", this.getAttribute("style")); + if(this.getAttribute("width") != null) image.setAttribute("width", this.getAttribute("width")); + if(this.getAttribute("height") != null) image.setAttribute("height", this.getAttribute("height")); + if(this.getAttribute("id") != null) image.setAttribute("id", this.getAttribute("id")); image.setAttribute("loading", "lazy"); image.setAttribute("original-src", originalURL); this.appendChild(image); @@ -45,4 +45,4 @@ class CustomImage extends HTMLElement { } } -customElements.define("jl-img", CustomImage) \ No newline at end of file +customElements.define("jl-img", CustomImage)