首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTMLEditorExtender删除"class","id“属性

HTMLEditorExtender删除"class","id“属性
EN

Stack Overflow用户
提问于 2013-04-08 22:20:44
回答 1查看 2K关注 0票数 2

这个类似乎剥离了我的HTML元素的“HTMLEditorExtender”和"id“属性,即使EnableSanitization=为”false“。

这是默认行为吗?有什么变通办法吗?

我使用的是最新版本的AjaxControlToolkit。

代码语言:javascript
复制
<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" />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-11 04:47:21

您需要下载AjaxControlToolkit库的源代码,并根据需要对其进行微调。有两个文件必须修改:

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

代码语言:javascript
复制
_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方法:

代码语言:javascript
复制
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\\:\\;\\?\\&\\.\\-\\=";

id属性添加到attributes变量:

代码语言:javascript
复制
string attributes = "style|size|color|face|align|dir|src|class|id";

这是所有构建项目,并在项目中使用ACT库的自定义dll。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15881594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档