declarationValueIndex.js 700 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. const { isObject, isString } = require('./validateTypes');
  3. /**
  4. * Get the index of a declaration's value
  5. *
  6. * @param {import('postcss').Declaration} decl
  7. * @returns {number}
  8. */
  9. module.exports = function declarationValueIndex(decl) {
  10. const raws = decl.raws;
  11. const prop = raws.prop;
  12. return [
  13. isObject(prop) && 'prefix' in prop && prop.prefix,
  14. (isObject(prop) && 'raw' in prop && prop.raw) || decl.prop,
  15. isObject(prop) && 'suffix' in prop && prop.suffix,
  16. raws.between || ':',
  17. raws.value && 'prefix' in raws.value && raws.value.prefix,
  18. ].reduce((/** @type {number} */ count, str) => {
  19. if (isString(str)) {
  20. return count + str.length;
  21. }
  22. return count;
  23. }, 0);
  24. };