editorInstance.SetHTML(text.substr(0,charLimit)) broken HTML tags creating problem
Am using FCK Editor & chars length in editor <= 2000 (this is configurable) including formatting (that is HTML tags it generates). If user tries to enter more than 20开发者_如何学C00 characters, I kept an alert that "You can't enter more than 2000 chars". But if user entered 1999 characters and clicked on Formatting toolbar icons it is exceeding 2000 chars. so I handled that and trimming extra chars. In this functionality I am facing the problem. If some formatting is in the end of editor, while triming HTML end tags are truncated by the editorInstance.SetHTML(text.substr(0,charLimit)); functionality. so all the HTML tags are visible in the editor Please help.
function IntervalClass()
{
var intervalID;
this.addSetInteval = function(val)
{
this.intervalID = val;
}
this.clearIntevals = function()
{
clearInterval(this.intervalID);
}
}
function addListners(editorName, charLimit, infoDiv)
{
if(editorName == null)
return;
var objInterval = new IntervalClass();
var editorInstance = FCKeditorAPI.GetInstance(editorName);
var oDOM = editorInstance.EditorDocument;
editorInstance.Events.AttachEvent( 'OnPaste', function(){ return TrimCharacters(editorInstance, charLimit, infoDiv, objInterval); }) ;
if (document.all)
{
oDOM.attachEvent("onkeypress",function(){ return LengthValidation(editorInstance, charLimit, infoDiv); });
oDOM.attachEvent("onkeyup",function(){ return TrimChars(editorName, charLimit, infoDiv, objInterval); });
}
else
{
oDOM.addEventListener("keypress",function(){ LengthValidation(editorInstance, charLimit, infoDiv); });
oDOM.addEventListener("keyup",function(){ TrimChars(editorName, charLimit, infoDiv, objInterval); });
}
objInterval.addSetInteval(setInterval(function(){ TrimChars(editorName, charLimit, infoDiv, objInterval); },1000));
}
function LengthValidation(editorInstance, charLimit, infodiv)
{
var text = editorInstance.GetHTML(true);
var textlength = text.length;
if(textlength > 0)
change();
if(textlength >= charLimit)
{
alert('You cannot enter more than (' + charLimit + ') characters!');
infodiv.innerHTML = 'You cannot enter more than (' + charLimit + ') characters!';
return false;
}
if(infodiv != null)
infodiv.innerHTML = '(' + (charLimit - textlength) + ') Characters Remaining';
return true;
}
function TrimCharacters(editorInstance, charLimit, infodiv, objInterval)
{
var text = editorInstance.GetHTML(true);
var textlength = text.length;
if(textlength > charLimit)
{
objInterval.clearIntevals();
editorInstance.SetHTML(text.substr(0,charLimit));
TrimXtraChars(editorInstance, charLimit, infodiv)
alert('You cannot enter more than (' + charLimit + ') characters!');
addListners(editorInstance.Name, charLimit, infodiv)
}
else
{
var textHtml = editorInstance.GetClipboardHTML();
if(textHtml.length + textlength <= charLimit)
{
editorInstance.InsertHtml( textHtml ); //.toLowerCase()
}
else
{
alert('Your paste action is resulting in more than (' + charLimit + ') characters!');
}
}
text = editorInstance.GetHTML(true);
textlength = text.length;
if(infodiv != null)
infodiv.innerHTML = '(' + (charLimit - textlength) + ') Characters Remaining';
return false;
}
function TrimChars(editorName, charLimit, infodiv, objInterval)
{
var editorInstance = FCKeditorAPI.GetInstance(editorName);
var text = editorInstance.GetHTML(true);
var textlength = text.length;
if(textlength > charLimit)
{
objInterval.clearIntevals();
editorInstance.SetHTML(text.substr(0,charLimit));
TrimXtraChars(editorName, charLimit, infodiv)
alert('You cannot enter more than (' + charLimit + ') characters!');
addListners(editorInstance.Name, charLimit, infodiv)
}
text = editorInstance.GetHTML(true);
textlength = text.length;
if(infodiv != null)
infodiv.innerHTML = '(' + (charLimit - textlength) + ') Characters Remaining';
}
function TrimXtraChars(editorName, charLimit, infodiv)
{
var editorInstance = FCKeditorAPI.GetInstance(editorName);
var text = editorInstance.GetHTML(true);
var txtlen = text.length;
if(txtlen <= charLimit)
return;
if(txtlen > charLimit)
{
var diffLen = (txtlen - charLimit);
var AlterText = text;
if(text.lastIndexOf("<") != -1)
AlterText = text.substr(0,text.lastIndexOf("<"));
if(AlterText.length > charLimit)
{
editorInstance.SetHTML(AlterText.substr(0,charLimit-(diffLen+diffLen)));
TrimChars(editorName, charLimit, infodiv);
}
else
{
editorInstance.SetHTML(AlterText);
}
}
}
精彩评论