You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.3 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var domToArray = _interopDefault(require('bianco.dom-to-array'));
  5. /**
  6. * Normalize the return values, in case of a single value we avoid to return an array
  7. * @param { Array } values - list of values we want to return
  8. * @returns { Array|string|boolean } either the whole list of values or the single one found
  9. * @private
  10. */
  11. const normalize = values => values.length === 1 ? values[0] : values;
  12. /**
  13. * Parse all the nodes received to get/remove/check their attributes
  14. * @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
  15. * @param { string|Array } name - name or list of attributes
  16. * @param { string } method - method that will be used to parse the attributes
  17. * @returns { Array|string } result of the parsing in a list or a single value
  18. * @private
  19. */
  20. function parseNodes(els, name, method) {
  21. const names = typeof name === 'string' ? [name] : name;
  22. return normalize(domToArray(els).map(el => {
  23. return normalize(names.map(n => el[method](n)))
  24. }))
  25. }
  26. /**
  27. * Set any attribute on a single or a list of DOM nodes
  28. * @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
  29. * @param { string|Object } name - either the name of the attribute to set
  30. * or a list of properties as object key - value
  31. * @param { string } value - the new value of the attribute (optional)
  32. * @returns { HTMLElement|NodeList|Array } the original array of elements passed to this function
  33. *
  34. * @example
  35. *
  36. * import { set } from 'bianco.attr'
  37. *
  38. * const img = document.createElement('img')
  39. *
  40. * set(img, 'width', 100)
  41. *
  42. * // or also
  43. * set(img, {
  44. * width: 300,
  45. * height: 300
  46. * })
  47. *
  48. */
  49. function set(els, name, value) {
  50. const attrs = typeof name === 'object' ? name : { [name]: value };
  51. const props = Object.keys(attrs);
  52. domToArray(els).forEach(el => {
  53. props.forEach(prop => el.setAttribute(prop, attrs[prop]));
  54. });
  55. return els
  56. }
  57. /**
  58. * Get any attribute from a single or a list of DOM nodes
  59. * @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
  60. * @param { string|Array } name - name or list of attributes to get
  61. * @returns { Array|string } list of the attributes found
  62. *
  63. * @example
  64. *
  65. * import { get } from 'bianco.attr'
  66. *
  67. * const img = document.createElement('img')
  68. *
  69. * get(img, 'width') // => '200'
  70. *
  71. * // or also
  72. * get(img, ['width', 'height']) // => ['200', '300']
  73. *
  74. * // or also
  75. * get([img1, img2], ['width', 'height']) // => [['200', '300'], ['500', '200']]
  76. */
  77. function get(els, name) {
  78. return parseNodes(els, name, 'getAttribute')
  79. }
  80. /**
  81. * Remove any attribute from a single or a list of DOM nodes
  82. * @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
  83. * @param { string|Array } name - name or list of attributes to remove
  84. * @returns { HTMLElement|NodeList|Array } the original array of elements passed to this function
  85. *
  86. * @example
  87. *
  88. * import { remove } from 'bianco.attr'
  89. *
  90. * remove(img, 'width') // remove the width attribute
  91. *
  92. * // or also
  93. * remove(img, ['width', 'height']) // remove the width and the height attribute
  94. *
  95. * // or also
  96. * remove([img1, img2], ['width', 'height']) // remove the width and the height attribute from both images
  97. */
  98. function remove(els, name) {
  99. return parseNodes(els, name, 'removeAttribute')
  100. }
  101. /**
  102. * Set any attribute on a single or a list of DOM nodes
  103. * @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
  104. * @param { string|Array } name - name or list of attributes to detect
  105. * @returns { boolean|Array } true or false or an array of boolean values
  106. * @example
  107. *
  108. * import { has } from 'bianco.attr'
  109. *
  110. * has(img, 'width') // false
  111. *
  112. * // or also
  113. * has(img, ['width', 'height']) // => [false, false]
  114. *
  115. * // or also
  116. * has([img1, img2], ['width', 'height']) // => [[false, false], [false, false]]
  117. */
  118. function has(els, name) {
  119. return parseNodes(els, name, 'hasAttribute')
  120. }
  121. var index_next = {
  122. get,
  123. set,
  124. remove,
  125. has
  126. };
  127. exports.set = set;
  128. exports.get = get;
  129. exports.remove = remove;
  130. exports.has = has;
  131. exports.default = index_next;