﻿
function EnableCustomFilters(editor, args)
{
	if(editor != null)
	{
		editor.get_filtersManager().add(new CMSRadEditorFilter());
	}
}

CMSRadEditorFilter = function()
{
	CMSRadEditorFilter.initializeBase(this);
	this.set_isDom(false);
	this.set_enabled(true);
	this.set_name("UE Standard RadEditor filter");
	this.set_description("Custom RadEditor Filter to help remove non-standard markup");
}

CMSRadEditorFilter.prototype =
{
	removeTag : function(content, tag)
	{
		var tagRE = new RegExp("<\\/?" + tag + "( [^>]*)?>", "gi");
		return content.replace(tagRE, "");
	},

	// replace a tag with a new tag and optional css class
	replaceTag : function(content, tag, newTag, cssClass)
	{
		var openTagRE = new RegExp("<" + tag + "( [^>]*)?>", "gi");
		var closeTagRE = new RegExp("<\\/" + tag + ">", "gi");
		var classAttribute = (cssClass != null) ? " class=\"" + cssClass + "\"" : "";
		content = content.replace(openTagRE, "<" + newTag + classAttribute + ">");
		content = content.replace(closeTagRE, "</" + newTag + ">");
		return content
	},
	
	// if content is single line, wrap in a P tag.
	ensurePTag : function(content)
	{
		content = content.trim();
		if((content.length > 0) && (!content.startsWith("<")))
		{
			content = "<p>" + content + "</p>";
		}
		return content;
	},

	parseContent : function(content)
	{
		content = this.replaceTag(content, "u", "span", "Underline");
		content = this.replaceTag(content, "s", "span", "StrikeThrough");
		content = this.replaceTag(content, "strike", "span", "StrikeThrough");
		content = this.replaceTag(content, "b", "strong");
		content = this.replaceTag(content, "i", "em");
		//content = this.ensurePTag(content);
		return content;
	},
	
	getHtmlContent : function(content)
	{
		return this.parseContent(content);
	},

	getDesignContent : function(content)
	{
		return this.parseContent(content);
	}
}

if(Telerik.Web.UI.Editor != null)
{
	CMSRadEditorFilter.registerClass('CMSRadEditorFilter', Telerik.Web.UI.Editor.Filter);
}
