[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

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


Generated : Tue Aug 18 08:20:25 2026 Cross-referenced by PHPXref