<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38358] trunk/src/wp-admin/js/word-count.js: Editor: improve `word-count.js` documentation.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38358">38358</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38358","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>wonderboymusic</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-08-26 09:24:34 +0000 (Fri, 26 Aug 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: improve `word-count.js` documentation.
Props jipmoors, andizer.
Fixes <a href="https://core.trac.wordpress.org/ticket/37718">#37718</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjswordcountjs">trunk/src/wp-admin/js/word-count.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjswordcountjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/word-count.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/word-count.js 2016-08-26 09:21:33 UTC (rev 38357)
+++ trunk/src/wp-admin/js/word-count.js 2016-08-26 09:24:34 UTC (rev 38358)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,10 +1,57 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Word or character counting functionality. Count words or characters in a provided text string.
+ *
+ * @summary Count words or characters in a text.
+ *
+ * @namespace wp.utils
+ * @since 2.6
+ */
+
</ins><span class="cx" style="display: block; padding: 0 10px"> ( function() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Word counting utility
+ *
+ * @namespace wp.utils.wordcounter
+ * @memberof wp.utils
+ *
+ * @class
+ *
+ * @param {Object} settings Optional. Key-value object containing overrides for
+ * settings.
+ * @param {RegExp} settings.HTMLRegExp Optional. Regular expression to find HTML elements.
+ * @param {RegExp} settings.HTMLcommentRegExp Optional. Regular expression to find HTML comments.
+ * @param {RegExp} settings.spaceRegExp Optional. Regular expression to find irregular space
+ * characters.
+ * @param {RegExp} settings.HTMLEntityRegExp Optional. Regular expression to find HTML entities.
+ * @param {RegExp} settings.connectorRegExp Optional. Regular expression to find connectors that
+ * split words.
+ * @param {RegExp} settings.removeRegExp Optional. Regular expression to find remove unwanted
+ * characters to reduce false-positives.
+ * @param {RegExp} settings.astralRegExp Optional. Regular expression to find unwanted
+ * characters when searching for non-words.
+ * @param {RegExp} settings.wordsRegExp Optional. Regular expression to find words by spaces.
+ * @param {RegExp} settings.characters_excluding_spacesRegExp Optional. Regular expression to find characters which
+ * are non-spaces.
+ * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters
+ * including spaces.
+ * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes.
+ * @param {Object} settings.l10n Optional. Localization object containing specific
+ * configuration for the current localization.
+ * @param {String} settings.l10n.type Optional. Method of finding words to count.
+ * @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed
+ * from the text.
+ *
+ * @return void
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> function WordCounter( settings ) {
</span><span class="cx" style="display: block; padding: 0 10px"> var key,
</span><span class="cx" style="display: block; padding: 0 10px"> shortcodes;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Apply provided settings to object settings.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( settings ) {
</span><span class="cx" style="display: block; padding: 0 10px"> for ( key in settings ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Only apply valid settings.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( settings.hasOwnProperty( key ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> this.settings[ key ] = settings[ key ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -13,91 +60,151 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> shortcodes = this.settings.l10n.shortcodes;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If there are any localization shortcodes, add this as type in the settings.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( shortcodes && shortcodes.length ) {
</span><span class="cx" style="display: block; padding: 0 10px"> this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Default settings.
</ins><span class="cx" style="display: block; padding: 0 10px"> WordCounter.prototype.settings = {
</span><span class="cx" style="display: block; padding: 0 10px"> HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
</span><span class="cx" style="display: block; padding: 0 10px"> HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
</span><span class="cx" style="display: block; padding: 0 10px"> spaceRegExp: / | /gi,
</span><span class="cx" style="display: block; padding: 0 10px"> HTMLEntityRegExp: /&\S+?;/g,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // \u2014 = em-dash
</ins><span class="cx" style="display: block; padding: 0 10px"> connectorRegExp: /--|\u2014/g,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Characters to be removed from input text.
</ins><span class="cx" style="display: block; padding: 0 10px"> removeRegExp: new RegExp( [
</span><span class="cx" style="display: block; padding: 0 10px"> '[',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Basic Latin (extract)
</span><span class="cx" style="display: block; padding: 0 10px"> '\u0021-\u0040\u005B-\u0060\u007B-\u007E',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Latin-1 Supplement (extract)
</span><span class="cx" style="display: block; padding: 0 10px"> '\u0080-\u00BF\u00D7\u00F7',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // General Punctuation
- // Superscripts and Subscripts
- // Currency Symbols
- // Combining Diacritical Marks for Symbols
- // Letterlike Symbols
- // Number Forms
- // Arrows
- // Mathematical Operators
- // Miscellaneous Technical
- // Control Pictures
- // Optical Character Recognition
- // Enclosed Alphanumerics
- // Box Drawing
- // Block Elements
- // Geometric Shapes
- // Miscellaneous Symbols
- // Dingbats
- // Miscellaneous Mathematical Symbols-A
- // Supplemental Arrows-A
- // Braille Patterns
- // Supplemental Arrows-B
- // Miscellaneous Mathematical Symbols-B
- // Supplemental Mathematical Operators
- // Miscellaneous Symbols and Arrows
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /*
+ * The following range consists of:
+ * General Punctuation
+ * Superscripts and Subscripts
+ * Currency Symbols
+ * Combining Diacritical Marks for Symbols
+ * Letterlike Symbols
+ * Number Forms
+ * Arrows
+ * Mathematical Operators
+ * Miscellaneous Technical
+ * Control Pictures
+ * Optical Character Recognition
+ * Enclosed Alphanumerics
+ * Box Drawing
+ * Block Elements
+ * Geometric Shapes
+ * Miscellaneous Symbols
+ * Dingbats
+ * Miscellaneous Mathematical Symbols-A
+ * Supplemental Arrows-A
+ * Braille Patterns
+ * Supplemental Arrows-B
+ * Miscellaneous Mathematical Symbols-B
+ * Supplemental Mathematical Operators
+ * Miscellaneous Symbols and Arrows
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> '\u2000-\u2BFF',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Supplemental Punctuation
</span><span class="cx" style="display: block; padding: 0 10px"> '\u2E00-\u2E7F',
</span><span class="cx" style="display: block; padding: 0 10px"> ']'
</span><span class="cx" style="display: block; padding: 0 10px"> ].join( '' ), 'g' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
</ins><span class="cx" style="display: block; padding: 0 10px"> astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
</span><span class="cx" style="display: block; padding: 0 10px"> wordsRegExp: /\S\s+/g,
</span><span class="cx" style="display: block; padding: 0 10px"> characters_excluding_spacesRegExp: /\S/g,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /*
+ * Match anything that is not a formatting character, excluding:
+ * \f = form feed
+ * \n = new line
+ * \r = carriage return
+ * \t = tab
+ * \v = vertical tab
+ * \u00AD = soft hyphen
+ * \u2028 = line separator
+ * \u2029 = paragraph separator
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
</span><span class="cx" style="display: block; padding: 0 10px"> l10n: window.wordCountL10n || {}
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Counts the number of words (or other specified type) in the specified text.
+ *
+ * @summary Count the number of elements in a text.
+ *
+ * @since 2.6
+ * @memberof wp.utils.wordcounter
+ *
+ * @param {String} text Text to count elements in.
+ * @param {String} type Optional. Specify type to use.
+ *
+ * @return {Number} The number of items counted.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> WordCounter.prototype.count = function( text, type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> var count = 0;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Use default type if none was provided.
</ins><span class="cx" style="display: block; padding: 0 10px"> type = type || this.settings.l10n.type;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) {
</span><span class="cx" style="display: block; padding: 0 10px"> type = 'words';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If we have any text at all.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( text ) {
</span><span class="cx" style="display: block; padding: 0 10px"> text = text + '\n';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Replace all HTML with a new-line.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.HTMLRegExp, '\n' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Remove all HTML comments.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.HTMLcommentRegExp, '' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If a shortcode regular expression has been provided use it to remove shortcodes.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( this.settings.shortcodesRegExp ) {
</span><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.shortcodesRegExp, '\n' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Normalize non-breaking space to a normal space.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.spaceRegExp, ' ' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( type === 'words' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Remove HTML Entities.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.HTMLEntityRegExp, '' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Convert connectors to spaces to count attached text as words.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.connectorRegExp, ' ' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Remove unwanted characters.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.removeRegExp, '' );
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Convert HTML Entities to "a".
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.HTMLEntityRegExp, 'a' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Remove surrogate points.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.replace( this.settings.astralRegExp, 'a' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Match with the selected type regular expression to count the items.
</ins><span class="cx" style="display: block; padding: 0 10px"> text = text.match( this.settings[ type + 'RegExp' ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If we have any matches, set the count to the number of items found.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( text ) {
</span><span class="cx" style="display: block; padding: 0 10px"> count = text.length;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -106,6 +213,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return count;
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Add the WordCounter to the WP Utils.
</ins><span class="cx" style="display: block; padding: 0 10px"> window.wp = window.wp || {};
</span><span class="cx" style="display: block; padding: 0 10px"> window.wp.utils = window.wp.utils || {};
</span><span class="cx" style="display: block; padding: 0 10px"> window.wp.utils.WordCounter = WordCounter;
</span></span></pre>
</div>
</div>
</body>
</html>