token-list.js 7.1 KB

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