2011-10-14 6 views
1

나는 간단한 기사를 쓰는 사람들이 사용하는 작은 사이트에 tinyMCE를 사용하고 있습니다. 그들은 보통 MS 단어로 쓰고 텍스트를 tinyMCE에 복사하고 제출합니다. 내가에만 허용 왜유효한 태그를 제거하는 TinyMCE

몇 가지 태그 :

<img alt=""/> 

코드에 나타납니다
valid_elements: "a[href|target],strong/b,em/i,div[align],br,p[style|align],ul,li,ol,table,tr,td,iframe[*],img[*]", 

그러나 IMG가 [*] 전용 '삽입/편집 이미지'로 이미지를 삽입 한 후 허용에도 불구하고

. iframe에 동일하게 적용됨 (complitly removed) 나는 img 및 iframe 속성의 전체 목록과 extended_valid_elements를 가진 valid_elements의 모든 조합을 이미 시도해 보았습니다.

valid_elements 절을 제거하면 모든 항목이 올바르게 작동하지만 h1, h2 등의 허용되지 않는 단어 형식은 스타일을 엉망으로 만듭니다.

TinyMCE 버전은 3.4.2입니다.

답변

1

나는 tinymce 붙여 넣기 플러그인과 함께 paste_preprocess 설정을 사용하고 있으므로 필자는 원하지 않는 태그를 필터링하여 제거합니다.

strip_tags = function (str, allowed_tags) { 
    var key = '', allowed = false; 
    var matches = []; var allowed_array = []; 
    var allowed_tag = ''; 
    var i = 0; 
    var k = ''; 
    var html = ''; 
    var replacer = function (search, replace, str) { 
     return str.split(search).join(replace); 
    }; 
    // Build allowes tags associative array 
    if (allowed_tags) { 
     allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi); 
    } 
    str += ''; 

    // Match tags 
    matches = str.match(/(<\/?[\S][^>]*>)/gi); 
    // Go through all HTML tags 
    for (key in matches) { 
     if (isNaN(key)) { 
      // IE7 Hack 
      continue;  } 

     // Save HTML tag 
     html = matches[key].toString(); 
     // Is tag not in allowed list? Remove from str! 
     allowed = false; 

     // Go through all allowed tags 
     for (k in allowed_array) {   // Init 
      allowed_tag = allowed_array[k]; 
      i = -1; 

      if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}   
      if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');} 
      if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag) ;} 

      // Determine 
      if (i == 0) {    allowed = true; 
       break; 
      } 
     } 
     if (!allowed) { 
      str = replacer(html, "", str); // Custom replace. No regexing 
     } 
    } 

    return str; 
}; 
: 태그를 제거하는

paste_preprocess : function(pl, o) { 
    //if(console) console.log('Object', o); 
    //if(console) console.log('Content:', o.content); 
     // usage param1 = the string to strip out tags from, param2 = tags to keep in the string 
    o.content = ir.im.strip_tags(o.content,'<p><div><br><br/>'); 
}, 

도움말 기능 : 당신의 TinyMCE에 초기화에

예를 들면 다음과 같습니다

관련 문제