src.app.shared.pipes.thumbnail.pipe.ts Maven / Gradle / Ivy
The newest version!
/*
* SPDX-FileCopyrightText: 2017-2024 Enedis
*
* SPDX-License-Identifier: Apache-2.0
*
*/
import { Pipe, PipeTransform } from '@angular/core';
import { SafeHtml, SafeValue } from '@angular/platform-browser';
@Pipe({
name: 'thumbnail'
})
export class ThumbnailPipe implements PipeTransform {
constructor() {}
public transform(value: SafeValue): SafeHtml {
const doc = new DOMParser().parseFromString(value.toString(), 'text/html');
const imgElements = doc.getElementsByTagName('img');
for (let i = 0; i < imgElements.length; i++) {
const imgElement = imgElements.item(i);
imgElement.classList.add('img-thumbnail');
imgElement.insertAdjacentHTML('beforebegin', '' + imgElement.outerHTML + '');
imgElement.remove();
}
return doc.documentElement.innerHTML;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy