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