1jQuery(function($){ 2 3 var strlen = function( text ) { 4 var count = 0; 5 6 for ( var i = 0; i < text.length; i++ ) { 7 if ( "\n" == text[i] || "\r" == text[i] ) { 8 continue; 9 } 10 11 count++; 12 } 13 14 return count; 15 }; 16 17 var truncate = function ( text, limit ) { 18 var res = ""; 19 var i = 0; 20 var length = 0; 21 22 while ( true ) { 23 if ( length == limit || i == text.length ) { 24 break; 25 } 26 27 res += text[i]; 28 29 if ( "\n" != text[i] && "\r" != text[i] ) { 30 length++; 31 } 32 33 i++; 34 } 35 36 return res; 37 }; 38 39 var html_filter = function ( text, limit ) { 40 var text_arr = text.split(/(<[^>]+>)/i); 41 var textLength = 0; 42 var cut_text = ""; 43 text_arr.forEach( function( part ) { 44 var html_filter = part.replace( /(<([^>]+)>)/ig,"" ); 45 46 if ( textLength + strlen( html_filter ) <= limit ) { 47 textLength += strlen( html_filter ); 48 cut_text += part; 49 } else { 50 cut_text += truncate( part, limit - textLength ); 51 textLength = limit; 52 } 53 } ); 54 55 return [ cut_text, textLength ]; 56 } 57 58 $( '#wpbdp-submit-listing' ).on( 'keyup keydown', '.wpbdp-form-field[data-limit-characters="1"] textarea, .wpbdp-form-field[data-limit-characters="1"] input[type="text"]', function() { 59 var $field = $( this ).parents( '.wpbdp-form-field' ); 60 var limit = parseInt( $field.attr( 'data-characters-limit' ), 10 ); 61 62 if ( limit > 0 ) { 63 var $placeholder = $field.find( '.characters-left-placeholder' ); 64 var text = $(this).val(); 65 66 var textLength = 0; 67 68 var allow_html = parseInt( $field.attr( 'data-allow-html' ), 10 ); 69 70 allow_html = isNaN( allow_html ) ? 0 : allow_html; 71 72 if ( $field.hasClass('wpbdp-form-field-type-textarea') && allow_html ) { 73 [ text, textLength ] = html_filter( text, limit ); 74 } else { 75 textLength = strlen( text ); 76 77 if ( textLength > limit ) { 78 text = truncate( text, limit ); 79 textLength = strlen( text ); 80 } 81 } 82 83 $placeholder.text( limit - textLength ); 84 $(this).val( text ); 85 } 86 87 } ); 88 89 $( document ).on( 'tinymce-editor-setup', function( e, editor ) { 90 editor.on( 'keyup keydown', function( ) { 91 var $field = $( '#wpbdp-submit-listing .wpbdp-form-field[data-limit-characters="1"] textarea#' + editor.id ).parents( '.wpbdp-form-field' ); 92 var limit = parseInt( $field.attr( 'data-characters-limit' ), 10 ); 93 94 if ( limit > 0 ) { 95 var $placeholder = $field.find( '.characters-left-placeholder' ); 96 var bookmark = editor.selection.getBookmark(2, true); 97 var text = editor.getContent(); 98 99 var textLength = 0; 100 var cut_text = ""; 101 102 [ cut_text, textLength ] = html_filter( text, limit ); 103 104 if ( textLength === limit ) { 105 editor.setContent( cut_text ); 106 editor.selection.moveToBookmark(bookmark); 107 } 108 109 $placeholder.text( limit - textLength ); 110 } 111 } ); 112 } ); 113});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.