All files / src/transformers backgroundTransformer.ts

100% Statements 26/26
100% Branches 12/12
100% Functions 2/2
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 486x     6x   488x 488x   488x 41x   41x 34x 26x 26x   8x 8x     34x 34x 3x 3x 3x   31x     7x 7x 7x 7x 7x             41x 41x   7x        
import Color from 'color';
import { AttributeTransformer, Properties } from '.';
 
export class BackgroundTransformer implements AttributeTransformer {
	transform(element: Properties) {
		const bg = element.getAttribute('bg');
		const target = element.getAttribute('onTarget');
 
		if (bg != undefined) {
			const color = this.readColor(bg);
 
			if (color) {
				if (color.isLight()) {
					element.addClass('has-light-background');
					element.deleteClass('has-dark-background');
				} else {
					element.addClass('has-dark-background');
					element.deleteClass('has-light-background');
				}
 
				element.deleteAttribute('bg');
				if (target && target == 'slide') {
					element.deleteAttribute('data-background-image');
					element.deleteAttribute('data-background-color');
					element.addAttribute('data-background-color', bg);
				} else {
					element.addStyle('background-color', bg);
				}
			} else {
				element.deleteAttribute('bg');
				if (target && target == 'slide') {
					element.deleteAttribute('data-background-color');
					element.deleteAttribute('data-background-image');
					element.addAttribute('data-background-image', bg);
				}
			}
		}
	}
 
	readColor(bg: string) {
		try {
			return Color(bg);
		} catch (_) {
			return null;
		}
	}
}