editorInstance.SetHTML(text.substr(0,charLimit)) 损坏的 HTML 标签创建问题

发布于 2024-08-19 04:36:14 字数 321 浏览 6 评论 0原文

我正在使用 FCK 编辑器和编辑器中的字符长度 <= 2000(这是可配置的),包括格式(即它生成的 HTML 标签)。如果用户尝试输入超过 2000 个字符,我会发出警告“您不能输入超过 2000 个字符”。但如果用户输入 1999 个字符并单击“格式设置”工具栏图标,则会超过 2000 个字符。所以我处理了这个问题并修剪了多余的字符。在此功能中我面临着问题。如果某些格式位于编辑器的末尾,则在修剪 HTML 结束标记时会被 editorInstance.SetHTML(text.substr(0,charLimit)); 功能。所以所有 HTML 标签在编辑器中都可见,请帮忙。

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 2000 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-08-26 04:36:14
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);
        }
    }
}
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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文