dongshi2836 2017-07-06 07:16
浏览 64
已采纳

删除未关闭的html标记 - 将内容粘贴到具有contenteditable的div

I am struggling here to find a solution to using

div using contenteditable

PHP

<div class="panel-body">
 <div contenteditable="true" style="text-align: justify" id="text_data_1">
    <?php echo(html_entity_decode($text_data_from_backend))?>
 </div>                   
</div>

jQuery

 var postFormData =  {
    'agent_id'              : $("#text_data_1").val(),                  
    'actionmode'            : id
    };
 var link = 'myfile.php';
$.fn.getajaxresponse(link,  postFormData, $.fn.this_callback); 

Data is pasted or typed into the div which is contenteditable. The whole html layout fails when end users paste content from websites with tags that are not closed properly.

I am using div contenteditable so that new lines and basic html tags are preserved and can be transacted back and forth to database.

Is there a way to remove html tags that are not closed properly and I believe this is the show stopper in getting this methodology in place. Please note I use jQuery, PHP and MySQL

  • 写回答

1条回答 默认 最新

  • douwen5690 2017-07-10 02:26
    关注

    Thank you for your responses.

    It was a bit tricky one and on searching I found a library where jQuery cleaned up without using regex

    1. I attached an editor called Trumbowyg to the div - http://alex-d.github.io/Trumbowyg/ It was quite easy and cool. This one is lightweight and and requires to add the js file to the required page.
    2. On saving via ajax, called a function where JClean [https://code.google.com/archive/p/jquery-clean/] library is used to clean the unclosed tags. Add the Jclean js file to the required page

    This is how I did it eventually.

    1. Attaching editor

      $.fn.attach_event_div_contenteditable = function(){                  
         var tb_buttons = [                     
                            ['viewHTML'],
                            ['formatting'],
                            'btnGrp-semantic',
                            ['superscript', 'subscript'],
                            ['link'],
                            'btnGrp-justify',
                            'btnGrp-lists',
                            ['horizontalRule'],
                            ['removeformat'],
                            ['fullscreen']
                           ];
         $('#text_data_1').trumbowyg({
             removeformatPasted: true,
             btns: tb_buttons,
             height:"100%",
         });
         $('#text_data_2').trumbowyg({
             removeformatPasted: true,
             btns: tb_buttons,
         });  
         $('#text_data_3').trumbowyg({
             removeformatPasted: true,
             btns: tb_buttons,
         });
         $('#text_data_4').trumbowyg({
             removeformatPasted: true,
             btns: tb_buttons,
         });         
      }
      
    2. Cleaning elements

      $.fn.get_latest_value = function(elem){
       var tagname = elem.prop("tagName");
       var returnvalue = "";
       switch(tagname.toUpperCase()){
          case "TEXTAREA": 
              returnvalue = elem.val();break;
          case "INPUT": 
              returnvalue = elem.val(); break;                
          default:
              returnvalue = elem.html();break;
       }
       returnvalue = returnvalue.replace(/\"/g,'&quot;');
       returnvalue = returnvalue.replace(/\'/g,'&apos;');
       var temp = $("<input value='"+returnvalue+"'/>");
       returnvalue = $.htmlClean(temp.val(), { format: true });
       return returnvalue;
      }
      

    Ajax call

     var postFormData =  {
      'agent_id'              : $.fn.get_latest_value($("#text_data_1")),                  
      'actionmode'            : id
      };
     var link = 'myfile.php';
     $.fn.getajaxresponse(link,  postFormData, $.fn.this_callback);
    

    This is the best I could find as an answer and went ahead. Hope this helps to anyone who wondered how I fixed it.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部