[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> wordcount.js (source)

   1  "use strict";
   2  var wp;
   3  (wp ||= {}).wordcount = (() => {
   4    var __defProp = Object.defineProperty;
   5    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   6    var __getOwnPropNames = Object.getOwnPropertyNames;
   7    var __hasOwnProp = Object.prototype.hasOwnProperty;
   8    var __export = (target, all) => {
   9      for (var name in all)
  10        __defProp(target, name, { get: all[name], enumerable: true });
  11    };
  12    var __copyProps = (to, from, except, desc) => {
  13      if (from && typeof from === "object" || typeof from === "function") {
  14        for (let key of __getOwnPropNames(from))
  15          if (!__hasOwnProp.call(to, key) && key !== except)
  16            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17      }
  18      return to;
  19    };
  20    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  21  
  22    // packages/wordcount/build-module/index.js
  23    var index_exports = {};
  24    __export(index_exports, {
  25      count: () => count
  26    });
  27  
  28    // packages/wordcount/build-module/defaultSettings.js
  29    var defaultSettings = {
  30      HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
  31      HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
  32      spaceRegExp: /&nbsp;|&#160;/gi,
  33      HTMLEntityRegExp: /&\S+?;/g,
  34      // \u2014 = em-dash.
  35      connectorRegExp: /--|\u2014/g,
  36      // Characters to be removed from input text.
  37      removeRegExp: new RegExp(
  38        [
  39          "[",
  40          // Basic Latin (extract)
  41          "!-/:-@[-`{-~",
  42          // Latin-1 Supplement (extract)
  43          "\x80-\xBF\xD7\xF7",
  44          /*
  45           * The following range consists of:
  46           * General Punctuation
  47           * Superscripts and Subscripts
  48           * Currency Symbols
  49           * Combining Diacritical Marks for Symbols
  50           * Letterlike Symbols
  51           * Number Forms
  52           * Arrows
  53           * Mathematical Operators
  54           * Miscellaneous Technical
  55           * Control Pictures
  56           * Optical Character Recognition
  57           * Enclosed Alphanumerics
  58           * Box Drawing
  59           * Block Elements
  60           * Geometric Shapes
  61           * Miscellaneous Symbols
  62           * Dingbats
  63           * Miscellaneous Mathematical Symbols-A
  64           * Supplemental Arrows-A
  65           * Braille Patterns
  66           * Supplemental Arrows-B
  67           * Miscellaneous Mathematical Symbols-B
  68           * Supplemental Mathematical Operators
  69           * Miscellaneous Symbols and Arrows
  70           */
  71          "\u2000-\u2BFF",
  72          // Supplemental Punctuation.
  73          "\u2E00-\u2E7F",
  74          "]"
  75        ].join(""),
  76        "g"
  77      ),
  78      // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
  79      astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
  80      wordsRegExp: /\S\s+/g,
  81      characters_excluding_spacesRegExp: /\S/g,
  82      /*
  83       * Match anything that is not a formatting character, excluding:
  84       * \f = form feed
  85       * \n = new line
  86       * \r = carriage return
  87       * \t = tab
  88       * \v = vertical tab
  89       * \u00AD = soft hyphen
  90       * \u2028 = line separator
  91       * \u2029 = paragraph separator
  92       */
  93      characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g,
  94      l10n: {
  95        type: "words"
  96      }
  97    };
  98  
  99    // packages/wordcount/build-module/stripTags.js
 100    function stripTags(settings, text) {
 101      return text.replace(settings.HTMLRegExp, "\n");
 102    }
 103  
 104    // packages/wordcount/build-module/transposeAstralsToCountableChar.js
 105    function transposeAstralsToCountableChar(settings, text) {
 106      return text.replace(settings.astralRegExp, "a");
 107    }
 108  
 109    // packages/wordcount/build-module/stripHTMLEntities.js
 110    function stripHTMLEntities(settings, text) {
 111      return text.replace(settings.HTMLEntityRegExp, "");
 112    }
 113  
 114    // packages/wordcount/build-module/stripConnectors.js
 115    function stripConnectors(settings, text) {
 116      return text.replace(settings.connectorRegExp, " ");
 117    }
 118  
 119    // packages/wordcount/build-module/stripRemovables.js
 120    function stripRemovables(settings, text) {
 121      return text.replace(settings.removeRegExp, "");
 122    }
 123  
 124    // packages/wordcount/build-module/stripHTMLComments.js
 125    function stripHTMLComments(settings, text) {
 126      return text.replace(settings.HTMLcommentRegExp, "");
 127    }
 128  
 129    // packages/wordcount/build-module/stripShortcodes.js
 130    function stripShortcodes(settings, text) {
 131      if (settings.shortcodesRegExp) {
 132        return text.replace(settings.shortcodesRegExp, "\n");
 133      }
 134      return text;
 135    }
 136  
 137    // packages/wordcount/build-module/stripSpaces.js
 138    function stripSpaces(settings, text) {
 139      return text.replace(settings.spaceRegExp, " ");
 140    }
 141  
 142    // packages/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
 143    function transposeHTMLEntitiesToCountableChars(settings, text) {
 144      return text.replace(settings.HTMLEntityRegExp, "a");
 145    }
 146  
 147    // packages/wordcount/build-module/index.js
 148    function loadSettings(type = "words", userSettings = {}) {
 149      const mergedSettings = { ...defaultSettings, ...userSettings };
 150      const settings = {
 151        ...mergedSettings,
 152        type,
 153        shortcodes: []
 154      };
 155      settings.shortcodes = settings.l10n?.shortcodes ?? [];
 156      if (settings.shortcodes && settings.shortcodes.length) {
 157        settings.shortcodesRegExp = new RegExp(
 158          "\\[\\/?(?:" + settings.shortcodes.join("|") + ")[^\\]]*?\\]",
 159          "g"
 160        );
 161      }
 162      if (settings.type !== "characters_excluding_spaces" && settings.type !== "characters_including_spaces") {
 163        settings.type = "words";
 164      }
 165      return settings;
 166    }
 167    function countWords(text, regex, settings) {
 168      text = [
 169        stripTags.bind(null, settings),
 170        stripHTMLComments.bind(null, settings),
 171        stripShortcodes.bind(null, settings),
 172        stripSpaces.bind(null, settings),
 173        stripHTMLEntities.bind(null, settings),
 174        stripConnectors.bind(null, settings),
 175        stripRemovables.bind(null, settings)
 176      ].reduce((result, fn) => fn(result), text);
 177      text = text + "\n";
 178      return text.match(regex)?.length ?? 0;
 179    }
 180    function countCharacters(text, regex, settings) {
 181      text = [
 182        stripTags.bind(null, settings),
 183        stripHTMLComments.bind(null, settings),
 184        stripShortcodes.bind(null, settings),
 185        transposeAstralsToCountableChar.bind(null, settings),
 186        stripSpaces.bind(null, settings),
 187        transposeHTMLEntitiesToCountableChars.bind(null, settings)
 188      ].reduce((result, fn) => fn(result), text);
 189      text = text + "\n";
 190      return text.match(regex)?.length ?? 0;
 191    }
 192    function count(text, type, userSettings) {
 193      const settings = loadSettings(type, userSettings);
 194      let matchRegExp;
 195      switch (settings.type) {
 196        case "words":
 197          matchRegExp = settings.wordsRegExp;
 198          return countWords(text, matchRegExp, settings);
 199        case "characters_including_spaces":
 200          matchRegExp = settings.characters_including_spacesRegExp;
 201          return countCharacters(text, matchRegExp, settings);
 202        case "characters_excluding_spaces":
 203          matchRegExp = settings.characters_excluding_spacesRegExp;
 204          return countCharacters(text, matchRegExp, settings);
 205        default:
 206          return 0;
 207      }
 208    }
 209    return __toCommonJS(index_exports);
 210  })();


Generated : Thu Apr 16 08:20:10 2026 Cross-referenced by PHPXref