/*
*  Markup transpiler utilities.
*/

/*
*  Define a window with JSDOM when used standalone.
*/
var JSDOM = null;
if (typeof window === 'undefined') {
	try {
		JSDOM = require('jsdom').JSDOM;
	} catch (e) {
		throw new Error (
			'JSDOM npm package required when ' +
			'ls-markup is used standalone.'
		);
	}
	global.window = (new JSDOM('')).window;
}

exports.is_css_color = function(color) {
	/*
	*  Return true is 'color' is a valid CSS color
	*  and false otherwise.
	*/
	let opt = new global.window.Option();
	opt.style.color = color;
	return opt.style.color.length !== 0;
}

exports.sanitize = function(str) {
	/*
	*  Sanitize HTML.
	*/
	let tmp = global.window.document.createElement('div');
	tmp.appendChild(global.window.document.createTextNode(str));
	return tmp.innerHTML;
}
