<commit_before>/**
 * Script to add prefixes to standard CSS3 in textareas or style attributes
 * @author <NAME>
 * MIT License
 */

(function() {

var self = window.CSSSnippet = function(element) {
	var me = this,
	    selector = element.getAttribute('data-subject');
	
	// this holds the elements the CSS is gonna be applied to
	this.subjects = selector? toArray(document.querySelectorAll(selector)) || [] : [];

	// Test if its text field first
	if(/^(input|textarea)$/i.test(element.nodeName)) {
		this.textField = element;
		
		// Turn spellchecking off
		this.textField.spellcheck = false;
		
		// If no subject specified, it will be the slide
		if(!this.subjects.length) {
			// Find containing slide
			this.slide = SlideShow.getSlide(this.textField.parentNode);
			
			if(this.slide) {		
				this.subjects = [this.slide];
			}
			else {
				// If no slide present, it will be the text field itself
				this.subjects = [this.textField];
			}
		}
		
		this.textField.addEventListener('keyup', function() {
			me.update();
		}, false);
		
		this.textField.addEventListener('keydown', function() {
			me.updateStyle();
		}, false);
		
		this.update();
	}
	else {
		// Not a text field, add current element as a subject
		this.subjects.unshift(element);
		
		// Keep original style, just in case
		element.setAttribute('data-originalstyle', element.getAttribute('style'));
		
		// Update style, only once
		this.updateStyle();
	}
}

self.prototype = {
	updateStyle: function() {
		var supportedStyle = self.prefixCSS(this.getCSS());
		
		// If it's a text field, test if style is valid first to avoid applying 
		// random stuff in between valid keystrokes
		if(this.textField) {
			if (!self.isCSSValid(supportedStyle)) {
				this.textField.classList && this.textField.classList.add('error');
				return;
			}
			
			this.textField.classList && this.textField.classList.remove('error');
		}
		
		for (var i=0; i<this.subjects.length; i++) {
			this.subjects[i].setAttribute('style', supportedStyle);
		}
	},
	
	getCSS: function() {
		return this.textField ? this.textField.value : this.subjects[0].getAttribute('style');
	},
	
	adjustHeight: function() {
		if ('rows' in this.textField) {
			this.textField.rows = this.textField.value.split(/\r\n?|\n/).length;
			
			this.textField.style.fontSize = Math.min(90, 100 - this.textField.rows * 5) + '%';
		}
	},
	
	update: function() {
		this.updateStyle();
		this.adjustHeight();
	}
};

/*****************************
 * Static properties 
 *****************************/
self.guineaPig = document.createElement('div');
<commit_msg>Changes to CSS Snippets plugin: empty data-subject refers to self, dont-adjust class to text fields turns row adjustment off<commit_after>/**
 * Script to add prefixes to standard CSS3 in textareas or style attributes
 * @author <NAME>
 * MIT License
 */

(function() {

var self = window.CSSSnippet = function(element) {
	var me = this,
	    selector = element.getAttribute('data-subject');
	
	// this holds the elements the CSS is gonna be applied to
	this.subjects = selector? toArray(document.querySelectorAll(selector)) || [] : (element.hasAttribute('data-subject')? [element] : []);

	// Test if its text field first
	if(/^(input|textarea)$/i.test(element.nodeName)) {
		this.textField = element;
		
		// Turn spellchecking off
		this.textField.spellcheck = false;
		
		// If no subject specified, it will be the slide
		if(!this.subjects.length) {
			// Find containing slide
			this.slide = SlideShow.getSlide(this.textField.parentNode);
			
			if(this.slide) {		
				this.subjects = [this.slide];
			}
			else {
				// If no slide present, it will be the text field itself
				this.subjects = [this.textField];
			}
		}
		
		this.textField.addEventListener('keyup', function() {
			me.update();
		}, false);
		
		this.textField.addEventListener('keydown', function() {
			me.updateStyle();
		}, false);
		
		this.update();
	}
	else {
		// Not a text field, add current element as a subject
		this.subjects.unshift(element);
		
		// Keep original style, just in case
		element.setAttribute('data-originalstyle', element.getAttribute('style'));
		
		// Update style, only once
		this.updateStyle();
	}
}

self.prototype = {
	updateStyle: function() {
		var supportedStyle = self.prefixCSS(this.getCSS());
		
		// If it's a text field, test if style is valid first to avoid applying 
		// random stuff in between valid keystrokes
		if(this.textField) {
			if (!self.isCSSValid(supportedStyle)) {
				this.textField.classList && this.textField.classList.add('error');
				return;
			}
			
			this.textField.classList && this.textField.classList.remove('error');
		}
		
		for (var i=0; i<this.subjects.length; i++) {
			this.subjects[i].setAttribute('style', supportedStyle);
		}
	},
	
	getCSS: function() {
		return this.textField ? this.textField.value : this.subjects[0].getAttribute('style');
	},
	
	adjustHeight: function() {
		if ('rows' in this.textField && !this.textField.classList.contains('dont-adjust')) {
			this.textField.rows = this.textField.value.split(/\r\n?|\n/).length;
			
			this.textField.style.fontSize = Math.min(90, 100 - this.textField.rows * 5) + '%';
		}
	},
	
	update: function() {
		this.updateStyle();
		this.adjustHeight();
	}
};

/*****************************
 * Static properties 
 *****************************/
self.guineaPig = document.createElement('div');

{'fraction_non_alphanumeric': 0.16320089119940587, 'fraction_numerical': 0.0033419977720014855, 'mean_word_length': 3.5117252931323284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3359322', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1661, 'n_words': 401}