jQuery.htmlPrefilter( html ) > 개발자팁

개발자팁

개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.

jQuery.htmlPrefilter( html ) 정보

jQuery jQuery.htmlPrefilter( html )

본문

jQuery.htmlPrefilter( html )

 

설명 : jQuery 조작 메소드를 통해 전달 된 HTML 문자열을 수정하고 필터링 합니다 .

이 방법은 거의 직접 호출 할 필요가 없습니다. 대신 엔트리 포인트로 사용하여 기존 jQuery 조작 메소드 를 수정하십시오 . 예를 들어 <del>들어오는 HTML 문자열에서 모든 태그 를 제거하려면 다음을 수행하십시오.

var htmlPrefilter = $.htmlPrefilter,
  rdel = /<(del)(?=[\s>])[\w\W]*?<\/\1\s*>/gi;
 
$.htmlPrefilter = function( html ) {
  return htmlPrefilter.call( this, html ).replace( rdel, "" );
};

이 기능은 특정 엣지 케이스 문제를 우회하기 위해 덮어 쓸 수도 있습니다. htmlPrefilterjQuery 의 기본 기능은 모든 태그가 XHTML과 호환되는지 확인합니다. 여기에는 HTML 태그처럼 보이지만 실제로는 문자열 내에있는 모든 것이 포함됩니다 (예 :

<a title="<div /> "> <>
). jQuery.htmlPrefilter()기능은 이것을 무시하기 위해 사용될 수있다 :

$.htmlPrefilter = function( html ) {
  // Return HTML strings unchanged
  return html;
};

그러나 위의 해결 방법은 짧고 간단하지만 모든 HTML 문자열에서 XHTML 호환 태그를 보장해야하는 부담이 있습니다. 이 문제에 대한보다 철저한 수정은 다음과 같습니다.

var panything = "[\\w\\W]*?",
 
  // Whitespace
  // https://html.spec.whatwg.org/multipage/infrastructure.html#space-character
  pspace = "[\\x20\\t\\r\\n\\f]",
 
  // End of tag name (whitespace or greater-than)
  pnameEnd = pspace.replace( "]", ">]" ),
 
  // Tag name (a leading letter, then almost anything)
  // https://html.spec.whatwg.org/multipage/syntax.html#tag-open-state
  // https://html.spec.whatwg.org/multipage/syntax.html#tag-name-state
  pname = "[a-z]" + pnameEnd.replace( "[", "[^/\\0" ) + "*",
 
  // Void element (end tag prohibited)
  // https://html.spec.whatwg.org/multipage/syntax.html#void-elements
  pvoidName = "(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|" +
    "source|track|wbr)(?=" + pnameEnd + ")",
 
  // Attributes (double-quoted value, single-quoted value, unquoted value, or no value)
  // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
  pattrs = "(?:" + pspace + "+[^\\0-\\x20\\x7f-\\x9f=\"'/>]+(?:" + pspace + "*=" + pspace +
    "*(?:\"" + panything + "\"|'" + panything + "'|" +
    pnameEnd.replace( "[", "[^" ) + "*(?!/)" +
    ")|))*" + pspace + "*",
 
  // Trailing content of a close tag
  pcloseTail = "(?:" + pspace + panything + "|)",
 
  rspecialHtml = new RegExp(
    // Non-void element that self-closes: $1–$5
    "(<)(?!" + pvoidName + ")(" + pname + ")(" + pattrs + ")(\\/)(>)|" +
      // No-innerHTML container (element, comment, or CDATA): $6
      "(<(script|style|textarea)" + pattrs + ">" + panything + "<\\/\\7" + pcloseTail + ">|" +
      "<!--" + panything + "--)",
    "gi"
  ),
 
  // "<"; element name; attributes; ">"; "<"; "/"; element name; ">"; no-innerHTML container
  pspecialReplacement = "$1$2$3$5$1$4$2$5$6";
 
$.htmlPrefilter = function( html ) {
  return ( html + "" ).replace( rspecialHtml, pspecialReplacement );
};

추천
1

댓글 1개

전체 5,352
개발자팁 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT