]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/word-count.js
WordPress 4.4
[autoinstalls/wordpress.git] / wp-admin / js / word-count.js
1 ( function() {
2         function WordCounter( settings ) {
3                 var key,
4                         shortcodes;
5
6                 if ( settings ) {
7                         for ( key in settings ) {
8                                 if ( settings.hasOwnProperty( key ) ) {
9                                         this.settings[ key ] = settings[ key ];
10                                 }
11                         }
12                 }
13
14                 shortcodes = this.settings.l10n.shortcodes;
15
16                 if ( shortcodes && shortcodes.length ) {
17                         this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
18                 }
19         }
20
21         WordCounter.prototype.settings = {
22                 HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
23                 HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
24                 spaceRegExp: /&nbsp;|&#160;/gi,
25                 HTMLEntityRegExp: /&\S+?;/g,
26                 connectorRegExp: /--|\u2014/g,
27                 removeRegExp: new RegExp( [
28                         '[',
29                                 // Basic Latin (extract)
30                                 '\u0021-\u0040\u005B-\u0060\u007B-\u007E',
31                                 // Latin-1 Supplement (extract)
32                                 '\u0080-\u00BF\u00D7\u00F7',
33                                 // General Punctuation
34                                 // Superscripts and Subscripts
35                                 // Currency Symbols
36                                 // Combining Diacritical Marks for Symbols
37                                 // Letterlike Symbols
38                                 // Number Forms
39                                 // Arrows
40                                 // Mathematical Operators
41                                 // Miscellaneous Technical
42                                 // Control Pictures
43                                 // Optical Character Recognition
44                                 // Enclosed Alphanumerics
45                                 // Box Drawing
46                                 // Block Elements
47                                 // Geometric Shapes
48                                 // Miscellaneous Symbols
49                                 // Dingbats
50                                 // Miscellaneous Mathematical Symbols-A
51                                 // Supplemental Arrows-A
52                                 // Braille Patterns
53                                 // Supplemental Arrows-B
54                                 // Miscellaneous Mathematical Symbols-B
55                                 // Supplemental Mathematical Operators
56                                 // Miscellaneous Symbols and Arrows
57                                 '\u2000-\u2BFF',
58                                 // Supplemental Punctuation
59                                 '\u2E00-\u2E7F',
60                         ']'
61                 ].join( '' ), 'g' ),
62                 astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
63                 wordsRegExp: /\S\s+/g,
64                 characters_excluding_spacesRegExp: /\S/g,
65                 characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
66                 l10n: window.wordCountL10n || {}
67         };
68
69         WordCounter.prototype.count = function( text, type ) {
70                 var count = 0;
71
72                 type = type || this.settings.l10n.type;
73
74                 if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) {
75                         type = 'words';
76                 }
77
78                 if ( text ) {
79                         text = text + '\n';
80
81                         text = text.replace( this.settings.HTMLRegExp, '\n' );
82                         text = text.replace( this.settings.HTMLcommentRegExp, '' );
83
84                         if ( this.settings.shortcodesRegExp ) {
85                                 text = text.replace( this.settings.shortcodesRegExp, '\n' );
86                         }
87
88                         text = text.replace( this.settings.spaceRegExp, ' ' );
89
90                         if ( type === 'words' ) {
91                                 text = text.replace( this.settings.HTMLEntityRegExp, '' );
92                                 text = text.replace( this.settings.connectorRegExp, ' ' );
93                                 text = text.replace( this.settings.removeRegExp, '' );
94                         } else {
95                                 text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
96                                 text = text.replace( this.settings.astralRegExp, 'a' );
97                         }
98
99                         text = text.match( this.settings[ type + 'RegExp' ] );
100
101                         if ( text ) {
102                                 count = text.length;
103                         }
104                 }
105
106                 return count;
107         };
108
109         window.wp = window.wp || {};
110         window.wp.utils = window.wp.utils || {};
111         window.wp.utils.WordCounter = WordCounter;
112 } )();