css.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.css = void 0;
  4. var utils_js_1 = require("../utils.js");
  5. /**
  6. * Set multiple CSS properties for every matched element.
  7. *
  8. * @category CSS
  9. * @param prop - The names of the properties.
  10. * @param val - The new values.
  11. * @returns The instance itself.
  12. * @see {@link https://api.jquery.com/css/}
  13. */
  14. function css(prop, val) {
  15. if ((prop != null && val != null) ||
  16. // When `prop` is a "plain" object
  17. (typeof prop === 'object' && !Array.isArray(prop))) {
  18. return (0, utils_js_1.domEach)(this, function (el, i) {
  19. if ((0, utils_js_1.isTag)(el)) {
  20. // `prop` can't be an array here anymore.
  21. setCss(el, prop, val, i);
  22. }
  23. });
  24. }
  25. if (this.length === 0) {
  26. return undefined;
  27. }
  28. return getCss(this[0], prop);
  29. }
  30. exports.css = css;
  31. /**
  32. * Set styles of all elements.
  33. *
  34. * @private
  35. * @param el - Element to set style of.
  36. * @param prop - Name of property.
  37. * @param value - Value to set property to.
  38. * @param idx - Optional index within the selection.
  39. */
  40. function setCss(el, prop, value, idx) {
  41. if (typeof prop === 'string') {
  42. var styles = getCss(el);
  43. var val = typeof value === 'function' ? value.call(el, idx, styles[prop]) : value;
  44. if (val === '') {
  45. delete styles[prop];
  46. }
  47. else if (val != null) {
  48. styles[prop] = val;
  49. }
  50. el.attribs['style'] = stringify(styles);
  51. }
  52. else if (typeof prop === 'object') {
  53. Object.keys(prop).forEach(function (k, i) {
  54. setCss(el, k, prop[k], i);
  55. });
  56. }
  57. }
  58. function getCss(el, prop) {
  59. if (!el || !(0, utils_js_1.isTag)(el))
  60. return;
  61. var styles = parse(el.attribs['style']);
  62. if (typeof prop === 'string') {
  63. return styles[prop];
  64. }
  65. if (Array.isArray(prop)) {
  66. var newStyles_1 = {};
  67. prop.forEach(function (item) {
  68. if (styles[item] != null) {
  69. newStyles_1[item] = styles[item];
  70. }
  71. });
  72. return newStyles_1;
  73. }
  74. return styles;
  75. }
  76. /**
  77. * Stringify `obj` to styles.
  78. *
  79. * @private
  80. * @category CSS
  81. * @param obj - Object to stringify.
  82. * @returns The serialized styles.
  83. */
  84. function stringify(obj) {
  85. return Object.keys(obj).reduce(function (str, prop) { return "".concat(str).concat(str ? ' ' : '').concat(prop, ": ").concat(obj[prop], ";"); }, '');
  86. }
  87. /**
  88. * Parse `styles`.
  89. *
  90. * @private
  91. * @category CSS
  92. * @param styles - Styles to be parsed.
  93. * @returns The parsed styles.
  94. */
  95. function parse(styles) {
  96. styles = (styles || '').trim();
  97. if (!styles)
  98. return {};
  99. var obj = {};
  100. var key;
  101. for (var _i = 0, _a = styles.split(';'); _i < _a.length; _i++) {
  102. var str = _a[_i];
  103. var n = str.indexOf(':');
  104. // If there is no :, or if it is the first/last character, add to the previous item's value
  105. if (n < 1 || n === str.length - 1) {
  106. var trimmed = str.trimEnd();
  107. if (trimmed.length > 0 && key !== undefined) {
  108. obj[key] += ";".concat(trimmed);
  109. }
  110. }
  111. else {
  112. key = str.slice(0, n).trim();
  113. obj[key] = str.slice(n + 1).trim();
  114. }
  115. }
  116. return obj;
  117. }
  118. //# sourceMappingURL=css.js.map