asp.net - HTMLEditorExtender 删除 "class"、 "id"属性

标签 asp.net ajax ajaxcontroltoolkit sanitization htmleditorextender

HTMLEditorExtender 似乎正在剥离我的 HTML 元素的“class”和“id”属性,即使使用 EnableSanitization="false"也是如此。

这是默认行为吗?有解决办法吗?

我正在使用最新版本的 AjaxControlToolkit。

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" Width="100%"></asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="TextBox1_HtmlEditorExtender" runat="server" EnableSanitization="false"
    DisplaySourceTab="true" TargetControlID="TextBox1">
</ajaxToolkit:HtmlEditorExtender>
<asp:Button ID="Button1" runat="server" Text="Button" />

最佳答案

您需要下载 AjaxControlToolkit 库的源代码并根据您的需要稍微调整它们。有两个文件必须修改:

HtmlEditorExtenderBehavior.Pre.js 让我们调整此文件中的 _encodeHtml 函数:

_encodeHtml = function () {
    //Encode html tags
    var isIE = Sys.Browser.agent == Sys.Browser.InternetExplorer;

    // code below to the next comment below can be removed completely if you 
    // want to preserve 'width' attribute as well
    var elements = this._editableDiv.getElementsByTagName('*');
    var element;
    for (var i = 0; element = elements[i]; i++) {
        /*
        try {
        element.className = '';
        element.removeAttribute('class');
        } catch (ex) { }
        try {
        element.id = '';
        element.removeAttribute('id');
        } catch (ex) { } */
        try {
            element.removeAttribute('width');
        } catch (ex) { }
        if (isIE) {
        }
    }
    // end of part of code that may be removed

    var html = this._editableDiv.innerHTML;
    if (isIE) {
        //force attributes to be double quoted
        var allTags = /\<[^\>]+\>/g;
        html = html.replace(allTags, function (tag) {
            var sQA = '';
            var nQA = '';
            if (tag.toLowerCase().substring(0, 2) != '<a') {
                sQA = /\=\'([^\'])*\'/g; //single quoted attributes
                nQA = /\=([^\"][^\s\/\>]*)/g; //non double quoted attributes
                return tag.replace(sQA, '="$1"').replace(nQA, '="$1"');
            }
            else {
                return tag;
            }
        });
    }
    //convert rgb colors to hex
    var fixRGB = this._rgbToHex;
    var replaceRGB = function () {
        html = html.replace(/(\<[^\>]+)(rgb\s?\(\d{1,3}\s?\,\s?\d{1,3}\s?\,\s?\d{1,3}\s?\))([^\>]*\>)/gi, function (text, p1, p2, p3) {
            return (p1 || '') + ((p2 && fixRGB(p2)) || '') + (p3 || '');
        });
    };
    //twice in case a tag has more than one rgb color in it;
    replaceRGB();
    replaceRGB();
    // remove empty class and id attributes
    html = html.replace(/\sclass\=\"\"/gi, '').replace(/\sid\=\"\"/gi, '');
    //converter to convert different tags into Html5 standard tags
    html = html.replace(/\<(\/?)strong\>/gi, '<$1b>').replace(/\<(\/?)em\>/gi, '<$1i>');
    //encode for safe transport
    html = html.replace(/&/ig, '&amp;').replace(/\xA0/ig, '&nbsp;');
    html = html.replace(/</ig, '&lt;').replace(/>/ig, '&gt;').replace(/\'/ig, '&apos;').replace(/\"/ig, '&quot;');
    return html;
}

请注意上面注释的代码块。

HtmlEditorExtender.cs 这是一个服务器控制代码文件,您需要稍微更改一下Decode方法:

public string Decode(string value)
{
    EnsureButtons();

    string tags = "font|div|span|br|strong|em|strike|sub|sup|center|blockquote|hr|ol|ul|li|br|s|p|b|i|u|img";
    string attributes = "style|size|color|face|align|dir|src";
    string attributeCharacters = "\\'\\,\\w\\-#\\s\\:\\;\\?\\&\\.\\-\\=";

idclass 属性添加到 attributes 变量:

string attributes = "style|size|color|face|align|dir|src|class|id";

这就是全部 - 构建项目并在项目中使用 ACT 库的自定义 dll。

关于asp.net - HTMLEditorExtender 删除 "class"、 "id"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15881594/

相关文章:

asp.net - 如何在asp.net中显示页面生成时间?

javascript - 无法通过 ajax 调用加载 php 页面

更新面板中的 Javascript 在部分回发后不起作用

javascript - jquery 与 ajax 控制工具包 2013 年 9 月/10 月版本冲突

c# - Ajax 重新排序列表不可拖动

asp.net - 标签中的换行符

asp.net - OutputCache 在持续时间之前过期

c# - GetUserDetails错误错误27名称 'IMGUserLabel'在当前上下文中不存在

javascript - Alert() 的警报超出预期

javascript - AJAX 请求失败