css.js 3.3 KB

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