[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> token-list.js (source)

   1  /******/ (() => { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/define property getters */
   8  /******/     (() => {
   9  /******/         // define getter functions for harmony exports
  10  /******/         __webpack_require__.d = (exports, definition) => {
  11  /******/             for(var key in definition) {
  12  /******/                 if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13  /******/                     Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14  /******/                 }
  15  /******/             }
  16  /******/         };
  17  /******/     })();
  18  /******/     
  19  /******/     /* webpack/runtime/hasOwnProperty shorthand */
  20  /******/     (() => {
  21  /******/         __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  22  /******/     })();
  23  /******/     
  24  /************************************************************************/
  25  var __webpack_exports__ = {};
  26  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
  27  /* harmony export */   "default": () => (/* binding */ TokenList)
  28  /* harmony export */ });
  29  /**
  30   * A set of tokens.
  31   *
  32   * @see https://dom.spec.whatwg.org/#domtokenlist
  33   */
  34  class TokenList {
  35    /**
  36     * Constructs a new instance of TokenList.
  37     *
  38     * @param {string} initialValue Initial value to assign.
  39     */
  40    constructor(initialValue = '') {
  41      this.value = initialValue;
  42  
  43      // Disable reason: These are type hints on the class.
  44      /* eslint-disable no-unused-expressions */
  45      /** @type {string} */
  46      this._currentValue;
  47  
  48      /** @type {string[]} */
  49      this._valueAsArray;
  50      /* eslint-enable no-unused-expressions */
  51    }
  52  
  53    /**
  54     * @param {Parameters<Array<string>['entries']>} args
  55     */
  56    entries(...args) {
  57      return this._valueAsArray.entries(...args);
  58    }
  59  
  60    /**
  61     * @param {Parameters<Array<string>['forEach']>} args
  62     */
  63    forEach(...args) {
  64      return this._valueAsArray.forEach(...args);
  65    }
  66  
  67    /**
  68     * @param {Parameters<Array<string>['keys']>} args
  69     */
  70    keys(...args) {
  71      return this._valueAsArray.keys(...args);
  72    }
  73  
  74    /**
  75     * @param {Parameters<Array<string>['values']>} args
  76     */
  77    values(...args) {
  78      return this._valueAsArray.values(...args);
  79    }
  80  
  81    /**
  82     * Returns the associated set as string.
  83     *
  84     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-value
  85     *
  86     * @return {string} Token set as string.
  87     */
  88    get value() {
  89      return this._currentValue;
  90    }
  91  
  92    /**
  93     * Replaces the associated set with a new string value.
  94     *
  95     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-value
  96     *
  97     * @param {string} value New token set as string.
  98     */
  99    set value(value) {
 100      value = String(value);
 101      this._valueAsArray = [...new Set(value.split(/\s+/g).filter(Boolean))];
 102      this._currentValue = this._valueAsArray.join(' ');
 103    }
 104  
 105    /**
 106     * Returns the number of tokens.
 107     *
 108     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-length
 109     *
 110     * @return {number} Number of tokens.
 111     */
 112    get length() {
 113      return this._valueAsArray.length;
 114    }
 115  
 116    /**
 117     * Returns the stringified form of the TokenList.
 118     *
 119     * @see https://dom.spec.whatwg.org/#DOMTokenList-stringification-behavior
 120     * @see https://www.ecma-international.org/ecma-262/9.0/index.html#sec-tostring
 121     *
 122     * @return {string} Token set as string.
 123     */
 124    toString() {
 125      return this.value;
 126    }
 127  
 128    /**
 129     * Returns an iterator for the TokenList, iterating items of the set.
 130     *
 131     * @see https://dom.spec.whatwg.org/#domtokenlist
 132     *
 133     * @return {IterableIterator<string>} TokenList iterator.
 134     */
 135    *[Symbol.iterator]() {
 136      return yield* this._valueAsArray;
 137    }
 138  
 139    /**
 140     * Returns the token with index `index`.
 141     *
 142     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-item
 143     *
 144     * @param {number} index Index at which to return token.
 145     *
 146     * @return {string|undefined} Token at index.
 147     */
 148    item(index) {
 149      return this._valueAsArray[index];
 150    }
 151  
 152    /**
 153     * Returns true if `token` is present, and false otherwise.
 154     *
 155     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-contains
 156     *
 157     * @param {string} item Token to test.
 158     *
 159     * @return {boolean} Whether token is present.
 160     */
 161    contains(item) {
 162      return this._valueAsArray.indexOf(item) !== -1;
 163    }
 164  
 165    /**
 166     * Adds all arguments passed, except those already present.
 167     *
 168     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-add
 169     *
 170     * @param {...string} items Items to add.
 171     */
 172    add(...items) {
 173      this.value += ' ' + items.join(' ');
 174    }
 175  
 176    /**
 177     * Removes arguments passed, if they are present.
 178     *
 179     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-remove
 180     *
 181     * @param {...string} items Items to remove.
 182     */
 183    remove(...items) {
 184      this.value = this._valueAsArray.filter(val => !items.includes(val)).join(' ');
 185    }
 186  
 187    /**
 188     * If `force` is not given, "toggles" `token`, removing it if it’s present
 189     * and adding it if it’s not present. If `force` is true, adds token (same
 190     * as add()). If force is false, removes token (same as remove()). Returns
 191     * true if `token` is now present, and false otherwise.
 192     *
 193     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
 194     *
 195     * @param {string}  token   Token to toggle.
 196     * @param {boolean} [force] Presence to force.
 197     *
 198     * @return {boolean} Whether token is present after toggle.
 199     */
 200    toggle(token, force) {
 201      if (undefined === force) {
 202        force = !this.contains(token);
 203      }
 204      if (force) {
 205        this.add(token);
 206      } else {
 207        this.remove(token);
 208      }
 209      return force;
 210    }
 211  
 212    /**
 213     * Replaces `token` with `newToken`. Returns true if `token` was replaced
 214     * with `newToken`, and false otherwise.
 215     *
 216     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-replace
 217     *
 218     * @param {string} token    Token to replace with `newToken`.
 219     * @param {string} newToken Token to use in place of `token`.
 220     *
 221     * @return {boolean} Whether replacement occurred.
 222     */
 223    replace(token, newToken) {
 224      if (!this.contains(token)) {
 225        return false;
 226      }
 227      this.remove(token);
 228      this.add(newToken);
 229      return true;
 230    }
 231  
 232    /**
 233     * Returns true if `token` is in the associated attribute’s supported
 234     * tokens. Returns false otherwise.
 235     *
 236     * Always returns `true` in this implementation.
 237     *
 238     * @see https://dom.spec.whatwg.org/#dom-domtokenlist-supports
 239     *
 240     * @return {boolean} Whether token is supported.
 241     */
 242    supports() {
 243      return true;
 244    }
 245  }
 246  
 247  (window.wp = window.wp || {}).tokenList = __webpack_exports__["default"];
 248  /******/ })()
 249  ;


Generated : Mon Mar 18 08:20:01 2024 Cross-referenced by PHPXref