All files / src/transformers index.ts

96.82% Statements 61/63
100% Branches 3/3
93.75% Functions 15/16
96.77% Lines 60/62

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 1356x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x           6x               214x 214x       34x 34x       9x       1948x 1948x                 249x       36x 36x 23x   36x       995x 995x       106x       8019x       465x 465x 465x   465x 465x       420x       420x   420x 1921x     420x       420x   420x 415x 246x   169x     420x         465x     465x 465x 465x 465x 465x 465x 465x 465x 465x 465x 465x 465x       488x 5856x 5856x        
import { BackgroundImageTransformer } from './backgroundImageTransformer';
import { BackgroundTransformer } from './backgroundTransformer';
import { BorderTransformer } from './borderTransformer';
import { ClassMappingTransformer } from './classMappingTransformer';
import { ClassTransformer } from './classTransformer';
import { FragmentTransformer } from './fragmentTransformer';
import { GridTransformer } from './gridTransformer';
import { PaddingTransformer } from './paddingTransformer';
import { RotateTransformer } from './rotateTransformer';
import { StyleMappingTransformer } from './styleMappingTransformer';
import { StyleTransformer } from './styleTransformer';
 
export interface AttributeTransformer {
	transform(element: Properties): void;
}
 
export class Properties {
	private transformer: AttributeTransformers;
 
	private style: Map<string, string>;
	private class: Set<string>;
	private attributes: Map<string, string>;
 
	public addClass(name: string): Properties {
		this.class.add(name);
		return this;
	}
 
	public deleteClass(name: string): Properties {
		this.class.delete(name);
		return this;
	}
 
	public hasClass(name: string): boolean {
		return this.class.has(name);
	}
 
	public addStyle(key: string, value: string): Properties {
		this.style.set(key, value);
		return this;
	}
 
	public deleteStyle(key: string): Properties {
		this.style.delete(key);
		return this;
	}
 
	public hasStyle(name: string): boolean {
		return this.style.has(name);
	}
 
	public addAttribute(key: string, value: string, update = true): Properties {
		this.attributes.set(key, value);
		if (update) {
			this.transformer.transform(this);
		}
		return this;
	}
 
	public deleteAttribute(key: string): Properties {
		this.attributes.delete(key);
		return this;
	}
 
	public hasAttribute(name: string): boolean {
		return this.attributes.has(name);
	}
 
	public getAttribute(name: string): string {
		return this.attributes.get(name);
	}
 
	constructor(attributes: Map<string, string>) {
		this.style = new Map<string, string>();
		this.class = new Set<string>();
		this.attributes = attributes;
 
		this.transformer = new AttributeTransformers();
		this.transformer.transform(this);
	}
 
	public getClasses(): string {
		return Array.from(this.class).join(' ');
	}
 
	public getStyles(): string {
		const result = Array<string>();
 
		for (const [key, value] of this.style) {
			result.push(`${key}: ${value}`);
		}
 
		return result.join('; ');
	}
 
	public getAttributes(): string {
		const result = Array<string>();
 
		for (const [key, value] of this.attributes) {
			if (key == 'onTarget') {
				continue;
			}
			result.push(`${key}="${value}"`);
		}
 
		return result.join(' ');
	}
}
 
class AttributeTransformers {
	private allTransformers: Array<AttributeTransformer> = new Array<AttributeTransformer>();
 
	constructor() {
		this.allTransformers.push(new ClassTransformer());
		this.allTransformers.push(new StyleTransformer());
		this.allTransformers.push(new BackgroundTransformer());
		this.allTransformers.push(new PaddingTransformer());
		this.allTransformers.push(new ClassMappingTransformer('animate'));
		this.allTransformers.push(new FragmentTransformer());
		this.allTransformers.push(new StyleMappingTransformer('opacity', 'opacity'));
		this.allTransformers.push(new BorderTransformer());
		this.allTransformers.push(new StyleMappingTransformer('filter', 'filter'));
		this.allTransformers.push(new RotateTransformer());
		this.allTransformers.push(new GridTransformer());
		this.allTransformers.push(new BackgroundImageTransformer());
	}
 
	transform(element: Properties) {
		for (let x = 0; x < this.allTransformers.length; x++) {
			const transformer = this.allTransformers[x];
			transformer.transform(element);
		}
	}
}