element-case.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module.exports = {
  2. name: 'element-case',
  3. syntax: ['css', 'less', 'sass', 'scss'],
  4. accepts: { string: /^lower|upper$/ },
  5. /**
  6. * Processes tree node.
  7. * @param {node} node
  8. */
  9. process: function(node) {
  10. if (!node.is('selector') &&
  11. !node.is('arguments')) return;
  12. var value = this.getValue('element-case');
  13. node.forEach('simpleSelector', function(selector) {
  14. selector.forEach('ident', function(ident) {
  15. ident.content = value === 'lower' ?
  16. ident.content.toLowerCase() :
  17. ident.content.toUpperCase();
  18. });
  19. });
  20. },
  21. /**
  22. * Detects the value of an option at the tree node.
  23. *
  24. * @param {node} node
  25. */
  26. detect: function(node) {
  27. if (!node.is('selector') &&
  28. !node.is('arguments')) return;
  29. var variants = [];
  30. node.forEach('simpleSelector', function(selector) {
  31. selector.forEach('ident', function(ident) {
  32. if (ident.content.match(/^[a-z]+$/)) {
  33. variants.push('lower');
  34. } else if (ident.content.match(/^[A-Z]+$/)) {
  35. variants.push('upper');
  36. }
  37. });
  38. });
  39. return variants;
  40. }
  41. };