테스트 사이트 - 개발 중인 베타 버전입니다

jQuery.htmlPrefilter( html )

· 7년 전 · 3232 · 1

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개

많이배웁니다.
그누보드에 이런게시판도 있엇네요 자주 올께용

게시글 목록

번호 제목
16019
16015
16014
16013
16011
16010
16009
16008
16004
16003
16002
16001
15993
15992
15991
15988
15987
15986
15985
15981
15980
15979
15978
15977
15976
15975
15974
15973
15971
15970